Commit 9703e5d6 authored by webchick's avatar webchick

#715476 by Jeremy and Crell: Schema object should respect active database connection (with tests).

parent c4ed4523
......@@ -308,6 +308,13 @@
*/
protected $temporaryNameIndex = 0;
/**
* The connection information for this connection object.
*
* @var array
*/
protected $connectionOptions = array();
/**
* The schema object for this connection.
*
......@@ -390,6 +397,22 @@ protected function defaultOptions() {
);
}
/**
* 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.
*
......
......@@ -25,6 +25,8 @@ public function __construct(array $connection_options = array()) {
$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.
......
......@@ -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');
......
......@@ -36,6 +36,8 @@ public function __construct(array $connection_options = array()) {
$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.
......
......@@ -187,7 +187,7 @@ public function nextPlaceholder() {
* 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 @@ protected function buildTableNameCondition($table_name, $operator = '=') {
}
$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;
......
......@@ -25,6 +25,8 @@ public function __construct(array $connection_options = array()) {
// 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,
......
......@@ -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.'));
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment