An asynchronous query executor implementation. See pq\Query\Executor for inherited methods and properties.
<?php
use pq\Connection, pq\Result;
use pq\Query\AsyncExecutor, pq\Query\Writer;
use React\Promise\Deferred;
$conn = new Connection;
$exec = new AsyncExecutor($conn);
$exec->setCallbacks(
# init context
function() {
return new Deferred;
},
# done
function(Deferred $context, $result) {
$context->resolve($result);
},
# then
function(Deferred $context, callable $cb) {
return $context->promise()->then($cb);
});
# a contrived query
$query = new Writer("SELECT \$1::int, \$2::int", [1, 2]);
$exec->execute($query, function(Result $result) {
var_dump($result->fetchAll());
});
# this call blocks; see pq\Connection::getResult() etc.
$conn->getResult();
?>
<?php
use pq\Connection, pq\Result;
use pq\Query\AsyncExecutor, pq\Query\Writer;
use Amphp\Deferred;
$conn = new Connection;
$exec = new AsyncExecutor($conn);
$exec->setCallbacks(
# init context
function() {
return new Deferred;
},
# done
function(Deferred $context, $result) {
$context->succeed($result);
},
# then
function(Deferred $context, callable $cb) {
return $context->promise()->when(function($error, $result) use ($cb) {
$cb($result);
});
});
# a contrived query
$query = new Writer("SELECT \$1::int, \$2::int", [1, 2]);
$exec->execute($query, function(Result $result) {
var_dump($result->fetchAll());
});
# this call blocks; see pq\Connection::getResult() etc.
$conn->getResult();
?>
Execute the query asynchronously and let the callback
process the result on resolve.
Promise pq\Query\AsyncExecutor::execute(pq\Query\WriterInterface $query, callable
$callback)
Set the callbacks the asynchronous executor should use to resolve the result.
void
pq\Query\AsyncExecutor::setCallbacks(callable
$init, callable
$done, callable
$then)