diff --git a/VERSION b/VERSION index 2efec6a8..0e249f88 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -xorm v0.4.5.0102 +xorm v0.4.5.0112 diff --git a/session.go b/session.go index 51073fa3..132a396d 100644 --- a/session.go +++ b/session.go @@ -49,6 +49,7 @@ type Session struct { cascadeDeep int // !evalphobia! stored the last executed query on this session + //beforeSQLExec func(string, ...interface{}) lastSQL string lastSQLArgs []interface{} } @@ -1225,10 +1226,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) var addedTableName = (len(session.Statement.JoinStr) > 0) if !session.Statement.noAutoCondition && len(condiBean) > 0 { - colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true, - false, true, session.Statement.allUseBool, session.Statement.useAllCols, - session.Statement.unscoped, session.Statement.mustColumnMap, - session.Statement.TableName(), addedTableName) + colNames, args := session.Statement.buildConditions(table, condiBean[0], true, true, false, true, addedTableName) session.Statement.ConditionStr = strings.Join(colNames, " AND ") session.Statement.BeanArgs = args } 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 var colName string = session.Engine.Quote(col.Name) 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) } } @@ -3641,9 +3643,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 var condiArgs []interface{} if !session.Statement.noAutoCondition && len(condiBean) > 0 { - condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true, - false, true, session.Statement.allUseBool, session.Statement.useAllCols, - session.Statement.unscoped, session.Statement.mustColumnMap, session.Statement.TableName(), false) + condiColNames, condiArgs = session.Statement.buildConditions(session.Statement.RefTable, condiBean[0], true, true, false, true, false) } var condition = "" @@ -3866,10 +3866,7 @@ func (session *Session) Delete(bean interface{}) (int64, error) { var args []interface{} if !session.Statement.noAutoCondition { - colNames, args = buildConditions(session.Engine, table, bean, true, true, - false, true, session.Statement.allUseBool, session.Statement.useAllCols, - session.Statement.unscoped, session.Statement.mustColumnMap, - session.Statement.TableName(), false) + colNames, args = session.Statement.buildConditions(table, bean, true, true, false, true, false) } var condition = "" var andStr = session.Engine.dialect.AndStr() diff --git a/statement.go b/statement.go index a8d82f24..444f4e93 100644 --- a/statement.go +++ b/statement.go @@ -427,7 +427,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{}, func buildConditions(engine *Engine, table *core.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil 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) var args = make([]interface{}, 0) for _, col := range table.Columns() { @@ -450,7 +450,11 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{}, var colName string 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 { 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 - 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)) } @@ -1123,9 +1127,7 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) var addedTableName = (len(statement.JoinStr) > 0) if !statement.noAutoCondition { - colNames, args := buildConditions(statement.Engine, table, bean, true, true, - false, true, statement.allUseBool, statement.useAllCols, - statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) + colNames, args := statement.buildConditions(table, bean, true, true, false, true, addedTableName) statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ") statement.BeanArgs = args @@ -1179,6 +1181,11 @@ func (s *Statement) genAddUniqueStr(uqeName string, cols []string) (string, []in 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{}) { table := statement.Engine.TableInfo(bean) statement.RefTable = table @@ -1186,9 +1193,7 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{} var addedTableName = (len(statement.JoinStr) > 0) if !statement.noAutoCondition { - colNames, args := buildConditions(statement.Engine, table, bean, true, true, false, - true, statement.allUseBool, statement.useAllCols, - statement.unscoped, statement.mustColumnMap, statement.TableName(), addedTableName) + colNames, args := statement.buildConditions(table, bean, true, true, false, true, addedTableName) statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ") statement.BeanArgs = args diff --git a/xorm.go b/xorm.go index 4a13edc6..3be9b661 100644 --- a/xorm.go +++ b/xorm.go @@ -17,7 +17,7 @@ import ( ) const ( - Version string = "0.4.5.0102" + Version string = "0.4.5.0112" ) func regDrvsNDialects() bool {