github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_def_col.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"strings"
     7  
     8  	"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
     9  )
    10  
    11  type Column struct {
    12  	Name      string
    13  	FieldName string
    14  	Table     *Table
    15  	exactly   bool
    16  
    17  	*ColumnType
    18  }
    19  
    20  func Col(name string) *Column {
    21  	return &Column{Name: strings.ToLower(name), ColumnType: &ColumnType{}}
    22  }
    23  
    24  func (c *Column) IsNil() bool { return c == nil }
    25  
    26  func (c *Column) Ex(ctx context.Context) *Ex {
    27  	toggles := TogglesFromContext(ctx)
    28  	if c.Table != nil && (c.exactly || toggles.Is(ToggleMultiTable)) {
    29  		if toggles.Is(ToggleNeedAutoAlias) {
    30  			return Expr("?.? AS ?", c.Table, Expr(c.Name), Expr(c.Name)).Ex(ctx)
    31  		}
    32  		return Expr("?.?", c.Table, Expr(c.Name)).Ex(ctx)
    33  	}
    34  	return ExactlyExpr(c.Name).Ex(ctx)
    35  }
    36  
    37  func (c *Column) Expr(query string, args ...interface{}) *Ex {
    38  	n, e := len(args), Expr("")
    39  	e.Grow(n)
    40  
    41  	idx := 0
    42  
    43  	for _, b := range []byte(query) {
    44  		switch b {
    45  		case '#':
    46  			e.WriteExpr(c)
    47  		case '?':
    48  			e.WriteQueryByte(b)
    49  			if n > idx {
    50  				e.AppendArgs(args[idx])
    51  				idx++
    52  			}
    53  		default:
    54  			e.WriteQueryByte(b)
    55  		}
    56  	}
    57  	return e
    58  }
    59  
    60  func (c Column) Of(t *Table) *Column {
    61  	return &Column{
    62  		Name:       c.Name,
    63  		FieldName:  c.FieldName,
    64  		Table:      t,
    65  		exactly:    true,
    66  		ColumnType: c.ColumnType,
    67  	}
    68  }
    69  
    70  func (c Column) Type(v interface{}, tag string) *Column {
    71  	c.ColumnType = AnalyzeColumnType(typesx.FromReflectType(reflect.TypeOf(v)), tag)
    72  	return &c
    73  }
    74  
    75  func (c Column) Field(name string) *Column { c.FieldName = name; return &c }
    76  
    77  func (c Column) On(t *Table) *Column { c.Table = t; return &c }
    78  
    79  func (c *Column) T() *Table { return c.Table }
    80  
    81  func (c *Column) ValueBy(v interface{}) *Assignment { return ColumnsAndValues(c, v) }
    82  
    83  func (c *Column) Inc(d int) SqlExpr { return Expr("?+?", c, d) }
    84  
    85  func (c *Column) Dec(d int) SqlExpr { return Expr("?-?", c, d) }
    86  
    87  func (c *Column) Like(v string) SqlCondition { return AsCond(Expr("? LIKE ?", c, "%"+v+"%")) }
    88  
    89  func (c *Column) LLike(v string) SqlCondition { return AsCond(Expr("? LIKE ?", c, "%"+v)) }
    90  
    91  func (c *Column) RLike(v string) SqlCondition { return AsCond(Expr("? LIKE ?", c, v+"%")) }
    92  
    93  func (c *Column) NotLike(v string) SqlCondition { return AsCond(Expr("? NOT LIKE ?", c, "%"+v+"%")) }
    94  
    95  func (c *Column) IsNull() SqlCondition { return AsCond(Expr("? IS NULL", c)) }
    96  
    97  func (c *Column) IsNotNull() SqlCondition { return AsCond(Expr("? IS NOT NULL", c)) }
    98  
    99  func (c *Column) Eq(v interface{}) SqlCondition { return AsCond(Expr("? = ?", c, v)) }
   100  
   101  func (c *Column) Neq(v interface{}) SqlCondition { return AsCond(Expr("? <> ?", c, v)) }
   102  
   103  func (c *Column) Gt(v interface{}) SqlCondition { return AsCond(Expr("? > ?", c, v)) }
   104  
   105  func (c *Column) Gte(v interface{}) SqlCondition { return AsCond(Expr("? >= ?", c, v)) }
   106  
   107  func (c *Column) Lt(v interface{}) SqlCondition { return AsCond(Expr("? < ?", c, v)) }
   108  
   109  func (c *Column) Lte(v interface{}) SqlCondition { return AsCond(Expr("? <= ?", c, v)) }
   110  
   111  func (c *Column) Between(l, r interface{}) SqlCondition {
   112  	return AsCond(Expr("? BETWEEN ? AND ?", c, l, r))
   113  }
   114  
   115  func (c *Column) NotBetween(l, r interface{}) SqlCondition {
   116  	return AsCond(Expr("? NOT BETWEEN ? AND ?", c, l, r))
   117  }
   118  
   119  func (c *Column) In(args ...interface{}) SqlCondition {
   120  	n := len(args)
   121  	if n == 0 {
   122  		return nil
   123  	}
   124  	if n == 1 {
   125  		_ = n
   126  		// TODO WithConditionFor this column
   127  	}
   128  	e := Expr("? IN ")
   129  	e.Grow(n + 1)
   130  	e.AppendArgs(c)
   131  	e.WriteGroup(func(e *Ex) {
   132  		for i := 0; i < n; i++ {
   133  			e.WriteHolder(i)
   134  		}
   135  	})
   136  	e.AppendArgs(args...)
   137  	return AsCond(e)
   138  }
   139  
   140  func (c *Column) NotIn(args ...interface{}) SqlCondition {
   141  	n := len(args)
   142  	if n == 0 {
   143  		return nil
   144  	}
   145  	e := Expr("? NOT IN ")
   146  	e.Grow(n + 1)
   147  	e.AppendArgs(c)
   148  	e.WriteGroup(func(e *Ex) {
   149  		for i := 0; i < n; i++ {
   150  			e.WriteHolder(i)
   151  		}
   152  	})
   153  	e.AppendArgs(args...)
   154  	return AsCond(e)
   155  }