forked from mirror/gorm
Refactor Scan
This commit is contained in:
parent
43a72b369e
commit
e2e802b837
|
@ -201,13 +201,15 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) {
|
|||
switch stmt.ReflectValue.Kind() {
|
||||
case reflect.Slice, reflect.Array:
|
||||
rValLen := stmt.ReflectValue.Len()
|
||||
stmt.SQL.Grow(rValLen * 18)
|
||||
values.Values = make([][]interface{}, rValLen)
|
||||
if rValLen == 0 {
|
||||
stmt.AddError(gorm.ErrEmptySlice)
|
||||
return
|
||||
}
|
||||
|
||||
stmt.SQL.Grow(rValLen * 18)
|
||||
stmt.Vars = make([]interface{}, 0, rValLen*len(values.Columns))
|
||||
values.Values = make([][]interface{}, rValLen)
|
||||
|
||||
defaultValueFieldsHavingValue := map[*schema.Field][]interface{}{}
|
||||
for i := 0; i < rValLen; i++ {
|
||||
rv := reflect.Indirect(stmt.ReflectValue.Index(i))
|
||||
|
|
19
scan.go
19
scan.go
|
@ -54,10 +54,6 @@ func (db *DB) scanIntoStruct(rows *sql.Rows, reflectValue reflect.Value, values
|
|||
for idx, field := range fields {
|
||||
if field != nil {
|
||||
values[idx] = field.NewValuePool.Get()
|
||||
defer field.NewValuePool.Put(values[idx])
|
||||
if len(joinFields) == 0 || joinFields[idx][0] == nil {
|
||||
defer field.Set(db.Statement.Context, reflectValue, values[idx])
|
||||
}
|
||||
} else if len(fields) == 1 {
|
||||
if reflectValue.CanAddr() {
|
||||
values[idx] = reflectValue.Addr().Interface()
|
||||
|
@ -70,9 +66,12 @@ func (db *DB) scanIntoStruct(rows *sql.Rows, reflectValue reflect.Value, values
|
|||
db.RowsAffected++
|
||||
db.AddError(rows.Scan(values...))
|
||||
|
||||
for idx, joinField := range joinFields {
|
||||
if joinField[0] != nil {
|
||||
relValue := joinField[0].ReflectValueOf(db.Statement.Context, reflectValue)
|
||||
for idx, field := range fields {
|
||||
if field != nil {
|
||||
if len(joinFields) == 0 || joinFields[idx][0] == nil {
|
||||
field.Set(db.Statement.Context, reflectValue, values[idx])
|
||||
} else {
|
||||
relValue := joinFields[idx][0].ReflectValueOf(db.Statement.Context, reflectValue)
|
||||
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
|
||||
if value := reflect.ValueOf(values[idx]).Elem(); value.Kind() == reflect.Ptr && value.IsNil() {
|
||||
return
|
||||
|
@ -80,7 +79,11 @@ func (db *DB) scanIntoStruct(rows *sql.Rows, reflectValue reflect.Value, values
|
|||
|
||||
relValue.Set(reflect.New(relValue.Type().Elem()))
|
||||
}
|
||||
joinField[1].Set(db.Statement.Context, relValue, values[idx])
|
||||
joinFields[idx][1].Set(db.Statement.Context, relValue, values[idx])
|
||||
}
|
||||
|
||||
// release data to pool
|
||||
field.NewValuePool.Put(values[idx])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ require (
|
|||
github.com/jackc/pgx/v4 v4.15.0 // indirect
|
||||
github.com/jinzhu/now v1.1.4
|
||||
github.com/lib/pq v1.10.4
|
||||
github.com/mattn/go-sqlite3 v1.14.11 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.12 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
|
||||
gorm.io/driver/mysql v1.3.2
|
||||
gorm.io/driver/postgres v1.3.1
|
||||
|
|
Loading…
Reference in New Issue