Skip to content

Commit 802d5c6

Browse files
committed
SqlPreprocessor: added constants for modes
1 parent 9c0305f commit 802d5c6

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/Database/SqlPreprocessor.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,25 @@ class SqlPreprocessor
1919
{
2020
use Nette\SmartObject;
2121

22-
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];
2331

2432
private const ARRAY_MODES = [
25-
'INSERT' => 'values',
26-
'REPLACE' => 'values',
27-
'KEY UPDATE' => 'set',
28-
'SET' => 'set',
29-
'WHERE' => 'and',
30-
'HAVING' => 'and',
31-
'ORDER BY' => 'order',
32-
'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,
3341
];
3442

3543
private const PARAMETRIC_COMMANDS = [
@@ -87,7 +95,7 @@ public function process(array $params, bool $useParams = false): array
8795
$param = $params[$this->counter++];
8896

8997
if (($this->counter === 2 && count($params) === 2) || !is_scalar($param)) {
90-
$res[] = $this->formatValue($param, 'auto');
98+
$res[] = $this->formatValue($param, self::MODE_AUTO);
9199
$this->arrayMode = null;
92100

93101
} elseif (is_string($param) && $this->counter > $prev + 1) {
@@ -114,7 +122,7 @@ private function callback(array $m): string
114122
if ($this->counter >= count($this->params)) {
115123
throw new Nette\InvalidArgumentException('There are more placeholders than passed parameters.');
116124
}
117-
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);
118126

119127
} elseif ($m[0] === "'" || $m[0] === '"' || $m[0] === '/' || $m[0] === '-') { // string or comment
120128
return $m;
@@ -130,7 +138,7 @@ private function callback(array $m): string
130138

131139
private function formatValue($value, string $mode = null): string
132140
{
133-
if (!$mode || $mode === 'auto') {
141+
if (!$mode || $mode === self::MODE_AUTO) {
134142
if (is_scalar($value) || is_resource($value)) {
135143
if ($this->useParams) {
136144
$this->remaining[] = $value;
@@ -187,14 +195,14 @@ private function formatValue($value, string $mode = null): string
187195

188196
if (is_array($value)) {
189197
$vx = $kx = [];
190-
if ($mode === 'auto') {
198+
if ($mode === self::MODE_AUTO) {
191199
$mode = $this->arrayMode;
192200
}
193201

194-
if ($mode === 'values') { // (key, key, ...) VALUES (value, value, ...)
202+
if ($mode === self::MODE_VALUES) { // (key, key, ...) VALUES (value, value, ...)
195203
if (array_key_exists(0, $value)) { // multi-insert
196204
if (!is_array($value[0]) && !$value[0] instanceof Row) {
197-
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.');
198206
}
199207
foreach ($value[0] as $k => $v) {
200208
$kx[] = $this->delimite($k);
@@ -217,7 +225,7 @@ private function formatValue($value, string $mode = null): string
217225
}
218226
return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
219227

220-
} elseif (!$mode || $mode === 'set') {
228+
} elseif (!$mode || $mode === self::MODE_SET) {
221229
foreach ($value as $k => $v) {
222230
if (is_int($k)) { // value, value, ... OR (1, 2), (3, 4)
223231
$vx[] = is_array($v)
@@ -232,7 +240,7 @@ private function formatValue($value, string $mode = null): string
232240
}
233241
return implode(', ', $vx);
234242

235-
} elseif ($mode === 'and' || $mode === 'or') { // (key [operator] value) AND ...
243+
} elseif ($mode === self::MODE_AND || $mode === self::MODE_OR) { // (key [operator] value) AND ...
236244
foreach ($value as $k => $v) {
237245
if (is_int($k)) {
238246
$vx[] = $this->formatValue($v);
@@ -259,7 +267,7 @@ private function formatValue($value, string $mode = null): string
259267
? '(' . implode(') ' . strtoupper($mode) . ' (', $vx) . ')'
260268
: '1=1';
261269

262-
} elseif ($mode === 'order') { // key, key DESC, ...
270+
} elseif ($mode === self::MODE_ORDER) { // key, key DESC, ...
263271
foreach ($value as $k => $v) {
264272
$vx[] = $this->delimite($k) . ($v > 0 ? '' : ' DESC');
265273
}
@@ -269,11 +277,11 @@ private function formatValue($value, string $mode = null): string
269277
throw new Nette\InvalidArgumentException("Unknown placeholder ?$mode.");
270278
}
271279

272-
} elseif (in_array($mode, self::MODE_LIST, true)) {
280+
} elseif (in_array($mode, self::MODES, true)) {
273281
$type = gettype($value);
274282
throw new Nette\InvalidArgumentException("Placeholder ?$mode expects array or Traversable object, $type given.");
275283

276-
} elseif ($mode && $mode !== 'auto') {
284+
} elseif ($mode && $mode !== self::MODE_AUTO) {
277285
throw new Nette\InvalidArgumentException("Unknown placeholder ?$mode.");
278286

279287
} else {

0 commit comments

Comments
 (0)