Browse Source

Merge pull request #14719 from cakephp/4.1-driver

Remove code duplication.
Mark Story 5 years ago
parent
commit
bf2cb75566

+ 23 - 0
src/Database/Driver.php

@@ -75,6 +75,13 @@ abstract class Driver implements DriverInterface
     protected $supportsCTEs = null;
 
     /**
+     * The server version
+     *
+     * @var string|null
+     */
+    protected $_version;
+
+    /**
      * Constructor
      *
      * @param array $config The configuration for the driver.
@@ -137,6 +144,22 @@ abstract class Driver implements DriverInterface
     {
         /** @psalm-suppress PossiblyNullPropertyAssignmentValue */
         $this->_connection = null;
+        $this->_version = null;
+    }
+
+    /**
+     * Returns connected server version.
+     *
+     * @return string
+     */
+    public function version(): string
+    {
+        if ($this->_version === null) {
+            $this->connect();
+            $this->_version = (string)$this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
+        }
+
+        return $this->_version;
     }
 
     /**

+ 3 - 25
src/Database/Driver/Mysql.php

@@ -62,13 +62,6 @@ class Mysql extends Driver
     protected $_schemaDialect;
 
     /**
-     * The server version
-     *
-     * @var string
-     */
-    protected $_version;
-
-    /**
      * Whether or not the server supports native JSON
      *
      * @var bool|null
@@ -231,21 +224,6 @@ class Mysql extends Driver
     }
 
     /**
-     * Returns connected server version.
-     *
-     * @return string
-     */
-    public function getVersion(): string
-    {
-        if ($this->_version === null) {
-            $this->connect();
-            $this->_version = (string)$this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
-        }
-
-        return $this->_version;
-    }
-
-    /**
      * Returns true if the server supports common table expressions.
      *
      * @return bool
@@ -253,7 +231,7 @@ class Mysql extends Driver
     public function supportsCTEs(): bool
     {
         if ($this->supportsCTEs === null) {
-            $this->supportsCTEs = version_compare($this->getVersion(), '8.0.0', '>=');
+            $this->supportsCTEs = version_compare($this->version(), '8.0.0', '>=');
         }
 
         return $this->supportsCTEs;
@@ -267,7 +245,7 @@ class Mysql extends Driver
     public function supportsNativeJson(): bool
     {
         if ($this->_supportsNativeJson === null) {
-            $this->_supportsNativeJson = version_compare($this->getVersion(), '5.7.0', '>=');
+            $this->_supportsNativeJson = version_compare($this->version(), '5.7.0', '>=');
         }
 
         return $this->_supportsNativeJson;
@@ -281,7 +259,7 @@ class Mysql extends Driver
     public function supportsWindowFunctions(): bool
     {
         if ($this->_supportsWindowFunctions === null) {
-            $this->_supportsWindowFunctions = version_compare($this->getVersion(), '8.0.0', '>=');
+            $this->_supportsWindowFunctions = version_compare($this->version(), '8.0.0', '>=');
         }
 
         return $this->_supportsWindowFunctions;

+ 2 - 24
src/Database/Driver/Sqlite.php

@@ -64,13 +64,6 @@ class Sqlite extends Driver
     protected $_schemaDialect;
 
     /**
-     * The connected server version.
-     *
-     * @var string
-     */
-    protected $_version;
-
-    /**
      * Whether or not the connected server supports window functions.
      *
      * @var bool|null
@@ -308,21 +301,6 @@ class Sqlite extends Driver
     }
 
     /**
-     * Returns connected server version.
-     *
-     * @return string
-     */
-    public function getVersion(): string
-    {
-        if ($this->_version === null) {
-            $this->connect();
-            $this->_version = (string)$this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
-        }
-
-        return $this->_version;
-    }
-
-    /**
      * Returns true if the server supports common table expressions.
      *
      * @return bool
@@ -330,7 +308,7 @@ class Sqlite extends Driver
     public function supportsCTEs(): bool
     {
         if ($this->supportsCTEs === null) {
-            $this->supportsCTEs = version_compare($this->getVersion(), '3.8.3', '>=');
+            $this->supportsCTEs = version_compare($this->version(), '3.8.3', '>=');
         }
 
         return $this->supportsCTEs;
@@ -344,7 +322,7 @@ class Sqlite extends Driver
     public function supportsWindowFunctions(): bool
     {
         if ($this->_supportsWindowFunctions === null) {
-            $this->_supportsWindowFunctions = version_compare($this->getVersion(), '3.25.0', '>=');
+            $this->_supportsWindowFunctions = version_compare($this->version(), '3.25.0', '>=');
         }
 
         return $this->_supportsWindowFunctions;

+ 0 - 12
src/Database/Driver/Sqlserver.php

@@ -287,18 +287,6 @@ class Sqlserver extends Driver
     }
 
     /**
-     * Get the version of SQLserver we are connected to.
-     *
-     * @return string
-     */
-    protected function version(): string
-    {
-        $this->connect();
-
-        return $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
-    }
-
-    /**
      * @inheritDoc
      */
     protected function _selectQueryTranslator(Query $query): Query