5 use \pq\Query\Writer
as QueryWriter
;
12 public static $defaultConnection;
27 protected $rowset = "\\pq\\Gateway\\Rowset";
30 * @var \pq\Query\Writer
36 * @param \pq\Connection $conn
38 function __construct($name, \pq\Connection
$conn = null) {
40 $this->conn
= $conn ?
: static::$defaultConnection ?
: new \pq\Connection
;
44 * Set the rowset prototype
45 * @param mixed $rowset
46 * @return \pq\Gateway\Table
48 function setRowsetPrototype($rowset) {
49 $this->rowset
= $rowset;
54 * Get the rowset prototype
57 function getRowsetPrototype() {
62 * Set the query writer
63 * @param \pq\Query\WriterInterface $query
64 * @return \pq\Gateway\Table
66 function setQueryWriter(\pq\Query\WriterInterface
$query) {
67 $this->query
= $query;
72 * Get the query writer
73 * @return \pq\Query\WriterInterface
75 function getQueryWriter() {
76 return $this->query ?
: new QueryWriter
;
80 * @return \pq\Connection
82 function getConnection() {
95 * @param \pq\Query\Writer $query
98 protected function execute(QueryWriter
$query) {
99 $result = $query->exec($this->conn
);
101 if ($result->status
!= \pq\Result
::TUPLES_OK
) {
105 $rowset = $this->getRowsetPrototype();
106 if (is_callable($rowset)) {
107 return $rowset($result);
109 return new $rowset($this, $result);
116 * Find rows in the table
117 * @param array $where
118 * @param array|string $order
123 function find(array $where = null, $order = null, $limit = 0, $offset = 0) {
124 $query = $this->getQueryWriter()->reset();
125 $query->write("SELECT * FROM", $this->conn
->quoteName($this->name
));
127 $query->write("WHERE")->criteria($where);
130 $query->write("ORDER BY", $order);
133 $query->write("LIMIT", $limit);
135 $query->write("OFFSET", $offset);
136 return $this->execute($query);
140 * Insert a row into the table
142 * @param string $returning
145 function create(array $data = null, $returning = "*") {
146 $query = $this->getQueryWriter()->reset();
147 $query->write("INSERT INTO", $this->conn
->quoteName($this->name
));
151 foreach ($data as $key => $val) {
152 $query->write($first ?
"(" : ",", $key);
153 $params[] = $query->param($val);
154 $first and $first = false;
156 $query->write(") VALUES (", $params, ")");
158 $query->write("DEFAULT VALUES");
161 if (strlen($returning)) {
162 $query->write("RETURNING", $returning);
164 return $this->execute($query);
168 * Update rows in the table
169 * @param array $where
171 * @param string $returning
174 function update(array $where, array $data, $returning = "*") {
175 $query = $this->getQueryWriter()->reset();
176 $query->write("UPDATE", $this->conn
->quoteName($this->name
));
178 foreach ($data as $key => $val) {
179 $query->write($first ?
"SET" : ",", $key, "=", $query->param($val));
180 $first and $first = false;
182 $query->write("WHERE")->criteria($where);
183 if (strlen($returning)) {
184 $query->write("RETURNING", $returning);
186 return $this->execute($query);
190 * Delete rows from the table
191 * @param array $where
192 * @param string $returning
195 function delete(array $where, $returning = null) {
196 $query = $this->getQueryWriter()->reset();
197 $query->write("DELETE FROM", $this->conn
->quoteName($this->name
));
198 $query->write("WHERE")->criteria($where);
199 if (strlen($returning)) {
200 $query->write("RETURNING", $returning);
202 return $this->execute($query);