2020-05-26 18:13:05 +03:00
|
|
|
package gorm
|
2020-03-04 06:32:36 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-09-01 11:55:30 +03:00
|
|
|
"database/sql/driver"
|
2020-03-04 06:32:36 +03:00
|
|
|
"reflect"
|
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"
|
2023-03-10 12:21:56 +03:00
|
|
|
"gorm.io/gorm/utils"
|
2020-03-04 06:32:36 +03:00
|
|
|
)
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// prepareValues prepare values slice
|
2020-09-01 11:55:30 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 12:21:56 +03:00
|
|
|
func (db *DB) scanIntoStruct(rows Rows, reflectValue reflect.Value, values []interface{}, fields []*schema.Field, joinFields [][]*schema.Field) {
|
2022-02-27 17:54:43 +03:00
|
|
|
for idx, field := range fields {
|
|
|
|
if field != nil {
|
2022-02-16 10:30:43 +03:00
|
|
|
values[idx] = field.NewValuePool.Get()
|
2022-02-27 17:54:43 +03:00
|
|
|
} else if len(fields) == 1 {
|
2022-02-27 04:09:29 +03:00
|
|
|
if reflectValue.CanAddr() {
|
|
|
|
values[idx] = reflectValue.Addr().Interface()
|
|
|
|
} else {
|
|
|
|
values[idx] = reflectValue.Interface()
|
|
|
|
}
|
2021-10-26 17:36:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db.RowsAffected++
|
|
|
|
db.AddError(rows.Scan(values...))
|
2023-03-10 12:21:56 +03:00
|
|
|
joinedNestedSchemaMap := make(map[string]interface{})
|
2022-02-28 04:28:19 +03:00
|
|
|
for idx, field := range fields {
|
2022-09-05 10:34:33 +03:00
|
|
|
if field == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-10 12:21:56 +03:00
|
|
|
if len(joinFields) == 0 || len(joinFields[idx]) == 0 {
|
2022-09-05 10:34:33 +03:00
|
|
|
db.AddError(field.Set(db.Statement.Context, reflectValue, values[idx]))
|
2023-03-10 12:21:56 +03:00
|
|
|
} else { // joinFields count is larger than 2 when using join
|
|
|
|
var isNilPtrValue bool
|
|
|
|
var relValue reflect.Value
|
|
|
|
// does not contain raw dbname
|
|
|
|
nestedJoinSchemas := joinFields[idx][:len(joinFields[idx])-1]
|
|
|
|
// current reflect value
|
|
|
|
currentReflectValue := reflectValue
|
|
|
|
fullRels := make([]string, 0, len(nestedJoinSchemas))
|
|
|
|
for _, joinSchema := range nestedJoinSchemas {
|
|
|
|
fullRels = append(fullRels, joinSchema.Name)
|
|
|
|
relValue = joinSchema.ReflectValueOf(db.Statement.Context, currentReflectValue)
|
|
|
|
if relValue.Kind() == reflect.Ptr {
|
|
|
|
fullRelsName := utils.JoinNestedRelationNames(fullRels)
|
|
|
|
// same nested structure
|
|
|
|
if _, ok := joinedNestedSchemaMap[fullRelsName]; !ok {
|
|
|
|
if value := reflect.ValueOf(values[idx]).Elem(); value.Kind() == reflect.Ptr && value.IsNil() {
|
|
|
|
isNilPtrValue = true
|
|
|
|
break
|
|
|
|
}
|
2022-09-05 10:34:33 +03:00
|
|
|
|
2023-03-10 12:21:56 +03:00
|
|
|
relValue.Set(reflect.New(relValue.Type().Elem()))
|
|
|
|
joinedNestedSchemaMap[fullRelsName] = nil
|
|
|
|
}
|
2022-02-28 04:28:19 +03:00
|
|
|
}
|
2023-03-10 12:21:56 +03:00
|
|
|
currentReflectValue = relValue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isNilPtrValue { // ignore if value is nil
|
|
|
|
f := joinFields[idx][len(joinFields[idx])-1]
|
|
|
|
db.AddError(f.Set(db.Statement.Context, relValue, values[idx]))
|
2021-10-26 17:36:37 +03:00
|
|
|
}
|
|
|
|
}
|
2022-09-05 10:34:33 +03:00
|
|
|
|
|
|
|
// release data to pool
|
|
|
|
field.NewValuePool.Put(values[idx])
|
2021-10-26 17:36:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// ScanMode scan data mode
|
2021-10-26 17:36:37 +03:00
|
|
|
type ScanMode uint8
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// scan modes
|
2021-10-26 17:36:37 +03:00
|
|
|
const (
|
2021-11-29 09:23:10 +03:00
|
|
|
ScanInitialized ScanMode = 1 << 0 // 1
|
|
|
|
ScanUpdate ScanMode = 1 << 1 // 2
|
|
|
|
ScanOnConflictDoNothing ScanMode = 1 << 2 // 4
|
2021-10-26 17:36:37 +03:00
|
|
|
)
|
|
|
|
|
2022-02-16 10:30:43 +03:00
|
|
|
// Scan scan rows into db statement
|
2022-03-29 13:14:29 +03:00
|
|
|
func Scan(rows Rows, db *DB, mode ScanMode) {
|
2021-10-26 17:36:37 +03:00
|
|
|
var (
|
|
|
|
columns, _ = rows.Columns()
|
|
|
|
values = make([]interface{}, len(columns))
|
|
|
|
initialized = mode&ScanInitialized != 0
|
|
|
|
update = mode&ScanUpdate != 0
|
|
|
|
onConflictDonothing = mode&ScanOnConflictDoNothing != 0
|
|
|
|
)
|
|
|
|
|
2020-09-27 07:25:38 +03:00
|
|
|
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() {
|
2020-09-01 11:55:30 +03:00
|
|
|
columnTypes, _ := rows.ColumnTypes()
|
|
|
|
prepareValues(values, db, columnTypes, columns)
|
2020-06-02 17:13:53 +03:00
|
|
|
|
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 {
|
2021-11-01 07:00:36 +03:00
|
|
|
if *v == nil {
|
|
|
|
*v = map[string]interface{}{}
|
|
|
|
}
|
2020-06-02 17:13:53 +03:00
|
|
|
mapValue = *v
|
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
2020-09-01 11:55:30 +03:00
|
|
|
scanIntoMap(mapValue, values, columns)
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
2022-02-16 10:30:43 +03:00
|
|
|
case *[]map[string]interface{}:
|
2020-09-01 11:55:30 +03:00
|
|
|
columnTypes, _ := rows.ColumnTypes()
|
2020-05-26 18:13:05 +03:00
|
|
|
for initialized || rows.Next() {
|
2020-09-01 11:55:30 +03:00
|
|
|
prepareValues(values, db, columnTypes, columns)
|
2020-06-02 17:13:53 +03:00
|
|
|
|
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-09-01 11:55:30 +03:00
|
|
|
scanIntoMap(mapValue, values, columns)
|
2022-02-16 10:30:43 +03:00
|
|
|
*dest = append(*dest, mapValue)
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
2020-12-02 09:00:16 +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:
|
2021-10-26 17:36:37 +03:00
|
|
|
var (
|
2022-09-30 12:18:42 +03:00
|
|
|
fields = make([]*schema.Field, len(columns))
|
2023-03-10 12:21:56 +03:00
|
|
|
joinFields [][]*schema.Field
|
2022-09-30 12:18:42 +03:00
|
|
|
sch = db.Statement.Schema
|
|
|
|
reflectValue = db.Statement.ReflectValue
|
2021-10-26 17:36:37 +03:00
|
|
|
)
|
|
|
|
|
2022-02-27 17:54:43 +03:00
|
|
|
if reflectValue.Kind() == reflect.Interface {
|
2021-09-17 09:04:19 +03:00
|
|
|
reflectValue = reflectValue.Elem()
|
|
|
|
}
|
2020-07-01 03:56:21 +03:00
|
|
|
|
2021-10-26 17:36:37 +03:00
|
|
|
reflectValueType := reflectValue.Type()
|
|
|
|
switch reflectValueType.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
reflectValueType = reflectValueType.Elem()
|
|
|
|
}
|
|
|
|
isPtr := reflectValueType.Kind() == reflect.Ptr
|
|
|
|
if isPtr {
|
|
|
|
reflectValueType = reflectValueType.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if sch != nil {
|
|
|
|
if reflectValueType != sch.ModelType && reflectValueType.Kind() == reflect.Struct {
|
|
|
|
sch, _ = schema.Parse(db.Statement.Dest, db.cacheStore, db.NamingStrategy)
|
2020-05-31 16:11:20 +03:00
|
|
|
}
|
|
|
|
|
2021-10-26 17:36:37 +03:00
|
|
|
if len(columns) == 1 {
|
2022-02-27 17:54:43 +03:00
|
|
|
// Is Pluck
|
2021-10-26 17:36:37 +03:00
|
|
|
if _, ok := reflect.New(reflectValueType).Interface().(sql.Scanner); (reflectValueType != sch.ModelType && ok) || // is scanner
|
2020-11-10 13:38:24 +03:00
|
|
|
reflectValueType.Kind() != reflect.Struct || // is not struct
|
2021-10-26 17:36:37 +03:00
|
|
|
sch.ModelType.ConvertibleTo(schema.TimeReflectType) { // is time
|
|
|
|
sch = nil
|
2020-09-24 15:08:24 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-27 17:54:43 +03:00
|
|
|
|
|
|
|
// Not Pluck
|
|
|
|
if sch != nil {
|
2022-09-30 12:18:42 +03:00
|
|
|
matchedFieldCount := make(map[string]int, len(columns))
|
2022-02-27 17:54:43 +03:00
|
|
|
for idx, column := range columns {
|
|
|
|
if field := sch.LookUpField(column); field != nil && field.Readable {
|
2022-09-30 12:18:42 +03:00
|
|
|
fields[idx] = field
|
|
|
|
if count, ok := matchedFieldCount[column]; ok {
|
|
|
|
// handle duplicate fields
|
|
|
|
for _, selectField := range sch.Fields {
|
|
|
|
if selectField.DBName == column && selectField.Readable {
|
|
|
|
if count == 0 {
|
|
|
|
matchedFieldCount[column]++
|
2022-05-28 17:18:07 +03:00
|
|
|
fields[idx] = selectField
|
|
|
|
break
|
|
|
|
}
|
2022-09-30 12:18:42 +03:00
|
|
|
count--
|
2022-03-17 17:51:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-30 12:18:42 +03:00
|
|
|
matchedFieldCount[column] = 1
|
2022-03-17 17:51:56 +03:00
|
|
|
}
|
2023-03-10 12:21:56 +03:00
|
|
|
} else if names := utils.SplitNestedRelationName(column); len(names) > 1 { // has nested relation
|
2022-02-27 17:54:43 +03:00
|
|
|
if rel, ok := sch.Relationships.Relations[names[0]]; ok {
|
2023-03-10 12:21:56 +03:00
|
|
|
subNameCount := len(names)
|
|
|
|
// nested relation fields
|
|
|
|
relFields := make([]*schema.Field, 0, subNameCount-1)
|
|
|
|
relFields = append(relFields, rel.Field)
|
|
|
|
for _, name := range names[1 : subNameCount-1] {
|
|
|
|
rel = rel.FieldSchema.Relationships.Relations[name]
|
|
|
|
relFields = append(relFields, rel.Field)
|
|
|
|
}
|
|
|
|
// lastest name is raw dbname
|
|
|
|
dbName := names[subNameCount-1]
|
|
|
|
if field := rel.FieldSchema.LookUpField(dbName); field != nil && field.Readable {
|
2022-02-27 17:54:43 +03:00
|
|
|
fields[idx] = field
|
|
|
|
|
|
|
|
if len(joinFields) == 0 {
|
2023-03-10 12:21:56 +03:00
|
|
|
joinFields = make([][]*schema.Field, len(columns))
|
2022-02-27 17:54:43 +03:00
|
|
|
}
|
2023-03-10 12:21:56 +03:00
|
|
|
relFields = append(relFields, field)
|
|
|
|
joinFields[idx] = relFields
|
2022-02-27 17:54:43 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
values[idx] = &sql.RawBytes{}
|
|
|
|
} else {
|
|
|
|
values[idx] = &sql.RawBytes{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 17:36:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch reflectValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
2022-09-22 10:51:47 +03:00
|
|
|
var (
|
2022-12-19 06:49:05 +03:00
|
|
|
elem reflect.Value
|
|
|
|
isArrayKind = reflectValue.Kind() == reflect.Array
|
2022-09-22 10:51:47 +03:00
|
|
|
)
|
2021-10-26 17:36:37 +03:00
|
|
|
|
2021-10-28 12:12:31 +03:00
|
|
|
if !update || reflectValue.Len() == 0 {
|
|
|
|
update = false
|
2022-09-22 09:50:35 +03:00
|
|
|
// if the slice cap is externally initialized, the externally initialized slice is directly used here
|
|
|
|
if reflectValue.Cap() == 0 {
|
|
|
|
db.Statement.ReflectValue.Set(reflect.MakeSlice(reflectValue.Type(), 0, 20))
|
2022-09-22 10:51:47 +03:00
|
|
|
} else if !isArrayKind {
|
2022-09-22 09:50:35 +03:00
|
|
|
reflectValue.SetLen(0)
|
|
|
|
db.Statement.ReflectValue.Set(reflectValue)
|
|
|
|
}
|
2021-10-26 17:36:37 +03:00
|
|
|
}
|
2020-09-24 15:08:24 +03:00
|
|
|
|
2020-05-26 18:13:05 +03:00
|
|
|
for initialized || rows.Next() {
|
2021-10-26 17:36:37 +03:00
|
|
|
BEGIN:
|
2020-05-26 18:13:05 +03:00
|
|
|
initialized = false
|
2020-06-02 17:13:53 +03:00
|
|
|
|
2021-10-26 17:36:37 +03:00
|
|
|
if update {
|
|
|
|
if int(db.RowsAffected) >= reflectValue.Len() {
|
|
|
|
return
|
2020-06-03 03:44:13 +03:00
|
|
|
}
|
2021-10-26 17:36:37 +03:00
|
|
|
elem = reflectValue.Index(int(db.RowsAffected))
|
|
|
|
if onConflictDonothing {
|
|
|
|
for _, field := range fields {
|
2022-02-16 10:30:43 +03:00
|
|
|
if _, ok := field.ValueOf(db.Statement.Context, elem); !ok {
|
2021-10-26 17:36:37 +03:00
|
|
|
db.RowsAffected++
|
|
|
|
goto BEGIN
|
2020-05-31 16:11:20 +03:00
|
|
|
}
|
2020-06-03 03:44:13 +03:00
|
|
|
}
|
2020-06-02 17:13:53 +03:00
|
|
|
}
|
2020-11-10 13:38:24 +03:00
|
|
|
} else {
|
2022-12-19 06:49:05 +03:00
|
|
|
elem = reflect.New(reflectValueType)
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
2021-09-17 13:35:14 +03:00
|
|
|
|
2022-02-27 17:54:43 +03:00
|
|
|
db.scanIntoStruct(rows, elem, values, fields, joinFields)
|
2020-07-01 03:56:21 +03:00
|
|
|
|
2021-10-26 17:36:37 +03:00
|
|
|
if !update {
|
2022-09-22 10:51:47 +03:00
|
|
|
if !isPtr {
|
|
|
|
elem = elem.Elem()
|
|
|
|
}
|
|
|
|
if isArrayKind {
|
|
|
|
if reflectValue.Len() >= int(db.RowsAffected) {
|
|
|
|
reflectValue.Index(int(db.RowsAffected - 1)).Set(elem)
|
|
|
|
}
|
2020-06-03 03:44:13 +03:00
|
|
|
} else {
|
2022-09-22 10:51:47 +03:00
|
|
|
reflectValue = reflect.Append(reflectValue, elem)
|
2020-06-03 03:44:13 +03:00
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
2021-10-26 17:36:37 +03:00
|
|
|
}
|
2020-03-04 06:32:36 +03:00
|
|
|
|
2021-10-26 17:36:37 +03:00
|
|
|
if !update {
|
|
|
|
db.Statement.ReflectValue.Set(reflectValue)
|
|
|
|
}
|
|
|
|
case reflect.Struct, reflect.Ptr:
|
|
|
|
if initialized || rows.Next() {
|
2022-02-27 17:54:43 +03:00
|
|
|
db.scanIntoStruct(rows, reflectValue, values, fields, joinFields)
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
2021-07-13 14:29:10 +03:00
|
|
|
default:
|
|
|
|
db.AddError(rows.Scan(dest))
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 04:35:41 +03:00
|
|
|
if err := rows.Err(); err != nil && err != db.Error {
|
|
|
|
db.AddError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if db.RowsAffected == 0 && db.Statement.RaiseErrorOnNotFound && db.Error == nil {
|
2020-05-26 18:13:05 +03:00
|
|
|
db.AddError(ErrRecordNotFound)
|
2020-03-04 06:32:36 +03:00
|
|
|
}
|
2022-10-08 15:48:22 +03:00
|
|
|
}
|