Skip navigation
Help

SelectQuery_pgsql

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

Hierarchy

Properties

NameDescription
Query::$commentsAn array of comments that can be prepended to a query.
Query::$connectionThe connection object on which to run this query.
Query::$connectionKeyThe key of the connection object.
Query::$connectionTargetThe target of the connection object.
Query::$nextPlaceholderThe placeholder counter.
Query::$queryOptionsThe query options to pass on to the connection object.
SelectQuery::$distinctWhether or not this query should be DISTINCT
SelectQuery::$expressionsThe expressions to SELECT as virtual fields.
SelectQuery::$fieldsThe fields to SELECT.
SelectQuery::$forUpdateThe FOR UPDATE status
SelectQuery::$groupThe fields by which to group.
SelectQuery::$havingThe conditional object for the HAVING clause.
SelectQuery::$orderThe fields by which to order this query.
SelectQuery::$preparedIndicates if preExecute() has already been called.
SelectQuery::$rangeThe range limiters for this query.
SelectQuery::$tablesThe tables against which to JOIN.
SelectQuery::$unionAn array whose elements specify a query to UNION, and the UNION type. The 'type' key may be '', 'ALL', or 'DISTINCT' to represent a 'UNION', 'UNION ALL', or 'UNION DISTINCT'…
SelectQuery::$whereThe conditional object for the WHERE clause.

Functions & methods

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

File

drupal/includes/database/pgsql/select.inc, line 13
Select builder for PostgreSQL database engine.

View source
class SelectQuery_pgsql extends SelectQuery {

  public function orderRandom() {
    $alias = $this->addExpression('RANDOM()', 'random_field');
    $this->orderBy($alias);
    return $this;
  }

  /**
   * Overrides SelectQuery::orderBy().
   *
   * PostgreSQL adheres strictly to the SQL-92 standard and requires that when
   * using DISTINCT or GROUP BY conditions, fields and expressions that are
   * ordered on also need to be selected. This is a best effort implementation
   * to handle the cases that can be automated by adding the field if it is not
   * yet selected.
   *
   * @code
   *   $query = db_select('node', 'n');
   *   $query->join('node_revision', 'nr', 'n.vid = nr.vid');
   *   $query
   *     ->distinct()
   *     ->fields('n')
   *     ->orderBy('timestamp');
   * @endcode
   *
   * In this query, it is not possible (without relying on the schema) to know
   * whether timestamp belongs to node_revisions and needs to be added or
   * belongs to node and is already selected. Queries like this will need to be
   * corrected in the original query by adding an explicit call to
   * SelectQuery::addField() or SelectQuery::fields().
   *
   * Since this has a small performance impact, both by the additional
   * processing in this function and in the database that needs to return the
   * additional fields, this is done as an override instead of implementing it
   * directly in SelectQuery::orderBy().
   */
  public function orderBy($field, $direction = 'ASC') {
    // Call parent function to order on this.
    $return = parent::orderBy($field, $direction);

    // If there is a table alias specified, split it up.
    if (strpos($field, '.') !== FALSE) {
      list($table, $table_field) = explode('.', $field);
    }
    // Figure out if the field has already been added.
    foreach ($this->fields as $existing_field) {
      if (!empty($table)) {
        // If table alias is given, check if field and table exists.
        if ($existing_field['table'] == $table && $existing_field['field'] == $table_field) {
          return $return;
        }
      }
      else {
        // If there is no table, simply check if the field exists as a field or
        // an aliased field.
        if ($existing_field['alias'] == $field) {
          return $return;
        }
      }
    }

    // Also check expression aliases.
    foreach ($this->expressions as $expression) {
      if ($expression['alias'] == $field) {
        return $return;
      }
    }

    // If a table loads all fields, it can not be added again. It would
    // result in an ambigious alias error because that field would be loaded
    // twice: Once through table_alias.* and once directly. If the field
    // actually belongs to a different table, it must be added manually.
    foreach ($this->tables as $table) {
      if (!empty($table['all_fields'])) {
        return $return;
      }
    }

    // If $field contains an characters which are not allowed in a field name
    // it is considered an expression, these can't be handeld automatically
    // either.
    if ($this->connection->escapeField($field) != $field) {
      return $return;
    }

    // This is a case that can be handled automatically, add the field.
    $this->addField(NULL, $field);
    return $return;
  }
}

Related topics