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"
|
|
|
|
)
|
|
|
|
|
2014-01-26 08:49:10 +04:00
|
|
|
func Query(scope *Scope) {
|
2014-08-23 12:00:04 +04:00
|
|
|
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()
|
2014-08-20 13:05:02 +04:00
|
|
|
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
|
|
|
|
2015-03-23 06:07:39 +03:00
|
|
|
if orderBy, ok := scope.Get("gorm:order_by_primary_key"); ok {
|
2014-08-25 12:41:26 +04:00
|
|
|
if primaryKey := scope.PrimaryKey(); primaryKey != "" {
|
2015-03-12 08:52:29 +03:00
|
|
|
scope.Search.Order(fmt.Sprintf("%v.%v %v", scope.QuotedTableName(), primaryKey, orderBy))
|
2014-08-25 12:41:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 07:11:38 +03:00
|
|
|
if kind := dest.Kind(); kind == reflect.Slice {
|
2014-01-28 05:25:30 +04:00
|
|
|
isSlice = true
|
|
|
|
destType = dest.Type().Elem()
|
2014-07-08 06:45:31 +04:00
|
|
|
if destType.Kind() == reflect.Ptr {
|
|
|
|
isPtr = true
|
|
|
|
destType = destType.Elem()
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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-03-12 07:11:38 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 06:46:48 +03: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)
|
2015-02-11 08:43:53 +03:00
|
|
|
DefaultCallback.Query().Register("gorm:preload", Preload)
|
2014-01-26 08:49:10 +04:00
|
|
|
}
|