5 class Rowset
implements \SeekableIterator
, \Countable
, \JsonSerializable
8 * @var \pq\Gateway\Table
25 protected $row = "\\pq\\Gateway\\Row";
28 * @param \pq\Gateway\Table $table
29 * @param \pq\Result $result
31 function __construct(Table
$table, \pq\Result
$result = null) {
32 $this->table
= $table;
33 $this->hydrate($result);
38 * @param \pq\Result $result
39 * @return \pq\Gateway\Rowset
41 function __invoke(\pq\Result
$result = null) {
43 $that->hydrate($result);
49 * @param \pq\Result $result
52 protected function hydrate(\pq\Result
$result = null) {
54 $this->rows
= array();
57 $row = $this->getRowPrototype();
59 if (is_callable($row)) {
60 while (($data = $result->fetchRow(\pq\Result
::FETCH_ASSOC
))) {
61 $this->rows
[] = $row($data);
64 while (($data = $result->fetchRow(\pq\Result
::FETCH_ASSOC
))) {
65 $this->rows
[] = new $row($this->table
, $data);
68 $this->rows
= $result->fetchAll(\pq\Result
::FETCH_OBJECT
);
76 * Set the row prototype
78 * @return \pq\Gateway\Table
80 function setRowPrototype($row) {
86 * Get the row prototype
89 function getRowPrototype() {
94 * @return \pq\Gateway\Table
101 * Create all rows of this rowset
103 * @return \pq\Gateway\Rowset
106 function create($txn = true) {
107 if ($txn && !($txn instanceof pq\Transaction
)) {
108 $txn = $this->table
->getConnection()->startTransaction();
111 foreach ($this->rows
as $row) {
114 } catch (\Exception
$e) {
127 * Update all rows of this rowset
129 * @return \pq\Gateway\Rowset
132 function update($txn = true) {
133 if ($txn && !($txn instanceof pq\Transaction
)) {
134 $txn = $this->table
->getConnection()->startTransaction();
137 foreach ($this->rows
as $row) {
140 } catch (\Exception
$e) {
153 * Delete all rows of this rowset
155 * @return \pq\Gateway\Rowset
158 function delete($txn = true) {
159 if ($txn && !($txn instanceof pq\Transaction
)) {
160 $txn = $this->table
->getConnection()->startTransaction();
163 foreach ($this->rows
as $row) {
166 } catch (\Exception
$e) {
179 * @implements JsonSerilaizable
181 function jsonSerialize() {
182 return array_map(function($row) {
183 return $row->jsonSerialize();
188 * @implements \Iterator
195 * @implements \Iterator
202 * @implements \Iterator
206 return $this->index
< count($this->rows
);
210 * @implements \Iterator
211 * @return \pq\Gateway\Row
214 if (!$this->valid()) {
215 throw new \
OutOfBoundsException("Invalid row index {$this->index}");
217 return $this->rows
[$this->index
];
221 * @implements \Iterator
229 * @implements SeekableIterator
232 function seek($pos) {
233 /* only index for now */
236 if (!$this->valid()) {
237 throw new \
OutOfBoundsException("Invalid seek position ($pos)");
244 * @implements \Countable
248 return count($this->rows
);
252 * Get the rows of this rowset
260 * Apply a callback on each row of this rowset
261 * @param callable $cb
262 * @return \pq\Gateway\Rowset
264 function apply(callable
$cb) {
265 array_walk($this->rows
, $cb, $this);
271 * @param callable $cb
272 * @return \pq\Gateway\Rowset
274 function filter(callable
$cb) {
275 $rowset = clone $this;
277 $rowset->rows
= array_filter($this->rows
, $cb);
282 * Append a row to the rowset
283 * @param \pq\Gateway\Row $row
285 function append(Row
$row) {
286 $this->rows
[] = $row;