Commit a902f664 authored by Dries's avatar Dries

- Patch #564852 by justinrandell, mcarbone, Crell: fixed subselect in WHERE...

- Patch #564852 by justinrandell, mcarbone, Crell: fixed subselect in WHERE condition with multiple placeholders cause invalid/duplicate placeholders.
parent 22634124
......@@ -925,6 +925,7 @@ public function __toString() {
$query = 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
if (count($this->condition)) {
$this->condition->compile($this->connection, $this);
$query .= "\nWHERE " . $this->condition;
}
......@@ -1234,6 +1235,8 @@ public function compile(DatabaseConnection $connection, QueryPlaceholderInterfac
}
else {
// It's a structured condition, so parse it out accordingly.
// Note that $condition['field'] will only be an object for a dependent
// DatabaseCondition object, not for a dependent subquery.
if ($condition['field'] instanceof QueryConditionInterface) {
// Compile the sub-condition recursively and add it to the list.
$condition['field']->compile($connection, $queryPlaceholder);
......@@ -1257,7 +1260,8 @@ public function compile(DatabaseConnection $connection, QueryPlaceholderInterfac
$operator += $operator_defaults;
$placeholders = array();
if ($condition['value'] instanceof SelectQuery) {
if ($condition['value'] instanceof SelectQueryInterface) {
$condition['value']->compile($connection, $queryPlaceholder);
$placeholders[] = (string)$condition['value'];
$arguments += $condition['value']->arguments();
}
......@@ -1275,7 +1279,6 @@ public function compile(DatabaseConnection $connection, QueryPlaceholderInterfac
}
}
$condition_fragments[] = ' (' . $condition['field'] . ' ' . $operator['operator'] . ' ' . $operator['prefix'] . implode($operator['delimiter'], $placeholders) . $operator['postfix'] . ') ';
}
}
}
......
......@@ -944,6 +944,27 @@ class DatabaseDeleteTruncateTestCase extends DatabaseTestCase {
);
}
/**
* Confirm that we can use a subselect in a delete successfully.
*/
function testSubselectDelete() {
$num_records_before = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
$pid_to_delete = db_query("SELECT * FROM {test_task} WHERE task = 'sleep'")->fetchField();
$subquery = db_select('test', 't')
->fields('t', array('id'))
->condition('t.id', array($pid_to_delete), 'IN');
$delete = db_delete('test_task')
->condition('task', 'sleep')
->condition('pid', $subquery, 'IN');
$num_deleted = $delete->execute();
$this->assertEqual($num_deleted, 1, t("Deleted 1 record."));
$num_records_after = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
$this->assertEqual($num_records_before, $num_records_after + $num_deleted, t('Deletion adds up.'));
}
/**
* Confirm that we can delete a single record successfully.
*/
......
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