代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。