Getting flag for the column via helper method (#492)

This commit is contained in:
Sergey 2016-11-12 07:30:16 +04:00 committed by Lunny Xiao
parent f2610e02a1
commit f31f552026
4 changed files with 99 additions and 14 deletions

View File

@ -490,9 +490,8 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
args := make([]interface{}, 0, len(table.ColumnsSeq())) args := make([]interface{}, 0, len(table.ColumnsSeq()))
for _, col := range table.Columns() { for _, col := range table.Columns() {
lColName := strings.ToLower(col.Name)
if useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated { if useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated {
if _, ok := session.Statement.columnMap[lColName]; !ok { if _, ok := getFlagForColumn(session.Statement.columnMap, col); !ok {
continue continue
} }
} }
@ -528,18 +527,18 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
} }
if session.Statement.ColumnStr != "" { if session.Statement.ColumnStr != "" {
if _, ok := session.Statement.columnMap[lColName]; !ok { if _, ok := getFlagForColumn(session.Statement.columnMap, col); !ok {
continue continue
} }
} }
if session.Statement.OmitStr != "" { if session.Statement.OmitStr != "" {
if _, ok := session.Statement.columnMap[lColName]; ok { if _, ok := getFlagForColumn(session.Statement.columnMap, col); ok {
continue continue
} }
} }
// !evalphobia! set fieldValue as nil when column is nullable and zero-value // !evalphobia! set fieldValue as nil when column is nullable and zero-value
if _, ok := session.Statement.nullableMap[lColName]; ok { if _, ok := getFlagForColumn(session.Statement.nullableMap, col); ok {
if col.Nullable && isZero(fieldValue.Interface()) { if col.Nullable && isZero(fieldValue.Interface()) {
var nilValue *int var nilValue *int
fieldValue = reflect.ValueOf(nilValue) fieldValue = reflect.ValueOf(nilValue)
@ -578,3 +577,23 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
func indexName(tableName, idxName string) string { func indexName(tableName, idxName string) string {
return fmt.Sprintf("IDX_%v_%v", tableName, idxName) return fmt.Sprintf("IDX_%v_%v", tableName, idxName)
} }
func getFlagForColumn(m map[string]bool, col *core.Column) (val bool, has bool) {
if len(m) == 0 {
return false, false
}
n := len(col.Name)
for mk := range m {
if len(mk) != n {
continue
}
if strings.EqualFold(mk, col.Name) {
return m[mk], true
}
}
return false, false
}

View File

@ -2318,12 +2318,12 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
continue continue
} }
if session.Statement.ColumnStr != "" { if session.Statement.ColumnStr != "" {
if _, ok := session.Statement.columnMap[strings.ToLower(col.Name)]; !ok { if _, ok := getFlagForColumn(session.Statement.columnMap, col); !ok {
continue continue
} }
} }
if session.Statement.OmitStr != "" { if session.Statement.OmitStr != "" {
if _, ok := session.Statement.columnMap[strings.ToLower(col.Name)]; ok { if _, ok := getFlagForColumn(session.Statement.columnMap, col); ok {
continue continue
} }
} }
@ -2373,12 +2373,12 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
continue continue
} }
if session.Statement.ColumnStr != "" { if session.Statement.ColumnStr != "" {
if _, ok := session.Statement.columnMap[strings.ToLower(col.Name)]; !ok { if _, ok := getFlagForColumn(session.Statement.columnMap, col); !ok {
continue continue
} }
} }
if session.Statement.OmitStr != "" { if session.Statement.OmitStr != "" {
if _, ok := session.Statement.columnMap[strings.ToLower(col.Name)]; ok { if _, ok := getFlagForColumn(session.Statement.columnMap, col); ok {
continue continue
} }
} }

View File

@ -273,9 +273,8 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
requiredField := useAllCols requiredField := useAllCols
includeNil := useAllCols includeNil := useAllCols
lColName := strings.ToLower(col.Name)
if b, ok := mustColumnMap[lColName]; ok { if b, ok := getFlagForColumn(mustColumnMap, col); ok {
if b { if b {
requiredField = true requiredField = true
} else { } else {
@ -284,7 +283,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
} }
// !evalphobia! set fieldValue as nil when column is nullable and zero-value // !evalphobia! set fieldValue as nil when column is nullable and zero-value
if b, ok := nullableMap[lColName]; ok { if b, ok := getFlagForColumn(nullableMap, col); ok {
if b && col.Nullable && isZero(fieldValue.Interface()) { if b && col.Nullable && isZero(fieldValue.Interface()) {
var nilValue *int var nilValue *int
fieldValue = reflect.ValueOf(nilValue) fieldValue = reflect.ValueOf(nilValue)
@ -533,7 +532,8 @@ func buildConds(engine *Engine, table *core.Table, bean interface{},
fieldType := reflect.TypeOf(fieldValue.Interface()) fieldType := reflect.TypeOf(fieldValue.Interface())
requiredField := useAllCols requiredField := useAllCols
if b, ok := mustColumnMap[strings.ToLower(col.Name)]; ok {
if b, ok := getFlagForColumn(mustColumnMap, col); ok {
if b { if b {
requiredField = true requiredField = true
} else { } else {
@ -993,7 +993,7 @@ func (statement *Statement) genColumnStr() string {
for _, col := range columns { for _, col := range columns {
if statement.OmitStr != "" { if statement.OmitStr != "" {
if _, ok := statement.columnMap[strings.ToLower(col.Name)]; ok { if _, ok := getFlagForColumn(statement.columnMap, col); ok {
continue continue
} }
} }

View File

@ -6,6 +6,8 @@ import (
"testing" "testing"
"time" "time"
"strings"
"github.com/go-xorm/core" "github.com/go-xorm/core"
) )
@ -83,6 +85,70 @@ func BenchmarkColumnsStringGeneration(b *testing.B) {
} }
} }
func BenchmarkGetFlagForColumnWithICKey_ContainsKey(b *testing.B) {
b.StopTimer()
mapCols := make(map[string]bool)
cols := []*core.Column{
&core.Column{Name: `ID`},
&core.Column{Name: `IsDeleted`},
&core.Column{Name: `Caption`},
&core.Column{Name: `Code1`},
&core.Column{Name: `Code2`},
&core.Column{Name: `Code3`},
&core.Column{Name: `ParentID`},
&core.Column{Name: `Latitude`},
&core.Column{Name: `Longitude`},
}
for _, col := range cols {
mapCols[strings.ToLower(col.Name)] = true
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, col := range cols {
if _, ok := getFlagForColumn(mapCols, col); !ok {
b.Fatal("Unexpected result")
}
}
}
}
func BenchmarkGetFlagForColumnWithICKey_EmptyMap(b *testing.B) {
b.StopTimer()
mapCols := make(map[string]bool)
cols := []*core.Column{
&core.Column{Name: `ID`},
&core.Column{Name: `IsDeleted`},
&core.Column{Name: `Caption`},
&core.Column{Name: `Code1`},
&core.Column{Name: `Code2`},
&core.Column{Name: `Code3`},
&core.Column{Name: `ParentID`},
&core.Column{Name: `Latitude`},
&core.Column{Name: `Longitude`},
}
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, col := range cols {
if _, ok := getFlagForColumn(mapCols, col); ok {
b.Fatal("Unexpected result")
}
}
}
}
type TestType struct { type TestType struct {
ID int64 `xorm:"ID PK"` ID int64 `xorm:"ID PK"`
IsDeleted bool `xorm:"IsDeleted"` IsDeleted bool `xorm:"IsDeleted"`