1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleMapper; |
4
|
|
|
|
5
|
|
|
use Nette\Database\Table\IRow; |
6
|
|
|
use Nette\Database\Table\Selection as NetteDatabaseSelection; |
7
|
|
|
use Nette\Database\Table\ActiveRow as NetteDatabaseActiveRow; |
8
|
|
|
use Nette\InvalidArgumentException; |
9
|
|
|
use ArrayAccess; |
10
|
|
|
use Iterator; |
11
|
|
|
use Countable; |
12
|
|
|
use Traversable; |
13
|
|
|
|
14
|
|
|
class Selection implements Iterator, Countable, ArrayAccess |
15
|
|
|
{ |
16
|
|
|
/** @var NetteDatabaseSelection */ |
17
|
|
|
private $selection; |
18
|
|
|
|
19
|
|
|
/** @var Structure */ |
20
|
|
|
protected $structure; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param NetteDatabaseSelection $selection |
24
|
|
|
* @param Structure|null $structure |
25
|
|
|
*/ |
26
|
36 |
|
public function __construct(NetteDatabaseSelection $selection, Structure $structure = null) |
27
|
|
|
{ |
28
|
36 |
|
$this->selection = $selection; |
29
|
36 |
|
$this->structure = $structure; |
30
|
36 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return NetteDatabaseSelection |
34
|
|
|
*/ |
35
|
2 |
|
public function getSelection() |
36
|
|
|
{ |
37
|
2 |
|
return $this->selection; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/**********************************************************************\ |
41
|
|
|
* Wrapper function - fetch |
42
|
|
|
\**********************************************************************/ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns row specified by primary key |
46
|
|
|
* @param mixed $key Primary key |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
2 |
|
public function get($key) |
50
|
|
|
{ |
51
|
2 |
|
$row = $this->selection->get($key); |
52
|
2 |
|
return $row instanceof NetteDatabaseActiveRow ? $this->prepareRecord($row) : $row; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns one record |
57
|
|
|
* @return bool|mixed |
58
|
|
|
*/ |
59
|
6 |
|
public function fetch() |
60
|
|
|
{ |
61
|
6 |
|
$row = $this->selection->fetch(); |
62
|
6 |
|
return $row ? $this->prepareRecord($row) : $row; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Fetches single field |
67
|
|
|
* @param string|null $column |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
2 |
|
public function fetchField($column = null) |
71
|
|
|
{ |
72
|
2 |
|
return $this->selection->fetchField($column); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Fetch key => value pairs |
77
|
|
|
* @param string|null $key |
78
|
|
|
* @param string|null $value |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
2 |
|
public function fetchPairs($key = null, $value = null) |
82
|
|
|
{ |
83
|
2 |
|
$result = []; |
84
|
|
|
|
85
|
2 |
|
$pairs = $this->selection->fetchPairs($key, $value); |
86
|
2 |
|
foreach ($pairs as $k => $v) { |
87
|
2 |
|
$result[$k] = $v instanceof NetteDatabaseActiveRow ? $this->prepareRecord($v) : $v; |
88
|
2 |
|
} |
89
|
2 |
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns all records |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
4 |
|
public function fetchAll() |
97
|
|
|
{ |
98
|
4 |
|
return $this->prepareRecords($this->selection->fetchAll()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Some examples of usage: https://github.com/nette/utils/blob/master/tests%2FUtils%2FArrays.associate().phpt |
103
|
|
|
* @param string $path |
104
|
|
|
* @return array|\stdClass |
105
|
|
|
*/ |
106
|
2 |
|
public function fetchAssoc($path) |
107
|
|
|
{ |
108
|
2 |
|
return $this->selection->fetchAssoc($path); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/**********************************************************************\ |
112
|
|
|
* Wrapper function - sql selections |
113
|
|
|
\**********************************************************************/ |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Adds select clause, more calls appends to the end |
117
|
|
|
* @param string $columns for example "column, MD5(column) AS column_md5" |
118
|
|
|
* @param mixed ...$params |
119
|
|
|
* @return Selection |
120
|
|
|
*/ |
121
|
4 |
|
public function select($columns, ...$params) |
122
|
|
|
{ |
123
|
4 |
|
$this->selection->select($columns, ...$params); |
124
|
4 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Adds condition for primary key |
129
|
|
|
* @param mixed $key |
130
|
|
|
* @return Selection |
131
|
|
|
*/ |
132
|
2 |
|
public function wherePrimary($key) |
133
|
|
|
{ |
134
|
2 |
|
$this->selection->wherePrimary($key); |
135
|
2 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Adds where condition, more calls appends with AND |
140
|
|
|
* @param string $condition |
141
|
|
|
* @param mixed ...$params |
142
|
|
|
* @return Selection |
143
|
|
|
*/ |
144
|
6 |
|
public function where($condition, ...$params) |
145
|
|
|
{ |
146
|
6 |
|
$this->selection->where($condition, ...$params); |
147
|
6 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Adds ON condition when joining specified table, more calls appends with AND |
152
|
|
|
* @param string $tableChain table chain or table alias for which you need additional left join condition |
153
|
|
|
* @param string $condition condition possibly containing ? |
154
|
|
|
* @param mixed ...$params |
155
|
|
|
* @return Selection |
156
|
|
|
*/ |
157
|
|
|
public function joinWhere($tableChain, $condition, ...$params) |
158
|
|
|
{ |
159
|
|
|
$this->selection->joinWhere($tableChain, $condition, ...$params); |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Adds where condition using the OR operator between parameters |
165
|
|
|
* More calls appends with AND. |
166
|
|
|
* @param array $parameters ['column1' => 1, 'column2 > ?' => 2, 'full condition'] |
167
|
|
|
* @return Selection |
168
|
|
|
* @throws InvalidArgumentException |
169
|
|
|
*/ |
170
|
2 |
|
public function whereOr(array $parameters) |
171
|
|
|
{ |
172
|
2 |
|
$this->selection->whereOr($parameters); |
173
|
2 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Adds order clause, more calls appends to the end |
178
|
|
|
* @param string $columns for example 'column1, column2 DESC' |
179
|
|
|
* @param mixed ...$params |
180
|
|
|
* @return Selection |
181
|
|
|
*/ |
182
|
4 |
|
public function order($columns, ...$params) |
183
|
|
|
{ |
184
|
4 |
|
$this->selection->order($columns, $params); |
185
|
4 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Sets limit clause, more calls rewrite old values |
190
|
|
|
* @param int $limit |
191
|
|
|
* @param int $offset |
192
|
|
|
* @return Selection |
193
|
|
|
*/ |
194
|
2 |
|
public function limit($limit, $offset = null) |
195
|
|
|
{ |
196
|
2 |
|
$this->selection->limit($limit, $offset); |
197
|
2 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Sets offset using page number, more calls rewrite old values |
202
|
|
|
* @param int $page |
203
|
|
|
* @param int $itemsPerPage |
204
|
|
|
* @param int|null $numOfPages |
205
|
|
|
* @return Selection |
206
|
|
|
*/ |
207
|
2 |
|
public function page($page, $itemsPerPage, & $numOfPages = null) |
208
|
|
|
{ |
209
|
2 |
|
$this->selection->page($page, $itemsPerPage, $numOfPages); |
210
|
2 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Sets group clause, more calls rewrite old value |
215
|
|
|
* @param string $columns |
216
|
|
|
* @param mixed ...$params |
217
|
|
|
* @return Selection |
218
|
|
|
*/ |
219
|
2 |
|
public function group($columns, ...$params) |
220
|
|
|
{ |
221
|
2 |
|
$this->selection->group($columns, ...$params); |
222
|
2 |
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Sets having clause, more calls rewrite old value |
227
|
|
|
* @param string $having |
228
|
|
|
* @param mixed ...$params |
229
|
|
|
* @return Selection |
230
|
|
|
*/ |
231
|
2 |
|
public function having($having, ...$params) |
232
|
|
|
{ |
233
|
2 |
|
$this->selection->having($having, ...$params); |
234
|
2 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Aliases table. Example ':book:book_tag.tag', 'tg' |
239
|
|
|
* @param string $tableChain |
240
|
|
|
* @param string $alias |
241
|
|
|
* @return Selection |
242
|
|
|
*/ |
243
|
|
|
public function alias($tableChain, $alias) |
244
|
|
|
{ |
245
|
|
|
$this->selection->alias($tableChain, $alias); |
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/**********************************************************************\ |
250
|
|
|
* Wrapper function - aggregations |
251
|
|
|
\**********************************************************************/ |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Executes aggregation function |
255
|
|
|
* @param string $function select call in "FUNCTION(column)" format |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
2 |
|
public function aggregation($function) |
259
|
|
|
{ |
260
|
2 |
|
return $this->selection->aggregation($function); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Counts number of rows |
265
|
|
|
* Countable interface |
266
|
|
|
* @param string $column If it is not provided returns count of result rows, otherwise runs new sql counting query |
267
|
|
|
* @return int |
268
|
|
|
*/ |
269
|
12 |
|
public function count($column = null) |
270
|
|
|
{ |
271
|
12 |
|
return $this->selection->count($column); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Returns minimum value from a column |
276
|
|
|
* @param string $column |
277
|
|
|
* @return int |
278
|
|
|
*/ |
279
|
2 |
|
public function min($column) |
280
|
|
|
{ |
281
|
2 |
|
return $this->selection->min($column); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Returns maximum value from a column |
286
|
|
|
* @param string $column |
287
|
|
|
* @return int |
288
|
|
|
*/ |
289
|
2 |
|
public function max($column) |
290
|
|
|
{ |
291
|
2 |
|
return $this->selection->max($column); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Returns sum of values in a column |
296
|
|
|
* @param string $column |
297
|
|
|
* @return int |
298
|
|
|
*/ |
299
|
2 |
|
public function sum($column) |
300
|
|
|
{ |
301
|
2 |
|
return $this->selection->sum($column); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/**********************************************************************\ |
305
|
|
|
* Wrapper function - manipulation |
306
|
|
|
\**********************************************************************/ |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Inserts row in a table |
310
|
|
|
* @param array|Traversable|Selection $data |
311
|
|
|
* @return IRow|int|bool |
312
|
|
|
*/ |
313
|
2 |
|
public function insert($data) |
314
|
|
|
{ |
315
|
2 |
|
$insertResult = $this->selection->insert($data); |
316
|
2 |
|
return $insertResult instanceof IRow ? $this->prepareRecord($insertResult) : $insertResult; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Updates all rows in result set |
321
|
|
|
* @param array|Traversable $data ($column => $value) |
322
|
|
|
* @return int |
323
|
|
|
*/ |
324
|
2 |
|
public function update($data) |
325
|
|
|
{ |
326
|
2 |
|
return $this->selection->update($data); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Deletes all rows in result set |
331
|
|
|
* @return int |
332
|
|
|
*/ |
333
|
2 |
|
public function delete() |
334
|
|
|
{ |
335
|
2 |
|
return $this->selection->delete(); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/**********************************************************************\ |
339
|
|
|
* Iterator interface |
340
|
|
|
\**********************************************************************/ |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Rewind selection |
344
|
|
|
*/ |
345
|
12 |
|
public function rewind() |
346
|
|
|
{ |
347
|
12 |
|
$this->selection->rewind(); |
348
|
12 |
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Returns current selection data record |
352
|
|
|
* @return bool|mixed |
353
|
|
|
*/ |
354
|
12 |
|
public function current() |
355
|
|
|
{ |
356
|
12 |
|
$row = $this->selection->current(); |
357
|
12 |
|
return $row instanceof IRow ? $this->prepareRecord($row) : false; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Returns current selection data key |
362
|
|
|
* @return string |
363
|
|
|
*/ |
364
|
|
|
public function key() |
365
|
|
|
{ |
366
|
|
|
return $this->selection->key(); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Move iterator |
371
|
|
|
*/ |
372
|
12 |
|
public function next() |
373
|
|
|
{ |
374
|
12 |
|
$this->selection->next(); |
375
|
12 |
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* It is selection valid |
379
|
|
|
* @return bool |
380
|
|
|
*/ |
381
|
12 |
|
public function valid() |
382
|
|
|
{ |
383
|
12 |
|
return $this->selection->valid(); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/**********************************************************************\ |
387
|
|
|
* ArrayAccess interface |
388
|
|
|
\**********************************************************************/ |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @param string $key Row ID |
392
|
|
|
* @param IRow $value |
393
|
|
|
*/ |
394
|
2 |
|
public function offsetSet($key, $value) |
395
|
|
|
{ |
396
|
2 |
|
$this->selection->offsetSet($key, $value); |
397
|
2 |
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Returns specified row |
401
|
|
|
* @param string $key Row ID |
402
|
|
|
* @return IRow|null |
403
|
|
|
*/ |
404
|
2 |
|
public function offsetGet($key) |
405
|
|
|
{ |
406
|
2 |
|
$row = $this->selection->offsetGet($key); |
407
|
2 |
|
return $row instanceof IRow ? $this->prepareRecord($row) : $row; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Tests if row exists |
412
|
|
|
* @param string $key Row ID |
413
|
|
|
* @return bool |
414
|
|
|
*/ |
415
|
2 |
|
public function offsetExists($key) |
416
|
|
|
{ |
417
|
2 |
|
return $this->selection->offsetExists($key); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Removes row from result set |
422
|
|
|
* @param string $key Row ID |
423
|
|
|
*/ |
424
|
2 |
|
public function offsetUnset($key) |
425
|
|
|
{ |
426
|
2 |
|
$this->selection->offsetUnset($key); |
427
|
2 |
|
} |
428
|
|
|
|
429
|
|
|
/**********************************************************************\ |
430
|
|
|
* Build methods |
431
|
|
|
\**********************************************************************/ |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Prepare one record |
435
|
|
|
* @param IRow $row |
436
|
|
|
* @return mixed |
437
|
|
|
*/ |
438
|
26 |
|
protected function prepareRecord(IRow $row) |
439
|
|
|
{ |
440
|
26 |
|
$recordClass = $this->structure->getActiveRowClass($row->getTable()->getName()); |
441
|
26 |
|
return new $recordClass($row, $this->structure); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Prepare records array |
446
|
|
|
* @param array $rows |
447
|
|
|
* @return array |
448
|
|
|
*/ |
449
|
4 |
|
protected function prepareRecords(array $rows) |
450
|
|
|
{ |
451
|
4 |
|
$result = []; |
452
|
4 |
|
foreach ($rows as $row) { |
453
|
4 |
|
$result[] = $this->prepareRecord($row); |
454
|
4 |
|
} |
455
|
4 |
|
return $result; |
456
|
|
|
} |
457
|
|
|
} |
458
|
|
|
|