Optimized session._row2bean function

This commit is contained in:
Sergey Kurt 2016-11-13 01:03:46 +03:00
parent f31f552026
commit 5480b6d000
1 changed files with 421 additions and 403 deletions

View File

@ -1606,23 +1606,20 @@ func (session *Session) dropAll() error {
return nil
}
func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table, idx int) *reflect.Value {
var col *core.Column
if col = table.GetColumnIdx(key, idx); col == nil {
//session.Engine.logger.Warnf("table %v has no column %v. %v", table.Name, key, table.ColumnsSeq())
return nil
}
func (session *Session) getField(dataStruct *reflect.Value, table *core.Table, col *core.Column) *reflect.Value {
fieldValue, err := col.ValueOfV(dataStruct)
if err != nil {
session.Engine.logger.Error(err)
return nil
}
if !fieldValue.IsValid() || !fieldValue.CanSet() {
session.Engine.logger.Warnf("table %v's column %v is not valid or cannot set", table.Name, key)
session.Engine.logger.Warnf("Table %v's column %v is not valid or cannot be set", table.Name, col.Name)
return nil
}
return fieldValue
}
@ -1656,6 +1653,24 @@ func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount i
return session._row2Bean(rows, fields, fieldsCount, bean, &dataStruct, session.Statement.RefTable)
}
func (session *Session) getColumnNdx(m map[string]int, name string) int {
n := len(name)
for k := range m {
if len(k) != n {
continue
}
if strings.EqualFold(k, name) {
m[k] = m[k] + 1
return m[k]
}
}
m[name] = 0
return 0
}
func (session *Session) _row2Bean(rows *core.Rows, fields []string, fieldsCount int, bean interface{}, dataStruct *reflect.Value, table *core.Table) error {
scanResults := make([]interface{}, fieldsCount)
for i := 0; i < len(fields); i++ {
@ -1680,19 +1695,24 @@ func (session *Session) _row2Bean(rows *core.Rows, fields []string, fieldsCount
}
}()
var tempMap = make(map[string]int)
for ii, key := range fields {
var idx int
var ok bool
var lKey = strings.ToLower(key)
if idx, ok = tempMap[lKey]; !ok {
idx = 0
} else {
idx = idx + 1
}
tempMap[lKey] = idx
var ndx int
var col *core.Column
var fieldValue *reflect.Value
var mColumnNdx = make(map[string]int)
for ii, key := range fields {
ndx = session.getColumnNdx(mColumnNdx, key)
if col = table.GetColumnIdx(key, ndx); col == nil {
//session.Engine.logger.Warnf("table %v has no column %v. %v", table.Name, key, table.ColumnsSeq())
continue
}
if fieldValue = session.getField(dataStruct, table, col); fieldValue == nil {
continue
}
if fieldValue := session.getField(dataStruct, key, table, idx); fieldValue != nil {
rawValue := reflect.Indirect(reflect.ValueOf(scanResults[ii]))
// if row is null then ignore
@ -1728,7 +1748,6 @@ func (session *Session) _row2Bean(rows *core.Rows, fields []string, fieldsCount
fieldType := fieldValue.Type()
hasAssigned := false
col := table.GetColumnIdx(key, idx)
if col.SQLType.IsJson() {
var bs []byte
@ -2119,7 +2138,6 @@ func (session *Session) _row2Bean(rows *core.Rows, fields []string, fieldsCount
}
}
}
}
return nil
}