mirror of https://github.com/go-gorm/gorm.git
Refactor find slice
This commit is contained in:
parent
b0e1bccf4a
commit
9f7f4b430e
|
@ -5,6 +5,7 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
"github.com/jinzhu/gorm/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Scan(rows *sql.Rows, db *gorm.DB) {
|
func Scan(rows *sql.Rows, db *gorm.DB) {
|
||||||
|
@ -52,14 +53,17 @@ func Scan(rows *sql.Rows, db *gorm.DB) {
|
||||||
case reflect.Slice, reflect.Array:
|
case reflect.Slice, reflect.Array:
|
||||||
isPtr := db.Statement.ReflectValue.Type().Elem().Kind() == reflect.Ptr
|
isPtr := db.Statement.ReflectValue.Type().Elem().Kind() == reflect.Ptr
|
||||||
db.Statement.ReflectValue.Set(reflect.MakeSlice(db.Statement.ReflectValue.Type(), 0, 0))
|
db.Statement.ReflectValue.Set(reflect.MakeSlice(db.Statement.ReflectValue.Type(), 0, 0))
|
||||||
|
fields := make([]*schema.Field, len(columns))
|
||||||
|
|
||||||
|
for idx, column := range columns {
|
||||||
|
fields[idx] = db.Statement.Schema.LookUpField(column)
|
||||||
|
}
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
elem := reflect.New(db.Statement.Schema.ModelType).Elem()
|
elem := reflect.New(db.Statement.Schema.ModelType).Elem()
|
||||||
for idx, column := range columns {
|
for idx, field := range fields {
|
||||||
if field := db.Statement.Schema.LookUpField(column); field != nil {
|
if field != nil {
|
||||||
values[idx] = field.ReflectValueOf(elem).Addr().Interface()
|
values[idx] = field.ReflectValueOf(elem).Addr().Interface()
|
||||||
} else if db.RowsAffected == 0 {
|
|
||||||
values[idx] = sql.RawBytes{}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -121,7 +121,7 @@ func (l logger) Error(msg string, data ...interface{}) {
|
||||||
|
|
||||||
// Trace print sql message
|
// Trace print sql message
|
||||||
func (l logger) Trace(begin time.Time, fc func() (string, int64), err error) {
|
func (l logger) Trace(begin time.Time, fc func() (string, int64), err error) {
|
||||||
if elapsed := time.Now().Sub(begin); err != nil || elapsed > l.SlowThreshold {
|
if elapsed := time.Now().Sub(begin); err != nil || (elapsed > l.SlowThreshold && l.SlowThreshold != 0) {
|
||||||
sql, rows := fc()
|
sql, rows := fc()
|
||||||
fileline := utils.FileWithLineNum()
|
fileline := utils.FileWithLineNum()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -15,8 +15,8 @@ services:
|
||||||
ports:
|
ports:
|
||||||
- 9920:5432
|
- 9920:5432
|
||||||
environment:
|
environment:
|
||||||
- POSTGRES_USER=gorm
|
|
||||||
- POSTGRES_DB=gorm
|
- POSTGRES_DB=gorm
|
||||||
|
- POSTGRES_USER=gorm
|
||||||
- POSTGRES_PASSWORD=gorm
|
- POSTGRES_PASSWORD=gorm
|
||||||
mssql:
|
mssql:
|
||||||
image: 'mcmoe/mssqldocker:latest'
|
image: 'mcmoe/mssqldocker:latest'
|
||||||
|
|
Loading…
Reference in New Issue