extend example

'status' => ['data' => t('Status'), 'field' => 'u.status'],
    ];

    $query = Database::getConnection()->select('users_field_data', 'u');
    $query->condition('u.uid', 0, '<>');
    $query->condition('u.default_langcode', 1);

    $count_query = clone $query;
    $count_query->addExpression('COUNT([u].[uid])');

    $query = $query
      ->extend(PagerSelectExtender::class)
      ->extend(TableSortExtender::class);
    $query
      ->fields('u', ['uid'])
      ->limit(50)
      ->orderByHeader($header)
      ->setCountQuery($count_query);
    $uids = $query
      ->execute()
      ->fetchCol();

    $options = [];

    

  }

  /** * Confirms that a pager query results with an inner pager query are valid. * * This is a regression test for #467984. */
  public function testInnerPagerQuery() {
    $connection = Database::getConnection();
    $query = $connection->select('test', 't')
      ->extend(PagerSelectExtender::class);
    $query
      ->fields('t', ['age'])
      ->orderBy('age')
      ->limit(5);

    $outer_query = $connection->select($query);
    $outer_query->addField('subquery', 'age');
    $outer_query->orderBy('age');

    $ages = $outer_query
      ->execute()
      

  public function buildContent(UserInterface $user = NULL) {
    if ($user) {
      $query = $this->database->select('tracker_user', 't')
        ->extend(PagerSelectExtender::class)
        ->addMetaData('base_table', 'tracker_user')
        ->condition('t.uid', $user->id());
    }
    else {
      $query = $this->databaseReplica->select('tracker_node', 't')
        ->extend(PagerSelectExtender::class)
        ->addMetaData('base_table', 'tracker_node');
    }

    // This array acts as a placeholder for the data selected later     // while keeping the correct order.

  public function pagerQueryEven($limit) {
    $query = $this->connection->select('test', 't');
    $query
      ->fields('t', ['name'])
      ->orderBy('age');

    // This should result in 2 pages of results.     $query = $query
      ->extend(PagerSelectExtender::class)
      ->limit($limit);

    $names = $query->execute()->fetchCol();

    return new JsonResponse([
      'names' => $names,
    ]);
  }

  /** * Runs a pager query and returns the results. * * This function does care about the page GET parameter, as set by the * test HTTP call. * * @return \Symfony\Component\HttpFoundation\JsonResponse */

      else {
        // If we have a single filter, just add it to the query.         $filter = $query;
      }
      foreach ($options['filters'] as $field => $string) {
        $filter->condition($this->dbFieldTable($field) . '.' . $field, '%' . $this->connection->escapeLike($string) . '%', 'LIKE');
      }
    }

    if (!empty($options['pager limit'])) {
      $query = $query->extend(PagerSelectExtender::class)->limit($options['pager limit']);
    }

    return $query;
  }

  /** * Creates a database record for a string object. * * @param \Drupal\locale\StringInterface $string * The string object. * * @return bool|int * If the operation failed, returns FALSE. * If it succeeded returns the last insert ID of the query, if one exists. * * @throws \Drupal\locale\StringStorageException * If the string is not suitable for this storage, an exception is thrown. */


  /** * Sets up and parses the search query. * * @param string $input * The search keywords entered by the user. */
  protected function queryParseSearchExpression($input) {
    if (!isset($this->searchQuery)) {
      $this->parsed = TRUE;
      $this->searchQuery = \Drupal::service('database.replica')->select('search_index', 'i')->extend(ViewsSearchQuery::class);
      $this->searchQuery->searchExpression($input$this->searchType);
      $this->searchQuery->publicParseSearchExpression();
    }
  }

  /** * {@inheritdoc} */
  public function query() {
    // Since attachment views don't validate the exposed input, parse the search     // expression if required.
$this->searchType = $this->definition['search_type'];
  }

  /** * Sets up and parses the search query. * * @param string $input * The search keywords entered by the user. */
  protected function queryParseSearchExpression($input) {
    if (!isset($this->searchQuery)) {
      $this->searchQuery = \Drupal::service('database.replica')->select('search_index', 'i')->extend(ViewsSearchQuery::class);
      $this->searchQuery->searchExpression($input$this->searchType);
      $this->searchQuery->publicParseSearchExpression();
    }
  }

  /** * {@inheritdoc} */
  public function query($group_by = FALSE) {
    $required = FALSE;
    $this->queryParseSearchExpression($this->argument);
    
'data' => $this->t('Last reply'), 'field' => 'f.last_comment_timestamp'],
    ];

    $order = $this->getTopicOrder($sortby);
    for ($i = 0; $i < count($header)$i++) {
      if ($header[$i]['field'] == $order['field']) {
        $header[$i]['sort'] = $order['sort'];
      }
    }

    $query = $this->connection->select('forum_index', 'f')
      ->extend(PagerSelectExtender::class)
      ->extend(TableSortExtender::class);
    $query->fields('f');
    $query
      ->condition('f.tid', $tid)
      ->addTag('node_access')
      ->addMetaData('base_table', 'forum_index')
      ->orderBy('f.sticky', 'DESC')
      ->orderByHeader($header)
      ->limit($forum_per_page);

    $count_query = $this->connection->select('forum_index', 'f');
    
->alwaysFalse()
      ->execute()->fetchCol();

    $this->assertCount(0, $names);
  }

  /** * Tests that we can force an extended query to return an empty result. */
  public function testExtenderAlwaysFalseCondition() {
    $names = $this->connection->select('test', 'test')
      ->extend(SelectExtender::class)
      ->fields('test', ['name'])
      ->condition('age', 27)
      ->execute()->fetchCol();

    $this->assertCount(1, $names);
    $this->assertSame($names[0], 'George');

    $names = $this->connection->select('test', 'test')
      ->extend(SelectExtender::class)
      ->fields('test', ['name'])
      ->condition('age', 27)
      
    // all of the fields we asked for.     $record = $query->execute()->fetch();
    $this->assertEquals('George', $record->{$name_field}, 'Correct data retrieved.');
    $this->assertEquals(27, $record->{$age_field}, 'Correct data retrieved.');
  }

  /** * Tests having queries. */
  public function testHavingCountQuery() {
    $query = $this->connection->select('test')
      ->extend(PagerSelectExtender::class)
      ->groupBy('age')
      ->having('[age] + 1 > 0');
    $query->addField('test', 'age');
    $query->addExpression('[age] + 1');
    $count = count($query->execute()->fetchCol());
    $this->assertEquals(4, $count, 'Counted the correct number of records.');
  }

  /** * Tests that countQuery removes 'all_fields' statements and ordering clauses. */
  
'"minim am veniam es" OR "dolore sit"' => [2],
      '"minim am veniam es" OR "sit dolore"' => [],
      '"am minim veniam es" -eu' => [6],
      '"am minim veniam" -"cillum dolore"' => [5, 6],
      '"am minim veniam" -"dolore cillum"' => [5, 6, 7],
      'xxxxx "minim am veniam es" OR dolore' => [],
      'xx "minim am veniam es" OR dolore' => [],
    ];
    $connection = Database::getConnection();
    foreach ($queries as $query => $results) {
      $result = $connection->select('search_index', 'i')
        ->extend(SearchQuery::class)
        ->searchExpression($querystatic::SEARCH_TYPE)
        ->execute();

      $set = $result ? $result->fetchAll() : [];
      $this->_testQueryMatching($query$set$results);
      $this->_testQueryScores($query$set$results);
    }

    // These queries are run against the second index type, SEARCH_TYPE_2.     $queries = [
      // Simple AND queries.
->condition('c.entity_id', $entity->id())
      ->condition('c.entity_type', $entity->getEntityTypeId())
      ->condition('c.field_name', $field_name)
      ->condition('c.default_langcode', 1)
      ->addTag('entity_access')
      ->addTag('comment_filter')
      ->addMetaData('base_table', 'comment')
      ->addMetaData('entity', $entity)
      ->addMetaData('field_name', $field_name);

    if ($comments_per_page) {
      $query = $query->extend(PagerSelectExtender::class)
        ->limit($comments_per_page);
      if ($pager_id) {
        $query->element($pager_id);
      }

      $count_query = $this->database->select($data_table, 'c');
      $count_query->addExpression('COUNT(*)');
      $count_query
        ->condition('c.entity_id', $entity->id())
        ->condition('c.entity_type', $entity->getEntityTypeId())
        ->condition('c.field_name', $field_name)
        

  protected function buildTestTable($element$limit) {
    $header = [
      ['data' => 'wid'],
      ['data' => 'type'],
      ['data' => 'timestamp'],
    ];
    $query = Database::getConnection()->select('watchdog', 'd')->extend(PagerSelectExtender::class)->element($element);
    $result = $query
      ->fields('d', ['wid', 'type', 'timestamp'])
      ->limit($limit)
      ->orderBy('d.wid')
      ->execute();
    $rows = [];
    foreach ($result as $row) {
      $rows[] = ['data' => (array) $row];
    }
    return [
      '#theme' => 'table',
      

  public function testExtend(string $expected, string $namespace, string $extend): void {
    $additional_class_loader = new ClassLoader();
    $additional_class_loader->addPsr4("Drupal\\corefake\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefake");
    $additional_class_loader->addPsr4("Drupal\\corefake\\Driver\\Database\\corefakeWithAllCustomClasses\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses");
    $additional_class_loader->register(TRUE);

    $mock_pdo = $this->createMock(StubPDO::class);
    $connection = new StubConnection($mock_pdo['namespace' => $namespace]);

    // Tests the method \Drupal\Core\Database\Query\Select::extend().     $select = $connection->select('test')->extend($extend);
    $this->assertEquals($expectedget_class($select));

    // Get an instance of the class \Drupal\Core\Database\Query\SelectExtender.     $select_extender = $connection->select('test')->extend(SelectExtender::class);
    $this->assertEquals(SelectExtender::classget_class($select_extender));

    // Tests the method \Drupal\Core\Database\Query\SelectExtender::extend().     $select_extender_extended = $select_extender->extend($extend);
    $this->assertEquals($expectedget_class($select_extender_extended));
  }

}
$query->addTag('test');

    $this->assertTrue($query->hasAnyTag('test', 'other'), 'hasAnyTag() returned true.');
    $this->assertFalse($query->hasAnyTag('other', 'stuff'), 'hasAnyTag() returned false.');
  }

  /** * Confirms that an extended query has a tag added to it. */
  public function testExtenderHasTag() {
    $query = $this->connection->select('test')
      ->extend(SelectExtender::class);
    $query->addField('test', 'name');
    $query->addField('test', 'age', 'age');

    $query->addTag('test');

    $this->assertTrue($query->hasTag('test'), 'hasTag() returned true.');
    $this->assertFalse($query->hasTag('other'), 'hasTag() returned false.');
  }

  /** * Tests extended query tagging "has all of these tags" functionality. */
Home | Imprint | This part of the site doesn't use cookies.