From 03c8cc9f196dd144a6259b58c1402ea9a2ad6f18 Mon Sep 17 00:00:00 2001 From: Lorenz Schori Date: Fri, 31 Aug 2012 12:02:37 +0200 Subject: [PATCH] Issue #1266572 by znerol, Damien Tournoud: workaround in SQLite causes some update to be missed. --- includes/database/sqlite/query.inc | 28 ++------------------------ modules/simpletest/tests/database_test.test | 15 ++++++++++++++ 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/includes/database/sqlite/query.inc b/includes/database/sqlite/query.inc index f0ff10d..20645e3 100644 --- a/includes/database/sqlite/query.inc +++ b/includes/database/sqlite/query.inc @@ -57,41 +57,19 @@ class InsertQuery_sqlite extends InsertQuery { * we don't select those rows. * * A query like this one: - * UPDATE test SET name = 'newname' WHERE tid = 1 + * UPDATE test SET col1 = 'newcol1', col2 = 'newcol2' WHERE tid = 1 * will become: - * UPDATE test SET name = 'newname' WHERE tid = 1 AND name <> 'newname' + * UPDATE test SET col1 = 'newcol1', col2 = 'newcol2' WHERE tid = 1 AND (col1 <> 'newcol1' OR col2 <> 'newcol2') */ class UpdateQuery_sqlite extends UpdateQuery { - /** - * Helper function that removes the fields that are already in a condition. - * - * @param $fields - * The fields. - * @param QueryConditionInterface $condition - * A database condition. - */ - protected function removeFieldsInCondition(&$fields, QueryConditionInterface $condition) { - foreach ($condition->conditions() as $child_condition) { - if (isset($child_condition['field'])) { - if ($child_condition['field'] instanceof ConditionInterface) { - $this->removeFieldsInCondition($fields, $child_condition['field']); - } - else { - unset($fields[$child_condition['field']]); - } - } - } - } public function execute() { if (!empty($this->queryOptions['sqlite_return_matched_rows'])) { return parent::execute(); } - // Get the fields used in the update query, and remove those that are already - // in the condition. + // Get the fields used in the update query. $fields = $this->expressionFields + $this->fields; - $this->removeFieldsInCondition($fields, $this->condition); // Add the inverse of the fields to the condition. $condition = new DatabaseCondition('OR'); diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index 6608336..e356107 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -809,6 +809,21 @@ class DatabaseUpdateTestCase extends DatabaseTestCase { ->execute(); $this->assertIdentical($num_rows, 3, t('Number of affected rows are returned.')); } + + /** + * Confirm that we can update the primary key of a record successfully. + * Bug: http://drupal.org/node/1266572 + */ + function testPrimaryKeyUpdate() { + $num_updated = db_update('test') + ->fields(array('id' => 42, 'name' => 'John')) + ->condition('id', 1) + ->execute(); + $this->assertIdentical($num_updated, 1, t('Updated 1 record.')); + + $saved_name= db_query('SELECT name FROM {test} WHERE id = :id', array(':id' => 42))->fetchField(); + $this->assertIdentical($saved_name, 'John', t('Updated primary key successfully.')); + } } /** -- 1.7.2.5