5 use \pq\Query\Writer
as QueryWriter
;
6 use \pq\Query\Executor
as QueryExecutor
;
13 public static $defaultConnection;
28 protected $rowset = "\\pq\\Gateway\\Rowset";
31 * @var \pq\Query\WriterIterface
36 * @var \pq\Query\ExecutorInterface
42 * @param \pq\Connection $conn
44 function __construct($name, \pq\Connection
$conn = null) {
46 $this->conn
= $conn ?
: static::$defaultConnection ?
: new \pq\Connection
;
50 * Set the rowset prototype
51 * @param mixed $rowset
52 * @return \pq\Gateway\Table
54 function setRowsetPrototype($rowset) {
55 $this->rowset
= $rowset;
60 * Get the rowset prototype
63 function getRowsetPrototype() {
68 * Set the query writer
69 * @param \pq\Query\WriterInterface $query
70 * @return \pq\Gateway\Table
72 function setQueryWriter(\pq\Query\WriterInterface
$query) {
73 $this->query
= $query;
78 * Get the query writer
79 * @return \pq\Query\WriterInterface
81 function getQueryWriter() {
83 $this->query
= new QueryWriter
;
89 * Set the query executor
90 * @param \pq\Query\ExecutorInterface $exec
91 * @return \pq\Gateway\Table
93 function setQueryExecutor(\pq\Query\ExecutorInterface
$exec) {
99 * Get the query executor
100 * @return \pq\Query\ExecutorInterface
102 function getQueryExecutor() {
104 $this->exec
= new QueryExecutor($this->conn
);
110 * @return \pq\Connection
112 function getConnection() {
125 * @param \pq\Query\WriterInterface $query
128 protected function execute(QueryWriter
$query) {
129 return $this->getQueryExecutor()->execute($query, array($this, "onResult"));
133 * Retreives the result of an executed query
134 * @param \pq\Result $result
137 public function onResult(\pq\Result
$result) {
138 if ($result->status
!= \pq\Result
::TUPLES_OK
) {
142 $rowset = $this->getRowsetPrototype();
143 if (is_callable($rowset)) {
144 return $rowset($result);
146 return new $rowset($this, $result);
153 * Find rows in the table
154 * @param array $where
155 * @param array|string $order
160 function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
161 $query = $this->getQueryWriter()->reset();
162 $query->write("SELECT * FROM", $this->conn
->quoteName($this->name
));
164 $query->write("WHERE")->criteria($where);
167 $query->write("ORDER BY", $order);
170 $query->write("LIMIT", $limit);
172 $query->write("OFFSET", $offset);
173 return $this->execute($query);
177 * Insert a row into the table
179 * @param string $returning
182 function create(array $data = null, $returning = "*") {
183 $query = $this->getQueryWriter()->reset();
184 $query->write("INSERT INTO", $this->conn
->quoteName($this->name
));
188 foreach ($data as $key => $val) {
189 $query->write($first ?
"(" : ",", $key);
190 $params[] = $query->param($val);
191 $first and $first = false;
193 $query->write(") VALUES (", $params, ")");
195 $query->write("DEFAULT VALUES");
198 if (strlen($returning)) {
199 $query->write("RETURNING", $returning);
201 return $this->execute($query);
205 * Update rows in the table
206 * @param array $where
208 * @param string $returning
211 function update(array $where, array $data, $returning = "*") {
212 $query = $this->getQueryWriter()->reset();
213 $query->write("UPDATE", $this->conn
->quoteName($this->name
));
215 foreach ($data as $key => $val) {
216 $query->write($first ?
"SET" : ",", $key, "=", $query->param($val));
217 $first and $first = false;
219 $query->write("WHERE")->criteria($where);
220 if (strlen($returning)) {
221 $query->write("RETURNING", $returning);
223 return $this->execute($query);
227 * Delete rows from the table
228 * @param array $where
229 * @param string $returning
232 function delete(array $where, $returning = null) {
233 $query = $this->getQueryWriter()->reset();
234 $query->write("DELETE FROM", $this->conn
->quoteName($this->name
));
235 $query->write("WHERE")->criteria($where);
236 if (strlen($returning)) {
237 $query->write("RETURNING", $returning);
239 return $this->execute($query);