Skip to content

Commit

Permalink
(split)#1001242 by agentrickard, pwolanin, Crell, chx: Add DBTNG supp…
Browse files Browse the repository at this point in the history
…ort for EXISTS conditions.
  • Loading branch information
webchick committed Dec 31, 2010
1 parent 419f01b commit a341c57
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
86 changes: 86 additions & 0 deletions query.inc
Expand Up @@ -76,6 +76,28 @@ interface QueryConditionInterface {
*/
public function isNotNull($field);

/**
* Sets a condition that the specified subquery returns values.
*
* @param SelectQueryInterface $select
* The subquery that must contain results.
*
* @return QueryConditionInterface
* The called object.
*/
public function exists(SelectQueryInterface $select);

/**
* Sets a condition that the specified subquery returns no values.
*
* @param SelectQueryInterface $select
* The subquery that must not contain results.
*
* @return QueryConditionInterface
* The called object.
*/
public function notExists(SelectQueryInterface $select);

/**
* Gets a complete list of all conditions in this conditional clause.
*
Expand Down Expand Up @@ -726,6 +748,22 @@ class DeleteQuery extends Query implements QueryConditionInterface {
return $this;
}

/**
* Implements QueryConditionInterface::exists().
*/
public function exists(SelectQueryInterface $select) {
$this->condition->exists($select);
return $this;
}

/**
* Implements QueryConditionInterface::notExists().
*/
public function notExists(SelectQueryInterface $select) {
$this->condition->notExists($select);
return $this;
}

/**
* Implements QueryConditionInterface::conditions().
*/
Expand Down Expand Up @@ -946,6 +984,22 @@ class UpdateQuery extends Query implements QueryConditionInterface {
return $this;
}

/**
* Implements QueryConditionInterface::exists().
*/
public function exists(SelectQueryInterface $select) {
$this->condition->exists($select);
return $this;
}

/**
* Implements QueryConditionInterface::notExists().
*/
public function notExists(SelectQueryInterface $select) {
$this->condition->notExists($select);
return $this;
}

/**
* Implements QueryConditionInterface::conditions().
*/
Expand Down Expand Up @@ -1416,6 +1470,22 @@ class MergeQuery extends Query implements QueryConditionInterface {
return $this;
}

/**
* Implements QueryConditionInterface::exists().
*/
public function exists(SelectQueryInterface $select) {
$this->condition->exists($select);
return $this;
}

/**
* Implements QueryConditionInterface::notExists().
*/
public function notExists(SelectQueryInterface $select) {
$this->condition->notExists($select);
return $this;
}

/**
* Implements QueryConditionInterface::conditions().
*/
Expand Down Expand Up @@ -1614,6 +1684,20 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
return $this->condition($field, NULL, 'IS NOT NULL');
}

/**
* Implements QueryConditionInterface::exists().
*/
public function exists(SelectQueryInterface $select) {
return $this->condition('', $select, 'EXISTS');
}

/**
* Implements QueryConditionInterface::notExists().
*/
public function notExists(SelectQueryInterface $select) {
return $this->condition('', $select, 'NOT EXISTS');
}

/**
* Implements QueryConditionInterface::conditions().
*/
Expand Down Expand Up @@ -1756,6 +1840,8 @@ class DatabaseCondition implements QueryConditionInterface, Countable {
'BETWEEN' => array('delimiter' => ' AND '),
'IN' => array('delimiter' => ', ', 'prefix' => ' (', 'postfix' => ')'),
'NOT IN' => array('delimiter' => ', ', 'prefix' => ' (', 'postfix' => ')'),
'EXISTS' => array('prefix' => ' (', 'postfix' => ')'),
'NOT EXISTS' => array('prefix' => ' (', 'postfix' => ')'),
'IS NULL' => array('use_value' => FALSE),
'IS NOT NULL' => array('use_value' => FALSE),
// Use backslash for escaping wildcard characters.
Expand Down
32 changes: 31 additions & 1 deletion select.inc
Expand Up @@ -810,6 +810,16 @@ class SelectQueryExtender implements SelectQueryInterface {
return $this;
}

public function exists(SelectQueryInterface $select) {
$this->query->exists($select);
return $this;
}

public function notExists(SelectQueryInterface $select) {
$this->query->notExists($select);
return $this;
}

public function __toString() {
return (string) $this->query;
}
Expand Down Expand Up @@ -1022,6 +1032,16 @@ class SelectQuery extends Query implements SelectQueryInterface {
return $this;
}

public function exists(SelectQueryInterface $select) {
$this->where->exists($select);
return $this;
}

public function notExists(SelectQueryInterface $select) {
$this->where->notExists($select);
return $this;
}

public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL) {
return $this->where->compile($connection, isset($queryPlaceholder) ? $queryPlaceholder : $this);
}
Expand Down Expand Up @@ -1069,7 +1089,17 @@ class SelectQuery extends Query implements SelectQueryInterface {
$this->having->isNotNull($field);
return $this;
}


public function havingExists(SelectQueryInterface $select) {
$this->having->exists($select);
return $this;
}

public function havingNotExists(SelectQueryInterface $select) {
$this->having->notExists($select);
return $this;
}

public function forUpdate($set = TRUE) {
if (isset($set)) {
$this->forUpdate = $set;
Expand Down

0 comments on commit a341c57

Please sign in to comment.