Fix statement modifier support

This commit is contained in:
Jinzhu 2020-06-14 11:46:17 +08:00
parent 1bbaa43951
commit 56bdded0f8
3 changed files with 7 additions and 6 deletions

View File

@ -27,6 +27,8 @@ func (db *DB) Clauses(conds ...clause.Expression) (tx *DB) {
for _, cond := range conds { for _, cond := range conds {
if c, ok := cond.(clause.Interface); ok { if c, ok := cond.(clause.Interface); ok {
tx.Statement.AddClause(c) tx.Statement.AddClause(c)
} else if optimizer, ok := cond.(StatementModifier); ok {
optimizer.ModifyStatement(tx.Statement)
} else { } else {
whereConds = append(whereConds, cond) whereConds = append(whereConds, cond)
} }

View File

@ -48,7 +48,7 @@ func (c Clause) Build(builder Builder) {
} }
if c.AfterNameExpression != nil { if c.AfterNameExpression != nil {
c.BeforeExpression.Build(builder) c.AfterNameExpression.Build(builder)
builder.WriteByte(' ') builder.WriteByte(' ')
} }

View File

@ -202,12 +202,11 @@ func (stmt *Statement) AddClause(v clause.Interface) {
if optimizer, ok := v.(StatementModifier); ok { if optimizer, ok := v.(StatementModifier); ok {
optimizer.ModifyStatement(stmt) optimizer.ModifyStatement(stmt)
} else { } else {
c, ok := stmt.Clauses[v.Name()] name := v.Name()
if !ok { c, _ := stmt.Clauses[name]
c.Name = v.Name() c.Name = name
}
v.MergeClause(&c) v.MergeClause(&c)
stmt.Clauses[v.Name()] = c stmt.Clauses[name] = c
} }
} }