Skip to content

Commit

Permalink
Merge pull request #32 from petersunbag/master
Browse files Browse the repository at this point in the history
Add SetAppend in UpdateBuilder
  • Loading branch information
huandu committed Aug 19, 2019
2 parents 679dffb + 362f568 commit 604a8e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions update.go
Expand Up @@ -47,6 +47,12 @@ func (ub *UpdateBuilder) Set(assignment ...string) *UpdateBuilder {
return ub
}

// SetMore appends the assignements in SET.
func (ub *UpdateBuilder) SetMore(assignment ...string) *UpdateBuilder {
ub.assignments = append(ub.assignments, assignment...)
return ub
}

// Where sets expressions of WHERE in UPDATE.
func (ub *UpdateBuilder) Where(andExpr ...string) *UpdateBuilder {
ub.whereExprs = append(ub.whereExprs, andExpr...)
Expand Down
18 changes: 18 additions & 0 deletions update_test.go
Expand Up @@ -57,3 +57,21 @@ func TestUpdateAssignments(t *testing.T) {
}
}
}

func TestUpdateBuilder_SetMore(t *testing.T) {
ub := NewUpdateBuilder()
ub.Update("demo.user")
ub.Set(
ub.Assign("type", "sys"),
ub.Incr("credit"),
)
ub.SetMore(
"modified_at = UNIX_TIMESTAMP(NOW())", // It's allowed to write arbitrary SQL.
)
sql, args := ub.Build()
actual := fmt.Sprintf("%v|%v", sql, args)

if expected := "UPDATE demo.user SET type = ?, credit = credit + 1, modified_at = UNIX_TIMESTAMP(NOW())|[sys]"; actual != expected {
t.Fatalf("invalid assignment result. [expected:%v] [actual:%v]", expected, actual)
}
}

0 comments on commit 604a8e6

Please sign in to comment.