mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW Some minor refactoring of the PDO and MySQLi connectors
Some small performance optimisations (not using callables in loops, switch strval for string casting), Config call updates and replace call_user_func_array with direct variadic call. Also removes some redundant else statements after returns.
This commit is contained in:
parent
ee23a70ae6
commit
437e53f2fe
@ -73,8 +73,8 @@ class MySQLiConnector extends DBConnector
|
||||
$selectedDB = ($selectDB && !empty($parameters['database'])) ? $parameters['database'] : null;
|
||||
|
||||
// Connection charset and collation
|
||||
$connCharset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'connection_charset');
|
||||
$connCollation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'connection_collation');
|
||||
$connCharset = Config::inst()->get(MySQLDatabase::class, 'connection_charset');
|
||||
$connCollation = Config::inst()->get(MySQLDatabase::class, 'connection_collation');
|
||||
|
||||
$this->dbConn = mysqli_init();
|
||||
|
||||
@ -186,7 +186,8 @@ class MySQLiConnector extends DBConnector
|
||||
$types = '';
|
||||
$values = array();
|
||||
$blobs = array();
|
||||
for ($index = 0; $index < count($parameters); $index++) {
|
||||
$parametersCount = count($parameters);
|
||||
for ($index = 0; $index < $parametersCount; $index++) {
|
||||
$value = $parameters[$index];
|
||||
$phpType = gettype($value);
|
||||
|
||||
@ -247,12 +248,13 @@ class MySQLiConnector extends DBConnector
|
||||
// Because mysqli_stmt::bind_param arguments must be passed by reference
|
||||
// we need to do a bit of hackery
|
||||
$boundNames = [];
|
||||
for ($i = 0; $i < count($parameters); $i++) {
|
||||
$parametersCount = count($parameters);
|
||||
for ($i = 0; $i < $parametersCount; $i++) {
|
||||
$boundName = "param$i";
|
||||
$$boundName = $parameters[$i];
|
||||
$boundNames[] = &$$boundName;
|
||||
}
|
||||
call_user_func_array(array($statement, 'bind_param'), $boundNames);
|
||||
$statement->bind_param(...$boundNames);
|
||||
}
|
||||
|
||||
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR)
|
||||
@ -293,10 +295,10 @@ class MySQLiConnector extends DBConnector
|
||||
$metaData = $statement->result_metadata();
|
||||
if ($metaData) {
|
||||
return new MySQLStatement($statement, $metaData);
|
||||
} else {
|
||||
// Replicate normal behaviour of ->query() on non-select calls
|
||||
return new MySQLQuery($this, true);
|
||||
}
|
||||
|
||||
// Replicate normal behaviour of ->query() on non-select calls
|
||||
return new MySQLQuery($this, true);
|
||||
}
|
||||
|
||||
public function selectDatabase($name)
|
||||
@ -304,9 +306,9 @@ class MySQLiConnector extends DBConnector
|
||||
if ($this->dbConn->select_db($name)) {
|
||||
$this->databaseName = $name;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getSelectedDatabase()
|
||||
|
@ -106,7 +106,7 @@ class PDOConnector extends DBConnector
|
||||
*/
|
||||
public static function is_emulate_prepare()
|
||||
{
|
||||
return Config::inst()->get('SilverStripe\ORM\Connect\PDOConnector', 'emulate_prepare');
|
||||
return self::config()->get('emulate_prepare');
|
||||
}
|
||||
|
||||
public function connect($parameters, $selectDB = false)
|
||||
@ -159,8 +159,8 @@ class PDOConnector extends DBConnector
|
||||
}
|
||||
|
||||
// Connection charset and collation
|
||||
$connCharset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'connection_charset');
|
||||
$connCollation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'connection_collation');
|
||||
$connCharset = Config::inst()->get(MySQLDatabase::class, 'connection_charset');
|
||||
$connCollation = Config::inst()->get(MySQLDatabase::class, 'connection_collation');
|
||||
|
||||
// Set charset if given and not null. Can explicitly set to empty string to omit
|
||||
if (!in_array($parameters['driver'], ['sqlsrv', 'pgsql'])) {
|
||||
@ -325,7 +325,8 @@ class PDOConnector extends DBConnector
|
||||
public function bindParameters(PDOStatement $statement, $parameters)
|
||||
{
|
||||
// Bind all parameters
|
||||
for ($index = 0; $index < count($parameters); $index++) {
|
||||
$parameterCount = count($parameters);
|
||||
for ($index = 0; $index < $parameterCount; $index++) {
|
||||
$value = $parameters[$index];
|
||||
$phpType = gettype($value);
|
||||
|
||||
@ -338,7 +339,7 @@ class PDOConnector extends DBConnector
|
||||
// Check type of parameter
|
||||
$type = $this->getPDOParamType($phpType);
|
||||
if ($type === PDO::PARAM_STR) {
|
||||
$value = strval($value);
|
||||
$value = (string) $value;
|
||||
}
|
||||
|
||||
// Bind this value
|
||||
@ -388,7 +389,6 @@ class PDOConnector extends DBConnector
|
||||
// Ensure statement is closed
|
||||
if ($statement) {
|
||||
$statement->closeCursor();
|
||||
unset($statement);
|
||||
}
|
||||
|
||||
// Report any errors
|
||||
|
Loading…
Reference in New Issue
Block a user