bug fixed for auto condition on join
This commit is contained in:
parent
439cc27466
commit
ff6f3b7a8d
|
@ -1178,9 +1178,11 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(condiBean) > 0 {
|
if len(condiBean) > 0 {
|
||||||
|
var addedTableName = (len(session.Statement.JoinStr) > 0)
|
||||||
colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true,
|
colNames, args := buildConditions(session.Engine, table, condiBean[0], true, true,
|
||||||
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
||||||
session.Statement.unscoped, session.Statement.mustColumnMap)
|
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 {
|
||||||
|
@ -3467,7 +3469,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
||||||
if len(condiBean) > 0 {
|
if len(condiBean) > 0 {
|
||||||
condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true,
|
condiColNames, condiArgs = buildConditions(session.Engine, session.Statement.RefTable, condiBean[0], true, true,
|
||||||
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
||||||
session.Statement.unscoped, session.Statement.mustColumnMap)
|
session.Statement.unscoped, session.Statement.mustColumnMap, session.Statement.TableName(), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
var condition = ""
|
var condition = ""
|
||||||
|
@ -3687,7 +3689,8 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
|
||||||
session.Statement.RefTable = table
|
session.Statement.RefTable = table
|
||||||
colNames, args := buildConditions(session.Engine, table, bean, true, true,
|
colNames, args := buildConditions(session.Engine, table, bean, true, true,
|
||||||
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
false, true, session.Statement.allUseBool, session.Statement.useAllCols,
|
||||||
session.Statement.unscoped, session.Statement.mustColumnMap)
|
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()
|
||||||
|
|
26
statement.go
26
statement.go
|
@ -498,8 +498,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) ([]string, []interface{}) {
|
mustColumnMap map[string]bool, tableName 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() {
|
||||||
|
@ -516,6 +515,14 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
|
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var colName string
|
||||||
|
if addedTableName {
|
||||||
|
colName = engine.Quote(tableName)+"."+engine.Quote(col.Name)
|
||||||
|
} else {
|
||||||
|
colName = engine.Quote(col.Name)
|
||||||
|
}
|
||||||
|
|
||||||
fieldValuePtr, err := col.ValueOf(bean)
|
fieldValuePtr, err := col.ValueOf(bean)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
engine.LogError(err)
|
engine.LogError(err)
|
||||||
|
@ -523,7 +530,8 @@ 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')", engine.Quote(col.Name), engine.Quote(col.Name)))
|
colNames = append(colNames, fmt.Sprintf("(%v IS NULL or %v = '0001-01-01 00:00:00')",
|
||||||
|
colName, colName))
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldValue := *fieldValuePtr
|
fieldValue := *fieldValuePtr
|
||||||
|
@ -545,7 +553,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
if fieldValue.IsNil() {
|
if fieldValue.IsNil() {
|
||||||
if includeNil {
|
if includeNil {
|
||||||
args = append(args, nil)
|
args = append(args, nil)
|
||||||
colNames = append(colNames, fmt.Sprintf("%v %s ?", engine.Quote(col.Name), engine.dialect.EqStr()))
|
colNames = append(colNames, fmt.Sprintf("%v %s ?", colName, engine.dialect.EqStr()))
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
} else if !fieldValue.IsValid() {
|
} else if !fieldValue.IsValid() {
|
||||||
|
@ -668,7 +676,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
if col.IsPrimaryKey && engine.dialect.DBType() == "ql" {
|
if col.IsPrimaryKey && engine.dialect.DBType() == "ql" {
|
||||||
condi = "id() == ?"
|
condi = "id() == ?"
|
||||||
} else {
|
} else {
|
||||||
condi = fmt.Sprintf("%v %s ?", engine.Quote(col.Name), engine.dialect.EqStr())
|
condi = fmt.Sprintf("%v %s ?", colName, engine.dialect.EqStr())
|
||||||
}
|
}
|
||||||
colNames = append(colNames, condi)
|
colNames = append(colNames, condi)
|
||||||
}
|
}
|
||||||
|
@ -1146,9 +1154,11 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
|
||||||
table = statement.RefTable
|
table = statement.RefTable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var addedTableName = (len(statement.JoinStr) > 0)
|
||||||
|
|
||||||
colNames, args := buildConditions(statement.Engine, table, bean, true, true,
|
colNames, args := buildConditions(statement.Engine, table, bean, true, true,
|
||||||
false, true, statement.allUseBool, statement.useAllCols,
|
false, true, statement.allUseBool, statement.useAllCols,
|
||||||
statement.unscoped, statement.mustColumnMap)
|
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
|
||||||
|
@ -1205,9 +1215,11 @@ 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)
|
||||||
|
|
||||||
colNames, args := buildConditions(statement.Engine, table, bean, true, true, false,
|
colNames, args := buildConditions(statement.Engine, table, bean, true, true, false,
|
||||||
true, statement.allUseBool, statement.useAllCols,
|
true, statement.allUseBool, statement.useAllCols,
|
||||||
statement.unscoped, statement.mustColumnMap)
|
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