Remove IF.

This commit is contained in:
Spencer Connaughton
2023-01-05 10:27:07 -05:00
parent 8880301f5b
commit d4eb709637
2 changed files with 2 additions and 21 deletions
-15
View File
@@ -17,7 +17,6 @@ const (
updateMarkerAfterWhere
updateMarkerAfterOrderBy
updateMarkerAfterLimit
updateMarkerAfterIf
)
// NewUpdateBuilder creates a new UPDATE builder.
@@ -47,7 +46,6 @@ type UpdateBuilder struct {
orderByCols []string
order string
limit int
ifExprs []string
args *Args
@@ -159,13 +157,6 @@ func (ub *UpdateBuilder) Limit(limit int) *UpdateBuilder {
return ub
}
// If sets IF expressions for UPDATE.
func (ub *UpdateBuilder) If(andExpr ...string) *UpdateBuilder {
ub.ifExprs = append(ub.ifExprs, andExpr...)
ub.marker = updateMarkerAfterIf
return ub
}
// String returns the compiled UPDATE string.
func (ub *UpdateBuilder) String() string {
s, _ := ub.Build()
@@ -216,12 +207,6 @@ func (ub *UpdateBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
ub.injection.WriteTo(buf, updateMarkerAfterLimit)
}
if len(ub.ifExprs) > 0 {
buf.WriteString(" IF ")
buf.WriteString(strings.Join(ub.ifExprs, " AND "))
ub.injection.WriteTo(buf, updateMarkerAfterIf)
}
return ub.args.CompileWithFlavor(buf.String(), flavor, initialArg...)
}
+2 -6
View File
@@ -44,18 +44,14 @@ func ExampleUpdateBuilder() {
"modified_at > created_at + "+ub.Var(86400), // It's allowed to write arbitrary SQL.
)
ub.OrderBy("id").Asc()
ub.If(
ub.E("type", "sys"),
ub.E("ready", true),
)
sql, args := ub.Build()
fmt.Println(sql)
fmt.Println(args)
// Output:
// UPDATE demo.user SET type = ?, credit = credit + 1, modified_at = UNIX_TIMESTAMP(NOW()) WHERE id > ? AND name LIKE ? AND (id_card IS NULL OR status IN (?, ?, ?)) AND modified_at > created_at + ? ORDER BY id ASC IF type = ? AND ready = ?
// [sys 1234 %Du 1 2 5 86400 sys true]
// UPDATE demo.user SET type = ?, credit = credit + 1, modified_at = UNIX_TIMESTAMP(NOW()) WHERE id > ? AND name LIKE ? AND (id_card IS NULL OR status IN (?, ?, ?)) AND modified_at > created_at + ? ORDER BY id ASC
// [sys 1234 %Du 1 2 5 86400]
}
func TestUpdateAssignments(t *testing.T) {