class Query

The SQL storage entity query class.

Hierarchy

Expanded class hierarchy of Query

3 string references to 'Query'
QueryTest::getInfo in drupal/core/modules/views/lib/Drupal/views/Tests/Plugin/QueryTest.php
ViewUI::renderPreview in drupal/core/modules/views/views_ui/lib/Drupal/views_ui/ViewUI.php
_drupal_decode_exception in drupal/core/includes/errors.inc
Decodes an exception and retrieves the correct caller.

File

drupal/core/modules/field/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Query.php, line 16
Definition of Drupal\field_sql_storage\Entity\Query.

Namespace

Drupal\field_sql_storage\Entity
View source
class Query extends QueryBase {

  /**
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * @param string $entity_type
   *   The entity type.
   * @param string $conjunction
   *   - AND: all of the conditions on the query need to match.
   *   - OR: at least one of the conditions on the query need to match.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection to run the query against.
   */
  public function __construct($entity_type, $conjunction, $connection) {
    parent::__construct($entity_type, $conjunction);
    $this->connection = $connection;
  }

  /**
   * Implements Drupal\Core\Entity\Query\QueryInterface::conditionGroupFactory().
   */
  public function conditionGroupFactory($conjunction = 'AND') {
    return new Condition($conjunction);
  }

  /**
   * Implements Drupal\Core\Entity\Query\QueryInterface::execute().
   */
  public function execute() {
    $entity_type = $this->entityType;
    $entity_info = entity_get_info($entity_type);
    if (!isset($entity_info['base_table'])) {
      throw new QueryException("No base table, invalid query.");
    }
    $base_table = $entity_info['base_table'];
    $simple_query = TRUE;
    if (isset($entity_info['data_table'])) {
      $simple_query = FALSE;
    }
    $sqlQuery = $this->connection
      ->select($base_table, 'base_table', array(
      'conjunction' => $this->conjunction,
    ));
    $sqlQuery
      ->addMetaData('entity_type', $entity_type);
    $id_field = $entity_info['entity_keys']['id'];
    $fields[$id_field] = TRUE;
    if (empty($entity_info['entity_keys']['revision'])) {

      // Add the key field for fetchAllKeyed(). When there is no revision
      // support, this is the entity key.
      $sqlQuery
        ->addField('base_table', $entity_info['entity_keys']['id']);
    }
    else {

      // Add the key field for fetchAllKeyed(). When there is revision
      // support, this is the revision key.
      $revision_field = $entity_info['entity_keys']['revision'];
      $fields[$revision_field] = TRUE;
      $sqlQuery
        ->addField('base_table', $revision_field);
    }

    // Now add the value column for fetchAllKeyed(). This is always the
    // entity id.
    $sqlQuery
      ->addField('base_table', $id_field);
    if ($this->accessCheck) {
      $sqlQuery
        ->addTag($entity_type . '_access');
    }
    $sqlQuery
      ->addTag('entity_query');
    $sqlQuery
      ->addTag('entity_query_' . $this->entityType);

    // Add further tags added.
    if (isset($this->alterTags)) {
      foreach ($this->alterTags as $tag => $value) {
        $sqlQuery
          ->addTag($tag);
      }
    }

    // Add further metadata added.
    if (isset($this->alterMetaData)) {
      foreach ($this->alterMetaData as $key => $value) {
        $sqlQuery
          ->addMetaData($key, $value);
      }
    }

    // This now contains first the table containing entity properties and
    // last the entity base table. They might be the same.
    $sqlQuery
      ->addMetaData('age', $this->age);
    $sqlQuery
      ->addMetaData('simple_query', $simple_query);
    $this->condition
      ->compile($sqlQuery);
    if ($this->count) {
      $this->sort = FALSE;
    }

    // Gather the SQL field aliases first to make sure every field table
    // necessary is added. This might change whether the query is simple or
    // not. See below for more on simple queries.
    $sort = array();
    if ($this->sort) {
      $tables = new Tables($sqlQuery);
      foreach ($this->sort as $property => $data) {
        $sort[$property] = isset($fields[$property]) ? $property : $tables
          ->addField($property, 'LEFT', $data['langcode']);
      }
    }

    // If the query is set up for paging either via pager or by range or a
    // count is requested, then the correct amount of rows returned is
    // important. If the entity has a data table or multiple value fields are
    // involved then each revision might appear in several rows and this needs
    // a significantly more complex query.
    $simple_query = !$this->pager && !$this->range && !$this->count || $sqlQuery
      ->getMetaData('simple_query');
    if (!$simple_query) {

      // First, GROUP BY revision id (if it has been added) and entity id.
      // Now each group contains a single revision of an entity.
      foreach (array_keys($fields) as $field) {
        $sqlQuery
          ->groupBy($field);
      }
    }

    // Now we know whether this is a simple query or not, actually do the
    // sorting.
    foreach ($sort as $property => $sql_alias) {
      $direction = $this->sort[$property]['direction'];
      if ($simple_query || isset($fields[$property])) {

        // Simple queries, and the grouped columns of complicated queries
        // can be ordered normally, without the aggregation function.
        $sqlQuery
          ->orderBy($sql_alias, $direction);
      }
      else {

        // Order based on the smallest element of each group if the
        // direction is ascending, or on the largest element of each group
        // if the direction is descending.
        $function = $direction == 'ASC' ? 'min' : 'max';
        $expression = "{$function}({$sql_alias})";
        $sqlQuery
          ->addExpression($expression, "order_by_{$property}_{$direction}");
        $sqlQuery
          ->orderBy($expression, $direction);
      }
    }
    $this
      ->initializePager();
    if ($this->range) {
      $sqlQuery
        ->range($this->range['start'], $this->range['length']);
    }
    if ($this->count) {
      return $sqlQuery
        ->countQuery()
        ->execute()
        ->fetchField();
    }

    // Return a keyed array of results. The key is either the revision_id or
    // the entity_id depending on whether the entity type supports revisions.
    // The value is always the entity id.
    return $sqlQuery
      ->execute()
      ->fetchAllKeyed();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Query::$connection protected property
Query::conditionGroupFactory public function Implements Drupal\Core\Entity\Query\QueryInterface::conditionGroupFactory(). Overrides QueryInterface::conditionGroupFactory
Query::execute public function Implements Drupal\Core\Entity\Query\QueryInterface::execute(). Overrides QueryInterface::execute
Query::__construct public function Overrides QueryBase::__construct
QueryBase::$accessCheck protected property Whether access check is requested or not. Defaults to TRUE.
QueryBase::$age protected property Flag indicating whether to query the current revision or all revisions.
QueryBase::$condition protected property Conditions.
QueryBase::$count protected property TRUE if this is a count query, FALSE if it isn't.
QueryBase::$entityType protected property The entity type this query runs against.
QueryBase::$pager protected property The query pager data.
QueryBase::$range protected property The query range.
QueryBase::$sort protected property The sort data.
QueryBase::accessCheck public function Implements Drupal\Core\Entity\Query\QueryInterface::accessCheck(). Overrides QueryInterface::accessCheck
QueryBase::addMetaData public function Implements Drupal\Core\Database\Query\AlterableInterface::addMetaData(). Overrides AlterableInterface::addMetaData
QueryBase::addTag public function Implements Drupal\Core\Database\Query\AlterableInterface::addTag(). Overrides AlterableInterface::addTag
QueryBase::age public function Implements Drupal\Core\Entity\Query\QueryInterface::age(). Overrides QueryInterface::age
QueryBase::andConditionGroup public function Implements Drupal\Core\Entity\Query\QueryInterface::andConditionGroup(). Overrides QueryInterface::andConditionGroup
QueryBase::condition public function Implements Drupal\Core\Entity\Query\QueryInterface::condition(). Overrides QueryInterface::condition
QueryBase::count public function Implements Drupal\Core\Entity\Query\QueryInterface::count(). Overrides QueryInterface::count
QueryBase::exists public function Implements Drupal\Core\Entity\Query\QueryInterface::exists(). Overrides QueryInterface::exists
QueryBase::getEntityType public function Implements Drupal\Core\Entity\Query\QueryInterface::getEntityType(). Overrides QueryInterface::getEntityType
QueryBase::getMetaData public function Implements Drupal\Core\Database\Query\AlterableInterface::getMetaData(). Overrides AlterableInterface::getMetaData
QueryBase::hasAllTags public function Implements Drupal\Core\Database\Query\AlterableInterface::hasAllTags(). Overrides AlterableInterface::hasAllTags
QueryBase::hasAnyTag public function Implements Drupal\Core\Database\Query\AlterableInterface::hasAnyTag(). Overrides AlterableInterface::hasAnyTag
QueryBase::hasTag public function Implements Drupal\Core\Database\Query\AlterableInterface::hasTag(). Overrides AlterableInterface::hasTag
QueryBase::initializePager protected function Gets the total number of results and initialize a pager for the query.
QueryBase::notExists public function Implements Drupal\Core\Entity\Query\QueryInterface::notExists(). Overrides QueryInterface::notExists
QueryBase::orConditionGroup public function Implements Drupal\Core\Entity\Query\QueryInterface::orConditionGroup(). Overrides QueryInterface::orConditionGroup
QueryBase::pager public function Implements Drupal\Core\Entity\Query\QueryInterface::pager(). Overrides QueryInterface::pager
QueryBase::range public function Implements Drupal\Core\Entity\Query\QueryInterface::range(). Overrides QueryInterface::range
QueryBase::sort public function Implements Drupal\Core\Entity\Query\QueryInterface::sort(). Overrides QueryInterface::sort
QueryBase::tableSort public function Implements Drupal\Core\Entity\Query\QueryInterface::tableSort(). Overrides QueryInterface::tableSort
QueryBase::__clone function Makes sure that the Condition object is cloned as well.