Refactor find slice

This commit is contained in:
Jinzhu 2020-03-04 22:16:39 +08:00
parent b0e1bccf4a
commit 9f7f4b430e
3 changed files with 10 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import (
"reflect"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/schema"
)
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:
isPtr := db.Statement.ReflectValue.Type().Elem().Kind() == reflect.Ptr
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() {
elem := reflect.New(db.Statement.Schema.ModelType).Elem()
for idx, column := range columns {
if field := db.Statement.Schema.LookUpField(column); field != nil {
for idx, field := range fields {
if field != nil {
values[idx] = field.ReflectValueOf(elem).Addr().Interface()
} else if db.RowsAffected == 0 {
values[idx] = sql.RawBytes{}
}
}

View File

@ -121,7 +121,7 @@ func (l logger) Error(msg string, data ...interface{}) {
// Trace print sql message
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()
fileline := utils.FileWithLineNum()
if err != nil {

View File

@ -15,8 +15,8 @@ services:
ports:
- 9920:5432
environment:
- POSTGRES_USER=gorm
- POSTGRES_DB=gorm
- POSTGRES_USER=gorm
- POSTGRES_PASSWORD=gorm
mssql:
image: 'mcmoe/mssqldocker:latest'