This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples_test.go
330 lines (282 loc) · 6.64 KB
/
examples_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
package sqlr
import (
"context"
"database/sql"
"fmt"
"log"
"time"
)
var db *sql.DB
func ExampleSchema_Prepare() {
type UserRow struct {
ID int `sql:"primary key autoincrement"`
GivenName string
FamilyName string
}
// Define different schemas for different dialects and naming conventions
schemas := []*Schema{
NewSchema(
WithDialect(MSSQL),
WithNamingConvention(SameCase),
),
NewSchema(
WithDialect(MySQL),
WithNamingConvention(LowerCase),
),
NewSchema(
WithDialect(Postgres),
WithNamingConvention(SnakeCase),
),
}
// for each schema, print the SQL generated for each statement
for _, schema := range schemas {
stmt, err := schema.Prepare(UserRow{}, `insert into users({}) values({})`)
if err != nil {
log.Fatal(err)
}
fmt.Println(stmt)
}
// Output:
// insert into users([GivenName], [FamilyName]) values(?, ?)
// insert into users(`givenname`, `familyname`) values(?, ?)
// insert into users("given_name", "family_name") values($1, $2)
}
func ExampleWithIdentifier() {
// Take an example of a program that operates against an SQL Server
// database where a table is named "[User]", but the same table is
// named "users" in the Postgres schema.
mssql := NewSchema(
WithDialect(MSSQL),
WithNamingConvention(SameCase),
WithIdentifier("[User]", "users"),
WithIdentifier("UserId", "user_id"),
WithIdentifier("[Name]", "name"),
)
postgres := NewSchema(
WithDialect(Postgres),
WithNamingConvention(SnakeCase),
)
type User struct {
UserId int `sql:"primary key"`
Name string
}
// If a statement is prepared and executed for both
const query = "select {} from users where user_id = ?"
mssqlStmt, err := mssql.Prepare(User{}, query)
if err != nil {
log.Fatal(err)
}
fmt.Println(mssqlStmt)
postgresStmt, err := postgres.Prepare(User{}, query)
if err != nil {
log.Fatal(err)
}
fmt.Println(postgresStmt)
// Output:
// select [UserId], [Name] from [User] where UserId = ?
// select "user_id", "name" from users where user_id = $1
}
func ExampleSession_MakeQuery() {
var schema Schema
ctx := context.Background()
tx := beginTransaction() // get a DB transaction, assumes no errors
defer tx.Commit() // WARNING: no error handling here: example code only
type Row struct {
ID int64 `sql:"primary key"`
Name string
FavoriteColor string
}
// begin a request-scoped database session
sess := NewSession(ctx, tx, &schema)
// data access object
var dao struct {
Get func(id int64) (*Row, error)
Select func(query string, args ...interface{}) ([]*Row, error)
}
sess.MakeQuery(&dao.Get, &dao.Select)
// can now use the type-safe data access functions
row42, err := dao.Get(42)
if err != nil {
log.Fatal(err)
}
log.Println("Row 42:", row42)
redRows, err := dao.Select("select {} from rows where favorite_color = ?", "red")
if err != nil {
log.Fatal(err)
}
for _, row := range redRows {
log.Println("Likes red:", row)
}
}
func beginTransaction() *sql.Tx {
return nil
}
func newSession() *Session {
return nil
}
func ExamplNewSchema() {
type UserRow struct {
ID int64 `sql:"primary key"`
Name string
}
type PostRow struct {
ID int64 `sql:"primary key"`
UserID int64
CreatedAt time.Time
Title string
Content string
}
schema := NewSchema(
WithNamingConvention(SnakeCase),
WithTables(TablesConfig{
(*UserRow)(nil): {
TableName: "users_table", // override naming convention for table
},
(*PostRow)(nil): {
Columns: ColumnsConfig{
"CreatedAt": {
ColumnName: "create_timestamp", // override naming convention
EmptyNull: true, // store empty string as null
},
},
},
}),
)
doSomethingWith(schema)
}
func ExampleColumnConfig() {
type Address struct {
Street string
Locality string
City string
State string
Country string
}
type PersonRow struct {
ID int64 `sql:"primary key"`
Name string
DOB time.Time
Address Address
}
schema := NewSchema(
WithTables(TablesConfig{
(*PersonRow)(nil): {
Columns: ColumnsConfig{
"Address.Locality": {
ColumnName: "address_suburb",
},
"DOB": {
ColumnName: "date_of_birth",
EmptyNull: true,
},
},
},
}),
)
doSomethingWith(schema)
}
func ExampleColumnsConfig() {
type Address struct {
Street string
Locality string
City string
State string
Country string
}
type PersonRow struct {
ID int64 `sql:"primary key"`
Name string
DOB time.Time
Address Address
}
schema := NewSchema(
WithTables(TablesConfig{
(*PersonRow)(nil): {
Columns: ColumnsConfig{
"Address.Locality": {
ColumnName: "address_suburb",
},
"DOB": {
ColumnName: "date_of_birth",
EmptyNull: true,
},
},
},
}),
)
doSomethingWith(schema)
}
func doSomethingWith(v interface{}) {
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func ExampleSession_Row() {
var (
session *Session
)
// start a session
session = newSession()
// ExampleRow is an example of a row structure.
type ExampleRow struct {
ID int `sql:"primary key"`
Name string
Value int
}
var row = &ExampleRow{
ID: 1,
Name: "first row",
Value: 10,
}
// Insert a row
result, err := session.Row(row).Exec("insert into examples({}) values({})")
checkError(err)
count, err := result.RowsAffected()
checkError(err)
log.Printf("row inserted, count=%d", count)
// Delete the row
result, err = session.Row(row).Exec("delete from examples where {}")
checkError(err)
count, err = result.RowsAffected()
checkError(err)
log.Printf("row deleted, count=%d", count)
}
func ExampleSessionRow_Exec() {
var (
session *Session
)
// start a session
session = newSession()
// ExampleRow is an example of a row structure.
type ExampleRow struct {
ID int `sql:"primary key"`
Name string
Value int
}
var row = &ExampleRow{
ID: 1,
Name: "first row",
Value: 10,
}
// Performs an insert, bypassing any of the cleverness of the
// Insert() method.
//
// SQL looks something like:
// insert into the_table("id", "name", "value") values ($1, $2, $3)
// Arguments are
// [ 1, "first row", 10 ]
n, err := session.Row(row).Exec("insert into the_table({}) values({})")
checkError(err)
log.Printf("row inserted, count=%d", n)
// Perform an update with an additional test
//
// SQL looks like:
// update the_table set name = $1, value = $2 where id =$3 and value = $4
// Arguments are:
// [ "first row", 10, 1, 10 ]
n, err = session.Row(row).Exec("update the_table set {} where {} and value > ?", 10)
checkError(err)
log.Printf("row inserted, count=%d", n)
}