X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FRowset.php;h=15352591e282a95f5aac57177b1908d1855da5be;hb=HEAD;hp=2b039935dfb42eb10d7cac0a9cc70009def49269;hpb=20d2b6bcce8f1c7a1aaa375b86ffb5be30674956;p=m6w6%2Fpq-gateway diff --git a/lib/pq/Gateway/Rowset.php b/lib/pq/Gateway/Rowset.php index 2b03993..1535259 100644 --- a/lib/pq/Gateway/Rowset.php +++ b/lib/pq/Gateway/Rowset.php @@ -38,7 +38,7 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable * @param \pq\Result $result * @return \pq\Gateway\Rowset */ - function __invoke(\pq\Result $result) { + function __invoke(\pq\Result $result = null) { $that = clone $this; $that->hydrate($result); return $that; @@ -54,7 +54,7 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable $this->rows = array(); if ($result) { - $row = $this->row; + $row = $this->getRowPrototype(); if (is_callable($row)) { while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) { @@ -82,6 +82,14 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable return $this; } + /** + * Get the row prototype + * @return mixed + */ + function getRowPrototype() { + return $this->row; + } + /** * @return \pq\Gateway\Table */ @@ -89,24 +97,81 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable return $this->table; } - function create() { - array_map(function ($row) { - $row->create(); - }, $this->rows); + /** + * Create all rows of this rowset + * @param mixed $txn + * @return \pq\Gateway\Rowset + * @throws Exception + */ + function create($txn = true) { + if ($txn && !($txn instanceof pq\Transaction)) { + $txn = $this->table->getConnection()->startTransaction(); + } + try { + foreach ($this->rows as $row) { + $row->create(); + } + } catch (\Exception $e) { + if ($txn) { + $txn->rollback(); + } + throw $e; + } + if ($txn) { + $txn->commit(); + } return $this; } - function update() { - array_map(function ($row) { - $row->update(); - }, $this->rows); + /** + * Update all rows of this rowset + * @param mixed $txn + * @return \pq\Gateway\Rowset + * @throws \Exception + */ + function update($txn = true) { + if ($txn && !($txn instanceof pq\Transaction)) { + $txn = $this->table->getConnection()->startTransaction(); + } + try { + foreach ($this->rows as $row) { + $row->update(); + } + } catch (\Exception $e) { + if ($txn) { + $txn->rollback(); + } + throw $e; + } + if ($txn) { + $txn->commit(); + } return $this; } - function delete() { - array_map(function ($row) { - $row->delete(); - }, $this->rows); + /** + * Delete all rows of this rowset + * @param mixed $txn + * @return \pq\Gateway\Rowset + * @throws \Exception + */ + function delete($txn = true) { + if ($txn && !($txn instanceof pq\Transaction)) { + $txn = $this->table->getConnection()->startTransaction(); + } + try { + foreach ($this->rows as $row) { + $row->delete(); + } + } catch (\Exception $e) { + if ($txn) { + $txn->rollback(); + } + throw $e; + } + if ($txn) { + $txn->commit(); + } return $this; } @@ -125,12 +190,14 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable function rewind() { $this->index = 0; } + /** * @implements \Iterator */ function next() { ++$this->index; } + /** * @implements \Iterator * @return bool @@ -138,13 +205,18 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable function valid() { return $this->index < count($this->rows); } + /** * @implements \Iterator * @return \pq\Gateway\Row */ function current() { + if (!$this->valid()) { + throw new \OutOfBoundsException("Invalid row index {$this->index}"); + } return $this->rows[$this->index]; } + /** * @implements \Iterator * @return int @@ -164,6 +236,8 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable if (!$this->valid()) { throw new \OutOfBoundsException("Invalid seek position ($pos)"); } + + return $this; } /** @@ -182,6 +256,16 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable return $this->rows; } + /** + * Apply a callback on each row of this rowset + * @param callable $cb + * @return \pq\Gateway\Rowset + */ + function apply(callable $cb) { + array_walk($this->rows, $cb, $this); + return $this; + } + /** * Filter by callback * @param callable $cb @@ -189,7 +273,17 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable */ function filter(callable $cb) { $rowset = clone $this; + $rowset->index = 0; $rowset->rows = array_filter($this->rows, $cb); return $rowset; } + + /** + * Append a row to the rowset + * @param \pq\Gateway\Row $row + */ + function append(Row $row) { + $this->rows[] = $row; + return $this; + } }