2020-02-02 09:40:44 +03:00
|
|
|
package callbacks
|
|
|
|
|
2020-02-04 03:56:15 +03:00
|
|
|
import (
|
2020-04-15 04:14:24 +03:00
|
|
|
"fmt"
|
2020-02-23 16:22:35 +03:00
|
|
|
"reflect"
|
2020-05-07 05:03:48 +03:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-02-23 16:22:35 +03:00
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
"gorm.io/gorm/schema"
|
2020-02-04 03:56:15 +03:00
|
|
|
)
|
2020-02-02 09:40:44 +03:00
|
|
|
|
|
|
|
func Query(db *gorm.DB) {
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Error == nil {
|
|
|
|
if db.Statement.Schema != nil && !db.Statement.Unscoped {
|
|
|
|
for _, c := range db.Statement.Schema.QueryClauses {
|
|
|
|
db.Statement.AddClause(c)
|
|
|
|
}
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Statement.SQL.String() == "" {
|
|
|
|
BuildQuerySQL(db)
|
|
|
|
}
|
2020-04-15 04:14:24 +03:00
|
|
|
|
2020-07-24 03:32:50 +03:00
|
|
|
if !db.DryRun && db.Error == nil {
|
2020-06-01 16:26:23 +03:00
|
|
|
rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
|
|
|
if err != nil {
|
|
|
|
db.AddError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2020-05-31 14:23:32 +03:00
|
|
|
|
2020-06-01 16:26:23 +03:00
|
|
|
gorm.Scan(rows, db, false)
|
|
|
|
}
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-05-31 14:23:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func BuildQuerySQL(db *gorm.DB) {
|
2020-06-08 15:23:47 +03:00
|
|
|
db.Statement.SQL.Grow(100)
|
2020-06-05 14:19:08 +03:00
|
|
|
clauseSelect := clause.Select{Distinct: db.Statement.Distinct}
|
2020-05-31 14:23:32 +03:00
|
|
|
|
2020-07-01 03:56:21 +03:00
|
|
|
if db.Statement.ReflectValue.Kind() == reflect.Struct && db.Statement.ReflectValue.Type() == db.Statement.Schema.ModelType {
|
2020-06-01 14:41:33 +03:00
|
|
|
var conds []clause.Expression
|
|
|
|
for _, primaryField := range db.Statement.Schema.PrimaryFields {
|
|
|
|
if v, isZero := primaryField.ValueOf(db.Statement.ReflectValue); !isZero {
|
|
|
|
conds = append(conds, clause.Eq{Column: clause.Column{Table: db.Statement.Table, Name: primaryField.DBName}, Value: v})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(conds) > 0 {
|
|
|
|
db.Statement.AddClause(clause.Where{Exprs: conds})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 14:23:32 +03:00
|
|
|
if len(db.Statement.Selects) > 0 {
|
2020-06-08 08:45:41 +03:00
|
|
|
clauseSelect.Columns = make([]clause.Column, len(db.Statement.Selects))
|
|
|
|
for idx, name := range db.Statement.Selects {
|
2020-05-31 14:23:32 +03:00
|
|
|
if db.Statement.Schema == nil {
|
2020-06-08 08:45:41 +03:00
|
|
|
clauseSelect.Columns[idx] = clause.Column{Name: name, Raw: true}
|
2020-05-31 14:23:32 +03:00
|
|
|
} else if f := db.Statement.Schema.LookUpField(name); f != nil {
|
2020-06-08 08:45:41 +03:00
|
|
|
clauseSelect.Columns[idx] = clause.Column{Name: f.DBName}
|
2020-05-31 14:23:32 +03:00
|
|
|
} else {
|
2020-06-08 08:45:41 +03:00
|
|
|
clauseSelect.Columns[idx] = clause.Column{Name: name, Raw: true}
|
2020-04-15 04:14:24 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 16:48:58 +03:00
|
|
|
} else if db.Statement.Schema != nil && len(db.Statement.Omits) > 0 {
|
|
|
|
selectColumns, _ := db.Statement.SelectAndOmitColumns(false, false)
|
|
|
|
clauseSelect.Columns = make([]clause.Column, 0, len(db.Statement.Schema.DBNames))
|
|
|
|
for _, dbName := range db.Statement.Schema.DBNames {
|
|
|
|
if v, ok := selectColumns[dbName]; (ok && v) || !ok {
|
|
|
|
clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{Name: dbName})
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 07:15:35 +03:00
|
|
|
} else if db.Statement.Schema != nil && db.Statement.ReflectValue.IsValid() {
|
|
|
|
smallerStruct := false
|
|
|
|
switch db.Statement.ReflectValue.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
smallerStruct = db.Statement.ReflectValue.Type() != db.Statement.Schema.ModelType
|
|
|
|
case reflect.Slice:
|
|
|
|
smallerStruct = db.Statement.ReflectValue.Type().Elem() != db.Statement.Schema.ModelType
|
|
|
|
}
|
|
|
|
|
|
|
|
if smallerStruct {
|
|
|
|
stmt := gorm.Statement{DB: db}
|
|
|
|
// smaller struct
|
|
|
|
if err := stmt.Parse(db.Statement.Dest); err == nil && stmt.Schema.ModelType != db.Statement.Schema.ModelType {
|
|
|
|
clauseSelect.Columns = make([]clause.Column, len(stmt.Schema.DBNames))
|
2020-07-01 03:56:21 +03:00
|
|
|
|
2020-07-09 07:15:35 +03:00
|
|
|
for idx, dbName := range stmt.Schema.DBNames {
|
|
|
|
clauseSelect.Columns[idx] = clause.Column{Name: dbName}
|
|
|
|
}
|
2020-07-01 03:56:21 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 14:23:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// inline joins
|
|
|
|
if len(db.Statement.Joins) != 0 {
|
|
|
|
if len(db.Statement.Selects) == 0 {
|
2020-06-08 08:45:41 +03:00
|
|
|
clauseSelect.Columns = make([]clause.Column, len(db.Statement.Schema.DBNames))
|
|
|
|
for idx, dbName := range db.Statement.Schema.DBNames {
|
|
|
|
clauseSelect.Columns[idx] = clause.Column{Table: db.Statement.Table, Name: dbName}
|
2020-05-31 14:23:32 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-15 04:14:24 +03:00
|
|
|
|
2020-06-08 08:45:41 +03:00
|
|
|
joins := []clause.Join{}
|
2020-05-31 14:23:32 +03:00
|
|
|
for name, conds := range db.Statement.Joins {
|
|
|
|
if db.Statement.Schema == nil {
|
|
|
|
joins = append(joins, clause.Join{
|
|
|
|
Expression: clause.Expr{SQL: name, Vars: conds},
|
|
|
|
})
|
|
|
|
} else if relation, ok := db.Statement.Schema.Relationships.Relations[name]; ok {
|
|
|
|
tableAliasName := relation.Name
|
|
|
|
|
|
|
|
for _, s := range relation.FieldSchema.DBNames {
|
2020-04-15 04:14:24 +03:00
|
|
|
clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{
|
2020-05-31 14:23:32 +03:00
|
|
|
Table: tableAliasName,
|
|
|
|
Name: s,
|
|
|
|
Alias: tableAliasName + "__" + s,
|
2020-04-15 04:14:24 +03:00
|
|
|
})
|
|
|
|
}
|
2020-05-29 19:16:33 +03:00
|
|
|
|
2020-06-08 08:45:41 +03:00
|
|
|
exprs := make([]clause.Expression, len(relation.References))
|
|
|
|
for idx, ref := range relation.References {
|
2020-05-31 14:23:32 +03:00
|
|
|
if ref.OwnPrimaryKey {
|
2020-06-08 08:45:41 +03:00
|
|
|
exprs[idx] = clause.Eq{
|
2020-07-31 09:47:26 +03:00
|
|
|
Column: clause.Column{Table: clause.CurrentTable, Name: ref.PrimaryKey.DBName},
|
2020-05-31 14:23:32 +03:00
|
|
|
Value: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
|
2020-06-08 08:45:41 +03:00
|
|
|
}
|
2020-05-31 14:23:32 +03:00
|
|
|
} else {
|
|
|
|
if ref.PrimaryValue == "" {
|
2020-06-08 08:45:41 +03:00
|
|
|
exprs[idx] = clause.Eq{
|
2020-07-31 09:47:26 +03:00
|
|
|
Column: clause.Column{Table: clause.CurrentTable, Name: ref.ForeignKey.DBName},
|
2020-05-31 14:23:32 +03:00
|
|
|
Value: clause.Column{Table: tableAliasName, Name: ref.PrimaryKey.DBName},
|
2020-06-08 08:45:41 +03:00
|
|
|
}
|
2020-04-15 04:14:24 +03:00
|
|
|
} else {
|
2020-06-08 08:45:41 +03:00
|
|
|
exprs[idx] = clause.Eq{
|
2020-05-31 14:23:32 +03:00
|
|
|
Column: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
|
|
|
|
Value: ref.PrimaryValue,
|
2020-06-08 08:45:41 +03:00
|
|
|
}
|
2020-04-15 04:14:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 14:23:32 +03:00
|
|
|
joins = append(joins, clause.Join{
|
|
|
|
Type: clause.LeftJoin,
|
|
|
|
Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName},
|
|
|
|
ON: clause.Where{Exprs: exprs},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
joins = append(joins, clause.Join{
|
|
|
|
Expression: clause.Expr{SQL: name, Vars: conds},
|
|
|
|
})
|
|
|
|
}
|
2020-04-15 04:14:24 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 14:23:32 +03:00
|
|
|
db.Statement.AddClause(clause.From{Joins: joins})
|
|
|
|
} else {
|
|
|
|
db.Statement.AddClauseIfNotExists(clause.From{})
|
2020-02-22 15:57:29 +03:00
|
|
|
}
|
2020-02-04 03:56:15 +03:00
|
|
|
|
2020-05-31 15:21:52 +03:00
|
|
|
db.Statement.AddClauseIfNotExists(clauseSelect)
|
2020-03-03 09:18:12 +03:00
|
|
|
|
2020-05-31 14:23:32 +03:00
|
|
|
db.Statement.Build("SELECT", "FROM", "WHERE", "GROUP BY", "ORDER BY", "LIMIT", "FOR")
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Preload(db *gorm.DB) {
|
2020-06-08 08:45:41 +03:00
|
|
|
if db.Error == nil && len(db.Statement.Preloads) > 0 {
|
|
|
|
preloadMap := map[string][]string{}
|
|
|
|
for name := range db.Statement.Preloads {
|
2020-06-20 12:21:01 +03:00
|
|
|
if name == clause.Associations {
|
|
|
|
for _, rel := range db.Statement.Schema.Relationships.Relations {
|
|
|
|
if rel.Schema == db.Statement.Schema {
|
|
|
|
preloadMap[rel.Name] = []string{rel.Name}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
preloadFields := strings.Split(name, ".")
|
|
|
|
for idx := range preloadFields {
|
|
|
|
preloadMap[strings.Join(preloadFields[:idx+1], ".")] = preloadFields[:idx+1]
|
|
|
|
}
|
2020-05-07 05:03:48 +03:00
|
|
|
}
|
2020-06-08 08:45:41 +03:00
|
|
|
}
|
2020-05-07 05:03:48 +03:00
|
|
|
|
2020-06-08 08:45:41 +03:00
|
|
|
preloadNames := make([]string, len(preloadMap))
|
|
|
|
idx := 0
|
|
|
|
for key := range preloadMap {
|
|
|
|
preloadNames[idx] = key
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
sort.Strings(preloadNames)
|
|
|
|
|
|
|
|
for _, name := range preloadNames {
|
|
|
|
var (
|
|
|
|
curSchema = db.Statement.Schema
|
|
|
|
preloadFields = preloadMap[name]
|
|
|
|
rels = make([]*schema.Relationship, len(preloadFields))
|
|
|
|
)
|
|
|
|
|
|
|
|
for idx, preloadField := range preloadFields {
|
|
|
|
if rel := curSchema.Relationships.Relations[preloadField]; rel != nil {
|
|
|
|
rels[idx] = rel
|
|
|
|
curSchema = rel.FieldSchema
|
|
|
|
} else {
|
|
|
|
db.AddError(fmt.Errorf("%v: %w", name, gorm.ErrUnsupportedRelation))
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 08:45:41 +03:00
|
|
|
|
|
|
|
preload(db, rels, db.Statement.Preloads[name])
|
2020-05-07 05:03:48 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func AfterQuery(db *gorm.DB) {
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Error == nil && db.Statement.Schema != nil && db.Statement.Schema.AfterFind {
|
2020-06-05 16:23:20 +03:00
|
|
|
callMethod(db, func(value interface{}, tx *gorm.DB) bool {
|
|
|
|
if i, ok := value.(gorm.AfterFindInterface); ok {
|
|
|
|
db.AddError(i.AfterFind(tx))
|
|
|
|
return true
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
|
|
|
return false
|
2020-06-05 16:23:20 +03:00
|
|
|
})
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|