2020-01-29 14:22:44 +03:00
|
|
|
package gorm
|
|
|
|
|
2020-02-04 03:56:15 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-07-10 16:11:28 +03:00
|
|
|
"regexp"
|
2020-02-16 08:45:27 +03:00
|
|
|
"strings"
|
2020-02-04 03:56:15 +03:00
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
"gorm.io/gorm/utils"
|
2020-02-04 03:56:15 +03:00
|
|
|
)
|
2020-01-30 10:14:48 +03:00
|
|
|
|
2020-01-29 14:22:44 +03:00
|
|
|
// Model specify the model you would like to run db operations
|
2022-11-02 05:28:00 +03:00
|
|
|
//
|
|
|
|
// // update all users's name to `hello`
|
|
|
|
// db.Model(&User{}).Update("name", "hello")
|
2022-12-23 11:51:01 +03:00
|
|
|
// // if user's primary key is non-blank, will use it as condition, then will only update that user's name to `hello`
|
2022-11-02 05:28:00 +03:00
|
|
|
// db.Model(&user).Update("name", "hello")
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Model(value interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-01-29 22:03:06 +03:00
|
|
|
tx.Statement.Model = value
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 10:14:48 +03:00
|
|
|
// Clauses Add clauses
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// This supports both standard clauses (clause.OrderBy, clause.Limit, clause.Where) and more
|
|
|
|
// advanced techniques like specifying lock strength and optimizer hints. See the
|
|
|
|
// [docs] for more depth.
|
|
|
|
//
|
|
|
|
// // add a simple limit clause
|
|
|
|
// db.Clauses(clause.Limit{Limit: 1}).Find(&User{})
|
|
|
|
// // tell the optimizer to use the `idx_user_name` index
|
|
|
|
// db.Clauses(hints.UseIndex("idx_user_name")).Find(&User{})
|
|
|
|
// // specify the lock strength to UPDATE
|
|
|
|
// db.Clauses(clause.Locking{Strength: "UPDATE"}).Find(&users)
|
|
|
|
//
|
|
|
|
// [docs]: https://gorm.io/docs/sql_builder.html#Clauses
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Clauses(conds ...clause.Expression) (tx *DB) {
|
2020-01-30 10:14:48 +03:00
|
|
|
tx = db.getInstance()
|
|
|
|
var whereConds []interface{}
|
|
|
|
|
|
|
|
for _, cond := range conds {
|
|
|
|
if c, ok := cond.(clause.Interface); ok {
|
|
|
|
tx.Statement.AddClause(c)
|
2020-06-14 06:46:17 +03:00
|
|
|
} else if optimizer, ok := cond.(StatementModifier); ok {
|
|
|
|
optimizer.ModifyStatement(tx.Statement)
|
2020-01-30 10:14:48 +03:00
|
|
|
} else {
|
|
|
|
whereConds = append(whereConds, cond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(whereConds) > 0 {
|
2020-06-08 06:38:51 +03:00
|
|
|
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondition(whereConds[0], whereConds[1:]...)})
|
2020-01-30 10:14:48 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-02 16:46:27 +03:00
|
|
|
var tableRegexp = regexp.MustCompile(`(?i)(?:.+? AS (\w+)\s*(?:$|,)|^\w+\s+(\w+)$)`)
|
2020-07-10 16:11:28 +03:00
|
|
|
|
2020-01-29 14:22:44 +03:00
|
|
|
// Table specify the table you would like to run db operations
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// // Get a user
|
2023-04-26 17:19:06 +03:00
|
|
|
// db.Table("users").Take(&result)
|
2020-07-19 16:30:24 +03:00
|
|
|
func (db *DB) Table(name string, args ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-07-19 16:30:24 +03:00
|
|
|
if strings.Contains(name, " ") || strings.Contains(name, "`") || len(args) > 0 {
|
|
|
|
tx.Statement.TableExpr = &clause.Expr{SQL: name, Vars: args}
|
2023-01-02 16:46:27 +03:00
|
|
|
if results := tableRegexp.FindStringSubmatch(name); len(results) == 3 {
|
|
|
|
if results[1] != "" {
|
|
|
|
tx.Statement.Table = results[1]
|
|
|
|
} else {
|
|
|
|
tx.Statement.Table = results[2]
|
|
|
|
}
|
2020-07-10 16:11:28 +03:00
|
|
|
}
|
2020-07-19 16:30:24 +03:00
|
|
|
} else if tables := strings.Split(name, "."); len(tables) == 2 {
|
|
|
|
tx.Statement.TableExpr = &clause.Expr{SQL: tx.Statement.Quote(name)}
|
|
|
|
tx.Statement.Table = tables[1]
|
2022-03-18 08:38:46 +03:00
|
|
|
} else if name != "" {
|
2021-08-09 08:20:22 +03:00
|
|
|
tx.Statement.TableExpr = &clause.Expr{SQL: tx.Statement.Quote(name)}
|
|
|
|
tx.Statement.Table = name
|
2022-03-18 08:38:46 +03:00
|
|
|
} else {
|
|
|
|
tx.Statement.TableExpr = nil
|
|
|
|
tx.Statement.Table = ""
|
2020-07-10 16:11:28 +03:00
|
|
|
}
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-05 14:19:08 +03:00
|
|
|
// Distinct specify distinct fields that you want querying
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// // Select distinct names of users
|
|
|
|
// db.Distinct("name").Find(&results)
|
|
|
|
// // Select distinct name/age pairs from users
|
|
|
|
// db.Distinct("name", "age").Find(&results)
|
2020-06-05 14:19:08 +03:00
|
|
|
func (db *DB) Distinct(args ...interface{}) (tx *DB) {
|
2020-07-17 06:24:24 +03:00
|
|
|
tx = db.getInstance()
|
|
|
|
tx.Statement.Distinct = true
|
2020-06-05 14:19:08 +03:00
|
|
|
if len(args) > 0 {
|
|
|
|
tx = tx.Select(args[0], args[1:]...)
|
|
|
|
}
|
2020-08-18 14:03:09 +03:00
|
|
|
return
|
2020-06-05 14:19:08 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 14:22:44 +03:00
|
|
|
// Select specify fields that you want when querying, creating, updating
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// Use Select when you only want a subset of the fields. By default, GORM will select all fields.
|
|
|
|
// Select accepts both string arguments and arrays.
|
|
|
|
//
|
|
|
|
// // Select name and age of user using multiple arguments
|
|
|
|
// db.Select("name", "age").Find(&users)
|
|
|
|
// // Select name and age of user using an array
|
|
|
|
// db.Select([]string{"name", "age"}).Find(&users)
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-02-16 08:45:27 +03:00
|
|
|
|
|
|
|
switch v := query.(type) {
|
|
|
|
case []string:
|
|
|
|
tx.Statement.Selects = v
|
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
switch arg := arg.(type) {
|
|
|
|
case string:
|
|
|
|
tx.Statement.Selects = append(tx.Statement.Selects, arg)
|
|
|
|
case []string:
|
|
|
|
tx.Statement.Selects = append(tx.Statement.Selects, arg...)
|
|
|
|
default:
|
|
|
|
tx.AddError(fmt.Errorf("unsupported select args %v %v", query, args))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 15:57:33 +03:00
|
|
|
|
|
|
|
if clause, ok := tx.Statement.Clauses["SELECT"]; ok {
|
|
|
|
clause.Expression = nil
|
|
|
|
tx.Statement.Clauses["SELECT"] = clause
|
|
|
|
}
|
2020-02-16 08:45:27 +03:00
|
|
|
case string:
|
2020-12-28 05:40:30 +03:00
|
|
|
if strings.Count(v, "?") >= len(args) && len(args) > 0 {
|
2020-12-06 09:04:37 +03:00
|
|
|
tx.Statement.AddClause(clause.Select{
|
|
|
|
Distinct: db.Statement.Distinct,
|
|
|
|
Expression: clause.Expr{SQL: v, Vars: args},
|
|
|
|
})
|
2021-03-07 05:59:00 +03:00
|
|
|
} else if strings.Count(v, "@") > 0 && len(args) > 0 {
|
|
|
|
tx.Statement.AddClause(clause.Select{
|
|
|
|
Distinct: db.Statement.Distinct,
|
|
|
|
Expression: clause.NamedExpr{SQL: v, Vars: args},
|
|
|
|
})
|
2021-03-24 11:17:49 +03:00
|
|
|
} else {
|
2020-10-09 06:52:12 +03:00
|
|
|
tx.Statement.Selects = []string{v}
|
2020-02-16 08:45:27 +03:00
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
switch arg := arg.(type) {
|
|
|
|
case string:
|
|
|
|
tx.Statement.Selects = append(tx.Statement.Selects, arg)
|
|
|
|
case []string:
|
|
|
|
tx.Statement.Selects = append(tx.Statement.Selects, arg...)
|
|
|
|
default:
|
|
|
|
tx.Statement.AddClause(clause.Select{
|
2020-07-17 06:24:24 +03:00
|
|
|
Distinct: db.Statement.Distinct,
|
2020-02-16 08:45:27 +03:00
|
|
|
Expression: clause.Expr{SQL: v, Vars: args},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 13:58:53 +03:00
|
|
|
|
2022-03-18 15:57:33 +03:00
|
|
|
if clause, ok := tx.Statement.Clauses["SELECT"]; ok {
|
|
|
|
clause.Expression = nil
|
|
|
|
tx.Statement.Clauses["SELECT"] = clause
|
|
|
|
}
|
2020-02-16 08:45:27 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
tx.AddError(fmt.Errorf("unsupported select args %v %v", query, args))
|
|
|
|
}
|
|
|
|
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Omit specify fields that you want to ignore when creating, updating and querying
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Omit(columns ...string) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-02-16 08:45:27 +03:00
|
|
|
|
2020-02-19 07:53:46 +03:00
|
|
|
if len(columns) == 1 && strings.ContainsRune(columns[0], ',') {
|
2020-08-27 14:15:40 +03:00
|
|
|
tx.Statement.Omits = strings.FieldsFunc(columns[0], utils.IsValidDBNameChar)
|
2020-02-16 08:45:27 +03:00
|
|
|
} else {
|
|
|
|
tx.Statement.Omits = columns
|
|
|
|
}
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-12 03:39:42 +03:00
|
|
|
// Where add conditions
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// See the [docs] for details on the various formats that where clauses can take. By default, where clauses chain with AND.
|
|
|
|
//
|
|
|
|
// // Find the first user with name jinzhu
|
|
|
|
// db.Where("name = ?", "jinzhu").First(&user)
|
|
|
|
// // Find the first user with name jinzhu and age 20
|
|
|
|
// db.Where(&User{Name: "jinzhu", Age: 20}).First(&user)
|
|
|
|
// // Find the first user with name jinzhu and age not equal to 20
|
|
|
|
// db.Where("name = ?", "jinzhu").Where("age <> ?", "20").First(&user)
|
|
|
|
//
|
|
|
|
// [docs]: https://gorm.io/docs/query.html#Conditions
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Where(query interface{}, args ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-06-08 06:38:51 +03:00
|
|
|
if conds := tx.Statement.BuildCondition(query, args...); len(conds) > 0 {
|
2020-06-01 05:02:20 +03:00
|
|
|
tx.Statement.AddClause(clause.Where{Exprs: conds})
|
|
|
|
}
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-12 03:39:42 +03:00
|
|
|
// Not add NOT conditions
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// Not works similarly to where, and has the same syntax.
|
|
|
|
//
|
|
|
|
// // Find the first user with name not equal to jinzhu
|
|
|
|
// db.Not("name = ?", "jinzhu").First(&user)
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Not(query interface{}, args ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-06-08 06:38:51 +03:00
|
|
|
if conds := tx.Statement.BuildCondition(query, args...); len(conds) > 0 {
|
2020-06-01 05:02:20 +03:00
|
|
|
tx.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.Not(conds...)}})
|
|
|
|
}
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Or add OR conditions
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// Or is used to chain together queries with an OR.
|
|
|
|
//
|
|
|
|
// // Find the first user with name equal to jinzhu or john
|
|
|
|
// db.Where("name = ?", "jinzhu").Or("name = ?", "john").First(&user)
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Or(query interface{}, args ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-06-08 06:38:51 +03:00
|
|
|
if conds := tx.Statement.BuildCondition(query, args...); len(conds) > 0 {
|
2020-07-05 07:23:45 +03:00
|
|
|
tx.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.Or(clause.And(conds...))}})
|
2020-06-01 05:02:20 +03:00
|
|
|
}
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Joins specify Joins conditions
|
2022-11-02 05:28:00 +03:00
|
|
|
//
|
|
|
|
// db.Joins("Account").Find(&user)
|
|
|
|
// db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu@example.org").Find(&user)
|
|
|
|
// db.Joins("Account", DB.Select("id").Where("user_id = users.id AND name = ?", "someName").Model(&Account{}))
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Joins(query string, args ...interface{}) (tx *DB) {
|
2022-12-24 07:27:38 +03:00
|
|
|
return joins(db, clause.LeftJoin, query, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InnerJoins specify inner joins conditions
|
|
|
|
// db.InnerJoins("Account").Find(&user)
|
|
|
|
func (db *DB) InnerJoins(query string, args ...interface{}) (tx *DB) {
|
|
|
|
return joins(db, clause.InnerJoin, query, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func joins(db *DB, joinType clause.JoinType, query string, args ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
|
|
|
|
2021-09-28 17:37:15 +03:00
|
|
|
if len(args) == 1 {
|
2021-08-28 11:27:19 +03:00
|
|
|
if db, ok := args[0].(*DB); ok {
|
2023-02-18 04:13:36 +03:00
|
|
|
j := join{
|
|
|
|
Name: query, Conds: args, Selects: db.Statement.Selects,
|
|
|
|
Omits: db.Statement.Omits, JoinType: joinType,
|
|
|
|
}
|
2021-09-07 16:21:44 +03:00
|
|
|
if where, ok := db.Statement.Clauses["WHERE"].Expression.(clause.Where); ok {
|
2022-11-02 05:28:00 +03:00
|
|
|
j.On = &where
|
2021-09-07 16:21:44 +03:00
|
|
|
}
|
2022-11-02 05:28:00 +03:00
|
|
|
tx.Statement.Joins = append(tx.Statement.Joins, j)
|
|
|
|
return
|
2021-08-28 11:27:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-24 07:27:38 +03:00
|
|
|
tx.Statement.Joins = append(tx.Statement.Joins, join{Name: query, Conds: args, JoinType: joinType})
|
2021-07-08 11:04:40 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-29 14:22:44 +03:00
|
|
|
// Group specify the group method on the find
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// // Select the sum age of users with given names
|
|
|
|
// db.Model(&User{}).Select("name, sum(age) as total").Group("name").Find(&results)
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Group(name string) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-06-30 18:06:48 +03:00
|
|
|
|
2020-08-27 14:15:40 +03:00
|
|
|
fields := strings.FieldsFunc(name, utils.IsValidDBNameChar)
|
2020-03-08 13:05:22 +03:00
|
|
|
tx.Statement.AddClause(clause.GroupBy{
|
2020-06-30 18:06:48 +03:00
|
|
|
Columns: []clause.Column{{Name: name, Raw: len(fields) != 1}},
|
2020-03-08 13:05:22 +03:00
|
|
|
})
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Having specify HAVING conditions for GROUP BY
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// // Select the sum age of users with name jinzhu
|
|
|
|
// db.Model(&User{}).Select("name, sum(age) as total").Group("name").Having("name = ?", "jinzhu").Find(&result)
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Having(query interface{}, args ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-03-08 13:05:22 +03:00
|
|
|
tx.Statement.AddClause(clause.GroupBy{
|
2020-06-08 06:38:51 +03:00
|
|
|
Having: tx.Statement.BuildCondition(query, args...),
|
2020-03-08 13:05:22 +03:00
|
|
|
})
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 11:51:01 +03:00
|
|
|
// Order specify order when retrieving records from database
|
2022-11-02 05:28:00 +03:00
|
|
|
//
|
|
|
|
// db.Order("name DESC")
|
|
|
|
// db.Order(clause.OrderByColumn{Column: clause.Column{Name: "name"}, Desc: true})
|
2024-06-12 12:47:34 +03:00
|
|
|
// db.Order(clause.OrderBy{Columns: []clause.OrderByColumn{
|
|
|
|
// {Column: clause.Column{Name: "name"}, Desc: true},
|
|
|
|
// {Column: clause.Column{Name: "age"}, Desc: true},
|
|
|
|
// }})
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Order(value interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-02-04 03:56:15 +03:00
|
|
|
|
|
|
|
switch v := value.(type) {
|
2024-06-12 12:47:34 +03:00
|
|
|
case clause.OrderBy:
|
|
|
|
tx.Statement.AddClause(v)
|
2020-02-07 18:45:35 +03:00
|
|
|
case clause.OrderByColumn:
|
2020-02-16 08:45:27 +03:00
|
|
|
tx.Statement.AddClause(clause.OrderBy{
|
2020-02-07 18:45:35 +03:00
|
|
|
Columns: []clause.OrderByColumn{v},
|
2020-02-04 03:56:15 +03:00
|
|
|
})
|
2021-08-09 08:14:23 +03:00
|
|
|
case string:
|
|
|
|
if v != "" {
|
|
|
|
tx.Statement.AddClause(clause.OrderBy{
|
|
|
|
Columns: []clause.OrderByColumn{{
|
|
|
|
Column: clause.Column{Name: v, Raw: true},
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
}
|
2020-02-04 03:56:15 +03:00
|
|
|
}
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit specify the number of records to be retrieved
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// Limit conditions can be cancelled by using `Limit(-1)`.
|
|
|
|
//
|
|
|
|
// // retrieve 3 users
|
|
|
|
// db.Limit(3).Find(&users)
|
|
|
|
// // retrieve 3 users into users1, and all users into users2
|
|
|
|
// db.Limit(3).Find(&users1).Limit(-1).Find(&users2)
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Limit(limit int) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2022-10-07 15:14:14 +03:00
|
|
|
tx.Statement.AddClause(clause.Limit{Limit: &limit})
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offset specify the number of records to skip before starting to return the records
|
2022-12-23 11:51:01 +03:00
|
|
|
//
|
|
|
|
// Offset conditions can be cancelled by using `Offset(-1)`.
|
|
|
|
//
|
|
|
|
// // select the third user
|
|
|
|
// db.Offset(2).First(&user)
|
|
|
|
// // select the first user by cancelling an earlier chained offset
|
|
|
|
// db.Offset(5).Offset(-1).First(&user)
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Offset(offset int) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-03-04 18:56:42 +03:00
|
|
|
tx.Statement.AddClause(clause.Limit{Offset: offset})
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-09 10:32:55 +03:00
|
|
|
// Scopes pass current database connection to arguments `func(DB) DB`, which could be used to add conditions dynamically
|
2020-01-29 14:22:44 +03:00
|
|
|
//
|
2022-11-02 05:28:00 +03:00
|
|
|
// func AmountGreaterThan1000(db *gorm.DB) *gorm.DB {
|
|
|
|
// return db.Where("amount > ?", 1000)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func OrderStatus(status []string) func (db *gorm.DB) *gorm.DB {
|
|
|
|
// return func (db *gorm.DB) *gorm.DB {
|
|
|
|
// return db.Scopes(AmountGreaterThan1000).Where("status in (?)", status)
|
|
|
|
// }
|
|
|
|
// }
|
2020-01-29 14:22:44 +03:00
|
|
|
//
|
2022-11-02 05:28:00 +03:00
|
|
|
// db.Scopes(AmountGreaterThan1000, OrderStatus([]string{"paid", "shipped"})).Find(&orders)
|
2021-02-25 15:06:26 +03:00
|
|
|
func (db *DB) Scopes(funcs ...func(*DB) *DB) (tx *DB) {
|
|
|
|
tx = db.getInstance()
|
|
|
|
tx.Statement.scopes = append(tx.Statement.scopes, funcs...)
|
|
|
|
return tx
|
2020-01-29 14:22:44 +03:00
|
|
|
}
|
|
|
|
|
2023-04-11 07:01:23 +03:00
|
|
|
func (db *DB) executeScopes() (tx *DB) {
|
|
|
|
scopes := db.Statement.scopes
|
2024-01-12 11:42:21 +03:00
|
|
|
db.Statement.scopes = nil
|
2023-04-11 07:01:23 +03:00
|
|
|
for _, scope := range scopes {
|
2024-01-12 11:42:21 +03:00
|
|
|
db = scope(db)
|
2023-04-11 07:01:23 +03:00
|
|
|
}
|
2024-01-12 11:42:21 +03:00
|
|
|
return db
|
2023-04-11 07:01:23 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 14:22:44 +03:00
|
|
|
// Preload preload associations with given conditions
|
2022-11-02 05:28:00 +03:00
|
|
|
//
|
2022-12-23 11:51:01 +03:00
|
|
|
// // get all users, and preload all non-cancelled orders
|
2022-11-02 05:28:00 +03:00
|
|
|
// db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
|
2020-04-15 04:14:24 +03:00
|
|
|
func (db *DB) Preload(query string, args ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-04-15 04:14:24 +03:00
|
|
|
if tx.Statement.Preloads == nil {
|
|
|
|
tx.Statement.Preloads = map[string][]interface{}{}
|
|
|
|
}
|
|
|
|
tx.Statement.Preloads[query] = args
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 11:51:01 +03:00
|
|
|
// Attrs provide attributes used in [FirstOrCreate] or [FirstOrInit]
|
|
|
|
//
|
|
|
|
// Attrs only adds attributes if the record is not found.
|
|
|
|
//
|
|
|
|
// // assign an email if the record is not found
|
|
|
|
// db.Where(User{Name: "non_existing"}).Attrs(User{Email: "fake@fake.org"}).FirstOrInit(&user)
|
|
|
|
// // user -> User{Name: "non_existing", Email: "fake@fake.org"}
|
|
|
|
//
|
|
|
|
// // assign an email if the record is not found, otherwise ignore provided email
|
|
|
|
// db.Where(User{Name: "jinzhu"}).Attrs(User{Email: "fake@fake.org"}).FirstOrInit(&user)
|
|
|
|
// // user -> User{Name: "jinzhu", Age: 20}
|
|
|
|
//
|
|
|
|
// [FirstOrCreate]: https://gorm.io/docs/advanced_query.html#FirstOrCreate
|
|
|
|
// [FirstOrInit]: https://gorm.io/docs/advanced_query.html#FirstOrInit
|
2020-05-28 08:12:56 +03:00
|
|
|
func (db *DB) Attrs(attrs ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-05-28 08:12:56 +03:00
|
|
|
tx.Statement.attrs = attrs
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 11:51:01 +03:00
|
|
|
// Assign provide attributes used in [FirstOrCreate] or [FirstOrInit]
|
|
|
|
//
|
|
|
|
// Assign adds attributes even if the record is found. If using FirstOrCreate, this means that
|
|
|
|
// records will be updated even if they are found.
|
|
|
|
//
|
|
|
|
// // assign an email regardless of if the record is not found
|
|
|
|
// db.Where(User{Name: "non_existing"}).Assign(User{Email: "fake@fake.org"}).FirstOrInit(&user)
|
|
|
|
// // user -> User{Name: "non_existing", Email: "fake@fake.org"}
|
|
|
|
//
|
|
|
|
// // assign email regardless of if record is found
|
|
|
|
// db.Where(User{Name: "jinzhu"}).Assign(User{Email: "fake@fake.org"}).FirstOrInit(&user)
|
|
|
|
// // user -> User{Name: "jinzhu", Age: 20, Email: "fake@fake.org"}
|
|
|
|
//
|
|
|
|
// [FirstOrCreate]: https://gorm.io/docs/advanced_query.html#FirstOrCreate
|
|
|
|
// [FirstOrInit]: https://gorm.io/docs/advanced_query.html#FirstOrInit
|
2020-05-28 08:12:56 +03:00
|
|
|
func (db *DB) Assign(attrs ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-05-28 08:12:56 +03:00
|
|
|
tx.Statement.assigns = attrs
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-17 06:38:55 +03:00
|
|
|
// Unscoped disables the global scope of soft deletion in a query.
|
|
|
|
// By default, GORM uses soft deletion, marking records as "deleted"
|
|
|
|
// by setting a timestamp on a specific field (e.g., `deleted_at`).
|
|
|
|
// Unscoped allows queries to include records marked as deleted,
|
|
|
|
// overriding the soft deletion behavior.
|
|
|
|
// Example:
|
|
|
|
// var users []User
|
|
|
|
// db.Unscoped().Find(&users)
|
|
|
|
// // Retrieves all users, including deleted ones.
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Unscoped() (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-05-29 02:35:45 +03:00
|
|
|
tx.Statement.Unscoped = true
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Raw(sql string, values ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-02-22 15:57:29 +03:00
|
|
|
tx.Statement.SQL = strings.Builder{}
|
2020-07-10 07:28:24 +03:00
|
|
|
|
|
|
|
if strings.Contains(sql, "@") {
|
|
|
|
clause.NamedExpr{SQL: sql, Vars: values}.Build(tx.Statement)
|
|
|
|
} else {
|
|
|
|
clause.Expr{SQL: sql, Vars: values}.Build(tx.Statement)
|
|
|
|
}
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|