Skip to content

Commit

Permalink
feat: support defining MySQLi flags
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Sep 14, 2024
1 parent 6a3659d commit f61a921
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/ORM/Connect/MySQLiConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,20 @@ 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 = Config::inst()->get(MySQLDatabase::class, 'socket');
$flags = Config::inst()->get(MySQLDatabase::class, 'flags');

$flags = is_array($flags) ? array_reduce($flags, function ($carry, $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
// The alternative is not ideal, throw a notice-level error
} else {
user_error(
'mysqlnd PHP library is not available, numeric values will be fetched from the DB as strings',
Expand Down Expand Up @@ -117,7 +123,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 +134,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

0 comments on commit f61a921

Please sign in to comment.