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"
|
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 {
|
2020-11-04 06:03:22 +03:00
|
|
|
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
|
|
|
|
}
|
2021-10-26 17:36:37 +03:00
|
|
|
gorm.Scan(rows, db, 0)
|
2021-12-02 05:39:24 +03:00
|
|
|
db.AddError(rows.Close())
|
2020-06-01 16:26:23 +03:00
|
|
|
}
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-05-31 14:23:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func BuildQuerySQL(db *gorm.DB) {
|
2021-12-16 05:41:34 +03:00
|
|
|
if db.Statement.Schema != nil {
|
2020-11-04 06:03:22 +03:00
|
|
|
for _, c := range db.Statement.Schema.QueryClauses {
|
|
|
|
db.Statement.AddClause(c)
|
2020-06-01 14:41:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 08:58:06 +03:00
|
|
|
if db.Statement.SQL.Len() == 0 {
|
2020-11-04 06:03:22 +03:00
|
|
|
db.Statement.SQL.Grow(100)
|
|
|
|
clauseSelect := clause.Select{Distinct: db.Statement.Distinct}
|
|
|
|
|
|
|
|
if db.Statement.ReflectValue.Kind() == reflect.Struct && db.Statement.ReflectValue.Type() == db.Statement.Schema.ModelType {
|
|
|
|
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})
|
|
|
|
}
|
2020-04-15 04:14:24 +03:00
|
|
|
}
|
2020-11-04 06:03:22 +03:00
|
|
|
|
|
|
|
if len(conds) > 0 {
|
|
|
|
db.Statement.AddClause(clause.Where{Exprs: conds})
|
2020-07-19 16:48:58 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-09 07:15:35 +03:00
|
|
|
|
2020-11-04 06:03:22 +03:00
|
|
|
if len(db.Statement.Selects) > 0 {
|
|
|
|
clauseSelect.Columns = make([]clause.Column, len(db.Statement.Selects))
|
|
|
|
for idx, name := range db.Statement.Selects {
|
|
|
|
if db.Statement.Schema == nil {
|
|
|
|
clauseSelect.Columns[idx] = clause.Column{Name: name, Raw: true}
|
|
|
|
} else if f := db.Statement.Schema.LookUpField(name); f != nil {
|
|
|
|
clauseSelect.Columns[idx] = clause.Column{Name: f.DBName}
|
|
|
|
} else {
|
|
|
|
clauseSelect.Columns[idx] = clause.Column{Name: name, Raw: true}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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 {
|
2020-11-20 10:38:25 +03:00
|
|
|
clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{Table: db.Statement.Table, Name: dbName})
|
2020-11-04 06:03:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if db.Statement.Schema != nil && db.Statement.ReflectValue.IsValid() {
|
2020-11-20 10:44:39 +03:00
|
|
|
queryFields := db.QueryFields
|
|
|
|
if !queryFields {
|
2020-11-20 10:38:25 +03:00
|
|
|
switch db.Statement.ReflectValue.Kind() {
|
|
|
|
case reflect.Struct:
|
2020-11-20 10:44:39 +03:00
|
|
|
queryFields = db.Statement.ReflectValue.Type() != db.Statement.Schema.ModelType
|
2020-11-20 10:38:25 +03:00
|
|
|
case reflect.Slice:
|
2020-11-20 10:44:39 +03:00
|
|
|
queryFields = db.Statement.ReflectValue.Type().Elem() != db.Statement.Schema.ModelType
|
2020-11-20 10:38:25 +03:00
|
|
|
}
|
2020-11-20 10:44:39 +03:00
|
|
|
}
|
2020-11-20 10:38:25 +03:00
|
|
|
|
2020-11-20 10:44:39 +03:00
|
|
|
if queryFields {
|
2020-11-04 06:03:22 +03:00
|
|
|
stmt := gorm.Statement{DB: db}
|
|
|
|
// smaller struct
|
2020-11-20 10:44:39 +03:00
|
|
|
if err := stmt.Parse(db.Statement.Dest); err == nil && (db.QueryFields || stmt.Schema.ModelType != db.Statement.Schema.ModelType) {
|
2020-11-04 06:03:22 +03:00
|
|
|
clauseSelect.Columns = make([]clause.Column, len(stmt.Schema.DBNames))
|
2020-07-01 03:56:21 +03:00
|
|
|
|
2020-11-04 06:03:22 +03:00
|
|
|
for idx, dbName := range stmt.Schema.DBNames {
|
2020-11-20 10:38:25 +03:00
|
|
|
clauseSelect.Columns[idx] = clause.Column{Table: db.Statement.Table, Name: dbName}
|
2020-11-04 06:03:22 +03:00
|
|
|
}
|
2020-07-09 07:15:35 +03:00
|
|
|
}
|
2020-07-01 03:56:21 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 14:23:32 +03:00
|
|
|
|
2020-11-04 06:03:22 +03:00
|
|
|
// inline joins
|
2021-10-09 11:55:45 +03:00
|
|
|
joins := []clause.Join{}
|
|
|
|
if fromClause, ok := db.Statement.Clauses["FROM"].Expression.(clause.From); ok {
|
|
|
|
joins = fromClause.Joins
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(db.Statement.Joins) != 0 || len(joins) != 0 {
|
2020-11-04 06:03:22 +03:00
|
|
|
if len(db.Statement.Selects) == 0 && db.Statement.Schema != nil {
|
|
|
|
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-11-04 06:03:22 +03:00
|
|
|
for _, join := range db.Statement.Joins {
|
|
|
|
if db.Statement.Schema == nil {
|
|
|
|
joins = append(joins, clause.Join{
|
2020-12-07 05:31:06 +03:00
|
|
|
Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds},
|
2020-04-15 04:14:24 +03:00
|
|
|
})
|
2020-11-04 06:03:22 +03:00
|
|
|
} else if relation, ok := db.Statement.Schema.Relationships.Relations[join.Name]; ok {
|
|
|
|
tableAliasName := relation.Name
|
|
|
|
|
|
|
|
for _, s := range relation.FieldSchema.DBNames {
|
|
|
|
clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{
|
|
|
|
Table: tableAliasName,
|
|
|
|
Name: s,
|
|
|
|
Alias: tableAliasName + "__" + s,
|
|
|
|
})
|
|
|
|
}
|
2020-05-29 19:16:33 +03:00
|
|
|
|
2021-09-07 16:21:44 +03:00
|
|
|
exprs := make([]clause.Expression, len(relation.References))
|
|
|
|
for idx, ref := range relation.References {
|
|
|
|
if ref.OwnPrimaryKey {
|
|
|
|
exprs[idx] = clause.Eq{
|
|
|
|
Column: clause.Column{Table: clause.CurrentTable, Name: ref.PrimaryKey.DBName},
|
|
|
|
Value: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ref.PrimaryValue == "" {
|
2020-11-04 06:03:22 +03:00
|
|
|
exprs[idx] = clause.Eq{
|
2021-09-07 16:21:44 +03:00
|
|
|
Column: clause.Column{Table: clause.CurrentTable, Name: ref.ForeignKey.DBName},
|
|
|
|
Value: clause.Column{Table: tableAliasName, Name: ref.PrimaryKey.DBName},
|
2020-11-04 06:03:22 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-07 16:21:44 +03:00
|
|
|
exprs[idx] = clause.Eq{
|
|
|
|
Column: clause.Column{Table: tableAliasName, Name: ref.ForeignKey.DBName},
|
|
|
|
Value: ref.PrimaryValue,
|
2020-11-04 06:03:22 +03:00
|
|
|
}
|
2020-06-08 08:45:41 +03:00
|
|
|
}
|
2020-04-15 04:14:24 +03:00
|
|
|
}
|
2021-09-07 16:21:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if join.On != nil {
|
|
|
|
onStmt := gorm.Statement{Table: tableAliasName, DB: db}
|
|
|
|
join.On.Build(&onStmt)
|
|
|
|
onSQL := onStmt.SQL.String()
|
|
|
|
vars := onStmt.Vars
|
|
|
|
for idx, v := range onStmt.Vars {
|
|
|
|
bindvar := strings.Builder{}
|
|
|
|
onStmt.Vars = vars[0 : idx+1]
|
|
|
|
db.Dialector.BindVarTo(&bindvar, &onStmt, v)
|
|
|
|
onSQL = strings.Replace(onSQL, bindvar.String(), "?", 1)
|
|
|
|
}
|
2021-07-17 16:45:15 +03:00
|
|
|
|
2021-09-07 16:21:44 +03:00
|
|
|
exprs = append(exprs, clause.Expr{SQL: onSQL, Vars: vars})
|
2020-04-15 04:14:24 +03:00
|
|
|
}
|
2021-09-07 16:21:44 +03:00
|
|
|
|
|
|
|
joins = append(joins, clause.Join{
|
|
|
|
Type: clause.LeftJoin,
|
|
|
|
Table: clause.Table{Name: relation.FieldSchema.Table, Alias: tableAliasName},
|
|
|
|
ON: clause.Where{Exprs: exprs},
|
|
|
|
})
|
2020-11-04 06:03:22 +03:00
|
|
|
} else {
|
|
|
|
joins = append(joins, clause.Join{
|
2020-12-07 05:31:06 +03:00
|
|
|
Expression: clause.NamedExpr{SQL: join.Name, Vars: join.Conds},
|
2020-11-04 06:03:22 +03:00
|
|
|
})
|
|
|
|
}
|
2020-05-31 14:23:32 +03:00
|
|
|
}
|
2020-04-15 04:14:24 +03:00
|
|
|
|
2021-03-05 17:18:12 +03:00
|
|
|
db.Statement.Joins = nil
|
2020-11-04 06:03:22 +03:00
|
|
|
db.Statement.AddClause(clause.From{Joins: joins})
|
|
|
|
} else {
|
|
|
|
db.Statement.AddClauseIfNotExists(clause.From{})
|
|
|
|
}
|
2020-02-04 03:56:15 +03:00
|
|
|
|
2020-11-04 06:03:22 +03:00
|
|
|
db.Statement.AddClauseIfNotExists(clauseSelect)
|
2020-03-03 09:18:12 +03:00
|
|
|
|
2021-04-28 12:19:30 +03:00
|
|
|
db.Statement.Build(db.Statement.BuildClauses...)
|
2020-11-04 06:03:22 +03:00
|
|
|
}
|
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 {
|
2021-01-26 11:33:19 +03:00
|
|
|
preloadMap := map[string]map[string][]interface{}{}
|
2020-06-08 08:45:41 +03:00
|
|
|
for name := range db.Statement.Preloads {
|
2021-03-14 05:18:43 +03:00
|
|
|
preloadFields := strings.Split(name, ".")
|
|
|
|
if preloadFields[0] == clause.Associations {
|
2020-06-20 12:21:01 +03:00
|
|
|
for _, rel := range db.Statement.Schema.Relationships.Relations {
|
|
|
|
if rel.Schema == db.Statement.Schema {
|
2021-03-04 13:28:32 +03:00
|
|
|
if _, ok := preloadMap[rel.Name]; !ok {
|
|
|
|
preloadMap[rel.Name] = map[string][]interface{}{}
|
|
|
|
}
|
2021-01-26 11:33:19 +03:00
|
|
|
|
2021-03-14 05:18:43 +03:00
|
|
|
if value := strings.TrimPrefix(strings.TrimPrefix(name, preloadFields[0]), "."); value != "" {
|
|
|
|
preloadMap[rel.Name][value] = db.Statement.Preloads[name]
|
2021-03-11 11:36:49 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-14 05:18:43 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok := preloadMap[preloadFields[0]]; !ok {
|
|
|
|
preloadMap[preloadFields[0]] = map[string][]interface{}{}
|
|
|
|
}
|
2021-03-11 11:36:49 +03:00
|
|
|
|
2021-03-14 05:18:43 +03:00
|
|
|
if value := strings.TrimPrefix(strings.TrimPrefix(name, preloadFields[0]), "."); value != "" {
|
|
|
|
preloadMap[preloadFields[0]][value] = db.Statement.Preloads[name]
|
2020-06-20 12:21:01 +03:00
|
|
|
}
|
2020-05-07 05:03:48 +03:00
|
|
|
}
|
2020-06-08 08:45:41 +03:00
|
|
|
}
|
2020-05-07 05:03:48 +03:00
|
|
|
|
2021-01-26 11:33:19 +03:00
|
|
|
preloadNames := make([]string, 0, len(preloadMap))
|
2020-06-08 08:45:41 +03:00
|
|
|
for key := range preloadMap {
|
2021-01-26 11:33:19 +03:00
|
|
|
preloadNames = append(preloadNames, key)
|
2020-06-08 08:45:41 +03:00
|
|
|
}
|
|
|
|
sort.Strings(preloadNames)
|
|
|
|
|
|
|
|
for _, name := range preloadNames {
|
2021-01-26 11:33:19 +03:00
|
|
|
if rel := db.Statement.Schema.Relationships.Relations[name]; rel != nil {
|
2021-11-08 14:40:40 +03:00
|
|
|
preload(db, rel, append(db.Statement.Preloads[name], db.Statement.Preloads[clause.Associations]...), preloadMap[name])
|
2021-01-26 11:33:19 +03:00
|
|
|
} else {
|
2021-07-28 13:50:08 +03:00
|
|
|
db.AddError(fmt.Errorf("%s: %w for schema %s", name, gorm.ErrUnsupportedRelation, db.Statement.Schema.Name))
|
2020-11-16 07:23:13 +03:00
|
|
|
}
|
2020-05-07 05:03:48 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func AfterQuery(db *gorm.DB) {
|
2021-02-07 07:44:59 +03:00
|
|
|
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && db.Statement.Schema.AfterFind && db.RowsAffected > 0 {
|
2020-06-05 16:23:20 +03:00
|
|
|
callMethod(db, func(value interface{}, tx *gorm.DB) bool {
|
2020-08-27 10:03:57 +03:00
|
|
|
if i, ok := value.(AfterFindInterface); ok {
|
2020-06-05 16:23:20 +03:00
|
|
|
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
|
|
|
}
|