Merge pull request #8490 from creative-commoners/pulls/4.3/minor-db-tweaks

NEW Some minor refactoring of the PDO and MySQLi connectors
This commit is contained in:
Sam Minnée 2018-10-18 22:11:29 +13:00 committed by GitHub
commit 24b9dbc8ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 16 deletions

View File

@ -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,10 +295,10 @@ 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
return new MySQLQuery($this, true);
} }
// Replicate normal behaviour of ->query() on non-select calls
return new MySQLQuery($this, true);
} }
public function selectDatabase($name) public function selectDatabase($name)
@ -304,9 +306,9 @@ class MySQLiConnector extends DBConnector
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()

View File

@ -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