Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conditionsMode and extrapolateParameters to QueryResult #81

Open
wants to merge 2 commits into
base: 1.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Mouf/Database/QueryWriter/CountNbResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
}
54 changes: 48 additions & 6 deletions src/Mouf/Database/QueryWriter/QueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, string>|array<string, ValueInterface>|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<string, string>|array<string, ValueInterface>|ArrayValueInterface
*/
public function getParameters(): array
{
return $this->parameters;
}

/**
Expand All @@ -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);
}
Expand All @@ -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);
}

/**
Expand Down