-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
143 lines (118 loc) · 2.51 KB
/
main.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
package main
import (
"database/sql/driver"
"fmt"
"strings"
"github.com/nullism/bqb"
)
func basic() {
println("===[ Example: Basic ]===")
q := bqb.New("SELECT * FROM places WHERE id = ?", 1234)
q.Print()
}
func builder() {
println("===[ Example: Builder ]===")
// Changing these values changes the output of the query
getId := true
getName := true
lim := 10
filterName := true
filterNameNotNull := true
filterAge := true
// Optional queries will return nothing unless they have at least one query part.
sel := bqb.Optional("SELECT")
if getId {
sel.Comma("id")
}
if getName {
sel.Comma("name")
}
if sel.Len() > 0 {
sel.Space("FROM my_table")
}
where := bqb.Optional("WHERE")
if filterName {
nameQ := bqb.New("name = ?", "name")
if filterNameNotNull {
nameQ.And("name IS NOT NULL")
}
where.And("(?)", nameQ)
}
if filterAge {
where.And("age > ?", 21)
}
limit := bqb.Optional("LIMIT")
if lim > 0 {
limit.Space("?", lim)
}
query := bqb.New("? ? ?", sel, where, limit)
query.Print()
}
func customTypes() {
println("===[ Custom Types ]===")
type myStruct struct {
val string
}
q := bqb.New(
"DELETE FROM my_table WHERE a = ?",
&myStruct{val: "hello"},
)
q.Print()
}
func json() {
println("===[ JSON ]===")
q := bqb.New(
"INSERT INTO my_table (json_map, json_list) VALUES (?, ?)",
&bqb.JsonMap{"a": 1, "b": []string{"b", "c", "d"}},
&bqb.JsonList{"a", 1, true, nil, &bqb.JsonMap{"a": "b"}},
)
q.Print()
}
func nilQuery() {
println("===[ NIL Query ]===")
var q *bqb.Query
q.Print()
}
type valuerExample struct {
Name string
Age int
}
func (v valuerExample) Value() (driver.Value, error) {
return fmt.Sprintf("%v is %v years old", v.Name, v.Age), nil
}
func valuer() {
println("===[ driver.Valuer ]===")
example := &valuerExample{Name: "Bobby Tables", Age: 12}
q := bqb.New("info text = ?", example)
q.Print()
}
type embedderExample []string
func (e embedderExample) RawValue() string {
parts := []string{}
for _, v := range e {
part := fmt.Sprintf(`"%v"`, strings.ReplaceAll(v, `"`, `""`))
parts = append(parts, part)
}
return strings.Join(parts, ".")
}
func embedded() {
println("===[ Embedded ]===")
q := bqb.New("embedded: ?, unembedded: ?", bqb.Embedded("raw"), "bound")
q.Print()
}
func embedder() {
println("===[ Embedder ]===")
emb := embedderExample{"one", "two", `"three"`}
q := bqb.New("embedded: ?, unembedded: ?", emb, "bound")
q.Print()
}
func main() {
customTypes()
basic()
builder()
json()
nilQuery()
valuer()
embedded()
embedder()
}