Skip to content

Commit

Permalink
Fix subquery /w order but /wo limit (#1140)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Oct 14, 2023
1 parent 8e53675 commit 609ea70
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Persistence/Sql/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ protected function consume($expr, string $escapeMode = self::ESCAPE_PARAM)
$expressionParamBaseBackup = $expr->paramBase;
try {
$expr->paramBase = $this->renderParamBase;
[$sql, $params] = $expr->render();
[$sql, $params] = $expr->wrapInParentheses
? $expr->renderNested()
: $expr->render();
foreach ($params as $k => $v) {
$this->renderParams[$k] = $v;
}
Expand All @@ -209,11 +211,6 @@ protected function consume($expr, string $escapeMode = self::ESCAPE_PARAM)
$expr->paramBase = $expressionParamBaseBackup;
}

// wrap in parentheses if expression requires so
if ($expr->wrapInParentheses) {
$sql = '(' . $sql . ')';
}

return $sql;
}

Expand Down Expand Up @@ -438,6 +435,16 @@ public function render(): array
}
}

/**
* @return array{string, array<string, mixed>}
*/
protected function renderNested(): array
{
[$sql, $params] = $this->render();

return ['(' . $sql . ')', $params];
}

/**
* Return formatted debug SQL query.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/Persistence/Sql/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,25 @@ public function render(): array
return parent::render();
}

protected function renderNested(): array
{
if (isset($this->args['order']) && !isset($this->args['limit'])) {
$orderOrig = $this->args['order'];
unset($this->args['order']);
} else {
$orderOrig = null;
}
try {
[$sql, $params] = parent::renderNested();
} finally {
if ($orderOrig !== null) {
$this->args['order'] = $orderOrig;
}
}

return [$sql, $params];
}

/**
* Switch template for this query. Determines what would be done
* on execute.
Expand Down
28 changes: 28 additions & 0 deletions tests/Persistence/Sql/WithDb/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,32 @@ public function testImportAndAutoincrement(): void
['id' => 102, 'f1' => 'M'],
], $m->export());
}

public function testSubqueryWithOrderAndLimit(): void
{
$subQuery = $this->q('employee');
$query = $this->q($subQuery, 't')->field('name')->order('name');

self::assertSame(
[['name' => 'Charlie'], ['name' => 'Harry'], ['name' => 'Jack'], ['name' => 'Oliver']],
$query->getRows()
);

// subquery /w limit but /wo order
$subQuery->limit(2);
self::assertCount(2, $query->getRows());

$subQuery->order('surname', true);
self::assertSame(
[['name' => 'Harry'], ['name' => 'Jack']],
$query->getRows()
);

// subquery /w order but /wo limit
$subQuery->args['limit'] = null;
self::assertSame(
[['name' => 'Charlie'], ['name' => 'Harry'], ['name' => 'Jack'], ['name' => 'Oliver']],
$query->getRows()
);
}
}

0 comments on commit 609ea70

Please sign in to comment.