From 60d8a233091399ca06b47eaf9381e0f252f28154 Mon Sep 17 00:00:00 2001 From: Richard Fath Date: Sat, 31 Aug 2024 18:29:31 +0200 Subject: [PATCH 1/5] Get database server version from DB and not from PDO --- src/Mysql/MysqlDriver.php | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Mysql/MysqlDriver.php b/src/Mysql/MysqlDriver.php index c41517d0..4cf0fc1b 100644 --- a/src/Mysql/MysqlDriver.php +++ b/src/Mysql/MysqlDriver.php @@ -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. * @@ -196,15 +204,21 @@ public function connect() parent::connect(); } - $serverVersion = $this->getVersion(); + // Get database server version + $this->serverVersion = $this->setQuery('SELECT version();')->loadResult(); + + $this->mariadb = stripos($this->serverVersion, 'mariadb') !== false; - $this->mariadb = stripos($serverVersion, 'mariadb') !== false; + if ($this->mariadb) { + // MariaDB: Strip off any leading '5.5.5-', if present + $this->serverVersion = preg_replace('/^5\.5\.5-/', '', $this->serverVersion); + } 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', '>='); + $this->utf8mb4 = version_compare($this->serverVersion, '5.5.3', '>='); - if ($this->mariadb && version_compare($serverVersion, '10.0.0', '<')) { + if ($this->mariadb && version_compare($this->serverVersion, '10.0.0', '<')) { $this->utf8mb4 = false; } @@ -491,9 +505,9 @@ 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 */ @@ -501,14 +515,7 @@ 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; + return $this->serverVersion; } /** From c02d72d14ba0f6c8d45f891d2e96cba7c7958622 Mon Sep 17 00:00:00 2001 From: Richard Fath Date: Sun, 8 Sep 2024 15:55:20 +0200 Subject: [PATCH 2/5] Use cached result and move logic to getVersion() --- src/Mysql/MysqlDriver.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Mysql/MysqlDriver.php b/src/Mysql/MysqlDriver.php index 4cf0fc1b..06d96a6b 100644 --- a/src/Mysql/MysqlDriver.php +++ b/src/Mysql/MysqlDriver.php @@ -204,15 +204,9 @@ public function connect() parent::connect(); } - // Get database server version - $this->serverVersion = $this->setQuery('SELECT version();')->loadResult(); + $serverVersion = $this->getVersion(); - $this->mariadb = stripos($this->serverVersion, 'mariadb') !== false; - - if ($this->mariadb) { - // MariaDB: Strip off any leading '5.5.5-', if present - $this->serverVersion = preg_replace('/^5\.5\.5-/', '', $this->serverVersion); - } + $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. @@ -513,7 +507,16 @@ public function getTableList() */ public function getVersion() { - $this->connect(); + if (!isset($this->serverVersion)) { + $this->connect(); + + $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->serverVersion; } From 962be24bbf03af489c3262298822dde806e77c5e Mon Sep 17 00:00:00 2001 From: Richard Fath Date: Sun, 8 Sep 2024 15:55:41 +0200 Subject: [PATCH 3/5] Handle MySQLi, too --- src/Mysqli/MysqliDriver.php | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Mysqli/MysqliDriver.php b/src/Mysqli/MysqliDriver.php index dbc6977c..ddea3cb1 100644 --- a/src/Mysqli/MysqliDriver.php +++ b/src/Mysqli/MysqliDriver.php @@ -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. * @@ -262,9 +270,11 @@ public function connect() $this->select($this->options['database']); } - $this->mariadb = stripos($this->connection->server_info, 'mariadb') !== false; + $serverVersion = $this->getVersion(); - $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(); @@ -594,14 +604,18 @@ public function getTableList() */ 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; } /** @@ -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; } From e21178947e6e3e8f47e6e9e6b245bc726fe13946 Mon Sep 17 00:00:00 2001 From: Richard Fath Date: Sun, 8 Sep 2024 15:56:52 +0200 Subject: [PATCH 4/5] Revert unnecessary changes --- src/Mysql/MysqlDriver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mysql/MysqlDriver.php b/src/Mysql/MysqlDriver.php index 06d96a6b..b7a747ef 100644 --- a/src/Mysql/MysqlDriver.php +++ b/src/Mysql/MysqlDriver.php @@ -210,9 +210,9 @@ public function connect() 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($this->serverVersion, '5.5.3', '>='); + $this->utf8mb4 = version_compare($serverVersion, '5.5.3', '>='); - if ($this->mariadb && version_compare($this->serverVersion, '10.0.0', '<')) { + if ($this->mariadb && version_compare($serverVersion, '10.0.0', '<')) { $this->utf8mb4 = false; } From 2b3f51f4ab18f230d6f74156d33be150fd271fb1 Mon Sep 17 00:00:00 2001 From: Richard Fath Date: Sun, 8 Sep 2024 16:06:49 +0200 Subject: [PATCH 5/5] Change docblock comment for MySQLi, too --- src/Mysqli/MysqliDriver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mysqli/MysqliDriver.php b/src/Mysqli/MysqliDriver.php index ddea3cb1..3273473a 100644 --- a/src/Mysqli/MysqliDriver.php +++ b/src/Mysqli/MysqliDriver.php @@ -596,9 +596,9 @@ 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 */