Giter VIP home page Giter VIP logo

Comments (5)

caibirdme avatar caibirdme commented on July 24, 2024

不支持哈,gendry只支持生成简单sql。我们也不推荐在生产环境使用orm或者sql builder生成复杂sql,因为你自己手写都觉得sql很复杂了,用了orm或者sql builder之后你真的还能知道生成的sql到底长什么样吗?
所以我们建议在复杂sql的情况下,使用builder.NamedQuery,它能帮你节约一些拼接where in (?,?,?....)的时间

from gendry.

hookover avatar hookover commented on July 24, 2024

不支持哈,gendry只支持生成简单sql。我们也不推荐在生产环境使用orm或者sql builder生成复杂sql,因为你自己手写都觉得sql很复杂了,用了orm或者sql builder之后你真的还能知道生成的sql到底长什么样吗?
所以我们建议在复杂sql的情况下,使用builder.NamedQuery,它能帮你节约一些拼接where in (?,?,?....)的时间

但我的要做的项目是不能提前知道表名称、表类型、表结构的,所以不能提前定义模型。
但又有类似的需求,所以才需要手写SQL拼接组合。
举例来说: update set a=a+1 这种需求常见又合理,为啥会不支持呢
map[string]interface{}{字段: 原生语句} 如果你们有某个函数 比如builder.Raw("a+1")
map[string]interface{}{sec: builder.Raw("a+1")} ==> update xx set a=a+1
map[string]interface{}{sec: builder.Raw("case when b > 0 then 1 else 0 end")} ==> update xx set a=(case when b >0 then 1 else 0 end)
为啥不支持呢?

from gendry.

caibirdme avatar caibirdme commented on July 24, 2024

@hookover 因为gendry默认会把map[string]interface{}{k:v,}中的v用?代替,生成的sql就是select * from tb where a = ?(为了防注入必须prepare)。本质上我可以支持一个类似你说的Raw类型,当识别到是Raw类型时我就什么都不做。可以是可以,但这并不解决问题。如果你的Raw是这样的呢?

map[string]interface{}{
  "id >": builder.Raw("select id from user where city in 值1,值2,...")
}

值怎么填?
进一步你可能希望能够这样:

map[string]interface{}{
  "id >": builder.Raw("select id from user where city in {{cities}} and age < {{age}}", map[string]interface{}{
                                                                     "cities": []string{"Beijing", "Chengdu",},
                                                                     "age": 27,  
                                                                     }),
}

但是这依旧不解决问题,因为你说外层的sql都无法确定,那内层的子查询几乎也无法确定,所以实际上用处不大。
因此还是建议能够用NamedQuery手写,实在不行先用BuildSelect把外层的build好,然后自己找准位置去replace

from gendry.

hookover avatar hookover commented on July 24, 2024

属性的值可以增加两种类型,一种是 builder ,另一种是 Raw
遇到 builder 的时候,解析为子sql语句,这样一来你甚至可以无限嵌套下去
遇到Raw的时候相当于原生语句不做任何处理

再增加这两个功能的话,应该可以实现对绝大部分复杂SQL的构建

个人认为只要ORM或builder足够强大,生产环境使用它们比手写SQL的质效更高。

from gendry.

caibirdme avatar caibirdme commented on July 24, 2024

谢谢,确实是一个比较好的建议。不过基于目前的代码架构,这个改动挺大。后续我们会考虑这个feature的

from gendry.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.