-
Notifications
You must be signed in to change notification settings - Fork 14
/
example_test.go
331 lines (290 loc) · 8.61 KB
/
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package sqlf_test
import (
"context"
"database/sql"
"fmt"
"github.com/leporo/sqlf"
)
type dummyDB int
func (db *dummyDB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return nil, nil
}
func (db *dummyDB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
return nil, nil
}
func (db *dummyDB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
return nil
}
var db = new(dummyDB)
var ctx = context.Background()
func Example() {
var (
region string
product string
productUnits int
productSales float64
)
sqlf.SetDialect(sqlf.PostgreSQL)
err := sqlf.From("orders").
With("regional_sales",
sqlf.From("orders").
Select("region, SUM(amount) AS total_sales").
GroupBy("region")).
With("top_regions",
sqlf.From("regional_sales").
Select("region").
Where("total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)")).
// Map query fields to variables
Select("region").To(®ion).
Select("product").To(&product).
Select("SUM(quantity)").To(&productUnits).
Select("SUM(amount) AS product_sales").To(&productSales).
//
Where("region IN (SELECT region FROM top_regions)").
GroupBy("region, product").
OrderBy("product_sales DESC").
// Execute the query
QueryAndClose(ctx, db, func(row *sql.Rows) {
// Callback function is called for every returned row.
// Row values are scanned automatically to bound variables.
fmt.Printf("%s\t%s\t%d\t$%.2f\n", region, product, productUnits, productSales)
})
if err != nil {
panic(err)
}
}
func ExampleStmt_OrderBy() {
q := sqlf.Select("id").From("table").OrderBy("id", "name DESC")
fmt.Println(q.String())
// Output: SELECT id FROM table ORDER BY id, name DESC
}
func ExampleStmt_Limit() {
q := sqlf.Select("id").From("table").Limit(10)
fmt.Println(q.String())
// Output: SELECT id FROM table LIMIT ?
}
func ExampleStmt_Offset() {
q := sqlf.Select("id").From("table").Limit(10).Offset(10)
fmt.Println(q.String())
// Output: SELECT id FROM table LIMIT ? OFFSET ?
}
func ExampleStmt_Paginate() {
q := sqlf.Select("id").From("table").Paginate(5, 10)
fmt.Println(q.String(), q.Args())
q.Close()
q = sqlf.Select("id").From("table").Paginate(1, 10)
fmt.Println(q.String(), q.Args())
q.Close()
// Zero and negative values are replaced with 1
q = sqlf.Select("id").From("table").Paginate(-1, -1)
fmt.Println(q.String(), q.Args())
q.Close()
// Output:
// SELECT id FROM table LIMIT ? OFFSET ? [10 40]
// SELECT id FROM table LIMIT ? [10]
// SELECT id FROM table LIMIT ? [1]
}
func ExampleStmt_Update() {
q := sqlf.Update("table").Set("field1", "newvalue").Where("id = ?", 42)
fmt.Println(q.String(), q.Args())
q.Close()
// Output:
// UPDATE table SET field1=? WHERE id = ? [newvalue 42]
}
func ExampleStmt_SetExpr() {
q := sqlf.Update("table").SetExpr("field1", "field2 + 1").Where("id = ?", 42)
fmt.Println(q.String())
fmt.Println(q.Args())
q.Close()
// Output:
// UPDATE table SET field1=field2 + 1 WHERE id = ?
// [42]
}
func ExampleStmt_InsertInto() {
q := sqlf.InsertInto("table").
Set("field1", "newvalue").
SetExpr("field2", "field2 + 1")
fmt.Println(q.String())
fmt.Println(q.Args())
q.Close()
// Output:
// INSERT INTO table ( field1, field2 ) VALUES ( ?, field2 + 1 )
// [newvalue]
}
func ExampleStmt_DeleteFrom() {
q := sqlf.DeleteFrom("table").Where("id = ?", 42)
fmt.Println(q.String())
fmt.Println(q.Args())
q.Close()
// Output:
// DELETE FROM table WHERE id = ?
// [42]
}
func ExampleStmt_GroupBy() {
q := sqlf.From("incomes").
Select("source, sum(amount) as s").
Where("amount > ?", 42).
GroupBy("source")
fmt.Println(q.String())
fmt.Println(q.Args())
q.Close()
// Output:
// SELECT source, sum(amount) as s FROM incomes WHERE amount > ? GROUP BY source
// [42]
}
func ExampleStmt_Having() {
q := sqlf.From("incomes").
Select("source, sum(amount) as s").
Where("amount > ?", 42).
GroupBy("source").
Having("s > ?", 100)
fmt.Println(q.String())
fmt.Println(q.Args())
q.Close()
// Output:
// SELECT source, sum(amount) as s FROM incomes WHERE amount > ? GROUP BY source HAVING s > ?
// [42 100]
}
func ExampleStmt_Returning() {
var newId int
q := sqlf.InsertInto("table").
Set("field1", "newvalue").
Returning("id").To(&newId)
fmt.Println(q.String(), q.Args())
q.Close()
// Output:
// INSERT INTO table ( field1 ) VALUES ( ? ) RETURNING id [newvalue]
}
func ExamplePostgreSQL() {
q := sqlf.PostgreSQL.From("table").Select("field").Where("id = ?", 42)
fmt.Println(q.String())
q.Close()
// Output:
// SELECT field FROM table WHERE id = $1
}
func ExampleStmt_With() {
q := sqlf.From("orders").
With("regional_sales",
sqlf.From("orders").
Select("region, SUM(amount) AS total_sales").
GroupBy("region")).
With("top_regions",
sqlf.From("regional_sales").
Select("region").
Where("total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)")).
Select("region").
Select("product").
Select("SUM(quantity) AS product_units").
Select("SUM(amount) AS product_sales").
Where("region IN (SELECT region FROM top_regions)").
GroupBy("region, product")
fmt.Println(q.String())
q.Close()
// Output:
// WITH regional_sales AS (SELECT region, SUM(amount) AS total_sales FROM orders GROUP BY region), top_regions AS (SELECT region FROM regional_sales WHERE total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)) SELECT region, product, SUM(quantity) AS product_units, SUM(amount) AS product_sales FROM orders WHERE region IN (SELECT region FROM top_regions) GROUP BY region, product
}
func ExampleStmt_From() {
q := sqlf.Select("*").
From("").
SubQuery(
"(", ") counted_news",
sqlf.From("news").
Select("id, section, header, score").
Select("row_number() OVER (PARTITION BY section ORDER BY score DESC) AS rating_in_section").
OrderBy("section, rating_in_section")).
Where("rating_in_section <= 5")
fmt.Println(q.String())
q.Close()
// Output:
//SELECT * FROM (SELECT id, section, header, score, row_number() OVER (PARTITION BY section ORDER BY score DESC) AS rating_in_section FROM news ORDER BY section, rating_in_section) counted_news WHERE rating_in_section <= 5
}
func ExampleStmt_SubQuery() {
q := sqlf.From("orders o").
Select("date, region").
SubQuery("(", ") AS prev_order_date",
sqlf.From("orders po").
Select("date").
Where("region = o.region").
Where("id < o.id").
OrderBy("id DESC").
Clause("LIMIT 1")).
Where("date > CURRENT_DATE - interval '1 day'").
OrderBy("id DESC")
fmt.Println(q.String())
q.Close()
// Output:
// SELECT date, region, (SELECT date FROM orders po WHERE region = o.region AND id < o.id ORDER BY id DESC LIMIT 1) AS prev_order_date FROM orders o WHERE date > CURRENT_DATE - interval '1 day' ORDER BY id DESC
}
func ExampleStmt_Clause() {
q := sqlf.From("empsalary").
Select("sum(salary) OVER w").
Clause("WINDOW w AS (PARTITION BY depname ORDER BY salary DESC)")
fmt.Println(q.String())
q.Close()
// Output:
// SELECT sum(salary) OVER w FROM empsalary WINDOW w AS (PARTITION BY depname ORDER BY salary DESC)
}
func ExampleStmt_QueryRowAndClose() {
type Offer struct {
id int64
productId int64
price float64
isDeleted bool
}
var o Offer
err := sqlf.From("offers").
Select("id").To(&o.id).
Select("product_id").To(&o.productId).
Select("price").To(&o.price).
Select("is_deleted").To(&o.isDeleted).
Where("id = ?", 42).
QueryRowAndClose(ctx, db)
if err != nil {
panic(err)
}
}
func ExampleStmt_Bind() {
type Offer struct {
Id int64 `db:"id"`
ProductId int64 `db:"product_id"`
Price float64 `db:"price"`
IsDeleted bool `db:"is_deleted"`
}
var o Offer
err := sqlf.From("offers").
Bind(&o).
Where("id = ?", 42).
QueryRowAndClose(ctx, db)
if err != nil {
panic(err)
}
}
func ExampleStmt_In() {
q := sqlf.From("tasks").
Select("id, status").
Where("status").In("new", "pending", "wip")
fmt.Println(q.String())
fmt.Println(q.Args())
q.Close()
// Output:
// SELECT id, status FROM tasks WHERE status IN (?,?,?)
// [new pending wip]
}
func ExampleStmt_Union() {
q := sqlf.From("tasks").
Select("id, status").
Where("status = ?", "new").
Union(true, sqlf.From("tasks").
Select("id, status").
Where("status = ?", "pending")).
Union(true, sqlf.From("tasks").
Select("id, status").
Where("status = ?", "wip")).
OrderBy("id")
fmt.Println(q.String())
fmt.Println(q.Args())
q.Close()
// Output:
// SELECT id, status FROM tasks WHERE status = ? UNION ALL SELECT id, status FROM tasks WHERE status = ? UNION ALL SELECT id, status FROM tasks WHERE status = ? ORDER BY id
// [new pending wip]
}