5 use \pq\Query\Writer
as QueryWriter
;
6 use \pq\Query\Executor
as QueryExecutor
;
13 public static $defaultConnection;
18 public static $defaultResolver;
33 protected $rowset = "\\pq\\Gateway\\Rowset";
36 * @var \pq\Query\WriterIterface
41 * @var \pq\Query\ExecutorInterface
46 * @var \pq\Gateway\Table\Relations
51 * @param string $table
52 * @return \pq\Gateway\Table
54 public static function resolve($table) {
55 if ($table instanceof Table
) {
58 if (is_callable(static::$defaultResolver)) {
59 if (($resolved = call_user_func(static::$defaultResolver, $table))) {
63 return new Table($table);
68 * @param \pq\Connection $conn
69 * @param array $dependents
71 function __construct($name, \pq\Connection
$conn = null) {
73 $this->conn
= $conn ?
: static::$defaultConnection ?
: new \pq\Connection
;
77 * Set the rowset prototype
78 * @param mixed $rowset
79 * @return \pq\Gateway\Table
81 function setRowsetPrototype($rowset) {
82 $this->rowset
= $rowset;
87 * Get the rowset prototype
90 function getRowsetPrototype() {
95 * Set the query writer
96 * @param \pq\Query\WriterInterface $query
97 * @return \pq\Gateway\Table
99 function setQueryWriter(\pq\Query\WriterInterface
$query) {
100 $this->query
= $query;
105 * Get the query writer
106 * @return \pq\Query\WriterInterface
108 function getQueryWriter() {
110 $this->query
= new QueryWriter
;
116 * Set the query executor
117 * @param \pq\Query\ExecutorInterface $exec
118 * @return \pq\Gateway\Table
120 function setQueryExecutor(\pq\Query\ExecutorInterface
$exec) {
126 * Get the query executor
127 * @return \pq\Query\ExecutorInterface
129 function getQueryExecutor() {
131 $this->exec
= new QueryExecutor($this->conn
);
137 * Get foreign key relations
138 * @param string $to fkey
139 * @return \pq\Gateway\Table\Relations|stdClass
141 function getRelations($to = null) {
142 if (!isset($this->relations
)) {
143 $this->relations
= new Table\
Relations($this);
146 if (!isset($this->relations
->$to)) {
149 return $this->relations
->$to;
151 return $this->relations
;
155 * Check whether a certain relation exists
156 * @param string $name
157 * @param string $table
160 function hasRelation($name, $table = null) {
161 if (!($rel = $this->getRelations($name))) {
164 if (!isset($table)) {
167 return isset($rel->$table);
171 * @return \pq\Connection
173 function getConnection() {
186 * @param \pq\Query\WriterInterface $query
189 protected function execute(QueryWriter
$query) {
190 echo $query,"\n",json_encode($query->getParams()),"\n";
191 return $this->getQueryExecutor()->execute($query, array($this, "onResult"));
195 * Retreives the result of an executed query
196 * @param \pq\Result $result
199 public function onResult(\pq\Result
$result = null) {
200 if ($result && $result->status
!= \pq\Result
::TUPLES_OK
) {
204 $rowset = $this->getRowsetPrototype();
205 if (is_callable($rowset)) {
206 return $rowset($result);
208 return new $rowset($this, $result);
215 * Find rows in the table
216 * @param array $where
217 * @param array|string $order
222 function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
223 $query = $this->getQueryWriter()->reset();
224 $query->write("SELECT * FROM", $this->conn
->quoteName($this->name
));
226 $query->write("WHERE")->criteria($where);
229 $query->write("ORDER BY", $order);
232 $query->write("LIMIT", $limit);
234 $query->write("OFFSET", $offset);
235 return $this->execute($query);
239 * Get the child rows of a row by foreign key
240 * @param \pq\Gateway\Row $foreign
241 * @param string $name optional fkey name
242 * @param string $order
247 function of(Row
$foreign, $name = null, $order = null, $limit = 0, $offset = 0) {
248 // select * from $this where $this->$foreignColumn = $foreign->$referencedColumn
251 $name = $this->getName();
254 if (!$foreign->getTable()->hasRelation($name, $this->getName())) {
255 return $this->onResult(null);
257 $rel = $foreign->getTable()->getRelations($name)->{$this->getName()};
260 array($rel->foreignColumn
. "=" => $foreign->{$rel->referencedColumn
}),
261 $order, $limit, $offset
266 * Get the parent rows of a row by foreign key
267 * @param \pq\Gateway\Row $me
268 * @param string $foreign
269 * @param string $order
274 function by(Row
$me, $foreign, $order = null, $limit = 0, $offset = 0) {
275 // select * from $foreign where $foreign->$referencedColumn = $me->$foreignColumn
277 if (!$this->hasRelation($foreign, $this->getName())) {
278 return $this->onResult(null);
280 $rel = $this->getRelations($foreign)->{$this->getName()};
282 return static::resolve($rel->referencedTable
)->find(
283 array($rel->referencedColumn
. "=" => $me->{$rel->foreignColumn
}),
284 $order, $limit, $offset
289 * Insert a row into the table
291 * @param string $returning
294 function create(array $data = null, $returning = "*") {
295 $query = $this->getQueryWriter()->reset();
296 $query->write("INSERT INTO", $this->conn
->quoteName($this->name
));
300 foreach ($data as $key => $val) {
301 $query->write($first ?
"(" : ",", $key);
302 $params[] = $query->param($val);
303 $first and $first = false;
305 $query->write(") VALUES (", $params, ")");
307 $query->write("DEFAULT VALUES");
310 if (strlen($returning)) {
311 $query->write("RETURNING", $returning);
313 return $this->execute($query);
317 * Update rows in the table
318 * @param array $where
320 * @param string $returning
323 function update(array $where, array $data, $returning = "*") {
324 $query = $this->getQueryWriter()->reset();
325 $query->write("UPDATE", $this->conn
->quoteName($this->name
));
327 foreach ($data as $key => $val) {
328 $query->write($first ?
"SET" : ",", $key, "=", $query->param($val));
329 $first and $first = false;
331 $query->write("WHERE")->criteria($where);
332 if (strlen($returning)) {
333 $query->write("RETURNING", $returning);
335 return $this->execute($query);
339 * Delete rows from the table
340 * @param array $where
341 * @param string $returning
344 function delete(array $where, $returning = null) {
345 $query = $this->getQueryWriter()->reset();
346 $query->write("DELETE FROM", $this->conn
->quoteName($this->name
));
347 $query->write("WHERE")->criteria($where);
348 if (strlen($returning)) {
349 $query->write("RETURNING", $returning);
351 return $this->execute($query);