- Patch #564852 by justinrandell, mcarbone, Crell: fixed subselect in WHERE condition with multiple placeholders cause invalid/duplicate placeholders.

merge-requests/26/head
Dries Buytaert 2009-11-23 22:31:42 +00:00
parent 226341242b
commit a902f664d9
2 changed files with 26 additions and 2 deletions

View File

@ -925,6 +925,7 @@ class DeleteQuery extends Query implements QueryConditionInterface {
$query = 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} '; $query = 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
if (count($this->condition)) { if (count($this->condition)) {
$this->condition->compile($this->connection, $this); $this->condition->compile($this->connection, $this);
$query .= "\nWHERE " . $this->condition; $query .= "\nWHERE " . $this->condition;
} }
@ -1234,6 +1235,8 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
} }
else { else {
// It's a structured condition, so parse it out accordingly. // 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) { if ($condition['field'] instanceof QueryConditionInterface) {
// Compile the sub-condition recursively and add it to the list. // Compile the sub-condition recursively and add it to the list.
$condition['field']->compile($connection, $queryPlaceholder); $condition['field']->compile($connection, $queryPlaceholder);
@ -1257,7 +1260,8 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
$operator += $operator_defaults; $operator += $operator_defaults;
$placeholders = array(); $placeholders = array();
if ($condition['value'] instanceof SelectQuery) { if ($condition['value'] instanceof SelectQueryInterface) {
$condition['value']->compile($connection, $queryPlaceholder);
$placeholders[] = (string)$condition['value']; $placeholders[] = (string)$condition['value'];
$arguments += $condition['value']->arguments(); $arguments += $condition['value']->arguments();
} }
@ -1275,7 +1279,6 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
} }
} }
$condition_fragments[] = ' (' . $condition['field'] . ' ' . $operator['operator'] . ' ' . $operator['prefix'] . implode($operator['delimiter'], $placeholders) . $operator['postfix'] . ') '; $condition_fragments[] = ' (' . $condition['field'] . ' ' . $operator['operator'] . ' ' . $operator['prefix'] . implode($operator['delimiter'], $placeholders) . $operator['postfix'] . ') ';
} }
} }
} }

View File

@ -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. * Confirm that we can delete a single record successfully.
*/ */