代码拉取完成,页面将自动刷新
package wrapper
import (
"gitee.com/h79/goutils/dao/option"
"regexp"
"strings"
)
var nameRegex = regexp.MustCompile("^([a-zA-Z])[a-zA-Z0-9_.]+")
func MustName(name string) string {
if nameRegex.MatchString(name) {
return name
}
return ""
}
func SqlColumn(builder *strings.Builder, column string) bool {
before, after, ok := strings.Cut(column, ".")
if AddQuoted(builder, MustName(before)) {
if ok && after != "" {
if after = MustName(after); after != "" {
builder.WriteByte('.')
AddQuoted(builder, after)
}
}
return true
}
return false
}
func ParseColumn(column string) TableColumn {
var t = strings.Split(column, ".")
switch len(t) {
case 1:
return TableColumn{Col: t[0]}
case 2:
return TableColumn{Name: t[0], Col: t[1]}
}
return TableColumn{}
}
type TableColumn struct {
Name string `form:"name" json:"name"` //表名
Col string `form:"col" json:"col"` //项
}
func (t *TableColumn) Column() string {
var builder = strings.Builder{}
if AddQuoted(&builder, MustName(t.Name)) {
builder.WriteByte('.')
}
AddQuoted(&builder, MustName(t.Col))
return builder.String()
}
type FField string
func (ff FField) Equal(des FField) bool {
return ff == des
}
type Flag struct {
FInt int `form:"flag" json:"flag" yaml:"flag"`
}
type Column struct {
Flag //是否函数
Column string `form:"col" json:"col" yaml:"col"` //项
As string `form:"as" json:"as" yaml:"as"` // 别名
Expr string `form:"expr" json:"expr" yaml:"expr"`
Desc bool `form:"desc" json:"desc" yaml:"desc"` //(降序(大=>小)desc, 升序(小=>大)asc
}
func WithColumn(column string) *Column {
return &Column{
Column: column,
}
}
func WithColumns(cols []string) *Column {
c := &Column{}
c.SetColumn(cols)
return c
}
func (col *Column) GetColumn(opts ...option.Option) []string {
return strings.SplitN(col.Column, ",", option.MaxColumnExist(opts...))
}
func (col *Column) SetColumn(cols []string) {
if len(cols) <= 0 {
col.Column = ""
return
}
col.Column = strings.Join(cols, ",")
}
func (col *Column) With(column string) *Column {
col.Column = column
return col
}
func (col *Column) AS(as string) *Column {
col.As = as
return col
}
func (col *Column) WithFunc(fg int) *Column {
col.FInt = fg
return col
}
func (col *Column) Check(filters []FField) *Column {
var (
filter = func(col string) bool {
for i := range filters {
if filters[i].Equal(FField(col)) {
return true
}
}
return false
}
newCol []string
)
cols := col.GetColumn()
for i := range cols {
if filter(cols[i]) {
newCol = append(newCol, cols[i])
}
}
col.SetColumn(newCol)
return col
}
func (col *Column) Build(convert option.ColumnConvertFunc) string {
builder := strings.Builder{}
if col.Expr != "" {
builder.WriteString(col.Expr)
} else {
fn := SQLFunc(col.FInt)
if len(fn) > 0 {
builder.WriteString(fn)
builder.WriteByte('(')
if convert != nil {
builder.WriteString(convert(col.Column))
} else {
builder.WriteString(col.Column)
}
builder.WriteByte(')')
} else if convert != nil {
AddQuoted(&builder, convert(col.Column))
} else {
AddQuoted(&builder, col.Column)
}
}
AddAlias(&builder, col.As)
return builder.String()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。