Unverified Commit f124d526 authored by alexpott's avatar alexpott
Browse files

Issue #3130655 by daffie, andypost, Beakerboy, ravi.shankar, alexpott,...

Issue #3130655 by daffie, andypost, Beakerboy, ravi.shankar, alexpott, mradcliffe, Lendude: Deprecate creating an instance of the class Drupal\Core\Database\Query\Condition with the new keyword
parent cf5d3de9
......@@ -1171,7 +1171,10 @@ public function schema() {
*/
public function condition($conjunction) {
$class = $this->getDriverClass('Condition');
return new $class($conjunction);
// Creating an instance of the class Drupal\Core\Database\Query\Condition
// should only be created from the database layer. This will allow database
// drivers to override the default Condition class.
return new $class($conjunction, FALSE);
}
/**
......
......@@ -78,8 +78,19 @@ class Condition implements ConditionInterface, \Countable {
*
* @param string $conjunction
* The operator to use to combine conditions: 'AND' or 'OR'.
* @param bool $trigger_deprecation
* If TRUE then trigger the deprecation warning.
*
* @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Creating an
* instance of this class is deprecated.
*
* @see https://www.drupal.org/node/3159568
*/
public function __construct($conjunction) {
public function __construct($conjunction, $trigger_deprecation = TRUE) {
if ($trigger_deprecation) {
@trigger_error('Creating an instance of this class is deprecated in drupal:9.1.0 and is removed in drupal:10.0.0. Use Database::getConnection()->condition() instead. See https://www.drupal.org/node/3159568', E_USER_DEPRECATED);
}
$this->conditions['#conjunction'] = $conjunction;
}
......
......@@ -179,4 +179,14 @@ public function &getComments() {
return $this->comments;
}
/**
* Gets the database connection to be used for the query.
*
* @return \Drupal\Core\Database\Connection
* The database connection to be used for the query.
*/
public function getConnection() {
return $this->connection;
}
}
......@@ -3,7 +3,6 @@
namespace Drupal\Core\Entity\Query\Sql;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Database\Query\Condition as SqlCondition;
use Drupal\Core\Entity\Query\ConditionBase;
use Drupal\Core\Entity\Query\ConditionInterface;
......@@ -40,7 +39,7 @@ public function compile($conditionContainer) {
$tables = $this->query->getTables($sql_query);
foreach ($this->conditions as $condition) {
if ($condition['field'] instanceof ConditionInterface) {
$sql_condition = new SqlCondition($condition['field']->getConjunction());
$sql_condition = $sql_query->getConnection()->condition($condition['field']->getConjunction());
// Add the SQL query to the object before calling this method again.
$sql_condition->sqlQuery = $sql_query;
$condition['field']->nestedInsideOrCondition = $this->nestedInsideOrCondition || strtoupper($this->conjunction) === 'OR';
......
......@@ -5,7 +5,6 @@
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\Query\ConditionAggregateBase;
use Drupal\Core\Entity\Query\ConditionAggregateInterface;
use Drupal\Core\Database\Query\Condition as SqlCondition;
use Drupal\Core\Entity\Query\QueryBase;
/**
......@@ -26,7 +25,7 @@ public function compile($conditionContainer) {
$tables = new Tables($sql_query);
foreach ($this->conditions as $condition) {
if ($condition['field'] instanceof ConditionAggregateInterface) {
$sql_condition = new SqlCondition($condition['field']->getConjunction());
$sql_condition = $sql_query->getConnection()->condition($condition['field']->getConjunction());
// Add the SQL query to the object before calling this method again.
$sql_condition->sqlQuery = $sql_query;
$condition['field']->compile($sql_condition);
......
......@@ -3,7 +3,6 @@
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;
......@@ -91,7 +90,7 @@ public function query($group_by = FALSE) {
$subselect->where("c.entity_id = $this->tableAlias.$entity_id");
$subselect->condition('c.entity_type', $entity_type);
$condition = (new Condition('OR'))
$condition = ($this->view->query->getConnection()->condition('OR'))
->condition("$this->tableAlias.uid", $this->argument, '=')
->exists($subselect);
......
......@@ -3,7 +3,6 @@
namespace Drupal\comment\Plugin\views\filter;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
/**
......@@ -28,7 +27,7 @@ public function query() {
$subselect->where("c.entity_id = $this->tableAlias.$entity_id");
$subselect->condition('c.entity_type', $entity_type);
$condition = (new Condition('OR'))
$condition = ($this->view->query->getConnection()->condition('OR'))
->condition("$this->tableAlias.uid", $this->value, $this->operator)
->exists($subselect);
......
......@@ -4,7 +4,6 @@
use Drupal\content_moderation\Plugin\views\ModerationStateJoinViewsHandlerTrait;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
......@@ -174,11 +173,11 @@ protected function opSimple() {
// The values are strings composed from the workflow ID and the state ID, so
// we need to create a complex WHERE condition.
$field = new Condition('OR');
$field = $this->view->query->getConnection()->condition('OR');
foreach ((array) $this->value as $value) {
list($workflow_id, $state_id) = explode('-', $value, 2);
$and = new Condition('AND');
$and = $this->view->query->getConnection()->condition('AND');
$and
->condition("$this->tableAlias.workflow", $workflow_id, '=')
->condition("$this->tableAlias.$this->realField", $state_id, $operator);
......
......@@ -2,7 +2,6 @@
namespace Drupal\file\Plugin\migrate\source\d7;
use Drupal\Core\Database\Query\Condition;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
......@@ -58,7 +57,7 @@ public function query() {
$schemes = array_map([$this->getDatabase(), 'escapeLike'], $schemes);
// Add conditions, uri LIKE 'public://%' OR uri LIKE 'private://%'.
$conditions = new Condition('OR');
$conditions = $this->getDatabase()->condition('OR');
foreach ($schemes as $scheme) {
$conditions->condition('uri', $scheme . '%', 'LIKE');
}
......
......@@ -6,7 +6,6 @@
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\Config;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\PagerSelectExtender;
use Drupal\Core\Database\StatementInterface;
use Drupal\Core\Language\LanguageInterface;
......@@ -454,7 +453,7 @@ public function indexStatus() {
$query = $this->database->select('help_search_items', 'hsi');
$query->addExpression('COUNT(DISTINCT(hsi.sid))');
$query->leftJoin('search_dataset', 'sd', 'hsi.sid = sd.sid AND sd.type = :type', [':type' => $this->getType()]);
$condition = new Condition('OR');
$condition = $this->database->condition('OR');
$condition->condition('sd.reindex', 0, '<>')
->isNull('sd.sid');
$query->condition($condition);
......
......@@ -3,7 +3,6 @@
namespace Drupal\locale;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
/**
* Defines a class to store localized strings in the database.
......@@ -417,7 +416,7 @@ protected function dbStringSelect(array $conditions, array $options = []) {
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((new Condition('OR'))
$query->condition(($this->connection->condition('OR'))
->condition($field_alias, (array) $value, 'IN')
->isNull($field_alias)
);
......@@ -430,7 +429,7 @@ protected function dbStringSelect(array $conditions, array $options = []) {
// Process other options, string filter, query limit, etc.
if (!empty($options['filters'])) {
if (count($options['filters']) > 1) {
$filter = new Condition('OR');
$filter = $this->connection->condition('OR');
$query->condition($filter);
}
else {
......
......@@ -5,7 +5,6 @@
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
......@@ -98,7 +97,7 @@ public function access(NodeInterface $node, $operation, AccountInterface $accoun
$query->condition($nids);
$query->range(0, 1);
$grants = static::buildGrantsQueryCondition(node_access_grants($operation, $account));
$grants = $this->buildGrantsQueryCondition(node_access_grants($operation, $account));
if (count($grants) > 0) {
$query->condition($grants);
......@@ -136,7 +135,7 @@ public function checkAll(AccountInterface $account) {
->condition('nid', 0)
->condition('grant_view', 1, '>=');
$grants = static::buildGrantsQueryCondition(node_access_grants('view', $account));
$grants = $this->buildGrantsQueryCondition(node_access_grants('view', $account));
if (count($grants) > 0) {
$query->condition($grants);
......@@ -158,7 +157,7 @@ public function alterQuery($query, array $tables, $op, AccountInterface $account
$grants = node_access_grants($op, $account);
// If any grant exists for the specified user, then user has access to the
// node for the specified operation.
$grant_conditions = static::buildGrantsQueryCondition($grants);
$grant_conditions = $this->buildGrantsQueryCondition($grants);
$grants_exist = count($grant_conditions->conditions()) > 0;
$is_multilingual = \Drupal::languageManager()->isMultilingual();
......@@ -294,11 +293,11 @@ public function deleteNodeRecords(array $nids) {
*
* @see node_access_grants()
*/
protected static function buildGrantsQueryCondition(array $node_access_grants) {
$grants = new Condition("OR");
protected function buildGrantsQueryCondition(array $node_access_grants) {
$grants = $this->database->condition('OR');
foreach ($node_access_grants as $realm => $gids) {
if (!empty($gids)) {
$and = new Condition('AND');
$and = $this->database->condition('AND');
$grants->condition($and
->condition('gid', $gids, 'IN')
->condition('realm', $realm)
......
......@@ -7,7 +7,6 @@
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\Config;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\SelectExtender;
use Drupal\Core\Database\StatementInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
......@@ -295,7 +294,7 @@ protected function findResults() {
$info = $this->advanced[$option];
// Insert additional conditions. By default, all use the OR operator.
$operator = empty($info['operator']) ? 'OR' : $info['operator'];
$where = new Condition($operator);
$where = $this->databaseReplica->condition($operator);
foreach ($matched as $value) {
$where->condition($info['column'], $value);
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\node\Plugin\views\filter;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
......@@ -30,10 +29,10 @@ public function query() {
$account = $this->view->getUser();
if (!$account->hasPermission('bypass node access')) {
$table = $this->ensureMyTable();
$grants = new Condition('OR');
$grants = $this->query->getConnection()->condition('OR');
foreach (node_access_grants('view', $account) as $realm => $gids) {
foreach ($gids as $gid) {
$grants->condition((new Condition('AND'))
$grants->condition(($this->query->getConnection()->condition('AND'))
->condition($table . '.gid', $gid)
->condition($table . '.realm', $realm)
);
......
......@@ -3,7 +3,6 @@
namespace Drupal\path_alias;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Language\LanguageInterface;
......@@ -37,7 +36,7 @@ public function preloadPathAlias($preloaded, $langcode) {
->fields('base_table', ['path', 'alias']);
if (!empty($preloaded)) {
$conditions = new Condition('OR');
$conditions = $this->connection->condition('OR');
foreach ($preloaded as $preloaded_item) {
$conditions->condition('base_table.path', $this->connection->escapeLike($preloaded_item), 'LIKE');
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\search\Plugin\views\argument;
use Drupal\Core\Database\Query\Condition;
use Drupal\search\ViewsSearchQuery;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
......@@ -78,7 +77,7 @@ public function query($group_by = FALSE) {
else {
$search_index = $this->ensureMyTable();
$search_condition = new Condition('AND');
$search_condition = $this->view->query->getConnection()->condition('AND');
// Create a new join to relate the 'search_total' table to our current 'search_index' table.
$definition = [
......@@ -111,7 +110,7 @@ public function query($group_by = FALSE) {
// 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 = new Condition('OR');
$or = $this->view->query->getConnection()->condition('OR');
foreach ($words as $word) {
$or->condition("$search_index.word", $word);
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\search\Plugin\views\filter;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search\ViewsSearchQuery;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
......@@ -154,7 +153,7 @@ public function query() {
else {
$search_index = $this->ensureMyTable();
$search_condition = new Condition('AND');
$search_condition = $this->view->query->getConnection()->condition('AND');
// Create a new join to relate the 'search_total' table to our current
// 'search_index' table.
......@@ -188,7 +187,7 @@ public function query() {
// 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 = new Condition('OR');
$or = $this->view->query->getConnection()->condition('OR');
foreach ($words as $word) {
$or->condition("$search_index.word", $word);
}
......
......@@ -5,7 +5,6 @@
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\search\Exception\SearchIndexException;
/**
......@@ -313,7 +312,7 @@ public function updateWordWeights(array $words) {
// search_total. We use a LEFT JOIN between the two tables and keep only
// the rows which fail to join.
$result = $this->replica->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");
$or = new Condition('OR');
$or = $this->replica->condition('OR');
foreach ($result as $word) {
$or->condition('word', $word->realword);
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\search;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\SelectExtender;
use Drupal\Core\Database\Query\SelectInterface;
......@@ -205,7 +204,7 @@ public function searchExpression($expression, $type) {
$this->addTag('search_' . $type);
// Initialize conditions and status.
$this->conditions = new Condition('AND');
$this->conditions = $this->connection->condition('AND');
$this->status = 0;
return $this;
......@@ -315,7 +314,7 @@ protected function parseSearchExpression() {
}
$has_or = TRUE;
$has_new_scores = FALSE;
$queryor = new Condition('OR');
$queryor = $this->connection->condition('OR');
foreach ($key as $or) {
list($num_new_scores) = $this->parseWord($or);
$has_new_scores |= $num_new_scores;
......@@ -403,7 +402,7 @@ public function prepareAndNormalize() {
}
// Build the basic search query: match the entered keywords.
$or = new Condition('OR');
$or = $this->connection->condition('OR');
foreach ($this->words as $word) {
$or->condition('i.word', $word);
}
......
......@@ -2,7 +2,7 @@
namespace Drupal\search;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\ConditionInterface;
/**
* Extends the core SearchQuery to be able to gets its protected values.
......@@ -70,7 +70,7 @@ public function publicParseSearchExpression() {
* which must have a 'field' element.
*/
public function conditionReplaceString($search, $replace, &$condition) {
if ($condition['field'] instanceof Condition) {
if ($condition['field'] instanceof ConditionInterface) {
$conditions =& $condition['field']->conditions();
foreach ($conditions as $key => &$subcondition) {
if (is_numeric($key)) {
......
......@@ -3,7 +3,6 @@
namespace Drupal\taxonomy\Plugin\views\argument;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
......@@ -113,7 +112,7 @@ public function query($group_by = FALSE) {
// Now build the subqueries.
$subquery = Database::getConnection()->select('taxonomy_index', 'tn');
$subquery->addField('tn', 'nid');
$where = (new Condition('OR'))->condition('tn.tid', $tids, $operator);
$where = ($this->view->query->getConnection()->condition('OR'))->condition('tn.tid', $tids, $operator);
$last = "tn";
if ($this->options['depth'] > 0) {
......
......@@ -3,7 +3,6 @@
namespace Drupal\taxonomy\Plugin\views\filter;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
/**
......@@ -74,7 +73,7 @@ public function query() {
// Now build the subqueries.
$subquery = Database::getConnection()->select('taxonomy_index', 'tn');
$subquery->addField('tn', 'nid');
$where = (new Condition('OR'))->condition('tn.tid', $this->value, $operator);
$where = ($this->view->query->getConnection()->condition('OR'))->condition('tn.tid', $this->value, $operator);
$last = "tn";
if ($this->options['depth'] > 0) {
......
......@@ -3,7 +3,6 @@
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\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
......@@ -235,17 +234,17 @@ public function entityQueryAlter(SelectInterface $query) {
// 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 = new Condition('OR');
$or = $this->connection->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 = new Condition('AND');
$value_part = $this->connection->condition('AND');
$value_part->condition('anonymous_name', $condition['value'], $condition['operator']);
$value_part->compile($this->connection, $query);
$or->condition((new Condition('AND'))
$or->condition(($this->connection->condition('AND'))
->where(str_replace($query->escapeField('anonymous_name'), ':anonymous_name', (string) $value_part), $value_part->arguments() + [':anonymous_name' => \Drupal::config('user.settings')->get('anonymous')])
->condition('base_table.uid', 0)
);
......
......@@ -2,7 +2,6 @@
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;
......@@ -29,7 +28,7 @@ public function query() {
$this->ensureMyTable();
$field = $this->tableAlias . '.' . $this->realField . ' ';
$or = new Condition('OR');
$or = $this->view->query->getConnection()->condition('OR');
if (empty($this->value)) {
$or->condition($field, '***CURRENT_USER***', '<>');
......
......@@ -2,7 +2,6 @@
namespace Drupal\views;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\HandlerBase;
......@@ -327,7 +326,7 @@ public function addFilter() {
if ($add_condition) {
$field = $this->handler->realField;
$clause = $operator == 'or' ? new Condition('OR') : new Condition('AND');
$clause = $operator == 'or' ? $this->handler->query->getConnection()->condition('OR') : $this->handler->query->getConnection()->condition('AND');
foreach ($this->handler->tableAliases as $value => $alias) {
$clause->condition("$alias.$field", $value);
}
......
......@@ -3,7 +3,6 @@
namespace Drupal\views\Plugin\views\display;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -170,7 +169,7 @@ public function query() {
}
// Multiple search fields are ORed together.
$conditions = new Condition('OR');
$conditions = $this->view->query->getConnection()->condition('OR');
// Build the condition using the selected search fields.
foreach ($style_options['options']['search_fields'] as $field_id) {
......
......@@ -2,7 +2,6 @@
namespace Drupal\views\Plugin\views\filter;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
......@@ -234,12 +233,12 @@ protected function queryOpBoolean($field, $query_operator = self::EQUAL) {
if (empty($this->value)) {
if ($this->accept_null) {
if ($query_operator === self::EQUAL) {
$condition = (new Condition('OR'))
$condition = ($this->query->getConnection()->condition('OR'))
->condition($field, 0, $query_operator)
->isNull($field);
}
else {
$condition = (new Condition('AND'))
$condition = ($this->query->getConnection()->condition('AND'))
->condition($field, 0, $query_operator)
->isNotNull($field);
}
......
......@@ -3,7 +3,6 @@
namespace Drupal\views\Plugin\views\filter;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -330,7 +329,7 @@ protected function opContains($field) {
}
protected function opContainsWord($field) {
$where = $this->operator == 'word' ? new Condition(