From 20e541d64bd0723b6cfc46bf4aa73b913f47822b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 4 Jun 2019 22:25:14 +0200 Subject: [PATCH] Support custom database driver options --- wcfsetup/install/files/lib/system/WCF.class.php | 3 ++- .../files/lib/system/database/Database.class.php | 12 ++++++++++-- .../lib/system/database/MySQLDatabase.class.php | 9 +++------ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/wcfsetup/install/files/lib/system/WCF.class.php b/wcfsetup/install/files/lib/system/WCF.class.php index dfb45d3915..16d26a9071 100644 --- a/wcfsetup/install/files/lib/system/WCF.class.php +++ b/wcfsetup/install/files/lib/system/WCF.class.php @@ -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); } /** diff --git a/wcfsetup/install/files/lib/system/database/Database.class.php b/wcfsetup/install/files/lib/system/database/Database.class.php index 0d6f6b73f9..1ee45f5aef 100644 --- a/wcfsetup/install/files/lib/system/database/Database.class.php +++ b/wcfsetup/install/files/lib/system/database/Database.class.php @@ -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; diff --git a/wcfsetup/install/files/lib/system/database/MySQLDatabase.class.php b/wcfsetup/install/files/lib/system/database/MySQLDatabase.class.php index 50b6009441..b8e1630ac8 100644 --- a/wcfsetup/install/files/lib/system/database/MySQLDatabase.class.php +++ b/wcfsetup/install/files/lib/system/database/MySQLDatabase.class.php @@ -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 -- 2.20.1