优化func (session *Session) slice2Bean方法中的strings.ToLower以及减少map创建和访问次数(对应issue为: https://gitea.com/xorm/xorm/issues/2243)
This commit is contained in:
parent
a8ddcde883
commit
486fd42549
12
session.go
12
session.go
|
@ -393,10 +393,10 @@ func (session *Session) doPrepareTx(sqlStr string) (stmt *core.Stmt, err error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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, field *QueryedField) (*schemas.Column, *reflect.Value, error) {
|
||||||
col := table.GetColumnIdx(colName, idx)
|
col := field.ColumnSchema
|
||||||
if col == nil {
|
if col == nil {
|
||||||
return nil, nil, ErrFieldIsNotExist{colName, table.Name}
|
return nil, nil, ErrFieldIsNotExist{field.FieldName, table.Name}
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldValue, err := col.ValueOfV(dataStruct)
|
fieldValue, err := col.ValueOfV(dataStruct)
|
||||||
|
@ -404,10 +404,10 @@ func getField(dataStruct *reflect.Value, table *schemas.Table, colName string, i
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
if fieldValue == nil {
|
if fieldValue == nil {
|
||||||
return nil, nil, ErrFieldIsNotValid{colName, table.Name}
|
return nil, nil, ErrFieldIsNotValid{field.FieldName, table.Name}
|
||||||
}
|
}
|
||||||
if !fieldValue.IsValid() || !fieldValue.CanSet() {
|
if !fieldValue.IsValid() || !fieldValue.CanSet() {
|
||||||
return nil, nil, ErrFieldIsNotValid{colName, table.Name}
|
return nil, nil, ErrFieldIsNotValid{field.FieldName, table.Name}
|
||||||
}
|
}
|
||||||
|
|
||||||
return col, fieldValue, nil
|
return col, fieldValue, nil
|
||||||
|
@ -712,7 +712,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, allColum *AllColum
|
||||||
|
|
||||||
var pk schemas.PK
|
var pk schemas.PK
|
||||||
for i, field := range allColum.Fields {
|
for i, field := range allColum.Fields {
|
||||||
col, fieldValue, err := getField(dataStruct, table, field.FieldName, field.TempIndex)
|
col, fieldValue, err := getField(dataStruct, table, field)
|
||||||
if _, ok := err.(ErrFieldIsNotExist); ok {
|
if _, ok := err.(ErrFieldIsNotExist); ok {
|
||||||
continue
|
continue
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
|
|
@ -168,6 +168,7 @@ type QueryedField struct {
|
||||||
LowerFieldName string
|
LowerFieldName string
|
||||||
ColumnType *sql.ColumnType
|
ColumnType *sql.ColumnType
|
||||||
TempIndex int
|
TempIndex int
|
||||||
|
ColumnSchema *schemas.Column
|
||||||
}
|
}
|
||||||
|
|
||||||
type AllColumn struct {
|
type AllColumn struct {
|
||||||
|
@ -176,6 +177,19 @@ type AllColumn struct {
|
||||||
Types []*sql.ColumnType
|
Types []*sql.ColumnType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (allColumn *AllColumn) ParseTableSchema(table *schemas.Table) error {
|
||||||
|
for _, field := range allColumn.Fields {
|
||||||
|
col := table.GetColumnIdx(field.FieldName, field.TempIndex)
|
||||||
|
if col == nil {
|
||||||
|
return ErrFieldIsNotExist{FieldName: field.FieldName, TableName: table.Name}
|
||||||
|
}
|
||||||
|
|
||||||
|
field.ColumnSchema = col
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func ParseQueryRows(fieldNames []string, types []*sql.ColumnType) *AllColumn {
|
func ParseQueryRows(fieldNames []string, types []*sql.ColumnType) *AllColumn {
|
||||||
var allColumn AllColumn
|
var allColumn AllColumn
|
||||||
|
|
||||||
|
@ -289,6 +303,12 @@ func (session *Session) noCacheFind(table *schemas.Table, containerValue reflect
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parseTableSchemaError := allColumn.ParseTableSchema(tb)
|
||||||
|
if parseTableSchemaError != nil {
|
||||||
|
return parseTableSchemaError
|
||||||
|
}
|
||||||
|
|
||||||
err = session.rows2Beans(rows, allColumn, fields, types, tb, newElemFunc, containerValueSetFunc)
|
err = session.rows2Beans(rows, allColumn, fields, types, tb, newElemFunc, containerValueSetFunc)
|
||||||
rows.Close()
|
rows.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue