Commit 338064 by webchick
#715476 by Jeremy and Crell: Schema object should respect active database connection (with tests).
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/database.inc</a> 2010/03/06 06:31:24 1.99
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/database.inc</a> 2010/03/07 08:03:44 1.100
@@ -309,6 +309,13 @@
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 @@
}
/**
+ * 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
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/datab...</a> 2010/02/17 22:44:52 1.25
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/datab...</a> 2010/03/07 08:03:44 1.26
@@ -25,6 +25,8 @@
$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.
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schema.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schema.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schem...</a> 2010/03/01 11:30:37 1.31
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schema.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schema.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schem...</a> 2010/03/07 08:03:44 1.32
@@ -33,13 +33,13 @@
* 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');
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/datab...</a> 2010/02/28 20:10:34 1.35
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/datab...</a> 2010/03/07 08:03:45 1.36
@@ -36,6 +36,8 @@
$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.
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc</a> 2010/03/01 11:30:37 1.29
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc</a> 2010/03/07 08:03:44 1.30
@@ -187,7 +187,7 @@
* 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 @@
}
$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;
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/data...</a> 2010/03/07 07:53:15 1.27
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/data...</a> 2010/03/07 08:03:45 1.28
@@ -25,6 +25,8 @@
// 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,
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/database_test.test" title="http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/database_test.test" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/data...</a> 2010/03/01 11:30:37 1.82
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/database_test.test" title="http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/database_test.test" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/data...</a> 2010/03/07 08:03:45 1.83
@@ -244,6 +244,41 @@
// 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.'));
+ }
}
/**
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/database.inc</a> 2010/03/07 08:03:44 1.100
@@ -309,6 +309,13 @@
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 @@
}
/**
+ * 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
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/datab...</a> 2010/02/17 22:44:52 1.25
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/datab...</a> 2010/03/07 08:03:44 1.26
@@ -25,6 +25,8 @@
$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.
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schema.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schema.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schem...</a> 2010/03/01 11:30:37 1.31
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schema.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schema.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/mysql/schem...</a> 2010/03/07 08:03:44 1.32
@@ -33,13 +33,13 @@
* 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');
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/datab...</a> 2010/02/28 20:10:34 1.35
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/pgsql/datab...</a> 2010/03/07 08:03:45 1.36
@@ -36,6 +36,8 @@
$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.
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc</a> 2010/03/01 11:30:37 1.29
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/schema.inc</a> 2010/03/07 08:03:44 1.30
@@ -187,7 +187,7 @@
* 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 @@
}
$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;
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/data...</a> 2010/03/07 07:53:15 1.27
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/database.inc" title="http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/database.inc" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/includes/database/sqlite/data...</a> 2010/03/07 08:03:45 1.28
@@ -25,6 +25,8 @@
// 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,
--- <a href="http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/database_test.test" title="http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/database_test.test" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/data...</a> 2010/03/01 11:30:37 1.82
+++ <a href="http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/database_test.test" title="http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/database_test.test" rel="nofollow">http://drupalcode.org/viewvc/drupal/drupal/modules/simpletest/tests/data...</a> 2010/03/07 08:03:45 1.83
@@ -244,6 +244,41 @@
// 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.'));
+ }
}
/**