Commit 7613aa59 authored by Dries's avatar Dries
Browse files

- Patch #369314 by Berdir: make sub-selects work.

parent c25f3ac5
......@@ -1179,6 +1179,7 @@ public function compile(DatabaseConnection $connection) {
}
$operator += $operator_defaults;
$placeholders = array();
if ($condition['value'] instanceof SelectQuery) {
$placeholders[] = (string)$condition['value'];
$arguments += $condition['value']->arguments();
......@@ -1189,7 +1190,6 @@ public function compile(DatabaseConnection $connection) {
elseif (!$operator['delimiter']) {
$condition['value'] = array($condition['value']);
}
$placeholders = array();
if ($operator['use_value']) {
foreach ($condition['value'] as $value) {
$placeholder = ':db_condition_placeholder_' . $next_placeholder++;
......
......@@ -1364,6 +1364,29 @@ class DatabaseSelectSubqueryTestCase extends DatabaseTestCase {
$this->assertEqual(count($people), 1, t('Returned the correct number of rows.'));
}
/**
* Test that we can use a subquery in a FROM clause.
*/
function testConditionSubquerySelect() {
// Create a subquery, which is just a normal query object.
$subquery = db_select('test_task', 'tt');
$subquery->addField('tt', 'pid', 'pid');
$subquery->condition('tt.priority', 1);
// Create another query that joins against the virtual table resulting
// from the subquery.
$select = db_select('test_task', 'tt2');
$select->addField('tt2', 'task');
$select->condition('tt2.pid', $subquery, 'IN');
// The resulting query should be equivalent to:
// SELECT tt2.name
// FROM test tt2
// WHERE tt2.pid IN (SELECT tt.pid AS pid FROM test_task tt WHERE tt.priority=1)
$people = $select->execute()->fetchCol();
$this->assertEqual(count($people), 5, t('Returned the correct number of rows.'));
}
/**
* Test that we can use a subquery in a JOIN clause.
*/
......
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