Skip to content

Commit

Permalink
fix: forbid sharded queries from self-joining sharded tables
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Aug 15, 2024
1 parent bb37e40 commit d0c7c1a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ShardedQueryBuilder extends ExtendedQueryBuilder {
private ?int $offset = null;
/** @var array{column: string, order: string}[] */
private array $sortList = [];
private string $mainTable = '';

public function __construct(
IQueryBuilder $builder,
Expand Down Expand Up @@ -175,6 +176,7 @@ public function values(array $values) {
}

private function actOnTable(string $table): void {
$this->mainTable = $table;
foreach ($this->shardDefinitions as $shardDefinition) {
if ($shardDefinition->hasTable($table)) {
$this->shardDefinition = $shardDefinition;
Expand Down Expand Up @@ -213,6 +215,9 @@ public function delete($delete = null, $alias = null) {

private function checkJoin(string $table): void {
if ($this->shardDefinition) {
if ($table === $this->mainTable) {
throw new InvalidShardedQueryException("Sharded query on {$this->mainTable} isn't allowed to join on itself");
}
if (!$this->shardDefinition->hasTable($table)) {
throw new InvalidShardedQueryException("Sharded query on {$this->shardDefinition->table} isn't allowed to join on $table");
}
Expand Down Expand Up @@ -270,7 +275,7 @@ public function orderBy($sort, $order = null) {
return parent::orderBy($sort, $order);
}

private function registerOrder(string $column, string $order) {
private function registerOrder(string $column, string $order): void {
// handle `mime + 0` and similar by just sorting on the first part of the expression
[$column] = explode(' ', $column);
$column = trim($column, '`');
Expand Down

0 comments on commit d0c7c1a

Please sign in to comment.