add identity and lock
[m6w6/pq-gateway] / lib / pq / Query / AsyncExecutor.php
1 <?php
2
3 namespace pq\Query\Executor;
4
5 use \pq\Query\Executor;
6 use \pq\Query\WriterInterface;
7
8 /**
9 * @requires \React\Promise
10 */
11 use \React\Promise\Deferred;
12
13 /**
14 * An asynchronous query executor
15 */
16 class Async extends Executor
17 {
18 /**
19 * Execute the query asynchronously through \pq\Connection::execParamsAsync()
20 * @param \pq\Query\WriterInterface $query
21 * @param callable $callback
22 * @return \React\Promise\DeferredPromise
23 */
24 function execute(WriterInterface $query, callable $callback) {
25 $this->result = null;
26 $this->query = $query;
27 $this->notify();
28
29 $deferred = new Deferred;
30 $this->getConnection()->execParamsAsync($query, $query->getParams(), $query->getTypes(),
31 array($deferred->resolver(), "resolve"));
32
33 return $deferred->then(function($result) {
34 $this->result = $result;
35 $this->notify();
36 })->then($callback);
37 }
38 }