forked from doug-martin/goqu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goqu_example_test.go
255 lines (204 loc) · 6.87 KB
/
goqu_example_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package goqu_test
import (
"fmt"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/mysql"
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
_ "github.com/doug-martin/goqu/v9/dialect/sqlite3"
)
// Creating a mysql dataset. Be sure to import the mysql adapter.
func ExampleDialect_datasetMysql() {
// import _ "github.com/doug-martin/goqu/v9/dialect/mysql"
d := goqu.Dialect("mysql")
ds := d.From("test").Where(goqu.Ex{
"foo": "bar",
"baz": []int64{1, 2, 3},
}).Limit(10)
sql, args, _ := ds.ToSQL()
fmt.Println(sql, args)
sql, args, _ = ds.Prepared(true).ToSQL()
fmt.Println(sql, args)
// Output:
// SELECT * FROM `test` WHERE ((`baz` IN (1, 2, 3)) AND (`foo` = 'bar')) LIMIT 10 []
// SELECT * FROM `test` WHERE ((`baz` IN (?, ?, ?)) AND (`foo` = ?)) LIMIT ? [1 2 3 bar 10]
}
// Creating a mysql database. Be sure to import the mysql adapter.
func ExampleDialect_dbMysql() {
// import _ "github.com/doug-martin/goqu/v9/dialect/mysql"
type item struct {
ID int64 `db:"id"`
Address string `db:"address"`
Name string `db:"name"`
}
// set up a mock db this would normally be
// db, err := sql.Open("mysql", dbURI)
// if err != nil {
// panic(err.Error())
// }
mDB, mock, _ := sqlmock.New()
d := goqu.Dialect("mysql")
db := d.DB(mDB)
// use the db.From to get a dataset to execute queries
ds := db.From("items").Where(goqu.C("id").Eq(1))
// set up mock for example purposes
mock.ExpectQuery("SELECT `address`, `id`, `name` FROM `items` WHERE \\(`id` = 1\\) LIMIT 1").
WillReturnRows(
sqlmock.NewRows([]string{"id", "address", "name"}).
FromCSVString("1, 111 Test Addr,Test1"),
)
var it item
found, err := ds.ScanStruct(&it)
fmt.Println(it, found, err)
// set up mock for example purposes
mock.ExpectQuery("SELECT `address`, `id`, `name` FROM `items` WHERE \\(`id` = \\?\\) LIMIT \\?").
WithArgs(1, 1).
WillReturnRows(
sqlmock.NewRows([]string{"id", "address", "name"}).
FromCSVString("1, 111 Test Addr,Test1"),
)
found, err = ds.Prepared(true).ScanStruct(&it)
fmt.Println(it, found, err)
// Output:
// {1 111 Test Addr Test1} true <nil>
// {1 111 Test Addr Test1} true <nil>
}
// Creating a mysql dataset. Be sure to import the postgres adapter
func ExampleDialect_datasetPostgres() {
// import _ "github.com/doug-martin/goqu/v9/dialect/postgres"
d := goqu.Dialect("postgres")
ds := d.From("test").Where(goqu.Ex{
"foo": "bar",
"baz": []int64{1, 2, 3},
}).Limit(10)
sql, args, _ := ds.ToSQL()
fmt.Println(sql, args)
sql, args, _ = ds.Prepared(true).ToSQL()
fmt.Println(sql, args)
// Output:
// SELECT * FROM "test" WHERE (("baz" IN (1, 2, 3)) AND ("foo" = 'bar')) LIMIT 10 []
// SELECT * FROM "test" WHERE (("baz" IN ($1, $2, $3)) AND ("foo" = $4)) LIMIT $5 [1 2 3 bar 10]
}
// Creating a postgres dataset. Be sure to import the postgres adapter
func ExampleDialect_dbPostgres() {
// import _ "github.com/doug-martin/goqu/v9/dialect/postgres"
type item struct {
ID int64 `db:"id"`
Address string `db:"address"`
Name string `db:"name"`
}
// set up a mock db this would normally be
// db, err := sql.Open("postgres", dbURI)
// if err != nil {
// panic(err.Error())
// }
mDB, mock, _ := sqlmock.New()
d := goqu.Dialect("postgres")
db := d.DB(mDB)
// use the db.From to get a dataset to execute queries
ds := db.From("items").Where(goqu.C("id").Eq(1))
// set up mock for example purposes
mock.ExpectQuery(`SELECT "address", "id", "name" FROM "items" WHERE \("id" = 1\) LIMIT 1`).
WillReturnRows(
sqlmock.NewRows([]string{"id", "address", "name"}).
FromCSVString("1, 111 Test Addr,Test1"),
)
var it item
found, err := ds.ScanStruct(&it)
fmt.Println(it, found, err)
// set up mock for example purposes
mock.ExpectQuery(`SELECT "address", "id", "name" FROM "items" WHERE \("id" = \$1\) LIMIT \$2`).
WithArgs(1, 1).
WillReturnRows(
sqlmock.NewRows([]string{"id", "address", "name"}).
FromCSVString("1, 111 Test Addr,Test1"),
)
found, err = ds.Prepared(true).ScanStruct(&it)
fmt.Println(it, found, err)
// Output:
// {1 111 Test Addr Test1} true <nil>
// {1 111 Test Addr Test1} true <nil>
}
// Creating a mysql dataset. Be sure to import the sqlite3 adapter
func ExampleDialect_datasetSqlite3() {
// import _ "github.com/doug-martin/goqu/v9/dialect/sqlite3"
d := goqu.Dialect("sqlite3")
ds := d.From("test").Where(goqu.Ex{
"foo": "bar",
"baz": []int64{1, 2, 3},
}).Limit(10)
sql, args, _ := ds.ToSQL()
fmt.Println(sql, args)
sql, args, _ = ds.Prepared(true).ToSQL()
fmt.Println(sql, args)
// Output:
// SELECT * FROM `test` WHERE ((`baz` IN (1, 2, 3)) AND (`foo` = 'bar')) LIMIT 10 []
// SELECT * FROM `test` WHERE ((`baz` IN (?, ?, ?)) AND (`foo` = ?)) LIMIT ? [1 2 3 bar 10]
}
// Creating a sqlite3 database. Be sure to import the sqlite3 adapter
func ExampleDialect_dbSqlite3() {
// import _ "github.com/doug-martin/goqu/v9/dialect/sqlite3"
type item struct {
ID int64 `db:"id"`
Address string `db:"address"`
Name string `db:"name"`
}
// set up a mock db this would normally be
// db, err := sql.Open("sqlite3", dbURI)
// if err != nil {
// panic(err.Error())
// }
mDB, mock, _ := sqlmock.New()
d := goqu.Dialect("sqlite3")
db := d.DB(mDB)
// use the db.From to get a dataset to execute queries
ds := db.From("items").Where(goqu.C("id").Eq(1))
// set up mock for example purposes
mock.ExpectQuery("SELECT `address`, `id`, `name` FROM `items` WHERE \\(`id` = 1\\) LIMIT 1").
WillReturnRows(
sqlmock.NewRows([]string{"id", "address", "name"}).
FromCSVString("1, 111 Test Addr,Test1"),
)
var it item
found, err := ds.ScanStruct(&it)
fmt.Println(it, found, err)
// set up mock for example purposes
mock.ExpectQuery("SELECT `address`, `id`, `name` FROM `items` WHERE \\(`id` = \\?\\) LIMIT \\?").
WithArgs(1, 1).
WillReturnRows(
sqlmock.NewRows([]string{"id", "address", "name"}).
FromCSVString("1, 111 Test Addr,Test1"),
)
found, err = ds.Prepared(true).ScanStruct(&it)
fmt.Println(it, found, err)
// Output:
// {1 111 Test Addr Test1} true <nil>
// {1 111 Test Addr Test1} true <nil>
}
func ExampleSetTimeLocation() {
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
created, err := time.Parse(time.RFC3339, "2019-10-01T15:01:00Z")
if err != nil {
panic(err)
}
// use original time with tz info
goqu.SetTimeLocation(loc)
ds := goqu.Insert("test").Rows(goqu.Record{
"address": "111 Address",
"name": "Bob Yukon",
"created": created,
})
sql, _, _ := ds.ToSQL()
fmt.Println(sql)
// convert time to UTC
goqu.SetTimeLocation(time.UTC)
sql, _, _ = ds.ToSQL()
fmt.Println(sql)
// Output:
// INSERT INTO "test" ("address", "created", "name") VALUES ('111 Address', '2019-10-01T23:01:00+08:00', 'Bob Yukon')
// INSERT INTO "test" ("address", "created", "name") VALUES ('111 Address', '2019-10-01T15:01:00Z', 'Bob Yukon')
}