|
|
|
@ -161,6 +161,16 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (session *Session) noCacheFind(table *schemas.Table, containerValue reflect.Value, sqlStr string, args ...interface{}) error { |
|
|
|
|
elemType := containerValue.Type().Elem() |
|
|
|
|
var isPointer bool |
|
|
|
|
if elemType.Kind() == reflect.Ptr { |
|
|
|
|
isPointer = true |
|
|
|
|
elemType = elemType.Elem() |
|
|
|
|
} |
|
|
|
|
if elemType.Kind() == reflect.Ptr { |
|
|
|
|
return errors.New("pointer to pointer is not supported") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
rows, err := session.queryRows(sqlStr, args...) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
@ -177,31 +187,8 @@ func (session *Session) noCacheFind(table *schemas.Table, containerValue reflect |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var newElemFunc func(fields []string) reflect.Value |
|
|
|
|
elemType := containerValue.Type().Elem() |
|
|
|
|
var isPointer bool |
|
|
|
|
if elemType.Kind() == reflect.Ptr { |
|
|
|
|
isPointer = true |
|
|
|
|
elemType = elemType.Elem() |
|
|
|
|
} |
|
|
|
|
if elemType.Kind() == reflect.Ptr { |
|
|
|
|
return errors.New("pointer to pointer is not supported") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
newElemFunc = func(fields []string) reflect.Value { |
|
|
|
|
switch elemType.Kind() { |
|
|
|
|
case reflect.Slice: |
|
|
|
|
slice := reflect.MakeSlice(elemType, len(fields), len(fields)) |
|
|
|
|
x := reflect.New(slice.Type()) |
|
|
|
|
x.Elem().Set(slice) |
|
|
|
|
return x |
|
|
|
|
case reflect.Map: |
|
|
|
|
mp := reflect.MakeMap(elemType) |
|
|
|
|
x := reflect.New(mp.Type()) |
|
|
|
|
x.Elem().Set(mp) |
|
|
|
|
return x |
|
|
|
|
} |
|
|
|
|
return reflect.New(elemType) |
|
|
|
|
var newElemFunc = func(fields []string) reflect.Value { |
|
|
|
|
return utils.New(elemType, len(fields), len(fields)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var containerValueSetFunc func(*reflect.Value, schemas.PK) error |
|
|
|
@ -226,10 +213,15 @@ func (session *Session) noCacheFind(table *schemas.Table, containerValue reflect |
|
|
|
|
|
|
|
|
|
containerValueSetFunc = func(newValue *reflect.Value, pk schemas.PK) error { |
|
|
|
|
keyValue := reflect.New(keyType) |
|
|
|
|
err := convertPKToValue(table, keyValue.Interface(), pk) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
cols := table.PKColumns() |
|
|
|
|
if len(cols) == 1 { |
|
|
|
|
if err := convert.AssignValue(keyValue, pk[0]); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
keyValue.Set(reflect.ValueOf(&pk)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if isPointer { |
|
|
|
|
containerValue.SetMapIndex(keyValue.Elem(), newValue.Elem().Addr()) |
|
|
|
|
} else { |
|
|
|
@ -241,8 +233,7 @@ func (session *Session) noCacheFind(table *schemas.Table, containerValue reflect |
|
|
|
|
|
|
|
|
|
if elemType.Kind() == reflect.Struct { |
|
|
|
|
var newValue = newElemFunc(fields) |
|
|
|
|
dataStruct := utils.ReflectValue(newValue.Interface()) |
|
|
|
|
tb, err := session.engine.tagParser.ParseWithCache(dataStruct) |
|
|
|
|
tb, err := session.engine.tagParser.ParseWithCache(newValue) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -266,7 +257,6 @@ func (session *Session) noCacheFind(table *schemas.Table, containerValue reflect |
|
|
|
|
default: |
|
|
|
|
err = rows.Scan(bean) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -278,16 +268,6 @@ func (session *Session) noCacheFind(table *schemas.Table, containerValue reflect |
|
|
|
|
return rows.Err() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func convertPKToValue(table *schemas.Table, dst interface{}, pk schemas.PK) error { |
|
|
|
|
cols := table.PKColumns() |
|
|
|
|
if len(cols) == 1 { |
|
|
|
|
return convert.Assign(dst, pk[0], nil, nil) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
dst = pk |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr interface{}, args ...interface{}) (err error) { |
|
|
|
|
if !session.canCache() || |
|
|
|
|
utils.IndexNoCase(sqlStr, "having") != -1 || |
|
|
|
|