Skip to content

Commit

Permalink
Drop support for NOW() and IF.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpencerC committed Sep 25, 2022
1 parent 932325b commit 113058d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 32 deletions.
12 changes: 6 additions & 6 deletions insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func ExampleInsertBuilder() {
ib := NewInsertBuilder()
ib.InsertInto("demo.user")
ib.Cols("id", "name", "status", "created_at", "updated_at")
ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())"), Now())
ib.Values(2, "Charmy Liu", 1, 1234567890, Now())
ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())"), Raw("NOW()"))
ib.Values(2, "Charmy Liu", 1, 1234567890, Raw("NOW()"))

sql, args := ib.Build()
fmt.Println(sql)
Expand All @@ -69,8 +69,8 @@ func ExampleInsertBuilder_insertIgnore() {
ib := NewInsertBuilder()
ib.InsertIgnoreInto("demo.user")
ib.Cols("id", "name", "status", "created_at", "updated_at")
ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())"), Now())
ib.Values(2, "Charmy Liu", 1, 1234567890, Now())
ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())"), Raw("NOW()"))
ib.Values(2, "Charmy Liu", 1, 1234567890, Raw("NOW()"))

sql, args := ib.Build()
fmt.Println(sql)
Expand Down Expand Up @@ -117,8 +117,8 @@ func ExampleInsertBuilder_replaceInto() {
ib := NewInsertBuilder()
ib.ReplaceInto("demo.user")
ib.Cols("id", "name", "status", "created_at", "updated_at")
ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())"), Now())
ib.Values(2, "Charmy Liu", 1, 1234567890, Now())
ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())"), Raw("NOW()"))
ib.Values(2, "Charmy Liu", 1, 1234567890, Raw("NOW()"))

sql, args := ib.Build()
fmt.Println(sql)
Expand Down
5 changes: 0 additions & 5 deletions modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ func Raw(expr string) interface{} {
return rawArgs{expr}
}

// Now returns a raw value comprising the NOW() function.
func Now() interface{} {
return Raw("NOW()")
}

type listArgs struct {
args []interface{}
}
Expand Down
15 changes: 0 additions & 15 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
updateMarkerAfterWhere
updateMarkerAfterOrderBy
updateMarkerAfterLimit
updateMarkerAfterIf
)

// NewUpdateBuilder creates a new UPDATE builder.
Expand Down Expand Up @@ -47,7 +46,6 @@ type UpdateBuilder struct {
orderByCols []string
order string
limit int
ifExprs []string

args *Args

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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...)
}

Expand Down
8 changes: 2 additions & 6 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 113058d

Please sign in to comment.