Skip to content

Commit e603bd2

Browse files
committed
SqlPreprocessor: added constants for modes
1 parent 85f91d6 commit e603bd2

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/Database/SqlPreprocessor.php

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

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];
2431

2532
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,
3441
];
3542

3643
private const PARAMETRIC_COMMANDS = [
@@ -88,7 +95,7 @@ public function process(array $params, bool $useParams = false): array
8895
$param = $params[$this->counter++];
8996

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

94101
} elseif (is_string($param) && $this->counter > $prev + 1) {
@@ -115,7 +122,7 @@ private function callback(array $m): string
115122
if ($this->counter >= count($this->params)) {
116123
throw new Nette\InvalidArgumentException('There are more placeholders than passed parameters.');
117124
}
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);
119126

120127
} elseif ($m[0] === "'" || $m[0] === '"' || $m[0] === '/' || $m[0] === '-') { // string or comment
121128
return $m;
@@ -131,7 +138,7 @@ private function callback(array $m): string
131138

132139
private function formatValue($value, string $mode = null): string
133140
{
134-
if (!$mode || $mode === 'auto') {
141+
if (!$mode || $mode === self::MODE_AUTO) {
135142
if (is_scalar($value) || is_resource($value)) {
136143
if ($this->useParams) {
137144
$this->remaining[] = $value;
@@ -188,14 +195,14 @@ private function formatValue($value, string $mode = null): string
188195

189196
if (is_array($value)) {
190197
$vx = $kx = [];
191-
if ($mode === 'auto') {
198+
if ($mode === self::MODE_AUTO) {
192199
$mode = $this->arrayMode;
193200
}
194201

195-
if ($mode === 'values') { // (key, key, ...) VALUES (value, value, ...)
202+
if ($mode === self::MODE_VALUES) { // (key, key, ...) VALUES (value, value, ...)
196203
if (array_key_exists(0, $value)) { // multi-insert
197204
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.');
199206
}
200207
foreach ($value[0] as $k => $v) {
201208
$kx[] = $this->delimite($k);
@@ -218,7 +225,7 @@ private function formatValue($value, string $mode = null): string
218225
}
219226
return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
220227

221-
} elseif (!$mode || $mode === 'set') {
228+
} elseif (!$mode || $mode === self::MODE_SET) {
222229
foreach ($value as $k => $v) {
223230
if (is_int($k)) { // value, value, ... OR (1, 2), (3, 4)
224231
$vx[] = is_array($v) ? '(' . $this->formatValue($v) . ')' : $this->formatValue($v);
@@ -231,7 +238,7 @@ private function formatValue($value, string $mode = null): string
231238
}
232239
return implode(', ', $vx);
233240

234-
} elseif ($mode === 'and' || $mode === 'or') { // (key [operator] value) AND ...
241+
} elseif ($mode === self::MODE_AND || $mode === self::MODE_OR) { // (key [operator] value) AND ...
235242
foreach ($value as $k => $v) {
236243
if (is_int($k)) {
237244
$vx[] = $this->formatValue($v);
@@ -256,7 +263,7 @@ private function formatValue($value, string $mode = null): string
256263
}
257264
return $value ? '(' . implode(') ' . strtoupper($mode) . ' (', $vx) . ')' : '1=1';
258265

259-
} elseif ($mode === 'order') { // key, key DESC, ...
266+
} elseif ($mode === self::MODE_ORDER) { // key, key DESC, ...
260267
foreach ($value as $k => $v) {
261268
$vx[] = $this->delimite($k) . ($v > 0 ? '' : ' DESC');
262269
}
@@ -266,11 +273,11 @@ private function formatValue($value, string $mode = null): string
266273
throw new Nette\InvalidArgumentException("Unknown placeholder ?$mode.");
267274
}
268275

269-
} elseif (in_array($mode, self::MODE_LIST, true)) {
276+
} elseif (in_array($mode, self::MODES, true)) {
270277
$type = gettype($value);
271278
throw new Nette\InvalidArgumentException("Placeholder ?$mode expects array or Traversable object, $type given.");
272279

273-
} elseif ($mode && $mode !== 'auto') {
280+
} elseif ($mode && $mode !== self::MODE_AUTO) {
274281
throw new Nette\InvalidArgumentException("Unknown placeholder ?$mode.");
275282

276283
} else {

0 commit comments

Comments
 (0)