diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-08-02 18:55:17 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-08-02 18:55:17 +0000 |
commit | 9ad0a7ea9b9fefecef1d33ee26d654201f9627d1 (patch) | |
tree | be393990e3c2179b9aacf7a436ee6a1d201f3804 | |
parent | 36da783d5310d680d137ccaeb116691e35be424a (diff) | |
download | brdo-9ad0a7ea9b9fefecef1d33ee26d654201f9627d1.tar.gz brdo-9ad0a7ea9b9fefecef1d33ee26d654201f9627d1.tar.bz2 |
#851168 by Stevel, Damien Tournoud: Fixed db_find_tables() expects tables to be prefixed, inconsistent implementation and documentation.
-rw-r--r-- | includes/database/mysql/schema.inc | 17 | ||||
-rw-r--r-- | includes/database/schema.inc | 27 | ||||
-rw-r--r-- | modules/system/system.test | 2 |
3 files changed, 29 insertions, 17 deletions
diff --git a/includes/database/mysql/schema.inc b/includes/database/mysql/schema.inc index ca7a14be4..1712ca2a4 100644 --- a/includes/database/mysql/schema.inc +++ b/includes/database/mysql/schema.inc @@ -30,16 +30,19 @@ class DatabaseSchema_mysql extends DatabaseSchema { * @return * A keyed array with information about the database, table name and prefix. */ - protected function getPrefixInfo($table = 'default') { + protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) { $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; + if ($add_prefix) { + $table = $info['prefix'] . $table; + } + if (($pos = strpos($table, '.')) !== FALSE) { + $info['database'] = substr($table, 0, $pos); + $info['table'] = substr($table, ++$pos); } else { $db_info = Database::getConnectionInfo(); $info['database'] = $db_info['default']['database']; - $info['table'] = $info['prefix'] . $table; + $info['table'] = $table; } return $info; } @@ -52,10 +55,10 @@ class DatabaseSchema_mysql extends DatabaseSchema { * database as the schema unless specified otherwise, and exclude table_catalog * from the condition criteria. */ - protected function buildTableNameCondition($table_name, $operator = '=') { + protected function buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE) { $info = $this->connection->getConnectionOptions(); - $table_info = $this->getPrefixInfo($table_name); + $table_info = $this->getPrefixInfo($table_name, $add_prefix); $condition = new DatabaseCondition('AND'); $condition->condition('table_schema', $table_info['database']); diff --git a/includes/database/schema.inc b/includes/database/schema.inc index d7ae5eb36..2c3309562 100644 --- a/includes/database/schema.inc +++ b/includes/database/schema.inc @@ -175,26 +175,32 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { * @param * Name of table to look prefix up for. Defaults to 'default' because thats * default key for prefix. + * @param $add_prefix + * Boolean that indicates whether the given table name should be prefixed. + * * @return * A keyed array with information about the schema, table name and prefix. */ - protected function getPrefixInfo($table = 'default') { + protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) { $info = array( 'schema' => $this->defaultSchema, 'prefix' => $this->connection->tablePrefix($table), ); + if ($add_prefix) { + $table = $info['prefix'] . $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 // will be prefixed onto the front of the table. - if (($pos = strpos($info['prefix'], '.')) !== FALSE) { + if (($pos = strpos($table, '.')) !== FALSE) { // Grab everything before the period. - $info['schema'] = substr($info['prefix'], 0, $pos); - // Grab everything after the dot, and prefix on to the table. - $info['table'] = substr($info['prefix'], ++$pos) . $table; + $info['schema'] = substr($table, 0, $pos); + // Grab everything after the dot. + $info['table'] = substr($table, ++$pos); } else { - $info['table'] = $info['prefix'] . $table; + $info['table'] = $table; } return $info; } @@ -230,15 +236,17 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { * The name of the table in question. * @param $operator * The operator to apply on the 'table' part of the condition. + * @param $add_prefix + * Boolean to indicate whether the table name needs to be prefixed. * * @return QueryConditionInterface * A DatabaseCondition object. */ - protected function buildTableNameCondition($table_name, $operator = '=') { + protected function buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE) { $info = $this->connection->getConnectionOptions(); // Retrive the table name and schema - $table_info = $this->getPrefixInfo($table_name); + $table_info = $this->getPrefixInfo($table_name, $add_prefix); $condition = new DatabaseCondition('AND'); $condition->condition('table_catalog', $info['database']); @@ -278,7 +286,8 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface { * Array, both the keys and the values are the matching tables. */ public function findTables($table_expression) { - $condition = $this->buildTableNameCondition($table_expression, 'LIKE'); + $condition = $this->buildTableNameCondition($table_expression, 'LIKE', FALSE); + $condition->compile($this->connection, $this); // Normally, we would heartily discourage the use of string // concatenation for conditionals like this however, we diff --git a/modules/system/system.test b/modules/system/system.test index 977074bb6..1d665bf94 100644 --- a/modules/system/system.test +++ b/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($base_table . '%'); + $tables = db_find_tables(Database::getConnection()->prefixTables('{' . $base_table . '}') . '%'); if ($count) { return $this->assertTrue($tables, t('Tables matching "@base_table" found.', array('@base_table' => $base_table))); |