github.com/kunlun-qilian/sqlx/v3@v3.0.0/builder/def_column.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"strings"
     7  
     8  	"github.com/go-courier/x/types"
     9  )
    10  
    11  func Col(name string) *Column {
    12  	return &Column{
    13  		Name:       strings.ToLower(name),
    14  		ColumnType: &ColumnType{},
    15  	}
    16  }
    17  
    18  var _ TableDefinition = (*Column)(nil)
    19  
    20  type Column struct {
    21  	Name      string
    22  	FieldName string
    23  	Table     *Table
    24  	exactly   bool
    25  
    26  	*ColumnType
    27  }
    28  
    29  func (c Column) Full() *Column {
    30  	c.exactly = true
    31  	return &c
    32  }
    33  
    34  func (c *Column) Of(table *Table) *Column {
    35  	col := &Column{
    36  		Name:       c.Name,
    37  		FieldName:  c.FieldName,
    38  		Table:      table,
    39  		exactly:    true,
    40  		ColumnType: c.ColumnType,
    41  	}
    42  	return col
    43  }
    44  
    45  func (c *Column) IsNil() bool {
    46  	return c == nil
    47  }
    48  
    49  func (c *Column) Ex(ctx context.Context) *Ex {
    50  	toggles := TogglesFromContext(ctx)
    51  	if c.Table != nil && (c.exactly || toggles.Is(ToggleMultiTable)) {
    52  		if toggles.Is(ToggleNeedAutoAlias) {
    53  			return Expr("?.? AS ?", c.Table, Expr(c.Name), Expr(c.Name)).Ex(ctx)
    54  		}
    55  		return Expr("?.?", c.Table, Expr(c.Name)).Ex(ctx)
    56  	}
    57  	return ExactlyExpr(c.Name).Ex(ctx)
    58  }
    59  
    60  func (c *Column) Expr(query string, args ...interface{}) *Ex {
    61  	n := len(args)
    62  	e := Expr("")
    63  	e.Grow(n)
    64  
    65  	qc := 0
    66  
    67  	for _, key := range []byte(query) {
    68  		switch key {
    69  		case '#':
    70  			e.WriteExpr(c)
    71  		case '?':
    72  			e.WriteQueryByte(key)
    73  			if n > qc {
    74  				e.AppendArgs(args[qc])
    75  				qc++
    76  			}
    77  		default:
    78  			e.WriteQueryByte(key)
    79  		}
    80  	}
    81  
    82  	return e
    83  }
    84  
    85  func (c Column) Field(fieldName string) *Column {
    86  	c.FieldName = fieldName
    87  	return &c
    88  }
    89  
    90  func (c Column) Type(v interface{}, tagValue string) *Column {
    91  	c.ColumnType = ColumnTypeFromTypeAndTag(types.FromRType(reflect.TypeOf(v)), tagValue)
    92  	return &c
    93  }
    94  
    95  func (c Column) On(table *Table) *Column {
    96  	c.Table = table
    97  	return &c
    98  }
    99  
   100  func (c *Column) T() *Table {
   101  	return c.Table
   102  }
   103  
   104  func (c *Column) ValueBy(v interface{}) *Assignment {
   105  	return ColumnsAndValues(c, v)
   106  }
   107  
   108  func (c *Column) Incr(d int) SqlExpr {
   109  	return Expr("? + ?", c, d)
   110  }
   111  
   112  func (c *Column) Dec(d int) SqlExpr {
   113  	return Expr("? - ?", c, d)
   114  }
   115  
   116  func (c *Column) Like(v string) SqlCondition {
   117  	return AsCond(Expr("? LIKE ?", c, "%"+v+"%"))
   118  }
   119  
   120  func (c *Column) LeftLike(v string) SqlCondition {
   121  	return AsCond(Expr("? LIKE ?", c, "%"+v))
   122  }
   123  
   124  func (c *Column) RightLike(v string) SqlCondition {
   125  	return AsCond(Expr("? LIKE ?", c, v+"%"))
   126  }
   127  
   128  func (c *Column) NotLike(v string) SqlCondition {
   129  	return AsCond(Expr("? NOT LIKE ?", c, "%"+v+"%"))
   130  }
   131  
   132  func (c *Column) IsNull() SqlCondition {
   133  	return AsCond(Expr("? IS NULL", c))
   134  }
   135  
   136  func (c *Column) IsNotNull() SqlCondition {
   137  	return AsCond(Expr("? IS NOT NULL", c))
   138  }
   139  
   140  func (c *Column) Between(leftValue interface{}, rightValue interface{}) SqlCondition {
   141  	return AsCond(Expr("? BETWEEN ? AND ?", c, leftValue, rightValue))
   142  }
   143  
   144  func (c *Column) NotBetween(leftValue interface{}, rightValue interface{}) SqlCondition {
   145  	return AsCond(Expr("? NOT BETWEEN ? AND ?", c, leftValue, rightValue))
   146  }
   147  
   148  type WithConditionFor interface {
   149  	ConditionFor(c *Column) SqlCondition
   150  }
   151  
   152  func (c *Column) In(args ...interface{}) SqlCondition {
   153  	n := len(args)
   154  
   155  	switch n {
   156  	case 0:
   157  		return nil
   158  	case 1:
   159  		if withConditionFor, ok := args[0].(WithConditionFor); ok {
   160  			return withConditionFor.ConditionFor(c)
   161  		}
   162  	}
   163  
   164  	e := Expr("? IN ")
   165  
   166  	e.Grow(n + 1)
   167  
   168  	e.AppendArgs(c)
   169  
   170  	e.WriteGroup(func(e *Ex) {
   171  		for i := 0; i < n; i++ {
   172  			e.WriteHolder(i)
   173  		}
   174  	})
   175  
   176  	e.AppendArgs(args...)
   177  
   178  	return AsCond(e)
   179  }
   180  
   181  func (c *Column) NotIn(args ...interface{}) SqlCondition {
   182  	n := len(args)
   183  	if n == 0 {
   184  		return nil
   185  	}
   186  
   187  	e := Expr("")
   188  	e.Grow(n + 1)
   189  
   190  	e.WriteQuery("? NOT IN ")
   191  	e.AppendArgs(c)
   192  
   193  	e.WriteGroup(func(e *Ex) {
   194  		for i := 0; i < n; i++ {
   195  			e.WriteHolder(i)
   196  		}
   197  	})
   198  
   199  	e.AppendArgs(args...)
   200  
   201  	return AsCond(e)
   202  }
   203  
   204  func (c *Column) Eq(v interface{}) SqlCondition {
   205  	return AsCond(Expr("? = ?", c, v))
   206  }
   207  
   208  func (c *Column) Neq(v interface{}) SqlCondition {
   209  	return AsCond(Expr("? <> ?", c, v))
   210  }
   211  
   212  func (c *Column) Gt(v interface{}) SqlCondition {
   213  	return AsCond(Expr("? > ?", c, v))
   214  }
   215  
   216  func (c *Column) Gte(v interface{}) SqlCondition {
   217  	return AsCond(Expr("? >= ?", c, v))
   218  }
   219  
   220  func (c *Column) Lt(v interface{}) SqlCondition {
   221  	return AsCond(Expr("? < ?", c, v))
   222  }
   223  
   224  func (c *Column) Lte(v interface{}) SqlCondition {
   225  	return AsCond(Expr("? <= ?", c, v))
   226  }