d6134caaa314de1bfee7ba4a620fd98322cacd26
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) {
43 $that->hydrate($result);
49 * @param \pq\Result $result
52 protected function hydrate(\pq\Result
$result = null) {
54 $this->rows
= array();
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 * @return \pq\Gateway\Table
93 * Create all rows of this rowset
95 * @return \pq\Gateway\Rowset
98 function create($txn = true) {
99 $txn = $txn ?
$this->table
->getConnection()->startTransaction() : false;
101 foreach ($this->rows
as $row) {
104 } catch (\Exception
$e) {
117 * Update all rows of this rowset
119 * @return \pq\Gateway\Rowset
122 function update($txn = true) {
123 $txn = $txn ?
$this->table
->getConnection()->startTransaction() : false;
125 foreach ($this->rows
as $row) {
128 } catch (\Exception
$e) {
141 * Delete all rows of this rowset
143 * @return \pq\Gateway\Rowset
146 function delete($txn = true) {
147 $txn = $txn ?
$this->table
->getConnection()->startTransaction() : false;
149 foreach ($this->rows
as $row) {
152 } catch (\Exception
$e) {
165 * @implements JsonSerilaizable
167 function jsonSerialize() {
168 return array_map(function($row) {
169 return $row->jsonSerialize();
174 * @implements \Iterator
180 * @implements \Iterator
186 * @implements \Iterator
190 return $this->index
< count($this->rows
);
193 * @implements \Iterator
194 * @return \pq\Gateway\Row
197 return $this->rows
[$this->index
];
200 * @implements \Iterator
208 * @implements SeekableIterator
211 function seek($pos) {
212 /* only index for now */
215 if (!$this->valid()) {
216 throw new \
OutOfBoundsException("Invalid seek position ($pos)");
223 * @implements \Countable
227 return count($this->rows
);
231 * Get the rows of this rowset
239 * Apply a callback on each row of this rowset
240 * @param callable $cb
241 * @return \pq\Gateway\Rowset
243 function apply(callable
$cb) {
244 array_walk($this->rows
, $cb, $this);
250 * @param callable $cb
251 * @return \pq\Gateway\Rowset
253 function filter(callable
$cb) {
254 $rowset = clone $this;
255 $rowset->rows
= array_filter($this->rows
, $cb);
260 * Append a row to the rowset
261 * @param \pq\Gateway\Row $row
263 function append(Row
$row) {
264 $this->rows
[] = $row;