* @param \pq\Result $result
* @return \pq\Gateway\Rowset
*/
- function __invoke(\pq\Result $result) {
+ function __invoke(\pq\Result $result = null) {
$that = clone $this;
$that->hydrate($result);
return $that;
$this->rows = array();
if ($result) {
- $row = $this->row;
+ $row = $this->getRowPrototype();
if (is_callable($row)) {
while (($data = $result->fetchRow(\pq\Result::FETCH_ASSOC))) {
return $this;
}
+ /**
+ * Get the row prototype
+ * @return mixed
+ */
+ function getRowPrototype() {
+ return $this->row;
+ }
+
/**
* @return \pq\Gateway\Table
*/
function rewind() {
$this->index = 0;
}
+
/**
* @implements \Iterator
*/
function next() {
++$this->index;
}
+
/**
* @implements \Iterator
* @return bool
function valid() {
return $this->index < count($this->rows);
}
+
/**
* @implements \Iterator
* @return \pq\Gateway\Row
*/
function current() {
+ if (!$this->valid()) {
+ throw new OutOfBoundsException("Invalid row index {$this->index}");
+ }
return $this->rows[$this->index];
}
+
/**
* @implements \Iterator
* @return int
*/
function filter(callable $cb) {
$rowset = clone $this;
+ $rowset->index = 0;
$rowset->rows = array_filter($this->rows, $cb);
return $rowset;
}
* @var string
*/
protected $rowset = "\\pq\\Gateway\\Rowset";
+
+ /**
+ * @var \pq\Query\Writer
+ */
+ protected $query;
/**
* @param string $name
return $this;
}
+ /**
+ * Get the rowset prototype
+ * @return mixed
+ */
+ function getRowsetPrototype() {
+ return $this->rowset;
+ }
+
+ /**
+ * Set the query writer
+ * @param \pq\Query\WriterInterface $query
+ * @return \pq\Gateway\Table
+ */
+ function setQueryWriter(\pq\Query\WriterInterface $query) {
+ $this->query = $query;
+ return $this;
+ }
+
+ /**
+ * Get the query writer
+ * @return \pq\Query\WriterInterface
+ */
+ function getQueryWriter() {
+ return $this->query ?: new QueryWriter;
+ }
+
/**
* @return \pq\Connection
*/
return $result;
}
- if (is_callable($this->rowset)) {
- return call_user_func($this->rowset, $result);
- }
-
- if ($this->rowset) {
- $rowset = $this->rowset;
+ $rowset = $this->getRowsetPrototype();
+ if (is_callable($rowset)) {
+ return $rowset($result);
+ } elseif ($rowset) {
return new $rowset($this, $result);
}
* @return \pq\Result
*/
function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
- $query = new QueryWriter("SELECT * FROM ". $this->conn->quoteName($this->name));
+ $query = $this->getQueryWriter()->reset();
+ $query->write("SELECT * FROM", $this->conn->quoteName($this->name));
if ($where) {
$query->write("WHERE")->criteria($where);
}
* @return \pq\Result
*/
function create(array $data = null, $returning = "*") {
- $query = new QueryWriter("INSERT INTO ".$this->conn->quoteName($this->name));
+ $query = $this->getQueryWriter()->reset();
+ $query->write("INSERT INTO", $this->conn->quoteName($this->name));
if ($data) {
$first = true;
$params = array();
* @retunr \pq\Result
*/
function update(array $where, array $data, $returning = "*") {
- $query = new QueryWriter("UPDATE ".$this->conn->quoteName($this->name));
+ $query = $this->getQueryWriter()->reset();
+ $query->write("UPDATE", $this->conn->quoteName($this->name));
$first = true;
foreach ($data as $key => $val) {
$query->write($first ? "SET" : ",", $key, "=", $query->param($val));
* @return pq\Result
*/
function delete(array $where, $returning = null) {
- $query = new QueryWriter("DELETE FROM ".$this->conn->quoteName($this->name));
+ $query = $this->getQueryWriter()->reset();
+ $query->write("DELETE FROM", $this->conn->quoteName($this->name));
$query->write("WHERE")->criteria($where);
if (strlen($returning)) {
$query->write("RETURNING", $returning);