diff options
author | webchick <webchick@24967.no-reply.drupal.org> | 2011-06-22 17:42:34 -0700 |
---|---|---|
committer | webchick <webchick@24967.no-reply.drupal.org> | 2011-06-22 17:42:34 -0700 |
commit | b8cb76c48b9b119ce4c7ae237cea14aac36099bc (patch) | |
tree | 32fb229f4265194d5e607ae808a85fb9fd5a7127 | |
parent | 370a8e368cb19a5f77e4e47455082259fdb5d392 (diff) | |
download | brdo-b8cb76c48b9b119ce4c7ae237cea14aac36099bc.tar.gz brdo-b8cb76c48b9b119ce4c7ae237cea14aac36099bc.tar.bz2 |
Issue #561422 by sun, dalin, lyricnz, catch, Gerhard Killesreiter: Replace strtr() with str_replace() for db prefixing for increased performance.
-rw-r--r-- | includes/database/database.inc | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc index 4cc1a33d7..b04cdf7df 100644 --- a/includes/database/database.inc +++ b/includes/database/database.inc @@ -288,6 +288,20 @@ abstract class DatabaseConnection extends PDO { */ 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()) { // Initialize and prepare the connection prefix. $this->setPrefix(isset($this->connectionOptions['prefix']) ? $this->connectionOptions['prefix'] : ''); @@ -391,6 +405,20 @@ abstract class DatabaseConnection extends PDO { $this->defaultPrefix = $prefix; $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 @@ abstract class DatabaseConnection extends PDO { * The properly-prefixed string. */ public function prefixTables($sql) { - // Replace specific table prefixes first. - 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 , '}' => '')); + return str_replace($this->prefixSearch, $this->prefixReplace, $sql); } /** |