gorm/callback_query.go

114 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 (
"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
)
2014-07-30 10:58:00 +04:00
var dest = scope.IndirectValue()
if value, ok := scope.InstanceGet("gorm:query_destination"); ok {
2014-01-28 05:48:44 +04:00
dest = reflect.Indirect(reflect.ValueOf(value))
}
2014-01-28 05:25:30 +04:00
if orderBy, ok := scope.InstanceGet("gorm:order_by_primary_key"); ok {
if primaryKey := scope.PrimaryKey(); primaryKey != "" {
scope.Search = scope.Search.clone().order(fmt.Sprintf("%v.%v %v", scope.QuotedTableName(), primaryKey, orderBy))
}
}
2014-01-28 05:25:30 +04:00
if dest.Kind() == reflect.Slice {
isSlice = true
destType = dest.Type().Elem()
2014-07-08 06:45:31 +04:00
if destType.Kind() == reflect.Ptr {
isPtr = true
destType = destType.Elem()
}
2014-01-28 05:25:30 +04:00
} else {
scope.Search = scope.Search.clone().limit(1)
}
2014-01-28 05:48:44 +04:00
scope.prepareQuerySql()
2014-01-28 05:25:30 +04:00
if !scope.HasError() {
rows, err := scope.DB().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
}