add identity and lock
[m6w6/pq-gateway] / lib / pq / Query / Executor.php
index a01580a030231116fc1465526e29e4218bb7f522..d1c75210d9a7e38a6ea33ac3b06a823c6d569b09 100644 (file)
@@ -12,12 +12,28 @@ class Executor implements ExecutorInterface
         */
        protected $conn;
        
+       /**
+        * @var \SplObjectStorage
+        */
+       protected $observers;
+       
+       /**
+        * @var WriterInterface
+        */
+       protected $query;
+       
+       /**
+        * @var \pq\Result
+        */
+       protected $result;
+       
        /**
         * Create a synchronous query executor
         * @param \pq\Connection $conn
         */
        function __construct(\pq\Connection $conn) {
                $this->conn = $conn;
+               $this->observers = new \SplObjectStorage;
        }
        
        /**
@@ -38,6 +54,22 @@ class Executor implements ExecutorInterface
                return $this;
        }
        
+       /**
+        * @inheritdoc
+        * @return WriterInterface
+        */
+       function getQuery() {
+               return $this->query;
+       }
+       
+       /**
+        * @inheritdoc
+        * @return \pq\Result
+        */
+       function getResult() {
+               return $this->result;
+       }
+       
        /**
         * Execute the query synchronously through \pq\Connection::execParams()
         * @param \pq\Query\WriterInterface $query
@@ -45,6 +77,36 @@ class Executor implements ExecutorInterface
         * @return mixed
         */
        function execute(WriterInterface $query, callable $callback) {
-               return $callback($this->getConnection()->execParams($query, $query->getParams(), $query->getTypes()));
+               $this->result = null;
+               $this->query = $query;
+               $this->notify();
+               $this->result = $this->getConnection()->execParams($query, $query->getParams(), $query->getTypes());
+               $this->notify();
+               return $callback($this->result);
+       }
+       
+       /**
+        * @implements \SplSubject
+        * @param \SplObserver $observer
+        */
+       function attach(\SplObserver $observer) {
+               $this->observers->attach($observer);
+       }
+       
+       /**
+        * @implements \SplSubject
+        * @param \SplObserver $observer
+        */
+       function detach(\SplObserver $observer) {
+               $this->observers->detach($observer);
+       }
+       
+       /**
+        * @implements \SplSubject
+        */
+       function notify() {
+               foreach ($this->observers as $observer){
+                       $observer->update($this);
+               }
        }
 }