Skip to content

Commit

Permalink
Write identifiers in update statements
Browse files Browse the repository at this point in the history
Updates were just writing table names as raw strings, which means they
failed with table names that conflicted with identifiers. This updates
the `UpdateBuilder` to write the table name as an identifier.

Also updates tests.
JustinTulloss committed Jul 6, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a1ea13a commit f679e19
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion update.go
Original file line number Diff line number Diff line change
@@ -165,7 +165,7 @@ func (b *UpdateBuilder) ToSQL() (string, []interface{}) {
var args []interface{}

buf.WriteString("UPDATE ")
buf.WriteString(b.table)
writeIdentifier(buf, b.table)
buf.WriteString(" SET ")

var placeholderStartPos int64 = 1
20 changes: 10 additions & 10 deletions update_test.go
Original file line number Diff line number Diff line change
@@ -23,44 +23,44 @@ func BenchmarkUpdateValueMapSql(b *testing.B) {
func TestUpdateAllToSql(t *testing.T) {
sql, args := Update("a").Set("b", 1).Set("c", 2).ToSQL()

assert.Equal(t, sql, quoteSQL("UPDATE a SET %s = $1, %s = $2", "b", "c"))
assert.Equal(t, sql, quoteSQL(`UPDATE "a" SET %s = $1, %s = $2`, "b", "c"))
assert.Equal(t, args, []interface{}{1, 2})
}

func TestUpdateSingleToSql(t *testing.T) {
sql, args := Update("a").Set("b", 1).Set("c", 2).Where("id = $1", 1).ToSQL()

assert.Equal(t, sql, quoteSQL("UPDATE a SET %s = $1, %s = $2 WHERE (id = $3)", "b", "c"))
assert.Equal(t, sql, quoteSQL(`UPDATE "a" SET %s = $1, %s = $2 WHERE (id = $3)`, "b", "c"))
assert.Equal(t, args, []interface{}{1, 2, 1})
}

func TestUpdateSetMapToSql(t *testing.T) {
sql, args := Update("a").SetMap(map[string]interface{}{"b": 1, "c": 2}).Where("id = $1", 1).ToSQL()

if sql == quoteSQL("UPDATE a SET %s = $1, %s = $2 WHERE (id = $3)", "b", "c") {
if sql == quoteSQL(`UPDATE "a" SET %s = $1, %s = $2 WHERE (id = $3)`, "b", "c") {
assert.Equal(t, args, []interface{}{1, 2, 1})
} else {
assert.Equal(t, sql, quoteSQL("UPDATE a SET %s = $1, %s = $2 WHERE (id = $3)", "c", "b"))
assert.Equal(t, sql, quoteSQL(`UPDATE "a" SET %s = $1, %s = $2 WHERE (id = $3)`, "c", "b"))
assert.Equal(t, args, []interface{}{2, 1, 1})
}
}

func TestUpdateSetExprToSql(t *testing.T) {
sql, args := Update("a").Set("foo", 1).Set("bar", Expr("COALESCE(bar, 0) + 1")).Where("id = $1", 9).ToSQL()

assert.Equal(t, sql, quoteSQL("UPDATE a SET %s = $1, %s = COALESCE(bar, 0) + 1 WHERE (id = $2)", "foo", "bar"))
assert.Equal(t, sql, quoteSQL(`UPDATE "a" SET %s = $1, %s = COALESCE(bar, 0) + 1 WHERE (id = $2)`, "foo", "bar"))
assert.Equal(t, args, []interface{}{1, 9})

sql, args = Update("a").Set("foo", 1).Set("bar", Expr("COALESCE(bar, 0) + $1", 2)).Where("id = $1", 9).ToSQL()

assert.Equal(t, sql, quoteSQL("UPDATE a SET %s = $1, %s = COALESCE(bar, 0) + $2 WHERE (id = $3)", "foo", "bar"))
assert.Equal(t, sql, quoteSQL(`UPDATE "a" SET %s = $1, %s = COALESCE(bar, 0) + $2 WHERE (id = $3)`, "foo", "bar"))
assert.Equal(t, args, []interface{}{1, 2, 9})
}

func TestUpdateTenStaringFromTwentyToSql(t *testing.T) {
sql, args := Update("a").Set("b", 1).Limit(10).Offset(20).ToSQL()

assert.Equal(t, sql, quoteSQL("UPDATE a SET %s = $1 LIMIT 10 OFFSET 20", "b"))
assert.Equal(t, sql, quoteSQL(`UPDATE "a" SET %s = $1 LIMIT 10 OFFSET 20`, "b"))
assert.Equal(t, args, []interface{}{1})
}

@@ -75,7 +75,7 @@ func TestUpdateWhitelist(t *testing.T) {
SetWhitelist(sr, "user_id", "other").
ToSQL()

assert.Equal(t, sql, quoteSQL("UPDATE a SET %s = $1, %s = $2", "user_id", "other"))
assert.Equal(t, sql, quoteSQL(`UPDATE "a" SET %s = $1, %s = $2`, "user_id", "other"))
checkSliceEqual(t, args, []interface{}{2, false})
}

@@ -85,13 +85,13 @@ func TestUpdateBlacklist(t *testing.T) {
SetBlacklist(sr, "something_id").
ToSQL()

assert.Equal(t, sql, quoteSQL("UPDATE a SET %s = $1, %s = $2", "user_id", "other"))
assert.Equal(t, sql, quoteSQL(`UPDATE "a" SET %s = $1, %s = $2`, "user_id", "other"))
checkSliceEqual(t, args, []interface{}{2, false})
}

func TestUpdateWhereExprSql(t *testing.T) {
expr := Expr("id=$1", 100)
sql, args := Update("a").Set("b", 10).Where(expr).ToSQL()
assert.Equal(t, sql, `UPDATE a SET "b" = $1 WHERE (id=$2)`)
assert.Equal(t, sql, `UPDATE "a" SET "b" = $1 WHERE (id=$2)`)
assert.Exactly(t, args, []interface{}{10, 100})
}
6 changes: 3 additions & 3 deletions upsert_test.go
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ func TestUpsertSQLWhere(t *testing.T) {
expected := `
WITH
upd AS (
UPDATE tab
UPDATE "tab"
SET "b" = $1, "c" = $2
WHERE (d=$3)
RETURNING "b","c"
@@ -39,7 +39,7 @@ func TestUpsertSQLReturning(t *testing.T) {
expected := `
WITH
upd AS (
UPDATE tab
UPDATE "tab"
SET "b" = $1, "c" = $2
WHERE (d=$3)
RETURNING "f","g"
@@ -72,7 +72,7 @@ func TestUpsertSQLRecord(t *testing.T) {
expected := `
WITH
upd AS (
UPDATE tab
UPDATE "tab"
SET "b" = $1, "c" = $2
WHERE (d=$3)
RETURNING "f","g"

0 comments on commit f679e19

Please sign in to comment.