升级sql构建, 支持or语句

This commit is contained in:
2024-08-09 18:26:30 +08:00
parent 88f9e831d8
commit 7126765fd5
3 changed files with 149 additions and 52 deletions

64
base.go
View File

@ -199,59 +199,19 @@ func (b *BaseDao) setTxCondition(tx *gorm.DB, optionFuncList ...SetOption) *gorm
tx = tx.Offset(o.Offset)
}
}
// 设置where条件
if nil != o.Where && len(o.Where) > 0 {
tx = tx.Where(o.Where)
}
// in 语句
if nil != o.In {
tx = tx.Where(o.In)
}
// not in 语句
if nil != o.NotIn {
for field, value := range o.NotIn {
tx = tx.Where(field+" NOT IN ? ", value)
sqlBlockList := make([]string, 0)
bindValueList := make([]any, 0)
sql, bindVal := optionToSql(o)
tx.Or(sql, bindVal...)
sqlBlockList = append(sqlBlockList, sql)
bindValueList = append(bindValueList, bindVal)
for _, itemOrFuncList := range o.OR {
orOption := &Option{}
for _, fn := range itemOrFuncList {
fn(orOption)
}
}
// like 语句
if nil != o.Like {
for field, value := range o.Like {
tx = tx.Where(field+" LIKE ? ", "%"+value+"%")
}
}
// NOT LIKE 语句
if nil != o.NotLike {
for field, value := range o.NotLike {
tx = tx.Where(field+" NOT LIKE ? ", "%"+value+"%")
}
}
// >=
if nil != o.Start {
for field, value := range o.Start {
tx = tx.Where(field+" >= ?", value)
}
}
// <
if nil != o.End {
for field, value := range o.End {
tx = tx.Where(field+" < ?", value)
}
}
// 排序
for _, orderRule := range o.Order {
tx = tx.Order(orderRule)
}
// or 语句
if len(o.OR) > 0 {
orSql, orBindVal := optionToSql(orOption)
tx.Or(orSql, orBindVal...)
}
return tx
}