diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-03-07 08:03:45 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-03-07 08:03:45 +0000 |
commit | 9703e5d6e24fb8df2778c5d687128f90079aa023 (patch) | |
tree | d5b65057e4b50950991e688eab820de5f31003a0 | |
parent | c4ed4523bf7974355f72abd6218e86e7bed398a6 (diff) | |
download | brdo-9703e5d6e24fb8df2778c5d687128f90079aa023.tar.gz brdo-9703e5d6e24fb8df2778c5d687128f90079aa023.tar.bz2 |
#715476 by Jeremy and Crell: Schema object should respect active database connection (with tests).
-rw-r--r-- | includes/database/database.inc | 23 | ||||
-rw-r--r-- | includes/database/mysql/database.inc | 2 | ||||
-rw-r--r-- | includes/database/mysql/schema.inc | 4 | ||||
-rw-r--r-- | includes/database/pgsql/database.inc | 2 | ||||
-rw-r--r-- | includes/database/schema.inc | 4 | ||||
-rw-r--r-- | includes/database/sqlite/database.inc | 2 | ||||
-rw-r--r-- | modules/simpletest/tests/database_test.test | 35 |
7 files changed, 68 insertions, 4 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc index e11129136..b988c83eb 100644 --- a/includes/database/database.inc +++ b/includes/database/database.inc @@ -309,6 +309,13 @@ abstract class DatabaseConnection extends PDO { protected $temporaryNameIndex = 0; /** + * The connection information for this connection object. + * + * @var array + */ + protected $connectionOptions = array(); + + /** * The schema object for this connection. * * @var object @@ -391,6 +398,22 @@ abstract class DatabaseConnection extends PDO { } /** + * Return the connection information for this connection object. + * + * Note that Database::getConnectionInfo() is for requesting information + * about an arbitrary database connection that is defined. This method + * is for requesting the connection information of this specific + * open connection object. + * + * @return + * An array of the connection information. The exact list of + * properties is driver-dependent. + */ + public function getConnectionOptions() { + return $this->connectionOptions; + } + + /** * Append a database prefix to all tables in a query. * * Queries sent to Drupal should wrap all table names in curly brackets. This diff --git a/includes/database/mysql/database.inc b/includes/database/mysql/database.inc index 46acb4dda..249db5234 100644 --- a/includes/database/mysql/database.inc +++ b/includes/database/mysql/database.inc @@ -25,6 +25,8 @@ class DatabaseConnection_mysql extends DatabaseConnection { $connection_options['port'] = 3306; } + $this->connectionOptions = $connection_options; + $dsn = 'mysql:host=' . $connection_options['host'] . ';port=' . $connection_options['port'] . ';dbname=' . $connection_options['database']; parent::__construct($dsn, $connection_options['username'], $connection_options['password'], array( // So we don't have to mess around with cursors and unbuffered queries by default. diff --git a/includes/database/mysql/schema.inc b/includes/database/mysql/schema.inc index ee2b3d373..0d3166f48 100644 --- a/includes/database/mysql/schema.inc +++ b/includes/database/mysql/schema.inc @@ -33,13 +33,13 @@ class DatabaseSchema_mysql extends DatabaseSchema { * from the condition criteria. */ protected function buildTableNameCondition($table_name, $operator = '=') { - $info = Database::getConnectionInfo(); + $info = $this->connection->getConnectionOptions(); if (strpos($table_name, '.')) { list($schema, $table_name) = explode('.', $table_name); } else { - $schema = $info['default']['database']; + $schema = $info['database']; } $condition = new DatabaseCondition('AND'); diff --git a/includes/database/pgsql/database.inc b/includes/database/pgsql/database.inc index f76fd95ee..f3ed30b57 100644 --- a/includes/database/pgsql/database.inc +++ b/includes/database/pgsql/database.inc @@ -36,6 +36,8 @@ class DatabaseConnection_pgsql extends DatabaseConnection { $connection_options['password'] = null; } + $this->connectionOptions = $connection_options; + $dsn = 'pgsql:host=' . $connection_options['host'] . ' dbname=' . $connection_options['database'] . ' port=' . $connection_options['port']; parent::__construct($dsn, $connection_options['username'], $connection_options['password'], array( // Convert numeric values to strings when fetching. diff --git a/includes/database/schema.inc b/includes/database/schema.inc index b488af2cb..4cc45b59b 100644 --- a/includes/database/schema.inc +++ b/includes/database/schema.inc @@ -187,7 +187,7 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { * A DatabaseCondition object. */ protected function buildTableNameCondition($table_name, $operator = '=') { - $info = Database::getConnectionInfo(); + $info = $this->connection->getConnectionOptions(); // The table name may describe the schema eg. schema.table. if (strpos($table_name, '.')) { @@ -198,7 +198,7 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { } $condition = new DatabaseCondition('AND'); - $condition->condition('table_catalog', $info['default']['database']); + $condition->condition('table_catalog', $info['database']); $condition->condition('table_schema', $schema); $condition->condition('table_name', $table_name, $operator); return $condition; diff --git a/includes/database/sqlite/database.inc b/includes/database/sqlite/database.inc index dbfb4ef95..f3cb77a70 100644 --- a/includes/database/sqlite/database.inc +++ b/includes/database/sqlite/database.inc @@ -25,6 +25,8 @@ class DatabaseConnection_sqlite extends DatabaseConnection { // This driver defaults to transaction support, except if explicitly passed FALSE. $this->transactionSupport = !isset($connection_options['transactions']) || $connection_options['transactions'] !== FALSE; + $this->connectionOptions = $connection_options; + parent::__construct('sqlite:' . $connection_options['database'], '', '', array( // Force column names to lower case. PDO::ATTR_CASE => PDO::CASE_LOWER, diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index 3f3f80008..3dc48b727 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -244,6 +244,41 @@ class DatabaseConnectionTestCase extends DatabaseTestCase { // Opening a connection after closing it should yield an object different than the original. $this->assertNotIdentical($db1, $db2, t('Opening the default connection after it is closed returns a new object.')); } + + /** + * Tests the connection options of the active database. + */ + function testConnectionOptions() { + $connection_info = Database::getConnectionInfo('default'); + + // Be sure we're connected to the default database. + $db = Database::getConnection('default', 'default'); + $connectionOptions = $db->getConnectionOptions(); + + // In the MySQL driver, the port can be different, so check individual + // options. + $this->assertEqual($connection_info['default']['driver'], $connectionOptions['driver'], t('The default connection info driver matches the current connection options driver.')); + $this->assertEqual($connection_info['default']['database'], $connectionOptions['database'], t('The default connection info database matches the current connection options database.')); + + // Set up identical slave and confirm connection options are identical. + Database::addConnectionInfo('default', 'slave', $connection_info['default']); + $db2 = Database::getConnection('slave', 'default'); + $connectionOptions2 = $db2->getConnectionOptions(); + + // Get a fresh copy of the default connection options. + $connectionOptions = $db->getConnectionOptions(); + $this->assertIdentical($connectionOptions, $connectionOptions2, t('The default and slave connection options are identical.')); + + // Set up a new connection with different connection info. + $test = $connection_info['default']; + $test['database'] .= 'test'; + Database::addConnectionInfo('test', 'default', $test); + $connection_info = Database::getConnectionInfo('test'); + + // Get a fresh copy of the default connection options. + $connectionOptions = $db->getConnectionOptions(); + $this->assertNotEqual($connection_info['default']['database'], $connectionOptions['database'], t('The test connection info database does not match the current connection options database.')); + } } /** |