X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fpq%2FGateway%2FTable.php;h=0807626f42156bf6e09e0b1be50c0f7dfec0664b;hb=01bd45d05ce58796db7540d60671b8cff5d46bff;hp=295e7a2c60f5db4aa7c5dccd607d07c401c36e5c;hpb=caa2499169b61c7dc6254886e73dcfc000737ed6;p=m6w6%2Fpq-gateway diff --git a/lib/pq/Gateway/Table.php b/lib/pq/Gateway/Table.php index 295e7a2..0807626 100644 --- a/lib/pq/Gateway/Table.php +++ b/lib/pq/Gateway/Table.php @@ -9,7 +9,7 @@ class Table /** * @var \pq\Connection */ - protected $connection; + protected $conn; /** * @var string @@ -22,49 +22,51 @@ class Table protected $rowset; /** - * @param \pq\Connection $c + * @param \pq\Connection $conn * @param string $name */ - function __construct(\pq\Connection $c, $name, $rowset = "\\pq\\Gateway\\Rowset") { - $this->connection = $c; - $this->name = $name; + function __construct(\pq\Connection $conn, $name, $rowset = "\\pq\\Gateway\\Rowset") { + $this->conn = $conn; + $this->name = $name; $this->rowset = $rowset; } /** - * Accessor to read-only properties - * @param string $p + * @return \pq\Connection */ - function __get($p) { - return $this->$p; + function getConnection() { + return $this->conn; + } + + /** + * @return string + */ + function getName() { + return $this->name; } /** + * Execute the query * @param \pq\Query\Writer $query - * @param array $criteria - * @param string $join + * @return mixed */ - 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; + protected function execute(QueryWriter $query) { + $result = $query->exec($this->conn); + + if ($result->status != \pq\Result::TUPLES_OK) { + return $result; + } + + if (is_callable($this->rowset)) { + return call_user_func($this->rowset, $result); } - $query->write(")"); + + if ($this->rowset) { + $rowset = $this->rowset; + return new $rowset($this, $result); + } + + return $result; } /** @@ -76,9 +78,9 @@ class Table * @return \pq\Result */ function find(array $where = null, $order = null, $limit = 0, $offset = 0) { - $query = new QueryWriter("SELECT * FROM ". $this->connection->quoteName($this->name)); + $query = new QueryWriter("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); @@ -87,7 +89,7 @@ class Table $query->write("LIMIT", $limit); } $query->write("OFFSET", $offset); - return new Rowset($this, $query->exec($this->connection)); + return $this->execute($query); } /** @@ -98,7 +100,7 @@ class Table */ function create(array $data, $returning = "*") { $params = array(); - $query = new QueryWriter("INSERT INTO ".$this->connection->quoteName($this->name)." ("); + $query = new QueryWriter("INSERT INTO ".$this->conn->quoteName($this->name)." ("); foreach ($data as $key => $val) { $query->write($key); $params[] = $query->param($val); @@ -107,12 +109,7 @@ class Table 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); } /** @@ -123,20 +120,15 @@ class Table * @retunr \pq\Result */ function update(array $where, array $data, $returning = "*") { - $query = new QueryWriter("UPDATE ".$this->connection->quoteName($this->name)." SET"); + $query = new QueryWriter("UPDATE ".$this->conn->quoteName($this->name)." SET"); foreach ($data as $key => $val) { $query->write($key, "=", $query->param($val)); } - $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); } /** @@ -146,17 +138,11 @@ class Table * @return pq\Result */ function delete(array $where, $returning = null) { - $query = new QueryWriter("DELETE FROM ".$this->connection->quoteName($this->name)); - $this->criteria($query->write("WHERE"), $where); + $query = new QueryWriter("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); } } -