|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pinq; |
|
4
|
|
|
|
|
5
|
|
|
use Pinq\Expressions as O; |
|
6
|
|
|
use Pinq\Iterators\IIteratorScheme; |
|
7
|
|
|
use Pinq\Queries\Requests; |
|
8
|
|
|
use Pinq\Queries\Segments; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* The standard queryable class, fully implements the queryable API |
|
12
|
|
|
* |
|
13
|
|
|
* @author Elliot Levin <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class Queryable extends QueryBuilder implements IQueryable, Interfaces\IOrderedQueryable |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Queries\ISourceInfo |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $sourceInfo; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var IIteratorScheme |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $scheme; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
Providers\IQueryProvider $provider, |
|
29
|
|
|
Queries\ISourceInfo $sourceInfo, |
|
30
|
|
|
O\TraversalExpression $queryExpression = null, |
|
31
|
|
|
IIteratorScheme $scheme = null |
|
32
|
|
|
) { |
|
33
|
|
|
parent::__construct($provider); |
|
34
|
|
|
$this->sourceInfo = $sourceInfo; |
|
35
|
|
|
$this->expression = $queryExpression ?: O\Expression::value($this); |
|
36
|
|
|
$this->scheme = $scheme ?: Iterators\SchemeProvider::getDefault(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Returns the requested query from the query provider. |
|
41
|
|
|
* |
|
42
|
|
|
* @param O\Expression $expression |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed The result of the request query |
|
45
|
|
|
*/ |
|
46
|
|
|
final protected function loadQuery(O\Expression $expression) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->provider->load($expression); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getExpression() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->expression; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getSourceInfo() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->sourceInfo; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function isSource() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->expression instanceof O\ValueExpression && $this->expression->getValue() === $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getSource() |
|
67
|
|
|
{ |
|
68
|
|
|
if ($this->isSource()) { |
|
69
|
|
|
return $this; |
|
70
|
|
|
} else { |
|
71
|
|
|
$expression = $this->expression; |
|
72
|
|
|
while ($expression instanceof O\TraversalExpression) { |
|
73
|
|
|
$expression = $expression->getValue(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($expression instanceof O\ValueExpression) { |
|
77
|
|
|
return $expression->getValue(); |
|
78
|
|
|
} else { |
|
79
|
|
|
throw new PinqException( |
|
80
|
|
|
'Invalid origin expression: must be instance of %s', |
|
81
|
|
|
O\ValueExpression::getType()); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
final public function asArray() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
final public function getIterator() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getTrueIterator() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function asTraversable() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function asCollection() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getIteratorScheme() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->scheme; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function iterate(callable $function) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->scheme->walk($this->getTrueIterator(), $function); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
final public function getProvider() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->provider; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// <editor-fold defaultstate="collapsed" desc="Query segments"> |
|
127
|
|
|
|
|
128
|
|
|
public function select(callable $function) |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$function]); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function selectMany(callable $function) |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$function]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function indexBy(callable $function) |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$function]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function keys() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->newMethodSegment(__FUNCTION__); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function reindex() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->newMethodSegment(__FUNCTION__); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function where(callable $predicate) |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$predicate]); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function groupBy(callable $function) |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$function]); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function join($values) |
|
164
|
|
|
{ |
|
165
|
|
|
return new Connectors\JoiningQueryable($this->provider, $this->newMethod(__FUNCTION__, [$values])); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function groupJoin($values) |
|
169
|
|
|
{ |
|
170
|
|
|
return new Connectors\JoiningQueryable($this->provider, $this->newMethod(__FUNCTION__, [$values])); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function union($values) |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$values]); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function intersect($values) |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$values]); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function difference($values) |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$values]); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function append($values) |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$values]); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function whereIn($values) |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$values]); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function except($values) |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$values]); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function skip($amount) |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$amount]); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function take($amount) |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$amount]); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function slice($start, $amount) |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$start, $amount]); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function orderBy(callable $function, $direction) |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$function, $direction]); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function orderByAscending(callable $function) |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$function]); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function orderByDescending(callable $function) |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$function]); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function thenBy(callable $function, $direction) |
|
234
|
|
|
{ |
|
235
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$function, $direction]); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function thenByAscending(callable $function) |
|
239
|
|
|
{ |
|
240
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$function]); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function thenByDescending(callable $function) |
|
244
|
|
|
{ |
|
245
|
|
|
return $this->newMethodSegment(__FUNCTION__, [$function]); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
public function unique() |
|
249
|
|
|
{ |
|
250
|
|
|
return $this->newMethodSegment(__FUNCTION__); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
// </editor-fold> |
|
254
|
|
|
|
|
255
|
|
|
// <editor-fold defaultstate="collapsed" desc="Query Requests"> |
|
256
|
|
|
|
|
257
|
|
|
public function offsetExists($index) |
|
258
|
|
|
{ |
|
259
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$index])); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function offsetGet($index) |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$index])); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
public function offsetSet($index, $value) |
|
268
|
|
|
{ |
|
269
|
|
|
throw PinqException::notSupported(__METHOD__); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
public function offsetUnset($index) |
|
273
|
|
|
{ |
|
274
|
|
|
throw PinqException::notSupported(__METHOD__); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
public function first() |
|
278
|
|
|
{ |
|
279
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__)); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
public function last() |
|
283
|
|
|
{ |
|
284
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__)); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
public function count() |
|
288
|
|
|
{ |
|
289
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__)); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
public function isEmpty() |
|
293
|
|
|
{ |
|
294
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__)); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
public function contains($value) |
|
298
|
|
|
{ |
|
299
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$value])); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
public function aggregate(callable $function) |
|
303
|
|
|
{ |
|
304
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$function])); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
public function all(callable $function = null) |
|
308
|
|
|
{ |
|
309
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$function])); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
public function any(callable $function = null) |
|
313
|
|
|
{ |
|
314
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$function])); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
public function maximum(callable $function = null) |
|
318
|
|
|
{ |
|
319
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$function])); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
public function minimum(callable $function = null) |
|
323
|
|
|
{ |
|
324
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$function])); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
public function sum(callable $function = null) |
|
328
|
|
|
{ |
|
329
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$function])); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
public function average(callable $function = null) |
|
333
|
|
|
{ |
|
334
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$function])); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
public function implode($delimiter, callable $function = null) |
|
338
|
|
|
{ |
|
339
|
|
|
return $this->loadQuery($this->newMethod(__FUNCTION__, [$delimiter, $function])); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
// </editor-fold> |
|
343
|
|
|
} |
|
344
|
|
|
|