mirror of https://github.com/go-gorm/gorm.git
Refactor clauses
This commit is contained in:
parent
38d1cd2bf1
commit
6937d713c3
|
@ -25,10 +25,9 @@ type Builder interface {
|
|||
// Clause
|
||||
type Clause struct {
|
||||
Name string // WHERE
|
||||
Priority float64
|
||||
BeforeExpressions []Expression
|
||||
AfterNameExpressions []Expression
|
||||
AfterExpressions []Expression
|
||||
BeforeExpression Expression
|
||||
AfterNameExpression Expression
|
||||
AfterExpression Expression
|
||||
Expression Expression
|
||||
Builder ClauseBuilder
|
||||
}
|
||||
|
@ -37,29 +36,34 @@ type Clause struct {
|
|||
func (c Clause) Build(builder Builder) {
|
||||
if c.Builder != nil {
|
||||
c.Builder(c, builder)
|
||||
} else {
|
||||
builders := c.BeforeExpressions
|
||||
if c.Name != "" {
|
||||
builders = append(builders, Expr{SQL: c.Name})
|
||||
}
|
||||
|
||||
builders = append(builders, c.AfterNameExpressions...)
|
||||
if c.Expression != nil {
|
||||
builders = append(builders, c.Expression)
|
||||
}
|
||||
|
||||
for idx, expr := range append(builders, c.AfterExpressions...) {
|
||||
if idx != 0 {
|
||||
} else if c.Expression != nil {
|
||||
if c.BeforeExpression != nil {
|
||||
c.BeforeExpression.Build(builder)
|
||||
builder.WriteByte(' ')
|
||||
}
|
||||
expr.Build(builder)
|
||||
|
||||
if c.Name != "" {
|
||||
builder.WriteString(c.Name)
|
||||
builder.WriteByte(' ')
|
||||
}
|
||||
|
||||
if c.AfterNameExpression != nil {
|
||||
c.BeforeExpression.Build(builder)
|
||||
builder.WriteByte(' ')
|
||||
}
|
||||
|
||||
c.Expression.Build(builder)
|
||||
|
||||
if c.AfterExpression != nil {
|
||||
builder.WriteByte(' ')
|
||||
c.AfterExpression.Build(builder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
PrimaryKey string = "@@@priamry_key@@@"
|
||||
CurrentTable string = "@@@table@@@"
|
||||
PrimaryKey string = "@@@py@@@" // primary key
|
||||
CurrentTable string = "@@@ct@@@" // current table
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func TestFor(t *testing.T) {
|
||||
func TestLocking(t *testing.T) {
|
||||
results := []struct {
|
||||
Clauses []clause.Interface
|
||||
Result string
|
||||
|
|
|
@ -14,7 +14,7 @@ func (where Where) Name() string {
|
|||
func (where Where) Build(builder Builder) {
|
||||
// Switch position if the first query expression is a single Or condition
|
||||
for idx, expr := range where.Exprs {
|
||||
if v, ok := expr.(OrConditions); (!ok && expr != nil) || len(v.Exprs) > 1 {
|
||||
if v, ok := expr.(OrConditions); !ok || len(v.Exprs) > 1 {
|
||||
if idx != 0 {
|
||||
where.Exprs[0], where.Exprs[idx] = where.Exprs[idx], where.Exprs[0]
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ func (where Where) Build(builder Builder) {
|
|||
}
|
||||
|
||||
for idx, expr := range where.Exprs {
|
||||
if expr != nil {
|
||||
if idx > 0 {
|
||||
if v, ok := expr.(OrConditions); ok && len(v.Exprs) == 1 {
|
||||
builder.WriteString(" OR ")
|
||||
|
@ -34,7 +33,6 @@ func (where Where) Build(builder Builder) {
|
|||
|
||||
expr.Build(builder)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ func TestWhere(t *testing.T) {
|
|||
},
|
||||
{
|
||||
[]clause.Interface{clause.Select{}, clause.From{}, clause.Where{
|
||||
Exprs: []clause.Expression{clause.Or(), clause.Or(clause.Neq{Column: "name", Value: "jinzhu"}), clause.Eq{Column: clause.PrimaryColumn, Value: "1"}, clause.Gt{Column: "age", Value: 18}},
|
||||
Exprs: []clause.Expression{clause.Or(clause.Neq{Column: "name", Value: "jinzhu"}), clause.Eq{Column: clause.PrimaryColumn, Value: "1"}, clause.Gt{Column: "age", Value: 18}},
|
||||
}},
|
||||
"SELECT * FROM `users` WHERE `users`.`id` = ? OR `name` <> ? AND `age` > ?", []interface{}{"1", "jinzhu", 18},
|
||||
},
|
||||
|
|
|
@ -32,14 +32,15 @@ func (db *DB) Save(value interface{}) (tx *DB) {
|
|||
for idx, pf := range tx.Statement.Schema.PrimaryFields {
|
||||
if pv, isZero := pf.ValueOf(reflectValue); isZero {
|
||||
tx.callbacks.Create().Execute(tx)
|
||||
where.Exprs[idx] = clause.Eq{Column: pf.DBName, Value: pv}
|
||||
return
|
||||
}
|
||||
} else {
|
||||
where.Exprs[idx] = clause.Eq{Column: pf.DBName, Value: pv}
|
||||
}
|
||||
}
|
||||
|
||||
tx.Statement.AddClause(where)
|
||||
}
|
||||
}
|
||||
|
||||
if len(tx.Statement.Selects) == 0 {
|
||||
tx.Statement.Selects = append(tx.Statement.Selects, "*")
|
||||
|
|
|
@ -201,8 +201,7 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
|
|||
func (stmt *Statement) AddClause(v clause.Interface) {
|
||||
if optimizer, ok := v.(StatementModifier); ok {
|
||||
optimizer.ModifyStatement(stmt)
|
||||
}
|
||||
|
||||
} else {
|
||||
c, ok := stmt.Clauses[v.Name()]
|
||||
if !ok {
|
||||
c.Name = v.Name()
|
||||
|
@ -210,10 +209,11 @@ func (stmt *Statement) AddClause(v clause.Interface) {
|
|||
v.MergeClause(&c)
|
||||
stmt.Clauses[v.Name()] = c
|
||||
}
|
||||
}
|
||||
|
||||
// AddClauseIfNotExists add clause if not exists
|
||||
func (stmt *Statement) AddClauseIfNotExists(v clause.Interface) {
|
||||
if c, ok := stmt.Clauses[v.Name()]; !ok && c.Expression == nil {
|
||||
if c, ok := stmt.Clauses[v.Name()]; !ok || c.Expression == nil {
|
||||
stmt.AddClause(v)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue