@@ -19,18 +19,25 @@ class SqlPreprocessor
19
19
{
20
20
use Nette \SmartObject;
21
21
22
- /** @var array */
23
- private const MODE_LIST = ['and ' , 'or ' , 'set ' , 'values ' , 'order ' ];
22
+ private const
23
+ MODE_AND = 'and ' , // (key [operator] value) AND ...
24
+ MODE_OR = 'or ' , // (key [operator] value) OR ...
25
+ MODE_SET = 'set ' , // key=value, key=value, ...
26
+ MODE_VALUES = 'values ' , // (key, key, ...) VALUES (value, value, ...)
27
+ MODE_ORDER = 'order ' , // key, key DESC, ...
28
+ MODE_AUTO = 'auto ' ; // arrayMode for arrays
29
+
30
+ private const MODES = [self ::MODE_AND , self ::MODE_OR , self ::MODE_SET , self ::MODE_VALUES , self ::MODE_ORDER ];
24
31
25
32
private const ARRAY_MODES = [
26
- 'INSERT ' => ' values ' ,
27
- 'REPLACE ' => ' values ' ,
28
- 'KEY UPDATE ' => ' set ' ,
29
- 'SET ' => ' set ' ,
30
- 'WHERE ' => ' and ' ,
31
- 'HAVING ' => ' and ' ,
32
- 'ORDER BY ' => ' order ' ,
33
- 'GROUP BY ' => ' order ' ,
33
+ 'INSERT ' => self :: MODE_VALUES ,
34
+ 'REPLACE ' => self :: MODE_VALUES ,
35
+ 'KEY UPDATE ' => self :: MODE_SET ,
36
+ 'SET ' => self :: MODE_SET ,
37
+ 'WHERE ' => self :: MODE_AND ,
38
+ 'HAVING ' => self :: MODE_AND ,
39
+ 'ORDER BY ' => self :: MODE_ORDER ,
40
+ 'GROUP BY ' => self :: MODE_ORDER ,
34
41
];
35
42
36
43
private const PARAMETRIC_COMMANDS = [
@@ -88,7 +95,7 @@ public function process(array $params, bool $useParams = false): array
88
95
$ param = $ params [$ this ->counter ++];
89
96
90
97
if (($ this ->counter === 2 && count ($ params ) === 2 ) || !is_scalar ($ param )) {
91
- $ res [] = $ this ->formatValue ($ param , ' auto ' );
98
+ $ res [] = $ this ->formatValue ($ param , self :: MODE_AUTO );
92
99
$ this ->arrayMode = null ;
93
100
94
101
} elseif (is_string ($ param ) && $ this ->counter > $ prev + 1 ) {
@@ -115,7 +122,7 @@ private function callback(array $m): string
115
122
if ($ this ->counter >= count ($ this ->params )) {
116
123
throw new Nette \InvalidArgumentException ('There are more placeholders than passed parameters. ' );
117
124
}
118
- return $ this ->formatValue ($ this ->params [$ this ->counter ++], substr ($ m , 1 ) ?: ' auto ' );
125
+ return $ this ->formatValue ($ this ->params [$ this ->counter ++], substr ($ m , 1 ) ?: self :: MODE_AUTO );
119
126
120
127
} elseif ($ m [0 ] === "' " || $ m [0 ] === '" ' || $ m [0 ] === '/ ' || $ m [0 ] === '- ' ) { // string or comment
121
128
return $ m ;
@@ -131,7 +138,7 @@ private function callback(array $m): string
131
138
132
139
private function formatValue ($ value , string $ mode = null ): string
133
140
{
134
- if (!$ mode || $ mode === ' auto ' ) {
141
+ if (!$ mode || $ mode === self :: MODE_AUTO ) {
135
142
if (is_scalar ($ value ) || is_resource ($ value )) {
136
143
if ($ this ->useParams ) {
137
144
$ this ->remaining [] = $ value ;
@@ -188,14 +195,14 @@ private function formatValue($value, string $mode = null): string
188
195
189
196
if (is_array ($ value )) {
190
197
$ vx = $ kx = [];
191
- if ($ mode === ' auto ' ) {
198
+ if ($ mode === self :: MODE_AUTO ) {
192
199
$ mode = $ this ->arrayMode ;
193
200
}
194
201
195
- if ($ mode === ' values ' ) { // (key, key, ...) VALUES (value, value, ...)
202
+ if ($ mode === self :: MODE_VALUES ) { // (key, key, ...) VALUES (value, value, ...)
196
203
if (array_key_exists (0 , $ value )) { // multi-insert
197
204
if (!is_array ($ value [0 ]) && !$ value [0 ] instanceof Row) {
198
- throw new Nette \InvalidArgumentException ('Automaticaly detected multi-insert, but values aren \'t array. If you need try to change mode like "?[ ' . implode ('| ' , self ::MODE_LIST ) . ']". Mode " ' . $ mode . '" was used. ' );
205
+ throw new Nette \InvalidArgumentException ('Automaticaly detected multi-insert, but values aren \'t array. If you need try to change mode like "?[ ' . implode ('| ' , self ::MODES ) . ']". Mode " ' . $ mode . '" was used. ' );
199
206
}
200
207
foreach ($ value [0 ] as $ k => $ v ) {
201
208
$ kx [] = $ this ->delimite ($ k );
@@ -218,7 +225,7 @@ private function formatValue($value, string $mode = null): string
218
225
}
219
226
return '( ' . implode (', ' , $ kx ) . ') VALUES ( ' . implode (', ' , $ vx ) . ') ' ;
220
227
221
- } elseif (!$ mode || $ mode === ' set ' ) {
228
+ } elseif (!$ mode || $ mode === self :: MODE_SET ) {
222
229
foreach ($ value as $ k => $ v ) {
223
230
if (is_int ($ k )) { // value, value, ... OR (1, 2), (3, 4)
224
231
$ vx [] = is_array ($ v ) ? '( ' . $ this ->formatValue ($ v ) . ') ' : $ this ->formatValue ($ v );
@@ -231,7 +238,7 @@ private function formatValue($value, string $mode = null): string
231
238
}
232
239
return implode (', ' , $ vx );
233
240
234
- } elseif ($ mode === ' and ' || $ mode === ' or ' ) { // (key [operator] value) AND ...
241
+ } elseif ($ mode === self :: MODE_AND || $ mode === self :: MODE_OR ) { // (key [operator] value) AND ...
235
242
foreach ($ value as $ k => $ v ) {
236
243
if (is_int ($ k )) {
237
244
$ vx [] = $ this ->formatValue ($ v );
@@ -256,7 +263,7 @@ private function formatValue($value, string $mode = null): string
256
263
}
257
264
return $ value ? '( ' . implode (') ' . strtoupper ($ mode ) . ' ( ' , $ vx ) . ') ' : '1=1 ' ;
258
265
259
- } elseif ($ mode === ' order ' ) { // key, key DESC, ...
266
+ } elseif ($ mode === self :: MODE_ORDER ) { // key, key DESC, ...
260
267
foreach ($ value as $ k => $ v ) {
261
268
$ vx [] = $ this ->delimite ($ k ) . ($ v > 0 ? '' : ' DESC ' );
262
269
}
@@ -266,11 +273,11 @@ private function formatValue($value, string $mode = null): string
266
273
throw new Nette \InvalidArgumentException ("Unknown placeholder ? $ mode. " );
267
274
}
268
275
269
- } elseif (in_array ($ mode , self ::MODE_LIST , true )) {
276
+ } elseif (in_array ($ mode , self ::MODES , true )) {
270
277
$ type = gettype ($ value );
271
278
throw new Nette \InvalidArgumentException ("Placeholder ? $ mode expects array or Traversable object, $ type given. " );
272
279
273
- } elseif ($ mode && $ mode !== ' auto ' ) {
280
+ } elseif ($ mode && $ mode !== self :: MODE_AUTO ) {
274
281
throw new Nette \InvalidArgumentException ("Unknown placeholder ? $ mode. " );
275
282
276
283
} else {
0 commit comments