Fix join problem (#2291)
Fix #2284 Reviewed-on: https://gitea.com/xorm/xorm/pulls/2291
This commit is contained in:
parent
486c344ba3
commit
79a8bc804b
|
@ -12,6 +12,7 @@ import (
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
"xorm.io/xorm/internal/utils"
|
"xorm.io/xorm/internal/utils"
|
||||||
"xorm.io/xorm/names"
|
"xorm.io/xorm/names"
|
||||||
|
"xorm.io/xorm/schemas"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
@ -1196,3 +1197,43 @@ func TestUpdateFindDate(t *testing.T) {
|
||||||
assert.EqualValues(t, 1, len(tufs))
|
assert.EqualValues(t, 1, len(tufs))
|
||||||
assert.EqualValues(t, tuf.Tm.Format("2006-01-02"), tufs[0].Tm.Format("2006-01-02"))
|
assert.EqualValues(t, tuf.Tm.Format("2006-01-02"), tufs[0].Tm.Format("2006-01-02"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuilderDialect(t *testing.T) {
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
|
||||||
|
type TestBuilderDialect struct {
|
||||||
|
Id int64
|
||||||
|
Name string `xorm:"index"`
|
||||||
|
Age2 int
|
||||||
|
}
|
||||||
|
|
||||||
|
type TestBuilderDialectFoo struct {
|
||||||
|
Id int64
|
||||||
|
DialectId int64 `xorm:"index"`
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
|
||||||
|
assertSync(t, new(TestBuilderDialect), new(TestBuilderDialectFoo))
|
||||||
|
|
||||||
|
session := testEngine.NewSession()
|
||||||
|
defer session.Close()
|
||||||
|
|
||||||
|
var dialect string
|
||||||
|
switch testEngine.Dialect().URI().DBType {
|
||||||
|
case schemas.MYSQL:
|
||||||
|
dialect = builder.MYSQL
|
||||||
|
case schemas.MSSQL:
|
||||||
|
dialect = builder.MSSQL
|
||||||
|
case schemas.POSTGRES:
|
||||||
|
dialect = builder.POSTGRES
|
||||||
|
case schemas.SQLITE:
|
||||||
|
dialect = builder.SQLITE
|
||||||
|
}
|
||||||
|
|
||||||
|
tbName := testEngine.TableName(new(TestBuilderDialectFoo), dialect == builder.POSTGRES)
|
||||||
|
|
||||||
|
inner := builder.Dialect(dialect).Select("*").From(tbName).Where(builder.Eq{"age": 20})
|
||||||
|
result := make([]*TestBuilderDialect, 0, 10)
|
||||||
|
err := testEngine.Table("test_builder_dialect").Where(builder.Eq{"age2": 2}).Join("INNER", inner, "test_builder_dialect_foo.dialect_id = test_builder_dialect.id").Find(&result)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
|
@ -15,82 +15,97 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Join The joinOP should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
// Join The joinOP should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
||||||
func (statement *Statement) Join(joinOP string, tablename interface{}, condition interface{}, args ...interface{}) *Statement {
|
func (statement *Statement) Join(joinOP string, joinTable interface{}, condition interface{}, args ...interface{}) *Statement {
|
||||||
var buf strings.Builder
|
statement.joins = append(statement.joins, join{
|
||||||
if len(statement.JoinStr) > 0 {
|
op: joinOP,
|
||||||
fmt.Fprintf(&buf, "%v %v JOIN ", statement.JoinStr, joinOP)
|
table: joinTable,
|
||||||
} else {
|
condition: condition,
|
||||||
fmt.Fprintf(&buf, "%v JOIN ", joinOP)
|
args: args,
|
||||||
}
|
})
|
||||||
|
|
||||||
condStr := ""
|
|
||||||
condArgs := []interface{}{}
|
|
||||||
switch condTp := condition.(type) {
|
|
||||||
case string:
|
|
||||||
condStr = condTp
|
|
||||||
case builder.Cond:
|
|
||||||
var err error
|
|
||||||
condStr, condArgs, err = builder.ToSQL(condTp)
|
|
||||||
if err != nil {
|
|
||||||
statement.LastError = err
|
|
||||||
return statement
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
statement.LastError = fmt.Errorf("unsupported join condition type: %v", condTp)
|
|
||||||
return statement
|
|
||||||
}
|
|
||||||
|
|
||||||
switch tp := tablename.(type) {
|
|
||||||
case builder.Builder:
|
|
||||||
subSQL, subQueryArgs, err := tp.ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
statement.LastError = err
|
|
||||||
return statement
|
|
||||||
}
|
|
||||||
|
|
||||||
fields := strings.Split(tp.TableName(), ".")
|
|
||||||
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
|
||||||
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
|
||||||
|
|
||||||
fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), statement.quote(aliasName), statement.ReplaceQuote(condStr))
|
|
||||||
statement.joinArgs = append(append(statement.joinArgs, subQueryArgs...), condArgs...)
|
|
||||||
case *builder.Builder:
|
|
||||||
subSQL, subQueryArgs, err := tp.ToSQL()
|
|
||||||
if err != nil {
|
|
||||||
statement.LastError = err
|
|
||||||
return statement
|
|
||||||
}
|
|
||||||
|
|
||||||
fields := strings.Split(tp.TableName(), ".")
|
|
||||||
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
|
||||||
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
|
||||||
|
|
||||||
fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), statement.quote(aliasName), statement.ReplaceQuote(condStr))
|
|
||||||
statement.joinArgs = append(append(statement.joinArgs, subQueryArgs...), condArgs...)
|
|
||||||
default:
|
|
||||||
tbName := dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), tablename, true)
|
|
||||||
if !utils.IsSubQuery(tbName) {
|
|
||||||
var buf strings.Builder
|
|
||||||
_ = statement.dialect.Quoter().QuoteTo(&buf, tbName)
|
|
||||||
tbName = buf.String()
|
|
||||||
} else {
|
|
||||||
tbName = statement.ReplaceQuote(tbName)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(&buf, "%s ON %v", tbName, statement.ReplaceQuote(condStr))
|
|
||||||
statement.joinArgs = append(statement.joinArgs, condArgs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
statement.JoinStr = buf.String()
|
|
||||||
statement.joinArgs = append(statement.joinArgs, args...)
|
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) writeJoin(w builder.Writer) error {
|
func (statement *Statement) writeJoins(w *builder.BytesWriter) error {
|
||||||
if statement.JoinStr != "" {
|
for _, join := range statement.joins {
|
||||||
if _, err := fmt.Fprint(w, " ", statement.JoinStr); err != nil {
|
if err := statement.writeJoin(w, join); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
w.Append(statement.joinArgs...)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) writeJoin(buf *builder.BytesWriter, join join) error {
|
||||||
|
// write join operator
|
||||||
|
if _, err := fmt.Fprintf(buf, " %v JOIN", join.op); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// write join table or subquery
|
||||||
|
switch tp := join.table.(type) {
|
||||||
|
case builder.Builder:
|
||||||
|
if _, err := fmt.Fprintf(buf, " ("); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := tp.WriteTo(statement.QuoteReplacer(buf)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := strings.Split(tp.TableName(), ".")
|
||||||
|
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
||||||
|
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(buf, ") %s", statement.quote(aliasName)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case *builder.Builder:
|
||||||
|
if _, err := fmt.Fprintf(buf, " ("); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := tp.WriteTo(statement.QuoteReplacer(buf)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := strings.Split(tp.TableName(), ".")
|
||||||
|
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
||||||
|
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(buf, ") %s", statement.quote(aliasName)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
tbName := dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), join.table, true)
|
||||||
|
if !utils.IsSubQuery(tbName) {
|
||||||
|
var sb strings.Builder
|
||||||
|
if err := statement.dialect.Quoter().QuoteTo(&sb, tbName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tbName = sb.String()
|
||||||
|
} else {
|
||||||
|
tbName = statement.ReplaceQuote(tbName)
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprint(buf, " ", tbName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// write on condition
|
||||||
|
if _, err := fmt.Fprint(buf, " ON "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch condTp := join.condition.(type) {
|
||||||
|
case string:
|
||||||
|
if _, err := fmt.Fprint(buf, statement.ReplaceQuote(condTp)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case builder.Cond:
|
||||||
|
if err := condTp.WriteTo(statement.QuoteReplacer(buf)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported join condition type: %v", condTp)
|
||||||
|
}
|
||||||
|
buf.Append(join.args...)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ func (statement *Statement) GenQuerySQL(sqlOrArgs ...interface{}) (string, []int
|
||||||
if len(statement.SelectStr) > 0 {
|
if len(statement.SelectStr) > 0 {
|
||||||
columnStr = statement.SelectStr
|
columnStr = statement.SelectStr
|
||||||
} else {
|
} else {
|
||||||
if statement.JoinStr == "" {
|
if len(statement.joins) == 0 {
|
||||||
if columnStr == "" {
|
if columnStr == "" {
|
||||||
if statement.GroupByStr != "" {
|
if statement.GroupByStr != "" {
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
||||||
|
@ -108,7 +108,7 @@ func (statement *Statement) GenGetSQL(bean interface{}) (string, []interface{},
|
||||||
columnStr = statement.SelectStr
|
columnStr = statement.SelectStr
|
||||||
} else {
|
} else {
|
||||||
// TODO: always generate column names, not use * even if join
|
// TODO: always generate column names, not use * even if join
|
||||||
if len(statement.JoinStr) == 0 {
|
if len(statement.joins) == 0 {
|
||||||
if len(columnStr) == 0 {
|
if len(columnStr) == 0 {
|
||||||
if len(statement.GroupByStr) > 0 {
|
if len(statement.GroupByStr) > 0 {
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
||||||
|
@ -188,7 +188,7 @@ func (statement *Statement) GenCountSQL(beans ...interface{}) (string, []interfa
|
||||||
return sqlStr, condArgs, nil
|
return sqlStr, condArgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) writeFrom(w builder.Writer) error {
|
func (statement *Statement) writeFrom(w *builder.BytesWriter) error {
|
||||||
if _, err := fmt.Fprint(w, " FROM "); err != nil {
|
if _, err := fmt.Fprint(w, " FROM "); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -198,7 +198,7 @@ func (statement *Statement) writeFrom(w builder.Writer) error {
|
||||||
if err := statement.writeAlias(w); err != nil {
|
if err := statement.writeAlias(w); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return statement.writeJoin(w)
|
return statement.writeJoins(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) writeLimitOffset(w builder.Writer) error {
|
func (statement *Statement) writeLimitOffset(w builder.Writer) error {
|
||||||
|
@ -263,7 +263,7 @@ func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderB
|
||||||
} else {
|
} else {
|
||||||
column = statement.RefTable.PKColumns()[0].Name
|
column = statement.RefTable.PKColumns()[0].Name
|
||||||
}
|
}
|
||||||
if statement.needTableName() {
|
if statement.NeedTableName() {
|
||||||
if len(statement.TableAlias) > 0 {
|
if len(statement.TableAlias) > 0 {
|
||||||
column = fmt.Sprintf("%s.%s", statement.TableAlias, column)
|
column = fmt.Sprintf("%s.%s", statement.TableAlias, column)
|
||||||
} else {
|
} else {
|
||||||
|
@ -291,7 +291,7 @@ func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderB
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := statement.WriteGroupBy(mssqlCondi); err != nil {
|
if err := statement.writeGroupBy(mssqlCondi); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if _, err := fmt.Fprint(mssqlCondi, "))"); err != nil {
|
if _, err := fmt.Fprint(mssqlCondi, "))"); err != nil {
|
||||||
|
@ -331,7 +331,7 @@ func (statement *Statement) genSelectSQL(columnStr string, needLimit, needOrderB
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := statement.WriteGroupBy(buf); err != nil {
|
if err := statement.writeGroupBy(buf); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := statement.writeHaving(buf); err != nil {
|
if err := statement.writeHaving(buf); err != nil {
|
||||||
|
@ -402,7 +402,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT TOP 1 * FROM %s", tableName); err != nil {
|
if _, err := fmt.Fprintf(buf, "SELECT TOP 1 * FROM %s", tableName); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := statement.writeJoin(buf); err != nil {
|
if err := statement.writeJoins(buf); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if statement.Conds().IsValid() {
|
if statement.Conds().IsValid() {
|
||||||
|
@ -417,7 +417,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT * FROM %s", tableName); err != nil {
|
if _, err := fmt.Fprintf(buf, "SELECT * FROM %s", tableName); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := statement.writeJoin(buf); err != nil {
|
if err := statement.writeJoins(buf); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if _, err := fmt.Fprintf(buf, " WHERE "); err != nil {
|
if _, err := fmt.Fprintf(buf, " WHERE "); err != nil {
|
||||||
|
@ -438,7 +438,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT 1 FROM %s", tableName); err != nil {
|
if _, err := fmt.Fprintf(buf, "SELECT 1 FROM %s", tableName); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := statement.writeJoin(buf); err != nil {
|
if err := statement.writeJoins(buf); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if statement.Conds().IsValid() {
|
if statement.Conds().IsValid() {
|
||||||
|
@ -471,7 +471,7 @@ func (statement *Statement) GenFindSQL(autoCond builder.Cond) (string, []interfa
|
||||||
if len(statement.SelectStr) > 0 {
|
if len(statement.SelectStr) > 0 {
|
||||||
columnStr = statement.SelectStr
|
columnStr = statement.SelectStr
|
||||||
} else {
|
} else {
|
||||||
if statement.JoinStr == "" {
|
if len(statement.joins) == 0 {
|
||||||
if columnStr == "" {
|
if columnStr == "" {
|
||||||
if statement.GroupByStr != "" {
|
if statement.GroupByStr != "" {
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
||||||
|
|
|
@ -102,7 +102,7 @@ func (statement *Statement) genColumnStr() string {
|
||||||
buf.WriteString(", ")
|
buf.WriteString(", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
if statement.JoinStr != "" {
|
if len(statement.joins) > 0 {
|
||||||
if statement.TableAlias != "" {
|
if statement.TableAlias != "" {
|
||||||
buf.WriteString(statement.TableAlias)
|
buf.WriteString(statement.TableAlias)
|
||||||
} else {
|
} else {
|
||||||
|
@ -119,7 +119,7 @@ func (statement *Statement) genColumnStr() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) colName(col *schemas.Column, tableName string) string {
|
func (statement *Statement) colName(col *schemas.Column, tableName string) string {
|
||||||
if statement.needTableName() {
|
if statement.NeedTableName() {
|
||||||
nm := tableName
|
nm := tableName
|
||||||
if len(statement.TableAlias) > 0 {
|
if len(statement.TableAlias) > 0 {
|
||||||
nm = statement.TableAlias
|
nm = statement.TableAlias
|
||||||
|
|
|
@ -34,6 +34,13 @@ var (
|
||||||
ErrTableNotFound = errors.New("Table not found")
|
ErrTableNotFound = errors.New("Table not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type join struct {
|
||||||
|
op string
|
||||||
|
table interface{}
|
||||||
|
condition interface{}
|
||||||
|
args []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
// Statement save all the sql info for executing SQL
|
// Statement save all the sql info for executing SQL
|
||||||
type Statement struct {
|
type Statement struct {
|
||||||
RefTable *schemas.Table
|
RefTable *schemas.Table
|
||||||
|
@ -45,8 +52,7 @@ type Statement struct {
|
||||||
idParam schemas.PK
|
idParam schemas.PK
|
||||||
orderStr string
|
orderStr string
|
||||||
orderArgs []interface{}
|
orderArgs []interface{}
|
||||||
JoinStr string
|
joins []join
|
||||||
joinArgs []interface{}
|
|
||||||
GroupByStr string
|
GroupByStr string
|
||||||
HavingStr string
|
HavingStr string
|
||||||
SelectStr string
|
SelectStr string
|
||||||
|
@ -123,8 +129,7 @@ func (statement *Statement) Reset() {
|
||||||
statement.LimitN = nil
|
statement.LimitN = nil
|
||||||
statement.ResetOrderBy()
|
statement.ResetOrderBy()
|
||||||
statement.UseCascade = true
|
statement.UseCascade = true
|
||||||
statement.JoinStr = ""
|
statement.joins = nil
|
||||||
statement.joinArgs = make([]interface{}, 0)
|
|
||||||
statement.GroupByStr = ""
|
statement.GroupByStr = ""
|
||||||
statement.HavingStr = ""
|
statement.HavingStr = ""
|
||||||
statement.ColumnMap = columnMap{}
|
statement.ColumnMap = columnMap{}
|
||||||
|
@ -205,8 +210,8 @@ func (statement *Statement) SetRefBean(bean interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) needTableName() bool {
|
func (statement *Statement) NeedTableName() bool {
|
||||||
return len(statement.JoinStr) > 0
|
return len(statement.joins) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Incr Generate "Update ... Set column = column + arg" statement
|
// Incr Generate "Update ... Set column = column + arg" statement
|
||||||
|
@ -290,7 +295,7 @@ func (statement *Statement) GroupBy(keys string) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) WriteGroupBy(w builder.Writer) error {
|
func (statement *Statement) writeGroupBy(w builder.Writer) error {
|
||||||
if statement.GroupByStr == "" {
|
if statement.GroupByStr == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -605,7 +610,7 @@ func (statement *Statement) BuildConds(table *schemas.Table, bean interface{}, i
|
||||||
// MergeConds merge conditions from bean and id
|
// MergeConds merge conditions from bean and id
|
||||||
func (statement *Statement) MergeConds(bean interface{}) error {
|
func (statement *Statement) MergeConds(bean interface{}) error {
|
||||||
if !statement.NoAutoCondition && statement.RefTable != nil {
|
if !statement.NoAutoCondition && statement.RefTable != nil {
|
||||||
addedTableName := (len(statement.JoinStr) > 0)
|
addedTableName := (len(statement.joins) > 0)
|
||||||
autoCond, err := statement.BuildConds(statement.RefTable, bean, true, true, false, true, addedTableName)
|
autoCond, err := statement.BuildConds(statement.RefTable, bean, true, true, false, true, addedTableName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -673,7 +678,7 @@ func (statement *Statement) joinColumns(cols []*schemas.Column, includeTableName
|
||||||
// CondDeleted returns the conditions whether a record is soft deleted.
|
// CondDeleted returns the conditions whether a record is soft deleted.
|
||||||
func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
|
func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
|
||||||
colName := statement.quote(col.Name)
|
colName := statement.quote(col.Name)
|
||||||
if statement.JoinStr != "" {
|
if len(statement.joins) > 0 {
|
||||||
var prefix string
|
var prefix string
|
||||||
if statement.TableAlias != "" {
|
if statement.TableAlias != "" {
|
||||||
prefix = statement.TableAlias
|
prefix = statement.TableAlias
|
||||||
|
|
10
rows.go
10
rows.go
|
@ -46,8 +46,8 @@ func newRows(session *Session, bean interface{}) (*Rows, error) {
|
||||||
|
|
||||||
if rows.session.statement.RawSQL == "" {
|
if rows.session.statement.RawSQL == "" {
|
||||||
var autoCond builder.Cond
|
var autoCond builder.Cond
|
||||||
var addedTableName = (len(session.statement.JoinStr) > 0)
|
addedTableName := session.statement.NeedTableName()
|
||||||
var table = rows.session.statement.RefTable
|
table := rows.session.statement.RefTable
|
||||||
|
|
||||||
if !session.statement.NoAutoCondition {
|
if !session.statement.NoAutoCondition {
|
||||||
var err error
|
var err error
|
||||||
|
@ -103,12 +103,12 @@ func (rows *Rows) Scan(beans ...interface{}) error {
|
||||||
return rows.Err()
|
return rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
var bean = beans[0]
|
bean := beans[0]
|
||||||
var tp = reflect.TypeOf(bean)
|
tp := reflect.TypeOf(bean)
|
||||||
if tp.Kind() == reflect.Ptr {
|
if tp.Kind() == reflect.Ptr {
|
||||||
tp = tp.Elem()
|
tp = tp.Elem()
|
||||||
}
|
}
|
||||||
var beanKind = tp.Kind()
|
beanKind := tp.Kind()
|
||||||
|
|
||||||
if len(beans) == 1 {
|
if len(beans) == 1 {
|
||||||
if reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
|
if reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
|
||||||
|
|
|
@ -354,7 +354,7 @@ func (session *Session) DB() *core.DB {
|
||||||
|
|
||||||
func (session *Session) canCache() bool {
|
func (session *Session) canCache() bool {
|
||||||
if session.statement.RefTable == nil ||
|
if session.statement.RefTable == nil ||
|
||||||
session.statement.JoinStr != "" ||
|
session.statement.NeedTableName() ||
|
||||||
session.statement.RawSQL != "" ||
|
session.statement.RawSQL != "" ||
|
||||||
!session.statement.UseCache ||
|
!session.statement.UseCache ||
|
||||||
session.statement.IsForUpdate ||
|
session.statement.IsForUpdate ||
|
||||||
|
|
|
@ -114,7 +114,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
|
|
||||||
var (
|
var (
|
||||||
table = session.statement.RefTable
|
table = session.statement.RefTable
|
||||||
addedTableName = (len(session.statement.JoinStr) > 0)
|
addedTableName = session.statement.NeedTableName()
|
||||||
autoCond builder.Cond
|
autoCond builder.Cond
|
||||||
)
|
)
|
||||||
if tp == tpStruct {
|
if tp == tpStruct {
|
||||||
|
|
Loading…
Reference in New Issue