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