X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fpq%2FMapper%2FStorage.php;h=042b6947874b0addc923f1f39684d14d1139f8a6;hb=861bb294d28aba35e011d9d35aad750d93c7dbd7;hp=8e0fcb4df7b20f8b8347e8e241e3c9626f180820;hpb=d4d84fb9b85f0216bd611946d59054a606c71960;p=m6w6%2Fpq-gateway diff --git a/lib/pq/Mapper/Storage.php b/lib/pq/Mapper/Storage.php index 8e0fcb4..042b694 100644 --- a/lib/pq/Mapper/Storage.php +++ b/lib/pq/Mapper/Storage.php @@ -2,39 +2,159 @@ namespace pq\Mapper; +use InvalidArgumentException; +use pq\Connection; +use pq\Gateway\Table; +use pq\Transaction; + class Storage implements StorageInterface { /** - * - * @var pq\Mapper\MapInterface + * The mapping of this storage + * @var MapInterface */ private $map; /** - * @var \pq\Gateway\Table + * The mapper + * @var Mapper + */ + private $mapper; + + /** + * The underlying table gateway + * @var Table */ private $gateway; - - function __construct(MapInterface $map) { - $this->map = $map; - $this->gateway = $map->getGateway(); + + /** + * Create a storage for $map + * @param Mapper $mapper + * @param string $class + */ + function __construct(Mapper $mapper, $class) { + $this->mapper = $mapper; + $this->map = $mapper->mapOf($class); + $this->gateway = $this->map->getGateway(); } - + + /** + * Find by PK + * @param mixed $pk + * @return object + */ + function get($pk) { + $id = $this->gateway->getIdentity(); + if (count($id) == 1 && is_scalar($pk)) { + $vals = [$pk]; + } elseif (is_array($pk) && count($pk) === count($id)) { + $vals = $pk; + } else { + throw InvalidArgumentException( + "Insufficient identity provided; not all fields of %s are provided in %s", + json_encode($id->getColumns()), json_encode($pk)); + } + + $keys = array_map(function($v) { + return "$v="; + }, $id->getColumns()); + + $rowset = $this->gateway->find(array_combine($keys, $vals)); + + return $this->map->map($rowset->current()); + } + + /** + * Find + * @param array $where + * @param string $order + * @param int $limit + * @param int $offset + * @return object[] + */ function find($where = [], $order = null, $limit = null, $offset = null) { /* @var pq\Gateway\Rowset $rowset */ $rowset = $this->gateway->find($where, $order, $limit, $offset); return $this->map->mapAll($rowset); } - + + /** + * Find parent + * @param object $object + * @param string $refName + * @return object + */ + function by($object, $refName) { + $row = $this->mapper->mapOf($object)->getObjects()->getRow($object); + $this->map->refOf($row, $refName, $objects); + return current($objects); + } + + /** + * Find childs + * @param object $object + * @param string $refName + * @return array + */ + function of($object, $refName) { + $row = $this->mapper->mapOf($object)->getObjects()->getRow($object); + $this->map->allOf($row, $refName, $objects); + return $objects; + } + + /** + * Delete + * @param object $object + */ function delete($object) { $cache = $this->map->getObjects(); $row = $cache->asRow($object)->delete(); $cache->resetObject($row); $cache->resetRow($object); } - + + /** + * Save + * @param object $object + */ function save($object) { $this->map->unmap($object); } - -} \ No newline at end of file + + /** + * 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(); + } +}