Skip to content
Closed
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
21 changes: 21 additions & 0 deletions src/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ abstract class DatabaseDriver implements DatabaseInterface, DispatcherAwareInter
*/
protected $options;

/**
* True if the database engine supports the ROW_NUMBER() window function.
*
* @var boolean
* @since __DEPLOY_VERSION__
*/
protected $rownum = true;

/**
* The current SQL statement to execute.
*
Expand Down Expand Up @@ -1040,6 +1048,19 @@ public function getIterator($column = null, $class = \stdClass::class)
*/
abstract public function getTableCreate($tables);

/**
* Determine whether or not the database engine supports the ROW_NUMBER() window function.
*
* @return boolean True if the database engine supports supports the ROW_NUMBER()
* window function, false if not.
*
* @since __DEPLOY_VERSION__
*/
public function hasRowNumberSupport(): bool
{
return $this->rownum;
}

/**
* Determine whether or not the database engine supports UTF-8 character encoding.
*
Expand Down
10 changes: 10 additions & 0 deletions src/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ public function getTableList();
*/
public function getVersion();

/**
* Determine whether or not the database engine supports the ROW_NUMBER() window function.
*
* @return boolean True if the database engine supports supports the ROW_NUMBER()
* window function, false if not.
*
* @since __DEPLOY_VERSION__
*/
public function hasRowNumberSupport(): bool;

/**
* Determine whether or not the database engine supports UTF-8 character encoding.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Mysql/MysqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ public function connect()

$this->mariadb = stripos($serverVersion, 'mariadb') !== false;

// The ROW_NUMBER() window function is supported on MariaDB >= 10.2.0 and MySQL >= 8.0.0
$this->rownum
= $this->mariadb && version_compare($serverVersion, '10.2.0', '>=')
|| !$this->mariadb && version_compare($serverVersion, '8.0.0', '>=');

if ($this->utf8mb4) {
// At this point we know the client supports utf8mb4. Now we must check if the server supports utf8mb4 as well.
$this->utf8mb4 = version_compare($serverVersion, '5.5.3', '>=');
Expand Down
7 changes: 7 additions & 0 deletions src/Mysqli/MysqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,13 @@ public function connect()

$this->mariadb = stripos($this->connection->server_info, 'mariadb') !== false;

$serverVersion = $this->getVersion();

// The ROW_NUMBER() window function is supported on MariaDB >= 10.2.0 and MySQL >= 8.0.0
$this->rownum
= $this->mariadb && version_compare($serverVersion, '10.2.0', '>=')
|| !$this->mariadb && version_compare($serverVersion, '8.0.0', '>=');

$this->utf8mb4 = $this->serverClaimsUtf8mb4Support();

// Set character sets (needed for MySQL 4.1.2+ and MariaDB).
Expand Down
9 changes: 9 additions & 0 deletions src/Query/MysqlQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,18 @@ public function findInSet($value, $set)
*
* @since 2.0.0
* @throws \RuntimeException
*
* @todo Remove this method when the database version requirements have been raised
* to >= 8.0.0 for MySQL and >= 10.2.0 for MariaDB so the ROW_NUMBER() window
* function can be used in any case.
*/
public function selectRowNumber($orderBy, $orderColumnAlias)
{
// Use parent method with ROW_NUMBER() window function if supported by the database engine.
if ($this->db->hasRowNumberSupport()) {
return parent::selectRowNumber($orderBy, $orderColumnAlias);
}

$this->validateRowNumber($orderBy, $orderColumnAlias);

return $this->select("(SELECT @rownum := @rownum + 1 FROM (SELECT @rownum := 0) AS r) AS $orderColumnAlias");
Expand Down