some improvement #2136

Merged
lunny merged 2 commits from lunny/fix_bug into master 2022-04-22 02:16:35 +00:00
Showing only changes of commit cf8e9338a0 - Show all commits

View File

@ -79,7 +79,7 @@ type Session struct {
afterClosures []func(interface{})
afterProcessors []executedProcessor
stmtCache map[uint32]*core.Stmt //key: hash.Hash32 of (queryStr, len(queryStr))
stmtCache map[uint32]*core.Stmt // key: hash.Hash32 of (queryStr, len(queryStr))
txStmtCache map[uint32]*core.Stmt // for tx statement
lastSQL string
@ -314,7 +314,7 @@ func (session *Session) Cascade(trueOrFalse ...bool) *Session {
// MustLogSQL means record SQL or not and don't follow engine's setting
func (session *Session) MustLogSQL(logs ...bool) *Session {
var showSQL = true
showSQL := true
if len(logs) > 0 {
showSQL = logs[0]
}
@ -396,7 +396,7 @@ func (session *Session) doPrepareTx(sqlStr string) (stmt *core.Stmt, err error)
}
func getField(dataStruct *reflect.Value, table *schemas.Table, colName string, idx int) (*schemas.Column, *reflect.Value, error) {
var col = table.GetColumnIdx(colName, idx)
col := table.GetColumnIdx(colName, idx)
if col == nil {
return nil, nil, ErrFieldIsNotExist{colName, table.Name}
}
@ -420,9 +420,10 @@ type Cell *interface{}
func (session *Session) rows2Beans(rows *core.Rows, fields []string, types []*sql.ColumnType,
table *schemas.Table, newElemFunc func([]string) reflect.Value,
sliceValueSetFunc func(*reflect.Value, schemas.PK) error) error {
sliceValueSetFunc func(*reflect.Value, schemas.PK) error,
) error {
for rows.Next() {
var newValue = newElemFunc(fields)
newValue := newElemFunc(fields)
bean := newValue.Interface()
dataStruct := newValue.Elem()
@ -533,8 +534,11 @@ func asKind(vv reflect.Value, tp reflect.Type) (interface{}, error) {
return nil, fmt.Errorf("unsupported primary key type: %v, %v", tp, vv)
}
var uint8ZeroValue = reflect.ValueOf(uint8(0))
func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflect.Value,
scanResult interface{}, table *schemas.Table) error {
scanResult interface{}, table *schemas.Table,
) error {
v, ok := scanResult.(*interface{})
if ok {
scanResult = *v
@ -596,7 +600,7 @@ func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflec
return nil
case reflect.Complex64, reflect.Complex128:
return setJSON(fieldValue, fieldType, scanResult)
case reflect.Slice, reflect.Array:
case reflect.Slice:
bs, ok := convert.AsBytes(scanResult)
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
if col.SQLType.IsText() {
@ -607,15 +611,29 @@ func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflec
}
fieldValue.Set(x.Elem())
} else {
if fieldValue.Len() > 0 {
for i := 0; i < fieldValue.Len(); i++ {
if i < vv.Len() {
fieldValue.Index(i).Set(vv.Index(i))
}
}
} else {
for i := 0; i < vv.Len(); i++ {
fieldValue.Set(reflect.Append(*fieldValue, vv.Index(i)))
fieldValue.Set(reflect.ValueOf(bs))
}
return nil
}
case reflect.Array:
bs, ok := convert.AsBytes(scanResult)
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
if col.SQLType.IsText() {
x := reflect.New(fieldType)
err := json.DefaultJSONHandler.Unmarshal(bs, x.Interface())
if err != nil {
return err
}
fieldValue.Set(x.Elem())
} else {
if fieldValue.Len() < vv.Len() {
return fmt.Errorf("Set field %s[Array] failed because of data too long", col.Name)
}
for i := 0; i < fieldValue.Len(); i++ {
if i < vv.Len() {
fieldValue.Index(i).Set(vv.Index(i))
} else {
fieldValue.Index(i).Set(uint8ZeroValue)
}
}
}
@ -659,7 +677,7 @@ func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflec
if len(table.PrimaryKeys) != 1 {
return errors.New("unsupported non or composited primary key cascade")
}
var pk = make(schemas.PK, len(table.PrimaryKeys))
pk := make(schemas.PK, len(table.PrimaryKeys))
pk[0], err = asKind(vv, reflect.TypeOf(scanResult))
if err != nil {
return err
@ -694,11 +712,11 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
buildAfterProcessors(session, bean)
var tempMap = make(map[string]int)
tempMap := make(map[string]int)
var pk schemas.PK
for i, colName := range fields {
var idx int
var lKey = strings.ToLower(colName)
lKey := strings.ToLower(colName)
var ok bool
if idx, ok = tempMap[lKey]; !ok {