1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
column.go 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-01-15 01:52 . time
package wrapper
import "strings"
type FField string
func (ff FField) Equal(des FField) bool {
return ff == des
}
type Flag struct {
FInt int `form:"flag" json:"flag"`
}
type Column struct {
Flag
Column string `form:"col" json:"col"`
As string `form:"-" json:"-"`
Desc bool `form:"desc" json:"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() []string {
return strings.SplitN(col.Column, ",", maxColumns)
}
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(newCol string) *Column {
if newCol == "" {
col.As = col.Column
} else {
col.As = newCol
}
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 defColumnConvert(col string) string {
return col
}
type ColumnConvert func(col string) string
func (col *Column) Sql(convert ColumnConvert) string {
if convert == nil {
convert = defColumnConvert
}
builder := strings.Builder{}
fn := SQLFunc(col.FInt)
if len(fn) > 0 {
builder.WriteString(fn)
builder.WriteByte('(')
}
builder.WriteString(convert(col.Column))
if len(fn) > 0 {
builder.WriteByte(')')
}
if len(col.As) > 0 {
builder.WriteString(" AS ")
builder.WriteString(col.As)
}
return builder.String()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.6.5

搜索帮助