FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PopSQL/QueryGenerator.php at master · osvapp/PopSQL · GitHub
osvapp
/
PopSQL
Public
forked from
iFixit/PopSQL
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
PopSQL
/
QueryGenerator.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
445 lines (407 loc) · 13.6 KB
Breadcrumbs
PopSQL
/
QueryGenerator.php
Copy path
File metadata and controls
445 lines (407 loc) · 13.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/**
* A simple query generator for conditionally building MySQL statements.
*
* Build clauses by calling their corresponding member functions.
*
* Each method takes two arguments:
* 1. An array of strings that will later be combined to form the clause.
* 2. An array of paramters for use in prepared statements.
*
* Available clauses / methods are:
* select, insert, replace, update, delete, from, join, set, columns, values,
* where, group, having, order, limit, offset, duplicate, modify
*
* After all clauses have been built, call the 'build' member function to
* compose the entire query. This returns an array containing the query and
* an array parameters.
*
* Example usage:
* $qGen = new QueryGenerator();
* $qGen->select(['field1', 'field2']);
* $qGen->from(['table1']);
* $qGen->join(['INNER JOIN table2 ON asdf = ?'], 'asdf');
* $qGen->where(['condition1 < ?', 'condition2 > ?'], [5, 7]);
* list($query, $params) = $qGen->build();
*
* Generated query:
* SELECT field1, field2
* FROM table1
* INNER JOIN table2 ON asdf = ?
* WHERE (condition1 < ?) AND (condition2 > ?)
*
* Generated parameters:
* ['asdf', 5, 7]
*/
class
QueryGenerator {
/**
* The keys of this array are the set of clauses that can compose different
* statements. These correspond to methods that can be called on this class.
* The values are the syntax rules for collapsing the corresponding clauses.
*/
private
static
$
methods
= [
'
select
'
=> [
'
clause
'
=>
'
SELECT <<MODIFIERS>>
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
true
,
],
'
insert
'
=> [
'
clause
'
=>
'
INSERT <<MODIFIERS>> INTO
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
''
,
],
'
replace
'
=> [
'
clause
'
=>
'
REPLACE <<MODIFIERS>> INTO
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
''
,
],
'
update
'
=> [
'
clause
'
=>
'
UPDATE <<MODIFIERS>>
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
''
,
],
'
delete
'
=> [
'
clause
'
=>
'
DELETE <<MODIFIERS>>
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
''
,
],
'
from
'
=> [
'
clause
'
=>
'
FROM
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
true
,
],
'
join
'
=> [
'
clause
'
=>
''
,
'
prefix
'
=>
''
,
'
glue
'
=>
"\n"
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
true
,
],
'
set
'
=> [
'
clause
'
=>
'
SET
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
true
,
],
'
columns
'
=> [
'
clause
'
=>
''
,
'
prefix
'
=>
'
(
'
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
'
)
'
,
'
requiresArgument
'
=>
true
,
],
'
values
'
=> [
'
clause
'
=>
'
VALUES
'
,
'
prefix
'
=>
'
(
'
,
'
glue
'
=>
'
), (
'
,
'
suffix
'
=>
'
)
'
,
'
requiresArgument
'
=>
true
,
],
'
where
'
=> [
'
clause
'
=>
'
WHERE
'
,
'
prefix
'
=>
'
(
'
,
'
glue
'
=>
'
) AND (
'
,
'
suffix
'
=>
'
)
'
,
'
requiresArgument
'
=>
true
,
],
'
group
'
=> [
'
clause
'
=>
'
GROUP BY
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
true
,
],
'
having
'
=> [
'
clause
'
=>
'
HAVING
'
,
'
prefix
'
=>
'
(
'
,
'
glue
'
=>
'
) AND (
'
,
'
suffix
'
=>
'
)
'
,
'
requiresArgument
'
=>
true
,
],
'
order
'
=> [
'
clause
'
=>
'
ORDER BY
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
true
,
],
'
limit
'
=> [
'
clause
'
=>
'
LIMIT
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
''
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
true
,
],
'
offset
'
=> [
'
clause
'
=>
'
OFFSET
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
''
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
true
,
],
'
duplicate
'
=> [
'
clause
'
=>
'
ON DUPLICATE KEY UPDATE
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
,
'
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
true
,
],
'
modify
'
=> [
'
clause
'
=>
''
,
'
prefix
'
=>
''
,
'
glue
'
=>
'
'
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
true
,
],
'
forupdate
'
=> [
'
clause
'
=>
'
FOR UPDATE
'
,
'
prefix
'
=>
''
,
'
glue
'
=>
''
,
'
suffix
'
=>
''
,
'
requiresArgument
'
=>
false
,
],
];
/**
* The keys of this array are the primary clauses that can be present in a
* MySQL query. Each primary clause has a set of valid sub-clauses that can
* be present in a completed query of that type.
*/
private
static
$
possibleClauses
= [
'
select
'
=> [
'
from
'
,
'
join
'
,
'
where
'
,
'
group
'
,
'
having
'
,
'
order
'
,
'
limit
'
,
'
offset
'
,
'
forupdate
'
],
'
insert
'
=> [
'
set
'
,
'
columns
'
,
'
values
'
,
'
duplicate
'
],
'
replace
'
=> [
'
set
'
,
'
columns
'
,
'
values
'
],
'
update
'
=> [
'
set
'
,
'
where
'
,
'
order
'
,
'
limit
'
],
'
delete
'
=> [
'
from
'
,
'
where
'
,
'
order
'
,
'
limit
'
],
];
/**
* Each query type can be formatted in a number of ways according to
* different sub-trees of its grammar. Each element in the value arrays of
* this array correspond to the minimum required set of sub-clauses needed
* in each of these grammar sub-trees. A query will be considered complete
* if it has all the sub-clauses listed in any of these sets.
*/
private
static
$
minimumClauses
= [
'
select
'
=> [[
'
from
'
]],
'
insert
'
=> [[
'
set
'
], [
'
columns
'
,
'
values
'
]],
'
replace
'
=> [[
'
set
'
], [
'
columns
'
,
'
values
'
]],
'
update
'
=> [[
'
set
'
]],
'
delete
'
=> [[
'
from
'
]],
];
/**
* Each query type can specify a certain selection of modifiers. They each
* change some aspect of how the query runs.
*/
private
static
$
queryModifiers
= [
'
select
'
=> [
'
ALL
'
,
'
DISTINCT
'
,
'
DISTINCTROW
'
,
'
HIGH_PRIORITY
'
,
'
STRAIGHT_JOIN
'
,
'
SQL_SMALL_RESULT
'
,
'
SQL_BIG_RESULT
'
,
'
SQL_BUFFER_RESULT
'
,
'
SQL_CACHE
'
,
'
SQL_NO_CACHE
'
,
'
SQL_CALC_FOUND_ROWS
'
],
'
insert
'
=> [
'
LOW_PRIORITY
'
,
'
DELAYED
'
,
'
HIGH_PRIORITY
'
,
'
IGNORE
'
],
'
replace
'
=> [
'
LOW_PRIORITY
'
,
'
DELAYED
'
],
'
update
'
=> [
'
LOW_PRIORITY
'
,
'
IGNORE
'
],
'
delete
'
=> [
'
LOW_PRIORITY
'
,
'
QUICK
'
,
'
IGNORE
'
],
];
private
$
clauses
;
private
$
params
;
private
$
validateQuery
;
private
$
useOr
;
public
function
__construct
() {
$
this
->
clauses
= [];
$
this
->
params
= [];
foreach
(
array_keys
(
self
::
$
methods
)
as
$
method
) {
$
this
->
clauses
[
$
method
] = [];
$
this
->
params
[
$
method
] = [];
}
$
this
->
validateQuery
=
true
;
$
this
->
useOr
=
false
;
}
/**
* Append the given clause components and parameters to their existing
* counterparts for the specified clause.
*/
public
function
&
__call
(
$
method
,
$
args
) {
$
method
=
strtolower
(
$
method
);
if
(!
isset
(
self
::
$
methods
[
$
method
])) {
throw
new
Exception
(
"
Method
\"
$
method
\"
does not exist.
"
);
}
$
requiresArgument
= (
isset
(
self
::
$
methods
[
$
method
][
'
requiresArgument
'
]) ?
self
::
$
methods
[
$
method
][
'
requiresArgument
'
] :
false
);
if
(
$
requiresArgument
&&
count
(
$
args
) <
1
) {
throw
new
Exception
(
"
Missing argument 1 (
\$
clauses) for
$
method
()
"
);
}
else
if
(
count
(
$
args
) <
2
) {
$
clauses
=
reset
(
$
args
);
$
params
= [];
}
else
{
list
(
$
clauses
,
$
params
) =
$
args
;
}
if
(
$
clauses
instanceOf
QueryGenerator) {
$
clauses
->
skipValidation
();
list
(
$
clauses
,
$
params
) =
$
clauses
->
build
(
/* $skipClauses = */
true
);
}
if
(!
is_array
(
$
clauses
)) {
$
clauses
= [
$
clauses
];
}
if
(!
is_array
(
$
params
)) {
$
params
= [
$
params
];
}
$
this
->
clauses
[
$
method
] =
array_merge
(
$
this
->
clauses
[
$
method
],
$
clauses
);
$
this
->
params
[
$
method
] =
array_merge
(
$
this
->
params
[
$
method
],
$
params
);
return
$
this
;
}
/**
* Combine the clauses and parameters in this QueryGenerator to compose a
* complete query and paramter list.
*
* Incomplete queries will cause a MissingClauseException to be thrown
* (one of MissingPrimaryClauseException or MissingRequiredClauseException)
* unless `skipValidation` has been called.
*
* @param $skipClauses : Exclude the 'clause' part (WHERE, SELECT, FROM,
* ...) of each sub-expression. See constructClause
* for more info. This is mostly for internal usage.
*
* Returns an array containing the query and paramter list, respectively.
*/
public
function
build
(
$
skipClauses
=
false
) {
if
(
$
this
->
validateQuery
) {
$
this
->
assertCompleteQuery
();
}
$
setMethods
=
$
this
->
getSetMethods
();
$
clauses
=
$
params
= [];
foreach
(
array_keys
(
self
::
$
methods
)
as
$
method
) {
// Modifiers are handled automatically by constructClause.
if
(
$
method
==
'
modify
'
) {
continue
;
}
// Because we are indiscriminantly interating over every possible
// clause we need to verify that each clause we use has been set.
if
(!
isset
(
$
setMethods
[
$
method
])) {
continue
;
}
$
clauses
[] =
$
this
->
constructClause
(
$
method
,
$
skipClauses
);
$
params
=
array_merge
(
$
params
,
$
this
->
params
[
$
method
]);
}
return
[
implode
(
"\n"
,
$
clauses
),
$
params
];
}
/**
* Bypass query validation when building.
*/
public
function
&
skipValidation
() {
$
this
->
validateQuery
=
false
;
return
$
this
;
}
/**
* Use OR when joining where conditions
*/
public
function
&
useOr
() {
$
this
->
useOr
=
true
;
return
$
this
;
}
/**
* Assert the completeness of this QueryGenerator instance by verifying
* that all required clauses have been set.
*/
private
function
assertCompleteQuery
() {
$
primaryMethod
=
$
this
->
getPrimaryMethod
();
if
(!
$
primaryMethod
) {
$
primaryClauseStr
=
implode
(
"
', '
"
,
$
this
->
getPrimaryClauses
());
throw
new
MissingPrimaryClauseException
(
"
Missing primary clause. One of '
$
primaryClauseStr
' needed.
"
);
}
$
minimumClauses
=
self
::
$
minimumClauses
[
$
primaryMethod
];
$
setMethods
=
$
this
->
getSetMethods
();
foreach
(
$
minimumClauses
as
$
option
) {
$
intersection
=
array_intersect
(
$
option
,
$
setMethods
);
// We will want to compare this array to another for set equality,
// so we need to throw away arbitrary ordering.
sort
(
$
option
);
sort
(
$
intersection
);
// A matching minimum set was found.
if
(
$
option
==
$
intersection
) {
return
;
}
}
$
requiredClauseOptions
=
array_map
(
function
(
$
option
) {
return
"
'
"
.
implode
(
"
', '
"
,
$
option
) .
"
'
"
;
},
$
minimumClauses
);
$
requiredClauseStr
=
'
{
'
.
implode
(
'
}, {
'
,
$
requiredClauseOptions
) .
'
}
'
;
throw
new
MissingRequiredClauseException
(
"
Missing required clauses. One of
$
requiredClauseStr
needed.
"
);
}
/**
* Return the list of primary query clauses.
*/
private
static
function
getPrimaryClauses
() {
return
array_keys
(
self
::
$
possibleClauses
);
}
/**
* Return the primary clause in this QueryGenerator instance.
* If multiple primary clauses have been set, all but the first set clause
* will be ignored.
*/
private
function
getPrimaryMethod
() {
$
primaryClauses
=
self
::
getPrimaryClauses
();
$
setMethods
=
$
this
->
getSetMethods
();
$
setPrimaryClauses
=
array_intersect
(
$
primaryClauses
,
$
setMethods
);
return
reset
(
$
setPrimaryClauses
);
}
private
function
getSetMethods
() {
$
methods
=
array_keys
(
array_filter
(
$
this
->
clauses
));
return
array_combine
(
$
methods
,
$
methods
);
}
/**
* Return a string of the specified SQL clause using its syntax rules,
* optionally excluding the clause part (i.e. WHERE, SELECT, ...)
*
* Example:
* given where clauses 'foo = ?' and 'bar != ?'
* constructClause('where') => 'WHERE (foo = ?) AND (bar != ?)'
* constructClause('where', false) => '(foo = ?) AND (bar != ?)'
*/
private
function
constructClause
(
$
method
,
$
skipClause
=
false
) {
$
clauseInfo
=
self
::
$
methods
[
$
method
];
$
prefix
=
$
clauseInfo
[
'
prefix
'
];
$
clause
=
$
clauseInfo
[
'
clause
'
];
if
(
$
skipClause
) {
$
clause
=
''
;
// The assumed precondition is that modify's prefix element will never
// contain the substring '<<MODIFIERS>>'.
}
else
if
(
strpos
(
$
clause
,
'
<<MODIFIERS>>
'
) !==
false
) {
$
clause
=
str_replace
(
'
<<MODIFIERS>>
'
,
$
this
->
constructClause
(
'
modify
'
),
$
clause
);
// If there are no modifiers to apply we end up with an extra space
// after the primary verb.
$
clause
=
str_replace
(
'
'
,
'
'
,
$
clause
);
}
$
suffix
=
$
clauseInfo
[
'
suffix
'
];
$
glue
=
$
this
->
getGlue
(
$
method
);
$
pieces
=
implode
(
$
glue
,
$
this
->
clauses
[
$
method
]);
return
"
$
clause
$
prefix
$
pieces
$
suffix
"
;
}
/**
* return the appropriate glue string for the given clause, taking into
* account $this->useOr
*/
private
function
getGlue
(
$
method
) {
if
(
$
method
!==
'
where
'
|| !
$
this
->
useOr
) {
return
self
::
$
methods
[
$
method
][
'
glue
'
];
}
else
{
return
"
) OR (
"
;
}
}
}
class
MissingClauseException
extends
Exception {}
class
MissingPrimaryClauseException
extends
MissingClauseException {}
class
MissingRequiredClauseException
extends
MissingClauseException {}
Back
|
FazBrowse Home
|
New Git URL