Commit 4399ae73 authored by webchick's avatar webchick

Issue #1837118 by chx, Berdir: Fixed UPDATE foo SET bar=(SELECT...) is not supported.

parent 6ed4e11b
......@@ -212,6 +212,10 @@ public function execute() {
if (!empty($data['arguments'])) {
$update_values += $data['arguments'];
}
if ($data['expression'] instanceof SelectInterface) {
$data['expression']->compile($this->connection, $this);
$update_values += $data['expression']->arguments();
}
unset($fields[$field]);
}
......@@ -245,6 +249,11 @@ public function __toString() {
$fields = $this->fields;
$update_fields = array();
foreach ($this->expressionFields as $field => $data) {
if ($data['expression'] instanceof SelectInterface) {
// Compile and cast expression subquery to a string.
$data['expression']->compile($this->connection, $this);
$data['expression'] = ' (' . $data['expression'] . ')';
}
$update_fields[] = $field . '=' . $data['expression'];
unset($fields[$field]);
}
......
......@@ -132,4 +132,28 @@ function testUpdateOnlyExpression() {
$after_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchField();
$this->assertEqual($before_age + 4, $after_age, 'Age updated correctly');
}
/**
* Test UPDATE with a subselect value.
*/
function testSubSelectUpdate() {
$subselect = db_select('test_task', 't');
$subselect->addExpression('MAX(priority) + :increment', 'max_priority', array(':increment' => 30));
// Clone this to make sure we are running a different query when
// asserting.
$select = clone $subselect;
$query = db_update('test')
->expression('age', $subselect)
->condition('name', 'Ringo');
// Save the query for a __toString test later.
$string_test = $query;
$query->execute();
$after_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchField();
$expected_age = $select->execute()->fetchField();
$this->assertEqual($after_age, $expected_age);
// Replace whitespace with a single space.
$query_string = preg_replace('/\s+/', ' ', $string_test);
$this->assertIdentical('UPDATE {test} SET age= (SELECT MAX(priority) + :increment AS max_priority FROM {test_task} t) WHERE (name = :db_condition_placeholder_0)', trim($query_string));
}
}
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