gorm/callbacks/query.go

170 lines
4.5 KiB
Go
Raw Normal View History

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-02-04 03:56:15 +03:00
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/clause"
2020-05-07 05:03:48 +03:00
"github.com/jinzhu/gorm/schema"
2020-02-04 03:56:15 +03:00
)
2020-02-02 09:40:44 +03:00
func Query(db *gorm.DB) {
2020-02-22 15:57:29 +03:00
if db.Statement.SQL.String() == "" {
2020-04-15 04:14:24 +03:00
clauseSelect := clause.Select{}
if len(db.Statement.Selects) > 0 {
for _, name := range db.Statement.Selects {
if f := db.Statement.Schema.LookUpField(name); f != nil {
clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{
Name: f.DBName,
})
2020-05-24 06:32:59 +03:00
} else {
clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{
Name: name,
Raw: true,
})
2020-04-15 04:14:24 +03:00
}
}
}
2020-05-14 07:19:12 +03:00
// inline joins
2020-04-15 04:14:24 +03:00
if len(db.Statement.Joins) != 0 {
joins := []clause.Join{}
if len(db.Statement.Selects) == 0 {
for _, dbName := range db.Statement.Schema.DBNames {
clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{
2020-04-29 18:47:18 +03:00
Table: db.Statement.Table,
Name: dbName,
2020-04-15 04:14:24 +03:00
})
}
}
for name, conds := range db.Statement.Joins {
if relation, ok := db.Statement.Schema.Relationships.Relations[name]; ok {
for _, s := range relation.FieldSchema.DBNames {
clauseSelect.Columns = append(clauseSelect.Columns, clause.Column{
2020-04-29 18:47:18 +03:00
Table: relation.Name,
2020-04-15 04:14:24 +03:00
Name: s,
2020-04-29 18:47:18 +03:00
Alias: relation.Name + "__" + s,
2020-04-15 04:14:24 +03:00
})
}
var exprs []clause.Expression
for _, ref := range relation.References {
if ref.OwnPrimaryKey {
exprs = append(exprs, clause.Expr{
2020-04-29 18:47:18 +03:00
SQL: fmt.Sprintf("%s.%s = %s.%s", db.Statement.Schema.Table, ref.PrimaryKey.DBName, relation.Name, ref.ForeignKey.DBName),
2020-04-15 04:14:24 +03:00
})
} else {
if ref.PrimaryValue == "" {
exprs = append(exprs, clause.Expr{
2020-04-29 18:47:18 +03:00
SQL: fmt.Sprintf("%s.%s = %s.%s", db.Statement.Schema.Table, ref.ForeignKey.DBName, relation.Name, ref.PrimaryKey.DBName),
2020-04-15 04:14:24 +03:00
})
} else {
exprs = append(exprs, clause.Expr{
2020-04-29 18:47:18 +03:00
SQL: fmt.Sprintf("%s.%s = ?", relation.Name, ref.PrimaryKey.DBName),
2020-04-15 04:14:24 +03:00
Vars: []interface{}{ref.PrimaryValue},
})
}
}
}
joins = append(joins, clause.Join{
Type: clause.LeftJoin,
2020-04-29 18:47:18 +03:00
Table: clause.Table{Name: relation.FieldSchema.Table, Alias: relation.Name},
2020-04-15 04:14:24 +03:00
ON: clause.Where{Exprs: exprs},
})
} else {
joins = append(joins, clause.Join{
Expression: clause.Expr{SQL: name, Vars: conds},
})
}
}
db.Statement.AddClause(clause.From{Joins: joins})
} else {
db.Statement.AddClauseIfNotExists(clause.From{})
}
2020-05-24 06:32:59 +03:00
db.Statement.AddClause(clauseSelect)
2020-02-22 15:57:29 +03:00
db.Statement.Build("SELECT", "FROM", "WHERE", "GROUP BY", "ORDER BY", "LIMIT", "FOR")
}
2020-02-04 03:56:15 +03:00
2020-03-09 08:10:48 +03:00
rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
2020-03-03 09:18:12 +03:00
if err != nil {
db.AddError(err)
return
}
defer rows.Close()
2020-03-04 06:32:36 +03:00
Scan(rows, db)
2020-02-02 09:40:44 +03:00
}
func Preload(db *gorm.DB) {
2020-05-07 05:03:48 +03:00
if len(db.Statement.Preloads) > 0 {
preloadMap := map[string][]string{}
for name := range db.Statement.Preloads {
preloadFields := strings.Split(name, ".")
for idx := range preloadFields {
preloadMap[strings.Join(preloadFields[:idx+1], ".")] = preloadFields[:idx+1]
}
}
preloadNames := make([]string, len(preloadMap))
idx := 0
for key := range preloadMap {
preloadNames[idx] = key
idx++
}
sort.Strings(preloadNames)
for _, name := range preloadNames {
2020-05-14 07:19:12 +03:00
var (
curSchema = db.Statement.Schema
preloadFields = preloadMap[name]
rels = make([]*schema.Relationship, len(preloadFields))
)
2020-05-07 05:03:48 +03:00
for idx, preloadField := range preloadFields {
if rel := curSchema.Relationships.Relations[preloadField]; rel != nil {
2020-05-14 07:19:12 +03:00
rels[idx] = rel
curSchema = rel.FieldSchema
2020-05-07 05:03:48 +03:00
} else {
db.AddError(fmt.Errorf("%v: %w", name, gorm.ErrUnsupportedRelation))
}
}
2020-05-14 07:19:12 +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-02-23 16:22:35 +03:00
if db.Statement.Schema != nil && db.Statement.Schema.AfterFind {
callMethod := func(value interface{}) bool {
if db.Statement.Schema.AfterFind {
if i, ok := value.(gorm.AfterFindInterface); ok {
i.AfterFind(db)
return true
}
}
return false
}
if ok := callMethod(db.Statement.Dest); !ok {
switch db.Statement.ReflectValue.Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i <= db.Statement.ReflectValue.Len(); i++ {
callMethod(db.Statement.ReflectValue.Index(i).Interface())
}
case reflect.Struct:
callMethod(db.Statement.ReflectValue.Interface())
}
}
}
2020-02-02 09:40:44 +03:00
}