From 5fa88663b0d4c95966767b67a96c99a4e4256267 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Sun, 15 Sep 2024 06:58:24 +1200 Subject: [PATCH] feat: support defining MySQLi flags --- src/ORM/Connect/MySQLiConnector.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ORM/Connect/MySQLiConnector.php b/src/ORM/Connect/MySQLiConnector.php index 2fbbd70c5..e480f5db9 100644 --- a/src/ORM/Connect/MySQLiConnector.php +++ b/src/ORM/Connect/MySQLiConnector.php @@ -6,6 +6,7 @@ use mysqli; use mysqli_sql_exception; use mysqli_stmt; use SilverStripe\Core\Config\Config; +use SilverStripe\Core\Environment; /** * Connector for MySQL using the MySQLi method @@ -81,15 +82,21 @@ class MySQLiConnector extends DBConnector // Connection charset and collation $connCharset = Config::inst()->get(MySQLDatabase::class, 'connection_charset'); $connCollation = Config::inst()->get(MySQLDatabase::class, 'connection_collation'); + $socket = Environment::getEnv('SS_DATABASE_SOCKET'); + $flags = Environment::getEnv('SS_DATABASE_FLAGS'); + + $flags = $flags ? array_reduce(explode(',', $flags), function ($carry, $item) { + $item = trim($item); + return $carry | constant($item); + }, 0) : $flags; $this->dbConn = mysqli_init(); // Use native types (MysqlND only) if (defined('MYSQLI_OPT_INT_AND_FLOAT_NATIVE')) { $this->dbConn->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true); - - // The alternative is not ideal, throw a notice-level error } else { + // The alternative is not ideal, throw a notice-level error user_error( 'mysqlnd PHP library is not available, numeric values will be fetched from the DB as strings', E_USER_NOTICE @@ -117,7 +124,9 @@ class MySQLiConnector extends DBConnector $parameters['username'], $parameters['password'], $selectedDB, - !empty($parameters['port']) ? $parameters['port'] : ini_get("mysqli.default_port") + !empty($parameters['port']) ? $parameters['port'] : ini_get("mysqli.default_port"), + $socket ?? null, + $flags ?? 0 ); if ($this->dbConn->connect_error) { @@ -126,8 +135,8 @@ class MySQLiConnector extends DBConnector // Set charset and collation if given and not null. Can explicitly set to empty string to omit $charset = isset($parameters['charset']) - ? $parameters['charset'] - : $connCharset; + ? $parameters['charset'] + : $connCharset; if (!empty($charset)) { $this->dbConn->set_charset($charset);