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
43 protected $dependents;
47 * @param \pq\Connection $conn
48 * @param array $dependents
50 function __construct($name, \pq\Connection
$conn = null, array $dependents = array()) {
52 $this->conn
= $conn ?
: static::$defaultConnection ?
: new \pq\Connection
;
53 $this->dependents
= $dependents;
57 * Set the rowset prototype
58 * @param mixed $rowset
59 * @return \pq\Gateway\Table
61 function setRowsetPrototype($rowset) {
62 $this->rowset
= $rowset;
67 * Get the rowset prototype
70 function getRowsetPrototype() {
75 * Set the query writer
76 * @param \pq\Query\WriterInterface $query
77 * @return \pq\Gateway\Table
79 function setQueryWriter(\pq\Query\WriterInterface
$query) {
80 $this->query
= $query;
85 * Get the query writer
86 * @return \pq\Query\WriterInterface
88 function getQueryWriter() {
90 $this->query
= new QueryWriter
;
96 * Set the query executor
97 * @param \pq\Query\ExecutorInterface $exec
98 * @return \pq\Gateway\Table
100 function setQueryExecutor(\pq\Query\ExecutorInterface
$exec) {
106 * Get the query executor
107 * @return \pq\Query\ExecutorInterface
109 function getQueryExecutor() {
111 $this->exec
= new QueryExecutor($this->conn
);
117 * @return \pq\Connection
119 function getConnection() {
132 * @param \pq\Query\WriterInterface $query
135 protected function execute(QueryWriter
$query) {
136 return $this->getQueryExecutor()->execute($query, array($this, "onResult"));
140 * Retreives the result of an executed query
141 * @param \pq\Result $result
144 public function onResult(\pq\Result
$result) {
145 if ($result->status
!= \pq\Result
::TUPLES_OK
) {
149 $rowset = $this->getRowsetPrototype();
150 if (is_callable($rowset)) {
151 return $rowset($result);
153 return new $rowset($this, $result);
160 * Find rows in the table
161 * @param array $where
162 * @param array|string $order
167 function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
168 $query = $this->getQueryWriter()->reset();
169 $query->write("SELECT * FROM", $this->conn
->quoteName($this->name
));
171 $query->write("WHERE")->criteria($where);
174 $query->write("ORDER BY", $order);
177 $query->write("LIMIT", $limit);
179 $query->write("OFFSET", $offset);
180 return $this->execute($query);
184 * Get the parent row of a row by foreign key
185 * @param \pq\Gateway\Row $dependent
186 * @param string $name optional fkey name
187 * @param string $order
192 function of(Row
$dependent, $name = null, $order = null, $limit = 0, $offset = 0) {
194 $name = $dependent->getTable()->getName();
196 return $this->find(array("{$name}_id=" => $dependent->id
),
197 $order, $limit, $offset);
201 * Get the child rows of a row by foreign key
202 * @param \pq\Gateway\Row $me
203 * @param string $dependent
204 * @param string $order
208 * @throws \LogicException
210 function by(Row
$me, $dependent, $order = null, $limit = 0, $offset = 0) {
211 if (!isset($this->dependents
[$dependent])) {
212 throw new \
LogicException("Unknown dependent table $dependent");
215 $dependentClass = $this->dependents
[$dependent];
216 $dependentModel = new $dependentClass($this->conn
);
217 return $dependentModel->of($me, null, $order, $limit, $offset);
221 * Insert a row into the table
223 * @param string $returning
226 function create(array $data = null, $returning = "*") {
227 $query = $this->getQueryWriter()->reset();
228 $query->write("INSERT INTO", $this->conn
->quoteName($this->name
));
232 foreach ($data as $key => $val) {
233 $query->write($first ?
"(" : ",", $key);
234 $params[] = $query->param($val);
235 $first and $first = false;
237 $query->write(") VALUES (", $params, ")");
239 $query->write("DEFAULT VALUES");
242 if (strlen($returning)) {
243 $query->write("RETURNING", $returning);
245 return $this->execute($query);
249 * Update rows in the table
250 * @param array $where
252 * @param string $returning
255 function update(array $where, array $data, $returning = "*") {
256 $query = $this->getQueryWriter()->reset();
257 $query->write("UPDATE", $this->conn
->quoteName($this->name
));
259 foreach ($data as $key => $val) {
260 $query->write($first ?
"SET" : ",", $key, "=", $query->param($val));
261 $first and $first = false;
263 $query->write("WHERE")->criteria($where);
264 if (strlen($returning)) {
265 $query->write("RETURNING", $returning);
267 return $this->execute($query);
271 * Delete rows from the table
272 * @param array $where
273 * @param string $returning
276 function delete(array $where, $returning = null) {
277 $query = $this->getQueryWriter()->reset();
278 $query->write("DELETE FROM", $this->conn
->quoteName($this->name
));
279 $query->write("WHERE")->criteria($where);
280 if (strlen($returning)) {
281 $query->write("RETURNING", $returning);
283 return $this->execute($query);