/
SelectLoader.php
545 lines (476 loc) · 17.3 KB
/
SelectLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.4.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM\Association\Loader;
use Cake\Database\Expression\IdentifierExpression;
use Cake\Database\Expression\TupleComparison;
use Cake\Database\ValueBinder;
use Cake\ORM\Association;
use InvalidArgumentException;
use RuntimeException;
/**
* Implements the logic for loading an association using a SELECT query
*
* @internal
*/
class SelectLoader
{
/**
* The alias of the association loading the results
*
* @var string
*/
protected $alias;
/**
* The alias of the source association
*
* @var string
*/
protected $sourceAlias;
/**
* The alias of the target association
*
* @var string
*/
protected $targetAlias;
/**
* The foreignKey to the target association
*
* @var string|array
*/
protected $foreignKey;
/**
* The strategy to use for loading, either select or subquery
*
* @var string
*/
protected $strategy;
/**
* The binding key for the source association.
*
* @var string
*/
protected $bindingKey;
/**
* A callable that will return a query object used for loading the association results
*
* @var callable
*/
protected $finder;
/**
* The type of the association triggering the load
*
* @var string
*/
protected $associationType;
/**
* The sorting options for loading the association
*
* @var string
*/
protected $sort;
/**
* Copies the options array to properties in this class. The keys in the array correspond
* to properties in this class.
*
* @param array $options Properties to be copied to this class
*/
public function __construct(array $options)
{
$this->alias = $options['alias'];
$this->sourceAlias = $options['sourceAlias'];
$this->targetAlias = $options['targetAlias'];
$this->foreignKey = $options['foreignKey'];
$this->strategy = $options['strategy'];
$this->bindingKey = $options['bindingKey'];
$this->finder = $options['finder'];
$this->associationType = $options['associationType'];
$this->sort = isset($options['sort']) ? $options['sort'] : null;
}
/**
* Returns a callable that can be used for injecting association results into a given
* iterator. The options accepted by this method are the same as `Association::eagerLoader()`
*
* @param array $options Same options as `Association::eagerLoader()`
* @return callable
*/
public function buildEagerLoader(array $options)
{
$options += $this->_defaultOptions();
$fetchQuery = $this->_buildQuery($options);
$resultMap = $this->_buildResultMap($fetchQuery, $options);
return $this->_resultInjector($fetchQuery, $resultMap, $options);
}
/**
* Returns the default options to use for the eagerLoader
*
* @return array
*/
protected function _defaultOptions()
{
return [
'foreignKey' => $this->foreignKey,
'conditions' => [],
'strategy' => $this->strategy,
'nestKey' => $this->alias,
'sort' => $this->sort
];
}
/**
* Auxiliary function to construct a new Query object to return all the records
* in the target table that are associated to those specified in $options from
* the source table
*
* @param array $options options accepted by eagerLoader()
* @return \Cake\ORM\Query
* @throws \InvalidArgumentException When a key is required for associations but not selected.
*/
protected function _buildQuery($options)
{
$key = $this->_linkField($options);
$filter = $options['keys'];
$useSubquery = $options['strategy'] === Association::STRATEGY_SUBQUERY;
$finder = $this->finder;
if (!isset($options['fields'])) {
$options['fields'] = [];
}
$query = $finder();
if (isset($options['finder'])) {
list($finderName, $opts) = $this->_extractFinder($options['finder']);
$query = $query->find($finderName, $opts);
}
$fetchQuery = $query
->select($options['fields'])
->where($options['conditions'])
->eagerLoaded(true)
->enableHydration($options['query']->isHydrationEnabled());
if ($useSubquery) {
$filter = $this->_buildSubquery($options['query']);
$fetchQuery = $this->_addFilteringJoin($fetchQuery, $key, $filter);
} else {
$fetchQuery = $this->_addFilteringCondition($fetchQuery, $key, $filter);
}
if (!empty($options['sort'])) {
$fetchQuery->order($options['sort']);
}
if (!empty($options['contain'])) {
$fetchQuery->contain($options['contain']);
}
if (!empty($options['queryBuilder'])) {
$fetchQuery = $options['queryBuilder']($fetchQuery);
}
$this->_assertFieldsPresent($fetchQuery, (array)$key);
return $fetchQuery;
}
/**
* Helper method to infer the requested finder and its options.
*
* Returns the inferred options from the finder $type.
*
* ### Examples:
*
* The following will call the finder 'translations' with the value of the finder as its options:
* $query->contain(['Comments' => ['finder' => ['translations']]]);
* $query->contain(['Comments' => ['finder' => ['translations' => []]]]);
* $query->contain(['Comments' => ['finder' => ['translations' => ['locales' => ['en_US']]]]]);
*
* @param string|array $finderData The finder name or an array having the name as key
* and options as value.
* @return array
*/
protected function _extractFinder($finderData)
{
$finderData = (array)$finderData;
if (is_numeric(key($finderData))) {
return [current($finderData), []];
}
return [key($finderData), current($finderData)];
}
/**
* Checks that the fetching query either has auto fields on or
* has the foreignKey fields selected.
* If the required fields are missing, throws an exception.
*
* @param \Cake\ORM\Query $fetchQuery The association fetching query
* @param array $key The foreign key fields to check
* @return void
* @throws InvalidArgumentException
*/
protected function _assertFieldsPresent($fetchQuery, $key)
{
$select = $fetchQuery->aliasFields($fetchQuery->clause('select'));
if (empty($select)) {
return;
}
$missingKey = function ($fieldList, $key) {
foreach ($key as $keyField) {
if (!in_array($keyField, $fieldList, true)) {
return true;
}
}
return false;
};
$missingFields = $missingKey($select, $key);
if ($missingFields) {
$driver = $fetchQuery->getConnection()->driver();
$quoted = array_map([$driver, 'quoteIdentifier'], $key);
$missingFields = $missingKey($select, $quoted);
}
if ($missingFields) {
throw new InvalidArgumentException(
sprintf(
'You are required to select the "%s" field(s)',
implode(', ', (array)$key)
)
);
}
}
/**
* Appends any conditions required to load the relevant set of records in the
* target table query given a filter key and some filtering values when the
* filtering needs to be done using a subquery.
*
* @param \Cake\ORM\Query $query Target table's query
* @param string $key the fields that should be used for filtering
* @param \Cake\ORM\Query $subquery The Subquery to use for filtering
* @return \Cake\ORM\Query
*/
protected function _addFilteringJoin($query, $key, $subquery)
{
$filter = [];
$aliasedTable = $this->sourceAlias;
foreach ($subquery->clause('select') as $aliasedField => $field) {
if (is_int($aliasedField)) {
$filter[] = new IdentifierExpression($field);
} else {
$filter[$aliasedField] = $field;
}
}
$subquery->select($filter, true);
if (is_array($key)) {
$conditions = $this->_createTupleCondition($query, $key, $filter, '=');
} else {
$filter = current($filter);
}
$conditions = isset($conditions) ? $conditions : $query->newExpr([$key => $filter]);
return $query->innerJoin(
[$aliasedTable => $subquery],
$conditions
);
}
/**
* Appends any conditions required to load the relevant set of records in the
* target table query given a filter key and some filtering values.
*
* @param \Cake\ORM\Query $query Target table's query
* @param string|array $key the fields that should be used for filtering
* @param mixed $filter the value that should be used to match for $key
* @return \Cake\ORM\Query
*/
protected function _addFilteringCondition($query, $key, $filter)
{
if (is_array($key)) {
$conditions = $this->_createTupleCondition($query, $key, $filter, 'IN');
}
$conditions = isset($conditions) ? $conditions : [$key . ' IN' => $filter];
return $query->andWhere($conditions);
}
/**
* Returns a TupleComparison object that can be used for matching all the fields
* from $keys with the tuple values in $filter using the provided operator.
*
* @param \Cake\ORM\Query $query Target table's query
* @param array $keys the fields that should be used for filtering
* @param mixed $filter the value that should be used to match for $key
* @param string $operator The operator for comparing the tuples
* @return \Cake\Database\Expression\TupleComparison
*/
protected function _createTupleCondition($query, $keys, $filter, $operator)
{
$types = [];
$defaults = $query->getDefaultTypes();
foreach ($keys as $k) {
if (isset($defaults[$k])) {
$types[] = $defaults[$k];
}
}
return new TupleComparison($keys, $filter, $types, $operator);
}
/**
* Generates a string used as a table field that contains the values upon
* which the filter should be applied
*
* @param array $options The options for getting the link field.
* @return string|array
*/
protected function _linkField($options)
{
$links = [];
$name = $this->alias;
if ($options['foreignKey'] === false && $this->associationType === Association::ONE_TO_MANY) {
$msg = 'Cannot have foreignKey = false for hasMany associations. ' .
'You must provide a foreignKey column.';
throw new RuntimeException($msg);
}
$keys = in_array($this->associationType, [Association::ONE_TO_ONE, Association::ONE_TO_MANY]) ?
$this->foreignKey :
$this->bindingKey;
foreach ((array)$keys as $key) {
$links[] = sprintf('%s.%s', $name, $key);
}
if (count($links) === 1) {
return $links[0];
}
return $links;
}
/**
* Builds a query to be used as a condition for filtering records in the
* target table, it is constructed by cloning the original query that was used
* to load records in the source table.
*
* @param \Cake\ORM\Query $query the original query used to load source records
* @return \Cake\ORM\Query
*/
protected function _buildSubquery($query)
{
$filterQuery = clone $query;
$filterQuery->enableAutoFields(false);
$filterQuery->mapReduce(null, null, true);
$filterQuery->formatResults(null, true);
$filterQuery->contain([], true);
$filterQuery->valueBinder(new ValueBinder());
if (!$filterQuery->clause('limit')) {
$filterQuery->limit(null);
$filterQuery->order([], true);
$filterQuery->offset(null);
}
$fields = $this->_subqueryFields($query);
$filterQuery->select($fields['select'], true)->group($fields['group']);
return $filterQuery;
}
/**
* Calculate the fields that need to participate in a subquery.
*
* Normally this includes the binding key columns. If there is a an ORDER BY,
* those columns are also included as the fields may be calculated or constant values,
* that need to be present to ensure the correct association data is loaded.
*
* @param \Cake\ORM\Query $query The query to get fields from.
* @return array The list of fields for the subquery.
*/
protected function _subqueryFields($query)
{
$keys = (array)$this->bindingKey;
if ($this->associationType === Association::MANY_TO_ONE) {
$keys = (array)$this->foreignKey;
}
$fields = $query->aliasFields($keys, $this->sourceAlias);
$group = $fields = array_values($fields);
$order = $query->clause('order');
if ($order) {
$columns = $query->clause('select');
$order->iterateParts(function ($direction, $field) use (&$fields, $columns) {
if (isset($columns[$field])) {
$fields[$field] = $columns[$field];
}
});
}
return ['select' => $fields, 'group' => $group];
}
/**
* Builds an array containing the results from fetchQuery indexed by
* the foreignKey value corresponding to this association.
*
* @param \Cake\ORM\Query $fetchQuery The query to get results from
* @param array $options The options passed to the eager loader
* @return array
*/
protected function _buildResultMap($fetchQuery, $options)
{
$resultMap = [];
$singleResult = in_array($this->associationType, [Association::MANY_TO_ONE, Association::ONE_TO_ONE]);
$keys = in_array($this->associationType, [Association::ONE_TO_ONE, Association::ONE_TO_MANY]) ?
$this->foreignKey :
$this->bindingKey;
$key = (array)$keys;
foreach ($fetchQuery->all() as $result) {
$values = [];
foreach ($key as $k) {
$values[] = $result[$k];
}
if ($singleResult) {
$resultMap[implode(';', $values)] = $result;
} else {
$resultMap[implode(';', $values)][] = $result;
}
}
return $resultMap;
}
/**
* Returns a callable to be used for each row in a query result set
* for injecting the eager loaded rows
*
* @param \Cake\ORM\Query $fetchQuery the Query used to fetch results
* @param array $resultMap an array with the foreignKey as keys and
* the corresponding target table results as value.
* @param array $options The options passed to the eagerLoader method
* @return \Closure
*/
protected function _resultInjector($fetchQuery, $resultMap, $options)
{
$keys = $this->associationType === Association::MANY_TO_ONE ?
$this->foreignKey :
$this->bindingKey;
$sourceKeys = [];
foreach ((array)$keys as $key) {
$f = $fetchQuery->aliasField($key, $this->sourceAlias);
$sourceKeys[] = key($f);
}
$nestKey = $options['nestKey'];
if (count($sourceKeys) > 1) {
return $this->_multiKeysInjector($resultMap, $sourceKeys, $nestKey);
}
$sourceKey = $sourceKeys[0];
return function ($row) use ($resultMap, $sourceKey, $nestKey) {
if (isset($row[$sourceKey], $resultMap[$row[$sourceKey]])) {
$row[$nestKey] = $resultMap[$row[$sourceKey]];
}
return $row;
};
}
/**
* Returns a callable to be used for each row in a query result set
* for injecting the eager loaded rows when the matching needs to
* be done with multiple foreign keys
*
* @param array $resultMap A keyed arrays containing the target table
* @param array $sourceKeys An array with aliased keys to match
* @param string $nestKey The key under which results should be nested
* @return \Closure
*/
protected function _multiKeysInjector($resultMap, $sourceKeys, $nestKey)
{
return function ($row) use ($resultMap, $sourceKeys, $nestKey) {
$values = [];
foreach ($sourceKeys as $key) {
$values[] = $row[$key];
}
$key = implode(';', $values);
if (isset($resultMap[$key])) {
$row[$nestKey] = $resultMap[$key];
}
return $row;
};
}
}