2014-01-26 08:49:10 +04:00
|
|
|
package gorm
|
|
|
|
|
2014-01-28 05:25:30 +04:00
|
|
|
import (
|
2015-03-12 07:11:38 +03:00
|
|
|
"errors"
|
2014-08-25 12:41:26 +04:00
|
|
|
"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() {
|
2016-03-05 14:22:29 +03:00
|
|
|
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) {
|
2018-02-09 19:07:16 +03:00
|
|
|
if _, skip := scope.InstanceGet("gorm:skip_query_callback"); skip {
|
2018-02-09 18:22:53 +03:00
|
|
|
return
|
|
|
|
}
|
2019-03-10 14:33:49 +03:00
|
|
|
|
2018-09-10 01:52:32 +03:00
|
|
|
//we are only preloading relations, dont touch base model
|
|
|
|
if _, skip := scope.InstanceGet("gorm:only_preload"); skip {
|
|
|
|
return
|
|
|
|
}
|
2018-02-09 18:22:53 +03:00
|
|
|
|
2019-06-10 15:45:42 +03:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2015-03-23 06:07:39 +03: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))
|
2014-08-25 12:41:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 11:58:33 +03:00
|
|
|
if value, ok := scope.Get("gorm:query_destination"); ok {
|
2017-02-07 03:32:18 +03:00
|
|
|
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
|
|
|
}
|
2015-03-12 07:11:38 +03: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
|
2016-02-15 14:36:48 +03:00
|
|
|
if str, ok := scope.Get("gorm:query_option"); ok {
|
2016-03-07 09:54:20 +03:00
|
|
|
scope.SQL += addExtraSpaceIfExist(fmt.Sprint(str))
|
2016-02-15 14:36:48 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-02 17:29:41 +03:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
scope.Err(err)
|
2017-04-16 22:15:51 +03:00
|
|
|
} 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) {
|
2016-01-17 11:14:14 +03:00
|
|
|
if !scope.HasError() {
|
|
|
|
scope.CallMethod("AfterFind")
|
|
|
|
}
|
2014-01-26 08:49:10 +04:00
|
|
|
}
|