namespace pq\Gateway;
use \pq\Query\Writer as QueryWriter;
+use \pq\Query\Executor as QueryExecutor;
class Table
{
/**
* @var \pq\Connection
*/
- protected $connection;
+ public static $defaultConnection;
+
+ /**
+ * @var \pq\Connection
+ */
+ protected $conn;
/**
* @var string
/**
* @var string
*/
- protected $rowset;
+ protected $rowset = "\\pq\\Gateway\\Rowset";
+
+ /**
+ * @var \pq\Query\WriterIterface
+ */
+ protected $query;
+
+ /**
+ * @var \pq\Query\ExecutorInterface
+ */
+ protected $exec;
/**
- * @param \pq\Connection $c
* @param string $name
+ * @param \pq\Connection $conn
*/
- function __construct(\pq\Connection $c, $name, $rowset = "\\pq\\Gateway\\Rowset") {
- $this->connection = $c;
+ function __construct($name, \pq\Connection $conn = null) {
$this->name = $name;
+ $this->conn = $conn ?: static::$defaultConnection ?: new \pq\Connection;
+ }
+
+ /**
+ * Set the rowset prototype
+ * @param mixed $rowset
+ * @return \pq\Gateway\Table
+ */
+ function setRowsetPrototype($rowset) {
$this->rowset = $rowset;
+ return $this;
}
/**
- * Accessor to read-only properties
- * @param string $p
+ * Get the rowset prototype
+ * @return mixed
*/
- function __get($p) {
- return $this->$p;
+ function getRowsetPrototype() {
+ return $this->rowset;
}
-
+
/**
- * @param \pq\Query\Writer $query
- * @param array $criteria
- * @param string $join
- */
- protected function criteria(QueryWriter $query, array $criteria, $join = "AND") {
- $joinable = false;
- $query->write("(");
- foreach ($criteria as $lop => $rop) {
- if (is_array($rop)) {
- if ($joinable) {
- $query->write(")", $join, "(");
- }
- $this->criteria($query, $rop, is_int($lop) ? "AND" : $lop);
- } else {
- if ($joinable) {
- $query->write(")", $join, "(");
- }
- if (!is_int($lop)) {
- $query->write($lop);
- }
- $query->write($query->param($rop));
- }
- $joinable or $joinable = true;
+ * 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);
}
- $query->write(")");
+ return $this->exec;
+ }
+
+ /**
+ * @return \pq\Connection
+ */
+ function getConnection() {
+ return $this->conn;
+ }
+
+ /**
+ * @return string
+ */
+ function getName() {
+ return $this->name;
+ }
+
+ /**
+ * Execute the query
+ * @param \pq\Query\WriterInterface $query
+ * @return mixed
+ */
+ protected function execute(QueryWriter $query) {
+ 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;
+ }
+
+ $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->connection->quoteName($this->name));
+ $query = $this->getQueryWriter()->reset();
+ $query->write("SELECT * FROM", $this->conn->quoteName($this->name));
if ($where) {
- $this->criteria($query->write("WHERE"), $where);
+ $query->write("WHERE")->criteria($where);
}
if ($order) {
$query->write("ORDER BY", $order);
$query->write("LIMIT", $limit);
}
$query->write("OFFSET", $offset);
- return new Rowset($this, $query->exec($this->connection));
+ return $this->execute($query);
}
/**
* Insert a row into the table
* @param array $data
* @param string $returning
- * @return \pq\Result
+ * @return mixed
*/
- function create(array $data, $returning = "*") {
- $params = array();
- $query = new QueryWriter("INSERT INTO ".$this->connection->quoteName($this->name)." (");
- foreach ($data as $key => $val) {
- $query->write($key);
- $params[] = $query->param($val);
+ function create(array $data = null, $returning = "*") {
+ $query = $this->getQueryWriter()->reset();
+ $query->write("INSERT INTO", $this->conn->quoteName($this->name));
+ if ($data) {
+ $first = true;
+ $params = array();
+ foreach ($data as $key => $val) {
+ $query->write($first ? "(" : ",", $key);
+ $params[] = $query->param($val);
+ $first and $first = false;
+ }
+ $query->write(") VALUES (", $params, ")");
+ } else {
+ $query->write("DEFAULT VALUES");
}
- $query->write(") VALUES (", $params, ")");
+
if (strlen($returning)) {
$query->write("RETURNING", $returning);
}
- $result = $query->exec($this->connection);
- if ($result->status == \pq\Result::TUPLES_OK) {
- $rowset = $this->rowset;
- return new $rowset($this, $result);
- }
- return $result;
+ return $this->execute($query);
}
/**
* @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->connection->quoteName($this->name)." SET");
+ $query = $this->getQueryWriter()->reset();
+ $query->write("UPDATE", $this->conn->quoteName($this->name));
+ $first = true;
foreach ($data as $key => $val) {
- $query->write($key, "=", $query->param($val));
+ $query->write($first ? "SET" : ",", $key, "=", $query->param($val));
+ $first and $first = false;
}
- $this->criteria($query->write("WHERE"), $where);
+ $query->write("WHERE")->criteria($where);
if (strlen($returning)) {
$query->write("RETURNING", $returning);
}
- $result = $query->exec($this->connection);
- if ($result->status == \pq\Result::TUPLES_OK) {
- $rowset = $this->rowset;
- return new $rowset($this, $result);
- }
- return $result;
+ return $this->execute($query);
}
/**
* 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->connection->quoteName($this->name));
- $this->criteria($query->write("WHERE"), $where);
+ $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);
}
- $result = $query->exec($this->connection);
- if ($result->status == \pq\Result::TUPLES_OK) {
- $rowset = $this->rowset;
- return new $rowset($this, $result);
- }
- return $result;
+ return $this->execute($query);
}
}
-