protected $rows;
/**
- * @var string
+ * @var mixed
*/
- protected $row;
+ protected $row = "\\pq\\Gateway\\Row";
/**
* @param \pq\Gateway\Table $table
* @param \pq\Result $result
*/
- function __construct(Table $table, \pq\Result $result, $row = "\\pq\\Gateway\\Row") {
+ function __construct(Table $table, \pq\Result $result = null) {
$this->table = $table;
- $this->row = $row;
-
$this->hydrate($result);
}
* @param \pq\Result $result
* @return array
*/
- protected function hydrate(\pq\Result $result) {
+ protected function hydrate(\pq\Result $result = null) {
$this->index = 0;
$this->rows = array();
- $row = $this->row;
- if (is_callable($row)) {
- while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) {
- $this->rows[] = $row($data);
- }
- } else {
- while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) {
- $this->rows[] = new $row($this->table, $data);
+ 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 $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;
}
if (!$this->valid()) {
throw new \OutOfBoundsException("Invalid seek position ($pos)");
}
+
+ return $this;
}
/**
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
$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;
+ }
}