gf/database/gdb/gdb_model_where.go

120 lines
4.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://githum.com/gogf/gf.
2021-11-17 22:01:17 +08:00
package gdb
// callWhereBuilder creates and returns a new Model, and sets its WhereBuilder if current Model is safe.
// It sets the WhereBuilder and returns current Model directly if it is not safe.
func (m *Model) callWhereBuilder(builder *WhereBuilder) *Model {
2021-11-17 22:01:17 +08:00
model := m.getModel()
model.whereBuilder = builder
2021-11-17 22:01:17 +08:00
return model
}
// Where sets the condition statement for the builder. 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".
// See WhereBuilder.Where.
2021-12-28 17:15:01 +08:00
func (m *Model) Where(where interface{}, args ...interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.Where(where, args...))
2021-12-28 17:15:01 +08:00
}
// 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.
// See WhereBuilder.Wheref.
2021-12-28 17:15:01 +08:00
func (m *Model) Wheref(format string, args ...interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.Wheref(format, args...))
2021-12-28 17:15:01 +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".
// See WhereBuilder.WherePri.
2021-11-17 22:01:17 +08:00
func (m *Model) WherePri(where interface{}, args ...interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WherePri(where, args...))
2021-11-17 22:01:17 +08:00
}
// WhereLT builds `column < value` statement.
// See WhereBuilder.WhereLT.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereLT(column string, value interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereLT(column, value))
2021-11-17 22:01:17 +08:00
}
// WhereLTE builds `column <= value` statement.
// See WhereBuilder.WhereLTE.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereLTE(column string, value interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereLTE(column, value))
2021-11-17 22:01:17 +08:00
}
// WhereGT builds `column > value` statement.
// See WhereBuilder.WhereGT.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereGT(column string, value interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereGT(column, value))
2021-11-17 22:01:17 +08:00
}
// WhereGTE builds `column >= value` statement.
// See WhereBuilder.WhereGTE.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereGTE(column string, value interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereGTE(column, value))
2021-11-17 22:01:17 +08:00
}
// WhereBetween builds `column BETWEEN min AND max` statement.
// See WhereBuilder.WhereBetween.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereBetween(column string, min, max interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereBetween(column, min, max))
2021-11-17 22:01:17 +08:00
}
// WhereLike builds `column LIKE like` statement.
// See WhereBuilder.WhereLike.
2021-12-28 17:15:01 +08:00
func (m *Model) WhereLike(column string, like string) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereLike(column, like))
2021-11-17 22:01:17 +08:00
}
// WhereIn builds `column IN (in)` statement.
// See WhereBuilder.WhereIn.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereIn(column string, in interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereIn(column, in))
2021-11-17 22:01:17 +08:00
}
// WhereNull builds `columns[0] IS NULL AND columns[1] IS NULL ...` statement.
// See WhereBuilder.WhereNull.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereNull(columns ...string) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereNull(columns...))
2021-11-17 22:01:17 +08:00
}
// WhereNotBetween builds `column NOT BETWEEN min AND max` statement.
// See WhereBuilder.WhereNotBetween.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereNotBetween(column string, min, max interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereNotBetween(column, min, max))
2021-11-17 22:01:17 +08:00
}
// WhereNotLike builds `column NOT LIKE like` statement.
// See WhereBuilder.WhereNotLike.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereNotLike(column string, like interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereNotLike(column, like))
2021-11-17 22:01:17 +08:00
}
// WhereNot builds `column != value` statement.
// See WhereBuilder.WhereNot.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereNot(column string, value interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereNot(column, value))
2021-11-17 22:01:17 +08:00
}
// WhereNotIn builds `column NOT IN (in)` statement.
// See WhereBuilder.WhereNotIn.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereNotIn(column string, in interface{}) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereNotIn(column, in))
2021-11-17 22:01:17 +08:00
}
// WhereNotNull builds `columns[0] IS NOT NULL AND columns[1] IS NOT NULL ...` statement.
// See WhereBuilder.WhereNotNull.
2021-11-17 22:01:17 +08:00
func (m *Model) WhereNotNull(columns ...string) *Model {
return m.callWhereBuilder(m.whereBuilder.WhereNotNull(columns...))
2021-11-17 22:01:17 +08:00
}