gorm/callback_query.go

95 lines
2.2 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) {
2016-01-13 09:58:30 +03:00
defer scope.trace(NowFunc())
2014-01-28 05:25:30 +04:00
var (
2016-01-17 15:51:11 +03:00
isSlice bool
isPtr bool
results = scope.IndirectValue()
resultType reflect.Type
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 {
2016-01-17 15:51:11 +03:00
results = reflect.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
}
2014-01-28 05:48:44 +04: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-02-15 14:42:49 +03:00
scope.Sql += addExtraSpaceIfExist(fmt.Sprint(str))
}
2016-02-15 14:42:49 +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-07 07:15:15 +03:00
scope.scan(rows, columns, scope.New(elem.Addr().Interface()).fieldsMap())
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
}
2016-01-17 15:51:11 +03:00
if scope.db.RowsAffected == 0 && !isSlice {
scope.Err(RecordNotFound)
}
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
}