From 88ed3df1c3e0042161c0b5b334b3d2b5b5bc388f Mon Sep 17 00:00:00 2001 From: 8ctopus Date: Mon, 3 Apr 2023 11:19:05 +0400 Subject: [PATCH] added support for SELECT BETWEEN operator [Closes #297] --- src/Database/SqlPreprocessor.php | 18 +++++++++++++++++- tests/Database/SqlPreprocessor.phpt | 5 +++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Database/SqlPreprocessor.php b/src/Database/SqlPreprocessor.php index 1829d8123..c6c8af25d 100644 --- a/src/Database/SqlPreprocessor.php +++ b/src/Database/SqlPreprocessor.php @@ -286,7 +286,23 @@ private function formatValue($value, ?string $mode = null): string $k = $this->delimite($k); if (is_array($v)) { if ($v) { - $vx[] = $k . ' ' . ($operator ? $operator . ' ' : '') . 'IN (' . $this->formatValue(array_values($v), self::ModeList) . ')'; + switch ($operator) { + case '': + case 'IN': + $vx[] = $k . ' ' . ($operator ? $operator . ' ' : 'IN') . ' (' . $this->formatValue(array_values($v), self::ModeList) . ')'; + break; + + case 'NOT': + $vx[] = $k . ' ' . $operator . ' IN (' . $this->formatValue(array_values($v), self::ModeList) . ')'; + break; + + case 'BETWEEN': + $vx[] = $k . ' ' . $operator . $this->formatValue(array_values($v), self::ModeAnd); + break; + + default: + throw new Nette\InvalidArgumentException("unsupported operator {$operator}"); + } } elseif ($operator === 'NOT') { } else { $vx[] = '1=0'; diff --git a/tests/Database/SqlPreprocessor.phpt b/tests/Database/SqlPreprocessor.phpt index d85beebc1..b43e3a3c6 100644 --- a/tests/Database/SqlPreprocessor.phpt +++ b/tests/Database/SqlPreprocessor.phpt @@ -104,6 +104,11 @@ test('IN', function () use ($preprocessor) { Assert::same([10, 11], $params); }); +test('BETWEEN', function () use ($preprocessor) { + [$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id BETWEEN (?, ?)', [10, 11]]); + Assert::same('SELECT id FROM author WHERE id BETWEEN (?, ?)', $sql); + Assert::same([10, 11], $params); +}); test('?name', function () use ($preprocessor) { [$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE ?name = ? OR ?name = ?', 'id', 12, 'table.number', 23]);