mirror of https://github.com/go-gorm/gorm.git
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() {
|
switch stmt.ReflectValue.Kind() {
|
||||||
case reflect.Slice, reflect.Array:
|
case reflect.Slice, reflect.Array:
|
||||||
rValLen := stmt.ReflectValue.Len()
|
rValLen := stmt.ReflectValue.Len()
|
||||||
stmt.SQL.Grow(rValLen * 18)
|
|
||||||
values.Values = make([][]interface{}, rValLen)
|
|
||||||
if rValLen == 0 {
|
if rValLen == 0 {
|
||||||
stmt.AddError(gorm.ErrEmptySlice)
|
stmt.AddError(gorm.ErrEmptySlice)
|
||||||
return
|
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{}{}
|
defaultValueFieldsHavingValue := map[*schema.Field][]interface{}{}
|
||||||
for i := 0; i < rValLen; i++ {
|
for i := 0; i < rValLen; i++ {
|
||||||
rv := reflect.Indirect(stmt.ReflectValue.Index(i))
|
rv := reflect.Indirect(stmt.ReflectValue.Index(i))
|
||||||
|
|
29
scan.go
29
scan.go
|
@ -54,10 +54,6 @@ func (db *DB) scanIntoStruct(rows *sql.Rows, reflectValue reflect.Value, values
|
||||||
for idx, field := range fields {
|
for idx, field := range fields {
|
||||||
if field != nil {
|
if field != nil {
|
||||||
values[idx] = field.NewValuePool.Get()
|
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 {
|
} else if len(fields) == 1 {
|
||||||
if reflectValue.CanAddr() {
|
if reflectValue.CanAddr() {
|
||||||
values[idx] = reflectValue.Addr().Interface()
|
values[idx] = reflectValue.Addr().Interface()
|
||||||
|
@ -70,17 +66,24 @@ func (db *DB) scanIntoStruct(rows *sql.Rows, reflectValue reflect.Value, values
|
||||||
db.RowsAffected++
|
db.RowsAffected++
|
||||||
db.AddError(rows.Scan(values...))
|
db.AddError(rows.Scan(values...))
|
||||||
|
|
||||||
for idx, joinField := range joinFields {
|
for idx, field := range fields {
|
||||||
if joinField[0] != nil {
|
if field != nil {
|
||||||
relValue := joinField[0].ReflectValueOf(db.Statement.Context, reflectValue)
|
if len(joinFields) == 0 || joinFields[idx][0] == nil {
|
||||||
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
|
field.Set(db.Statement.Context, reflectValue, values[idx])
|
||||||
if value := reflect.ValueOf(values[idx]).Elem(); value.Kind() == reflect.Ptr && value.IsNil() {
|
} else {
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|
||||||
relValue.Set(reflect.New(relValue.Type().Elem()))
|
relValue.Set(reflect.New(relValue.Type().Elem()))
|
||||||
|
}
|
||||||
|
joinFields[idx][1].Set(db.Statement.Context, relValue, values[idx])
|
||||||
}
|
}
|
||||||
joinField[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/jackc/pgx/v4 v4.15.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.4
|
github.com/jinzhu/now v1.1.4
|
||||||
github.com/lib/pq v1.10.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
|
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
|
||||||
gorm.io/driver/mysql v1.3.2
|
gorm.io/driver/mysql v1.3.2
|
||||||
gorm.io/driver/postgres v1.3.1
|
gorm.io/driver/postgres v1.3.1
|
||||||
|
|
Loading…
Reference in New Issue