Skip to content

Commit

Permalink
added support for SELECT BETWEEN operator [Closes nette#297]
Browse files Browse the repository at this point in the history
  • Loading branch information
8ctopus committed Apr 3, 2023
1 parent 4048b01 commit 88ed3df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Database/SqlPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 5 additions & 0 deletions tests/Database/SqlPreprocessor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 88ed3df

Please sign in to comment.