Skip to content

[3.x] Get database server version from database so it works also with ProxySQL #312

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

Draft
wants to merge 6 commits into
base: 3.x-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
26 changes: 18 additions & 8 deletions src/Mysql/MysqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ class MysqlDriver extends PdoDriver implements UTF8MB4SupportInterface
*/
public $charset = 'utf8';

/**
* The database server version.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $serverVersion;

/**
* Constructor.
*
Expand Down Expand Up @@ -491,24 +499,26 @@ public function getTableList()
}

/**
* Get the version of the database connector.
* Get the version of the database server.
*
* @return string The database connector version.
* @return string The database server version.
*
* @since 2.0.0
*/
public function getVersion()
{
$this->connect();
if (!isset($this->serverVersion)) {
$this->connect();

$version = $this->getOption(\PDO::ATTR_SERVER_VERSION);
$this->serverVersion = $this->setQuery('SELECT @@version;')->loadResult();

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

return $version;
return $this->serverVersion;
}

/**
Expand Down
39 changes: 26 additions & 13 deletions src/Mysqli/MysqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ class MysqliDriver extends DatabaseDriver implements UTF8MB4SupportInterface
*/
protected static $dbMinMariadb = '10.0';

/**
* The database server version.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $serverVersion;

/**
* Constructor.
*
Expand Down Expand Up @@ -262,9 +270,11 @@ public function connect()
$this->select($this->options['database']);
}

$this->mariadb = stripos($this->connection->server_info, 'mariadb') !== false;
$serverVersion = $this->getVersion();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not happy with this call on every connect, can we lazy load the mariadb and utf8mb4 variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be out of scope of my PR, I think. The change here is equal to what we already have in the MySQL PDO driver.


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

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

// Set character sets (needed for MySQL 4.1.2+ and MariaDB).
$this->utf = $this->setUtf();
Expand Down Expand Up @@ -586,22 +596,26 @@ public function getTableList()
}

/**
* Get the version of the database connector.
* Get the version of the database server.
*
* @return string The database connector version.
* @return string The database server version.
*
* @since 1.0
*/
public function getVersion()
{
$this->connect();
if (!isset($this->serverVersion)) {
$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);
$this->serverVersion = $this->setQuery('SELECT @@version;')->loadResult();

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

return $this->connection->server_info;
return $this->serverVersion;
}

/**
Expand Down Expand Up @@ -994,16 +1008,15 @@ public function unlockTables()
*
* @since 1.4.0
*/
private function serverClaimsUtf8mb4Support()
private function serverClaimsUtf8mb4Support($serverVersion)
{
$client_version = mysqli_get_client_info();
$server_version = $this->getVersion();

if (version_compare($server_version, '5.5.3', '<')) {
if (version_compare($serverVersion, '5.5.3', '<')) {
return false;
}

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

Expand Down