AsyncMysqlRowBlock::getIterator

Get the iterator for the rows in the block

Description

public function getIterator(): AsyncMysqlRowBlockIterator;

Return Values

Examples

The following example shows you how to get an iterator of an AsyncMysqlRowBlock via getIterator(). Getting an iterator of an AsyncMysqlRowBlock gives you an AsyncMysqlRowBlockIterator, where each key of that iterator is an int representing the key to an AsyncMysqlRow of that row block, and each value from current() is the actual AsyncMysqlRow.

And then you can perform operations on that AsyncMysqlRow, including get an iterator of that as well.

<?hh

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\RowBlock\GetIterator;

require __DIR__ .'/../../__includes/async_mysql_connect.inc.php';

use \Hack\UserDocumentation\API\Examples\AsyncMysql\ConnectionInfo as CI;

async function connect(\AsyncMysqlConnectionPool $pool):
  Awaitable<\AsyncMysqlConnection> {
  return await $pool->connect(
    CI::$host,
    CI::$port,
    CI::$db,
    CI::$user,
    CI::$passwd
  );
}
async function iterate(): Awaitable<int> {
  $pool = new \AsyncMysqlConnectionPool(array());
  $conn = await connect($pool);
  $result = await $conn->query(
    'SELECT * FROM test_table WHERE userID < 50'
  );
  $conn->close();
  // A call to $result->rowBlocks() actually pops the first element of the
  // row block Vector. So the call actually mutates the Vector.
  $row_blocks = $result->rowBlocks();
  if ($row_blocks->count() > 0) {
    // An AsyncMysqlRowBlock
    $row_block = $row_blocks[0];
    // An AsyncMysqlRowBlockIterator
    $rbit = $row_block->getIterator();
    // Iterating through a row block iterator will have an int key and
    // an AsyncMysqlRow as its value
    while ($rbit->valid()) {
      // current() gets you am AsyncMysqlRow, getIterator() gets you
      // an AsyncmysqlRowIterator
      $rit = $rbit->current()->getIterator();
      while ($rit->valid()) {
        // current() will give you a string value of the field in the row
        if ($rit->key() > 0 && is_numeric($rit->current())) {
          return intval($rit->current());
        }
        $rit->next();
      }
      $rbit->next();
    }
    return -1;
  } else {
    return -1;
  }
}

function run(): void {
  $r = \HH\Asio\join(iterate());
  var_dump($r);
}

run();
Output
int(41)