Skip to content

Commit

Permalink
Merge branch 'refs/heads/2.x' into fork/markinigor/2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jun 10, 2024
2 parents d238e89 + e6f7bd0 commit 2ad648d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
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 @@ -377,4 +375,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;
}
}
39 changes: 39 additions & 0 deletions tests/Database/Functional/Driver/Common/Query/SelectQueryTest.php
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

0 comments on commit 2ad648d

Please sign in to comment.