Issue #2117305 by disasm, Pavan B S, ieguskiza, Jo Fitzgerald: Replace usages of db_or() and db_and()

8.4.x
Alex Pott 2017-03-03 11:37:33 +00:00
parent 0d27b06355
commit 23d140fd6c
20 changed files with 59 additions and 37 deletions

View File

@ -5,6 +5,8 @@
* Hooks related to the Database system and the Schema API.
*/
use Drupal\Core\Database\Query\Condition;
/**
* @defgroup database Database abstraction layer
* @{
@ -432,11 +434,11 @@ function hook_query_TAG_alter(Drupal\Core\Database\Query\AlterableInterface $que
if (!\Drupal::currentUser()->hasPermission('bypass node access')) {
// The node_access table has the access grants for any given node.
$access_alias = $query->join('node_access', 'na', '%alias.nid = n.nid');
$or = db_or();
$or = new Condition('OR');
// If any grant exists for the specified user, then user has access to the node for the specified operation.
foreach (node_access_grants($op, $query->getMetaData('account')) as $realm => $gids) {
foreach ($gids as $gid) {
$or->condition(db_and()
$or->condition((new Condition('AND'))
->condition($access_alias . '.gid', $gid)
->condition($access_alias . '.realm', $realm)
);

View File

@ -3,6 +3,7 @@
namespace Drupal\comment\Plugin\views\argument;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -90,7 +91,7 @@ class UserUid extends ArgumentPluginBase {
$subselect->where("c.entity_id = $this->tableAlias.$entity_id");
$subselect->condition('c.entity_type', $entity_type);
$condition = db_or()
$condition = (new Condition('OR'))
->condition("$this->tableAlias.uid", $this->argument, '=')
->exists($subselect);

View File

@ -2,6 +2,7 @@
namespace Drupal\comment\Plugin\views\filter;
use Drupal\Core\Database\Query\Condition;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
/**
@ -26,7 +27,7 @@ class UserUid extends FilterPluginBase {
$subselect->where("c.entity_id = $this->tableAlias.$entity_id");
$subselect->condition('c.entity_type', $entity_type);
$condition = db_or()
$condition = (new Condition('OR'))
->condition("$this->tableAlias.uid", $this->value, $this->operator)
->exists($subselect);

View File

@ -3,6 +3,7 @@
namespace Drupal\locale;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
/**
* Defines a class to store localized strings in the database.
@ -416,7 +417,7 @@ class StringDatabaseStorage implements StringStorageInterface {
elseif ($table_alias == 't' && $join === 'leftJoin') {
// Conditions for target fields when doing an outer join only make
// sense if we add also OR field IS NULL.
$query->condition(db_or()
$query->condition((new Condition('OR'))
->condition($field_alias, (array) $value, 'IN')
->isNull($field_alias)
);
@ -429,7 +430,7 @@ class StringDatabaseStorage implements StringStorageInterface {
// Process other options, string filter, query limit, etc.
if (!empty($options['filters'])) {
if (count($options['filters']) > 1) {
$filter = db_or();
$filter = new Condition('OR');
$query->condition($filter);
}
else {

View File

@ -2,6 +2,7 @@
namespace Drupal\node\Plugin\views\filter;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
@ -27,10 +28,10 @@ class Access extends FilterPluginBase {
$account = $this->view->getUser();
if (!$account->hasPermission('bypass node access')) {
$table = $this->ensureMyTable();
$grants = db_or();
$grants = new Condition('OR');
foreach (node_access_grants('view', $account) as $realm => $gids) {
foreach ($gids as $gid) {
$grants->condition(db_and()
$grants->condition((new Condition('AND'))
->condition($table . '.gid', $gid)
->condition($table . '.realm', $realm)
);

View File

@ -8,6 +8,7 @@
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
@ -218,7 +219,7 @@ function search_update_totals() {
// search_total. We use a LEFT JOIN between the two tables and keep only the
// rows which fail to join.
$result = db_query("SELECT t.word AS realword, i.word FROM {search_total} t LEFT JOIN {search_index} i ON t.word = i.word WHERE i.word IS NULL", array(), array('target' => 'replica'));
$or = db_or();
$or = new Condition('OR');
foreach ($result as $word) {
$or->condition('word', $word->realword);
}

View File

@ -2,6 +2,7 @@
namespace Drupal\search\Plugin\views\argument;
use Drupal\Core\Database\Query\Condition;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
@ -76,7 +77,7 @@ class Search extends ArgumentPluginBase {
else {
$search_index = $this->ensureMyTable();
$search_condition = db_and();
$search_condition = new Condition('AND');
// Create a new join to relate the 'search_total' table to our current 'search_index' table.
$definition = array(
@ -109,7 +110,7 @@ class Search extends ArgumentPluginBase {
// Add the keyword conditions, as is done in
// SearchQuery::prepareAndNormalize(), but simplified because we are
// only concerned with relevance ranking so we do not need to normalize.
$or = db_or();
$or = new Condition('OR');
foreach ($words as $word) {
$or->condition("$search_index.word", $word);
}

View File

@ -2,6 +2,7 @@
namespace Drupal\search\Plugin\views\filter;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
@ -150,7 +151,7 @@ class Search extends FilterPluginBase {
else {
$search_index = $this->ensureMyTable();
$search_condition = db_and();
$search_condition = new Condition('AND');
// Create a new join to relate the 'search_total' table to our current
// 'search_index' table.
@ -184,7 +185,7 @@ class Search extends FilterPluginBase {
// Add the keyword conditions, as is done in
// SearchQuery::prepareAndNormalize(), but simplified because we are
// only concerned with relevance ranking so we do not need to normalize.
$or = db_or();
$or = new Condition('OR');
foreach ($words as $word) {
$or->condition("$search_index.word", $word);
}

View File

@ -2,6 +2,7 @@
namespace Drupal\search;
use Drupal\Core\Database\Query\Condition;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\Query\SelectExtender;
use Drupal\Core\Database\Query\SelectInterface;
@ -205,7 +206,7 @@ class SearchQuery extends SelectExtender {
$this->addTag('search_' . $type);
// Initialize conditions and status.
$this->conditions = db_and();
$this->conditions = new Condition('AND');
$this->status = 0;
return $this;
@ -313,7 +314,7 @@ class SearchQuery extends SelectExtender {
}
$has_or = TRUE;
$has_new_scores = FALSE;
$queryor = db_or();
$queryor = new Condition('OR');
foreach ($key as $or) {
list($num_new_scores) = $this->parseWord($or);
$has_new_scores |= $num_new_scores;
@ -401,7 +402,7 @@ class SearchQuery extends SelectExtender {
}
// Build the basic search query: match the entered keywords.
$or = db_or();
$or = new Condition('OR');
foreach ($this->words as $word) {
$or->condition('i.word', $word);
}

View File

@ -74,8 +74,8 @@ class ViewsSearchQuery extends SearchQuery {
$conditions =& $condition['field']->conditions();
foreach ($conditions as $key => &$subcondition) {
if (is_numeric($key)) {
// As conditions can have subconditions, for example db_or(), the
// function has to be called recursively.
// As conditions can be nested, the function has to be called
// recursively.
$this->conditionReplaceString($search, $replace, $subcondition);
}
}

View File

@ -2,6 +2,7 @@
namespace Drupal\taxonomy\Plugin\views\argument;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@ -106,7 +107,7 @@ class IndexTidDepth extends ArgumentPluginBase implements ContainerFactoryPlugin
// Now build the subqueries.
$subquery = db_select('taxonomy_index', 'tn');
$subquery->addField('tn', 'nid');
$where = db_or()->condition('tn.tid', $tids, $operator);
$where = (new Condition('OR'))->condition('tn.tid', $tids, $operator);
$last = "tn";
if ($this->options['depth'] > 0) {

View File

@ -2,6 +2,7 @@
namespace Drupal\taxonomy\Plugin\views\filter;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
/**
@ -72,7 +73,7 @@ class TaxonomyIndexTidDepth extends TaxonomyIndexTid {
// Now build the subqueries.
$subquery = db_select('taxonomy_index', 'tn');
$subquery->addField('tn', 'nid');
$where = db_or()->condition('tn.tid', $this->value, $operator);
$where = (new Condition('OR'))->condition('tn.tid', $this->value, $operator);
$last = "tn";
if ($this->options['depth'] > 0) {

View File

@ -3,6 +3,7 @@
namespace Drupal\user\Plugin\EntityReferenceSelection;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
@ -228,17 +229,17 @@ class UserSelection extends DefaultSelection {
// Re-add the condition and a condition on uid = 0 so that we end up
// with a query in the form:
// WHERE (name LIKE :name) OR (:anonymous_name LIKE :name AND uid = 0)
$or = db_or();
$or = new Condition('OR');
$or->condition($condition['field'], $condition['value'], $condition['operator']);
// Sadly, the Database layer doesn't allow us to build a condition
// in the form ':placeholder = :placeholder2', because the 'field'
// part of a condition is always escaped.
// As a (cheap) workaround, we separately build a condition with no
// field, and concatenate the field and the condition separately.
$value_part = db_and();
$value_part = new Condition('AND');
$value_part->condition('anonymous_name', $condition['value'], $condition['operator']);
$value_part->compile($this->connection, $query);
$or->condition(db_and()
$or->condition((new Condition('AND'))
->where(str_replace('anonymous_name', ':anonymous_name', (string) $value_part), $value_part->arguments() + array(':anonymous_name' => \Drupal::config('user.settings')->get('anonymous')))
->condition('base_table.uid', 0)
);

View File

@ -2,6 +2,7 @@
namespace Drupal\user\Plugin\views\filter;
use Drupal\Core\Database\Query\Condition;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\filter\BooleanOperator;
@ -28,7 +29,7 @@ class Current extends BooleanOperator {
$this->ensureMyTable();
$field = $this->tableAlias . '.' . $this->realField . ' ';
$or = db_or();
$or = new Condition('OR');
if (empty($this->value)) {
$or->condition($field, '***CURRENT_USER***', '<>');

View File

@ -2,6 +2,7 @@
namespace Drupal\views;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\HandlerBase;
@ -268,8 +269,8 @@ class ManyToOneHelper {
$options['group'] = 0;
}
// add_condition determines whether a single expression is enough(FALSE) or the
// conditions should be added via an db_or()/db_and() (TRUE).
// If $add_condition is set to FALSE, a single expression is enough. If it
// is set to TRUE, conditions will be added.
$add_condition = TRUE;
if ($operator == 'not') {
$value = NULL;
@ -326,7 +327,7 @@ class ManyToOneHelper {
if ($add_condition) {
$field = $this->handler->realField;
$clause = $operator == 'or' ? db_or() : db_and();
$clause = $operator == 'or' ? new Condition('OR') : new Condition('AND');
foreach ($this->handler->tableAliases as $value => $alias) {
$clause->condition("$alias.$field", $value);
}

View File

@ -2,6 +2,8 @@
namespace Drupal\views\Plugin\views\display;
use Drupal\Core\Database\Query\Condition;
/**
* The plugin that handles an EntityReference display.
*
@ -131,7 +133,7 @@ class EntityReference extends DisplayPluginBase {
}
// Multiple search fields are OR'd together.
$conditions = db_or();
$conditions = new Condition('OR');
// Build the condition using the selected search fields.
foreach ($style_options['options']['search_fields'] as $field_id) {

View File

@ -2,6 +2,7 @@
namespace Drupal\views\Plugin\views\filter;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
/**
@ -265,7 +266,7 @@ class StringFilter extends FilterPluginBase {
}
protected function opContainsWord($field) {
$where = $this->operator == 'word' ? db_or() : db_and();
$where = $this->operator == 'word' ? new Condition('OR') : new Condition('AND');
// Don't filter on empty strings.
if (empty($this->value)) {

View File

@ -5,6 +5,7 @@ namespace Drupal\views\Plugin\views\query;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
@ -830,7 +831,7 @@ class Sql extends QueryPluginBase {
* @code
* $this->query->addWhere(
* $this->options['group'],
* db_or()
* (new Condition('OR'))
* ->condition($field, $value, 'NOT IN')
* ->condition($field, $value, 'IS NULL')
* );
@ -1056,13 +1057,13 @@ class Sql extends QueryPluginBase {
$has_arguments = FALSE;
$has_filter = FALSE;
$main_group = db_and();
$filter_group = $this->groupOperator == 'OR' ? db_or() : db_and();
$main_group = new Condition('AND');
$filter_group = $this->groupOperator == 'OR' ? new Condition('OR') : new Condition('AND');
foreach ($this->$where as $group => $info) {
if (!empty($info['conditions'])) {
$sub_group = $info['type'] == 'OR' ? db_or() : db_and();
$sub_group = $info['type'] == 'OR' ? new Condition('OR') : new Condition('AND');
foreach ($info['conditions'] as $clause) {
if ($clause['operator'] == 'formula') {
$has_condition = TRUE;

View File

@ -3,6 +3,7 @@
namespace Drupal\KernelTests\Core\Database;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\RowCountException;
use Drupal\user\Entity\User;
@ -312,7 +313,7 @@ class SelectComplexTest extends DatabaseTestBase {
$query = db_select('test');
$query->addField('test', 'job');
$query->condition('name', 'Paul');
$query->condition(db_or()->condition('age', 26)->condition('age', 27));
$query->condition((new Condition('OR'))->condition('age', 26)->condition('age', 27));
$job = $query->execute()->fetchField();
$this->assertEqual($job, 'Songwriter', 'Correct data retrieved.');
@ -395,7 +396,7 @@ class SelectComplexTest extends DatabaseTestBase {
public function testJoinConditionObject() {
// Same test as testDefaultJoin, but with a Condition object.
$query = db_select('test_task', 't');
$join_cond = db_and()->where('t.pid = p.id');
$join_cond = (new Condition('AND'))->where('t.pid = p.id');
$people_alias = $query->join('test', 'p', $join_cond);
$name_field = $query->addField($people_alias, 'name', 'name');
$query->addField('t', 'task', 'task');
@ -418,7 +419,7 @@ class SelectComplexTest extends DatabaseTestBase {
// Test a condition object that creates placeholders.
$t1_name = 'John';
$t2_name = 'George';
$join_cond = db_and()
$join_cond = (new Condition('AND'))
->condition('t1.name', $t1_name)
->condition('t2.name', $t2_name);
$query = db_select('test', 't1');

View File

@ -2,6 +2,8 @@
namespace Drupal\KernelTests\Core\Database;
use Drupal\Core\Database\Query\Condition;
/**
* Tests the Update query builder, complex queries.
*
@ -15,7 +17,7 @@ class UpdateComplexTest extends DatabaseTestBase {
function testOrConditionUpdate() {
$update = db_update('test')
->fields(array('job' => 'Musician'))
->condition(db_or()
->condition((new Condition('OR'))
->condition('name', 'John')
->condition('name', 'Paul')
);