1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sql.go 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-11-28 00:57 . db cond
package wrapper
import "strings"
type SQL struct {
selector ISelect
where IQuery
from string
as string
groupBy string
}
func NewSQL(selector ISelect, from string) *SQL {
return &SQL{
selector: selector,
from: from,
}
}
func (s *SQL) Where(where IQuery) *SQL {
s.where = where
return s
}
func (s *SQL) GroupBy(by string) *SQL {
s.groupBy = by
return s
}
func (s *SQL) AS(table string) *SQL {
s.as = table
return s
}
func (s *SQL) Build() string {
if s.selector == nil || len(s.from) == 0 {
return ""
}
builder := strings.Builder{}
if len(s.as) > 0 {
builder.WriteByte('(')
}
builder.WriteString(s.selector.Sql(true))
builder.WriteString(" FROM ")
builder.WriteString(s.from)
if s.where != nil {
builder.WriteString(s.where.Sql(true))
}
if len(s.groupBy) > 0 {
builder.WriteString(s.groupBy)
}
if len(s.as) > 0 {
builder.WriteString(") AS ")
builder.WriteString(s.as)
}
return builder.String()
}
func SQLFunc(flag int) string {
if flag <= KSQLFunc {
return ""
}
if fn, ok := sqlFuncMap[flag]; ok {
return fn
}
return ""
}
const (
KSQLFunc = iota
KSQLCount
KSQLMax
KSQLMin
KSQLWeek
KSQLYear
KSQLMonth
KSQLSum
KSQLAbs
KSQLFloor
KSQLGreatest
KSQLLeast
KSQLUpper
KSQLLower
KSQLTrim
KSQLRTrim
KSQLLTrim
KSQLCharLength
KSQLConcat
KSQLReverse
)
var (
sqlFuncMap = map[int]string{
KSQLCount: "COUNT",
KSQLMax: "MAX",
KSQLMin: "MIN",
KSQLWeek: "WEEK",
KSQLYear: "YEAR",
KSQLMonth: "MONTH",
KSQLSum: "SUM",
KSQLAbs: "ABS",
KSQLFloor: "FLOOR", //返回小于或等于 x 的最大整数
KSQLGreatest: "GREATEST",
KSQLLeast: "LEAST",
KSQLUpper: "UPPER", //字符串大写
KSQLLower: "LOWER",
KSQLTrim: "TRIM",
KSQLRTrim: "RTRIM",
KSQLLTrim: "LTRIM",
KSQLCharLength: "CHAR_LENGTH",
KSQLConcat: "CONCAT",
KSQLReverse: "REVERSE", //将字符串s的顺序反过来
}
)
func AddSQLFunc(flag int, fn string) {
if _, ok := sqlFuncMap[flag]; ok {
return
}
sqlFuncMap[flag] = fn
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.6.5

搜索帮助