gorm/scan.go

248 lines
7.3 KiB
Go
Raw Normal View History

2020-05-26 18:13:05 +03:00
package gorm
2020-03-04 06:32:36 +03:00
import (
"database/sql"
"database/sql/driver"
2020-03-04 06:32:36 +03:00
"reflect"
2020-04-29 18:47:18 +03:00
"strings"
2020-09-24 15:08:24 +03:00
"time"
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
)
func prepareValues(values []interface{}, db *DB, columnTypes []*sql.ColumnType, columns []string) {
if db.Statement.Schema != nil {
for idx, name := range columns {
if field := db.Statement.Schema.LookUpField(name); field != nil {
values[idx] = reflect.New(reflect.PtrTo(field.FieldType)).Interface()
continue
}
values[idx] = new(interface{})
}
} else if len(columnTypes) > 0 {
for idx, columnType := range columnTypes {
if columnType.ScanType() != nil {
values[idx] = reflect.New(reflect.PtrTo(columnType.ScanType())).Interface()
} else {
values[idx] = new(interface{})
}
}
} else {
for idx := range columns {
values[idx] = new(interface{})
}
}
}
func scanIntoMap(mapValue map[string]interface{}, values []interface{}, columns []string) {
for idx, column := range columns {
if reflectValue := reflect.Indirect(reflect.Indirect(reflect.ValueOf(values[idx]))); reflectValue.IsValid() {
mapValue[column] = reflectValue.Interface()
if valuer, ok := mapValue[column].(driver.Valuer); ok {
mapValue[column], _ = valuer.Value()
} else if b, ok := mapValue[column].(sql.RawBytes); ok {
mapValue[column] = string(b)
}
} else {
mapValue[column] = nil
}
}
}
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))
db.RowsAffected = 0
2020-03-04 06:32:36 +03:00
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() {
columnTypes, _ := rows.ColumnTypes()
prepareValues(values, db, columnTypes, columns)
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
}
2020-03-04 06:32:36 +03:00
}
scanIntoMap(mapValue, values, columns)
2020-03-04 06:32:36 +03:00
}
case *[]map[string]interface{}:
columnTypes, _ := rows.ColumnTypes()
2020-05-26 18:13:05 +03:00
for initialized || rows.Next() {
prepareValues(values, db, columnTypes, columns)
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
mapValue := map[string]interface{}{}
scanIntoMap(mapValue, values, columns)
*dest = append(*dest, mapValue)
2020-03-04 06:32:36 +03:00
}
case *int, *int8, *int16, *int32, *int64,
*uint, *uint8, *uint16, *uint32, *uint64, *uintptr,
*float32, *float64,
*bool, *string, *time.Time,
*sql.NullInt32, *sql.NullInt64, *sql.NullFloat64,
*sql.NullBool, *sql.NullString, *sql.NullTime:
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:
2020-07-01 03:56:21 +03:00
Schema := db.Statement.Schema
2020-03-04 06:32:36 +03:00
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-11-10 13:38:24 +03:00
db.Statement.ReflectValue.Set(reflect.MakeSlice(db.Statement.ReflectValue.Type(), 0, 20))
2020-03-04 17:16:39 +03:00
2020-07-01 03:56:21 +03:00
if Schema != nil {
if reflectValueType != Schema.ModelType && reflectValueType.Kind() == reflect.Struct {
Schema, _ = schema.Parse(db.Statement.Dest, db.cacheStore, db.NamingStrategy)
}
2020-06-09 10:34:55 +03:00
for idx, column := range columns {
2020-07-01 03:56:21 +03:00
if field := Schema.LookUpField(column); field != nil && field.Readable {
2020-06-09 10:34:55 +03:00
fields[idx] = field
} else if names := strings.Split(column, "__"); len(names) > 1 {
2020-07-01 03:56:21 +03:00
if rel, ok := Schema.Relationships.Relations[names[0]]; ok {
2020-06-09 10:34:55 +03:00
if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {
fields[idx] = field
2020-11-10 13:38:24 +03:00
if len(joinFields) == 0 {
joinFields = make([][2]*schema.Field, len(columns))
}
2020-06-09 10:34:55 +03:00
joinFields[idx] = [2]*schema.Field{rel.Field, field}
continue
}
2020-04-29 18:47:18 +03:00
}
2020-06-09 10:34:55 +03:00
values[idx] = &sql.RawBytes{}
} else {
values[idx] = &sql.RawBytes{}
2020-04-29 18:47:18 +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
2020-09-24 15:08:24 +03:00
isPluck := false
if len(fields) == 1 {
2020-11-10 13:38:24 +03:00
if _, ok := reflect.New(reflectValueType).Interface().(sql.Scanner); ok || // is scanner
reflectValueType.Kind() != reflect.Struct || // is not struct
Schema.ModelType.ConvertibleTo(schema.TimeReflectType) { // is time
2020-09-24 15:08:24 +03:00
isPluck = true
}
}
2020-05-26 18:13:05 +03:00
for initialized || rows.Next() {
initialized = false
db.RowsAffected++
2020-11-10 13:38:24 +03:00
elem := reflect.New(reflectValueType)
2020-06-08 08:45:41 +03:00
if isPluck {
2020-11-10 13:38:24 +03:00
db.AddError(rows.Scan(elem.Interface()))
2020-05-31 16:11:20 +03:00
} else {
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()
}
}
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 {
value := reflect.ValueOf(values[idx]).Elem()
relValue := joinFields[idx][0].ReflectValueOf(elem)
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
if value.IsNil() {
continue
}
relValue.Set(reflect.New(relValue.Type().Elem()))
2020-05-31 16:11:20 +03:00
}
2020-03-04 06:32:36 +03:00
field.Set(relValue, values[idx])
} else if field != nil {
field.Set(elem, values[idx])
}
}
}
2020-03-04 06:32:36 +03:00
if isPtr {
db.Statement.ReflectValue.Set(reflect.Append(db.Statement.ReflectValue, elem))
2020-11-10 13:38:24 +03:00
} else {
db.Statement.ReflectValue.Set(reflect.Append(db.Statement.ReflectValue, elem.Elem()))
2020-03-04 06:32:36 +03:00
}
}
2021-01-26 15:08:41 +03:00
case reflect.Struct, reflect.Ptr:
2020-07-01 03:56:21 +03:00
if db.Statement.ReflectValue.Type() != Schema.ModelType {
Schema, _ = schema.Parse(db.Statement.Dest, db.cacheStore, db.NamingStrategy)
}
if initialized || rows.Next() {
for idx, column := range columns {
2020-07-01 03:56:21 +03:00
if field := Schema.LookUpField(column); field != nil && field.Readable {
2020-06-08 08:45:41 +03:00
values[idx] = reflect.New(reflect.PtrTo(field.IndirectFieldType)).Interface()
} else if names := strings.Split(column, "__"); len(names) > 1 {
2020-07-01 03:56:21 +03:00
if rel, ok := 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()
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...))
for idx, column := range columns {
2020-07-01 03:56:21 +03:00
if field := Schema.LookUpField(column); field != nil && field.Readable {
field.Set(db.Statement.ReflectValue, values[idx])
} else if names := strings.Split(column, "__"); len(names) > 1 {
2020-07-01 03:56:21 +03:00
if rel, ok := Schema.Relationships.Relations[names[0]]; ok {
if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {
2020-11-10 13:38:24 +03:00
relValue := rel.Field.ReflectValueOf(db.Statement.ReflectValue)
value := reflect.ValueOf(values[idx]).Elem()
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
if value.IsNil() {
continue
}
relValue.Set(reflect.New(relValue.Type().Elem()))
}
field.Set(relValue, values[idx])
}
}
}
}
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
}
}