Skip to content

Commit

Permalink
drop required IConventions in DbalTableJoin constructor (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach authored Feb 6, 2024
1 parent 87d6980 commit 1e3ec30
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 48 deletions.
15 changes: 11 additions & 4 deletions src/Collection/Aggregations/AnyAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
use Nextras\Orm\Collection\Functions\Result\DbalTableJoin;
use Nextras\Orm\Exception\InvalidArgumentException;
use Nextras\Orm\Exception\InvalidStateException;
use function array_merge;
use function array_pop;
Expand Down Expand Up @@ -56,21 +57,27 @@ public function aggregateExpression(
if ($join === null) {
throw new InvalidStateException('Aggregation applied over expression without a relationship');
}
if (count($join->primaryKeys) === 0) {
throw new InvalidArgumentException('Aggregation applied over a table join without specifying a primary key.');
}
if (count($join->primaryKeys) > 1) {
throw new InvalidArgumentException(
'Aggregation applied over a table join with multi column primary key; currently, this is not supported.',
);
}

$joins[] = new DbalTableJoin(
toExpression: $join->toExpression,
toArgs: $join->toArgs,
toAlias: $join->toAlias,
onExpression: "($join->onExpression) AND $expression->expression",
onArgs: array_merge($join->onArgs, $expression->args),
conventions: $join->conventions,
primaryKeys: $join->primaryKeys,
);

$primaryKey = $join->conventions->getStoragePrimaryKey()[0];

return new DbalExpressionResult(
expression: 'COUNT(%table.%column) > 0',
args: [$join->toAlias, $primaryKey],
args: [$join->toAlias, $join->primaryKeys[0]],
joins: $joins,
groupBy: $expression->groupBy,
isHavingClause: true,
Expand Down
15 changes: 11 additions & 4 deletions src/Collection/Aggregations/CountAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
use Nextras\Orm\Collection\Functions\Result\DbalTableJoin;
use Nextras\Orm\Exception\InvalidArgumentException;
use Nextras\Orm\Exception\InvalidStateException;


Expand Down Expand Up @@ -60,21 +61,27 @@ public function aggregateExpression(
if ($join === null) {
throw new InvalidStateException('Aggregation applied over expression without a relationship');
}
if (count($join->primaryKeys) === 0) {
throw new InvalidArgumentException('Aggregation applied over a table join without specifying a primary key.');
}
if (count($join->primaryKeys) > 1) {
throw new InvalidArgumentException(
'Aggregation applied over a table join with multi column primary key; currently, this is not supported.',
);
}

$joins[] = new DbalTableJoin(
toExpression: $join->toExpression,
toArgs: $join->toArgs,
toAlias: $join->toAlias,
onExpression: "($join->onExpression) AND $expression->expression",
onArgs: array_merge($join->onArgs, $expression->args),
conventions: $join->conventions,
primaryKeys: $join->primaryKeys,
);

$primaryKey = $join->conventions->getStoragePrimaryKey()[0];

return new DbalExpressionResult(
expression: 'COUNT(%table.%column) >= %i AND COUNT(%table.%column) <= %i',
args: [$join->toAlias, $primaryKey, $this->atLeast, $join->toAlias, $primaryKey, $this->atMost],
args: [$join->toAlias, $join->primaryKeys[0], $this->atLeast, $join->toAlias, $join->primaryKeys[0], $this->atMost],
joins: $joins,
groupBy: $expression->groupBy,
isHavingClause: true,
Expand Down
15 changes: 11 additions & 4 deletions src/Collection/Aggregations/NoneAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
use Nextras\Orm\Collection\Functions\Result\DbalTableJoin;
use Nextras\Orm\Exception\InvalidArgumentException;
use Nextras\Orm\Exception\InvalidStateException;
use function array_merge;
use function array_pop;
Expand Down Expand Up @@ -56,21 +57,27 @@ public function aggregateExpression(
if ($join === null) {
throw new InvalidStateException('Aggregation applied over expression without a relationship');
}
if (count($join->primaryKeys) === 0) {
throw new InvalidArgumentException('Aggregation applied over a table join without specifying a primary key.');
}
if (count($join->primaryKeys) > 1) {
throw new InvalidArgumentException(
'Aggregation applied over a table join with multi column primary key; currently, this is not supported.',
);
}

$joins[] = new DbalTableJoin(
toExpression: $join->toExpression,
toArgs: $join->toArgs,
toAlias: $join->toAlias,
onExpression: "($join->onExpression) AND $expression->expression",
onArgs: array_merge($join->onArgs, $expression->args),
conventions: $join->conventions,
primaryKeys: $join->primaryKeys,
);

$primaryKey = $join->conventions->getStoragePrimaryKey()[0];

return new DbalExpressionResult(
expression: 'COUNT(%table.%column) = 0',
args: [$join->toAlias, $primaryKey],
args: [$join->toAlias, $join->primaryKeys[0]],
joins: $joins,
groupBy: $expression->groupBy,
isHavingClause: true,
Expand Down
4 changes: 2 additions & 2 deletions src/Collection/Functions/FetchPropertyFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private function processRelationship(
toAlias: $joinAlias,
onExpression: "%table.%column = %table.%column",
onArgs: [$currentAlias, $fromColumn, $joinAlias, $inColumn],
conventions: $currentConventions,
primaryKeys: [$currentConventions->getStoragePrimaryKey()[0]],
);

$currentAlias = $joinAlias;
Expand All @@ -358,7 +358,7 @@ private function processRelationship(
toAlias: $targetAlias,
onExpression: "%table.%column = %table.%column",
onArgs: [$currentAlias, $fromColumn, $targetAlias, $toColumn],
conventions: $targetConventions,
primaryKeys: [$targetConventions->getStoragePrimaryKey()[0]],
);

return [$targetAlias, $targetConventions, $targetEntityMetadata, $targetMapper];
Expand Down
49 changes: 16 additions & 33 deletions src/Collection/Functions/Result/DbalTableJoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,37 @@


use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Mapper\Dbal\Conventions\IConventions;


/**
* SQL join metadata holder.
*
* The joins are created lazily and this class holds data for it.
*
* If there is an aggregation, the joined table needs to be grouped by {@see DbalTableJoin::$primaryKeys},
* if not needed or possible, pass jum an empty array.
*
* @experimental
*/
class DbalTableJoin
{
/** @var literal-string */
public readonly string $toExpression;

/** @var array<mixed> */
public readonly array $toArgs;

/** @var literal-string */
public readonly string $toAlias;

/** @var literal-string */
public readonly string $onExpression;

/** @var array<mixed> */
public readonly array $onArgs;

public readonly IConventions $conventions;


/**
* @param array<mixed> $toArgs
* @param array<mixed> $onArgs
* @param literal-string $toExpression
* @param array<mixed> $toArgs
* @param literal-string $toAlias
* @param literal-string $onExpression
* @param array<mixed> $onArgs
* @param list<string> $primaryKeys
*/
public function __construct(
string $toExpression,
array $toArgs,
string $toAlias,
string $onExpression,
array $onArgs,
IConventions $conventions,
public readonly string $toExpression,
public readonly array $toArgs,
public readonly string $toAlias,
public readonly string $onExpression,
public readonly array $onArgs,
public readonly array $primaryKeys = [],
)
{
$this->toExpression = $toExpression;
$this->toArgs = $toArgs;
$this->toAlias = $toAlias;
$this->onExpression = $onExpression;
$this->onArgs = $onArgs;
$this->conventions = $conventions;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Collection/Helpers/DbalQueryBuilderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function mergeJoins(string $dbalModifier, array $joins): array
toAlias: $first->toAlias,
onExpression: $dbalModifier,
onArgs: [$args],
conventions: $first->conventions,
primaryKeys: $first->primaryKeys,
);
}
}
Expand Down

0 comments on commit 1e3ec30

Please sign in to comment.