Cleanup code and remove unnecessary wrappers in statement.go
This commit is contained in:
parent
3442ce81b6
commit
ee6e17b4e3
|
@ -1173,9 +1173,9 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
// See https://github.com/go-xorm/xorm/issues/179
|
// See https://github.com/go-xorm/xorm/issues/179
|
||||||
if col := table.DeletedColumn(); col != nil && !session.Statement.unscoped {
|
if col := table.DeletedColumn(); col != nil && !session.Statement.unscoped {
|
||||||
// tag "deleted" is enabled
|
// tag "deleted" is enabled
|
||||||
var colName = session.Statement.colName(col, session.Statement.TableName())
|
var colName = session.Statement.colName(col)
|
||||||
session.Statement.ConditionStr = fmt.Sprintf("(%v IS NULL OR %v = '0001-01-01 00:00:00')",
|
session.Statement.ConditionStr = fmt.Sprintf(
|
||||||
colName, colName)
|
"(%v IS NULL OR %v = '0001-01-01 00:00:00')", colName, colName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
108
statement.go
108
statement.go
|
@ -454,47 +454,62 @@ func (statement *Statement) needTableName() bool {
|
||||||
return len(statement.JoinStr) > 0
|
return len(statement.JoinStr) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) colName(col *core.Column, tableName string) string {
|
func (statement *Statement) tableMapAdd(table string) {
|
||||||
return buildColName(statement.Engine, col, tableName, statement.TableAlias,
|
tableName := statement.Engine.Quote(strings.ToLower(table))
|
||||||
statement.outTableName(), statement.needTableName(), statement.tableMap)
|
statement.tableMap[tableName] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildColName(engine *Engine, col *core.Column,
|
func (statement *Statement) tableMapDelete(table string) {
|
||||||
mainTableName, mainTableAlias, outTableName string, needTableName bool,
|
tableName := statement.Engine.Quote(strings.ToLower(table))
|
||||||
knownTables map[string]bool) string {
|
delete(statement.tableMap, tableName)
|
||||||
var colTable string
|
}
|
||||||
|
|
||||||
if needTableName {
|
func (statement *Statement) isKnownTable(table string) bool {
|
||||||
|
if len(table) > 0 {
|
||||||
var mainTable string
|
var mainTable string
|
||||||
|
|
||||||
if len(mainTableAlias) > 0 {
|
if len(statement.TableAlias) > 0 {
|
||||||
mainTable = mainTableAlias
|
mainTable = statement.TableAlias
|
||||||
} else {
|
} else {
|
||||||
mainTable = mainTableName
|
mainTable = statement.TableName()
|
||||||
}
|
}
|
||||||
|
|
||||||
if isKnownTable(engine, knownTables, mainTable, col.TableName) {
|
cm := statement.Engine.Quote(strings.ToLower(mainTable))
|
||||||
|
ct := statement.Engine.Quote(strings.ToLower(table))
|
||||||
|
|
||||||
|
if ct == cm || statement.tableMap[ct] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) colName(col *core.Column) string {
|
||||||
|
var colTable string
|
||||||
|
|
||||||
|
if statement.needTableName() {
|
||||||
|
if statement.isKnownTable(col.TableName) {
|
||||||
colTable = col.TableName
|
colTable = col.TableName
|
||||||
} else if isKnownTable(engine, knownTables, mainTable, outTableName) {
|
} else if statement.isKnownTable(statement.outTableName()) {
|
||||||
colTable = outTableName
|
colTable = statement.outTableName()
|
||||||
} else {
|
} else {
|
||||||
colTable = ""
|
colTable = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if colTable != "" {
|
if colTable != "" {
|
||||||
return engine.Quote(colTable) + "." + engine.Quote(col.Name)
|
return statement.Engine.Quote(colTable) + "." + statement.Engine.Quote(col.Name)
|
||||||
} else {
|
} else {
|
||||||
return engine.Quote(col.Name)
|
return statement.Engine.Quote(col.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto generating conditions according a struct
|
// Auto generating conditions according a struct
|
||||||
func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
func (statement *Statement) buildConditions(
|
||||||
includeVersion bool, includeUpdated bool, includeNil bool,
|
table *core.Table, bean interface{},
|
||||||
includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool,
|
includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool,
|
||||||
mustColumnMap map[string]bool, tableName, aliasName, outTableName string,
|
addedTableName bool) ([]string, []interface{}) {
|
||||||
addedTableName bool, knownTables map[string]bool) ([]string, []interface{}) {
|
engine := statement.Engine
|
||||||
var colNames []string
|
var colNames []string
|
||||||
var args = make([]interface{}, 0)
|
var args = make([]interface{}, 0)
|
||||||
for _, col := range table.Columns() {
|
for _, col := range table.Columns() {
|
||||||
|
@ -515,8 +530,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
colName := buildColName(
|
colName := statement.colName(col)
|
||||||
engine, col, tableName, aliasName, outTableName, addedTableName, knownTables)
|
|
||||||
|
|
||||||
fieldValuePtr, err := col.ValueOf(bean)
|
fieldValuePtr, err := col.ValueOf(bean)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -524,9 +538,9 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if col.IsDeleted && !unscoped { // tag "deleted" is enabled
|
if col.IsDeleted && !statement.unscoped { // tag "deleted" is enabled
|
||||||
colNames = append(colNames, fmt.Sprintf("(%v IS NULL OR %v = '0001-01-01 00:00:00')",
|
colNames = append(colNames, fmt.Sprintf(
|
||||||
colName, colName))
|
"(%v IS NULL OR %v = '0001-01-01 00:00:00')", colName, colName))
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldValue := *fieldValuePtr
|
fieldValue := *fieldValuePtr
|
||||||
|
@ -535,8 +549,8 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldType := reflect.TypeOf(fieldValue.Interface())
|
fieldType := reflect.TypeOf(fieldValue.Interface())
|
||||||
requiredField := useAllCols
|
requiredField := statement.useAllCols
|
||||||
if b, ok := mustColumnMap[strings.ToLower(col.Name)]; ok {
|
if b, ok := statement.mustColumnMap[strings.ToLower(col.Name)]; ok {
|
||||||
if b {
|
if b {
|
||||||
requiredField = true
|
requiredField = true
|
||||||
} else {
|
} else {
|
||||||
|
@ -564,7 +578,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
var val interface{}
|
var val interface{}
|
||||||
switch fieldType.Kind() {
|
switch fieldType.Kind() {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
if allUseBool || requiredField {
|
if statement.allUseBool || requiredField {
|
||||||
val = fieldValue.Interface()
|
val = fieldValue.Interface()
|
||||||
} else {
|
} else {
|
||||||
// if a bool in a struct, it will not be as a condition because it default is false,
|
// if a bool in a struct, it will not be as a condition because it default is false,
|
||||||
|
@ -1111,7 +1125,7 @@ func (statement *Statement) genColumnStr() string {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
name := statement.colName(col, statement.TableName())
|
name := statement.colName(col)
|
||||||
|
|
||||||
if col.IsPrimaryKey && statement.Engine.Dialect().DBType() == "ql" {
|
if col.IsPrimaryKey && statement.Engine.Dialect().DBType() == "ql" {
|
||||||
colNames = append(colNames, "id() AS "+name)
|
colNames = append(colNames, "id() AS "+name)
|
||||||
|
@ -1218,21 +1232,16 @@ func (s *Statement) genAddUniqueStr(uqeName string, cols []string) (string, []in
|
||||||
return sql, []interface{}{}
|
return sql, []interface{}{}
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
func (statement *Statement) buildConditions(table *core.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, addedTableName bool) ([]string, []interface{}) {
|
|
||||||
return buildConditions(statement.Engine, table, bean, includeVersion, includeUpdated, includeNil, includeAutoIncr, statement.allUseBool, statement.useAllCols,
|
|
||||||
statement.unscoped, statement.mustColumnMap, statement.TableName(), statement.TableAlias, statement.outTableName(), addedTableName, statement.tableMap)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}) {
|
func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}) {
|
||||||
table := statement.Engine.TableInfo(bean)
|
table := statement.Engine.TableInfo(bean)
|
||||||
statement.RefTable = table
|
statement.RefTable = table
|
||||||
|
|
||||||
var addedTableName = (len(statement.JoinStr) > 0)
|
|
||||||
|
|
||||||
if !statement.noAutoCondition {
|
if !statement.noAutoCondition {
|
||||||
colNames, args := statement.buildConditions(table, bean, true, true, false, true, addedTableName)
|
colNames, args := statement.buildConditions(
|
||||||
|
table, bean, true, true, false, true, statement.needTableName())
|
||||||
|
|
||||||
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ")
|
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ")
|
||||||
|
|
||||||
statement.BeanArgs = args
|
statement.BeanArgs = args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1356,7 +1365,7 @@ func (statement *Statement) processIdParam() {
|
||||||
if statement.IdParam != nil {
|
if statement.IdParam != nil {
|
||||||
if statement.Engine.dialect.DBType() != "ql" {
|
if statement.Engine.dialect.DBType() != "ql" {
|
||||||
for i, col := range statement.RefTable.PKColumns() {
|
for i, col := range statement.RefTable.PKColumns() {
|
||||||
var colName = statement.colName(col, statement.TableName())
|
var colName = statement.colName(col)
|
||||||
if i < len(*(statement.IdParam)) {
|
if i < len(*(statement.IdParam)) {
|
||||||
statement.And(fmt.Sprintf("%v %s ?", colName,
|
statement.And(fmt.Sprintf("%v %s ?", colName,
|
||||||
statement.Engine.dialect.EqStr()), (*(statement.IdParam))[i])
|
statement.Engine.dialect.EqStr()), (*(statement.IdParam))[i])
|
||||||
|
@ -1372,24 +1381,3 @@ func (statement *Statement) processIdParam() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) tableMapAdd(table string) {
|
|
||||||
tableName := statement.Engine.Quote(strings.ToLower(table))
|
|
||||||
statement.tableMap[tableName] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (statement *Statement) tableMapDelete(table string) {
|
|
||||||
tableName := statement.Engine.Quote(strings.ToLower(table))
|
|
||||||
delete(statement.tableMap, tableName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func isKnownTable(engine *Engine, tableMap map[string]bool, mainTable, table string) bool {
|
|
||||||
if len(table) > 0 {
|
|
||||||
cm := engine.Quote(strings.ToLower(mainTable))
|
|
||||||
ct := engine.Quote(strings.ToLower(table))
|
|
||||||
if ct == cm || tableMap[ct] {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue