+ $this->hydrate($result);
+ }
+
+ /**
+ * Copy constructor
+ * @param \pq\Result $result
+ * @return \pq\Gateway\Rowset
+ */
+ function __invoke(\pq\Result $result) {
+ $that = clone $this;
+ $that->hydrate($result);
+ return $that;
+ }
+
+ /**
+ *
+ * @param \pq\Result $result
+ * @return array
+ */
+ protected function hydrate(\pq\Result $result = null) {
+ $this->index = 0;
+ $this->rows = array();
+
+ if ($result) {
+ $row = $this->row;
+
+ if (is_callable($row)) {
+ while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) {
+ $this->rows[] = $row($data);
+ }
+ } elseif ($row) {
+ while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) {
+ $this->rows[] = new $row($this->table, $data);
+ }
+ } else {
+ $this->rows = $result->fetchAll(\pq\Result::FETCH_OBJECT);
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * Set the row prototype
+ * @param mixed $row
+ * @return \pq\Gateway\Table
+ */
+ function setRowPrototype($row) {
+ $this->row = $row;
+ return $this;
+ }
+
+ /**
+ * @return \pq\Gateway\Table
+ */
+ function getTable() {
+ return $this->table;
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * 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;