5 use InvalidArgumentException
;
10 class Storage
implements StorageInterface
13 * The mapping of this storage
19 * The underlying table gateway
25 * Buffered transaction
31 * Create a storage for $map
32 * @param MapInterface $map
34 function __construct(MapInterface
$map) {
36 $this->gateway
= $map->getGateway();
45 $id = $this->gateway
->getIdentity();
46 if (count($id) == 1 && is_scalar($pk)) {
47 $pk = [current($id->getColumns()) => $pk];
48 } elseif (!is_array($pk) ||
count($pk) !== count($id)) {
49 throw InvalidArgumentException(
50 "Insufficient identity provided; not all fields of %s are provided in %s",
51 json_encode($id->getColumns()), json_encode($pk));
55 foreach ($pk as $k => $v) {
58 $rowset = $this->gateway
->find($where);
60 return $this->map
->map($rowset->current());
66 * @param string $order
71 function find($where = [], $order = null, $limit = null, $offset = null) {
72 /* @var pq\Gateway\Rowset $rowset */
73 $rowset = $this->gateway
->find($where, $order, $limit, $offset);
74 return $this->map
->mapAll($rowset);
79 * @param object $object
81 function delete($object) {
82 $cache = $this->map
->getObjects();
83 $row = $cache->asRow($object)->delete();
84 $cache->resetObject($row);
85 $cache->resetRow($object);
90 * @param object $object
92 function save($object) {
93 $this->map
->unmap($object);
97 * Buffer in a transaction
100 switch ($this->gateway
->getConnection()->transactionStatus
) {
101 case Connection
::TRANS_INTRANS
:
104 $this->gateway
->getQueryExecutor()->execute(new \pq\Query\
Writer("START TRANSACTION"));
112 switch ($this->gateway
->getConnection()->transactionStatus
) {
113 case Connection
::TRANS_IDLE
:
116 $this->gateway
->getQueryExecutor()->execute(new \pq\Query\
Writer("COMMIT"));
124 switch ($this->gateway
->getConnection()->transactionStatus
) {
125 case Connection
::TRANS_IDLE
:
128 $this->gateway
->getQueryExecutor()->execute(new \pq\Query\
Writer("ROLLBACK"));
130 $this->map
->getObjects()->reset();