diff --git a/core/lib/Drupal/Core/Database/database.api.php b/core/lib/Drupal/Core/Database/database.api.php index 07e43437734..bf2f1fd3444 100644 --- a/core/lib/Drupal/Core/Database/database.api.php +++ b/core/lib/Drupal/Core/Database/database.api.php @@ -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) ); diff --git a/core/modules/comment/src/Plugin/views/argument/UserUid.php b/core/modules/comment/src/Plugin/views/argument/UserUid.php index cd384d560d5..4ad41854bbd 100644 --- a/core/modules/comment/src/Plugin/views/argument/UserUid.php +++ b/core/modules/comment/src/Plugin/views/argument/UserUid.php @@ -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); diff --git a/core/modules/comment/src/Plugin/views/filter/UserUid.php b/core/modules/comment/src/Plugin/views/filter/UserUid.php index adee320d440..248b4609c32 100644 --- a/core/modules/comment/src/Plugin/views/filter/UserUid.php +++ b/core/modules/comment/src/Plugin/views/filter/UserUid.php @@ -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); diff --git a/core/modules/locale/src/StringDatabaseStorage.php b/core/modules/locale/src/StringDatabaseStorage.php index f925c1ad00b..8976224b05b 100644 --- a/core/modules/locale/src/StringDatabaseStorage.php +++ b/core/modules/locale/src/StringDatabaseStorage.php @@ -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 { diff --git a/core/modules/node/src/Plugin/views/filter/Access.php b/core/modules/node/src/Plugin/views/filter/Access.php index 73844e11d19..10ae9227ae5 100644 --- a/core/modules/node/src/Plugin/views/filter/Access.php +++ b/core/modules/node/src/Plugin/views/filter/Access.php @@ -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) ); diff --git a/core/modules/search/search.module b/core/modules/search/search.module index c36aa443d14..215a6c8d108 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -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); } diff --git a/core/modules/search/src/Plugin/views/argument/Search.php b/core/modules/search/src/Plugin/views/argument/Search.php index 9c3d6bb3ae5..c56cd09982a 100644 --- a/core/modules/search/src/Plugin/views/argument/Search.php +++ b/core/modules/search/src/Plugin/views/argument/Search.php @@ -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); } diff --git a/core/modules/search/src/Plugin/views/filter/Search.php b/core/modules/search/src/Plugin/views/filter/Search.php index ea52e1cebb7..14b14da5acc 100644 --- a/core/modules/search/src/Plugin/views/filter/Search.php +++ b/core/modules/search/src/Plugin/views/filter/Search.php @@ -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); } diff --git a/core/modules/search/src/SearchQuery.php b/core/modules/search/src/SearchQuery.php index 2ca2bda3a11..77ae8ff7361 100644 --- a/core/modules/search/src/SearchQuery.php +++ b/core/modules/search/src/SearchQuery.php @@ -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); } diff --git a/core/modules/search/src/ViewsSearchQuery.php b/core/modules/search/src/ViewsSearchQuery.php index fc67ed172b5..e61a7e0a1c6 100644 --- a/core/modules/search/src/ViewsSearchQuery.php +++ b/core/modules/search/src/ViewsSearchQuery.php @@ -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); } } diff --git a/core/modules/taxonomy/src/Plugin/views/argument/IndexTidDepth.php b/core/modules/taxonomy/src/Plugin/views/argument/IndexTidDepth.php index 68847168f4a..97ee5952d9c 100644 --- a/core/modules/taxonomy/src/Plugin/views/argument/IndexTidDepth.php +++ b/core/modules/taxonomy/src/Plugin/views/argument/IndexTidDepth.php @@ -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) { diff --git a/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTidDepth.php b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTidDepth.php index a2b8108ffd3..ecd32eb80e8 100644 --- a/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTidDepth.php +++ b/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTidDepth.php @@ -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) { diff --git a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php index 8dfba39636d..6043c37208e 100644 --- a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php +++ b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php @@ -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) ); diff --git a/core/modules/user/src/Plugin/views/filter/Current.php b/core/modules/user/src/Plugin/views/filter/Current.php index e7126736397..aa42529f5a5 100644 --- a/core/modules/user/src/Plugin/views/filter/Current.php +++ b/core/modules/user/src/Plugin/views/filter/Current.php @@ -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***', '<>'); diff --git a/core/modules/views/src/ManyToOneHelper.php b/core/modules/views/src/ManyToOneHelper.php index a3c4834acda..d9ed2ca7f9b 100644 --- a/core/modules/views/src/ManyToOneHelper.php +++ b/core/modules/views/src/ManyToOneHelper.php @@ -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); } diff --git a/core/modules/views/src/Plugin/views/display/EntityReference.php b/core/modules/views/src/Plugin/views/display/EntityReference.php index bf2d46034ef..8291378390b 100644 --- a/core/modules/views/src/Plugin/views/display/EntityReference.php +++ b/core/modules/views/src/Plugin/views/display/EntityReference.php @@ -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) { diff --git a/core/modules/views/src/Plugin/views/filter/StringFilter.php b/core/modules/views/src/Plugin/views/filter/StringFilter.php index 618b5845868..68739c37208 100644 --- a/core/modules/views/src/Plugin/views/filter/StringFilter.php +++ b/core/modules/views/src/Plugin/views/filter/StringFilter.php @@ -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)) { diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php index cfe593cc2ee..9252b6cb03a 100644 --- a/core/modules/views/src/Plugin/views/query/Sql.php +++ b/core/modules/views/src/Plugin/views/query/Sql.php @@ -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; diff --git a/core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php b/core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php index d6da2548237..f10710053e7 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SelectComplexTest.php @@ -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'); diff --git a/core/tests/Drupal/KernelTests/Core/Database/UpdateComplexTest.php b/core/tests/Drupal/KernelTests/Core/Database/UpdateComplexTest.php index c2946d5c396..e4289e6187f 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/UpdateComplexTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/UpdateComplexTest.php @@ -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') );