Issue #561422 follow-up by Damien Tournoud, lyricnz, chx: Fixed SQLite support for faster db prefixes.

8.0.x
webchick 2011-06-27 08:42:29 -07:00
parent 95430f986c
commit 8f33be54d6
2 changed files with 17 additions and 27 deletions

View File

@ -273,16 +273,7 @@ abstract class DatabaseConnection extends PDO {
protected $schema = NULL; 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 string
*/
protected $defaultPrefix = '';
/**
* The non-default prefixes used by this database connection.
* *
* @var array * @var array
*/ */
@ -389,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 * @param $prefix
* The prefixes, in any of the multiple forms documented in * The prefixes, in any of the multiple forms documented in
@ -397,13 +388,10 @@ abstract class DatabaseConnection extends PDO {
*/ */
protected function setPrefix($prefix) { protected function setPrefix($prefix) {
if (is_array($prefix)) { if (is_array($prefix)) {
$this->defaultPrefix = isset($prefix['default']) ? $prefix['default'] : ''; $this->prefixes = $prefix + array('default' => '');
unset($prefix['default']);
$this->prefixes = $prefix;
} }
else { else {
$this->defaultPrefix = $prefix; $this->prefixes = array('default' => $prefix);
$this->prefixes = array();
} }
// Set up variables for use in prefixTables(). Replace table-specific // Set up variables for use in prefixTables(). Replace table-specific
@ -411,12 +399,14 @@ abstract class DatabaseConnection extends PDO {
$this->prefixSearch = array(); $this->prefixSearch = array();
$this->prefixReplace = array(); $this->prefixReplace = array();
foreach ($this->prefixes as $key => $val) { foreach ($this->prefixes as $key => $val) {
$this->prefixSearch[] = '{' . $key . '}'; if ($key != 'default') {
$this->prefixReplace[] = $val . $key; $this->prefixSearch[] = '{' . $key . '}';
$this->prefixReplace[] = $val . $key;
}
} }
// Then replace remaining tables with the default prefix. // Then replace remaining tables with the default prefix.
$this->prefixSearch[] = '{'; $this->prefixSearch[] = '{';
$this->prefixReplace[] = $this->defaultPrefix; $this->prefixReplace[] = $this->prefixes['default'];
$this->prefixSearch[] = '}'; $this->prefixSearch[] = '}';
$this->prefixReplace[] = ''; $this->prefixReplace[] = '';
} }
@ -450,7 +440,7 @@ abstract class DatabaseConnection extends PDO {
return $this->prefixes[$table]; return $this->prefixes[$table];
} }
else { else {
return $this->defaultPrefix; return $this->prefixes['default'];
} }
} }

View File

@ -71,12 +71,8 @@ class DatabaseConnection_sqlite extends DatabaseConnection {
)); ));
// Attach one database for each registered prefix. // Attach one database for each registered prefix.
$prefixes = &$this->prefixes; $prefixes = $this->prefixes;
if (!empty($this->defaultPrefix)) { foreach ($prefixes as $table => &$prefix) {
// Add in the default prefix, which is also attached.
$prefixes[] = &$this->defaultPrefix;
}
foreach ($this->prefixes as $table => &$prefix) {
// Empty prefix means query the main database -- no need to attach anything. // Empty prefix means query the main database -- no need to attach anything.
if (!empty($prefix)) { if (!empty($prefix)) {
// Only attach the database once. // Only attach the database once.
@ -90,6 +86,8 @@ class DatabaseConnection_sqlite extends DatabaseConnection {
$prefix .= '.'; $prefix .= '.';
} }
} }
// Regenerate the prefixes replacement table.
$this->setPrefix($prefixes);
// Detect support for SAVEPOINT. // Detect support for SAVEPOINT.
$version = $this->query('SELECT sqlite_version()')->fetchField(); $version = $this->query('SELECT sqlite_version()')->fetchField();
@ -240,7 +238,9 @@ class DatabaseConnection_sqlite extends DatabaseConnection {
// Generate a new temporary table name and protect it from prefixing. // Generate a new temporary table name and protect it from prefixing.
// SQLite requires that temporary tables to be non-qualified. // SQLite requires that temporary tables to be non-qualified.
$tablename = $this->generateTemporaryTableName(); $tablename = $this->generateTemporaryTableName();
$this->prefixes[$tablename] = ''; $prefixes = $this->prefixes;
$prefixes[$tablename] = '';
$this->setPrefix($prefixes);
$this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' AS SELECT', $query), $args, $options); $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' AS SELECT', $query), $args, $options);
return $tablename; return $tablename;