1. 8.2.x core/lib/Drupal/Core/Database/Driver/pgsql/Select.php Select
  2. 8.2.x core/lib/Drupal/Core/Database/Driver/sqlite/Select.php Select
  3. 8.2.x core/tests/Drupal/Tests/Core/Database/Stub/Select.php Select
  4. 8.2.x core/lib/Drupal/Core/Database/Driver/mysql/Select.php Select
  5. 8.2.x core/lib/Drupal/Core/Render/Element/Select.php Select
  6. 8.2.x core/lib/Drupal/Core/Database/Query/Select.php Select
  7. 8.0.x core/lib/Drupal/Core/Database/Query/Select.php Select
  8. 8.0.x core/lib/Drupal/Core/Database/Driver/mysql/Select.php Select
  9. 8.0.x core/lib/Drupal/Core/Database/Driver/pgsql/Select.php Select
  10. 8.0.x core/lib/Drupal/Core/Database/Driver/sqlite/Select.php Select
  11. 8.0.x core/lib/Drupal/Core/Render/Element/Select.php Select
  12. 8.1.x core/lib/Drupal/Core/Database/Driver/sqlite/Select.php Select
  13. 8.1.x core/tests/Drupal/Tests/Core/Database/Stub/Select.php Select
  14. 8.1.x core/lib/Drupal/Core/Database/Driver/mysql/Select.php Select
  15. 8.1.x core/lib/Drupal/Core/Render/Element/Select.php Select
  16. 8.1.x core/lib/Drupal/Core/Database/Driver/pgsql/Select.php Select
  17. 8.1.x core/lib/Drupal/Core/Database/Query/Select.php Select
  18. 8.3.x core/lib/Drupal/Core/Database/Driver/sqlite/Select.php Select
  19. 8.3.x core/tests/Drupal/Tests/Core/Database/Stub/Select.php Select
  20. 8.3.x core/lib/Drupal/Core/Database/Driver/pgsql/Select.php Select
  21. 8.3.x core/lib/Drupal/Core/Render/Element/Select.php Select
  22. 8.3.x core/lib/Drupal/Core/Database/Query/Select.php Select
  23. 8.3.x core/lib/Drupal/Core/Database/Driver/mysql/Select.php Select

PostgreSQL implementation of \Drupal\Core\Database\Query\Select.


Expanded class hierarchy of Select

Related topics

5 string references to 'Select'
Connection::select in core/lib/Drupal/Core/Database/Connection.php
Prepares and returns a SELECT query object.
ConnectionTest::providerGetDriverClass in core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
Dataprovider for testGetDriverClass().
FilterPluginBase::buildExposedFiltersGroupForm in core/modules/views/src/Plugin/views/filter/FilterPluginBase.php
Build the form to let users create the group of exposed filters. This form is displayed when users click on button 'Build group'
FormTestValidateRequiredForm::buildForm in core/modules/system/tests/modules/form_test/src/Form/FormTestValidateRequiredForm.php
form_test.routing.yml in core/modules/system/tests/modules/form_test/form_test.routing.yml


core/lib/Drupal/Core/Database/Driver/pgsql/Select.php, line 15


View source
class Select extends QuerySelect {

  public function orderRandom() {
    $alias = $this->addExpression('RANDOM()', 'random_field');
    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('example', 'e');
   *   $query->join('example_revision', 'er', 'e.vid = er.vid');
   *   $query
   *     ->distinct()
   *     ->fields('e')
   *     ->orderBy('timestamp');
   * @endcode
   * In this query, it is not possible (without relying on the schema) to know
   * whether timestamp belongs to example_revision 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') {
    // Only allow ASC and DESC, default to ASC.
    // Emulate MySQL default behavior to sort NULL values first for ascending,
    // and last for descending.
    // @see http://www.postgresql.org/docs/9.3/static/queries-order.html
    $direction = strtoupper($direction) == 'DESC' ? 'DESC NULLS LAST' : 'ASC NULLS FIRST';
    $this->order[$field] = $direction;

    if ($this->hasTag('entity_query')) {
      return $this;

    // 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 $this;
      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 $this;

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

    // If a table loads all fields, it can not be added again. It would
    // result in an ambiguous 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 $this;

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

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

   * {@inheritdoc}
  public function addExpression($expression, $alias = NULL, $arguments = []) {
    if (empty($alias)) {
      $alias = 'expression';

    // This implements counting in the same manner as the parent method.
    $alias_candidate = $alias;
    $count = 2;
    while (!empty($this->expressions[$alias_candidate])) {
      $alias_candidate = $alias . '_' . $count++;
    $alias = $alias_candidate;

    $this->expressions[$alias] = [
      'expression' => $expression,
      'alias' => $this->connection->escapeAlias($alias_candidate),
      'arguments' => $arguments,

    return $alias;

   * {@inheritdoc}
  public function execute() {
    try {
      $result = parent::execute();
    catch (\Exception $e) {
      throw $e;

    return $result;



Contains filters are case sensitive
Namesort descending Modifiers Type Description
Query::$comments protected property An array of comments that can be prepended to a query.
Query::$connection protected property The connection object on which to run this query.
Query::$connectionKey protected property The key of the connection object.
Query::$connectionTarget protected property The target of the connection object.
Query::$nextPlaceholder protected property The placeholder counter.
Query::$queryOptions protected property The query options to pass on to the connection object.
Query::$uniqueIdentifier protected property A unique identifier for this query object.
Query::comment public function Adds a comment to the query.
Query::getComments public function Returns a reference to the comments array for the query.
Query::nextPlaceholder public function Gets the next placeholder value for this query object. Overrides PlaceholderInterface::nextPlaceholder
Query::uniqueIdentifier public function Returns a unique identifier for this object. Overrides PlaceholderInterface::uniqueIdentifier
Query::__sleep public function Implements the magic __sleep function to disconnect from the database.
Query::__wakeup public function Implements the magic __wakeup function to reconnect to the database.
QueryConditionTrait::$condition protected property The condition object for this query.
QueryConditionTrait::andConditionGroup public function Creates a new group of conditions ANDed together. Overrides ConditionInterface::andConditionGroup
QueryConditionTrait::condition public function Helper function: builds the most common conditional clauses. Overrides ConditionInterface::condition
QueryConditionTrait::conditionGroupFactory public function Creates an object holding a group of conditions. Overrides ConditionInterface::conditionGroupFactory
QueryConditionTrait::conditions public function Gets the, possibly nested, list of conditions in this conditional clause. Overrides ConditionInterface::conditions
QueryConditionTrait::exists public function Sets a condition that the specified subquery returns values. Overrides ConditionInterface::exists
QueryConditionTrait::isNotNull public function Sets a condition that the specified field be NOT NULL. Overrides ConditionInterface::isNotNull
QueryConditionTrait::isNull public function Sets a condition that the specified field be NULL. Overrides ConditionInterface::isNull
QueryConditionTrait::notExists public function Sets a condition that the specified subquery returns no values. Overrides ConditionInterface::notExists
QueryConditionTrait::orConditionGroup public function Creates a new group of conditions ORed together. Overrides ConditionInterface::orConditionGroup
QueryConditionTrait::where public function Adds an arbitrary WHERE clause to the query. Overrides ConditionInterface::where
Select::$distinct protected property Whether or not this query should be DISTINCT
Select::$expressions protected property The expressions to SELECT as virtual fields.
Select::$fields protected property The fields to SELECT.
Select::$forUpdate protected property The FOR UPDATE status
Select::$group protected property The fields by which to group.
Select::$having protected property The conditional object for the HAVING clause.
Select::$order protected property The fields by which to order this query.
Select::$prepared protected property Indicates if preExecute() has already been called.
Select::$range protected property The range limiters for this query.
Select::$tables protected property The tables against which to JOIN.
Select::$union protected property An 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'…
Select::addExpression public function Overrides Select::addExpression
Select::addField public function Adds a field to the list to be SELECTed. Overrides SelectInterface::addField
Select::addJoin public function Join against another table in the database. Overrides SelectInterface::addJoin
Select::addMetaData public function Adds additional metadata to the query. Overrides AlterableInterface::addMetaData
Select::addTag public function Adds a tag to a query. Overrides AlterableInterface::addTag
Select::arguments public function Gets a complete list of all values to insert into the prepared statement. Overrides QueryConditionTrait::arguments
Select::compile public function Compiles the saved conditions for later retrieval. Overrides QueryConditionTrait::compile
Select::compiled public function Check whether a condition has been previously compiled. Overrides QueryConditionTrait::compiled
Select::countQuery public function Get the equivalent COUNT query of this query as a new query object. Overrides SelectInterface::countQuery
Select::distinct public function Sets this query to be DISTINCT. Overrides SelectInterface::distinct
Select::escapeField public function Escapes a field name string. Overrides SelectInterface::escapeField
Select::escapeLike public function Escapes characters that work as wildcard characters in a LIKE pattern. Overrides SelectInterface::escapeLike
Select::execute public function Overrides Select::execute
Select::extend public function Enhance this object by wrapping it in an extender object. Overrides ExtendableInterface::extend
Select::fields public function Add multiple fields from the same table to be SELECTed. Overrides SelectInterface::fields
Select::forUpdate public function Add FOR UPDATE to the query. Overrides SelectInterface::forUpdate
Select::getArguments public function Compiles and returns an associative array of the arguments for this prepared statement. Overrides SelectInterface::getArguments
Select::getExpressions public function Returns a reference to the expressions array for this query. Overrides SelectInterface::getExpressions
Select::getFields public function Returns a reference to the fields array for this query. Overrides SelectInterface::getFields
Select::getGroupBy public function Returns a reference to the group-by array for this query. Overrides SelectInterface::getGroupBy
Select::getMetaData public function Retrieves a given piece of metadata. Overrides AlterableInterface::getMetaData
Select::getOrderBy public function Returns a reference to the order by array for this query. Overrides SelectInterface::getOrderBy
Select::getTables public function Returns a reference to the tables array for this query. Overrides SelectInterface::getTables
Select::getUnion public function Returns a reference to the union queries for this query. This include queries for UNION, UNION ALL, and UNION DISTINCT. Overrides SelectInterface::getUnion
Select::groupBy public function Groups the result set by the specified field. Overrides SelectInterface::groupBy
Select::hasAllTags public function Determines if a given query has all specified tags. Overrides AlterableInterface::hasAllTags
Select::hasAnyTag public function Determines if a given query has any specified tag. Overrides AlterableInterface::hasAnyTag
Select::hasTag public function Determines if a given query has a given tag. Overrides AlterableInterface::hasTag
Select::having public function Adds an arbitrary HAVING clause to the query. Overrides SelectInterface::having
Select::havingArguments public function Gets a list of all values to insert into the HAVING clause. Overrides SelectInterface::havingArguments
Select::havingCompile public function Compiles the HAVING clause for later retrieval. Overrides SelectInterface::havingCompile
Select::havingCondition public function Helper function to build most common HAVING conditional clauses. Overrides SelectInterface::havingCondition
Select::havingConditions public function Gets a list of all conditions in the HAVING clause. Overrides SelectInterface::havingConditions
Select::havingExists public function Sets a HAVING condition that the specified subquery returns values. Overrides SelectInterface::havingExists
Select::havingIsNotNull public function Sets a condition in the HAVING clause that the specified field be NOT NULL. Overrides SelectInterface::havingIsNotNull
Select::havingIsNull public function Sets a condition in the HAVING clause that the specified field be NULL. Overrides SelectInterface::havingIsNull
Select::havingNotExists public function Sets a HAVING condition that the specified subquery returns no values. Overrides SelectInterface::havingNotExists
Select::innerJoin public function Inner Join against another table in the database. Overrides SelectInterface::innerJoin
Select::isPrepared public function Indicates if preExecute() has already been called on that object. Overrides SelectInterface::isPrepared
Select::join public function Default Join against another table in the database. Overrides SelectInterface::join
Select::leftJoin public function Left Outer Join against another table in the database. Overrides SelectInterface::leftJoin
Select::orderBy public function Overrides SelectQuery::orderBy(). Overrides Select::orderBy
Select::orderRandom public function Overrides Select::orderRandom
Select::preExecute public function Generic preparation and validation for a SELECT query. Overrides SelectInterface::preExecute
Select::prepareCountQuery protected function Prepares a count query from the current query object.
Select::range public function Restricts a query to a given range in the result set. Overrides SelectInterface::range
Select::rightJoin public function Right Outer Join against another table in the database. Overrides SelectInterface::rightJoin
Select::union public function Add another Select query to UNION to this one. Overrides SelectInterface::union
Select::__clone public function Implements the magic __clone function. Overrides Query::__clone
Select::__construct public function Constructs a Select object. Overrides Query::__construct
Select::__toString public function Implements PHP magic __toString method to convert the query to a string. Overrides Query::__toString