MCPcopy
hub / github.com/Masterminds/squirrel / TestUpdateBuilderToSql

Function TestUpdateBuilderToSql

update_test.go:9–40  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

7)
8
9func TestUpdateBuilderToSql(t *testing.T) {
10 b := Update("").
11 Prefix("WITH prefix AS ?", 0).
12 Table("a").
13 Set("b", Expr("? + 1", 1)).
14 SetMap(Eq{"c": 2}).
15 Set("c1", Case("status").When("1", "2").When("2", "1")).
16 Set("c2", Case().When("a = 2", Expr("?", "foo")).When("a = 3", Expr("?", "bar"))).
17 Set("c3", Select("a").From("b")).
18 Where("d = ?", 3).
19 OrderBy("e").
20 Limit(4).
21 Offset(5).
22 Suffix("RETURNING ?", 6)
23
24 sql, args, err := b.ToSql()
25 assert.NoError(t, err)
26
27 expectedSql :=
28 "WITH prefix AS ? " +
29 "UPDATE a SET b = ? + 1, c = ?, " +
30 "c1 = CASE status WHEN 1 THEN 2 WHEN 2 THEN 1 END, " +
31 "c2 = CASE WHEN a = 2 THEN ? WHEN a = 3 THEN ? END, " +
32 "c3 = (SELECT a FROM b) " +
33 "WHERE d = ? " +
34 "ORDER BY e LIMIT 4 OFFSET 5 " +
35 "RETURNING ?"
36 assert.Equal(t, expectedSql, sql)
37
38 expectedArgs := []interface{}{0, 1, 2, "foo", "bar", 3, 6}
39 assert.Equal(t, expectedArgs, args)
40}
41
42func TestUpdateBuilderToSqlErr(t *testing.T) {
43 _, _, err := Update("").Set("x", 1).ToSql()

Callers

nothing calls this directly

Calls 15

UpdateFunction · 0.85
ExprFunction · 0.85
CaseFunction · 0.85
SelectFunction · 0.85
SetMethod · 0.80
TableMethod · 0.80
WhenMethod · 0.80
ToSqlMethod · 0.65
SuffixMethod · 0.45
OffsetMethod · 0.45
LimitMethod · 0.45
OrderByMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…