class Select

Same name in this branch
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Select.php \Drupal\sqlite\Driver\Database\sqlite\Select
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Select.php \Drupal\mysql\Driver\Database\mysql\Select
  3. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/Select.php \Drupal\driver_test\Driver\Database\DrivertestMysql\Select
  4. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysqlDeprecatedVersion/Select.php \Drupal\driver_test\Driver\Database\DrivertestMysqlDeprecatedVersion\Select
  5. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Select.php \Drupal\driver_test\Driver\Database\DrivertestPgsql\Select
  6. 9 core/tests/Drupal/Tests/Core/Database/Stub/Select.php \Drupal\Tests\Core\Database\Stub\Select
  7. 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/Select.php \Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Select
  8. 9 core/lib/Drupal/Core/Render/Element/Select.php \Drupal\Core\Render\Element\Select
  9. 9 core/lib/Drupal/Core/Database/Driver/sqlite/Select.php \Drupal\Core\Database\Driver\sqlite\Select
  10. 9 core/lib/Drupal/Core/Database/Driver/pgsql/Select.php \Drupal\Core\Database\Driver\pgsql\Select
  11. 9 core/lib/Drupal/Core/Database/Query/Select.php \Drupal\Core\Database\Query\Select
Same name and namespace in other branches
  1. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Select.php \Drupal\sqlite\Driver\Database\sqlite\Select
  2. 11.x core/modules/mysql/src/Driver/Database/mysql/Select.php \Drupal\mysql\Driver\Database\mysql\Select
  3. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Select.php \Drupal\pgsql\Driver\Database\pgsql\Select
  4. 11.x core/tests/Drupal/Tests/Core/Database/Stub/Select.php \Drupal\Tests\Core\Database\Stub\Select
  5. 11.x core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Select.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Select
  6. 11.x core/lib/Drupal/Core/Render/Element/Select.php \Drupal\Core\Render\Element\Select
  7. 11.x core/lib/Drupal/Core/Database/Query/Select.php \Drupal\Core\Database\Query\Select
  8. 10 core/modules/sqlite/src/Driver/Database/sqlite/Select.php \Drupal\sqlite\Driver\Database\sqlite\Select
  9. 10 core/modules/mysql/src/Driver/Database/mysql/Select.php \Drupal\mysql\Driver\Database\mysql\Select
  10. 10 core/modules/pgsql/src/Driver/Database/pgsql/Select.php \Drupal\pgsql\Driver\Database\pgsql\Select
  11. 10 core/tests/Drupal/Tests/Core/Database/Stub/Select.php \Drupal\Tests\Core\Database\Stub\Select
  12. 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Select.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Select
  13. 10 core/lib/Drupal/Core/Render/Element/Select.php \Drupal\Core\Render\Element\Select
  14. 10 core/lib/Drupal/Core/Database/Driver/sqlite/Select.php \Drupal\Core\Database\Driver\sqlite\Select
  15. 10 core/lib/Drupal/Core/Database/Driver/pgsql/Select.php \Drupal\Core\Database\Driver\pgsql\Select
  16. 10 core/lib/Drupal/Core/Database/Query/Select.php \Drupal\Core\Database\Query\Select
  17. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/Select.php \Drupal\driver_test\Driver\Database\DrivertestMysql\Select
  18. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Select.php \Drupal\driver_test\Driver\Database\DrivertestPgsql\Select
  19. 8.9.x core/tests/Drupal/Tests/Core/Database/Stub/Select.php \Drupal\Tests\Core\Database\Stub\Select
  20. 8.9.x core/lib/Drupal/Core/Render/Element/Select.php \Drupal\Core\Render\Element\Select
  21. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Select.php \Drupal\Core\Database\Driver\sqlite\Select
  22. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Select.php \Drupal\Core\Database\Driver\mysql\Select
  23. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Select.php \Drupal\Core\Database\Driver\pgsql\Select
  24. 8.9.x core/lib/Drupal/Core/Database/Query/Select.php \Drupal\Core\Database\Query\Select

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

Hierarchy

Expanded class hierarchy of Select

Related topics

2 files declare their use of Select
Select.php in core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Select.php
Select.php in core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
125 string references to 'Select'
AjaxFormPageCacheTest::testSimpleAJAXFormValue in core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormPageCacheTest.php
Create a simple form, then submit the form via AJAX to change to it.
BooleanOperator::valueForm in core/modules/views/src/Plugin/views/filter/BooleanOperator.php
Callbacks::selectCallback in core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php
Ajax callback triggered by select.
ckeditor5_form_filter_format_form_alter in core/modules/ckeditor5/ckeditor5.module
Implements hook_form_FORM_ID_alter().
Connection::getDriverClass in core/lib/Drupal/Core/Database/Connection.php
Gets the driver-specific override class if any for the specified class.

... See full list

File

core/modules/pgsql/src/Driver/Database/pgsql/Select.php, line 15

Namespace

Drupal\pgsql\Driver\Database\pgsql
View source
class Select extends QuerySelect {
  
  /**
   * {@inheritdoc}
   */
  public function __construct(Connection $connection, $table, $alias = NULL, array $options = []) {
    // @todo Remove the __construct in Drupal 11.
    // @see https://www.drupal.org/project/drupal/issues/3256524
    parent::__construct($connection, $table, $alias, $options);
    unset($this->queryOptions['return']);
  }
  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 = \Drupal::database()->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) {
      [$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 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() {
    $this->connection
      ->addSavepoint();
    try {
      $result = parent::execute();
    } catch (\Exception $e) {
      $this->connection
        ->rollbackSavepoint();
      throw $e;
    }
    $this->connection
      ->releaseSavepoint();
    return $result;
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.