Skip navigation
Help

SelectQueryExtender

  1. drupal
    1. 7 drupal/includes/database/select.inc

The base extender class for Select queries.

Hierarchy

Properties

NameDescription
SelectQueryExtender::$connectionThe connection object on which to run this query.
SelectQueryExtender::$placeholderThe placeholder counter.
SelectQueryExtender::$queryThe SelectQuery object we are extending/decorating.

Functions & methods

NameDescription
SelectQueryExtender::addExpressionAdds an expression to the list of "fields" to be SELECTed. Overrides SelectQueryInterface::addExpression
SelectQueryExtender::addFieldAdds a field to the list to be SELECTed. Overrides SelectQueryInterface::addField
SelectQueryExtender::addJoinJoin against another table in the database. Overrides SelectQueryInterface::addJoin
SelectQueryExtender::addMetaData
SelectQueryExtender::addTag
SelectQueryExtender::arguments
SelectQueryExtender::compile
SelectQueryExtender::condition
SelectQueryExtender::conditions
SelectQueryExtender::countQueryGet the equivalent COUNT query of this query as a new query object. Overrides SelectQueryInterface::countQuery
SelectQueryExtender::distinctSets this query to be DISTINCT. Overrides SelectQueryInterface::distinct
SelectQueryExtender::execute
SelectQueryExtender::exists
SelectQueryExtender::extend
SelectQueryExtender::fieldsAdd multiple fields from the same table to be SELECTed. Overrides SelectQueryInterface::fields
SelectQueryExtender::forUpdateAdd FOR UPDATE to the query. Overrides SelectQueryInterface::forUpdate
SelectQueryExtender::getArgumentsCompiles and returns an associative array of the arguments for this prepared statement. Overrides SelectQueryInterface::getArguments
SelectQueryExtender::getExpressionsReturns a reference to the expressions array for this query. Overrides SelectQueryInterface::getExpressions
SelectQueryExtender::getFieldsReturns a reference to the fields array for this query. Overrides SelectQueryInterface::getFields
SelectQueryExtender::getGroupByReturns a reference to the group-by array for this query. Overrides SelectQueryInterface::getGroupBy
SelectQueryExtender::getMetaData
SelectQueryExtender::getOrderByReturns a reference to the order by array for this query. Overrides SelectQueryInterface::getOrderBy
SelectQueryExtender::getTablesReturns a reference to the tables array for this query. Overrides SelectQueryInterface::getTables
SelectQueryExtender::getUnionReturns a reference to the union queries for this query. This include queries for UNION, UNION ALL, and UNION DISTINCT. Overrides SelectQueryInterface::getUnion
SelectQueryExtender::groupByGroups the result set by the specified field. Overrides SelectQueryInterface::groupBy
SelectQueryExtender::hasAllTags
SelectQueryExtender::hasAnyTag
SelectQueryExtender::hasTag
SelectQueryExtender::having
SelectQueryExtender::havingArguments
SelectQueryExtender::havingCompile
SelectQueryExtender::havingConditionHelper function to build most common HAVING conditional clauses. Overrides SelectQueryInterface::havingCondition
SelectQueryExtender::havingConditions
SelectQueryExtender::innerJoinInner Join against another table in the database. Overrides SelectQueryInterface::innerJoin
SelectQueryExtender::isNotNull
SelectQueryExtender::isNull
SelectQueryExtender::isPreparedIndicates if preExecute() has already been called on that object. Overrides SelectQueryInterface::isPrepared
SelectQueryExtender::joinDefault Join against another table in the database. Overrides SelectQueryInterface::join
SelectQueryExtender::leftJoinLeft Outer Join against another table in the database. Overrides SelectQueryInterface::leftJoin
SelectQueryExtender::nextPlaceholder
SelectQueryExtender::notExists
SelectQueryExtender::orderByOrders the result set by a given field. Overrides SelectQueryInterface::orderBy
SelectQueryExtender::orderRandomOrders the result set by a random value. Overrides SelectQueryInterface::orderRandom
SelectQueryExtender::preExecuteGeneric preparation and validation for a SELECT query. Overrides SelectQueryInterface::preExecute
SelectQueryExtender::rangeRestricts a query to a given range in the result set. Overrides SelectQueryInterface::range
SelectQueryExtender::rightJoinRight Outer Join against another table in the database. Overrides SelectQueryInterface::rightJoin
SelectQueryExtender::unionAdd another Select query to UNION to this one. Overrides SelectQueryInterface::union
SelectQueryExtender::where
SelectQueryExtender::__callMagic override for undefined methods.
SelectQueryExtender::__cloneClone magic method. Overrides SelectQueryInterface::__clone
SelectQueryExtender::__construct
SelectQueryExtender::__toString

File

drupal/includes/database/select.inc, line 535

View source
class SelectQueryExtender implements SelectQueryInterface {

  /**
   * The SelectQuery object we are extending/decorating.
   *
   * @var SelectQueryInterface
   */
  protected $query;

  /**
   * The connection object on which to run this query.
   *
   * @var DatabaseConnection
   */
  protected $connection;

  /**
   * The placeholder counter.
   */
  protected $placeholder = 0;

  public function __construct(SelectQueryInterface $query, DatabaseConnection $connection) {
    $this->query = $query;
    $this->connection = $connection;
  }

  /* Implementations of QueryPlaceholderInterface. */

  public function nextPlaceholder() {
    return $this->placeholder++;
  }

  /* Implementations of QueryAlterableInterface. */

  public function addTag($tag) {
    $this->query->addTag($tag);
    return $this;
  }

  public function hasTag($tag) {
    return $this->query->hasTag($tag);
  }

  public function hasAllTags() {
    return call_user_func_array(array($this->query, 'hasAllTags'), func_get_args());
  }

  public function hasAnyTag() {
    return call_user_func_array(array($this->query, 'hasAnyTags'), func_get_args());
  }

  public function addMetaData($key, $object) {
    $this->query->addMetaData($key, $object);
    return $this;
  }

  public function getMetaData($key) {
    return $this->query->getMetaData($key);
  }

  /* Implementations of QueryConditionInterface for the WHERE clause. */

  public function condition($field, $value = NULL, $operator = NULL) {
    $this->query->condition($field, $value, $operator);
    return $this;
  }

  public function &conditions() {
    return $this->query->conditions();
  }

  public function arguments() {
    return $this->query->arguments();
  }

  public function where($snippet, $args = array()) {
    $this->query->where($snippet, $args);
    return $this;
  }

  public function compile(DatabaseConnection $connection, QueryPlaceholderInterface $queryPlaceholder = NULL) {
    return $this->condition->compile($connection, isset($queryPlaceholder) ? $queryPlaceholder : $this);
  }

  /* Implementations of QueryConditionInterface for the HAVING clause. */

  public function havingCondition($field, $value = NULL, $operator = '=') {
    $this->query->condition($field, $value, $operator, $num_args);
    return $this;
  }

  public function &havingConditions() {
    return $this->having->conditions();
  }

  public function havingArguments() {
    return $this->having->arguments();
  }

  public function having($snippet, $args = array()) {
    $this->query->having($snippet, $args);
    return $this;
  }

  public function havingCompile(DatabaseConnection $connection) {
    return $this->query->havingCompile($connection);
  }

  /* Implementations of QueryExtendableInterface. */

  public function extend($extender_name) {
    // The extender can be anywhere so this needs to go to the registry, which
    // is surely loaded by now.
    $class = $this->connection->getDriverClass($extender_name, array(), TRUE);
    return new $class($this, $this->connection);
  }

  /* Alter accessors to expose the query data to alter hooks. */

  public function &getFields() {
    return $this->query->getFields();
  }

  public function &getExpressions() {
    return $this->query->getExpressions();
  }

  public function &getOrderBy() {
    return $this->query->getOrderBy();
  }

  public function &getGroupBy() {
    return $this->query->getGroupBy();
  }

  public function &getTables() {
    return $this->query->getTables();
  }

  public function &getUnion() {
    return $this->query->getUnion();
  }

  public function getArguments(QueryPlaceholderInterface $queryPlaceholder = NULL) {
    return $this->query->getArguments($queryPlaceholder);
  }

  public function isPrepared() {
    return $this->query->isPrepared();
  }

  public function preExecute(SelectQueryInterface $query = NULL) {
    // If no query object is passed in, use $this.
    if (!isset($query)) {
      $query = $this;
    }

    return $this->query->preExecute($query);
  }

  public function execute() {
    // By calling preExecute() here, we force it to preprocess the extender
    // object rather than just the base query object.  That means
    // hook_query_alter() gets access to the extended object.
    if (!$this->preExecute($this)) {
      return NULL;
    }

    return $this->query->execute();
  }

  public function distinct($distinct = TRUE) {
    $this->query->distinct($distinct);
    return $this;
  }

  public function addField($table_alias, $field, $alias = NULL) {
    return $this->query->addField($table_alias, $field, $alias);
  }

  public function fields($table_alias, array $fields = array()) {
    $this->query->fields($table_alias, $fields);
    return $this;
  }

  public function addExpression($expression, $alias = NULL, $arguments = array()) {
    return $this->query->addExpression($expression, $alias, $arguments);
  }

  public function join($table, $alias = NULL, $condition = NULL, $arguments = array()) {
    return $this->query->join($table, $alias, $condition, $arguments);
  }

  public function innerJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
    return $this->query->innerJoin($table, $alias, $condition, $arguments);
  }

  public function leftJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
    return $this->query->leftJoin($table, $alias, $condition, $arguments);
  }

  public function rightJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
    return $this->query->rightJoin($table, $alias, $condition, $arguments);
  }

  public function addJoin($type, $table, $alias = NULL, $condition = NULL, $arguments = array()) {
    return $this->query->addJoin($type, $table, $alias, $condition, $arguments);
  }

  public function orderBy($field, $direction = 'ASC') {
    $this->query->orderBy($field, $direction);
    return $this;
  }

  public function orderRandom() {
    $this->query->orderRandom();
    return $this;
  }

  public function range($start = NULL, $length = NULL) {
    $this->query->range($start, $length);
    return $this;
  }

  public function union(SelectQueryInterface $query, $type = '') {
    $this->query->union($query, $type);
    return $this;
  }

  public function groupBy($field) {
    $this->query->groupBy($field);
    return $this;
  }

  public function forUpdate($set = TRUE) {
    $this->query->forUpdate($set);
    return $this;
  }

  public function countQuery() {
    // Create our new query object that we will mutate into a count query.
    $count = clone($this);

    // Zero-out existing fields and expressions.
    $fields = &$count->getFields();
    $fields = array();
    $expressions = &$count->getExpressions();
    $expressions = array();

    // Also remove 'all_fields' statements, which are expanded into tablename.*
    // when the query is executed.
    $tables = &$count->getTables();
    foreach ($tables as $alias => &$table) {
      unset($table['all_fields']);
    }

    // Ordering a count query is a waste of cycles, and breaks on some
    // databases anyway.
    $orders = &$count->getOrderBy();
    $orders = array();

    // COUNT() is an expression, so we add that back in.
    $count->addExpression('COUNT(*)');

    return $count;
  }

  function isNull($field) {
    $this->query->isNull($field);
    return $this;
  }

  function isNotNull($field) {
    $this->query->isNotNull($field);
    return $this;
  }

  public function exists(SelectQueryInterface $select) {
    $this->query->exists($select);
    return $this;
  }

  public function notExists(SelectQueryInterface $select) {
    $this->query->notExists($select);
    return $this;
  }

  public function __toString() {
    return (string) $this->query;
  }

  public function __clone() {
    // We need to deep-clone the query we're wrapping, which in turn may
    // deep-clone other objects.  Exciting!
    $this->query = clone($this->query);
  }

  /**
   * Magic override for undefined methods.
   *
   * If one extender extends another extender, then methods in the inner extender
   * will not be exposed on the outer extender.  That's because we cannot know
   * in advance what those methods will be, so we cannot provide wrapping
   * implementations as we do above.  Instead, we use this slower catch-all method
   * to handle any additional methods.
   */
  public function __call($method, $args) {
    $return = call_user_func_array(array($this->query, $method), $args);

    // Some methods will return the called object as part of a fluent interface.
    // Others will return some useful value.  If it's a value, then the caller
    // probably wants that value.  If it's the called object, then we instead
    // return this object.  That way we don't "lose" an extender layer when
    // chaining methods together.
    if ($return instanceof SelectQueryInterface) {
      return $this;
    }
    else {
      return $return;
    }
  }
}

Related topics