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"
|
||||
|
||||
"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{}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Reference in New Issue