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;
|
$selectedDB = ($selectDB && !empty($parameters['database'])) ? $parameters['database'] : null;
|
||||||
|
|
||||||
// Connection charset and collation
|
// Connection charset and collation
|
||||||
$connCharset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'connection_charset');
|
$connCharset = Config::inst()->get(MySQLDatabase::class, 'connection_charset');
|
||||||
$connCollation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'connection_collation');
|
$connCollation = Config::inst()->get(MySQLDatabase::class, 'connection_collation');
|
||||||
|
|
||||||
$this->dbConn = mysqli_init();
|
$this->dbConn = mysqli_init();
|
||||||
|
|
||||||
@ -186,7 +186,8 @@ class MySQLiConnector extends DBConnector
|
|||||||
$types = '';
|
$types = '';
|
||||||
$values = array();
|
$values = array();
|
||||||
$blobs = array();
|
$blobs = array();
|
||||||
for ($index = 0; $index < count($parameters); $index++) {
|
$parametersCount = count($parameters);
|
||||||
|
for ($index = 0; $index < $parametersCount; $index++) {
|
||||||
$value = $parameters[$index];
|
$value = $parameters[$index];
|
||||||
$phpType = gettype($value);
|
$phpType = gettype($value);
|
||||||
|
|
||||||
@ -247,12 +248,13 @@ class MySQLiConnector extends DBConnector
|
|||||||
// Because mysqli_stmt::bind_param arguments must be passed by reference
|
// Because mysqli_stmt::bind_param arguments must be passed by reference
|
||||||
// we need to do a bit of hackery
|
// we need to do a bit of hackery
|
||||||
$boundNames = [];
|
$boundNames = [];
|
||||||
for ($i = 0; $i < count($parameters); $i++) {
|
$parametersCount = count($parameters);
|
||||||
|
for ($i = 0; $i < $parametersCount; $i++) {
|
||||||
$boundName = "param$i";
|
$boundName = "param$i";
|
||||||
$$boundName = $parameters[$i];
|
$$boundName = $parameters[$i];
|
||||||
$boundNames[] = &$$boundName;
|
$boundNames[] = &$$boundName;
|
||||||
}
|
}
|
||||||
call_user_func_array(array($statement, 'bind_param'), $boundNames);
|
$statement->bind_param(...$boundNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR)
|
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR)
|
||||||
@ -293,20 +295,20 @@ class MySQLiConnector extends DBConnector
|
|||||||
$metaData = $statement->result_metadata();
|
$metaData = $statement->result_metadata();
|
||||||
if ($metaData) {
|
if ($metaData) {
|
||||||
return new MySQLStatement($statement, $metaData);
|
return new MySQLStatement($statement, $metaData);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
// Replicate normal behaviour of ->query() on non-select calls
|
// Replicate normal behaviour of ->query() on non-select calls
|
||||||
return new MySQLQuery($this, true);
|
return new MySQLQuery($this, true);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function selectDatabase($name)
|
public function selectDatabase($name)
|
||||||
{
|
{
|
||||||
if ($this->dbConn->select_db($name)) {
|
if ($this->dbConn->select_db($name)) {
|
||||||
$this->databaseName = $name;
|
$this->databaseName = $name;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSelectedDatabase()
|
public function getSelectedDatabase()
|
||||||
|
@ -106,7 +106,7 @@ class PDOConnector extends DBConnector
|
|||||||
*/
|
*/
|
||||||
public static function is_emulate_prepare()
|
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)
|
public function connect($parameters, $selectDB = false)
|
||||||
@ -159,8 +159,8 @@ class PDOConnector extends DBConnector
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connection charset and collation
|
// Connection charset and collation
|
||||||
$connCharset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'connection_charset');
|
$connCharset = Config::inst()->get(MySQLDatabase::class, 'connection_charset');
|
||||||
$connCollation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'connection_collation');
|
$connCollation = Config::inst()->get(MySQLDatabase::class, 'connection_collation');
|
||||||
|
|
||||||
// Set charset if given and not null. Can explicitly set to empty string to omit
|
// Set charset if given and not null. Can explicitly set to empty string to omit
|
||||||
if (!in_array($parameters['driver'], ['sqlsrv', 'pgsql'])) {
|
if (!in_array($parameters['driver'], ['sqlsrv', 'pgsql'])) {
|
||||||
@ -325,7 +325,8 @@ class PDOConnector extends DBConnector
|
|||||||
public function bindParameters(PDOStatement $statement, $parameters)
|
public function bindParameters(PDOStatement $statement, $parameters)
|
||||||
{
|
{
|
||||||
// Bind all parameters
|
// Bind all parameters
|
||||||
for ($index = 0; $index < count($parameters); $index++) {
|
$parameterCount = count($parameters);
|
||||||
|
for ($index = 0; $index < $parameterCount; $index++) {
|
||||||
$value = $parameters[$index];
|
$value = $parameters[$index];
|
||||||
$phpType = gettype($value);
|
$phpType = gettype($value);
|
||||||
|
|
||||||
@ -338,7 +339,7 @@ class PDOConnector extends DBConnector
|
|||||||
// Check type of parameter
|
// Check type of parameter
|
||||||
$type = $this->getPDOParamType($phpType);
|
$type = $this->getPDOParamType($phpType);
|
||||||
if ($type === PDO::PARAM_STR) {
|
if ($type === PDO::PARAM_STR) {
|
||||||
$value = strval($value);
|
$value = (string) $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind this value
|
// Bind this value
|
||||||
@ -388,7 +389,6 @@ class PDOConnector extends DBConnector
|
|||||||
// Ensure statement is closed
|
// Ensure statement is closed
|
||||||
if ($statement) {
|
if ($statement) {
|
||||||
$statement->closeCursor();
|
$statement->closeCursor();
|
||||||
unset($statement);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Report any errors
|
// Report any errors
|
||||||
|
Loading…
Reference in New Issue
Block a user