diff options
Diffstat (limited to 'includes/database/database.inc')
-rw-r--r-- | includes/database/database.inc | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc index 4cc1a33d7..e08f9074f 100644 --- a/includes/database/database.inc +++ b/includes/database/database.inc @@ -273,20 +273,25 @@ abstract class DatabaseConnection extends PDO { protected $schema = NULL; /** - * The default prefix used by this database connection. + * The prefixes used by this database connection. * - * Separated from the other prefixes for performance reasons. + * @var array + */ + protected $prefixes = array(); + + /** + * List of search values for use in prefixTables(). * - * @var string + * @var array */ - protected $defaultPrefix = ''; + protected $prefixSearch = array(); /** - * The non-default prefixes used by this database connection. + * List of replacement values for use in prefixTables(). * * @var array */ - protected $prefixes = array(); + protected $prefixReplace = array(); function __construct($dsn, $username, $password, $driver_options = array()) { // Initialize and prepare the connection prefix. @@ -375,7 +380,7 @@ abstract class DatabaseConnection extends PDO { } /** - * Preprocess the prefixes used by this database connection. + * Set the list of prefixes used by this database connection. * * @param $prefix * The prefixes, in any of the multiple forms documented in @@ -383,14 +388,27 @@ abstract class DatabaseConnection extends PDO { */ protected function setPrefix($prefix) { if (is_array($prefix)) { - $this->defaultPrefix = isset($prefix['default']) ? $prefix['default'] : ''; - unset($prefix['default']); - $this->prefixes = $prefix; + $this->prefixes = $prefix + array('default' => ''); } else { - $this->defaultPrefix = $prefix; - $this->prefixes = array(); + $this->prefixes = array('default' => $prefix); } + + // Set up variables for use in prefixTables(). Replace table-specific + // prefixes first. + $this->prefixSearch = array(); + $this->prefixReplace = array(); + foreach ($this->prefixes as $key => $val) { + if ($key != 'default') { + $this->prefixSearch[] = '{' . $key . '}'; + $this->prefixReplace[] = $val . $key; + } + } + // Then replace remaining tables with the default prefix. + $this->prefixSearch[] = '{'; + $this->prefixReplace[] = $this->prefixes['default']; + $this->prefixSearch[] = '}'; + $this->prefixReplace[] = ''; } /** @@ -408,12 +426,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); } /** @@ -427,7 +440,7 @@ abstract class DatabaseConnection extends PDO { return $this->prefixes[$table]; } else { - return $this->defaultPrefix; + return $this->prefixes['default']; } } |