X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FTable.php;h=ebab838bf672b8c01f2229720644a617038a17f0;hp=0807626f42156bf6e09e0b1be50c0f7dfec0664b;hb=89caabf7e3ebc62190fccba97e3bc11f7f32b58c;hpb=01bd45d05ce58796db7540d60671b8cff5d46bff diff --git a/lib/pq/Gateway/Table.php b/lib/pq/Gateway/Table.php index 0807626..ebab838 100644 --- a/lib/pq/Gateway/Table.php +++ b/lib/pq/Gateway/Table.php @@ -6,6 +6,11 @@ use \pq\Query\Writer as QueryWriter; class Table { + /** + * @var \pq\Connection + */ + public static $defaultConnection; + /** * @var \pq\Connection */ @@ -19,16 +24,56 @@ class Table /** * @var string */ - protected $rowset; + protected $rowset = "\\pq\\Gateway\\Rowset"; + + /** + * @var \pq\Query\Writer + */ + protected $query; /** - * @param \pq\Connection $conn * @param string $name + * @param \pq\Connection $conn */ - function __construct(\pq\Connection $conn, $name, $rowset = "\\pq\\Gateway\\Rowset") { - $this->conn = $conn; - $this->name = $name; + 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; + } + + /** + * 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; } /** @@ -57,12 +102,10 @@ class Table 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); } @@ -78,7 +121,8 @@ class Table * @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); } @@ -98,14 +142,22 @@ class Table * @param string $returning * @return \pq\Result */ - function create(array $data, $returning = "*") { - $params = array(); - $query = new QueryWriter("INSERT INTO ".$this->conn->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); } @@ -120,9 +172,12 @@ class Table * @retunr \pq\Result */ function update(array $where, array $data, $returning = "*") { - $query = new QueryWriter("UPDATE ".$this->conn->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; } $query->write("WHERE")->criteria($where); if (strlen($returning)) { @@ -138,7 +193,8 @@ class Table * @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);