gorm/callback_query.go

105 lines
2.5 KiB
Go
Raw Normal View History

2014-01-26 08:49:10 +04:00
package gorm
2014-01-28 05:25:30 +04:00
import (
"errors"
"fmt"
2014-01-28 05:25:30 +04:00
"reflect"
)
2016-01-17 15:51:11 +03:00
// Define callbacks for querying
2016-01-17 11:28:32 +03:00
func init() {
DefaultCallback.Query().Register("gorm:query", queryCallback)
DefaultCallback.Query().Register("gorm:preload", preloadCallback)
DefaultCallback.Query().Register("gorm:after_query", afterQueryCallback)
2016-01-17 11:28:32 +03:00
}
2016-01-17 15:51:11 +03:00
// queryCallback used to query data from database
2016-01-17 10:30:42 +03:00
func queryCallback(scope *Scope) {
if _, skip := scope.InstanceGet("gorm:skip_query_callback"); skip {
return
}
2019-03-10 14:33:49 +03:00
//we are only preloading relations, dont touch base model
if _, skip := scope.InstanceGet("gorm:only_preload"); skip {
return
}
defer scope.trace(scope.db.nowFunc())
2014-01-28 05:25:30 +04:00
var (
2016-03-08 16:45:20 +03:00
isSlice, isPtr bool
resultType reflect.Type
results = scope.IndirectValue()
2014-01-28 05:25:30 +04:00
)
if orderBy, ok := scope.Get("gorm:order_by_primary_key"); ok {
2016-01-17 15:51:11 +03:00
if primaryField := scope.PrimaryField(); primaryField != nil {
scope.Search.Order(fmt.Sprintf("%v.%v %v", scope.QuotedTableName(), scope.Quote(primaryField.DBName), orderBy))
}
}
2015-05-19 11:58:33 +03:00
if value, ok := scope.Get("gorm:query_destination"); ok {
results = indirect(reflect.ValueOf(value))
2015-04-17 13:27:20 +03:00
}
2016-01-17 15:51:11 +03:00
if kind := results.Kind(); kind == reflect.Slice {
2014-01-28 05:25:30 +04:00
isSlice = true
2016-01-17 15:51:11 +03:00
resultType = results.Type().Elem()
results.Set(reflect.MakeSlice(results.Type(), 0, 0))
2015-06-24 08:56:30 +03:00
2016-01-17 15:51:11 +03:00
if resultType.Kind() == reflect.Ptr {
2014-07-08 06:45:31 +04:00
isPtr = true
2016-01-17 15:51:11 +03:00
resultType = resultType.Elem()
2014-07-08 06:45:31 +04:00
}
} else if kind != reflect.Struct {
scope.Err(errors.New("unsupported destination, should be slice or struct"))
return
2014-01-28 05:25:30 +04:00
}
2016-03-07 09:54:20 +03:00
scope.prepareQuerySQL()
2014-01-28 05:25:30 +04:00
if !scope.HasError() {
2016-02-15 14:42:49 +03:00
scope.db.RowsAffected = 0
if str, ok := scope.Get("gorm:query_option"); ok {
2016-03-07 09:54:20 +03:00
scope.SQL += addExtraSpaceIfExist(fmt.Sprint(str))
}
2016-03-07 09:54:20 +03:00
if rows, err := scope.SQLDB().Query(scope.SQL, scope.SQLVars...); scope.Err(err) == nil {
2016-01-17 15:51:11 +03:00
defer rows.Close()
2014-01-28 05:25:30 +04:00
2016-01-17 15:51:11 +03:00
columns, _ := rows.Columns()
for rows.Next() {
scope.db.RowsAffected++
2015-01-28 06:16:29 +03:00
2016-01-17 15:51:11 +03:00
elem := results
if isSlice {
elem = reflect.New(resultType).Elem()
}
2014-01-28 05:25:30 +04:00
2016-03-10 12:13:48 +03:00
scope.scan(rows, columns, scope.New(elem.Addr().Interface()).Fields())
2015-01-19 10:50:21 +03:00
2016-01-17 15:51:11 +03:00
if isSlice {
if isPtr {
results.Set(reflect.Append(results, elem.Addr()))
} else {
results.Set(reflect.Append(results, elem))
}
2014-07-08 06:45:31 +04:00
}
2014-01-28 05:25:30 +04:00
}
if err := rows.Err(); err != nil {
scope.Err(err)
} else if scope.db.RowsAffected == 0 && !isSlice {
2016-03-07 09:54:20 +03:00
scope.Err(ErrRecordNotFound)
2016-01-17 15:51:11 +03:00
}
2014-01-28 05:25:30 +04:00
}
}
2014-01-26 08:49:10 +04:00
}
2016-01-17 15:51:11 +03:00
// afterQueryCallback will invoke `AfterFind` method after querying
2016-01-17 10:30:42 +03:00
func afterQueryCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("AfterFind")
}
2014-01-26 08:49:10 +04:00
}