bug fixed for deleted & alias conflict
This commit is contained in:
parent
fad61020e8
commit
025e2a20e5
23
session.go
23
session.go
|
@ -49,6 +49,7 @@ type Session struct {
|
||||||
cascadeDeep int
|
cascadeDeep int
|
||||||
|
|
||||||
// !evalphobia! stored the last executed query on this session
|
// !evalphobia! stored the last executed query on this session
|
||||||
|
//beforeSQLExec func(string, ...interface{})
|
||||||
lastSQL string
|
lastSQL string
|
||||||
lastSQLArgs []interface{}
|
lastSQLArgs []interface{}
|
||||||
}
|
}
|
||||||
|
@ -1225,10 +1226,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
|
|
||||||
var addedTableName = (len(session.Statement.JoinStr) > 0)
|
var addedTableName = (len(session.Statement.JoinStr) > 0)
|
||||||
if !session.Statement.noAutoCondition && len(condiBean) > 0 {
|
if !session.Statement.noAutoCondition && len(condiBean) > 0 {
|
||||||
colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true,
|
colNames, args := session.Statement.buildConditions(table, condiBean[0], true, true, false, true, addedTableName)
|
||||||
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
|
||||||
session.Statement.unscoped, session.Statement.mustColumnMap,
|
|
||||||
session.Statement.TableName(), addedTableName)
|
|
||||||
session.Statement.ConditionStr = strings.Join(colNames, " AND ")
|
session.Statement.ConditionStr = strings.Join(colNames, " AND ")
|
||||||
session.Statement.BeanArgs = args
|
session.Statement.BeanArgs = args
|
||||||
} else {
|
} else {
|
||||||
|
@ -1237,9 +1235,13 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
if col := table.DeletedColumn(); col != nil && !session.Statement.unscoped { // tag "deleted" is enabled
|
if col := table.DeletedColumn(); col != nil && !session.Statement.unscoped { // tag "deleted" is enabled
|
||||||
var colName string = session.Engine.Quote(col.Name)
|
var colName string = session.Engine.Quote(col.Name)
|
||||||
if addedTableName {
|
if addedTableName {
|
||||||
colName = session.Engine.Quote(session.Statement.TableName()) + "." + colName
|
var nm = session.Statement.TableName()
|
||||||
|
if len(session.Statement.TableAlias) > 0 {
|
||||||
|
nm = session.Statement.TableAlias
|
||||||
|
}
|
||||||
|
colName = session.Engine.Quote(nm) + "." + colName
|
||||||
}
|
}
|
||||||
session.Statement.ConditionStr = fmt.Sprintf("(%v IS NULL or %v = '0001-01-01 00:00:00') ",
|
session.Statement.ConditionStr = fmt.Sprintf("%v IS NULL OR %v = '0001-01-01 00:00:00'",
|
||||||
colName, colName)
|
colName, colName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3641,9 +3643,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
||||||
var condiArgs []interface{}
|
var condiArgs []interface{}
|
||||||
|
|
||||||
if !session.Statement.noAutoCondition && len(condiBean) > 0 {
|
if !session.Statement.noAutoCondition && len(condiBean) > 0 {
|
||||||
condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true,
|
condiColNames, condiArgs = session.Statement.buildConditions(session.Statement.RefTable, condiBean[0], true, true, false, true, false)
|
||||||
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
|
||||||
session.Statement.unscoped, session.Statement.mustColumnMap, session.Statement.TableName(), false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var condition = ""
|
var condition = ""
|
||||||
|
@ -3866,10 +3866,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
|
||||||
var args []interface{}
|
var args []interface{}
|
||||||
|
|
||||||
if !session.Statement.noAutoCondition {
|
if !session.Statement.noAutoCondition {
|
||||||
colNames, args = buildConditions(session.Engine, table, bean, true, true,
|
colNames, args = session.Statement.buildConditions(table, bean, true, true, false, true, false)
|
||||||
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
|
||||||
session.Statement.unscoped, session.Statement.mustColumnMap,
|
|
||||||
session.Statement.TableName(), false)
|
|
||||||
}
|
}
|
||||||
var condition = ""
|
var condition = ""
|
||||||
var andStr = session.Engine.dialect.AndStr()
|
var andStr = session.Engine.dialect.AndStr()
|
||||||
|
|
23
statement.go
23
statement.go
|
@ -427,7 +427,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
||||||
func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
includeVersion bool, includeUpdated bool, includeNil bool,
|
includeVersion bool, includeUpdated bool, includeNil bool,
|
||||||
includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool,
|
includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool,
|
||||||
mustColumnMap map[string]bool, tableName string, addedTableName bool) ([]string, []interface{}) {
|
mustColumnMap map[string]bool, tableName, aliasName string, addedTableName bool) ([]string, []interface{}) {
|
||||||
colNames := make([]string, 0)
|
colNames := make([]string, 0)
|
||||||
var args = make([]interface{}, 0)
|
var args = make([]interface{}, 0)
|
||||||
for _, col := range table.Columns() {
|
for _, col := range table.Columns() {
|
||||||
|
@ -450,7 +450,11 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
|
|
||||||
var colName string
|
var colName string
|
||||||
if addedTableName {
|
if addedTableName {
|
||||||
colName = engine.Quote(tableName) + "." + engine.Quote(col.Name)
|
var nm = tableName
|
||||||
|
if len(aliasName) > 0 {
|
||||||
|
nm = aliasName
|
||||||
|
}
|
||||||
|
colName = engine.Quote(nm) + "." + engine.Quote(col.Name)
|
||||||
} else {
|
} else {
|
||||||
colName = engine.Quote(col.Name)
|
colName = engine.Quote(col.Name)
|
||||||
}
|
}
|
||||||
|
@ -462,7 +466,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if col.IsDeleted && !unscoped { // tag "deleted" is enabled
|
if col.IsDeleted && !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("%v IS NULL or %v = '0001-01-01 00:00:00'",
|
||||||
colName, colName))
|
colName, colName))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1123,9 +1127,7 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
|
||||||
var addedTableName = (len(statement.JoinStr) > 0)
|
var addedTableName = (len(statement.JoinStr) > 0)
|
||||||
|
|
||||||
if !statement.noAutoCondition {
|
if !statement.noAutoCondition {
|
||||||
colNames, args := buildConditions(statement.Engine, table, bean, true, true,
|
colNames, args := statement.buildConditions(table, bean, true, true, false, true, addedTableName)
|
||||||
false, true, statement.allUseBool, statement.useAllCols,
|
|
||||||
statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName)
|
|
||||||
|
|
||||||
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ")
|
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ")
|
||||||
statement.BeanArgs = args
|
statement.BeanArgs = args
|
||||||
|
@ -1179,6 +1181,11 @@ 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, addedTableName)
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -1186,9 +1193,7 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}
|
||||||
var addedTableName = (len(statement.JoinStr) > 0)
|
var addedTableName = (len(statement.JoinStr) > 0)
|
||||||
|
|
||||||
if !statement.noAutoCondition {
|
if !statement.noAutoCondition {
|
||||||
colNames, args := buildConditions(statement.Engine, table, bean, true, true, false,
|
colNames, args := statement.buildConditions(table, bean, true, true, false, true, addedTableName)
|
||||||
true, statement.allUseBool, statement.useAllCols,
|
|
||||||
statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName)
|
|
||||||
|
|
||||||
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ")
|
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ")
|
||||||
statement.BeanArgs = args
|
statement.BeanArgs = args
|
||||||
|
|
Loading…
Reference in New Issue