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

[2.0] [WiP] MariaDB support for MySQLi and MySQL (PDO) drivers #166

Merged
merged 9 commits into from
Jun 20, 2019
2 changes: 1 addition & 1 deletion src/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ public function insertObject($table, &$object, $key = null)
*/
public function isMinimumVersion()
{
return version_compare($this->getVersion(), static::$dbMinimum) >= 0;
return version_compare($this->getVersion(), $this->getMinimum()) >= 0;
}

/**
Expand Down
60 changes: 59 additions & 1 deletion src/Mysql/MysqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ class MysqlDriver extends PdoDriver implements UTF8MB4SupportInterface
*/
protected $utf8mb4 = false;

/**
* True if the database engine is MariaDB.
*
* @var boolean
* @since __DEPLOY_VERSION__
*/
protected $mariadb = false;

/**
* The minimum supported database version.
*
Expand All @@ -63,6 +71,14 @@ class MysqlDriver extends PdoDriver implements UTF8MB4SupportInterface
*/
protected static $dbMinimum = '5.6';

/**
* The minimum supported MariaDB database version.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected static $dbMinMariadb = '10.0';

/**
* Constructor.
*
Expand Down Expand Up @@ -139,12 +155,20 @@ public function connect()
parent::connect();
}

$serverVersion = $this->getVersion();

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

if ($this->utf8mb4)
{
// At this point we know the client supports utf8mb4. Now we must check if the server supports utf8mb4 as well.
$serverVersion = $this->getVersion();
$this->utf8mb4 = version_compare($serverVersion, '5.5.3', '>=');

if ($this->mariadb && version_compare($server_version, '10.0.0', '<'))
{
$this->utf8mb4 = false;
}

if (!$this->utf8mb4)
{
// Reconnect with the utf8 character set.
Expand Down Expand Up @@ -400,6 +424,40 @@ public function getTableList()
return $this->setQuery('SHOW TABLES')->loadColumn();
}

/**
* Get the version of the database connector.
*
* @return string The database connector version.
*
* @since __DEPLOY_VERSION__
*/
public function getVersion()
{
$this->connect();

$version = $this->getOption(\PDO::ATTR_SERVER_VERSION);

if (stripos($version, 'mariadb') !== false)
{
// MariaDB: Strip off any leading '5.5.5-', if present
return preg_replace('/^5\.5\.5-/', '', $version);
}

return $version;
}

/**
* Get the minimum supported database version.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
public function getMinimum()
{
return $this->mariadb ? static::$dbMinMariadb : static::$dbMinimum;
}

/**
* Determine whether the database engine support the UTF-8 Multibyte (utf8mb4) character encoding.
*
Expand Down
47 changes: 44 additions & 3 deletions src/Mysqli/MysqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,29 @@ class MysqliDriver extends DatabaseDriver implements UTF8MB4SupportInterface
protected $utf8mb4 = false;

/**
* The minimum supported database version.
* True if the database engine is MariaDB.
*
* @var boolean
* @since __DEPLOY_VERSION__
*/
protected $mariadb = false;

/**
* The minimum supported MySQL database version.
*
* @var string
* @since 1.0
*/
protected static $dbMinimum = '5.6';

/**
* The minimum supported MariaDB database version.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected static $dbMinMariadb = '10.0';

/**
* Constructor.
*
Expand Down Expand Up @@ -203,7 +219,7 @@ public function connect()
if (!$connected)
{
throw new ConnectionFailureException(
'Could not connect to MySQL: ' . $this->connection->connect_error,
'Could not connect to database: ' . $this->connection->connect_error,
$this->connection->connect_errno
);
}
Expand All @@ -223,9 +239,11 @@ public function connect()
$this->select($this->options['database']);
}

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

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

// Set charactersets (needed for MySQL 4.1.2+).
// Set charactersets (needed for MySQL 4.1.2+ and MariaDB).
$this->utf = $this->setUtf();

$this->dispatchEvent(new ConnectionEvent(DatabaseEvents::POST_CONNECT, $this));
Expand Down Expand Up @@ -529,9 +547,27 @@ public function getVersion()
{
$this->connect();

if ($this->mariadb)
{
// MariaDB: Strip off any leading '5.5.5-', if present
return preg_replace('/^5\.5\.5-/', '', $this->connection->server_info);
richard67 marked this conversation as resolved.
Show resolved Hide resolved
}

return $this->connection->server_info;
}

/**
* Get the minimum supported database version.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
public function getMinimum()
{
return $this->mariadb ? static::$dbMinMariadb : static::$dbMinimum;
}

/**
* Determine whether the database engine support the UTF-8 Multibyte (utf8mb4) character encoding.
*
Expand Down Expand Up @@ -940,6 +976,11 @@ private function serverClaimsUtf8mb4Support()
return false;
}

if ($this->mariadb && version_compare($server_version, '10.0.0', '<'))
{
return false;
}

if (strpos($client_version, 'mysqlnd') !== false)
{
$client_version = preg_replace('/^\D+([\d.]+).*/', '$1', $client_version);
Expand Down