summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2010-03-01 11:30:37 +0000
committerDries Buytaert <dries@buytaert.net>2010-03-01 11:30:37 +0000
commit13704b8f628af43c5bd1c404cb4eb2ef4d550401 (patch)
tree0108353e4b66e5791fcd9c5c874a4da5a916209a
parent74e94b7e019d29bb58928df0b5b5de8ec3651359 (diff)
downloadbrdo-13704b8f628af43c5bd1c404cb4eb2ef4d550401.tar.gz
brdo-13704b8f628af43c5bd1c404cb4eb2ef4d550401.tar.bz2
- Patch #722912 by andypost: db_index_exists() must conform schemaAPI indexExists().
-rw-r--r--includes/database/database.inc22
-rw-r--r--includes/database/mysql/schema.inc5
-rw-r--r--includes/database/pgsql/schema.inc3
-rw-r--r--includes/database/query.inc2
-rw-r--r--includes/database/schema.inc23
-rw-r--r--modules/simpletest/tests/database_test.test20
-rw-r--r--modules/simpletest/tests/schema.test8
7 files changed, 58 insertions, 25 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc
index 533e25046..b381ac72b 100644
--- a/includes/database/database.inc
+++ b/includes/database/database.inc
@@ -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);
diff --git a/includes/database/mysql/schema.inc b/includes/database/mysql/schema.inc
index 69500d3fa..ee2b3d373 100644
--- a/includes/database/mysql/schema.inc
+++ b/includes/database/mysql/schema.inc
@@ -348,7 +348,10 @@ class DatabaseSchema_mysql extends DatabaseSchema {
}
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) {
diff --git a/includes/database/pgsql/schema.inc b/includes/database/pgsql/schema.inc
index b7ab95f75..679efbde8 100644
--- a/includes/database/pgsql/schema.inc
+++ b/includes/database/pgsql/schema.inc
@@ -375,8 +375,9 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
}
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();
}
/**
diff --git a/includes/database/query.inc b/includes/database/query.inc
index ff591f04c..fb4e99968 100644
--- a/includes/database/query.inc
+++ b/includes/database/query.inc
@@ -808,7 +808,7 @@ class MergeQuery extends Query {
$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) {
diff --git a/includes/database/schema.inc b/includes/database/schema.inc
index 75e7aab6b..b488af2cb 100644
--- a/includes/database/schema.inc
+++ b/includes/database/schema.inc
@@ -210,7 +210,7 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
* @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 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
// 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 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
// 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 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
// 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 class DatabaseSchema implements QueryPlaceholderInterface {
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);
diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test
index cf569f7cc..3f3f80008 100644
--- a/modules/simpletest/tests/database_test.test
+++ b/modules/simpletest/tests/database_test.test
@@ -2391,19 +2391,27 @@ class DatabaseRegressionTestCase extends DatabaseTestCase {
}
/**
+ * 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.'));
}
}
diff --git a/modules/simpletest/tests/schema.test b/modules/simpletest/tests/schema.test
index 7f7571ef9..0ddead4dc 100644
--- a/modules/simpletest/tests/schema.test
+++ b/modules/simpletest/tests/schema.test
@@ -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');