github.com/gogf/gf/v2@v2.7.4/database/gdb/gdb_model_builder_where_prefix.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 // WherePrefix performs as Where, but it adds prefix to each field in where statement. 10 // Eg: 11 // WherePrefix("order", "status", "paid") => WHERE `order`.`status`='paid' 12 // WherePrefix("order", struct{Status:"paid", "channel":"bank"}) => WHERE `order`.`status`='paid' AND `order`.`channel`='bank' 13 func (b *WhereBuilder) WherePrefix(prefix string, where interface{}, args ...interface{}) *WhereBuilder { 14 where, args = b.convertWhereBuilder(where, args) 15 16 builder := b.getBuilder() 17 if builder.whereHolder == nil { 18 builder.whereHolder = make([]WhereHolder, 0) 19 } 20 builder.whereHolder = append(builder.whereHolder, WhereHolder{ 21 Type: whereHolderTypeDefault, 22 Operator: whereHolderOperatorWhere, 23 Where: where, 24 Args: args, 25 Prefix: prefix, 26 }) 27 return builder 28 } 29 30 // WherePrefixLT builds `prefix.column < value` statement. 31 func (b *WhereBuilder) WherePrefixLT(prefix string, column string, value interface{}) *WhereBuilder { 32 return b.Wheref(`%s.%s < ?`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), value) 33 } 34 35 // WherePrefixLTE builds `prefix.column <= value` statement. 36 func (b *WhereBuilder) WherePrefixLTE(prefix string, column string, value interface{}) *WhereBuilder { 37 return b.Wheref(`%s.%s <= ?`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), value) 38 } 39 40 // WherePrefixGT builds `prefix.column > value` statement. 41 func (b *WhereBuilder) WherePrefixGT(prefix string, column string, value interface{}) *WhereBuilder { 42 return b.Wheref(`%s.%s > ?`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), value) 43 } 44 45 // WherePrefixGTE builds `prefix.column >= value` statement. 46 func (b *WhereBuilder) WherePrefixGTE(prefix string, column string, value interface{}) *WhereBuilder { 47 return b.Wheref(`%s.%s >= ?`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), value) 48 } 49 50 // WherePrefixBetween builds `prefix.column BETWEEN min AND max` statement. 51 func (b *WhereBuilder) WherePrefixBetween(prefix string, column string, min, max interface{}) *WhereBuilder { 52 return b.Wheref(`%s.%s BETWEEN ? AND ?`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), min, max) 53 } 54 55 // WherePrefixLike builds `prefix.column LIKE like` statement. 56 func (b *WhereBuilder) WherePrefixLike(prefix string, column string, like interface{}) *WhereBuilder { 57 return b.Wheref(`%s.%s LIKE ?`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), like) 58 } 59 60 // WherePrefixIn builds `prefix.column IN (in)` statement. 61 func (b *WhereBuilder) WherePrefixIn(prefix string, column string, in interface{}) *WhereBuilder { 62 return b.doWherefType(whereHolderTypeIn, `%s.%s IN (?)`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), in) 63 } 64 65 // WherePrefixNull builds `prefix.columns[0] IS NULL AND prefix.columns[1] IS NULL ...` statement. 66 func (b *WhereBuilder) WherePrefixNull(prefix string, columns ...string) *WhereBuilder { 67 builder := b 68 for _, column := range columns { 69 builder = builder.Wheref(`%s.%s IS NULL`, b.model.QuoteWord(prefix), b.model.QuoteWord(column)) 70 } 71 return builder 72 } 73 74 // WherePrefixNotBetween builds `prefix.column NOT BETWEEN min AND max` statement. 75 func (b *WhereBuilder) WherePrefixNotBetween(prefix string, column string, min, max interface{}) *WhereBuilder { 76 return b.Wheref(`%s.%s NOT BETWEEN ? AND ?`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), min, max) 77 } 78 79 // WherePrefixNotLike builds `prefix.column NOT LIKE like` statement. 80 func (b *WhereBuilder) WherePrefixNotLike(prefix string, column string, like interface{}) *WhereBuilder { 81 return b.Wheref(`%s.%s NOT LIKE ?`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), like) 82 } 83 84 // WherePrefixNot builds `prefix.column != value` statement. 85 func (b *WhereBuilder) WherePrefixNot(prefix string, column string, value interface{}) *WhereBuilder { 86 return b.Wheref(`%s.%s != ?`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), value) 87 } 88 89 // WherePrefixNotIn builds `prefix.column NOT IN (in)` statement. 90 func (b *WhereBuilder) WherePrefixNotIn(prefix string, column string, in interface{}) *WhereBuilder { 91 return b.doWherefType(whereHolderTypeIn, `%s.%s NOT IN (?)`, b.model.QuoteWord(prefix), b.model.QuoteWord(column), in) 92 } 93 94 // WherePrefixNotNull builds `prefix.columns[0] IS NOT NULL AND prefix.columns[1] IS NOT NULL ...` statement. 95 func (b *WhereBuilder) WherePrefixNotNull(prefix string, columns ...string) *WhereBuilder { 96 builder := b 97 for _, column := range columns { 98 builder = builder.Wheref(`%s.%s IS NOT NULL`, b.model.QuoteWord(prefix), b.model.QuoteWord(column)) 99 } 100 return builder 101 }