@@ -20,276 +20,276 @@ |
||
20 | 20 | */ |
21 | 21 | class ActiveRecordQuery implements \IteratorAggregate |
22 | 22 | { |
23 | - private $instance; |
|
23 | + private $instance; |
|
24 | 24 | |
25 | - private $query; |
|
25 | + private $query; |
|
26 | 26 | |
27 | - private $type; |
|
27 | + private $type; |
|
28 | 28 | |
29 | - private $clauses = []; |
|
29 | + private $clauses = []; |
|
30 | 30 | |
31 | - private $maxresultCount; |
|
31 | + private $maxresultCount; |
|
32 | 32 | |
33 | - private $results; |
|
33 | + private $results; |
|
34 | 34 | |
35 | - private $whereExpression = null; |
|
36 | - |
|
37 | - private $limit; |
|
38 | - |
|
39 | - private $offset; |
|
40 | - |
|
41 | - private $orderBy; |
|
42 | - |
|
43 | - private $orderDirection; |
|
44 | - |
|
45 | - /** |
|
46 | - * Constructs a new Active Record Query |
|
47 | - */ |
|
48 | - public function __construct(AbstractActiveRecord $instance, Array $additionalWhereClauses) |
|
49 | - { |
|
50 | - $this->instance = $instance; |
|
51 | - $this->query = new Query($instance->getPdo(), $instance->getTableName()); |
|
52 | - $this->type = $instance; |
|
53 | - $this->clauses = $additionalWhereClauses; |
|
54 | - $this->maxResultCount = null; |
|
55 | - $this->results = null; |
|
56 | - $this->limit = null; |
|
57 | - $this->offset = null; |
|
58 | - } |
|
59 | - |
|
60 | - private function getWhereCondition() |
|
61 | - { |
|
62 | - $clauses = $this->clauses; |
|
63 | - |
|
64 | - // Optionally add user concatenated where expression |
|
65 | - if ($this->whereExpression !== null) { |
|
66 | - $clauses[] = $this->whereExpression; |
|
67 | - } |
|
68 | - |
|
69 | - // Construct where clause |
|
70 | - if (count($clauses) > 0) { |
|
71 | - return Query::AndArray($clauses); |
|
72 | - } |
|
73 | - return null; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Executes the query |
|
78 | - */ |
|
79 | - public function execute() |
|
80 | - { |
|
81 | - $whereCondition = $this->getWhereCondition(); |
|
82 | - if ($whereCondition !== null) { |
|
83 | - $this->query->where($whereCondition); |
|
84 | - } |
|
85 | - |
|
86 | - $this->query->select(); |
|
87 | - |
|
88 | - $this->results = $this->query->execute(); |
|
89 | - |
|
90 | - return $this; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Returns an iterator for the result set |
|
95 | - * @return ArrayIterator |
|
96 | - */ |
|
97 | - public function getIterator() |
|
98 | - { |
|
99 | - return new \ArrayIterator($this->fetchAll()); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * returns the result set of ActiveRecord instances for this query |
|
104 | - * @return Array |
|
105 | - */ |
|
106 | - public function fetchAll() |
|
107 | - { |
|
108 | - try { |
|
109 | - if ($this->results === null) { |
|
110 | - $this->execute(); |
|
111 | - } |
|
112 | - |
|
113 | - $entries = $this->results->fetchAll(); |
|
114 | - if ($entries === false) { |
|
115 | - return []; |
|
116 | - } |
|
117 | - |
|
118 | - $typedResults = []; |
|
119 | - foreach ($entries as $entry) { |
|
120 | - $typedEntry = $this->type->newInstance(); |
|
121 | - $typedEntry->fill($entry); |
|
122 | - $typedResults[] = $typedEntry; |
|
123 | - } |
|
124 | - |
|
125 | - return $typedResults; |
|
126 | - } catch (\PDOException $e) { |
|
127 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - public function fetchAllAsArray($readWhitelist) |
|
132 | - { |
|
133 | - $data = $this->fetchAll(); |
|
134 | - $output = []; |
|
135 | - foreach ($data as $entry) { |
|
136 | - $output[] = $entry->toArray($readWhitelist); |
|
137 | - } |
|
138 | - return $output; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Fetch one record from the database |
|
143 | - * @return AbstractActiveRecord |
|
144 | - */ |
|
145 | - public function fetch() |
|
146 | - { |
|
147 | - try { |
|
148 | - if ($this->results === null) { |
|
149 | - $this->execute(); |
|
150 | - } |
|
151 | - |
|
152 | - $typedResult = $this->type->newInstance(); |
|
153 | - |
|
154 | - $entry = $this->results->fetch(); |
|
155 | - if ($entry === false) { |
|
156 | - return null; |
|
157 | - } |
|
158 | - |
|
159 | - $typedResult->fill($entry); |
|
160 | - |
|
161 | - return $typedResult; |
|
162 | - } catch (\PDOException $e) { |
|
163 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
164 | - } |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Fetch one record from the database and format it as an associative array, |
|
169 | - * filtered by the entries in $readwhitelist |
|
170 | - * @param Array $readWhitelist Array of whitelisted database column keys to be returned in the result |
|
171 | - * @return Array|Null |
|
172 | - */ |
|
173 | - public function fetchAsArray($readWhitelist) |
|
174 | - { |
|
175 | - $res = $this->fetch(); |
|
176 | - if ($res !== null) { |
|
177 | - return $res->toArray($readWhitelist); |
|
178 | - } |
|
179 | - return null; |
|
180 | - } |
|
181 | - |
|
182 | - public function countMaxResults() |
|
183 | - { |
|
184 | - if ($this->maxResultCount === null) { |
|
185 | - $query = new Query($this->instance->getPdo(), $this->instance->getTableName()); |
|
186 | - $query->select(['count(*) as count']); |
|
187 | - |
|
188 | - $whereCondition = $this->getWhereCondition(); |
|
189 | - if ($whereCondition !== null) { |
|
190 | - $query->where($whereCondition); |
|
191 | - } |
|
192 | - |
|
193 | - $this->maxResultCount = $query->execute()->fetch()['count']; |
|
194 | - } |
|
195 | - return $this->maxResultCount; |
|
196 | - } |
|
197 | - |
|
198 | - public function getNumberOfPages() |
|
199 | - { |
|
200 | - if ($this->limit === null) { |
|
201 | - return 1; |
|
202 | - } |
|
203 | - |
|
204 | - if ($this->limit === 0) { |
|
205 | - return 0; |
|
206 | - } |
|
207 | - |
|
208 | - $resultCount = $this->countMaxResults(); |
|
209 | - if ($resultCount % $this->limit > 0) { |
|
210 | - return (int) floor($resultCount / $this->limit) + 1; |
|
211 | - } |
|
212 | - return (int) floor($resultCount / $this->limit); |
|
213 | - } |
|
214 | - |
|
215 | - public function getCurrentPage() |
|
216 | - { |
|
217 | - if ($this->offset === null || $this->offset === 0) { |
|
218 | - return 1; |
|
219 | - } |
|
220 | - |
|
221 | - if ($this->limit === null || $this->limit === 0) { |
|
222 | - return 1; |
|
223 | - } |
|
224 | - |
|
225 | - return (int) floor($this->offset / $this->limit); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Set the where condition |
|
230 | - * |
|
231 | - * @param QueryExpression $expression the query expression |
|
232 | - * @return $this |
|
233 | - * @see https://en.wikipedia.org/wiki/SQL#Operators |
|
234 | - * @see https://en.wikipedia.org/wiki/Where_(SQL) |
|
235 | - */ |
|
236 | - public function where(QueryExpression $expression) |
|
237 | - { |
|
238 | - $this->whereExpression = $expression; |
|
239 | - return $this; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Set an additional group by. |
|
244 | - * |
|
245 | - * @param string $column |
|
246 | - * @return $this |
|
247 | - * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
248 | - */ |
|
249 | - public function groupBy($column) |
|
250 | - { |
|
251 | - $this->query->groupBy($column); |
|
252 | - return $this; |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Set an additional order condition. |
|
257 | - * |
|
258 | - * @param string $column |
|
259 | - * @param string|null $order |
|
260 | - * @return $this |
|
261 | - * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
262 | - * @see https://en.wikipedia.org/wiki/Order_by |
|
263 | - */ |
|
264 | - public function orderBy($column, $order = null) |
|
265 | - { |
|
266 | - $this->query->orderBy($column, $order); |
|
267 | - return $this; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Set the limit. |
|
272 | - * |
|
273 | - * @param mixed $limit |
|
274 | - * @return $this |
|
275 | - */ |
|
276 | - public function limit($limit) |
|
277 | - { |
|
278 | - $this->limit = $limit; |
|
279 | - $this->query->limit($limit); |
|
280 | - return $this; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Set the offset. |
|
285 | - * |
|
286 | - * @param mixed $offset |
|
287 | - * @return $this |
|
288 | - */ |
|
289 | - public function offset($offset) |
|
290 | - { |
|
291 | - $this->offset = $offset; |
|
292 | - $this->query->offset($offset); |
|
293 | - return $this; |
|
294 | - } |
|
35 | + private $whereExpression = null; |
|
36 | + |
|
37 | + private $limit; |
|
38 | + |
|
39 | + private $offset; |
|
40 | + |
|
41 | + private $orderBy; |
|
42 | + |
|
43 | + private $orderDirection; |
|
44 | + |
|
45 | + /** |
|
46 | + * Constructs a new Active Record Query |
|
47 | + */ |
|
48 | + public function __construct(AbstractActiveRecord $instance, Array $additionalWhereClauses) |
|
49 | + { |
|
50 | + $this->instance = $instance; |
|
51 | + $this->query = new Query($instance->getPdo(), $instance->getTableName()); |
|
52 | + $this->type = $instance; |
|
53 | + $this->clauses = $additionalWhereClauses; |
|
54 | + $this->maxResultCount = null; |
|
55 | + $this->results = null; |
|
56 | + $this->limit = null; |
|
57 | + $this->offset = null; |
|
58 | + } |
|
59 | + |
|
60 | + private function getWhereCondition() |
|
61 | + { |
|
62 | + $clauses = $this->clauses; |
|
63 | + |
|
64 | + // Optionally add user concatenated where expression |
|
65 | + if ($this->whereExpression !== null) { |
|
66 | + $clauses[] = $this->whereExpression; |
|
67 | + } |
|
68 | + |
|
69 | + // Construct where clause |
|
70 | + if (count($clauses) > 0) { |
|
71 | + return Query::AndArray($clauses); |
|
72 | + } |
|
73 | + return null; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Executes the query |
|
78 | + */ |
|
79 | + public function execute() |
|
80 | + { |
|
81 | + $whereCondition = $this->getWhereCondition(); |
|
82 | + if ($whereCondition !== null) { |
|
83 | + $this->query->where($whereCondition); |
|
84 | + } |
|
85 | + |
|
86 | + $this->query->select(); |
|
87 | + |
|
88 | + $this->results = $this->query->execute(); |
|
89 | + |
|
90 | + return $this; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Returns an iterator for the result set |
|
95 | + * @return ArrayIterator |
|
96 | + */ |
|
97 | + public function getIterator() |
|
98 | + { |
|
99 | + return new \ArrayIterator($this->fetchAll()); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * returns the result set of ActiveRecord instances for this query |
|
104 | + * @return Array |
|
105 | + */ |
|
106 | + public function fetchAll() |
|
107 | + { |
|
108 | + try { |
|
109 | + if ($this->results === null) { |
|
110 | + $this->execute(); |
|
111 | + } |
|
112 | + |
|
113 | + $entries = $this->results->fetchAll(); |
|
114 | + if ($entries === false) { |
|
115 | + return []; |
|
116 | + } |
|
117 | + |
|
118 | + $typedResults = []; |
|
119 | + foreach ($entries as $entry) { |
|
120 | + $typedEntry = $this->type->newInstance(); |
|
121 | + $typedEntry->fill($entry); |
|
122 | + $typedResults[] = $typedEntry; |
|
123 | + } |
|
124 | + |
|
125 | + return $typedResults; |
|
126 | + } catch (\PDOException $e) { |
|
127 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + public function fetchAllAsArray($readWhitelist) |
|
132 | + { |
|
133 | + $data = $this->fetchAll(); |
|
134 | + $output = []; |
|
135 | + foreach ($data as $entry) { |
|
136 | + $output[] = $entry->toArray($readWhitelist); |
|
137 | + } |
|
138 | + return $output; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Fetch one record from the database |
|
143 | + * @return AbstractActiveRecord |
|
144 | + */ |
|
145 | + public function fetch() |
|
146 | + { |
|
147 | + try { |
|
148 | + if ($this->results === null) { |
|
149 | + $this->execute(); |
|
150 | + } |
|
151 | + |
|
152 | + $typedResult = $this->type->newInstance(); |
|
153 | + |
|
154 | + $entry = $this->results->fetch(); |
|
155 | + if ($entry === false) { |
|
156 | + return null; |
|
157 | + } |
|
158 | + |
|
159 | + $typedResult->fill($entry); |
|
160 | + |
|
161 | + return $typedResult; |
|
162 | + } catch (\PDOException $e) { |
|
163 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
164 | + } |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Fetch one record from the database and format it as an associative array, |
|
169 | + * filtered by the entries in $readwhitelist |
|
170 | + * @param Array $readWhitelist Array of whitelisted database column keys to be returned in the result |
|
171 | + * @return Array|Null |
|
172 | + */ |
|
173 | + public function fetchAsArray($readWhitelist) |
|
174 | + { |
|
175 | + $res = $this->fetch(); |
|
176 | + if ($res !== null) { |
|
177 | + return $res->toArray($readWhitelist); |
|
178 | + } |
|
179 | + return null; |
|
180 | + } |
|
181 | + |
|
182 | + public function countMaxResults() |
|
183 | + { |
|
184 | + if ($this->maxResultCount === null) { |
|
185 | + $query = new Query($this->instance->getPdo(), $this->instance->getTableName()); |
|
186 | + $query->select(['count(*) as count']); |
|
187 | + |
|
188 | + $whereCondition = $this->getWhereCondition(); |
|
189 | + if ($whereCondition !== null) { |
|
190 | + $query->where($whereCondition); |
|
191 | + } |
|
192 | + |
|
193 | + $this->maxResultCount = $query->execute()->fetch()['count']; |
|
194 | + } |
|
195 | + return $this->maxResultCount; |
|
196 | + } |
|
197 | + |
|
198 | + public function getNumberOfPages() |
|
199 | + { |
|
200 | + if ($this->limit === null) { |
|
201 | + return 1; |
|
202 | + } |
|
203 | + |
|
204 | + if ($this->limit === 0) { |
|
205 | + return 0; |
|
206 | + } |
|
207 | + |
|
208 | + $resultCount = $this->countMaxResults(); |
|
209 | + if ($resultCount % $this->limit > 0) { |
|
210 | + return (int) floor($resultCount / $this->limit) + 1; |
|
211 | + } |
|
212 | + return (int) floor($resultCount / $this->limit); |
|
213 | + } |
|
214 | + |
|
215 | + public function getCurrentPage() |
|
216 | + { |
|
217 | + if ($this->offset === null || $this->offset === 0) { |
|
218 | + return 1; |
|
219 | + } |
|
220 | + |
|
221 | + if ($this->limit === null || $this->limit === 0) { |
|
222 | + return 1; |
|
223 | + } |
|
224 | + |
|
225 | + return (int) floor($this->offset / $this->limit); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Set the where condition |
|
230 | + * |
|
231 | + * @param QueryExpression $expression the query expression |
|
232 | + * @return $this |
|
233 | + * @see https://en.wikipedia.org/wiki/SQL#Operators |
|
234 | + * @see https://en.wikipedia.org/wiki/Where_(SQL) |
|
235 | + */ |
|
236 | + public function where(QueryExpression $expression) |
|
237 | + { |
|
238 | + $this->whereExpression = $expression; |
|
239 | + return $this; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Set an additional group by. |
|
244 | + * |
|
245 | + * @param string $column |
|
246 | + * @return $this |
|
247 | + * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
248 | + */ |
|
249 | + public function groupBy($column) |
|
250 | + { |
|
251 | + $this->query->groupBy($column); |
|
252 | + return $this; |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Set an additional order condition. |
|
257 | + * |
|
258 | + * @param string $column |
|
259 | + * @param string|null $order |
|
260 | + * @return $this |
|
261 | + * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
262 | + * @see https://en.wikipedia.org/wiki/Order_by |
|
263 | + */ |
|
264 | + public function orderBy($column, $order = null) |
|
265 | + { |
|
266 | + $this->query->orderBy($column, $order); |
|
267 | + return $this; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Set the limit. |
|
272 | + * |
|
273 | + * @param mixed $limit |
|
274 | + * @return $this |
|
275 | + */ |
|
276 | + public function limit($limit) |
|
277 | + { |
|
278 | + $this->limit = $limit; |
|
279 | + $this->query->limit($limit); |
|
280 | + return $this; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Set the offset. |
|
285 | + * |
|
286 | + * @param mixed $offset |
|
287 | + * @return $this |
|
288 | + */ |
|
289 | + public function offset($offset) |
|
290 | + { |
|
291 | + $this->offset = $offset; |
|
292 | + $this->query->offset($offset); |
|
293 | + return $this; |
|
294 | + } |
|
295 | 295 | } |