some improvement
This commit is contained in:
parent
ea9bba0d14
commit
cf8e9338a0
56
session.go
56
session.go
|
@ -79,7 +79,7 @@ type Session struct {
|
||||||
afterClosures []func(interface{})
|
afterClosures []func(interface{})
|
||||||
afterProcessors []executedProcessor
|
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
|
txStmtCache map[uint32]*core.Stmt // for tx statement
|
||||||
|
|
||||||
lastSQL string
|
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
|
// MustLogSQL means record SQL or not and don't follow engine's setting
|
||||||
func (session *Session) MustLogSQL(logs ...bool) *Session {
|
func (session *Session) MustLogSQL(logs ...bool) *Session {
|
||||||
var showSQL = true
|
showSQL := true
|
||||||
if len(logs) > 0 {
|
if len(logs) > 0 {
|
||||||
showSQL = 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) {
|
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 {
|
if col == nil {
|
||||||
return nil, nil, ErrFieldIsNotExist{colName, table.Name}
|
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,
|
func (session *Session) rows2Beans(rows *core.Rows, fields []string, types []*sql.ColumnType,
|
||||||
table *schemas.Table, newElemFunc func([]string) reflect.Value,
|
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() {
|
for rows.Next() {
|
||||||
var newValue = newElemFunc(fields)
|
newValue := newElemFunc(fields)
|
||||||
bean := newValue.Interface()
|
bean := newValue.Interface()
|
||||||
dataStruct := newValue.Elem()
|
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)
|
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,
|
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{})
|
v, ok := scanResult.(*interface{})
|
||||||
if ok {
|
if ok {
|
||||||
scanResult = *v
|
scanResult = *v
|
||||||
|
@ -596,7 +600,7 @@ func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflec
|
||||||
return nil
|
return nil
|
||||||
case reflect.Complex64, reflect.Complex128:
|
case reflect.Complex64, reflect.Complex128:
|
||||||
return setJSON(fieldValue, fieldType, scanResult)
|
return setJSON(fieldValue, fieldType, scanResult)
|
||||||
case reflect.Slice, reflect.Array:
|
case reflect.Slice:
|
||||||
bs, ok := convert.AsBytes(scanResult)
|
bs, ok := convert.AsBytes(scanResult)
|
||||||
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
|
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
|
||||||
if col.SQLType.IsText() {
|
if col.SQLType.IsText() {
|
||||||
|
@ -607,15 +611,29 @@ func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflec
|
||||||
}
|
}
|
||||||
fieldValue.Set(x.Elem())
|
fieldValue.Set(x.Elem())
|
||||||
} else {
|
} else {
|
||||||
if fieldValue.Len() > 0 {
|
fieldValue.Set(reflect.ValueOf(bs))
|
||||||
for i := 0; i < fieldValue.Len(); i++ {
|
}
|
||||||
if i < vv.Len() {
|
return nil
|
||||||
fieldValue.Index(i).Set(vv.Index(i))
|
}
|
||||||
}
|
case reflect.Array:
|
||||||
}
|
bs, ok := convert.AsBytes(scanResult)
|
||||||
} else {
|
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
|
||||||
for i := 0; i < vv.Len(); i++ {
|
if col.SQLType.IsText() {
|
||||||
fieldValue.Set(reflect.Append(*fieldValue, vv.Index(i)))
|
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 {
|
if len(table.PrimaryKeys) != 1 {
|
||||||
return errors.New("unsupported non or composited primary key cascade")
|
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))
|
pk[0], err = asKind(vv, reflect.TypeOf(scanResult))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -694,11 +712,11 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
|
||||||
|
|
||||||
buildAfterProcessors(session, bean)
|
buildAfterProcessors(session, bean)
|
||||||
|
|
||||||
var tempMap = make(map[string]int)
|
tempMap := make(map[string]int)
|
||||||
var pk schemas.PK
|
var pk schemas.PK
|
||||||
for i, colName := range fields {
|
for i, colName := range fields {
|
||||||
var idx int
|
var idx int
|
||||||
var lKey = strings.ToLower(colName)
|
lKey := strings.ToLower(colName)
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
if idx, ok = tempMap[lKey]; !ok {
|
if idx, ok = tempMap[lKey]; !ok {
|
||||||
|
|
Loading…
Reference in New Issue