github.com/wangyougui/gf/v2@v2.6.5/database/gdb/gdb_model_builder_where.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gdb
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/wangyougui/gf/v2/text/gstr"
    13  )
    14  
    15  // doWhereType sets the condition statement for the model. The parameter `where` can be type of
    16  // string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times,
    17  // multiple conditions will be joined into where statement using "AND".
    18  func (b *WhereBuilder) doWhereType(whereType string, where interface{}, args ...interface{}) *WhereBuilder {
    19  	where, args = b.convertWhereBuilder(where, args)
    20  
    21  	builder := b.getBuilder()
    22  	if builder.whereHolder == nil {
    23  		builder.whereHolder = make([]WhereHolder, 0)
    24  	}
    25  	if whereType == "" {
    26  		if len(args) == 0 {
    27  			whereType = whereHolderTypeNoArgs
    28  		} else {
    29  			whereType = whereHolderTypeDefault
    30  		}
    31  	}
    32  	builder.whereHolder = append(builder.whereHolder, WhereHolder{
    33  		Type:     whereType,
    34  		Operator: whereHolderOperatorWhere,
    35  		Where:    where,
    36  		Args:     args,
    37  	})
    38  	return builder
    39  }
    40  
    41  // doWherefType builds condition string using fmt.Sprintf and arguments.
    42  // Note that if the number of `args` is more than the placeholder in `format`,
    43  // the extra `args` will be used as the where condition arguments of the Model.
    44  func (b *WhereBuilder) doWherefType(t string, format string, args ...interface{}) *WhereBuilder {
    45  	var (
    46  		placeHolderCount = gstr.Count(format, "?")
    47  		conditionStr     = fmt.Sprintf(format, args[:len(args)-placeHolderCount]...)
    48  	)
    49  	return b.doWhereType(t, conditionStr, args[len(args)-placeHolderCount:]...)
    50  }
    51  
    52  // Where sets the condition statement for the builder. The parameter `where` can be type of
    53  // string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times,
    54  // multiple conditions will be joined into where statement using "AND".
    55  // Eg:
    56  // Where("uid=10000")
    57  // Where("uid", 10000)
    58  // Where("money>? AND name like ?", 99999, "vip_%")
    59  // Where("uid", 1).Where("name", "john")
    60  // Where("status IN (?)", g.Slice{1,2,3})
    61  // Where("age IN(?,?)", 18, 50)
    62  // Where(User{ Id : 1, UserName : "john"}).
    63  func (b *WhereBuilder) Where(where interface{}, args ...interface{}) *WhereBuilder {
    64  	return b.doWhereType(``, where, args...)
    65  }
    66  
    67  // Wheref builds condition string using fmt.Sprintf and arguments.
    68  // Note that if the number of `args` is more than the placeholder in `format`,
    69  // the extra `args` will be used as the where condition arguments of the Model.
    70  // Eg:
    71  // Wheref(`amount<? and status=%s`, "paid", 100)  => WHERE `amount`<100 and status='paid'
    72  // Wheref(`amount<%d and status=%s`, 100, "paid") => WHERE `amount`<100 and status='paid'
    73  func (b *WhereBuilder) Wheref(format string, args ...interface{}) *WhereBuilder {
    74  	return b.doWherefType(``, format, args...)
    75  }
    76  
    77  // WherePri does the same logic as Model.Where except that if the parameter `where`
    78  // is a single condition like int/string/float/slice, it treats the condition as the primary
    79  // key value. That is, if primary key is "id" and given `where` parameter as "123", the
    80  // WherePri function treats the condition as "id=123", but Model.Where treats the condition
    81  // as string "123".
    82  func (b *WhereBuilder) WherePri(where interface{}, args ...interface{}) *WhereBuilder {
    83  	if len(args) > 0 {
    84  		return b.Where(where, args...)
    85  	}
    86  	newWhere := GetPrimaryKeyCondition(b.model.getPrimaryKey(), where)
    87  	return b.Where(newWhere[0], newWhere[1:]...)
    88  }
    89  
    90  // WhereLT builds `column < value` statement.
    91  func (b *WhereBuilder) WhereLT(column string, value interface{}) *WhereBuilder {
    92  	return b.Wheref(`%s < ?`, b.model.QuoteWord(column), value)
    93  }
    94  
    95  // WhereLTE builds `column <= value` statement.
    96  func (b *WhereBuilder) WhereLTE(column string, value interface{}) *WhereBuilder {
    97  	return b.Wheref(`%s <= ?`, b.model.QuoteWord(column), value)
    98  }
    99  
   100  // WhereGT builds `column > value` statement.
   101  func (b *WhereBuilder) WhereGT(column string, value interface{}) *WhereBuilder {
   102  	return b.Wheref(`%s > ?`, b.model.QuoteWord(column), value)
   103  }
   104  
   105  // WhereGTE builds `column >= value` statement.
   106  func (b *WhereBuilder) WhereGTE(column string, value interface{}) *WhereBuilder {
   107  	return b.Wheref(`%s >= ?`, b.model.QuoteWord(column), value)
   108  }
   109  
   110  // WhereBetween builds `column BETWEEN min AND max` statement.
   111  func (b *WhereBuilder) WhereBetween(column string, min, max interface{}) *WhereBuilder {
   112  	return b.Wheref(`%s BETWEEN ? AND ?`, b.model.QuoteWord(column), min, max)
   113  }
   114  
   115  // WhereLike builds `column LIKE like` statement.
   116  func (b *WhereBuilder) WhereLike(column string, like string) *WhereBuilder {
   117  	return b.Wheref(`%s LIKE ?`, b.model.QuoteWord(column), like)
   118  }
   119  
   120  // WhereIn builds `column IN (in)` statement.
   121  func (b *WhereBuilder) WhereIn(column string, in interface{}) *WhereBuilder {
   122  	return b.doWherefType(whereHolderTypeIn, `%s IN (?)`, b.model.QuoteWord(column), in)
   123  }
   124  
   125  // WhereNull builds `columns[0] IS NULL AND columns[1] IS NULL ...` statement.
   126  func (b *WhereBuilder) WhereNull(columns ...string) *WhereBuilder {
   127  	builder := b
   128  	for _, column := range columns {
   129  		builder = builder.Wheref(`%s IS NULL`, b.model.QuoteWord(column))
   130  	}
   131  	return builder
   132  }
   133  
   134  // WhereNotBetween builds `column NOT BETWEEN min AND max` statement.
   135  func (b *WhereBuilder) WhereNotBetween(column string, min, max interface{}) *WhereBuilder {
   136  	return b.Wheref(`%s NOT BETWEEN ? AND ?`, b.model.QuoteWord(column), min, max)
   137  }
   138  
   139  // WhereNotLike builds `column NOT LIKE like` statement.
   140  func (b *WhereBuilder) WhereNotLike(column string, like interface{}) *WhereBuilder {
   141  	return b.Wheref(`%s NOT LIKE ?`, b.model.QuoteWord(column), like)
   142  }
   143  
   144  // WhereNot builds `column != value` statement.
   145  func (b *WhereBuilder) WhereNot(column string, value interface{}) *WhereBuilder {
   146  	return b.Wheref(`%s != ?`, b.model.QuoteWord(column), value)
   147  }
   148  
   149  // WhereNotIn builds `column NOT IN (in)` statement.
   150  func (b *WhereBuilder) WhereNotIn(column string, in interface{}) *WhereBuilder {
   151  	return b.doWherefType(whereHolderTypeIn, `%s NOT IN (?)`, b.model.QuoteWord(column), in)
   152  }
   153  
   154  // WhereNotNull builds `columns[0] IS NOT NULL AND columns[1] IS NOT NULL ...` statement.
   155  func (b *WhereBuilder) WhereNotNull(columns ...string) *WhereBuilder {
   156  	builder := b
   157  	for _, column := range columns {
   158  		builder = builder.Wheref(`%s IS NOT NULL`, b.model.QuoteWord(column))
   159  	}
   160  	return builder
   161  }