flush
[m6w6/pq-gateway] / lib / pq / Query / Executor / Async.php
diff --git a/lib/pq/Query/Executor/Async.php b/lib/pq/Query/Executor/Async.php
new file mode 100644 (file)
index 0000000..d41f53e
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+namespace pq\Query\Executor;
+
+use \pq\Query\ExecutorInterface;
+use \pq\Query\WriterInterface;
+
+use \React\Promise\Deferred;
+
+/**
+ * An asynchronous query executor
+ */
+class Async implements ExecutorInterface
+{
+       protected $conn;
+       
+       /**
+        * Create a asynchronous query exectuor
+        * @param \pq\Connection $conn
+        */
+       function __construct(\pq\Connection $conn) {
+               $this->conn = $conn;
+       }
+       
+       /**
+        * Get the connection
+        * @return \pq\Connection
+        */
+       function getConnection() {
+               return $this->conn;
+       }
+       
+       /**
+        * Set the connection
+        * @param \pq\Connection $conn
+        * @return \pq\Query\Executor\Async
+        */
+       function setConnection(\pq\Connection $conn) {
+               $this->conn = $conn;
+               return $this;
+       }
+       
+       /**
+        * Execute the query asynchronously through \pq\Connection::execParamsAsync()
+        * @param \pq\Query\WriterInterface $query
+        * @param callable $callback
+        * @return \React\Promise\DeferredPromise
+        */
+       function execute(WriterInterface $query, callable $callback) {
+               $deferred = new Deferred; // FIXME
+               $this->getConnection()->execParamsAsync($query, $query->getParams(), $query->getTypes(), 
+                       array($deferred->resolver(), "resolve"));
+               return $deferred->then($callback);
+       }
+}