X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FRowset.php;h=d6134caaa314de1bfee7ba4a620fd98322cacd26;hp=2b039935dfb42eb10d7cac0a9cc70009def49269;hb=4879955d1b86d606dc24401f26ebde9be7612fbf;hpb=20d2b6bcce8f1c7a1aaa375b86ffb5be30674956 diff --git a/lib/pq/Gateway/Rowset.php b/lib/pq/Gateway/Rowset.php index 2b03993..d6134ca 100644 --- a/lib/pq/Gateway/Rowset.php +++ b/lib/pq/Gateway/Rowset.php @@ -89,24 +89,75 @@ 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 bool $txn + * @return \pq\Gateway\Rowset + * @throws Exception + */ + function create($txn = true) { + $txn = $txn ? $this->table->getConnection()->startTransaction() : false; + 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 bool $txn + * @return \pq\Gateway\Rowset + * @throws \Exception + */ + function update($txn = true) { + $txn = $txn ? $this->table->getConnection()->startTransaction() : false; + 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 type $txn + * @return \pq\Gateway\Rowset + * @throws \Exception + */ + function delete($txn = true) { + $txn = $txn ? $this->table->getConnection()->startTransaction() : false; + try { + foreach ($this->rows as $row) { + $row->delete(); + } + } catch (\Exception $e) { + if ($txn) { + $txn->rollback(); + } + throw $e; + } + if ($txn) { + $txn->commit(); + } return $this; } @@ -164,6 +215,8 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable if (!$this->valid()) { throw new \OutOfBoundsException("Invalid seek position ($pos)"); } + + return $this; } /** @@ -182,6 +235,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 @@ -192,4 +255,13 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable $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; + } }