-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_driver.go
More file actions
165 lines (142 loc) · 3.33 KB
/
query_driver.go
File metadata and controls
165 lines (142 loc) · 3.33 KB
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
package database
import (
"math"
"strings"
)
type qItem struct {
Type string
Query string
Args []any
Closure bool
}
type qBuilder struct {
numeric bool
start int
queries []qItem
replacements []string
}
func (builder *qBuilder) addItem(q string, and, closure bool, args ...any) {
item := qItem{}
if and {
item.Type = "AND"
} else {
item.Type = "OR"
}
item.Query = q
item.Closure = closure
item.Args = args
builder.queries = append(builder.queries, item)
}
func (builder *qBuilder) And(query string, args ...any) QueryBuilder {
if query != "" {
builder.addItem(query, true, false, args...)
}
return builder
}
func (builder *qBuilder) AndIf(cond bool, query string, args ...any) QueryBuilder {
if cond && query != "" {
builder.addItem(query, true, false, args...)
}
return builder
}
func (builder *qBuilder) Or(query string, args ...any) QueryBuilder {
if query != "" {
builder.addItem(query, false, false, args...)
}
return builder
}
func (builder *qBuilder) OrIf(cond bool, query string, args ...any) QueryBuilder {
if cond && query != "" {
builder.addItem(query, false, false, args...)
}
return builder
}
func (builder *qBuilder) AndClosure(query string, args ...any) QueryBuilder {
if query != "" {
builder.addItem(query, true, true, args...)
}
return builder
}
func (builder *qBuilder) AndClosureIf(cond bool, query string, args ...any) QueryBuilder {
if cond && query != "" {
builder.addItem(query, true, true, args...)
}
return builder
}
func (builder *qBuilder) OrClosure(query string, args ...any) QueryBuilder {
if query != "" {
builder.addItem(query, false, true, args...)
}
return builder
}
func (builder *qBuilder) OrClosureIf(cond bool, query string, args ...any) QueryBuilder {
if cond && query != "" {
builder.addItem(query, false, true, args...)
}
return builder
}
func (builder *qBuilder) NumericArgs(numeric bool) QueryBuilder {
builder.numeric = numeric
return builder
}
func (builder *qBuilder) NumericStart(start int) QueryBuilder {
builder.start = start
return builder
}
func (builder *qBuilder) Replace(old, new string) QueryBuilder {
builder.replacements = append(builder.replacements, old, new)
return builder
}
func (builder *qBuilder) Raw() string {
command := ""
for _, q := range builder.queries {
query := q.Query
// generate @in
if strings.Contains(query, "@in") {
placeholders := strings.TrimLeft(
strings.Repeat(", ?", len(q.Args)),
", ",
)
query = strings.Replace(query, "@in", "IN ("+placeholders+")", 1)
}
// generate subquery
if q.Closure {
query = "(" + query + ")"
}
if command == "" {
command = query
} else {
command = command + " " + q.Type + " " + query
}
}
if builder.numeric {
command = numericArgs(command, int(math.Max(float64(builder.start), 1)))
}
return command
}
func (builder *qBuilder) SQL(query string) string {
if raw := builder.Raw(); raw == "" {
return strings.NewReplacer(
append(
builder.replacements,
"@query", raw,
"@where", "",
)...,
).Replace(query)
} else {
return strings.NewReplacer(
append(
builder.replacements,
"@query", raw,
"@where", "WHERE "+raw,
)...,
).Replace(query)
}
}
func (builder *qBuilder) Args() []any {
args := make([]any, 0)
for _, q := range builder.queries {
args = append(args, q.Args...)
}
return args
}