diff --git INSTALL.pgsql.txt INSTALL.pgsql.txt index f5f276e..59382f9 100644 --- INSTALL.pgsql.txt +++ INSTALL.pgsql.txt @@ -26,3 +26,19 @@ Note that the database must be created with UTF-8 (Unicode) encoding. createdb --encoding=UTF8 --owner=username databasename If there are no errors then the command was successful + +3. CREATE A SCHEMA OR SCHEMAS (Optional advanced) + + Drupal will run across different schemas within your database if you so wish. + By default, Drupal runs inside the 'public' schema but you can use $db_prefix + inside settings.php to define a schema for Drupal to inside of or specify tables + that are shared inside of a separate schema. Drupal will not create schemas for + you, infact the user that Drupal runs as should not be allowed to. You'll need + execute the SQL below as a superuser (such as a postgres user) and replace + 'drupaluser' with the username that Drupal uses to connect to PostgreSQL with + and replace schema_name with a schema name you wish to use such as 'shared': + + CREATE SCHEMA schema_name AUTHORIZATION drupaluser; + + Do this for as many schemas as you need. See default.settings.php for how to + set which tables use which schemas. diff --git includes/database/database.inc includes/database/database.inc index 39bba8e..7bff774 100644 --- includes/database/database.inc +++ includes/database/database.inc @@ -438,6 +438,26 @@ abstract class DatabaseConnection extends PDO { } /** + * Find the prefix for a table. + * + * This is function is for when you want to know the prefix of a table. This + * is not used in prefixTables due to performance reasons. + */ + public function tablePrefix($table = 'default') { + global $db_prefix; + if (is_array($db_prefix)) { + if (isset($db_prefix[$table])) { + return $db_prefix[$table]; + } + elseif (isset($db_prefix['default'])) { + return $db_prefix['default']; + } + return ''; + } + return $db_prefix; + } + + /** * Prepare a query string and return the prepared statement. * * This method caches prepared statements, reusing them when diff --git includes/database/mysql/schema.inc includes/database/mysql/schema.inc index b87bb16..7647cfc 100644 --- includes/database/mysql/schema.inc +++ includes/database/mysql/schema.inc @@ -25,7 +25,29 @@ class DatabaseSchema_mysql extends DatabaseSchema { const COMMENT_MAX_COLUMN = 255; /** - * Build a condition to match a table name against a standard information_schema. + * Get information about the table and database name from the db_prefix. + * + * @return + * A keyed array with information about the database, table name and prefix. + */ + protected function getPrefixInfo($table = 'default') { + $info = array( + 'prefix' => $this->connection->tablePrefix($table), + ); + if (($pos = strpos($info['prefix'], '.')) !== FALSE) { + $info['database'] = substr($info['prefix'], 0, $pos); + $info['table'] = substr($info['prefix'], ++$pos) . $table; + } + else { + $db_info = Database::getConnectionInfo(); + $info['database'] = $db_info['default']['database']; + $info['table'] = $info['prefix'] . $table; + } + return $info; + } + + /** + * Build a condition to match a table name against a standard information_schema. * * MySQL uses databases like schemas rather than catalogs so when we build * a condition to query the information_schema.tables, we set the default @@ -35,16 +57,11 @@ class DatabaseSchema_mysql extends DatabaseSchema { protected function buildTableNameCondition($table_name, $operator = '=') { $info = Database::getConnectionInfo(); - if (strpos($table_name, '.')) { - list($schema, $table_name) = explode('.', $table_name); - } - else { - $schema = $info['default']['database']; - } + $table_info = $this->getPrefixInfo($table_name); $condition = new DatabaseCondition('AND'); - $condition->condition('table_schema', $schema); - $condition->condition('table_name', $table_name, $operator); + $condition->condition('table_schema', $table_info['database']); + $condition->condition('table_name', $table_info['table'], $operator); return $condition; } @@ -269,7 +286,8 @@ class DatabaseSchema_mysql extends DatabaseSchema { } public function renameTable($table, $new_name) { - $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}'); + $info = $this->getPrefixInfo($new_name); + return $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO `' . $info['table'] . '`'); } public function dropTable($table) { @@ -371,7 +389,7 @@ class DatabaseSchema_mysql extends DatabaseSchema { * Retrieve a table or column comment. */ public function getComment($table, $column = NULL) { - $condition = $this->buildTableNameCondition($this->connection->prefixTables('{' . $table . '}')); + $condition = $this->buildTableNameCondition($table); if (isset($column)) { $condition->condition('column_name', $column); $condition->compile($this->connection, $this); diff --git includes/database/pgsql/schema.inc includes/database/pgsql/schema.inc index 578c074..4f8d138 100644 --- includes/database/pgsql/schema.inc +++ includes/database/pgsql/schema.inc @@ -95,7 +95,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema { } if (isset($table['unique keys']) && is_array($table['unique keys'])) { foreach ($table['unique keys'] as $key_name => $key) { - $sql_keys[] = 'CONSTRAINT {' . $name . '}_' . $key_name . '_key UNIQUE (' . implode(', ', $key) . ')'; + $sql_keys[] = 'CONSTRAINT ' . $this->prefixNonTable($name, $key_name, 'key') . ' UNIQUE (' . implode(', ', $key) . ')'; } } @@ -275,7 +275,12 @@ class DatabaseSchema_pgsql extends DatabaseSchema { } function renameTable($table, $new_name) { - $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}'); + // The new name of a table doesn't need to be referenced by schema. + // In the situation where the db_prefix is something like schema.prefix, + // the query will fail. So we must figure this out here instead of wrapping + // the new column in curly braces. + $prefixInfo = $this->getPrefixInfo($new_name); + $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO ' . $prefixInfo['table']); } public function dropTable($table) { @@ -399,7 +404,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema { * The table to be altered. */ public function dropPrimaryKey($table) { - $this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT {' . $table . '}_pkey'); + $this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT ' . $this->prefixNonTable($table, 'pkey')); } /** @@ -413,8 +418,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema { * An array of field names. */ function addUniqueKey($table, $name, $fields) { - $name = '{' . $table . '}_' . $name . '_key'; - $this->connection->query('ALTER TABLE {' . $table . '} ADD CONSTRAINT "' . $name . '" UNIQUE (' . implode(',', $fields) . ')'); + $this->connection->query('ALTER TABLE {' . $table . '} ADD CONSTRAINT "' . $this->prefixNonTable($table, $name, 'key') . '" UNIQUE (' . implode(',', $fields) . ')'); } /** @@ -426,8 +430,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema { * The name of the key. */ public function dropUniqueKey($table, $name) { - $name = '{' . $table . '}_' . $name . '_key'; - $this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT "' . $name . '"'); + $this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT "' . $this->prefixNonTable($table, $name, 'key') . '"'); } /** @@ -453,8 +456,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema { * The name of the index. */ public function dropIndex($table, $name) { - $name = '{' . $table . '}_' . $name . '_idx'; - $this->connection->query('DROP INDEX ' . $name); + $this->connection->query('DROP INDEX ' . $this->prefixNonTable($table, $name, 'idx')); } /** @@ -549,7 +551,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema { } protected function _createIndexSql($table, $name, $fields) { - $query = 'CREATE INDEX "{' . $table . '}_' . $name . '_idx" ON {' . $table . '} ('; + $query = 'CREATE INDEX "' . $this->prefixNonTable($table, $name, 'idx') . '" ON {' . $table . '} ('; $query .= $this->_createKeySql($fields) . ')'; return $query; } @@ -574,11 +576,11 @@ class DatabaseSchema_pgsql extends DatabaseSchema { * Retrieve a table or column comment. */ public function getComment($table, $column = NULL) { - $table = $this->connection->prefixTables('{' . $table . '}'); + $info = $this->getPrefixInfo($table); // Don't use {} around pg_class, pg_attribute tables. if (isset($column)) { - $this->connection->query('SELECT col_description(oid, attnum) FROM pg_class, pg_attribute WHERE attrelid = oid AND relname = ? AND attname = ?', array($table, $column))->fetchField(); + return $this->connection->query('SELECT col_description(oid, attnum) FROM pg_class, pg_attribute WHERE attrelid = oid AND relname = ? AND attname = ?', array($info['table'], $column))->fetchField(); } - $this->connection->query('SELECT obj_description(oid, ?) FROM pg_class WHERE relname = ?', array('pg_class', $table))->fetchField(); + return $this->connection->query('SELECT obj_description(oid, ?) FROM pg_class WHERE relname = ?', array('pg_class', $info['table']))->fetchField(); } } diff --git includes/database/schema.inc includes/database/schema.inc index 0437999..4a15734 100644 --- includes/database/schema.inc +++ includes/database/schema.inc @@ -154,7 +154,17 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { * The placeholder counter. */ protected $placeholder = 0; - + + /** + * Definition of prefixInfo array structure. + * + * Rather than redefining DatabaseSchema::getPrefixInfo() for each driver, + * by defining the defaultSchema variable only MySQL has to re-write the + * method. + * + * @see DatabaseSchema::getPrefixInfo() + */ + protected $defaultSchema = 'public'; public function __construct($connection) { $this->connection = $connection; @@ -165,6 +175,48 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { } /** + * Get information about the table name and schema from the db_prefix. + * + * @param + * Name of table to look prefix up for. Defaults to 'default' because thats + * default key for db_prefix. + * @return + * A keyed array with information about the schema, table name and prefix. + */ + protected function getPrefixInfo($table = 'default') { + $info = array( + 'schema' => $this->defaultSchema, + 'prefix' => $this->connection->tablePrefix($table), + ); + // If the prefix contains a period in it, then that means the prefix also + // contains a schema reference in which case we will change the schema key + // to the value before the period in the prefix. Everything after the dot with + // be prefixed onto the front of the table. + if (($pos = strpos($info['prefix'], '.')) !== FALSE) { + // Grab everything before the period. + $info['schema'] = substr($info['prefix'], 0, $pos); + // Grab everything after (++$pos increments $pos before it is used in substr). + $info['table'] = substr($info['prefix'], ++$pos) . $table; + } + else { + $info['table'] = $info['prefix'] . $table; + } + return $info; + } + + /** + * Create names for indexes, primary keys and contstraints. + * + * This prevents using {} around non-table names like indexes and keys. + */ + function prefixNonTable($table) { + $args = func_get_args(); + $info = $this->getPrefixInfo($table); + $args[0] = $info['table']; + return implode('_', $args); + } + + /** * Build a condition to match a table name against a standard information_schema. * * The information_schema is a SQL standard that provides information about the @@ -180,7 +232,7 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { * to make all the others work. For example see includes/databases/mysql/schema.inc. * * @param $table_name - * The name of the table to explode. + * The name of the table in question. * @param $operator * The operator to apply on the 'table' part of the condition. * @return QueryConditionInterface @@ -189,18 +241,13 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { protected function buildTableNameCondition($table_name, $operator = '=') { $info = Database::getConnectionInfo(); - // The table name may describe the schema eg. schema.table. - if (strpos($table_name, '.')) { - list($schema, $table_name) = explode('.', $table_name); - } - else { - $schema = 'public'; - } + // Retrive the table name and schema + $table_info = $this->getPrefixInfo($table_name); $condition = new DatabaseCondition('AND'); $condition->condition('table_catalog', $info['default']['database']); - $condition->condition('table_schema', $schema); - $condition->condition('table_name', $table_name, $operator); + $condition->condition('table_schema', $table_info['schema']); + $condition->condition('table_name', $table_info['table'], $operator); return $condition; } @@ -213,7 +260,7 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { * false is no table exists otherwise the actual table name. */ public function tableExists($table) { - $condition = $this->buildTableNameCondition($this->connection->prefixTables('{' . $table . '}')); + $condition = $this->buildTableNameCondition($table); $condition->compile($this->connection, $this); // Normally, we would heartily discourage the use of string // concatenation for conditionals like this however, we @@ -247,7 +294,7 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { * Check if a column exists in the given table. */ public function columnExists($table, $column) { - $condition = $this->buildTableNameCondition($this->connection->prefixTables('{' . $table . '}')); + $condition = $this->buildTableNameCondition($table); $condition->condition('column_name', $column); $condition->compile($this->connection, $this); // Normally, we would heartily discourage the use of string diff --git includes/database/sqlite/schema.inc includes/database/sqlite/schema.inc index ff3f35c..26bcfe1 100644 --- includes/database/sqlite/schema.inc +++ includes/database/sqlite/schema.inc @@ -14,6 +14,11 @@ class DatabaseSchema_sqlite extends DatabaseSchema { + /** + * Override DatabaseSchema::$defaultSchema + */ + protected $defaultSchema = 'main'; + public function tableExists($table) { // Don't use {} around sqlite_master table. return (bool) $this->connection->query("SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '{" . $table . "}'", array(), array())->fetchField(); @@ -45,14 +50,19 @@ class DatabaseSchema_sqlite extends DatabaseSchema { */ protected function createIndexSql($tablename, $schema) { $sql = array(); + $info = $this->getPrefixInfo($tablename); if (!empty($schema['unique keys'])) { foreach ($schema['unique keys'] as $key => $fields) { - $sql[] = 'CREATE UNIQUE INDEX "{' . $tablename . '}_' . $key . '" ON {' . $tablename . '} (' . $this->createKeySql($fields) . "); \n"; + // Normally we don't escape double quotes (we use single quotes) but + // describing the index name like this is faster and is readable. + $index = "\"{$info['schema']}\".\"{$info['table']}_$key\""; + $sql[] = 'CREATE UNIQUE INDEX ' . $index . ' ON ' . $info['table'] . ' (' . $this->createKeySql($fields) . "); \n"; } } if (!empty($schema['indexes'])) { - foreach ($schema['indexes'] as $index => $fields) { - $sql[] = 'CREATE INDEX "{' . $tablename . '}_' . $index . '" ON {' . $tablename . '} (' . $this->createKeySql($fields) . "); \n"; + foreach ($schema['indexes'] as $key => $fields) { + $index = "\"{$info['schema']}\".\"{$info['table']}_$key\""; + $sql[] = 'CREATE INDEX ' . $index . ' ON ' . $info['table'] . ' (' . $this->createKeySql($fields) . "); \n"; } } return $sql; @@ -220,7 +230,13 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * The new name for the table. */ public function renameTable($table, $new_name) { - $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}'); + // SQLite doesn't allow you to rename tables outside of the current + // database. So the syntax '...RENAME TO database.table' would fail. + // So we must determine the full table name here rather than surrounding + // the table with curly braces incase the db_prefix contains a reference + // to a database outside of our existsing database. + $info = $this->getPrefixInfo($new_name); + $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO ' . $info['table']); } /** @@ -446,7 +462,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * The name of the index. */ public function dropIndex($table, $name) { - $this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name); + $this->connection->query('DROP INDEX ' . $this->prefixNonTable($table, $name)); } /** @@ -476,7 +492,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * The name of the key. */ public function dropUniqueKey($table, $name) { - $this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name); + $this->connection->query('DROP INDEX ' . $this->prefixNonTable($table, $name)); } /** diff --git modules/system/system.test modules/system/system.test index 3d186a6..a44ae7c 100644 --- modules/system/system.test +++ modules/system/system.test @@ -24,7 +24,7 @@ class ModuleTestCase extends DrupalWebTestCase { * specified base table. Defaults to TRUE. */ function assertTableCount($base_table, $count = TRUE) { - $tables = db_find_tables(Database::getConnection()->prefixTables('{' . $base_table . '}') . '%'); + $tables = db_find_tables($base_table . '%'); if ($count) { return $this->assertTrue($tables, t('Tables matching "@base_table" found.', array('@base_table' => $base_table))); diff --git sites/default/default.settings.php sites/default/default.settings.php index 6c74139..822d22a 100644 --- sites/default/default.settings.php +++ sites/default/default.settings.php @@ -131,6 +131,21 @@ * 'authmap' => 'shared_', * ); * + * You can also use db_prefix as a reference to a schema/database. This maybe + * useful if your Drupal installation exists in a schema that is not the default + * or you want to access several databases from the same code base at the same time. + * Example: + * + * $db_prefix = array( + * 'default' => 'main.', + * 'users' => 'shared.', + * 'sessions' => 'shared.', + * 'role' => 'shared.', + * 'authmap' => 'shared.', + * ); + * + * NOTE: MySQL and SQLite's definition of a schema is a database. + * * Database configuration format: * $databases['default']['default'] = array( * 'driver' => 'mysql',