gorm/callback_query.go

119 lines
2.6 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"
)
2014-01-26 08:49:10 +04:00
func Query(scope *Scope) {
defer scope.Trace(NowFunc())
2014-01-28 05:25:30 +04:00
var (
isSlice bool
2014-07-08 06:45:31 +04:00
isPtr bool
2014-01-28 05:25:30 +04:00
anyRecordFound bool
destType reflect.Type
)
if orderBy, ok := scope.Get("gorm:order_by_primary_key"); ok {
if primaryKey := scope.PrimaryKey(); primaryKey != "" {
scope.Search.Order(fmt.Sprintf("%v.%v %v", scope.QuotedTableName(), scope.Quote(primaryKey), orderBy))
}
}
2015-04-17 13:27:20 +03:00
var dest = scope.IndirectValue()
2015-05-19 11:58:33 +03:00
if value, ok := scope.Get("gorm:query_destination"); ok {
2015-04-17 13:27:20 +03:00
dest = reflect.Indirect(reflect.ValueOf(value))
}
if kind := dest.Kind(); kind == reflect.Slice {
2014-01-28 05:25:30 +04:00
isSlice = true
destType = dest.Type().Elem()
dest.Set(reflect.MakeSlice(dest.Type(), 0, 0))
2015-06-24 08:56:30 +03:00
2014-07-08 06:45:31 +04:00
if destType.Kind() == reflect.Ptr {
isPtr = true
destType = destType.Elem()
}
} 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() {
2015-02-26 07:35:33 +03:00
rows, err := scope.SqlDB().Query(scope.Sql, scope.SqlVars...)
2015-01-28 06:16:29 +03:00
scope.db.RowsAffected = 0
2014-01-28 05:25:30 +04:00
if scope.Err(err) != nil {
return
}
2015-02-17 15:19:47 +03:00
defer rows.Close()
2014-01-28 05:25:30 +04:00
2015-02-01 18:19:29 +03:00
columns, _ := rows.Columns()
2014-01-28 05:25:30 +04:00
for rows.Next() {
2015-02-17 03:34:01 +03:00
scope.db.RowsAffected++
2015-01-28 06:16:29 +03:00
2014-01-28 05:25:30 +04:00
anyRecordFound = true
elem := dest
if isSlice {
elem = reflect.New(destType).Elem()
}
2015-01-19 10:50:21 +03:00
var values = make([]interface{}, len(columns))
2014-08-28 14:25:05 +04:00
fields := scope.New(elem.Addr().Interface()).Fields()
2015-01-19 10:50:21 +03:00
for index, column := range columns {
if field, ok := fields[column]; ok {
if field.Field.Kind() == reflect.Ptr {
values[index] = field.Field.Addr().Interface()
} else {
values[index] = reflect.New(reflect.PtrTo(field.Field.Type())).Interface()
}
2014-01-28 05:25:30 +04:00
} else {
2015-01-19 10:50:21 +03:00
var value interface{}
values[index] = &value
2014-01-28 05:25:30 +04:00
}
}
2015-01-19 10:50:21 +03:00
2014-01-28 05:25:30 +04:00
scope.Err(rows.Scan(values...))
2015-01-19 10:50:21 +03:00
for index, column := range columns {
value := values[index]
if field, ok := fields[column]; ok {
if field.Field.Kind() == reflect.Ptr {
field.Field.Set(reflect.ValueOf(value).Elem())
} else if v := reflect.ValueOf(value).Elem().Elem(); v.IsValid() {
field.Field.Set(v)
}
}
}
2014-01-28 05:25:30 +04:00
if isSlice {
2014-07-08 06:45:31 +04:00
if isPtr {
dest.Set(reflect.Append(dest, elem.Addr()))
} else {
dest.Set(reflect.Append(dest, elem))
}
2014-01-28 05:25:30 +04:00
}
}
if !anyRecordFound && !isSlice {
2014-01-28 05:25:30 +04:00
scope.Err(RecordNotFound)
}
}
2014-01-26 08:49:10 +04:00
}
func AfterQuery(scope *Scope) {
2015-02-24 11:28:15 +03:00
scope.CallMethodWithErrorCheck("AfterFind")
2014-01-26 08:49:10 +04:00
}
func init() {
2014-01-29 06:28:20 +04:00
DefaultCallback.Query().Register("gorm:query", Query)
DefaultCallback.Query().Register("gorm:after_query", AfterQuery)
DefaultCallback.Query().Register("gorm:preload", Preload)
2014-01-26 08:49:10 +04:00
}