Skip to content
Merged
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
44 changes: 32 additions & 12 deletions src/query/ConditionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,69 +81,69 @@ public function limit(int $number): static
/**
* Add WHERE clause.
*
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
*
* @return static The current instance.
*/
public function where(string|array|RawValue $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
public function where(string|array|RawValue|\Closure $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
{
return $this->addCondition(QueryConstants::COND_WHERE, $exprOrColumn, $value, $operator, QueryConstants::BOOLEAN_AND);
}

/**
* Add AND WHERE clause.
*
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
*
* @return static The current instance.
*/
public function andWhere(string|array|RawValue $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
public function andWhere(string|array|RawValue|\Closure $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
{
return $this->where($exprOrColumn, $value, $operator);
}

/**
* Add OR WHERE clause.
*
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
*
* @return static The current instance.
*/
public function orWhere(string|array|RawValue $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
public function orWhere(string|array|RawValue|\Closure $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
{
return $this->addCondition(QueryConstants::COND_WHERE, $exprOrColumn, $value, $operator, QueryConstants::BOOLEAN_OR);
}

/**
* Add HAVING clause.
*
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
*
* @return static The current instance.
*/
public function having(string|array|RawValue $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
public function having(string|array|RawValue|\Closure $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
{
return $this->addCondition(QueryConstants::COND_HAVING, $exprOrColumn, $value, $operator, QueryConstants::BOOLEAN_AND);
}

/**
* Add OR HAVING clause.
*
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
*
* @return static The current instance.
*/
public function orHaving(string|array|RawValue $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
public function orHaving(string|array|RawValue|\Closure $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
{
return $this->addCondition(QueryConstants::COND_HAVING, $exprOrColumn, $value, $operator, QueryConstants::BOOLEAN_OR);
}
Expand Down Expand Up @@ -653,7 +653,7 @@ public function getHaving(): array
* Add condition to the WHERE or HAVING clause.
*
* @param string $prop The property to add the condition to.
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
* @param string $cond The condition to use.
Expand All @@ -662,11 +662,31 @@ public function getHaving(): array
*/
protected function addCondition(
string $prop,
string|array|RawValue $exprOrColumn,
string|array|RawValue|\Closure $exprOrColumn,
mixed $value,
string $operator,
string $cond
): static {
// Handle logical grouping via Closure
if ($exprOrColumn instanceof \Closure) {
$instance = new QueryBuilder($this->connection, $this->prefix ?? '');
$exprOrColumn($instance);

$subCb = $instance->getConditionBuilder();
$items = ($prop === QueryConstants::COND_WHERE) ? $subCb->getWhere() : $subCb->getHaving();

if (!empty($items)) {
$sql = trim($this->buildConditionsClause($items, ''));

$subParams = $instance->getParameterManager()->getParams();
$map = $this->parameterManager->mergeSubParams($subParams, 'grp');
$sql = $this->parameterManager->replacePlaceholdersInSql($sql, $map);

$this->{$prop}[] = ['sql' => "({$sql})", 'cond' => $cond];
}
return $this;
}

if (is_array($exprOrColumn)) {
foreach ($exprOrColumn as $col => $val) {
$exprQuoted = $this->quoteQualifiedIdentifier((string)$col);
Expand Down
40 changes: 30 additions & 10 deletions src/query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1360,13 +1360,13 @@
/**
* Add WHERE clause.
*
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
*
* @return static The current instance.
*/
public function where(string|array|RawValue $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
public function where(string|array|RawValue|\Closure $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
{
// Handle the old signature: where(array $conditions, array $params)
if (is_array($exprOrColumn) && is_array($value) && $operator === QueryConstants::OP_EQUAL) {
Expand All @@ -1386,67 +1386,67 @@
}

// Handle the new signature: where(string|array|RawValue $exprOrColumn, mixed $value, string $operator)
$this->conditionBuilder->where($exprOrColumn, $value, $operator);

Check failure on line 1389 in src/query/QueryBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Parameter #1 $exprOrColumn of method tommyknocker\pdodb\query\interfaces\ConditionBuilderInterface::where() expects array<string, mixed>|string|tommyknocker\pdodb\helpers\values\RawValue, array<string, mixed>|(Closure)|string|tommyknocker\pdodb\helpers\values\RawValue given.
return $this;
}

/**
* Add AND WHERE clause.
*
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
*
* @return static The current instance.
*/
public function andWhere(string|array|RawValue $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
public function andWhere(string|array|RawValue|\Closure $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
{
$this->conditionBuilder->andWhere($exprOrColumn, $value, $operator);

Check failure on line 1404 in src/query/QueryBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Parameter #1 $exprOrColumn of method tommyknocker\pdodb\query\interfaces\ConditionBuilderInterface::andWhere() expects array<string, mixed>|string|tommyknocker\pdodb\helpers\values\RawValue, array<string, mixed>|(Closure)|string|tommyknocker\pdodb\helpers\values\RawValue given.
return $this;
}

/**
* Add OR WHERE clause.
*
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
*
* @return static The current instance.
*/
public function orWhere(string|array|RawValue $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
public function orWhere(string|array|RawValue|\Closure $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
{
$this->conditionBuilder->orWhere($exprOrColumn, $value, $operator);

Check failure on line 1419 in src/query/QueryBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Parameter #1 $exprOrColumn of method tommyknocker\pdodb\query\interfaces\ConditionBuilderInterface::orWhere() expects array<string, mixed>|string|tommyknocker\pdodb\helpers\values\RawValue, array<string, mixed>|(Closure)|string|tommyknocker\pdodb\helpers\values\RawValue given.
return $this;
}

/**
* Add HAVING clause.
*
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
*
* @return static The current instance.
*/
public function having(string|array|RawValue $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
public function having(string|array|RawValue|\Closure $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
{
$this->conditionBuilder->having($exprOrColumn, $value, $operator);

Check failure on line 1434 in src/query/QueryBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Parameter #1 $exprOrColumn of method tommyknocker\pdodb\query\interfaces\ConditionBuilderInterface::having() expects array<string, mixed>|string|tommyknocker\pdodb\helpers\values\RawValue, array<string, mixed>|(Closure)|string|tommyknocker\pdodb\helpers\values\RawValue given.
return $this;
}

/**
* Add OR HAVING clause.
*
* @param string|array<string, mixed>|RawValue $exprOrColumn The expression or column to add.
* @param string|array<string, mixed>|RawValue|\Closure(QueryBuilder): void $exprOrColumn The expression or column to add.
* @param mixed $value The value to use in the condition.
* @param string $operator The operator to use in the condition.
*
* @return static The current instance.
*/
public function orHaving(string|array|RawValue $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
public function orHaving(string|array|RawValue|\Closure $exprOrColumn, mixed $value = null, string $operator = QueryConstants::OP_EQUAL): static
{
$this->conditionBuilder->orHaving($exprOrColumn, $value, $operator);

Check failure on line 1449 in src/query/QueryBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Parameter #1 $exprOrColumn of method tommyknocker\pdodb\query\interfaces\ConditionBuilderInterface::orHaving() expects array<string, mixed>|string|tommyknocker\pdodb\helpers\values\RawValue, array<string, mixed>|(Closure)|string|tommyknocker\pdodb\helpers\values\RawValue given.
return $this;
}

Expand Down Expand Up @@ -2644,4 +2644,24 @@
'If you intended to call a macro, make sure it is registered using QueryBuilder::macro().'
);
}

/**
* Get the condition builder instance.
*
* @return ConditionBuilderInterface
*/
public function getConditionBuilder(): ConditionBuilderInterface
{
return $this->conditionBuilder;
}

/**
* Get the parameter manager instance.
*
* @return ParameterManagerInterface
*/
public function getParameterManager(): ParameterManagerInterface
{
return $this->parameterManager;
}
}
Loading