storage test
[m6w6/pq-gateway] / lib / pq / Mapper / Storage.php
index 8016ef8b8cc13693ce368241f4c3f199d860ef6a..4882eda881fb512232d6eb85b4455a92ad6e4afc 100644 (file)
@@ -2,7 +2,10 @@
 
 namespace pq\Mapper;
 
+use InvalidArgumentException;
+use pq\Connection;
 use pq\Gateway\Table;
+use pq\Transaction;
 
 class Storage implements StorageInterface
 {
@@ -18,6 +21,12 @@ class Storage implements StorageInterface
         */
        private $gateway;
 
+       /**
+        * Buffered transaction
+        * @var Transaction
+        */
+       private $xaction;
+
        /**
         * Create a storage for $map
         * @param MapInterface $map
@@ -27,6 +36,30 @@ class Storage implements StorageInterface
                $this->gateway = $map->getGateway();
        }
 
+       /**
+        * Find by PK
+        * @param mixed $pk
+        * @return object
+        */
+       function get($pk) {
+               $id = $this->gateway->getIdentity();
+               if (count($id) == 1 && is_scalar($pk)) {
+                       $pk = [current($id->getColumns()) => $pk];
+               } elseif (!is_array($pk) || count($pk) !== count($id)) {
+                       throw InvalidArgumentException(
+                               "Insufficient identity provided; not all fields of %s are provided in %s",
+                               json_encode($id->getColumns()), json_encode($pk));
+               }
+
+               $where = [];
+               foreach ($pk as $k => $v) {
+                       $where["$k="] = $v;
+               }
+               $rowset = $this->gateway->find($where);
+               
+               return $this->map->map($rowset->current());
+       }
+
        /**
         * Find
         * @param array $where
@@ -59,4 +92,41 @@ class Storage implements StorageInterface
        function save($object) {
                $this->map->unmap($object);
        }
+
+       /**
+        * Buffer in a transaction
+        */
+       function buffer() {
+               switch ($this->gateway->getConnection()->transactionStatus) {
+               case Connection::TRANS_INTRANS:
+                       break;
+               default:
+                       $this->gateway->getQueryExecutor()->execute(new \pq\Query\Writer("START TRANSACTION"));
+               }
+       }
+
+       /**
+        * Commit
+        */
+       function flush() {
+               switch ($this->gateway->getConnection()->transactionStatus) {
+               case Connection::TRANS_IDLE:
+                       break;
+               default:
+                       $this->gateway->getQueryExecutor()->execute(new \pq\Query\Writer("COMMIT"));
+               }
+       }
+
+       /**
+        * Rollback
+        */
+       function discard() {
+               switch ($this->gateway->getConnection()->transactionStatus) {
+               case Connection::TRANS_IDLE:
+                       break;
+               default:
+                       $this->gateway->getQueryExecutor()->execute(new \pq\Query\Writer("ROLLBACK"));
+               }
+               $this->map->getObjects()->reset();
+       }
 }