-
Notifications
You must be signed in to change notification settings - Fork 28
/
insert_test.go
96 lines (86 loc) · 3 KB
/
insert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package sqlz
import "testing"
func TestInsert(t *testing.T) {
runTests(t, func(dbz *DB) []test {
return []test{
{
"simple insert",
dbz.InsertInto("table").Columns("id", "name", "date").Values(1, "My Name", 96969696),
"INSERT INTO table (id, name, date) VALUES (?, ?, ?)",
[]interface{}{1, "My Name", 96969696},
},
{
"insert with value map",
dbz.InsertInto("table").ValueMap(map[string]interface{}{"id": 1, "name": "My Name"}),
"INSERT INTO table (id, name) VALUES (?, ?)",
[]interface{}{1, "My Name"},
},
{
"insert with returning clause",
dbz.InsertInto("table").Columns("one", "two").Values(1, 2).Returning("id"),
"INSERT INTO table (one, two) VALUES (?, ?) RETURNING id",
[]interface{}{1, 2},
},
{
"insert with on conflict do nothing clause",
dbz.InsertInto("table").Columns("one", "two").Values(1, 2).OnConflictDoNothing(),
"INSERT INTO table (one, two) VALUES (?, ?) ON CONFLICT DO NOTHING",
[]interface{}{1, 2},
},
{
"insert rows from a select query",
dbz.InsertInto("table").Columns("one", "two").FromSelect(
dbz.Select("*").From("table2"),
),
"INSERT INTO table (one, two) SELECT * FROM table2",
[]interface{}{},
},
{
"insert with on conflict do update",
dbz.InsertInto("table").Columns("name").Values("My Name").
OnConflict(
OnConflict("name", "something_else").
DoUpdate().
Set("update_date", 55151515).
SetMap(map[string]interface{}{
"name": "My Name Again",
"address": "Some Address",
})),
"INSERT INTO table (name) VALUES (?) ON CONFLICT (name, something_else) DO UPDATE SET update_date = ?, address = ?, name = ?",
[]interface{}{"My Name", 55151515, "Some Address", "My Name Again"},
},
{
"insert with on conflict do update conditional set",
dbz.InsertInto("table").Columns("name").Values("My Name").
OnConflict(
OnConflict("name").
DoUpdate().
Set("update_date", 55151515).
SetIf("true", 1, true).
SetIf("false", 0, false),
),
"INSERT INTO table (name) VALUES (?) ON CONFLICT (name) DO UPDATE SET update_date = ?, true = ?",
[]interface{}{"My Name", 55151515, 1},
},
{
"insert or ignore",
dbz.InsertInto("table").OrIgnore().Columns("id", "name", "date").Values(1, "My Name", 96969696),
"INSERT OR IGNORE INTO table (id, name, date) VALUES (?, ?, ?)",
[]interface{}{1, "My Name", 96969696},
},
{
"insert or replace",
dbz.InsertInto("table").OrReplace().Columns("id", "name", "date").Values(1, "My Name", 96969696),
"INSERT OR REPLACE INTO table (id, name, date) VALUES (?, ?, ?)",
[]interface{}{1, "My Name", 96969696},
},
{
"insert with multiple values",
dbz.InsertInto("table").Columns("id", "name").
ValueMultiple([][]interface{}{{1, "My Name"}, {2, "John"}, {3, "Golang"}}),
"INSERT INTO table (id, name) VALUES (?, ?), (?, ?), (?, ?)",
[]interface{}{1, "My Name", 2, "John", 3, "Golang"},
},
}
})
}