gf/database/gdb/gdb_model_where.go

160 lines
5.8 KiB
Go
Raw Normal View History

2021-11-17 22:01:17 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gdb
import (
"fmt"
"github.com/gogf/gf/v2/text/gstr"
)
2021-12-28 17:15:01 +08:00
// doWhereType sets the condition statement for the model. The parameter `where` can be type of
2021-11-17 22:01:17 +08:00
// string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times,
// multiple conditions will be joined into where statement using "AND".
2021-12-28 17:15:01 +08:00
func (m *Model) doWhereType(t string, where interface{}, args ...interface{}) *Model {
2021-11-17 22:01:17 +08:00
model := m.getModel()
if model.whereHolder == nil {
model.whereHolder = make([]ModelWhereHolder, 0)
}
2021-12-28 17:15:01 +08:00
if t == "" {
if len(args) == 0 {
t = whereHolderTypeNoArgs
} else {
t = whereHolderTypeDefault
}
}
2021-11-17 22:01:17 +08:00
model.whereHolder = append(model.whereHolder, ModelWhereHolder{
2021-12-28 17:15:01 +08:00
Type: t,
2021-11-17 22:01:17 +08:00
Operator: whereHolderOperatorWhere,
Where: where,
Args: args,
})
return model
}
2021-12-28 17:15:01 +08:00
// doWherefType builds condition string using fmt.Sprintf and arguments.
// Note that if the number of `args` is more than the placeholder in `format`,
// the extra `args` will be used as the where condition arguments of the Model.
func (m *Model) doWherefType(t string, format string, args ...interface{}) *Model {
var (
placeHolderCount = gstr.Count(format, "?")
conditionStr = fmt.Sprintf(format, args[:len(args)-placeHolderCount]...)
)
return m.doWhereType(t, conditionStr, args[len(args)-placeHolderCount:]...)
}
// Where sets the condition statement for the model. The parameter `where` can be type of
// string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times,
// multiple conditions will be joined into where statement using "AND".
// Eg:
// Where("uid=10000")
// Where("uid", 10000)
// Where("money>? AND name like ?", 99999, "vip_%")
// Where("uid", 1).Where("name", "john")
// Where("status IN (?)", g.Slice{1,2,3})
// Where("age IN(?,?)", 18, 50)
// Where(User{ Id : 1, UserName : "john"}).
func (m *Model) Where(where interface{}, args ...interface{}) *Model {
return m.doWhereType(``, where, args...)
}
// Wheref builds condition string using fmt.Sprintf and arguments.
// Note that if the number of `args` is more than the placeholder in `format`,
// the extra `args` will be used as the where condition arguments of the Model.
// Eg:
// Wheref(`amount<? and status=%s`, "paid", 100) => WHERE `amount`<100 and status='paid'
// Wheref(`amount<%d and status=%s`, 100, "paid") => WHERE `amount`<100 and status='paid'
func (m *Model) Wheref(format string, args ...interface{}) *Model {
return m.doWherefType(``, format, args...)
}
2021-11-17 22:01:17 +08:00
// WherePri does the same logic as Model.Where except that if the parameter `where`
// is a single condition like int/string/float/slice, it treats the condition as the primary
// key value. That is, if primary key is "id" and given `where` parameter as "123", the
// WherePri function treats the condition as "id=123", but Model.Where treats the condition
// as string "123".
func (m *Model) WherePri(where interface{}, args ...interface{}) *Model {
if len(args) > 0 {
return m.Where(where, args...)
}
newWhere := GetPrimaryKeyCondition(m.getPrimaryKey(), where)
return m.Where(newWhere[0], newWhere[1:]...)
}
// WhereLT builds `column < value` statement.
func (m *Model) WhereLT(column string, value interface{}) *Model {
2021-12-28 17:15:01 +08:00
return m.Wheref(`%s < ?`, m.QuoteWord(column), value)
2021-11-17 22:01:17 +08:00
}
// WhereLTE builds `column <= value` statement.
func (m *Model) WhereLTE(column string, value interface{}) *Model {
2021-12-28 17:15:01 +08:00
return m.Wheref(`%s <= ?`, m.QuoteWord(column), value)
2021-11-17 22:01:17 +08:00
}
// WhereGT builds `column > value` statement.
func (m *Model) WhereGT(column string, value interface{}) *Model {
2021-12-28 17:15:01 +08:00
return m.Wheref(`%s > ?`, m.QuoteWord(column), value)
2021-11-17 22:01:17 +08:00
}
// WhereGTE builds `column >= value` statement.
func (m *Model) WhereGTE(column string, value interface{}) *Model {
2021-12-28 17:15:01 +08:00
return m.Wheref(`%s >= ?`, m.QuoteWord(column), value)
2021-11-17 22:01:17 +08:00
}
// WhereBetween builds `column BETWEEN min AND max` statement.
func (m *Model) WhereBetween(column string, min, max interface{}) *Model {
return m.Wheref(`%s BETWEEN ? AND ?`, m.QuoteWord(column), min, max)
}
// WhereLike builds `column LIKE like` statement.
2021-12-28 17:15:01 +08:00
func (m *Model) WhereLike(column string, like string) *Model {
2021-11-17 22:01:17 +08:00
return m.Wheref(`%s LIKE ?`, m.QuoteWord(column), like)
}
// WhereIn builds `column IN (in)` statement.
func (m *Model) WhereIn(column string, in interface{}) *Model {
2021-12-28 17:15:01 +08:00
return m.doWherefType(whereHolderTypeIn, `%s IN (?)`, m.QuoteWord(column), in)
2021-11-17 22:01:17 +08:00
}
// WhereNull builds `columns[0] IS NULL AND columns[1] IS NULL ...` statement.
func (m *Model) WhereNull(columns ...string) *Model {
model := m
for _, column := range columns {
model = m.Wheref(`%s IS NULL`, m.QuoteWord(column))
}
return model
}
// WhereNotBetween builds `column NOT BETWEEN min AND max` statement.
func (m *Model) WhereNotBetween(column string, min, max interface{}) *Model {
return m.Wheref(`%s NOT BETWEEN ? AND ?`, m.QuoteWord(column), min, max)
}
// WhereNotLike builds `column NOT LIKE like` statement.
func (m *Model) WhereNotLike(column string, like interface{}) *Model {
return m.Wheref(`%s NOT LIKE ?`, m.QuoteWord(column), like)
}
// WhereNot builds `column != value` statement.
func (m *Model) WhereNot(column string, value interface{}) *Model {
return m.Wheref(`%s != ?`, m.QuoteWord(column), value)
}
// WhereNotIn builds `column NOT IN (in)` statement.
func (m *Model) WhereNotIn(column string, in interface{}) *Model {
2021-12-28 17:15:01 +08:00
return m.doWherefType(whereHolderTypeIn, `%s NOT IN (?)`, m.QuoteWord(column), in)
2021-11-17 22:01:17 +08:00
}
// WhereNotNull builds `columns[0] IS NOT NULL AND columns[1] IS NOT NULL ...` statement.
func (m *Model) WhereNotNull(columns ...string) *Model {
model := m
for _, column := range columns {
model = m.Wheref(`%s IS NOT NULL`, m.QuoteWord(column))
}
return model
}