1 Star 0 Fork 0

tech4good/common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
condition.go 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
v_chaoni 提交于 2022-04-08 11:31 +08:00 . 1
package db
import (
"errors"
"fmt"
"regexp"
"strings"
"gitee.com/tech4good/common/utils"
)
// BuildCondition 根据condition生成where条件
func BuildCondition(scons []SqlCondition, fcons []FieldCondition) (string, []interface{}, error) {
sql := ""
pattern := "^[a-z0-9A-Z_\\.]+$"
var params []interface{}
// 添加sql条件
for _, con := range scons {
sql += " and (" + con.Sql + ")"
for _, item := range con.Values {
params = append(params, item)
}
}
// 添加字段条件
for _, con := range fcons {
if ok, _ := regexp.MatchString(pattern, con.Field); !ok {
return "", params, errors.New("列名错误:" + con.Field)
}
tableAlias := ""
if con.TableName != "" {
tableAlias = con.TableName + "."
}
if con.MatchType == Fuzzy {
sql += fmt.Sprintf(" and upper(%s`%s`) like ? ", tableAlias, con.Field)
params = append(params, "%"+strings.ToUpper(fmt.Sprintf("%v", con.Value))+"%")
} else if con.MatchType == Accurate || con.MatchType == "" {
sql += fmt.Sprintf(" and %s`%s` = ? ", tableAlias, con.Field)
params = append(params, con.Value)
} else if con.MatchType == Not {
sql += fmt.Sprintf(" and %s`%s` != ? ", tableAlias, con.Field)
params = append(params, con.Value)
} else if con.MatchType == In {
sql += fmt.Sprintf(" and %s`%s` in (?) ", tableAlias, con.Field)
params = append(params, utils.Split(con.Value, ";", ","))
} else if con.MatchType == Larger {
sql += fmt.Sprintf(" and %s`%s` > (?) ", tableAlias, con.Field)
params = append(params, con.Value)
} else if con.MatchType == LargerOrEqual {
sql += fmt.Sprintf(" and %s`%s` >= (?) ", tableAlias, con.Field)
params = append(params, con.Value)
} else if con.MatchType == Smaller {
sql += fmt.Sprintf(" and %s`%s` < (?) ", tableAlias, con.Field)
params = append(params, con.Value)
} else if con.MatchType == SmallerOrEqual {
sql += fmt.Sprintf(" and %s`%s` <= (?) ", tableAlias, con.Field)
params = append(params, con.Value)
} else if con.MatchType == Bwt {
sql += fmt.Sprintf(" and ( %s`%s` between ? and ? ) ", tableAlias, con.Field)
condition := fmt.Sprintf("%v", con.Value)
values := utils.Split(condition, ";", ",")
var start, end string
if len(values) >= 2 {
start = values[0]
end = values[1]
} else {
start = con.Value
end = ""
}
params = append(params, start)
params = append(params, end)
}
}
return sql, params, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tech4good/common.git
git@gitee.com:tech4good/common.git
tech4good
common
common
v1.0.2

搜索帮助