flush
[m6w6/pq-gateway] / lib / pq / Gateway / Table.php
1 <?php
2
3 namespace pq\Gateway;
4
5 use \pq\Query\Writer as QueryWriter;
6 use \pq\Query\Executor as QueryExecutor;
7
8 class Table
9 {
10 /**
11 * @var \pq\Connection
12 */
13 public static $defaultConnection;
14
15 /**
16 * @var \pq\Connection
17 */
18 protected $conn;
19
20 /**
21 * @var string
22 */
23 protected $name;
24
25 /**
26 * @var string
27 */
28 protected $rowset = "\\pq\\Gateway\\Rowset";
29
30 /**
31 * @var \pq\Query\WriterIterface
32 */
33 protected $query;
34
35 /**
36 * @var \pq\Query\ExecutorInterface
37 */
38 protected $exec;
39
40 /**
41 * @param string $name
42 * @param \pq\Connection $conn
43 */
44 function __construct($name, \pq\Connection $conn = null) {
45 $this->name = $name;
46 $this->conn = $conn ?: static::$defaultConnection ?: new \pq\Connection;
47 }
48
49 /**
50 * Set the rowset prototype
51 * @param mixed $rowset
52 * @return \pq\Gateway\Table
53 */
54 function setRowsetPrototype($rowset) {
55 $this->rowset = $rowset;
56 return $this;
57 }
58
59 /**
60 * Get the rowset prototype
61 * @return mixed
62 */
63 function getRowsetPrototype() {
64 return $this->rowset;
65 }
66
67 /**
68 * Set the query writer
69 * @param \pq\Query\WriterInterface $query
70 * @return \pq\Gateway\Table
71 */
72 function setQueryWriter(\pq\Query\WriterInterface $query) {
73 $this->query = $query;
74 return $this;
75 }
76
77 /**
78 * Get the query writer
79 * @return \pq\Query\WriterInterface
80 */
81 function getQueryWriter() {
82 if (!$this->query) {
83 $this->query = new QueryWriter;
84 }
85 return $this->query;
86 }
87
88 /**
89 * Set the query executor
90 * @param \pq\Query\ExecutorInterface $exec
91 * @return \pq\Gateway\Table
92 */
93 function setQueryExecutor(\pq\Query\ExecutorInterface $exec) {
94 $this->exec = $exec;
95 return $this;
96 }
97
98 /**
99 * Get the query executor
100 * @return \pq\Query\ExecutorInterface
101 */
102 function getQueryExecutor() {
103 if (!$this->exec) {
104 $this->exec = new QueryExecutor($this->conn);
105 }
106 return $this->exec;
107 }
108
109 /**
110 * @return \pq\Connection
111 */
112 function getConnection() {
113 return $this->conn;
114 }
115
116 /**
117 * @return string
118 */
119 function getName() {
120 return $this->name;
121 }
122
123 /**
124 * Execute the query
125 * @param \pq\Query\WriterInterface $query
126 * @return mixed
127 */
128 protected function execute(QueryWriter $query) {
129 return $this->getQueryExecutor()->execute($query, array($this, "onResult"));
130 }
131
132 /**
133 * Retreives the result of an executed query
134 * @param \pq\Result $result
135 * @return mixed
136 */
137 public function onResult(\pq\Result $result) {
138 if ($result->status != \pq\Result::TUPLES_OK) {
139 return $result;
140 }
141
142 $rowset = $this->getRowsetPrototype();
143 if (is_callable($rowset)) {
144 return $rowset($result);
145 } elseif ($rowset) {
146 return new $rowset($this, $result);
147 }
148
149 return $result;
150 }
151
152 /**
153 * Find rows in the table
154 * @param array $where
155 * @param array|string $order
156 * @param int $limit
157 * @param int $offset
158 * @return mixed
159 */
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));
163 if ($where) {
164 $query->write("WHERE")->criteria($where);
165 }
166 if ($order) {
167 $query->write("ORDER BY", $order);
168 }
169 if ($limit) {
170 $query->write("LIMIT", $limit);
171 }
172 $query->write("OFFSET", $offset);
173 return $this->execute($query);
174 }
175
176 /**
177 * Insert a row into the table
178 * @param array $data
179 * @param string $returning
180 * @return mixed
181 */
182 function create(array $data = null, $returning = "*") {
183 $query = $this->getQueryWriter()->reset();
184 $query->write("INSERT INTO", $this->conn->quoteName($this->name));
185 if ($data) {
186 $first = true;
187 $params = array();
188 foreach ($data as $key => $val) {
189 $query->write($first ? "(" : ",", $key);
190 $params[] = $query->param($val);
191 $first and $first = false;
192 }
193 $query->write(") VALUES (", $params, ")");
194 } else {
195 $query->write("DEFAULT VALUES");
196 }
197
198 if (strlen($returning)) {
199 $query->write("RETURNING", $returning);
200 }
201 return $this->execute($query);
202 }
203
204 /**
205 * Update rows in the table
206 * @param array $where
207 * @param array $data
208 * @param string $returning
209 * @retunr mixed
210 */
211 function update(array $where, array $data, $returning = "*") {
212 $query = $this->getQueryWriter()->reset();
213 $query->write("UPDATE", $this->conn->quoteName($this->name));
214 $first = true;
215 foreach ($data as $key => $val) {
216 $query->write($first ? "SET" : ",", $key, "=", $query->param($val));
217 $first and $first = false;
218 }
219 $query->write("WHERE")->criteria($where);
220 if (strlen($returning)) {
221 $query->write("RETURNING", $returning);
222 }
223 return $this->execute($query);
224 }
225
226 /**
227 * Delete rows from the table
228 * @param array $where
229 * @param string $returning
230 * @return mixed
231 */
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);
238 }
239 return $this->execute($query);
240 }
241 }