github.com/gogf/gf/v2@v2.7.4/database/gdb/gdb_model_builder_whereor.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/gogf/gf.
     6  
     7  package gdb
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/gogf/gf/v2/text/gstr"
    13  )
    14  
    15  // WhereOr adds "OR" condition to the where statement.
    16  func (b *WhereBuilder) doWhereOrType(t string, where interface{}, args ...interface{}) *WhereBuilder {
    17  	where, args = b.convertWhereBuilder(where, args)
    18  
    19  	builder := b.getBuilder()
    20  	if builder.whereHolder == nil {
    21  		builder.whereHolder = make([]WhereHolder, 0)
    22  	}
    23  	builder.whereHolder = append(builder.whereHolder, WhereHolder{
    24  		Type:     t,
    25  		Operator: whereHolderOperatorOr,
    26  		Where:    where,
    27  		Args:     args,
    28  	})
    29  	return builder
    30  }
    31  
    32  // WhereOrf builds `OR` condition string using fmt.Sprintf and arguments.
    33  func (b *WhereBuilder) doWhereOrfType(t string, format string, args ...interface{}) *WhereBuilder {
    34  	var (
    35  		placeHolderCount = gstr.Count(format, "?")
    36  		conditionStr     = fmt.Sprintf(format, args[:len(args)-placeHolderCount]...)
    37  	)
    38  	return b.doWhereOrType(t, conditionStr, args[len(args)-placeHolderCount:]...)
    39  }
    40  
    41  // WhereOr adds "OR" condition to the where statement.
    42  func (b *WhereBuilder) WhereOr(where interface{}, args ...interface{}) *WhereBuilder {
    43  	return b.doWhereOrType(``, where, args...)
    44  }
    45  
    46  // WhereOrf builds `OR` condition string using fmt.Sprintf and arguments.
    47  // Eg:
    48  // WhereOrf(`amount<? and status=%s`, "paid", 100)  => WHERE xxx OR `amount`<100 and status='paid'
    49  // WhereOrf(`amount<%d and status=%s`, 100, "paid") => WHERE xxx OR `amount`<100 and status='paid'
    50  func (b *WhereBuilder) WhereOrf(format string, args ...interface{}) *WhereBuilder {
    51  	return b.doWhereOrfType(``, format, args...)
    52  }
    53  
    54  // WhereOrNot builds `column != value` statement in `OR` conditions.
    55  func (b *WhereBuilder) WhereOrNot(column string, value interface{}) *WhereBuilder {
    56  	return b.WhereOrf(`%s != ?`, column, value)
    57  }
    58  
    59  // WhereOrLT builds `column < value` statement in `OR` conditions.
    60  func (b *WhereBuilder) WhereOrLT(column string, value interface{}) *WhereBuilder {
    61  	return b.WhereOrf(`%s < ?`, column, value)
    62  }
    63  
    64  // WhereOrLTE builds `column <= value` statement in `OR` conditions.
    65  func (b *WhereBuilder) WhereOrLTE(column string, value interface{}) *WhereBuilder {
    66  	return b.WhereOrf(`%s <= ?`, column, value)
    67  }
    68  
    69  // WhereOrGT builds `column > value` statement in `OR` conditions.
    70  func (b *WhereBuilder) WhereOrGT(column string, value interface{}) *WhereBuilder {
    71  	return b.WhereOrf(`%s > ?`, column, value)
    72  }
    73  
    74  // WhereOrGTE builds `column >= value` statement in `OR` conditions.
    75  func (b *WhereBuilder) WhereOrGTE(column string, value interface{}) *WhereBuilder {
    76  	return b.WhereOrf(`%s >= ?`, column, value)
    77  }
    78  
    79  // WhereOrBetween builds `column BETWEEN min AND max` statement in `OR` conditions.
    80  func (b *WhereBuilder) WhereOrBetween(column string, min, max interface{}) *WhereBuilder {
    81  	return b.WhereOrf(`%s BETWEEN ? AND ?`, b.model.QuoteWord(column), min, max)
    82  }
    83  
    84  // WhereOrLike builds `column LIKE 'like'` statement in `OR` conditions.
    85  func (b *WhereBuilder) WhereOrLike(column string, like interface{}) *WhereBuilder {
    86  	return b.WhereOrf(`%s LIKE ?`, b.model.QuoteWord(column), like)
    87  }
    88  
    89  // WhereOrIn builds `column IN (in)` statement in `OR` conditions.
    90  func (b *WhereBuilder) WhereOrIn(column string, in interface{}) *WhereBuilder {
    91  	return b.doWhereOrfType(whereHolderTypeIn, `%s IN (?)`, b.model.QuoteWord(column), in)
    92  }
    93  
    94  // WhereOrNull builds `columns[0] IS NULL OR columns[1] IS NULL ...` statement in `OR` conditions.
    95  func (b *WhereBuilder) WhereOrNull(columns ...string) *WhereBuilder {
    96  	var builder *WhereBuilder
    97  	for _, column := range columns {
    98  		builder = b.WhereOrf(`%s IS NULL`, b.model.QuoteWord(column))
    99  	}
   100  	return builder
   101  }
   102  
   103  // WhereOrNotBetween builds `column NOT BETWEEN min AND max` statement in `OR` conditions.
   104  func (b *WhereBuilder) WhereOrNotBetween(column string, min, max interface{}) *WhereBuilder {
   105  	return b.WhereOrf(`%s NOT BETWEEN ? AND ?`, b.model.QuoteWord(column), min, max)
   106  }
   107  
   108  // WhereOrNotLike builds `column NOT LIKE like` statement in `OR` conditions.
   109  func (b *WhereBuilder) WhereOrNotLike(column string, like interface{}) *WhereBuilder {
   110  	return b.WhereOrf(`%s NOT LIKE ?`, b.model.QuoteWord(column), like)
   111  }
   112  
   113  // WhereOrNotIn builds `column NOT IN (in)` statement.
   114  func (b *WhereBuilder) WhereOrNotIn(column string, in interface{}) *WhereBuilder {
   115  	return b.doWhereOrfType(whereHolderTypeIn, `%s NOT IN (?)`, b.model.QuoteWord(column), in)
   116  }
   117  
   118  // WhereOrNotNull builds `columns[0] IS NOT NULL OR columns[1] IS NOT NULL ...` statement in `OR` conditions.
   119  func (b *WhereBuilder) WhereOrNotNull(columns ...string) *WhereBuilder {
   120  	builder := b
   121  	for _, column := range columns {
   122  		builder = builder.WhereOrf(`%s IS NOT NULL`, b.model.QuoteWord(column))
   123  	}
   124  	return builder
   125  }