gitee.com/h79/goutils@v1.22.10/dao/wrapper/column.go (about)

     1  package wrapper
     2  
     3  import (
     4  	"fmt"
     5  	commonoption "gitee.com/h79/goutils/common/option"
     6  	"gitee.com/h79/goutils/dao/option"
     7  	"strings"
     8  )
     9  
    10  func SqlColumn(builder *strings.Builder, column string) bool {
    11  	before, after, ok := strings.Cut(column, ".")
    12  	if ok && IsQuoted(before) && IsQuoted(after) {
    13  		builder.WriteString(column)
    14  		return true
    15  	}
    16  	if !AddQuoted(builder, before) {
    17  		return false
    18  	}
    19  	if ok && after != "" {
    20  		builder.WriteByte('.')
    21  		AddQuoted(builder, after)
    22  	}
    23  	return true
    24  }
    25  
    26  func ParseColumn(column string) TableColumn {
    27  	var t = strings.Split(column, ".")
    28  	switch len(t) {
    29  	case 1:
    30  		return TableColumn{Col: t[0]}
    31  	case 2:
    32  		return TableColumn{Name: t[0], Col: t[1]}
    33  	}
    34  	return TableColumn{}
    35  }
    36  
    37  type TableColumn struct {
    38  	Name string `form:"name" json:"name"` //表名
    39  	Col  string `form:"col" json:"col"`   //项
    40  }
    41  
    42  func (t *TableColumn) Column() string {
    43  	var builder = strings.Builder{}
    44  	if AddQuoted(&builder, t.Name) {
    45  		builder.WriteByte('.')
    46  	}
    47  	AddQuoted(&builder, t.Col)
    48  	return builder.String()
    49  }
    50  
    51  type FField string
    52  
    53  func (ff FField) Equal(des FField) bool {
    54  	return ff == des
    55  }
    56  
    57  type Flag struct {
    58  	FInt int `form:"flag" json:"flag" yaml:"flag"`
    59  }
    60  
    61  type Column struct {
    62  	Flag   //是否函数
    63  	Table  string
    64  	Column string `form:"col" json:"col" yaml:"col"` //项
    65  	As     string `form:"as" json:"as" yaml:"as"`    // 别名
    66  	Expr   string `form:"expr" json:"expr" yaml:"expr"`
    67  	Desc   bool   `form:"desc" json:"desc" yaml:"desc"` //(降序(大=>小)desc, 升序(小=>大)asc
    68  }
    69  
    70  func WithTable(table string) *Column {
    71  	return &Column{
    72  		Table: table,
    73  	}
    74  }
    75  
    76  func WithColumn(column string) *Column {
    77  	return &Column{
    78  		Column: column,
    79  	}
    80  }
    81  
    82  func WithColumns(cols []string) *Column {
    83  	c := &Column{}
    84  	c.SetColumn(cols)
    85  	return c
    86  }
    87  
    88  func (col *Column) GetColumn(opts ...commonoption.Option) []string {
    89  	return strings.SplitN(col.Column, ",", option.MaxColumnExist(opts...))
    90  }
    91  
    92  func (col *Column) SetColumn(cols []string) {
    93  	if len(cols) <= 0 {
    94  		col.Column = ""
    95  		return
    96  	}
    97  	col.Column = strings.Join(cols, ",")
    98  }
    99  
   100  func (col *Column) WithTable(table string) *Column {
   101  	col.Table = table
   102  	return col
   103  }
   104  
   105  func (col *Column) With(column string) *Column {
   106  	col.Column = column
   107  	return col
   108  }
   109  
   110  func (col *Column) AS(as string) *Column {
   111  	col.As = as
   112  	return col
   113  }
   114  
   115  func (col *Column) WithFunc(fg int) *Column {
   116  	col.FInt = fg
   117  	return col
   118  }
   119  
   120  func (col *Column) Check(filters []FField) *Column {
   121  	var (
   122  		filter = func(col string) bool {
   123  			for i := range filters {
   124  				if filters[i].Equal(FField(col)) {
   125  					return true
   126  				}
   127  			}
   128  			return false
   129  		}
   130  		newCol []string
   131  	)
   132  	cols := col.GetColumn()
   133  	for i := range cols {
   134  		if filter(cols[i]) {
   135  			newCol = append(newCol, cols[i])
   136  		}
   137  	}
   138  	col.SetColumn(newCol)
   139  	return col
   140  }
   141  
   142  func (col *Column) Build(convert option.ColumnConvertFunc) string {
   143  
   144  	builder := strings.Builder{}
   145  	if col.Expr != "" {
   146  		builder.WriteString(col.Expr)
   147  	} else {
   148  		fn := SQLFunc(col.FInt)
   149  		if len(fn) > 0 {
   150  			builder.WriteString(fn)
   151  			builder.WriteByte('(')
   152  			if convert != nil {
   153  				AddTable(&builder, col.Table)
   154  				AddQuoted(&builder, convert(col.Column))
   155  			} else {
   156  				AddTable(&builder, col.Table)
   157  				AddQuoted(&builder, col.Column)
   158  			}
   159  			builder.WriteByte(')')
   160  		} else if convert != nil {
   161  			AddTable(&builder, col.Table)
   162  			AddQuoted(&builder, convert(col.Column))
   163  		} else {
   164  			AddTable(&builder, col.Table)
   165  			AddQuoted(&builder, col.Column)
   166  		}
   167  	}
   168  	AddAlias(&builder, col.As)
   169  	return builder.String()
   170  }
   171  
   172  func ColumnName[T ~string](column T) string {
   173  	return string(column)
   174  }
   175  
   176  func TColumnName[T ~string](table string, column T) string {
   177  	return fmt.Sprintf("`%s`.`%s`", table, column)
   178  }
   179  
   180  func ColumnAs[T ~string](column T, as string) string {
   181  	if as != "" {
   182  		return fmt.Sprintf("`%s` as %s", column, as)
   183  	}
   184  	return fmt.Sprintf("`%s`", column)
   185  }
   186  
   187  func TColumnAs[T ~string](table string, column T, as string) string {
   188  	if as != "" {
   189  		return fmt.Sprintf("`%s`.`%s` as %s", table, column, as)
   190  	}
   191  	return fmt.Sprintf("`%s`.`%s`", table, column)
   192  }