more versatile transaction handling
[m6w6/pq-gateway] / lib / pq / Gateway / Rowset.php
index 7c0a3c23395f56428d9c1506dfdc5f86ef089459..15352591e282a95f5aac57177b1908d1855da5be 100644 (file)
@@ -99,12 +99,14 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable
        
        /**
         * Create all rows of this rowset
-        * @param bool $txn
+        * @param mixed $txn
         * @return \pq\Gateway\Rowset
         * @throws Exception
         */
        function create($txn = true) {
-               $txn = $txn ? $this->table->getConnection()->startTransaction() : false;
+               if ($txn && !($txn instanceof pq\Transaction)) {
+                       $txn = $this->table->getConnection()->startTransaction();
+               }
                try {
                        foreach ($this->rows as $row) {
                                $row->create();
@@ -123,12 +125,14 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable
        
        /**
         * Update all rows of this rowset
-        * @param bool $txn
+        * @param mixed $txn
         * @return \pq\Gateway\Rowset
         * @throws \Exception
         */
        function update($txn = true) {
-               $txn = $txn ? $this->table->getConnection()->startTransaction() : false;
+               if ($txn && !($txn instanceof pq\Transaction)) {
+                       $txn = $this->table->getConnection()->startTransaction();
+               }
                try {
                        foreach ($this->rows as $row) {
                                $row->update();
@@ -147,12 +151,14 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable
        
        /**
         * Delete all rows of this rowset
-        * @param type $txn
+        * @param mixed $txn
         * @return \pq\Gateway\Rowset
         * @throws \Exception
         */
        function delete($txn = true) {
-               $txn = $txn ? $this->table->getConnection()->startTransaction() : false;
+               if ($txn && !($txn instanceof pq\Transaction)) {
+                       $txn = $this->table->getConnection()->startTransaction();
+               }
                try {
                        foreach ($this->rows as $row) {
                                $row->delete();
@@ -206,7 +212,7 @@ class Rowset implements \SeekableIterator, \Countable, \JsonSerializable
         */
        function current() {
                if (!$this->valid()) {
-                       throw new OutOfBoundsException("Invalid row index {$this->index}");
+                       throw new \OutOfBoundsException("Invalid row index {$this->index}");
                }
                return $this->rows[$this->index];
        }