Commit 8f139a81 authored by webchick's avatar webchick

Issue #561422 by sun, dalin, lyricnz, catch, Gerhard Killesreiter: Replace...

Issue #561422 by sun, dalin, lyricnz, catch, Gerhard Killesreiter: Replace strtr() with str_replace() for db prefixing for increased performance.
parent 743b895a
...@@ -288,6 +288,20 @@ abstract class DatabaseConnection extends PDO { ...@@ -288,6 +288,20 @@ abstract class DatabaseConnection extends PDO {
*/ */
protected $prefixes = array(); protected $prefixes = array();
/**
* List of search values for use in prefixTables().
*
* @var array
*/
protected $prefixSearch = array();
/**
* List of replacement values for use in prefixTables().
*
* @var array
*/
protected $prefixReplace = array();
function __construct($dsn, $username, $password, $driver_options = array()) { function __construct($dsn, $username, $password, $driver_options = array()) {
// Initialize and prepare the connection prefix. // Initialize and prepare the connection prefix.
$this->setPrefix(isset($this->connectionOptions['prefix']) ? $this->connectionOptions['prefix'] : ''); $this->setPrefix(isset($this->connectionOptions['prefix']) ? $this->connectionOptions['prefix'] : '');
...@@ -391,6 +405,20 @@ protected function setPrefix($prefix) { ...@@ -391,6 +405,20 @@ protected function setPrefix($prefix) {
$this->defaultPrefix = $prefix; $this->defaultPrefix = $prefix;
$this->prefixes = array(); $this->prefixes = array();
} }
// Set up variables for use in prefixTables(). Replace table-specific
// prefixes first.
$this->prefixSearch = array();
$this->prefixReplace = array();
foreach ($this->prefixes as $key => $val) {
$this->prefixSearch[] = '{' . $key . '}';
$this->prefixReplace[] = $val . $key;
}
// Then replace remaining tables with the default prefix.
$this->prefixSearch[] = '{';
$this->prefixReplace[] = $this->defaultPrefix;
$this->prefixSearch[] = '}';
$this->prefixReplace[] = '';
} }
/** /**
...@@ -408,12 +436,7 @@ protected function setPrefix($prefix) { ...@@ -408,12 +436,7 @@ protected function setPrefix($prefix) {
* The properly-prefixed string. * The properly-prefixed string.
*/ */
public function prefixTables($sql) { public function prefixTables($sql) {
// Replace specific table prefixes first. return str_replace($this->prefixSearch, $this->prefixReplace, $sql);
foreach ($this->prefixes as $key => $val) {
$sql = strtr($sql, array('{' . $key . '}' => $val . $key));
}
// Then replace remaining tables with the default prefix.
return strtr($sql, array('{' => $this->defaultPrefix , '}' => ''));
} }
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment