Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support defining MySQLi flags #11383

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/ORM/Connect/MySQLiConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use mysqli_sql_exception;
use mysqli_stmt;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Environment;

/**
* Connector for MySQL using the MySQLi method
Expand Down Expand Up @@ -81,15 +82,21 @@ public function connect($parameters, $selectDB = false)
// 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;
Comment on lines +88 to +91
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't super easy to read but I won't ask for you to change it now. Just something to think about for next time.


$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
Expand Down Expand Up @@ -117,7 +124,9 @@ public function connect($parameters, $selectDB = false)
$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) {
Expand All @@ -126,8 +135,8 @@ public function connect($parameters, $selectDB = false)

// 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);
Expand Down
Loading