2020-05-26 18:13:05 +03:00
|
|
|
package gorm
|
2020-03-04 06:32:36 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"reflect"
|
2020-04-29 18:47:18 +03:00
|
|
|
"strings"
|
2020-03-04 06:32:36 +03:00
|
|
|
|
2020-03-04 17:16:39 +03:00
|
|
|
"github.com/jinzhu/gorm/schema"
|
2020-03-04 06:32:36 +03:00
|
|
|
)
|
|
|
|
|
2020-05-26 18:13:05 +03:00
|
|
|
func Scan(rows *sql.Rows, db *DB, initialized bool) {
|
2020-03-04 06:32:36 +03:00
|
|
|
columns, _ := rows.Columns()
|
|
|
|
values := make([]interface{}, len(columns))
|
|
|
|
|
|
|
|
switch dest := db.Statement.Dest.(type) {
|
|
|
|
case map[string]interface{}, *map[string]interface{}:
|
|
|
|
for idx, _ := range columns {
|
|
|
|
values[idx] = new(interface{})
|
|
|
|
}
|
|
|
|
|
2020-05-26 18:13:05 +03:00
|
|
|
if initialized || rows.Next() {
|
2020-03-04 06:32:36 +03:00
|
|
|
db.RowsAffected++
|
2020-05-30 20:21:16 +03:00
|
|
|
db.AddError(rows.Scan(values...))
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mapValue, ok := dest.(map[string]interface{})
|
|
|
|
if ok {
|
|
|
|
if v, ok := dest.(*map[string]interface{}); ok {
|
|
|
|
mapValue = *v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, column := range columns {
|
|
|
|
mapValue[column] = *(values[idx].(*interface{}))
|
|
|
|
}
|
|
|
|
case *[]map[string]interface{}:
|
|
|
|
for idx, _ := range columns {
|
|
|
|
values[idx] = new(interface{})
|
|
|
|
}
|
|
|
|
|
2020-05-26 18:13:05 +03:00
|
|
|
for initialized || rows.Next() {
|
|
|
|
initialized = false
|
2020-03-04 06:32:36 +03:00
|
|
|
db.RowsAffected++
|
2020-05-30 20:21:16 +03:00
|
|
|
db.AddError(rows.Scan(values...))
|
2020-03-04 06:32:36 +03:00
|
|
|
|
|
|
|
v := map[string]interface{}{}
|
|
|
|
for idx, column := range columns {
|
|
|
|
v[column] = *(values[idx].(*interface{}))
|
|
|
|
}
|
|
|
|
*dest = append(*dest, v)
|
|
|
|
}
|
2020-05-24 06:32:59 +03:00
|
|
|
case *int, *int64, *uint, *uint64:
|
2020-05-26 18:13:05 +03:00
|
|
|
for initialized || rows.Next() {
|
|
|
|
initialized = false
|
2020-05-24 06:32:59 +03:00
|
|
|
db.RowsAffected++
|
2020-05-30 20:21:16 +03:00
|
|
|
db.AddError(rows.Scan(dest))
|
2020-05-24 06:32:59 +03:00
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
default:
|
|
|
|
switch db.Statement.ReflectValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
2020-05-31 16:11:20 +03:00
|
|
|
reflectValueType := db.Statement.ReflectValue.Type().Elem()
|
|
|
|
isPtr := reflectValueType.Kind() == reflect.Ptr
|
|
|
|
if isPtr {
|
|
|
|
reflectValueType = reflectValueType.Elem()
|
|
|
|
}
|
|
|
|
|
2020-03-04 06:32:36 +03:00
|
|
|
db.Statement.ReflectValue.Set(reflect.MakeSlice(db.Statement.ReflectValue.Type(), 0, 0))
|
2020-03-04 17:16:39 +03:00
|
|
|
fields := make([]*schema.Field, len(columns))
|
2020-04-29 18:47:18 +03:00
|
|
|
joinFields := make([][2]*schema.Field, len(columns))
|
2020-03-04 17:16:39 +03:00
|
|
|
|
|
|
|
for idx, column := range columns {
|
2020-04-08 03:15:00 +03:00
|
|
|
if field := db.Statement.Schema.LookUpField(column); field != nil && field.Readable {
|
|
|
|
fields[idx] = field
|
2020-04-29 18:47:18 +03:00
|
|
|
} else if names := strings.Split(column, "__"); len(names) > 1 {
|
|
|
|
if rel, ok := db.Statement.Schema.Relationships.Relations[names[0]]; ok {
|
|
|
|
if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {
|
|
|
|
joinFields[idx] = [2]*schema.Field{rel.Field, field}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
values[idx] = &sql.RawBytes{}
|
2020-04-08 03:15:00 +03:00
|
|
|
} else {
|
2020-04-29 18:47:18 +03:00
|
|
|
values[idx] = &sql.RawBytes{}
|
2020-04-08 03:15:00 +03:00
|
|
|
}
|
2020-03-04 17:16:39 +03:00
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
|
2020-05-26 18:13:05 +03:00
|
|
|
for initialized || rows.Next() {
|
|
|
|
initialized = false
|
2020-05-31 16:11:20 +03:00
|
|
|
elem := reflect.New(reflectValueType).Elem()
|
2020-05-29 18:38:03 +03:00
|
|
|
|
2020-05-31 16:11:20 +03:00
|
|
|
if reflectValueType.Kind() != reflect.Struct && len(fields) == 1 {
|
|
|
|
values[0] = elem.Addr().Interface()
|
|
|
|
} else {
|
|
|
|
for idx, field := range fields {
|
|
|
|
if field != nil {
|
|
|
|
values[idx] = field.ReflectValueOf(elem).Addr().Interface()
|
|
|
|
} else if joinFields[idx][0] != nil {
|
|
|
|
relValue := joinFields[idx][0].ReflectValueOf(elem)
|
|
|
|
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
|
|
|
|
relValue.Set(reflect.New(relValue.Type().Elem()))
|
|
|
|
}
|
|
|
|
|
|
|
|
values[idx] = joinFields[idx][1].ReflectValueOf(relValue).Addr().Interface()
|
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db.RowsAffected++
|
2020-05-30 20:21:16 +03:00
|
|
|
db.AddError(rows.Scan(values...))
|
2020-03-04 06:32:36 +03:00
|
|
|
|
|
|
|
if isPtr {
|
|
|
|
db.Statement.ReflectValue.Set(reflect.Append(db.Statement.ReflectValue, elem.Addr()))
|
|
|
|
} else {
|
|
|
|
db.Statement.ReflectValue.Set(reflect.Append(db.Statement.ReflectValue, elem))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
for idx, column := range columns {
|
2020-04-08 03:15:00 +03:00
|
|
|
if field := db.Statement.Schema.LookUpField(column); field != nil && field.Readable {
|
2020-03-04 06:32:36 +03:00
|
|
|
values[idx] = field.ReflectValueOf(db.Statement.ReflectValue).Addr().Interface()
|
2020-04-29 18:47:18 +03:00
|
|
|
} else if names := strings.Split(column, "__"); len(names) > 1 {
|
|
|
|
if rel, ok := db.Statement.Schema.Relationships.Relations[names[0]]; ok {
|
|
|
|
relValue := rel.Field.ReflectValueOf(db.Statement.ReflectValue)
|
|
|
|
if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {
|
2020-05-29 18:38:03 +03:00
|
|
|
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
|
|
|
|
relValue.Set(reflect.New(relValue.Type().Elem()))
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:47:18 +03:00
|
|
|
values[idx] = field.ReflectValueOf(relValue).Addr().Interface()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
values[idx] = &sql.RawBytes{}
|
2020-03-04 06:32:36 +03:00
|
|
|
} else {
|
2020-04-29 18:47:18 +03:00
|
|
|
values[idx] = &sql.RawBytes{}
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 18:13:05 +03:00
|
|
|
if initialized || rows.Next() {
|
2020-03-04 06:32:36 +03:00
|
|
|
db.RowsAffected++
|
2020-05-30 20:21:16 +03:00
|
|
|
db.AddError(rows.Scan(values...))
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if db.RowsAffected == 0 && db.Statement.RaiseErrorOnNotFound {
|
2020-05-26 18:13:05 +03:00
|
|
|
db.AddError(ErrRecordNotFound)
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
|
|
|
}
|