didi / gendry

a golang library for sql builder
Apache License 2.0
1.62k stars 195 forks source link

Where 过滤能否单独开放出来 #114

Closed Colstuwjx closed 3 years ago

Colstuwjx commented 3 years ago

目前我们使用 where ( 一个 map[string]interface{} )一般是配套 builder 的,比如 builder.BuildSelect(table, where, selectFields) 带上一个 where 的 map。我这边有个需求是想使用 NamedQuery 做一个复杂查询,但是这样一来,where 查询部分也就不能通过 map 来表现了。

是否可以通过将 whereConnector 这样的方法或者搞个类似的公开方法,将 where map 转换为具体 SQL 的这块功能开放给用户?这样的话,NamedQuery 也可以传入一个 where map 来做查询了。

希望能够考量一下是否可行!谢谢!

caibirdme commented 3 years ago

你是说想实现类似于这样的效果吗:

NamedQuery(`select * from tb1 join tb2 where {{condition}}`, map[string]interface{}{
    "condition": WhereBuilder(whereMap),
})
Colstuwjx commented 3 years ago

@caibirdme 是的!

caibirdme commented 3 years ago

应该是可以转成public方法的,我看看

Colstuwjx commented 3 years ago

简单写了一下: 3058cf281f17, 但是仔细一想发现 NamedQuery 用的是 {{ xxx }} + map[string]interface{}{} 的写法来组合出最终语句的,而 BuildWhere 的话会直接产出 condvalues

感觉还是维持原样好一些吧,开放出构造 Where 子句的方法并没有真正需要消费它的地方。