queryRange example

/** * {@inheritdoc} */
  public function claimItem($lease_time = 30) {
    // Claim an item by updating its expire fields. If claim is not successful     // another thread may have claimed the item in the meantime. Therefore loop     // until an item is successfully claimed or we are reasonably sure there     // are no unclaimed items left.     while (TRUE) {
      try {
        $item = $this->connection->queryRange('SELECT [data], [created], [item_id] FROM {' . static::TABLE_NAME . '} q WHERE [expire] = 0 AND [name] = :name ORDER BY [created], [item_id] ASC', 0, 1, [':name' => $this->name])->fetchObject();
      }
      catch (\Exception $e) {
        $this->catchException($e);
      }

      // If the table does not exist there are no items currently available to       // claim.       if (empty($item)) {
        return FALSE;
      }

      


  /** * {@inheritdoc} */
  #[\ReturnTypeWillChange]   public function read(#[\SensitiveParameter] $sid) {     $data = '';
    if (!empty($sid)) {
      // Read the session data from the database.       $query = $this->connection
        ->queryRange('SELECT [session] FROM {sessions} WHERE [sid] = :sid', 0, 1, [':sid' => Crypt::hashBase64($sid)]);
      $data = (string) $query->fetchField();
    }
    return $data;
  }

  /** * {@inheritdoc} */
  #[\ReturnTypeWillChange]   public function write(#[\SensitiveParameter] $sid, $value) {     // The exception handler is not active at this point, so we need to do it

class RangeQueryTest extends DatabaseTestBase {

  /** * Confirms that range queries work and return the correct result. */
  public function testRangeQuery() {
    // Test if return correct number of rows.     $range_rows = $this->connection->queryRange("SELECT [name] FROM {test} ORDER BY [name]", 1, 3)->fetchAll();
    $this->assertCount(3, $range_rows, 'Range query work and return correct number of rows.');

    // Test if return target data.     $raw_rows = $this->connection->query('SELECT [name] FROM {test} ORDER BY [name]')->fetchAll();
    $raw_rows = array_slice($raw_rows, 1, 3);
    $this->assertEquals($range_rows$raw_rows);
  }

}

  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /** * {@inheritdoc} */
  public function getOriginalTermId(NodeInterface $node) {
    return $this->database->queryRange("SELECT [f].[tid] FROM {forum} [f] INNER JOIN {node} [n] ON [f].[vid] = [n].[vid] WHERE [n].[nid] = :nid ORDER BY [f].[vid] DESC", 0, 1, [':nid' => $node->id()])->fetchField();
  }

  /** * {@inheritdoc} */
  public function create(NodeInterface $node) {
    $this->database->insert('forum')
      ->fields([
        'tid' => $node->forum_tid,
        'vid' => $node->getRevisionId(),
        'nid' => $node->id(),
      ])
'#maxlength' => '60',
        '#description' => $description,
      ];
    }
    return $form;
  }

  /** * {@inheritdoc} */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $exists = (bool) $this->connection->queryRange('SELECT 1 FROM {users_field_data} WHERE [uid] = :uid AND [default_langcode] = 1', 0, 1, [':uid' => $form_state->getValue('owner_uid')])->fetchField();
    if (!$exists) {
      $form_state->setErrorByName('owner_uid', $this->t('Enter a valid username.'));
    }
  }

  /** * {@inheritdoc} */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['owner_uid'] = $form_state->getValue('owner_uid');
  }

  
$this->connection = $connection;
    $this->table = $table;
    $this->options = $options;
    $this->collection = $collection;
  }

  /** * {@inheritdoc} */
  public function exists($name) {
    try {
      return (bool) $this->connection->queryRange('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :name', 0, 1, [
        ':collection' => $this->collection,
        ':name' => $name,
      ]$this->options)->fetchField();
    }
    catch (\Exception $e) {
      // If we attempt a read without actually having the database or the table       // available, just return FALSE so the caller can handle it.       return FALSE;
    }
  }

  
class Batch extends DatabaseQueue {

  /** * Overrides \Drupal\Core\Queue\DatabaseQueue::claimItem(). * * Unlike \Drupal\Core\Queue\DatabaseQueue::claimItem(), this method provides * a default lease time of 0 (no expiration) instead of 30. This allows the * item to be claimed repeatedly until it is deleted. */
  public function claimItem($lease_time = 0) {
    try {
      $item = $this->connection->queryRange('SELECT [data], [item_id] FROM {queue} q WHERE [name] = :name ORDER BY [item_id] ASC', 0, 1, [':name' => $this->name])->fetchObject();
      if ($item) {
        $item->data = unserialize($item->data);
        return $item;
      }
    }
    catch (\Exception $e) {
      $this->catchException($e);
    }
    return FALSE;
  }

  
if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_node'] = 0;
    $context['sandbox']['max'] = $database->query('SELECT COUNT(DISTINCT [nid]) FROM {node}')->fetchField();
  }

  // For this example, we decide that we can safely process   // 5 nodes at a time without a timeout.   $limit = 5;

  // With each pass through the callback, retrieve the next group of nids.   $result = $database->queryRange("SELECT [nid] FROM {node} WHERE [nid] > :nid ORDER BY [nid] ASC", 0, $limit[':nid' => $context['sandbox']['current_node']]);
  foreach ($result as $row) {

    // Here we actually perform our processing on the current node.     $node_storage->resetCache([$row['nid']]);
    $node = $node_storage->load($row['nid']);
    $node->value1 = $options1;
    $node->value2 = $options2;
    node_save($node);

    // Store some result for post-processing in the finished callback.     $context['results'][] = $node->title;

    
Home | Imprint | This part of the site doesn't use cookies.