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-06-02 04:16:07 +03:00
|
|
|
"gorm.io/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{}:
|
2020-05-26 18:13:05 +03:00
|
|
|
if initialized || rows.Next() {
|
2020-06-02 17:13:53 +03:00
|
|
|
for idx := range columns {
|
|
|
|
values[idx] = new(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
|
|
|
|
2020-06-02 17:13:53 +03:00
|
|
|
mapValue, ok := dest.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
if v, ok := dest.(*map[string]interface{}); ok {
|
|
|
|
mapValue = *v
|
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:13:53 +03:00
|
|
|
for idx, column := range columns {
|
|
|
|
if v, ok := values[idx].(*interface{}); ok {
|
|
|
|
if v == nil {
|
|
|
|
mapValue[column] = nil
|
|
|
|
} else {
|
|
|
|
mapValue[column] = *v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
|
|
|
case *[]map[string]interface{}:
|
2020-05-26 18:13:05 +03:00
|
|
|
for initialized || rows.Next() {
|
2020-06-02 17:13:53 +03:00
|
|
|
for idx := range columns {
|
|
|
|
values[idx] = new(interface{})
|
|
|
|
}
|
|
|
|
|
2020-05-26 18:13:05 +03:00
|
|
|
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
|
|
|
|
2020-06-02 17:13:53 +03:00
|
|
|
mapValue := map[string]interface{}{}
|
2020-03-04 06:32:36 +03:00
|
|
|
for idx, column := range columns {
|
2020-06-02 17:13:53 +03:00
|
|
|
if v, ok := values[idx].(*interface{}); ok {
|
|
|
|
if v == nil {
|
|
|
|
mapValue[column] = nil
|
|
|
|
} else {
|
|
|
|
mapValue[column] = *v
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
2020-06-02 17:13:53 +03:00
|
|
|
|
|
|
|
*dest = append(*dest, mapValue)
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
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-06-08 08:45:41 +03:00
|
|
|
var (
|
|
|
|
reflectValueType = db.Statement.ReflectValue.Type().Elem()
|
|
|
|
isPtr = reflectValueType.Kind() == reflect.Ptr
|
|
|
|
fields = make([]*schema.Field, len(columns))
|
|
|
|
joinFields [][2]*schema.Field
|
|
|
|
)
|
|
|
|
|
2020-05-31 16:11:20 +03:00
|
|
|
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
|
|
|
|
|
|
|
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 {
|
2020-06-08 08:45:41 +03:00
|
|
|
if len(joinFields) == 0 {
|
|
|
|
joinFields = make([][2]*schema.Field, len(columns))
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:47:18 +03:00
|
|
|
if rel, ok := db.Statement.Schema.Relationships.Relations[names[0]]; ok {
|
|
|
|
if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {
|
2020-06-03 03:44:13 +03:00
|
|
|
fields[idx] = field
|
2020-04-29 18:47:18 +03:00
|
|
|
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-06-08 08:45:41 +03:00
|
|
|
// pluck values into slice of data
|
|
|
|
isPluck := len(fields) == 1 && reflectValueType.Kind() != reflect.Struct
|
2020-05-26 18:13:05 +03:00
|
|
|
for initialized || rows.Next() {
|
|
|
|
initialized = false
|
2020-06-02 17:13:53 +03:00
|
|
|
db.RowsAffected++
|
|
|
|
|
2020-05-31 16:11:20 +03:00
|
|
|
elem := reflect.New(reflectValueType).Elem()
|
2020-06-08 08:45:41 +03:00
|
|
|
if isPluck {
|
|
|
|
db.AddError(rows.Scan(elem.Addr().Interface()))
|
2020-05-31 16:11:20 +03:00
|
|
|
} else {
|
2020-06-03 03:44:13 +03:00
|
|
|
for idx, field := range fields {
|
|
|
|
if field != nil {
|
2020-06-08 08:45:41 +03:00
|
|
|
values[idx] = reflect.New(reflect.PtrTo(field.IndirectFieldType)).Interface()
|
2020-06-03 03:44:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:13:53 +03:00
|
|
|
db.AddError(rows.Scan(values...))
|
|
|
|
|
2020-05-31 16:11:20 +03:00
|
|
|
for idx, field := range fields {
|
2020-06-08 08:45:41 +03:00
|
|
|
if len(joinFields) != 0 && joinFields[idx][0] != nil {
|
2020-06-03 03:44:13 +03:00
|
|
|
value := reflect.ValueOf(values[idx]).Elem()
|
|
|
|
relValue := joinFields[idx][0].ReflectValueOf(elem)
|
2020-06-02 17:13:53 +03:00
|
|
|
|
2020-06-03 03:44:13 +03:00
|
|
|
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
|
|
|
|
if value.IsNil() {
|
|
|
|
continue
|
2020-06-02 17:13:53 +03:00
|
|
|
}
|
2020-06-03 03:44:13 +03:00
|
|
|
relValue.Set(reflect.New(relValue.Type().Elem()))
|
2020-05-31 16:11:20 +03:00
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
|
2020-06-03 03:44:13 +03:00
|
|
|
field.Set(relValue, values[idx])
|
|
|
|
} else if field != nil {
|
|
|
|
field.Set(elem, values[idx])
|
|
|
|
}
|
2020-06-02 17:13:53 +03:00
|
|
|
}
|
|
|
|
}
|
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:
|
2020-06-02 17:13:53 +03:00
|
|
|
if initialized || rows.Next() {
|
2020-06-03 03:44:13 +03:00
|
|
|
for idx, column := range columns {
|
|
|
|
if field := db.Statement.Schema.LookUpField(column); field != nil && field.Readable {
|
2020-06-08 08:45:41 +03:00
|
|
|
values[idx] = reflect.New(reflect.PtrTo(field.IndirectFieldType)).Interface()
|
2020-06-03 03:44:13 +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 {
|
2020-06-08 08:45:41 +03:00
|
|
|
values[idx] = reflect.New(reflect.PtrTo(field.IndirectFieldType)).Interface()
|
2020-06-03 03:44:13 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
values[idx] = &sql.RawBytes{}
|
|
|
|
} else {
|
|
|
|
values[idx] = &sql.RawBytes{}
|
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
db.RowsAffected++
|
2020-05-30 20:21:16 +03:00
|
|
|
db.AddError(rows.Scan(values...))
|
2020-06-02 17:13:53 +03:00
|
|
|
|
|
|
|
for idx, column := range columns {
|
|
|
|
if field := db.Statement.Schema.LookUpField(column); field != nil && field.Readable {
|
2020-06-03 03:44:13 +03:00
|
|
|
field.Set(db.Statement.ReflectValue, values[idx])
|
2020-06-02 17:13:53 +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-06-03 03:44:13 +03:00
|
|
|
value := reflect.ValueOf(values[idx]).Elem()
|
2020-06-02 17:13:53 +03:00
|
|
|
|
2020-06-03 03:44:13 +03:00
|
|
|
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
|
|
|
|
if value.IsNil() {
|
|
|
|
continue
|
2020-06-02 17:13:53 +03:00
|
|
|
}
|
2020-06-03 03:44:13 +03:00
|
|
|
relValue.Set(reflect.New(relValue.Type().Elem()))
|
2020-06-02 17:13:53 +03:00
|
|
|
}
|
2020-06-03 03:44:13 +03:00
|
|
|
|
|
|
|
field.Set(relValue, values[idx])
|
2020-06-02 17:13:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|