commit f9a435eab81787753a27908cfbf5dd68bb2401f8 Author: Karoly Negyesi Date: Tue Jul 28 18:10:44 2015 +0200 Introduce queryconditiontrait diff --git a/core/lib/Drupal/Core/Database/Query/Delete.php b/core/lib/Drupal/Core/Database/Query/Delete.php index 08e7af1..1f59dd9 100644 --- a/core/lib/Drupal/Core/Database/Query/Delete.php +++ b/core/lib/Drupal/Core/Database/Query/Delete.php @@ -17,6 +17,8 @@ */ class Delete extends Query implements ConditionInterface { + use QueryConditionTrait; + /** * The table from which to delete. * @@ -25,15 +27,6 @@ class Delete extends Query implements ConditionInterface { protected $table; /** - * The condition object for this query. - * - * Condition handling is handled via composition. - * - * @var Condition - */ - protected $condition; - - /** * Constructs a Delete object. * * @param \Drupal\Core\Database\Connection $connection @@ -52,82 +45,6 @@ public function __construct(Connection $connection, $table, array $options = arr } /** - * Implements Drupal\Core\Database\Query\ConditionInterface::condition(). - */ - public function condition($field, $value = NULL, $operator = '=') { - $this->condition->condition($field, $value, $operator); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::isNull(). - */ - public function isNull($field) { - $this->condition->isNull($field); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::isNotNull(). - */ - public function isNotNull($field) { - $this->condition->isNotNull($field); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::exists(). - */ - public function exists(SelectInterface $select) { - $this->condition->exists($select); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::notExists(). - */ - public function notExists(SelectInterface $select) { - $this->condition->notExists($select); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::conditions(). - */ - public function &conditions() { - return $this->condition->conditions(); - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::arguments(). - */ - public function arguments() { - return $this->condition->arguments(); - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::where(). - */ - public function where($snippet, $args = array()) { - $this->condition->where($snippet, $args); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::compile(). - */ - public function compile(Connection $connection, PlaceholderInterface $queryPlaceholder) { - return $this->condition->compile($connection, $queryPlaceholder); - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::compiled(). - */ - public function compiled() { - return $this->condition->compiled(); - } - - /** * Executes the DELETE query. * * @return diff --git a/core/lib/Drupal/Core/Database/Query/Merge.php b/core/lib/Drupal/Core/Database/Query/Merge.php index 34f8378..c292894 100644 --- a/core/lib/Drupal/Core/Database/Query/Merge.php +++ b/core/lib/Drupal/Core/Database/Query/Merge.php @@ -49,6 +49,9 @@ * fields() and updateFields(). */ class Merge extends Query implements ConditionInterface { + + use QueryConditionTrait; + /** * Returned by execute() if an INSERT query has been executed. */ @@ -340,82 +343,6 @@ public function key($field, $value = NULL) { } /** - * Implements Drupal\Core\Database\Query\ConditionInterface::condition(). - */ - public function condition($field, $value = NULL, $operator = '=') { - $this->condition->condition($field, $value, $operator); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::isNull(). - */ - public function isNull($field) { - $this->condition->isNull($field); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::isNotNull(). - */ - public function isNotNull($field) { - $this->condition->isNotNull($field); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::exists(). - */ - public function exists(SelectInterface $select) { - $this->condition->exists($select); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::notExists(). - */ - public function notExists(SelectInterface $select) { - $this->condition->notExists($select); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::conditions(). - */ - public function &conditions() { - return $this->condition->conditions(); - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::arguments(). - */ - public function arguments() { - return $this->condition->arguments(); - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::where(). - */ - public function where($snippet, $args = array()) { - $this->condition->where($snippet, $args); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::compile(). - */ - public function compile(Connection $connection, PlaceholderInterface $queryPlaceholder) { - return $this->condition->compile($connection, $queryPlaceholder); - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::compiled(). - */ - public function compiled() { - return $this->condition->compiled(); - } - - /** * Implements PHP magic __toString method to convert the query to a string. * * In the degenerate case, there is no string-able query as this operation diff --git a/core/lib/Drupal/Core/Database/Query/Query.php b/core/lib/Drupal/Core/Database/Query/Query.php index 790ed5c..8dba91b 100644 --- a/core/lib/Drupal/Core/Database/Query/Query.php +++ b/core/lib/Drupal/Core/Database/Query/Query.php @@ -180,25 +180,4 @@ public function &getComments() { return $this->comments; } - /** - * {@inheritdoc} - */ - public function conditionGroupFactory($conjunction = 'AND') { - return new Condition($conjunction); - } - - /** - * {@inheritdoc} - */ - public function andConditionGroup() { - return $this->conditionGroupFactory('AND'); - } - - /** - * {@inheritdoc} - */ - public function orConditionGroup() { - return $this->conditionGroupFactory('OR'); - } - } diff --git a/core/lib/Drupal/Core/Database/Query/QueryConditionTrait.php b/core/lib/Drupal/Core/Database/Query/QueryConditionTrait.php new file mode 100644 index 0000000..f3d69e1 --- /dev/null +++ b/core/lib/Drupal/Core/Database/Query/QueryConditionTrait.php @@ -0,0 +1,119 @@ +condition->condition($field, $value, $operator); + return $this; + } + + /** + * Implements Drupal\Core\Database\Query\ConditionInterface::isNull(). + */ + public function isNull($field) { + $this->condition->isNull($field); + return $this; + } + + /** + * Implements Drupal\Core\Database\Query\ConditionInterface::isNotNull(). + */ + public function isNotNull($field) { + $this->condition->isNotNull($field); + return $this; + } + + /** + * Implements Drupal\Core\Database\Query\ConditionInterface::exists(). + */ + public function exists(SelectInterface $select) { + $this->condition->exists($select); + return $this; + } + + /** + * Implements Drupal\Core\Database\Query\ConditionInterface::notExists(). + */ + public function notExists(SelectInterface $select) { + $this->condition->notExists($select); + return $this; + } + + /** + * Implements Drupal\Core\Database\Query\ConditionInterface::conditions(). + */ + public function &conditions() { + return $this->condition->conditions(); + } + + /** + * Implements Drupal\Core\Database\Query\ConditionInterface::arguments(). + */ + public function arguments() { + return $this->condition->arguments(); + } + + /** + * Implements Drupal\Core\Database\Query\ConditionInterface::where(). + */ + public function where($snippet, $args = array()) { + $this->condition->where($snippet, $args); + return $this; + } + + /** + * Implements Drupal\Core\Database\Query\ConditionInterface::compile(). + */ + public function compile(Connection $connection, PlaceholderInterface $queryPlaceholder) { + $this->condition->compile($connection, $queryPlaceholder); + } + + /** + * Implements Drupal\Core\Database\Query\ConditionInterface::compiled(). + */ + public function compiled() { + return $this->condition->compiled(); + } + + /** + * {@inheritdoc} + */ + public function conditionGroupFactory($conjunction = 'AND') { + return new Condition($conjunction); + } + + /** + * {@inheritdoc} + */ + public function andConditionGroup() { + return $this->conditionGroupFactory('AND'); + } + + /** + * {@inheritdoc} + */ + public function orConditionGroup() { + return $this->conditionGroupFactory('OR'); + } + +} diff --git a/core/lib/Drupal/Core/Database/Query/Select.php b/core/lib/Drupal/Core/Database/Query/Select.php index d925a07..0fe5500 100644 --- a/core/lib/Drupal/Core/Database/Query/Select.php +++ b/core/lib/Drupal/Core/Database/Query/Select.php @@ -18,6 +18,8 @@ */ class Select extends Query implements SelectInterface { + use QueryConditionTrait; + /** * The fields to SELECT. * @@ -72,13 +74,6 @@ class Select extends Query implements SelectInterface { protected $group = array(); /** - * The conditional object for the WHERE clause. - * - * @var \Drupal\Core\Database\Query\Condition - */ - protected $where; - - /** * The conditional object for the HAVING clause. * * @var \Drupal\Core\Database\Query\Condition @@ -139,7 +134,7 @@ public function __construct($table, $alias = NULL, Connection $connection, $opti $options['return'] = Database::RETURN_STATEMENT; parent::__construct($connection, $options); $conjunction = isset($options['conjunction']) ? $options['conjunction'] : 'AND'; - $this->where = new Condition($conjunction); + $this->condition = new Condition($conjunction); $this->having = new Condition($conjunction); $this->addJoin(NULL, $table, $alias); } @@ -191,27 +186,12 @@ public function getMetaData($key) { /** * {@inheritdoc} */ - public function condition($field, $value = NULL, $operator = '=') { - $this->where->condition($field, $value, $operator); - return $this; - } - - /** - * {@inheritdoc} - */ - public function &conditions() { - return $this->where->conditions(); - } - - /** - * {@inheritdoc} - */ public function arguments() { if (!$this->compiled()) { return NULL; } - $args = $this->where->arguments() + $this->having->arguments(); + $args = $this->condition->arguments() + $this->having->arguments(); foreach ($this->tables as $table) { if ($table['arguments']) { @@ -241,48 +221,8 @@ public function arguments() { /** * {@inheritdoc} */ - public function where($snippet, $args = array()) { - $this->where->where($snippet, $args); - return $this; - } - - /** - * {@inheritdoc} - */ - public function isNull($field) { - $this->where->isNull($field); - return $this; - } - - /** - * {@inheritdoc} - */ - public function isNotNull($field) { - $this->where->isNotNull($field); - return $this; - } - - /** - * {@inheritdoc} - */ - public function exists(SelectInterface $select) { - $this->where->exists($select); - return $this; - } - - /** - * {@inheritdoc} - */ - public function notExists(SelectInterface $select) { - $this->where->notExists($select); - return $this; - } - - /** - * {@inheritdoc} - */ public function compile(Connection $connection, PlaceholderInterface $queryPlaceholder) { - $this->where->compile($connection, $queryPlaceholder); + $this->condition->compile($connection, $queryPlaceholder); $this->having->compile($connection, $queryPlaceholder); foreach ($this->tables as $table) { @@ -302,7 +242,7 @@ public function compile(Connection $connection, PlaceholderInterface $queryPlace * {@inheritdoc} */ public function compiled() { - if (!$this->where->compiled() || !$this->having->compiled()) { + if (!$this->condition->compiled() || !$this->having->compiled()) { return FALSE; } @@ -885,9 +825,9 @@ public function __toString() { } // WHERE - if (count($this->where)) { + if (count($this->condition)) { // There is an implicit string cast on $this->condition. - $query .= "\nWHERE " . $this->where; + $query .= "\nWHERE " . $this->condition; } // GROUP BY @@ -943,7 +883,7 @@ public function __clone() { // want to clone the database connection object as that would duplicate the // connection itself. - $this->where = clone($this->where); + $this->condition = clone($this->condition); $this->having = clone($this->having); foreach ($this->union as $key => $aggregate) { $this->union[$key]['query'] = clone($aggregate['query']); diff --git a/core/lib/Drupal/Core/Database/Query/Update.php b/core/lib/Drupal/Core/Database/Query/Update.php index 5c652e7..3f7fc39 100644 --- a/core/lib/Drupal/Core/Database/Query/Update.php +++ b/core/lib/Drupal/Core/Database/Query/Update.php @@ -17,6 +17,8 @@ */ class Update extends Query implements ConditionInterface { + use QueryConditionTrait; + /** * The table to update. * @@ -39,15 +41,6 @@ class Update extends Query implements ConditionInterface { protected $arguments = array(); /** - * The condition object for this query. - * - * Condition handling is handled via composition. - * - * @var \Drupal\Core\Database\Query\Condition - */ - protected $condition; - - /** * Array of fields to update to an expression in case of a duplicate record. * * This variable is a nested array in the following format: @@ -81,82 +74,6 @@ public function __construct(Connection $connection, $table, array $options = arr } /** - * Implements Drupal\Core\Database\Query\ConditionInterface::condition(). - */ - public function condition($field, $value = NULL, $operator = '=') { - $this->condition->condition($field, $value, $operator); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::isNull(). - */ - public function isNull($field) { - $this->condition->isNull($field); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::isNotNull(). - */ - public function isNotNull($field) { - $this->condition->isNotNull($field); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::exists(). - */ - public function exists(SelectInterface $select) { - $this->condition->exists($select); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::notExists(). - */ - public function notExists(SelectInterface $select) { - $this->condition->notExists($select); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::conditions(). - */ - public function &conditions() { - return $this->condition->conditions(); - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::arguments(). - */ - public function arguments() { - return $this->condition->arguments(); - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::where(). - */ - public function where($snippet, $args = array()) { - $this->condition->where($snippet, $args); - return $this; - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::compile(). - */ - public function compile(Connection $connection, PlaceholderInterface $queryPlaceholder) { - return $this->condition->compile($connection, $queryPlaceholder); - } - - /** - * Implements Drupal\Core\Database\Query\ConditionInterface::compiled(). - */ - public function compiled() { - return $this->condition->compiled(); - } - - /** * Adds a set of field->value pairs to be updated. * * @param $fields