-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
sql_builder.go
269 lines (228 loc) · 6.37 KB
/
sql_builder.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
package pop
import (
"fmt"
"regexp"
"strings"
"sync"
"github.com/gobuffalo/pop/v6/columns"
"github.com/gobuffalo/pop/v6/logging"
"github.com/jmoiron/sqlx"
)
type sqlBuilder struct {
Query Query
Model *Model
AddColumns []string
sql string
args []interface{}
isCompiled bool
err error
}
func newSQLBuilder(q Query, m *Model, addColumns ...string) *sqlBuilder {
return &sqlBuilder{
Query: q,
Model: m,
AddColumns: addColumns,
args: []interface{}{},
isCompiled: false,
}
}
var (
regexpMatchLimit = regexp.MustCompile(`(?i).*\s+limit\s+[0-9]*(\s?,\s?[0-9]*)?$`)
regexpMatchOffset = regexp.MustCompile(`(?i).*\s+offset\s+[0-9]*$`)
regexpMatchRowsOnly = regexp.MustCompile(`(?i).*\s+rows only`)
regexpMatchNames = regexp.MustCompile("(?i).*;+.*") // https://play.golang.org/p/FAmre5Sjin5
)
func hasLimitOrOffset(sqlString string) bool {
trimmedSQL := strings.TrimSpace(sqlString)
if regexpMatchLimit.MatchString(trimmedSQL) {
return true
}
if regexpMatchOffset.MatchString(trimmedSQL) {
return true
}
if regexpMatchRowsOnly.MatchString(trimmedSQL) {
return true
}
return false
}
func (sq *sqlBuilder) String() string {
if !sq.isCompiled {
sq.compile()
}
return sq.sql
}
func (sq *sqlBuilder) Args() []interface{} {
if !sq.isCompiled {
sq.compile()
}
return sq.args
}
var inRegex = regexp.MustCompile(`(?i)in\s*\(\s*\?\s*\)`)
func (sq *sqlBuilder) compile() {
if sq.sql == "" {
if sq.Query.RawSQL.Fragment != "" {
if sq.Query.Paginator != nil && !hasLimitOrOffset(sq.Query.RawSQL.Fragment) {
sq.sql = sq.buildPaginationClauses(sq.Query.RawSQL.Fragment)
} else {
if sq.Query.Paginator != nil {
log(logging.Warn, "Query already contains pagination")
}
sq.sql = sq.Query.RawSQL.Fragment
}
sq.args = sq.Query.RawSQL.Arguments
} else {
if sq.Model == nil {
sq.err = fmt.Errorf("sqlBuilder.compile() called but no RawSQL and Model specified")
return
}
switch sq.Query.Operation {
case Select:
sq.sql = sq.buildSelectSQL()
case Delete:
sq.sql = sq.buildDeleteSQL()
default:
panic("unexpected query operation " + sq.Query.Operation)
}
}
if inRegex.MatchString(sq.sql) {
s, args, err := sqlx.In(sq.sql, sq.Args()...)
if err == nil {
sq.sql = s
sq.args = args
}
}
sq.sql = sq.Query.Connection.Dialect.TranslateSQL(sq.sql)
}
}
func (sq *sqlBuilder) buildSelectSQL() string {
cols := sq.buildColumns()
fc := sq.buildfromClauses()
sql := fmt.Sprintf("SELECT %s FROM %s", cols.Readable().SelectString(), fc)
sql = sq.buildJoinClauses(sql)
sql = sq.buildWhereClauses(sql)
sql = sq.buildGroupClauses(sql)
sql = sq.buildOrderClauses(sql)
sql = sq.buildPaginationClauses(sql)
return sql
}
func (sq *sqlBuilder) buildDeleteSQL() string {
fc := sq.buildfromClauses()
sql := fmt.Sprintf("DELETE FROM %s", fc)
sql = sq.buildWhereClauses(sql)
// paginated delete supported by sqlite and mysql
// > If SQLite is compiled with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option [...] - from https://www.sqlite.org/lang_delete.html
//
// not generic enough IMO, therefore excluded
//
//switch sq.Query.Connection.Dialect.Name() {
//case nameMySQL, nameSQLite3:
// sql = sq.buildOrderClauses(sql)
// sql = sq.buildPaginationClauses(sql)
//}
return sql
}
func (sq *sqlBuilder) buildfromClauses() fromClauses {
models := []*Model{
sq.Model,
}
for _, mc := range sq.Query.belongsToThroughClauses {
models = append(models, mc.Through)
}
fc := sq.Query.fromClauses
for _, m := range models {
tableName := m.TableName()
asName := m.Alias()
fc = append(fc, fromClause{
From: tableName,
As: asName,
})
}
return fc
}
func (sq *sqlBuilder) buildWhereClauses(sql string) string {
mcs := sq.Query.belongsToThroughClauses
for _, mc := range mcs {
sq.Query.Where(fmt.Sprintf("%s.%s = ?", mc.Through.TableName(), mc.BelongsTo.associationName()), mc.BelongsTo.ID())
sq.Query.Where(fmt.Sprintf("%s.id = %s.%s", sq.Model.TableName(), mc.Through.TableName(), sq.Model.associationName()))
}
wc := sq.Query.whereClauses
if len(wc) > 0 {
sql = fmt.Sprintf("%s WHERE %s", sql, wc.Join(" AND "))
sq.args = append(sq.args, wc.Args()...)
}
return sql
}
func (sq *sqlBuilder) buildJoinClauses(sql string) string {
oc := sq.Query.joinClauses
if len(oc) > 0 {
sql += " " + oc.String()
for i := range oc {
sq.args = append(sq.args, oc[i].Arguments...)
}
}
return sql
}
func (sq *sqlBuilder) buildGroupClauses(sql string) string {
gc := sq.Query.groupClauses
if len(gc) > 0 {
sql = fmt.Sprintf("%s GROUP BY %s", sql, gc.String())
hc := sq.Query.havingClauses
if len(hc) > 0 {
sql = fmt.Sprintf("%s HAVING %s", sql, hc.String())
}
for i := range hc {
sq.args = append(sq.args, hc[i].Arguments...)
}
}
return sql
}
func (sq *sqlBuilder) buildOrderClauses(sql string) string {
oc := sq.Query.orderClauses
if len(oc) > 0 {
orderSQL := oc.Join(", ")
if regexpMatchNames.MatchString(orderSQL) {
warningMsg := fmt.Sprintf("Order clause(s) contains invalid characters: %s", orderSQL)
log(logging.Warn, warningMsg)
return sql
}
sql = fmt.Sprintf("%s ORDER BY %s", sql, orderSQL)
sq.args = append(sq.args, oc.Args()...)
}
return sql
}
func (sq *sqlBuilder) buildPaginationClauses(sql string) string {
if sq.Query.limitResults > 0 && sq.Query.Paginator == nil {
sql = fmt.Sprintf("%s LIMIT %d", sql, sq.Query.limitResults)
}
if sq.Query.Paginator != nil {
sql = fmt.Sprintf("%s LIMIT %d", sql, sq.Query.Paginator.PerPage)
sql = fmt.Sprintf("%s OFFSET %d", sql, sq.Query.Paginator.Offset)
}
return sql
}
// columnCache is used to prevent columns rebuilding.
var columnCache = map[string]columns.Columns{}
var columnCacheMutex = sync.RWMutex{}
func (sq *sqlBuilder) buildColumns() columns.Columns {
tableName := sq.Model.TableName()
asName := sq.Model.Alias()
acl := len(sq.AddColumns)
if acl == 0 {
columnCacheMutex.RLock()
cols, ok := columnCache[tableName]
columnCacheMutex.RUnlock()
// if alias is the same, don't remake columns
if ok && cols.TableAlias == asName {
return cols
}
cols = columns.ForStructWithAlias(sq.Model.Value, tableName, asName, sq.Model.IDField())
columnCacheMutex.Lock()
columnCache[tableName] = cols
columnCacheMutex.Unlock()
return cols
}
// acl > 0
cols := columns.NewColumns("", sq.Model.IDField())
cols.Add(sq.AddColumns...)
return cols
}