Commit 13704b8f authored by Dries's avatar Dries
Browse files

- Patch #722912 by andypost: db_index_exists() must conform schemaAPI indexExists().

parent 74e94b7e
......@@ -2423,19 +2423,26 @@ function db_field_names($fields) {
}
/**
* Check if an index exists.
* Check if an index exists in the given table.
*
* @param $table
* The name of the table in drupal (no prefixing).
* @param $name
* Index name.
* The name of the index in drupal (no prefixing).
* @return
* TRUE if the given index exists, otherwise FALSE.
*/
function db_index_exists($name) {
return Database::getConnection()->schema()->indexExists($name);
function db_index_exists($table, $name) {
return Database::getConnection()->schema()->indexExists($table, $name);
}
/**
* Check if a table exists.
*
* @param $table
* The name of the table in drupal (no prefixing).
* @return
* TRUE if the given table exists, otherwise FALSE.
*/
function db_table_exists($table) {
return Database::getConnection()->schema()->tableExists($table);
......@@ -2443,6 +2450,13 @@ function db_table_exists($table) {
/**
* Check if a column exists in the given table.
*
* @param $table
* The name of the table in drupal (no prefixing).
* @param $name
* The name of the column.
* @return
* TRUE if the given column exists, otherwise FALSE.
*/
function db_column_exists($table, $column) {
return Database::getConnection()->schema()->columnExists($table, $column);
......
......@@ -348,7 +348,10 @@ public function fieldSetNoDefault($table, $field) {
}
public function indexExists($table, $name) {
return $this->connection->query('SHOW INDEX FROM {' . $table . "} WHERE key_name = '$name'")->fetchField();
// Returns one row for each column in the index. Result is string or FALSE.
// Details at http://dev.mysql.com/doc/refman/5.0/en/show-index.html
$row = $this->connection->query('SHOW INDEX FROM {' . $table . "} WHERE key_name = '$name'")->fetchAssoc();
return isset($row['key_name']);
}
public function addPrimaryKey($table, $fields) {
......
......@@ -375,8 +375,9 @@ public function fieldSetNoDefault($table, $field) {
}
public function indexExists($table, $name) {
// Details http://www.postgresql.org/docs/8.3/interactive/view-pg-indexes.html
$index_name = '{' . $table . '}_' . $name . '_idx';
return $this->connection->query("SELECT 1 FROM pg_indexes WHERE indexname = '$index_name'")->fetchField();
return (bool) $this->connection->query("SELECT 1 FROM pg_indexes WHERE indexname = '$index_name'")->fetchField();
}
/**
......
......@@ -808,7 +808,7 @@ public function execute() {
$select = $select->countQuery();
$sql = (string)$select;
$arguments = $select->getArguments();
$num_existing = db_query($sql, $arguments)->fetchField();
$num_existing = $this->connection->query($sql, $arguments)->fetchField();
if ($num_existing) {
......
......@@ -210,7 +210,7 @@ protected function buildTableNameCondition($table_name, $operator = '=') {
* @param $table
* The name of the table in drupal (no prefixing).
* @return
* false is no table exists otherwise the actual table name.
* TRUE if the given table exists, otherwise FALSE.
*/
public function tableExists($table) {
$condition = $this->buildTableNameCondition($this->connection->prefixTables('{' . $table . '}'));
......@@ -220,7 +220,7 @@ public function tableExists($table) {
// couldn't use db_select() here because it would prefix
// information_schema.tables and the query would fail.
// Don't use {} around information_schema.tables table.
return db_query("SELECT table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField();
return (bool) $this->connection->query("SELECT 1 FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField();
}
/**
......@@ -240,11 +240,18 @@ public function findTables($table_expression) {
// couldn't use db_select() here because it would prefix
// information_schema.tables and the query would fail.
// Don't use {} around information_schema.tables table.
return db_query("SELECT table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchAllKeyed(0, 0);
return $this->connection->query("SELECT table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchAllKeyed(0, 0);
}
/**
* Check if a column exists in the given table.
*
* @param $table
* The name of the table in drupal (no prefixing).
* @param $name
* The name of the column.
* @return
* TRUE if the given column exists, otherwise FALSE.
*/
public function columnExists($table, $column) {
$condition = $this->buildTableNameCondition($this->connection->prefixTables('{' . $table . '}'));
......@@ -255,7 +262,7 @@ public function columnExists($table, $column) {
// couldn't use db_select() here because it would prefix
// information_schema.tables and the query would fail.
// Don't use {} around information_schema.columns table.
return db_query("SELECT column_name FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchAllKeyed(0, 0);
return (bool) $this->connection->query("SELECT 1 FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchField();
}
/**
......@@ -356,14 +363,14 @@ abstract public function fieldSetDefault($table, $field, $default);
abstract public function fieldSetNoDefault($table, $field);
/**
* Checks if an index exists.
* Checks if an index exists in the given table.
*
* @param $table
* Name of the table.
* The name of the table in drupal (no prefixing).
* @param $name
* Name of the index.
* The name of the index in drupal (no prefixing).
* @return
* TRUE if the index exists, otherwise FALSE.
* TRUE if the given index exists, otherwise FALSE.
*/
abstract public function indexExists($table, $name);
......
......@@ -2390,20 +2390,28 @@ class DatabaseRegressionTestCase extends DatabaseTestCase {
$this->assertIdentical($name, $from_database, t("The database handles UTF-8 characters cleanly."));
}
/**
* Test the db_table_exists() function.
*/
function testDBTableExists() {
$this->assertIdentical(TRUE, db_table_exists('node'), t('Returns true for existent table.'));
$this->assertIdentical(FALSE, db_table_exists('nosuchtable'), t('Returns false for nonexistent table.'));
}
/**
* Test the db_column_exists() function.
*/
function testDBColumnExists() {
$this->assertTrue(db_column_exists('node', 'nid'), t('Returns true for existent column.'));
$this->assertFalse(db_column_exists('node', 'nosuchcolumn'), t('Returns false for nonexistent column.'));
$this->assertIdentical(TRUE, db_column_exists('node', 'nid'), t('Returns true for existent column.'));
$this->assertIdentical(FALSE, db_column_exists('node', 'nosuchcolumn'), t('Returns false for nonexistent column.'));
}
/**
* Test the db_table_exists() function.
* Test the db_index_exists() function.
*/
function testDBTableExists() {
$this->assertTrue(db_table_exists('node'), t('Returns true for existent table.'));
$this->assertFalse(db_table_exists('nosuchtable'), t('Returns false for nonexistent table.'));
function testDBIndexExists() {
$this->assertIdentical(TRUE, db_index_exists('node', 'node_created'), t('Returns true for existent index.'));
$this->assertIdentical(FALSE, db_index_exists('node', 'nosuchindex'), t('Returns false for nonexistent index.'));
}
}
......
......@@ -61,14 +61,14 @@ class SchemaTestCase extends DrupalWebTestCase {
// The insert should fail again.
$this->assertFalse($this->tryInsert(), t('Insert without a default failed.'));
// Test for fake index.
// Test for fake index and test for the boolean result of indexExists().
$index_exists = Database::getConnection()->schema()->indexExists('test_table', 'test_field');
$this->assertFalse($index_exists, t('Fake index does not exists'));
$this->assertIdentical($index_exists, FALSE, t('Fake index does not exists'));
// Add index.
db_add_index('test_table', 'test_field', array('test_field'));
// Test for created index.
// Test for created index and test for the boolean result of indexExists().
$index_exists = Database::getConnection()->schema()->indexExists('test_table', 'test_field');
$this->assertTrue($index_exists, t('Index created.'));
$this->assertIdentical($index_exists, TRUE, t('Index created.'));
// Rename the table.
db_rename_table('test_table', 'test_table2');
......
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