Commit 2ddb3983 authored by catch's avatar catch

Issue #2986334 by amateescu, alexpott: Add a way to enforce an empty result...

Issue #2986334 by amateescu, alexpott: Add a way to enforce an empty result set for a database condition query
parent 7ae9670d
......@@ -156,6 +156,13 @@ public function notExists(SelectInterface $select) {
return $this->condition('', $select, 'NOT EXISTS');
}
/**
* {@inheritdoc}
*/
public function alwaysFalse() {
return $this->where('1 = 0');
}
/**
* {@inheritdoc}
*/
......
......@@ -132,6 +132,13 @@ public function exists(SelectInterface $select);
*/
public function notExists(SelectInterface $select);
/**
* Sets a condition that is always false.
*
* @return $this
*/
public function alwaysFalse();
/**
* Gets the, possibly nested, list of conditions in this conditional clause.
*
......
......@@ -60,6 +60,14 @@ public function notExists(SelectInterface $select) {
return $this;
}
/**
* {@inheritdoc}
*/
public function alwaysFalse() {
$this->condition->alwaysFalse();
return $this;
}
/**
* {@inheritdoc}
*/
......
......@@ -471,6 +471,14 @@ public function notExists(SelectInterface $select) {
return $this;
}
/**
* {@inheritdoc}
*/
public function alwaysFalse() {
$this->query->alwaysFalse();
return $this;
}
/**
* {@inheritdoc}
*/
......
......@@ -219,7 +219,7 @@ public function addField($field, $type, $langcode) {
// field no other value than 0 makes sense.
if (is_numeric($next)) {
if ($next > 0) {
$this->sqlQuery->condition('1 <> 1');
$this->sqlQuery->alwaysFalse();
}
$key++;
$next = $specifiers[$key + 1];
......
......@@ -251,6 +251,50 @@ public function testIsNotNullCondition() {
$this->assertEqual($names[1], 'Kermit', 'Correct record returned for NOT NULL age.');
}
/**
* Tests that we can force a query to return an empty result.
*/
public function testAlwaysFalseCondition() {
$names = $this->connection->select('test', 'test')
->fields('test', ['name'])
->condition('age', 27)
->execute()->fetchCol();
$this->assertCount(1, $names);
$this->assertSame($names[0], 'George');
$names = $this->connection->select('test', 'test')
->fields('test', ['name'])
->condition('age', 27)
->alwaysFalse()
->execute()->fetchCol();
$this->assertCount(0, $names);
}
/**
* Tests that we can force an extended query to return an empty result.
*/
public function testExtenderAlwaysFalseCondition() {
$names = $this->connection->select('test', 'test')
->extend('Drupal\Core\Database\Query\SelectExtender')
->fields('test', ['name'])
->condition('age', 27)
->execute()->fetchCol();
$this->assertCount(1, $names);
$this->assertSame($names[0], 'George');
$names = $this->connection->select('test', 'test')
->extend('Drupal\Core\Database\Query\SelectExtender')
->fields('test', ['name'])
->condition('age', 27)
->alwaysFalse()
->execute()->fetchCol();
$this->assertCount(0, $names);
}
/**
* Tests that we can UNION multiple Select queries together.
*
......
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