From 132ed95226477472f37cf50ab30cabfb435d6ffd Mon Sep 17 00:00:00 2001 From: Rodion Kulakov Date: Sun, 8 Aug 2021 20:23:58 +0300 Subject: [PATCH 1/2] Add conditionsMode and extrapolateParameters to QueryResult --- src/Mouf/Database/QueryWriter/QueryResult.php | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Mouf/Database/QueryWriter/QueryResult.php b/src/Mouf/Database/QueryWriter/QueryResult.php index d0287b3..fdcaa46 100644 --- a/src/Mouf/Database/QueryWriter/QueryResult.php +++ b/src/Mouf/Database/QueryWriter/QueryResult.php @@ -2,6 +2,7 @@ namespace Mouf\Database\QueryWriter; +use SQLParser\SqlRenderInterface; use function method_exists; use Mouf\Database\QueryWriter\Utils\DbHelper; use Mouf\Utils\Value\ValueUtils; @@ -48,15 +49,25 @@ class QueryResult implements ArrayValueInterface, PaginableInterface, SortableIn private $offset; /** @var int|null */ private $limit; + private int $conditionsMode; + private bool $extrapolateParameters; /** * @param Select $select * @param Connection $connection + * @param int $conditionsMode + * @param bool $extrapolateParameters */ - public function __construct(Select $select, Connection $connection) - { + public function __construct( + Select $select, + Connection $connection, + int $conditionsMode = SqlRenderInterface::CONDITION_APPLY, + bool $extrapolateParameters = true + ) { $this->select = $select; $this->connection = $connection; + $this->conditionsMode = $conditionsMode; + $this->extrapolateParameters = $extrapolateParameters; } /** @@ -76,8 +87,7 @@ public function setParameters($parameters): void */ public function val() { - $parameters = ValueUtils::val($this->parameters); - $pdoStatement = $this->connection->query($this->select->toSql($parameters, $this->connection->getDatabasePlatform()).DbHelper::getFromLimitString($this->offset, $this->limit)); + $pdoStatement = $this->connection->query($this->toSql().DbHelper::getFromLimitString($this->offset, $this->limit)); return new ResultSet($pdoStatement); } @@ -91,7 +101,13 @@ public function toSql() { $parameters = ValueUtils::val($this->parameters); - return $this->select->toSql($parameters, $this->connection->getDatabasePlatform()); + return $this->select->toSql( + $parameters, + $this->connection->getDatabasePlatform(), + 0, + $this->conditionsMode, + $this->extrapolateParameters + ); } /** From 38596ac3072f2db6bdf389cbe8a78f04fa66bba1 Mon Sep 17 00:00:00 2001 From: Rodion Kulakov Date: Thu, 12 Aug 2021 19:42:06 +0300 Subject: [PATCH 2/2] Add parameter types to QueryResult; pass parameters to queries if extrapolation is off --- .../Database/QueryWriter/CountNbResult.php | 7 ++++- src/Mouf/Database/QueryWriter/QueryResult.php | 30 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Mouf/Database/QueryWriter/CountNbResult.php b/src/Mouf/Database/QueryWriter/CountNbResult.php index 0de2238..34bcaec 100644 --- a/src/Mouf/Database/QueryWriter/CountNbResult.php +++ b/src/Mouf/Database/QueryWriter/CountNbResult.php @@ -50,6 +50,11 @@ public function val() { $sql = 'SELECT count(*) as cnt FROM ('.$this->queryResult->toSql().') tmp'; - return $this->connection->fetchColumn($sql); + return $this->connection->fetchColumn( + $sql, + $this->queryResult->getParametersForBind(), + 0, + $this->queryResult->getParameterTypesForBind(), + ); } } diff --git a/src/Mouf/Database/QueryWriter/QueryResult.php b/src/Mouf/Database/QueryWriter/QueryResult.php index fdcaa46..592ddbe 100644 --- a/src/Mouf/Database/QueryWriter/QueryResult.php +++ b/src/Mouf/Database/QueryWriter/QueryResult.php @@ -2,6 +2,7 @@ namespace Mouf\Database\QueryWriter; +use Doctrine\DBAL\Types\Type; use SQLParser\SqlRenderInterface; use function method_exists; use Mouf\Database\QueryWriter\Utils\DbHelper; @@ -51,6 +52,10 @@ class QueryResult implements ArrayValueInterface, PaginableInterface, SortableIn private $limit; private int $conditionsMode; private bool $extrapolateParameters; + /** + * @var Type[]|int[]|string[] + */ + private array $parameterTypes = []; /** * @param Select $select @@ -74,10 +79,20 @@ public function __construct( * The list of parameters to apply to the SQL request. * * @param array|array|ArrayValueInterface $parameters + * @param Type[]|string[]|int[] $types Parameter types */ - public function setParameters($parameters): void + public function setParameters($parameters, array $types = []): void { $this->parameters = $parameters; + $this->parameterTypes = $types; + } + + /** + * @return array|array|ArrayValueInterface + */ + public function getParameters(): array + { + return $this->parameters; } /** @@ -87,7 +102,8 @@ public function setParameters($parameters): void */ public function val() { - $pdoStatement = $this->connection->query($this->toSql().DbHelper::getFromLimitString($this->offset, $this->limit)); + $sql = $this->toSql().DbHelper::getFromLimitString($this->offset, $this->limit); + $pdoStatement = $this->connection->executeQuery($sql, $this->getParametersForBind(), $this->getParameterTypesForBind()); return new ResultSet($pdoStatement); } @@ -110,6 +126,16 @@ public function toSql() ); } + public function getParametersForBind(): array + { + return $this->extrapolateParameters ? [] : $this->parameters; + } + + public function getParameterTypesForBind(): array + { + return $this->extrapolateParameters ? [] : array_intersect_key($this->parameterTypes, $this->parameters); + } + /** * Paginates the result set. *