From 812517e8e9cfe137a4b6f5647c98435e143feacc Mon Sep 17 00:00:00 2001 From: webimpress Date: Thu, 14 Feb 2019 15:22:11 +0000 Subject: [PATCH] Hotfix #355: Named params in subquery - limit and offset Set proper names for limit and offset parameters in subquery - add prefix. Fixes #355 --- src/Sql/Select.php | 4 ++-- test/unit/Sql/SelectTest.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Sql/Select.php b/src/Sql/Select.php index 321eb5b6b4..e877a28853 100644 --- a/src/Sql/Select.php +++ b/src/Sql/Select.php @@ -716,7 +716,7 @@ protected function processLimit( if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; $parameterContainer->offsetSet($paramPrefix . 'limit', $this->limit, ParameterContainer::TYPE_INTEGER); - return [$driver->formatParameterName('limit')]; + return [$driver->formatParameterName($paramPrefix . 'limit')]; } return [$platform->quoteValue($this->limit)]; } @@ -732,7 +732,7 @@ protected function processOffset( if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; $parameterContainer->offsetSet($paramPrefix . 'offset', $this->offset, ParameterContainer::TYPE_INTEGER); - return [$driver->formatParameterName('offset')]; + return [$driver->formatParameterName($paramPrefix . 'offset')]; } return [$platform->quoteValue($this->offset)]; diff --git a/test/unit/Sql/SelectTest.php b/test/unit/Sql/SelectTest.php index fae5790dc0..0054b42936 100644 --- a/test/unit/Sql/SelectTest.php +++ b/test/unit/Sql/SelectTest.php @@ -16,6 +16,7 @@ use Zend\Db\Sql\Expression; use Zend\Db\Sql\Having; use Zend\Db\Sql\Predicate; +use Zend\Db\Sql\Predicate\In; use Zend\Db\Sql\Select; use Zend\Db\Sql\TableIdentifier; use Zend\Db\Sql\Where; @@ -1404,6 +1405,22 @@ public function providerData() ]], ]; + $subSelect53 = new Select; + $subSelect53->from('bar')->columns(['id'])->limit(10)->offset(9); + $select53 = new Select; + $select53->from('foo')->where(new In('bar_id', $subSelect53))->limit(11)->offset(12); + $params53 = ['limit' => 11, 'offset' => 12, 'subselect1limit' => 10, 'subselect1offset' => 9]; + // @codingStandardsIgnoreStart + $sqlPrep53 = 'SELECT "foo".* FROM "foo" WHERE "bar_id" IN (SELECT "bar"."id" AS "id" FROM "bar" LIMIT :subselect1limit OFFSET :subselect1offset) LIMIT :limit OFFSET :offset'; + $sqlStr53 = 'SELECT "foo".* FROM "foo" WHERE "bar_id" IN (SELECT "bar"."id" AS "id" FROM "bar" LIMIT \'10\' OFFSET \'9\') LIMIT \'11\' OFFSET \'12\''; + // @codingStandardsIgnoreEnd + $internalTests53 = [ + 'processSelect' => [[['"foo".*']], '"foo"'], + 'processWhere' => ['"bar_id" IN (SELECT "bar"."id" AS "id" FROM "bar" LIMIT ? OFFSET ?)'], + 'processLimit' => ['?'], + 'processOffset' => ['?'], + ]; + /** * $select = the select object * $sqlPrep = the sql as a result of preparation @@ -1467,6 +1484,7 @@ public function providerData() [$select50, $sqlPrep50, [], $sqlStr50, $internalTests50], [$select51, $sqlPrep51, [], $sqlStr51, $internalTests51], [$select52, $sqlPrep52, [], $sqlStr52, $internalTests52], + [$select53, $sqlPrep53, $params53, $sqlStr53, $internalTests53, true], ]; } }