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 d0287b3..592ddbe 100644 --- a/src/Mouf/Database/QueryWriter/QueryResult.php +++ b/src/Mouf/Database/QueryWriter/QueryResult.php @@ -2,6 +2,8 @@ namespace Mouf\Database\QueryWriter; +use Doctrine\DBAL\Types\Type; +use SQLParser\SqlRenderInterface; use function method_exists; use Mouf\Database\QueryWriter\Utils\DbHelper; use Mouf\Utils\Value\ValueUtils; @@ -48,25 +50,49 @@ class QueryResult implements ArrayValueInterface, PaginableInterface, SortableIn private $offset; /** @var int|null */ private $limit; + private int $conditionsMode; + private bool $extrapolateParameters; + /** + * @var Type[]|int[]|string[] + */ + private array $parameterTypes = []; /** * @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; } /** * 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; } /** @@ -76,8 +102,8 @@ 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)); + $sql = $this->toSql().DbHelper::getFromLimitString($this->offset, $this->limit); + $pdoStatement = $this->connection->executeQuery($sql, $this->getParametersForBind(), $this->getParameterTypesForBind()); return new ResultSet($pdoStatement); } @@ -91,7 +117,23 @@ 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 + ); + } + + public function getParametersForBind(): array + { + return $this->extrapolateParameters ? [] : $this->parameters; + } + + public function getParameterTypesForBind(): array + { + return $this->extrapolateParameters ? [] : array_intersect_key($this->parameterTypes, $this->parameters); } /**