-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
62 lines (52 loc) · 1.22 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
// nolint (WIP)
package queries_test
import (
"context"
"database/sql"
"fmt"
"os"
"os/signal"
"strings"
"time"
"go-simpler.org/queries"
// <database driver of your choice>
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if err := run(ctx); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func run(ctx context.Context) error {
db, err := sql.Open("<driver name>", "<connection string>")
if err != nil {
return err
}
columns := []string{"first_name", "last_name"}
if true {
columns = append(columns, "created_at")
}
var qb queries.Builder
qb.Appendf("select %s from users", strings.Join(columns, ", "))
if true {
qb.Appendf(" where created_at >= %$", time.Date(2024, time.January, 1, 0, 0, 0, 0, time.Local))
}
// select first_name, last_name, created_at from users where created_at >= $1
rows, err := db.QueryContext(ctx, qb.Query(), qb.Args()...)
if err != nil {
return err
}
defer rows.Close()
var users []struct {
FirstName string `sql:"first_name"`
LastName string `sql:"last_name"`
CreatedAt time.Time `sql:"created_at"`
}
if err := queries.Scan(&users, rows); err != nil {
return err
}
fmt.Println(users)
return nil
}