Skip to content

Commit

Permalink
Fix upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasili Stefanenko committed Aug 21, 2018
1 parent e984ee9 commit 2554a65
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion builder_pgsql.go
Expand Up @@ -82,7 +82,7 @@ func (b *PgsqlBuilder) Upsert(table string, cols Params, constraints ...string)
q.sql += " ON CONFLICT DO UPDATE SET " + strings.Join(lines, ", ")
}

return q
return b.NewQuery(q.sql).Bind(q.params)
}

// DropIndex creates a Query that can be used to remove the named index from a table.
Expand Down
11 changes: 6 additions & 5 deletions builder_pgsql_test.go
Expand Up @@ -16,11 +16,12 @@ func TestPgsqlBuilder_Upsert(t *testing.T) {
"name": "James",
"age": 30,
}, "id")
assert.Equal(t, q.SQL(), `INSERT INTO "users" ("age", "name") VALUES ({:p0}, {:p1}) ON CONFLICT ("id") DO UPDATE SET "age"={:p2}, "name"={:p3}`, "t1")
assert.Equal(t, q.Params()["p0"], 30, "t2")
assert.Equal(t, q.Params()["p1"], "James", "t3")
assert.Equal(t, q.Params()["p2"], 30, "t2")
assert.Equal(t, q.Params()["p3"], "James", "t3")
assert.Equal(t, q.sql, `INSERT INTO "users" ("age", "name") VALUES ({:p0}, {:p1}) ON CONFLICT ("id") DO UPDATE SET "age"={:p2}, "name"={:p3}`, "t1")
assert.Equal(t, q.rawSQL, `INSERT INTO "users" ("age", "name") VALUES ($1, $2) ON CONFLICT ("id") DO UPDATE SET "age"=$3, "name"=$4`, "t2")
assert.Equal(t, q.Params()["p0"], 30, "t3")
assert.Equal(t, q.Params()["p1"], "James", "t4")
assert.Equal(t, q.Params()["p2"], 30, "t5")
assert.Equal(t, q.Params()["p3"], "James", "t6")
}
func TestPgsqlBuilder_DropIndex(t *testing.T) {
b := getPgsqlBuilder()
Expand Down

0 comments on commit 2554a65

Please sign in to comment.