github.com/RevenueMonster/sqlike@v1.0.6/sql/expr/expression.go (about)

     1  package expr
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/RevenueMonster/sqlike/reflext"
     7  	"github.com/RevenueMonster/sqlike/sqlike/primitive"
     8  )
     9  
    10  // Equal :
    11  func Equal(field, value interface{}) (c primitive.C) {
    12  	c = clause(field, primitive.Equal, value)
    13  	return
    14  }
    15  
    16  // NotEqual :
    17  func NotEqual(field, value interface{}) (c primitive.C) {
    18  	c = clause(field, primitive.NotEqual, value)
    19  	return
    20  }
    21  
    22  // IsNull :
    23  func IsNull(field string) (c primitive.Nil) {
    24  	c.Field = wrapColumn(field)
    25  	c.IsNot = true
    26  	return
    27  }
    28  
    29  // NotNull :
    30  func NotNull(field string) (c primitive.Nil) {
    31  	c.Field = wrapColumn(field)
    32  	return
    33  }
    34  
    35  // In :
    36  func In(field, values interface{}) (c primitive.C) {
    37  	c = inGroup(field, primitive.In, values)
    38  	return
    39  }
    40  
    41  // NotIn :
    42  func NotIn(field, values interface{}) (c primitive.C) {
    43  	c = inGroup(field, primitive.NotIn, values)
    44  	return
    45  }
    46  
    47  // Like :
    48  func Like(field, value interface{}) (p primitive.L) {
    49  	p.Field = wrapColumn(field)
    50  	p.Value = value
    51  	return
    52  }
    53  
    54  // NotLike :
    55  func NotLike(field, value interface{}) (p primitive.L) {
    56  	p.Field = wrapColumn(field)
    57  	p.IsNot = true
    58  	p.Value = value
    59  	return
    60  }
    61  
    62  // GreaterOrEqual :
    63  func GreaterOrEqual(field, value interface{}) (c primitive.C) {
    64  	c = clause(field, primitive.GreaterOrEqual, value)
    65  	return
    66  }
    67  
    68  // GreaterThan :
    69  func GreaterThan(field, value interface{}) (c primitive.C) {
    70  	c = clause(field, primitive.GreaterThan, value)
    71  	return
    72  }
    73  
    74  // LesserOrEqual :
    75  func LesserOrEqual(field, value interface{}) (c primitive.C) {
    76  	c = clause(field, primitive.LesserOrEqual, value)
    77  	return
    78  }
    79  
    80  // LesserThan :
    81  func LesserThan(field, value interface{}) (c primitive.C) {
    82  	c = clause(field, primitive.LesserThan, value)
    83  	return
    84  }
    85  
    86  // Between :
    87  func Between(field, from, to interface{}) (c primitive.C) {
    88  	c = clause(field, primitive.Between, primitive.R{From: from, To: to})
    89  	return
    90  }
    91  
    92  // NotBetween :
    93  func NotBetween(field, from, to interface{}) (c primitive.C) {
    94  	c = clause(field, primitive.NotBetween, primitive.R{From: from, To: to})
    95  	return
    96  }
    97  
    98  // And :
    99  func And(conds ...interface{}) (g primitive.Group) {
   100  	g = buildGroup(primitive.And, conds)
   101  	return
   102  }
   103  
   104  // Or :
   105  func Or(conds ...interface{}) (g primitive.Group) {
   106  	g = buildGroup(primitive.Or, conds)
   107  	return
   108  }
   109  
   110  func buildGroup(op primitive.Operator, conds []interface{}) (g primitive.Group) {
   111  	length := len(conds)
   112  	if length < 1 {
   113  		return
   114  	}
   115  	if length == 1 {
   116  		x, ok := conds[0].(primitive.Group)
   117  		if ok {
   118  			g = x
   119  			return
   120  		}
   121  	}
   122  
   123  	sg := make([]interface{}, 0, length)
   124  	for len(conds) > 0 {
   125  		cond := conds[0]
   126  		conds = conds[1:]
   127  
   128  		if cond == nil || reflext.IsZero(reflext.ValueOf(cond)) {
   129  			continue
   130  		}
   131  
   132  		if len(sg) > 0 {
   133  			sg = append(sg, op)
   134  		}
   135  		sg = append(sg, cond)
   136  	}
   137  	if len(sg) > 1 {
   138  		g.Values = append(g.Values, Raw("("))
   139  		g.Values = append(g.Values, sg...)
   140  		g.Values = append(g.Values, Raw(")"))
   141  		return
   142  	}
   143  	g.Values = append(g.Values, sg...)
   144  	return
   145  }
   146  
   147  // ColumnValue :
   148  func ColumnValue(field string, value interface{}) (kv primitive.KV) {
   149  	kv.Field = field
   150  	kv.Value = value
   151  	return
   152  }
   153  
   154  // CastAs :
   155  func CastAs(value interface{}, datatype primitive.DataType) (cast primitive.CastAs) {
   156  	cast.Value = value
   157  	cast.DataType = datatype
   158  	return
   159  }
   160  
   161  func inGroup(field interface{}, op primitive.Operator, values interface{}) (c primitive.C) {
   162  	v := reflect.ValueOf(values)
   163  	k := v.Kind()
   164  	c.Field = wrapColumn(field)
   165  	c.Operator = op
   166  	grp := primitive.Group{}
   167  	grp.Values = append(grp.Values, Raw("("))
   168  	if k == reflect.Array || k == reflect.Slice {
   169  		for i := 0; i < v.Len(); i++ {
   170  			if i > 0 {
   171  				grp.Values = append(grp.Values, Raw(","))
   172  			}
   173  			grp.Values = append(grp.Values, v.Index(i).Interface())
   174  		}
   175  	} else {
   176  		grp.Values = append(grp.Values, values)
   177  	}
   178  	grp.Values = append(grp.Values, Raw(")"))
   179  	c.Value = grp
   180  	return c
   181  }
   182  
   183  func wrapColumn(it interface{}) interface{} {
   184  	switch vi := it.(type) {
   185  	case string:
   186  		return Column(vi)
   187  	case primitive.Column:
   188  		return vi
   189  	default:
   190  		return vi
   191  	}
   192  }
   193  
   194  func clause(field interface{}, op primitive.Operator, value interface{}) (c primitive.C) {
   195  	c.Field = wrapColumn(field)
   196  	c.Operator = op
   197  	c.Value = value
   198  	return
   199  }