Skip to content

Commit

Permalink
Merge pull request #3769 from michalsn/fix/sqlite_rand
Browse files Browse the repository at this point in the history
Fix orderBy() random for SQLite3
  • Loading branch information
paulbalandan authored Oct 15, 2020
2 parents 29beadf + 7c2a714 commit 3cffb41
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
9 changes: 9 additions & 0 deletions system/Database/SQLite3/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ class Builder extends BaseBuilder
*/
protected $canLimitWhereUpdates = false;

/**
* ORDER BY random keyword
*
* @var array
*/
protected $randomKeyword = [
'RANDOM()',
];

/**
* @var array
*/
Expand Down
11 changes: 0 additions & 11 deletions system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,6 @@ class Connection extends BaseConnection implements ConnectionInterface

//--------------------------------------------------------------------

/**
* ORDER BY random keyword
*
* @var array
*/
protected $_random_keyword = [
'RANDOM()',
];

//--------------------------------------------------------------------

/**
* Connect to the database.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/system/Database/Live/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,24 @@ public function testMultipleOrderValues()

//--------------------------------------------------------------------

public function testOrderRandom()
{
$sql = $this->db->table('job')
->orderBy('name', 'random')
->getCompiledSelect();

$key = 'RANDOM()';

if ($this->db->DBDriver === 'MySQLi')
{
$key = 'RAND()';
}

$expected = 'SELECT * FROM ' . $this->db->protectIdentifiers('job', true) . ' ORDER BY ' . $key;

$this->assertEquals($expected, str_replace("\n", ' ', $sql));
}

//--------------------------------------------------------------------

}

1 comment on commit 3cffb41

@nc03061981
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @paulbalandan

Please sign in to comment.