From d5cf2d497df9d0b82beec1248e02bf4921bccda7 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Tue, 18 Feb 2020 11:58:58 +0300 Subject: [PATCH] - added the ability to pass parameters into Expression --- CHANGELOG.md | 4 +++ src/Driver/Compiler.php | 1 + src/Driver/CompilerCache.php | 24 ++++++++++++++ tests/Database/SelectQueryTest.php | 53 ++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a509c7a..bbeaee52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ CHANGELOG for 0.9.0 RC ====================== +v2.7.9 (18.02.2020) +----- +- added the ability to pass parameters into Expression in operators and values + v2.7.8 (18.02.2020) ----- - added the ability to pass parameters into Expression diff --git a/src/Driver/Compiler.php b/src/Driver/Compiler.php index 208641ce..638221f0 100644 --- a/src/Driver/Compiler.php +++ b/src/Driver/Compiler.php @@ -12,6 +12,7 @@ namespace Spiral\Database\Driver; use Spiral\Database\Exception\CompilerException; +use Spiral\Database\Injection\Expression; use Spiral\Database\Injection\FragmentInterface; use Spiral\Database\Injection\Parameter; use Spiral\Database\Injection\ParameterInterface; diff --git a/src/Driver/CompilerCache.php b/src/Driver/CompilerCache.php index 43ddacaf..9258d639 100644 --- a/src/Driver/CompilerCache.php +++ b/src/Driver/CompilerCache.php @@ -237,10 +237,22 @@ protected function hashWhere(QueryParameters $params, array $where): string } elseif ($context[0] instanceof ParameterInterface) { $hash .= $this->hashParam($params, $context[0]); } else { + if ($context[0] instanceof Expression) { + foreach ($context[0]->getTokens()['parameters'] as $param) { + $params->push($param); + } + } + $hash .= $context[0]; } // operator + if ($context[1] instanceof Expression) { + foreach ($context[1]->getTokens()['parameters'] as $param) { + $params->push($param); + } + } + $hash .= $context[1]; if ($context[2] instanceof QueryInterface) { @@ -249,6 +261,12 @@ protected function hashWhere(QueryParameters $params, array $where): string } elseif ($context[2] instanceof ParameterInterface) { $hash .= $this->hashParam($params, $context[2]); } else { + if ($context[2] instanceof Expression) { + foreach ($context[2]->getTokens()['parameters'] as $param) { + $params->push($param); + } + } + $hash .= $context[2]; } @@ -259,6 +277,12 @@ protected function hashWhere(QueryParameters $params, array $where): string } elseif ($context[3] instanceof ParameterInterface) { $hash .= $this->hashParam($params, $context[3]); } else { + if ($context[3] instanceof Expression) { + foreach ($context[3]->getTokens()['parameters'] as $param) { + $params->push($param); + } + } + $hash .= $context[3]; } } diff --git a/tests/Database/SelectQueryTest.php b/tests/Database/SelectQueryTest.php index 45d377d4..35f9df97 100644 --- a/tests/Database/SelectQueryTest.php +++ b/tests/Database/SelectQueryTest.php @@ -2377,4 +2377,57 @@ public function testSelectWithParametricExpression(): void $select ); } + + public function testSelectWithParametricExpression2(): void + { + $select = $this->database->select() + ->from(['users']) + ->where( + new Expression('RANGE(?, ?)', 101, 102), + '&&', + new Expression('RANGE(?, ?)', 103, 104) + ); + + $this->assertSameQuery( + 'SELECT * FROM {users} WHERE RANGE(?, ?) && RANGE(?, ?)', + $select + ); + + $this->assertSameParameters( + [ + 101, + 102, + 103, + 104 + ], + $select + ); + } + + public function testSelectWithParametricExpression3(): void + { + $select = $this->database->select() + ->from(['users']) + ->where( + new Expression('RANGE(?, ?)', 101, 102), + new Expression('RANGE(name, ?)', 600), + new Expression('RANGE(?, ?)', 103, 104) + ); + + $this->assertSameQuery( + 'SELECT * FROM {users} WHERE RANGE(?, ?) RANGE({name}, ?) RANGE(?, ?)', + $select + ); + + $this->assertSameParameters( + [ + 101, + 102, + 600, + 103, + 104 + ], + $select + ); + } }