diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-11-13 21:08:16 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-11-13 21:08:16 +0000 |
commit | 44c8391d327f589fb5e96bb7bbe0c08db1611441 (patch) | |
tree | 7fbf766b62bd09abfbb1c9c230528b7a49188ae0 | |
parent | a269b9cc1d224c1fd0fa63306c84896c37fef173 (diff) | |
download | brdo-44c8391d327f589fb5e96bb7bbe0c08db1611441.tar.gz brdo-44c8391d327f589fb5e96bb7bbe0c08db1611441.tar.bz2 |
- Patch #321100 by hswong3i: empty insert statements are better handled now. Comes with tests.
-rw-r--r-- | includes/database/mysql/query.inc | 4 | ||||
-rw-r--r-- | includes/database/pgsql/query.inc | 4 | ||||
-rw-r--r-- | includes/database/query.inc | 8 | ||||
-rw-r--r-- | modules/simpletest/tests/database_test.test | 18 |
4 files changed, 33 insertions, 1 deletions
diff --git a/includes/database/mysql/query.inc b/includes/database/mysql/query.inc index 58fc8eb22..f468e486c 100644 --- a/includes/database/mysql/query.inc +++ b/includes/database/mysql/query.inc @@ -16,6 +16,10 @@ class InsertQuery_mysql extends InsertQuery { throw new PDOException('You may not specify the same field to have a value and a schema-default value.'); } + if (count($this->insertFields) + count($this->defaultFields) == 0) { + return NULL; + } + $last_insert_id = 0; $max_placeholder = 0; diff --git a/includes/database/pgsql/query.inc b/includes/database/pgsql/query.inc index dc9054a99..a489cbeae 100644 --- a/includes/database/pgsql/query.inc +++ b/includes/database/pgsql/query.inc @@ -22,6 +22,10 @@ class InsertQuery_pgsql extends InsertQuery { throw new PDOException('You may not specify the same field to have a value and a schema-default value.'); } + if (count($this->insertFields) + count($this->defaultFields) == 0) { + return NULL; + } + $schema = drupal_get_schema($this->table); $stmt = $this->connection->prepareQuery((string)$this); diff --git a/includes/database/query.inc b/includes/database/query.inc index 5ecc8ce6e..a5b2ae313 100644 --- a/includes/database/query.inc +++ b/includes/database/query.inc @@ -382,7 +382,9 @@ class InsertQuery extends Query { * was given multiple sets of values to insert, the return value is * undefined. If the query is flagged "delayed", then the insert ID * won't be created until later when the query actually runs so the - * return value is also undefined. + * return value is also undefined. If no fields are specified, this + * method will do nothing and return NULL. That makes it safe to use + * in multi-insert loops. */ public function execute() { @@ -394,6 +396,10 @@ class InsertQuery extends Query { throw new PDOException('You may not specify the same field to have a value and a schema-default value.'); } + if (count($this->insertFields) + count($this->defaultFields) == 0) { + return NULL; + } + // Each insert happens in its own query in the degenerate case. However, // we wrap it in a transaction so that it is atomic where possible. On many // databases, such as SQLite, this is also a notable performance boost. diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index e99a8c1b1..3325b189e 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -563,6 +563,24 @@ class DatabaseInsertDefaultsTestCase extends DatabaseTestCase { } /** + * Test that no action will be preformed if no fields are specified. + */ + function testDefaultEmptyInsert() { + try { + $num_records_before = (int) db_query("SELECT COUNT(*) FROM {test}")->fetchField(); + + $result = db_insert('test')->execute(); + $this->assertNull($result, t('Return NULL as no fields are specified.')); + + $num_records_after = (int) db_query("SELECT COUNT(*) FROM {test}")->fetchField(); + $this->assertIdentical($num_records_before, $num_records_after, t('Do nothing as no fields are specified.')); + } + catch (Exception $e) { + $this->assertTrue(FALSE, $e->getMessage()); + } + } + + /** * Test that we can insert fields with values and defaults in the same query. */ function testDefaultInsertWithFields() { |