github.com/godaddy-x/freego@v1.0.156/ormx/sqlc/cnd.go (about)

     1  package sqlc
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/godaddy-x/freego/ormx/sqld/dialect"
     6  )
     7  
     8  /**
     9   * @author shadow
    10   * @createby 2018.10.10
    11   */
    12  
    13  // 数据库操作类型枚举
    14  const (
    15  	EQ_ = iota
    16  	NOT_EQ_
    17  	LT_
    18  	LTE_
    19  	GT_
    20  	GTE_
    21  	IS_NULL_
    22  	IS_NOT_NULL_
    23  	BETWEEN_
    24  	BETWEEN2_
    25  	NOT_BETWEEN_
    26  	IN_
    27  	NOT_IN_
    28  	LIKE_
    29  	NOT_LIKE_
    30  	OR_
    31  	ORDER_BY_
    32  	LEFT_
    33  	RIGHT_
    34  	INNER_
    35  	SUM_
    36  	AVG_
    37  	MIN_
    38  	MAX_
    39  	CNT_
    40  )
    41  
    42  const ASC_ = 1
    43  const DESC_ = 2
    44  
    45  const (
    46  	Id      = "Id"
    47  	Key     = "key"
    48  	Auto    = "auto"
    49  	Ignore  = "ignore"
    50  	Bson    = "bson"
    51  	Json    = "json"
    52  	Mg      = "mg"
    53  	True    = "true"
    54  	Date    = "date"
    55  	Blob    = "blob"
    56  	DB      = "db"
    57  	Comment = "comment"
    58  	Charset = "charset"
    59  	Collate = "collate"
    60  )
    61  
    62  // 数据库操作逻辑条件对象
    63  type Condition struct {
    64  	Logic  int
    65  	Key    string
    66  	Value  interface{}
    67  	Values []interface{}
    68  	Alias  string
    69  }
    70  
    71  type Collation struct {
    72  	Locale          string `bson:",omitempty"` // The locale
    73  	CaseLevel       bool   `bson:",omitempty"` // The case level
    74  	CaseFirst       string `bson:",omitempty"` // The case ordering
    75  	Strength        int    `bson:",omitempty"` // The number of comparison levels to use
    76  	NumericOrdering bool   `bson:",omitempty"` // Whether to order numbers based on numerical order and not collation order
    77  	Alternate       string `bson:",omitempty"` // Whether spaces and punctuation are considered base characters
    78  	MaxVariable     string `bson:",omitempty"` // Which characters are affected by alternate: "shifted"
    79  	Normalization   bool   `bson:",omitempty"` // Causes text to be normalized into Unicode NFD
    80  	Backwards       bool   `bson:",omitempty"` // Causes secondary differences to be considered in reverse order, as it is done in the French language
    81  }
    82  
    83  // 连接表条件对象
    84  type JoinCond struct {
    85  	Type  int
    86  	Table string
    87  	Alias string
    88  	On    string
    89  }
    90  
    91  // 主表条件对象
    92  type FromCond struct {
    93  	Table string
    94  	Alias string
    95  }
    96  
    97  // 数据库操作汇总逻辑条件对象
    98  type Cnd struct {
    99  	ConditPart      []string
   100  	Conditions      []Condition
   101  	AnyFields       []string
   102  	AnyNotFields    []string
   103  	Distincts       []string
   104  	Groupbys        []string
   105  	Orderbys        []Condition
   106  	Aggregates      []Condition
   107  	Upsets          map[string]interface{}
   108  	Model           Object
   109  	CollationConfig *Collation
   110  	Pagination      dialect.Dialect
   111  	FromCond        *FromCond
   112  	JoinCond        []*JoinCond
   113  	SampleSize      int64
   114  	LimitSize       int64 // 固定截取结果集数量
   115  	CacheConfig     CacheConfig
   116  	Escape          bool
   117  }
   118  
   119  // 缓存结果集参数
   120  type CacheConfig struct {
   121  	Open   bool
   122  	Prefix string
   123  	Key    string
   124  	Expire int
   125  }
   126  
   127  // args[0]=对象类型
   128  func M(model ...Object) *Cnd {
   129  	c := &Cnd{}
   130  	if model != nil && len(model) > 0 {
   131  		c.Model = model[0]
   132  	}
   133  	c.Escape = true
   134  	return c
   135  }
   136  
   137  // 保存基础命令操作
   138  func addDefaultCondit(cnd *Cnd, condit Condition) *Cnd {
   139  	cnd.Conditions = append(cnd.Conditions, condit)
   140  	return cnd
   141  }
   142  
   143  func (self *Cnd) UnEscape() *Cnd {
   144  	self.Escape = false
   145  	return self
   146  }
   147  
   148  // =
   149  func (self *Cnd) Eq(key string, value interface{}) *Cnd {
   150  	if value == nil {
   151  		return self
   152  	}
   153  	condit := Condition{EQ_, key, value, nil, ""}
   154  	return addDefaultCondit(self, condit)
   155  }
   156  
   157  // <>
   158  func (self *Cnd) NotEq(key string, value interface{}) *Cnd {
   159  	if value == nil {
   160  		return self
   161  	}
   162  	condit := Condition{NOT_EQ_, key, value, nil, ""}
   163  	return addDefaultCondit(self, condit)
   164  }
   165  
   166  // <
   167  func (self *Cnd) Lt(key string, value interface{}) *Cnd {
   168  	if value == nil {
   169  		return self
   170  	}
   171  	condit := Condition{LT_, key, value, nil, ""}
   172  	return addDefaultCondit(self, condit)
   173  }
   174  
   175  // <=
   176  func (self *Cnd) Lte(key string, value interface{}) *Cnd {
   177  	if value == nil {
   178  		return self
   179  	}
   180  	condit := Condition{LTE_, key, value, nil, ""}
   181  	return addDefaultCondit(self, condit)
   182  }
   183  
   184  // >
   185  func (self *Cnd) Gt(key string, value interface{}) *Cnd {
   186  	if value == nil {
   187  		return self
   188  	}
   189  	condit := Condition{GT_, key, value, nil, ""}
   190  	return addDefaultCondit(self, condit)
   191  }
   192  
   193  // >=
   194  func (self *Cnd) Gte(key string, value interface{}) *Cnd {
   195  	if value == nil {
   196  		return self
   197  	}
   198  	condit := Condition{GTE_, key, value, nil, ""}
   199  	return addDefaultCondit(self, condit)
   200  }
   201  
   202  // is null
   203  func (self *Cnd) IsNull(key string) *Cnd {
   204  	condit := Condition{IS_NULL_, key, nil, nil, ""}
   205  	return addDefaultCondit(self, condit)
   206  }
   207  
   208  // is not null
   209  func (self *Cnd) IsNotNull(key string) *Cnd {
   210  	condit := Condition{IS_NOT_NULL_, key, nil, nil, ""}
   211  	return addDefaultCondit(self, condit)
   212  }
   213  
   214  // between, >= a b =<
   215  func (self *Cnd) Between(key string, value1 interface{}, value2 interface{}) *Cnd {
   216  	if value1 == nil || value2 == nil {
   217  		return self
   218  	}
   219  	condit := Condition{BETWEEN_, key, nil, []interface{}{value1, value2}, ""}
   220  	return addDefaultCondit(self, condit)
   221  }
   222  
   223  // 时间范围专用, >= a b <
   224  func (self *Cnd) InDate(key string, value1 interface{}, value2 interface{}) *Cnd {
   225  	if value1 == nil || value2 == nil {
   226  		return self
   227  	}
   228  	condit := Condition{BETWEEN2_, key, nil, []interface{}{value1, value2}, ""}
   229  	return addDefaultCondit(self, condit)
   230  }
   231  
   232  // not between
   233  func (self *Cnd) NotBetween(key string, value1 interface{}, value2 interface{}) *Cnd {
   234  	if value1 == nil || value2 == nil {
   235  		return self
   236  	}
   237  	condit := Condition{NOT_BETWEEN_, key, nil, []interface{}{value1, value2}, ""}
   238  	return addDefaultCondit(self, condit)
   239  }
   240  
   241  // in
   242  func (self *Cnd) In(key string, values ...interface{}) *Cnd {
   243  	if values == nil || len(values) == 0 {
   244  		return self
   245  	}
   246  	condit := Condition{IN_, key, nil, values, ""}
   247  	return addDefaultCondit(self, condit)
   248  }
   249  
   250  // not in
   251  func (self *Cnd) NotIn(key string, values ...interface{}) *Cnd {
   252  	if values == nil || len(values) == 0 {
   253  		return self
   254  	}
   255  	condit := Condition{NOT_IN_, key, nil, values, ""}
   256  	return addDefaultCondit(self, condit)
   257  }
   258  
   259  // like
   260  func (self *Cnd) Like(key string, value interface{}) *Cnd {
   261  	if value == nil {
   262  		return self
   263  	}
   264  	condit := Condition{LIKE_, key, value, nil, ""}
   265  	return addDefaultCondit(self, condit)
   266  }
   267  
   268  // not like
   269  func (self *Cnd) NotLike(key string, value interface{}) *Cnd {
   270  	if value == nil {
   271  		return self
   272  	}
   273  	condit := Condition{NOT_LIKE_, key, value, nil, ""}
   274  	return addDefaultCondit(self, condit)
   275  }
   276  
   277  // add other
   278  func (self *Cnd) AddOther(part string) *Cnd {
   279  	if len(part) > 0 {
   280  		self.ConditPart = append(self.ConditPart, part)
   281  	}
   282  	return self
   283  }
   284  
   285  // or
   286  func (self *Cnd) Or(cnds ...interface{}) *Cnd {
   287  	if cnds == nil || len(cnds) == 0 {
   288  		return self
   289  	}
   290  	condit := Condition{OR_, "", nil, cnds, ""}
   291  	return addDefaultCondit(self, condit)
   292  }
   293  
   294  // 复杂查询设定首个from table as
   295  func (self *Cnd) From(fromTable string) *Cnd {
   296  	self.FromCond = &FromCond{fromTable, ""}
   297  	return self
   298  }
   299  
   300  // left join
   301  func (self *Cnd) Join(join int, table string, on string) *Cnd {
   302  	if len(table) == 0 || len(on) == 0 {
   303  		return self
   304  	}
   305  	self.JoinCond = append(self.JoinCond, &JoinCond{join, table, "", on})
   306  	return self
   307  }
   308  
   309  func (self *Cnd) Collation(collation *Collation) *Cnd {
   310  	if collation == nil {
   311  		return self
   312  	}
   313  	if len(collation.Locale) == 0 {
   314  		collation.Locale = "zh"
   315  	}
   316  	self.CollationConfig = collation
   317  	return self
   318  }
   319  
   320  // limit,以页数跨度查询
   321  func (self *Cnd) Limit(pageNo int64, pageSize int64) *Cnd {
   322  	if pageNo <= 0 {
   323  		pageNo = 1
   324  	}
   325  	if pageSize <= 0 || pageSize > 5000 {
   326  		pageSize = 50
   327  	}
   328  	self.Pagination = dialect.Dialect{PageNo: pageNo, PageSize: pageSize, Spilled: true, IsPage: true}
   329  	return self
   330  }
   331  
   332  // offset,以下标跨度查询
   333  func (self *Cnd) Offset(offset int64, limit int64) *Cnd {
   334  	if offset <= 0 {
   335  		offset = 0
   336  	}
   337  	if limit <= 0 || limit > 5000 {
   338  		limit = 50
   339  	}
   340  	self.Pagination = dialect.Dialect{PageNo: offset, PageSize: limit, Spilled: true, IsOffset: true, IsPage: true}
   341  	return self
   342  }
   343  
   344  // 筛选字段去重
   345  func (self *Cnd) Distinct(keys ...string) *Cnd {
   346  	for _, v := range keys {
   347  		if len(v) == 0 {
   348  			continue
   349  		}
   350  		self.Distincts = append(self.Distincts, v)
   351  	}
   352  	return self
   353  }
   354  
   355  // 按字段分组
   356  func (self *Cnd) Groupby(keys ...string) *Cnd {
   357  	for _, v := range keys {
   358  		if len(v) == 0 {
   359  			continue
   360  		}
   361  		self.Groupbys = append(self.Groupbys, v)
   362  	}
   363  	return self
   364  }
   365  
   366  // 聚合函数
   367  func (self *Cnd) Agg(logic int, key string, alias ...string) *Cnd {
   368  	if len(key) == 0 {
   369  		return self
   370  	}
   371  	ali := key
   372  	if alias != nil || len(alias) > 0 {
   373  		ali = alias[0]
   374  	}
   375  	self.Aggregates = append(self.Aggregates, Condition{Logic: logic, Key: key, Alias: ali})
   376  	return self
   377  }
   378  
   379  // 按字段排序
   380  func (self *Cnd) Orderby(key string, sortby int) *Cnd {
   381  	if !(sortby == ASC_ || sortby == DESC_) {
   382  		panic("order by sort value invalid")
   383  	}
   384  	condit := Condition{ORDER_BY_, key, sortby, nil, ""}
   385  	self.Orderbys = append(self.Orderbys, condit)
   386  	return self
   387  }
   388  
   389  // 按字段排序升序
   390  func (self *Cnd) Asc(keys ...string) *Cnd {
   391  	if keys == nil || len(keys) == 0 {
   392  		return self
   393  	}
   394  	for _, v := range keys {
   395  		condit := Condition{ORDER_BY_, v, ASC_, nil, ""}
   396  		self.Orderbys = append(self.Orderbys, condit)
   397  	}
   398  	return self
   399  }
   400  
   401  // 按字段排序倒序
   402  func (self *Cnd) Desc(keys ...string) *Cnd {
   403  	if keys == nil || len(keys) == 0 {
   404  		return self
   405  	}
   406  	for _, v := range keys {
   407  		condit := Condition{ORDER_BY_, v, DESC_, nil, ""}
   408  		self.Orderbys = append(self.Orderbys, condit)
   409  	}
   410  	return self
   411  }
   412  
   413  // 筛选指定字段查询
   414  func (self *Cnd) Fields(keys ...string) *Cnd {
   415  	if keys == nil || len(keys) == 0 {
   416  		return self
   417  	}
   418  	for _, v := range keys {
   419  		self.AnyFields = append(self.AnyFields, v)
   420  	}
   421  	return self
   422  }
   423  
   424  // 筛选过滤指定字段查询
   425  func (self *Cnd) NotFields(keys ...string) *Cnd {
   426  	if keys == nil || len(keys) == 0 {
   427  		return self
   428  	}
   429  	for _, v := range keys {
   430  		self.AnyNotFields = append(self.AnyNotFields, v)
   431  	}
   432  	return self
   433  }
   434  
   435  // 随机选取数据条数
   436  func (self *Cnd) Sample(size int64) *Cnd {
   437  	if size <= 0 {
   438  		return self
   439  	}
   440  	if size > 2000 {
   441  		size = 10
   442  	}
   443  	self.SampleSize = size
   444  	return self
   445  }
   446  
   447  // 固定截取结果集数量
   448  func (self *Cnd) ResultSize(size int64) *Cnd {
   449  	if size <= 0 {
   450  		size = 50
   451  	}
   452  	if size > 5000 {
   453  		size = 50
   454  	}
   455  	self.LimitSize = size
   456  	return self
   457  }
   458  
   459  func (self *Cnd) FastPage(key string, sort int, prevID, lastID, pageSize int64, countQ ...bool) *Cnd {
   460  	if pageSize <= 0 {
   461  		pageSize = 50
   462  	}
   463  	if pageSize > 5000 {
   464  		pageSize = 50
   465  	}
   466  	if len(key) == 0 {
   467  		panic("fast limit key is nil")
   468  	}
   469  	queryCount := false
   470  	if len(countQ) > 0 {
   471  		queryCount = countQ[0]
   472  	}
   473  	self.Pagination = dialect.Dialect{PageNo: 0, PageSize: pageSize, IsFastPage: true, FastPageKey: key, FastPageSort: sort, FastPageParam: []int64{prevID, lastID}, FastPageSortCountQ: queryCount}
   474  	return self
   475  }
   476  
   477  // 缓存指定结果集
   478  func (self *Cnd) Cache(config CacheConfig) *Cnd {
   479  	self.CacheConfig = config
   480  	self.CacheConfig.Open = true
   481  	return self
   482  }
   483  
   484  // 指定更新字段
   485  func (self *Cnd) Upset(keys []string, values ...interface{}) *Cnd {
   486  	if values == nil || len(values) == 0 {
   487  		return self
   488  	}
   489  	if len(keys) == 0 || len(keys) != len(values) {
   490  		fmt.Println("the keys and values parameter size are not equal")
   491  		return self
   492  	}
   493  	if self.Upsets == nil {
   494  		self.Upsets = make(map[string]interface{}, len(keys))
   495  	}
   496  	for i := 0; i < len(keys); i++ {
   497  		self.Upsets[keys[i]] = values[i]
   498  	}
   499  	return self
   500  }
   501  
   502  func (self *Cnd) GetPageResult() dialect.PageResult {
   503  	return self.Pagination.GetResult()
   504  }