X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FTable.php;h=2e82a214b37d89c90815aded33d340616f222005;hp=ba8094ab0fba87d766b55120df7282f87ac24a46;hb=dc87da9c4ec52918e34f2e43eac3014f0f99a8bc;hpb=e2709f3f6de6d5c5ba272d353db16b015b5258b3 diff --git a/lib/pq/Gateway/Table.php b/lib/pq/Gateway/Table.php index ba8094a..2e82a21 100644 --- a/lib/pq/Gateway/Table.php +++ b/lib/pq/Gateway/Table.php @@ -3,6 +3,7 @@ namespace pq\Gateway; use \pq\Query\Writer as QueryWriter; +use \pq\Query\Executor as QueryExecutor; class Table { @@ -25,6 +26,16 @@ class Table * @var string */ protected $rowset = "\\pq\\Gateway\\Rowset"; + + /** + * @var \pq\Query\WriterIterface + */ + protected $query; + + /** + * @var \pq\Query\ExecutorInterface + */ + protected $exec; /** * @param string $name @@ -45,6 +56,56 @@ class Table 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() { + if (!$this->query) { + $this->query = new QueryWriter; + } + return $this->query; + } + + /** + * Set the query executor + * @param \pq\Query\ExecutorInterface $exec + * @return \pq\Gateway\Table + */ + function setQueryExecutor(\pq\Query\ExecutorInterface $exec) { + $this->exec = $exec; + return $this; + } + + /** + * Get the query executor + * @return \pq\Query\ExecutorInterface + */ + function getQueryExecutor() { + if (!$this->exec) { + $this->exec = new QueryExecutor($this->conn); + } + return $this->exec; + } + /** * @return \pq\Connection */ @@ -61,38 +122,44 @@ class Table /** * Execute the query - * @param \pq\Query\Writer $query + * @param \pq\Query\WriterInterface $query * @return mixed */ protected function execute(QueryWriter $query) { - $result = $query->exec($this->conn); - + return $this->getQueryExecutor()->execute($query, array($this, "onResult")); + } + + /** + * Retreives the result of an executed query + * @param \pq\Result $result + * @return mixed + */ + public function onResult(\pq\Result $result) { if ($result->status != \pq\Result::TUPLES_OK) { 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 $result; } - + /** * Find rows in the table * @param array $where * @param array|string $order * @param int $limit * @param int $offset - * @return \pq\Result + * @return mixed */ 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); } @@ -110,10 +177,11 @@ class Table * Insert a row into the table * @param array $data * @param string $returning - * @return \pq\Result + * @return mixed */ 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(); @@ -138,10 +206,11 @@ class Table * @param array $where * @param array $data * @param string $returning - * @retunr \pq\Result + * @retunr mixed */ 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)); @@ -158,10 +227,11 @@ class Table * Delete rows from the table * @param array $where * @param string $returning - * @return pq\Result + * @return mixed */ 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);