flush
[m6w6/pq-gateway] / lib / pq / Query / Executor / Async.php
1 <?php
2
3 namespace pq\Query\Executor;
4
5 use \pq\Query\ExecutorInterface;
6 use \pq\Query\WriterInterface;
7
8 use \React\Promise\Deferred;
9
10 /**
11 * An asynchronous query executor
12 */
13 class Async implements ExecutorInterface
14 {
15 protected $conn;
16
17 /**
18 * Create a asynchronous query exectuor
19 * @param \pq\Connection $conn
20 */
21 function __construct(\pq\Connection $conn) {
22 $this->conn = $conn;
23 }
24
25 /**
26 * Get the connection
27 * @return \pq\Connection
28 */
29 function getConnection() {
30 return $this->conn;
31 }
32
33 /**
34 * Set the connection
35 * @param \pq\Connection $conn
36 * @return \pq\Query\Executor\Async
37 */
38 function setConnection(\pq\Connection $conn) {
39 $this->conn = $conn;
40 return $this;
41 }
42
43 /**
44 * Execute the query asynchronously through \pq\Connection::execParamsAsync()
45 * @param \pq\Query\WriterInterface $query
46 * @param callable $callback
47 * @return \React\Promise\DeferredPromise
48 */
49 function execute(WriterInterface $query, callable $callback) {
50 $deferred = new Deferred; // FIXME
51 $this->getConnection()->execParamsAsync($query, $query->getParams(), $query->getTypes(),
52 array($deferred->resolver(), "resolve"));
53 return $deferred->then($callback);
54 }
55 }