Support custom database driver options
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 4 Jun 2019 20:25:14 +0000 (22:25 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Sun, 9 Jun 2019 21:25:16 +0000 (23:25 +0200)
wcfsetup/install/files/lib/system/WCF.class.php
wcfsetup/install/files/lib/system/database/Database.class.php
wcfsetup/install/files/lib/system/database/MySQLDatabase.class.php

index dfb45d3915fac3e1a5b129ec0785c3234b6827ff..16d26a9071bcf0f87f92aadcc31379e72cfc37d4 100644 (file)
@@ -345,10 +345,11 @@ class WCF {
                // get configuration
                $dbHost = $dbUser = $dbPassword = $dbName = '';
                $dbPort = 0;
+               $defaultDriverOptions = [];
                require(WCF_DIR.'config.inc.php');
                
                // create database connection
-               self::$dbObj = new MySQLDatabase($dbHost, $dbUser, $dbPassword, $dbName, $dbPort);
+               self::$dbObj = new MySQLDatabase($dbHost, $dbUser, $dbPassword, $dbName, $dbPort, false, false, $defaultDriverOptions);
        }
        
        /**
index 0d6f6b73f9620ca1a703a9abed714df9182d0a9e..1ee45f5aeff3a6ba543f222d0144df34fffd0738 100644 (file)
@@ -96,6 +96,12 @@ abstract class Database {
         */
        protected $tryToCreateDatabase = false;
        
+       /**
+        * default driver options passed to the PDO constructor
+        * @var array
+        */
+       protected $defaultDriverOptions = [];
+       
        /**
         * Creates a Database Object.
         * 
@@ -105,9 +111,10 @@ abstract class Database {
         * @param       string          $database               SQL database server database name
         * @param       integer         $port                   SQL database server port
         * @param       boolean         $failsafeTest
-        * @param       boolean         $tryToCreateDatabase
+        * @param       boolean         $tryToCreateDatabase
+        * @param       array           $defaultDriverOptions
         */
-       public function __construct($host, $user, $password, $database, $port, $failsafeTest = false, $tryToCreateDatabase = false) {
+       public function __construct($host, $user, $password, $database, $port, $failsafeTest = false, $tryToCreateDatabase = false, $defaultDriverOptions = []) {
                $this->host = $host;
                $this->port = $port;
                $this->user = $user;
@@ -115,6 +122,7 @@ abstract class Database {
                $this->database = $database;
                $this->failsafeTest = $failsafeTest;
                $this->tryToCreateDatabase = $tryToCreateDatabase;
+               $this->defaultDriverOptions = $defaultDriverOptions;
                
                if (defined('ENABLE_DEBUG_MODE') && ENABLE_DEBUG_MODE) {
                        $this->preparedStatementClassName = DebugPreparedStatement::class;
index 50b6009441c7a2405fa16d37112d86865e53e0ac..b8e1630ac8081edf396423a864f9185f0a40ab5e 100644 (file)
@@ -24,13 +24,10 @@ class MySQLDatabase extends Database {
                if (!$this->port) $this->port = 3306; // mysql default port
                
                try {
-                       $driverOptions = [
-                               \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'"
-                       ];
+                       $driverOptions = $this->defaultDriverOptions;
+                       $driverOptions[\PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES 'utf8mb4'";
                        if (!$this->failsafeTest) {
-                               $driverOptions = [
-                                       \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4', SESSION sql_mode = 'ANSI,ONLY_FULL_GROUP_BY,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'"
-                               ];
+                               $driverOptions[\PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES 'utf8mb4', SESSION sql_mode = 'ANSI,ONLY_FULL_GROUP_BY,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'";
                        }
                        
                        // disable prepared statement emulation since MySQL 5.1.17 is the minimum required version