c4b8afab65d6e563bc07394c147acb5d27e73ed2
[mdref/mdref-pq-gateway] / pq-gateway / pq / Query / AsyncExecutor.md
1 # class pq\Query\AsyncExecutor extends pq\Query\Executor
2
3 An asynchronous query executor implementation.
4 See pq\Query\Executor for inherited methods and properties.
5
6 ## Example with [React/Promise](https://github.com/reactphp/promise)
7
8 <?php
9
10 use pq\Connection, pq\Result;
11 use pq\Query\AsyncExecutor, pq\Query\Writer;
12
13 use React\Promise\Deferred;
14
15 $conn = new Connection;
16 $exec = new AsyncExecutor($conn);
17 $exec->setCallbacks(
18 # init context
19 function() {
20 return new Deferred;
21 },
22 # done
23 function(Deferred $context, $result) {
24 $context->resolve($result);
25 },
26 # then
27 function(Deferred $context, callable $cb) {
28 return $context->promise()->then($cb);
29 });
30
31 # a contrived query
32 $query = new Writer("SELECT \$1::int, \$2::int", [1, 2]);
33 $exec->execute($query, function(Result $result) {
34 var_dump($result->fetchAll());
35 });
36
37 # this call blocks; see pq\Connection::getResult() etc.
38 $conn->getResult();
39
40 ?>
41
42 ## Example with [Amphp/Amp](https://github.com/amphp/amp)
43
44 <?php
45
46 use pq\Connection, pq\Result;
47 use pq\Query\AsyncExecutor, pq\Query\Writer;
48
49 use Amphp\Deferred;
50
51 $conn = new Connection;
52 $exec = new AsyncExecutor($conn);
53 $exec->setCallbacks(
54 # init context
55 function() {
56 return new Deferred;
57 },
58 # done
59 function(Deferred $context, $result) {
60 $context->succeed($result);
61 },
62 # then
63 function(Deferred $context, callable $cb) {
64 return $context->promise()->when(function($error, $result) use ($cb) {
65 $cb($result);
66 });
67 });
68
69 # a contrived query
70 $query = new Writer("SELECT \$1::int, \$2::int", [1, 2]);
71 $exec->execute($query, function(Result $result) {
72 var_dump($result->fetchAll());
73 });
74
75 # this call blocks; see pq\Connection::getResult() etc.
76 $conn->getResult();
77
78 ?>