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

Fixed an issue where an unnecessary prefix was given when the random number was a column. #5179

Merged
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
1 change: 1 addition & 0 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,7 @@ public function orderBy(string $orderBy, string $direction = '', ?bool $escape =
if ($direction === 'RANDOM') {
$direction = '';
$orderBy = ctype_digit($orderBy) ? sprintf($this->randomKeyword[1], $orderBy) : $this->randomKeyword[0];
$escape = false;
} elseif ($direction !== '') {
$direction = in_array($direction, ['ASC', 'DESC'], true) ? ' ' . $direction : '';
}
Expand Down
13 changes: 13 additions & 0 deletions tests/system/Database/Builder/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ public function testOrderRandom()

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}

public function testOrderRandomWithRandomColumn()
{
$this->db->setPrefix('fail_');
$builder = new BaseBuilder('user', $this->db);
$this->setPrivateProperty($builder, 'randomKeyword', ['"SYSTEM"."RANDOM"']);

$builder->orderBy('name', 'random');

$expectedSQL = 'SELECT * FROM "fail_user" ORDER BY "SYSTEM"."RANDOM"';

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
}
}