Commit e1652e99 authored by Dries's avatar Dries
Browse files

- Patch #374940 by Dave Reid et al: fixed MergeQuery problem.

parent 916b11af
......@@ -143,6 +143,12 @@ public function __toString() {
}
}
// If the merge query has no fields to update, add the first key as an
// update field so the query will not fail if a duplicate key is found.
if (!$update_fields && !$this->expressionFields) {
$update_fields = array_slice($this->keyFields, 0, 1, TRUE);
}
$insert_fields = $this->insertFields + $this->keyFields;
$query = "INSERT INTO {" . $this->table . '} (' . implode(', ', array_keys($insert_fields)) . ') VALUES ';
......
......@@ -676,14 +676,17 @@ public function execute() {
unset($update_fields[$exclude_field]);
}
}
$update = $this->connection->update($this->table, $this->queryOptions)->fields($update_fields);
foreach ($this->keyFields as $field => $value) {
$update->condition($field, $value);
}
foreach ($this->expressionFields as $field => $expression) {
$update->expression($field, $expression['expression'], $expression['arguments']);
if ($update_fields || $this->expressionFields) {
// Only run the update if there are no fields or expressions to update.
$update = $this->connection->update($this->table, $this->queryOptions)->fields($update_fields);
foreach ($this->keyFields as $field => $value) {
$update->condition($field, $value);
}
foreach ($this->expressionFields as $field => $expression) {
$update->expression($field, $expression['expression'], $expression['arguments']);
}
$update->execute();
}
$update->execute();
}
else {
// If there is no existing record, run an insert query.
......
......@@ -958,6 +958,58 @@ class DatabaseMergeTestCase extends DatabaseTestCase {
$this->assertEqual($person->job, 'Speaker', t('Job set correctly.'));
}
/**
* Test that we can merge-insert without any update fields.
*/
function testMergeInsertWithoutUpdate() {
$num_records_before = db_query("SELECT COUNT(*) FROM {test_people}")->fetchField();
db_merge('test_people')
->key(array('job' => 'Presenter'))
->execute();
$num_records_after = db_query("SELECT COUNT(*) FROM {test_people}")->fetchField();
$this->assertEqual($num_records_before + 1, $num_records_after, t('Merge inserted properly.'));
$person = db_query("SELECT * FROM {test_people} WHERE job = :job", array(':job' => 'Presenter'))->fetch();
$this->assertEqual($person->name, '', t('Name set correctly.'));
$this->assertEqual($person->age, 0, t('Age set correctly.'));
$this->assertEqual($person->job, 'Presenter', t('Job set correctly.'));
}
/**
* Confirm that we can merge-update without any update fields.
*/
function testMergeUpdateWithoutUpdate() {
$num_records_before = db_query("SELECT COUNT(*) FROM {test_people}")->fetchField();
db_merge('test_people')
->key(array('job' => 'Speaker'))
->execute();
$num_records_after = db_query("SELECT COUNT(*) FROM {test_people}")->fetchField();
$this->assertEqual($num_records_before, $num_records_after, t('Merge skipped properly.'));
$person = db_query("SELECT * FROM {test_people} WHERE job = :job", array(':job' => 'Speaker'))->fetch();
$this->assertEqual($person->name, 'Meredith', t('Name skipped correctly.'));
$this->assertEqual($person->age, 30, t('Age skipped correctly.'));
$this->assertEqual($person->job, 'Speaker', t('Job skipped correctly.'));
db_merge('test_people')
->key(array('job' => 'Speaker'))
->fields(array('age' => 31))
->updateExcept(array('age'))
->execute();
$num_records_after = db_query("SELECT COUNT(*) FROM {test_people}")->fetchField();
$this->assertEqual($num_records_before, $num_records_after, t('Merge skipped properly.'));
$person = db_query("SELECT * FROM {test_people} WHERE job = :job", array(':job' => 'Speaker'))->fetch();
$this->assertEqual($person->name, 'Meredith', t('Name skipped correctly.'));
$this->assertEqual($person->age, 30, t('Age skipped correctly.'));
$this->assertEqual($person->job, 'Speaker', t('Job skipped correctly.'));
}
/**
* Test that an invalid merge query throws an exception like it is supposed to.
*/
......
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