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

fix: add parameter consideration when hashing the query part with orderBy #206

Merged
merged 3 commits into from
Jun 10, 2024
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
19 changes: 16 additions & 3 deletions src/Driver/CompilerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ protected function hashSelectQuery(QueryParameters $params, array $tokens): stri

$hash .= implode(',', $tokens['groupBy']);

foreach ($tokens['orderBy'] as $order) {
$hash .= $order[0] . $order[1];
}
$hash .= $this->hashOrderBy($params, $tokens['orderBy']);

$hash .= $this->compiler->hashLimit($params, $tokens);

Expand Down Expand Up @@ -355,4 +353,19 @@ private function hashParam(QueryParameters $params, ParameterInterface $param):

return '?';
}

private function hashOrderBy(QueryParameters $params, array $tokens): string
{
$hash = '';
foreach ($tokens as $order) {
if ($order[0] instanceof FragmentInterface) {
foreach ($order[0]->getTokens()['parameters'] as $param) {
$params->push($param);
}
}
$hash .= $order[0] . $order[1];
}

return $hash;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,45 @@ public function testMultipleOrderByFullySpecifiedAliasedAndPrefixed(): void
);
}

public function testOrderByWithExpressionAndParameter(): void
{
$select = $this->database
->select()
->from('permissions')
->orderBy(new Expression('role = ?', '*'), 'DESC');

$this->assertSameQuery('SELECT * FROM {permissions} ORDER BY {role} = ? DESC', $select);
$this->assertSameParameters(['*'], $select);
}

public function testOrderByWithFragmentAndParameter(): void
{
$select = $this->database
->select()
->from('permissions')
->orderBy(new Fragment('"role" = ?', '*'), 'DESC');

$this->assertSameQuery('SELECT * FROM {permissions} ORDER BY "role" = ? DESC', $select);
$this->assertSameParameters(['*'], $select);
}

public function testOrderByWithFragmentAndParameterInArray(): void
{
$select = $this->database
->select()
->from('permissions')
->orderBy([
new Fragment('"role" = ? DESC', '*'),
'read' => 'ASC',
]);

$this->assertSameQuery(
'SELECT * FROM {permissions} ORDER BY "role" = ? DESC, {read} ASC',
$select
);
$this->assertSameParameters(['*'], $select);
}

//Group By

public function testGroupBy(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
mysql_latest:
image: mysql:latest
restart: always
command: --default-authentication-plugin=mysql_native_password
command: --mysql-native-password=ON
ports:
- "13306:3306"
environment:
Expand Down
Loading