gogf / gf

GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
https://goframe.org
MIT License
11.69k stars 1.59k forks source link

增强gf的orm模块,提供sqlManager来管理所有动态sql语句 #374

Closed fxk2006 closed 1 year ago

fxk2006 commented 5 years ago

--user.md文件

getUserBy
===
*注释
select *
from t_user
where 1=1
{{if .Name}}
    and name=@Name
{{endif}}
{{if .MaxAge}}
    and age<@MaxAge
{{endif}}
{{if .MinAge}}
    and age>@MinAge
        or name=@Name
{{endif}}

updateUser
===
...

deleteUser
===
...

第一步:解析这个md 文件,把名称和语句解析到一个map中 第二步:调用方式 db.ExecTpl("getUserBy",&obj)

    处理过程示例一:
    当obj只传Name属性值时,生成语句和参数列表
    select * from t_user where 1=1 and name=?, ["张三"]

    当obj只传Name和MaxAge属性值时,生成语句和参数列表
    select * from t_user where 1=1 and name=? and age<? or name=?, ["张三", 12, "张三"]

           处理过程示例二:
           当obj只传Name属性值时,生成语句和参数map
    select * from t_user where 1=1 and name=@Name, {Name:"张三"}

    当obj只传Name和MaxAge属性值时,生成语句和参数map
    select * from t_user where 1=1 and name=@Name and age<@MaxAge or name=@Name,  {Name:"张三",MaxAge:12}
    后续步骤参考https://godoc.org/database/sql#Named

image

参考java实现方式

gqcn commented 5 years ago

@fxk2006 哈哈,这个一看就是从java那边参考过来的,建议不错的,不过优先级放低一些,如果能够小伙伴提供PR更好。

fxk2006 commented 5 years ago

@johngcn 这里有一个实现有例子,还不错 https://github.com/pinzolo/sqlt

zcool321 commented 4 years ago
  1. 单表操作链式就很不错
  2. 多表联查建议直接写sql,可以放到文件里面更好 参考1beetlSQL:https://www.kancloud.cn/xiandafu/beetlsql3_guide/1958111
    
    select
    ===
    select * from sys_user where id=#{id}

update

update sys_user set status=1 whre id=#{id}

参考2 jfinal:http://www.jfinal.com/doc/5-13
```sql
#sql("findGirl")
  select * from girl where age > ? and age < ? and weight < 50
#end

参考3 mybatis主流,但是历史原因还是xml

<select id="findUserList" parameterType="user" resultType="user">
    select * from user  where 1=1
        <if test="id!=null and id!=''">
            and id=#{id}
        </if>
        <if test="username!=null and username!=''">
            and username like '%${username}%'
        </if>
</select>

其实都是基本模板引擎,建议markdown形式,看的时候直观