From 437e53f2fec17bc778cc7bba39de322d43214441 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Wed, 17 Oct 2018 12:50:34 +0200 Subject: [PATCH] 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. --- src/ORM/Connect/MySQLiConnector.php | 22 ++++++++++++---------- src/ORM/Connect/PDOConnector.php | 12 ++++++------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/ORM/Connect/MySQLiConnector.php b/src/ORM/Connect/MySQLiConnector.php index 6b283be7b..43805c705 100644 --- a/src/ORM/Connect/MySQLiConnector.php +++ b/src/ORM/Connect/MySQLiConnector.php @@ -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() diff --git a/src/ORM/Connect/PDOConnector.php b/src/ORM/Connect/PDOConnector.php index f3fbae164..7d9f1af4a 100644 --- a/src/ORM/Connect/PDOConnector.php +++ b/src/ORM/Connect/PDOConnector.php @@ -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