代码拉取完成,页面将自动刷新
package wrapper
import (
"gitee.com/h79/goutils/dao/option"
"strings"
)
type SQL struct {
From
Selector ISelect
Where IQuery
Join IBuilder
GroupBy IBuilder
OrderBy IBuilder
Limit ILimit
Opts []option.Option
}
// NewSQL From 表名
func NewSQL(from From, opts ...option.Option) *SQL {
return &SQL{
From: from,
Opts: opts,
}
}
func (s *SQL) WithSelector(sel ISelect) *SQL {
s.Selector = sel
return s
}
func (s *SQL) WithJoin(join IBuilder) *SQL {
s.Join = join
return s
}
func (s *SQL) WithWhere(where IQuery) *SQL {
s.Where = where
return s
}
func (s *SQL) WithGroupBy(by IBuilder) *SQL {
s.GroupBy = by
return s
}
func (s *SQL) WithLimit(limit ILimit) *SQL {
s.Limit = limit
return s
}
func (s *SQL) WithOrderBy(by IBuilder) *SQL {
s.OrderBy = by
return s
}
// AsSubQuery 作为子查询使用
func (s *SQL) AsSubQuery(as string) *SQL {
s.Opts = append(s.Opts, option.WithSubQuerySQL(as))
return s
}
// AsChild 作为子源来使用
func (s *SQL) AsChild(as string, opts ...option.Option) From {
return From{From: s.Build(opts...), child: true, As: as}
}
func (s *SQL) Build(opts ...option.Option) string {
opts = append(opts, option.WithFullSQL())
return s.BuildOpt(opts...)
}
func (s *SQL) BuildOpt(opts ...option.Option) string {
opts = append(opts, s.Opts...)
as, sq := option.SubQueryExist(opts...)
builder := strings.Builder{}
if sq {
builder.WriteByte('(')
}
if s.Selector != nil {
builder.WriteString(s.Selector.Build(opts...))
}
builder.WriteString(s.From.Build(opts...))
if s.Join != nil {
builder.WriteString(s.Join.Build(opts...))
}
if s.Where != nil {
builder.WriteString(s.Where.Build(opts...))
}
if s.GroupBy != nil {
builder.WriteString(s.GroupBy.Build(opts...))
}
// order by ...limit...
if s.OrderBy != nil {
builder.WriteString(s.OrderBy.Build(opts...))
}
if s.Limit != nil {
builder.WriteString(s.Limit.Build(opts...))
}
if sq {
builder.WriteByte(')')
if as != "" {
builder.WriteString(" AS ")
builder.WriteString(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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。