summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/orm/src/Compat/FilterProcessor.php
blob: 7956898485ff97abc367b622c51e71757ccc5f38 (plain)
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
<?php

namespace ipl\Orm\Compat;

use AppendIterator;
use ArrayIterator;
use ipl\Orm\Exception\InvalidColumnException;
use ipl\Orm\Exception\ValueConversionException;
use ipl\Orm\Query;
use ipl\Orm\Relation;
use ipl\Orm\UnionQuery;
use ipl\Sql\Filter\Exists;
use ipl\Sql\Filter\In;
use ipl\Sql\Filter\NotExists;
use ipl\Sql\Filter\NotIn;
use ipl\Stdlib\Contract\Filterable;
use ipl\Stdlib\Filter\MetaDataProvider;
use ipl\Stdlib\Filter;

class FilterProcessor extends \ipl\Sql\Compat\FilterProcessor
{
    protected $baseJoins = [];

    protected $madeJoins = [];

    /**
     * Require and resolve the filter rule and apply it on the query
     *
     * Note that this applies the filter to {@see Query::$selectBase}
     * directly and bypasses {@see Query::$filter}. If this is not
     * desired, utilize the {@see Filterable} functions of the query.
     *
     * @param Filter\Rule $filter
     * @param Query $query
     */
    public static function apply(Filter\Rule $filter, Query $query)
    {
        if ($query instanceof UnionQuery) {
            foreach ($query->getUnions() as $union) {
                static::apply($filter, $union);
            }

            return;
        }

        if (! $filter instanceof Filter\Chain || ! $filter->isEmpty()) {
            $filter = clone $filter;
            if (! $filter instanceof Filter\Chain) {
                $filter = Filter::all($filter);
            }

            static::resolveFilter($filter, $query);

            $where = static::assembleFilter($filter);

            if ($where) {
                $operator = array_shift($where);
                $conditions = array_shift($where);

                $query->getSelectBase()->where($conditions, $operator);
            }
        }
    }

    /**
     * Resolve the filter in order to apply it on the query
     *
     * @param Filter\Chain $filter
     * @param Query $query
     *
     * @return void
     */
    public static function resolveFilter(Filter\Chain $filter, Query $query)
    {
        $processor = new static();
        foreach ($query->getUtilize() as $path => $_) {
            $processor->baseJoins[$path] = true;
        }

        $processor->requireAndResolveFilterColumns($filter, $query);
    }

    protected function requireAndResolveFilterColumns(Filter\Rule $filter, Query $query, $forceOptimization = null)
    {
        if ($filter instanceof Filter\Condition) {
            if (
                $filter instanceof Exists
                || $filter instanceof NotExists
                || $filter instanceof In
                || $filter instanceof NotIn
            ) {
                return;
            }

            $resolver = $query->getResolver();
            $baseTable = $query->getModel()->getTableAlias();
            $column = $resolver->qualifyPath(
                $filter->metaData()->get('columnName', $filter->getColumn()),
                $baseTable
            );

            $filter->metaData()->set('columnPath', $column);

            list($relationPath, $columnName) = preg_split('/\.(?=[^.]+$)/', $column);

            $subject = null;
            $relations = new AppendIterator();
            $relations->append(new ArrayIterator([$baseTable => null]));
            $relations->append($resolver->resolveRelations($relationPath));
            $behaviorsApplied = $filter->metaData()->get('behaviorsApplied', false);
            foreach ($relations as $path => $relation) {
                $columnName = substr($column, strlen($path) + 1);

                if ($path === $baseTable) {
                    $subject = $query->getModel();
                } else {
                    /** @var Relation $relation */
                    $subject = $relation->getTarget();
                }

                $subjectBehaviors = $resolver->getBehaviors($subject);
                // This is only used within the Binary behavior in rewriteCondition().
                $filter->metaData()->set('originalValue', $filter->getValue());

                if (! $behaviorsApplied) {
                    try {
                        // Prepare filter as if it were final to allow full control for rewrite filter behaviors
                        $filter->setValue($subjectBehaviors->persistProperty($filter->getValue(), $columnName));
                    } catch (ValueConversionException $_) {
                        // The search bar may submit values with wildcards or whatever the user has entered.
                        // In this case, we can simply ignore this error instead of rendering a stack trace.
                    }
                }

                $filter->setColumn($resolver->getAlias($subject) . '.' . $columnName);
                $filter->metaData()->set('columnName', $columnName);
                $filter->metaData()->set('relationPath', $path);

                if (! $behaviorsApplied) {
                    $rewrittenFilter = $subjectBehaviors->rewriteCondition($filter, $path . '.');
                    if ($rewrittenFilter !== null) {
                        return $this->requireAndResolveFilterColumns($rewrittenFilter, $query, $forceOptimization)
                            ?: $rewrittenFilter;
                    }
                }
            }

            /**
             * We have applied all the subject behaviors for this filter condition, so set this metadata to prevent
             * the behaviors from being applied for the same filter condition over again later in case of a subquery.
             * The behaviors are processed again due to $subQueryFilter being evaluated by this processor as part of
             * the subquery. The reason for this is the application of aliases used in said subquery. Since this is
             * part of the filter column qualification, and the behaviors are not, this should be separately done.
             * There's a similar comment in {@see Query::createSubQuery()} which should be considered when working
             * on improving this.
             */
            $filter->metaData()->set('behaviorsApplied', true);

            if (! $resolver->hasSelectableColumn($subject, $columnName)) {
                throw new InvalidColumnException($columnName, $subject);
            }

            if ($relationPath !== $baseTable) {
                $query->utilize($relationPath);
                $this->madeJoins[$relationPath][] = $filter;
            }
        } else {
            /** @var Filter\Chain $filter */

            if ($filter->metaData()->has('forceOptimization')) {
                // Rules can override the default behavior how it's determined that they need to be
                // optimized. If it's done by a chain, it applies to all of its children.
                $forceOptimization = $filter->metaData()->get('forceOptimization');
            }

            $subQueryGroups = [];
            /** @var Filter\Rule[] $outsourcedRules */
            $outsourcedRules = [];
            foreach ($filter as $child) {
                /** @var Filter\Rule $child */
                $rewrittenFilter = $this->requireAndResolveFilterColumns($child, $query, $forceOptimization);
                if ($rewrittenFilter !== null) {
                    $filter->replace($child, $rewrittenFilter);
                    $child = $rewrittenFilter;
                }

                $optimizeChild = $forceOptimization;
                if ($child instanceof MetaDataProvider && $child->metaData()->has('forceOptimization')) {
                    $optimizeChild = $child->metaData()->get('forceOptimization');
                }

                // We only optimize rules in a single level, nested chains are ignored
                if ($child instanceof Filter\Condition && $child->metaData()->has('relationPath')) {
                    $relationPath = $child->metaData()->get('relationPath');
                    if (
                        $relationPath !== $query->getModel()->getTableAlias() // Not the base table
                        && (
                            $optimizeChild !== null && $optimizeChild
                            || (
                                $optimizeChild === null
                                && ! isset($query->getWith()[$relationPath]) // Not a selected join
                                && ! $query->getResolver()->isDistinctRelation($relationPath) // Not a to-one relation
                            )
                        )
                    ) {
                        $subQueryGroups[$relationPath][$child->getColumn()][get_class($child)][] = $child;

                        // Register all rules that are going to be put into sub queries, for later cleanup
                        $outsourcedRules[] = $child;
                    }
                }
            }

            foreach ($subQueryGroups as $relationPath => $columns) {
                $generalRules = [];
                foreach ($columns as $column => $comparisons) {
                    if (isset($comparisons[Filter\Unequal::class]) || isset($comparisons[Filter\Unlike::class])) {
                        // If there's a unequal (!=) comparison for any column, all other comparisons (for the same
                        // column) also need to be outsourced to their own sub query. Regardless of their amount of
                        // occurrence. This is because `$generalRules` apply to all comparisons of such a column and
                        // need to be applied to all sub queries.
                        continue;
                    }

                    // Single occurring columns don't need their own sub query
                    foreach ($comparisons as $conditionClass => $rules) {
                        if (count($rules) === 1) {
                            $generalRules[] = $rules[0];
                            unset($columns[$column][$conditionClass]);
                        }
                    }

                    if (empty($columns[$column])) {
                        unset($columns[$column]);
                    }
                }

                $count = null;
                $baseFilters = null;
                $subQueryFilters = [];
                foreach ($columns as $column => $comparisons) {
                    foreach ($comparisons as $conditionClass => $rules) {
                        if ($conditionClass === Filter\Unequal::class || $conditionClass === Filter\Unlike::class) {
                            // Unequal comparisons are always put into their own sub query
                            $subQueryFilters[] = [$rules, count($rules), true];
                        } elseif (count($rules) > $count) {
                            // If there are multiple columns used multiple times in the same relation, we have to decide
                            // which to use as the primary comparison. That is the column that is used most often.
                            if (! empty($baseFilters)) {
                                array_push($generalRules, ...$baseFilters);
                            }

                            $count = count($rules);
                            $baseFilters = $rules;
                        } else {
                            array_push($generalRules, ...$rules);
                        }
                    }
                }

                if (! empty($baseFilters) || ! empty($generalRules)) {
                    $subQueryFilters[] = [$baseFilters ?: $generalRules, $count, false];
                }

                foreach ($subQueryFilters as list($filters, $count, $negate)) {
                    $subQueryFilter = null;
                    if ($count !== null) {
                        $aggregateFilter = Filter::any();
                        foreach ($filters as $condition) {
                            if ($negate) {
                                if ($condition instanceof Filter\Unequal) {
                                    $negation = Filter::equal($condition->getColumn(), $condition->getValue());
                                } else { // if ($condition instanceof Filter\Unlike)
                                    $negation = Filter::like($condition->getColumn(), $condition->getValue());
                                }

                                $negation->metaData()->merge($condition->metaData());
                                $condition = $negation;
                                $count = 1;
                            }

                            switch (true) {
                                case $filter instanceof Filter\All:
                                    $aggregateFilter->add(Filter::all($condition, ...$generalRules));
                                    break;
                                case $filter instanceof Filter\Any:
                                    $aggregateFilter->add(Filter::any($condition, ...$generalRules));
                                    break;
                                case $filter instanceof Filter\None:
                                    $aggregateFilter->add(Filter::none($condition, ...$generalRules));
                                    break;
                            }
                        }

                        $subQueryFilter = $aggregateFilter;
                    } else {
                        switch (true) {
                            case $filter instanceof Filter\All:
                                $subQueryFilter = Filter::all(...$filters);
                                break;
                            case $filter instanceof Filter\Any:
                                $subQueryFilter = Filter::any(...$filters);
                                break;
                            case $filter instanceof Filter\None:
                                $subQueryFilter = Filter::none(...$filters);
                                break;
                        }
                    }

                    $relation = $query->getResolver()->resolveRelation($relationPath);
                    $subQuery = $query->createSubQuery($relation->getTarget(), $relationPath, null, false);
                    $subQuery->getResolver()->setAliasPrefix('sub_');

                    $subQuery->filter($subQueryFilter);

                    $subQuerySelect = $subQuery->assembleSelect()->resetOrderBy();

                    if ($count !== null && ($negate || $filter instanceof Filter\All)) {
                        $targetKeys = join(
                            ',',
                            array_values(
                                $subQuery->getResolver()->qualifyColumns(
                                    (array) $subQuery->getModel()->getKeyName(),
                                    $subQuery->getModel()
                                )
                            )
                        );

                        $subQuerySelect->having(["COUNT(DISTINCT $targetKeys) >= ?" => $count]);
                        $subQuerySelect->groupBy(array_values($subQuerySelect->getColumns()));
                    }

                    // TODO: Qualification is only necessary since the `In` and `NotIn` conditions are ignored by
                    //       requireAndResolveFilterColumns(). In case it supports not only single columns but also
                    //       multiple, this might be reduced to: $keyTuple = (array) $query->getModel()->getKeyName()
                    $keyTuple = array_values(
                        $query->getResolver()->qualifyColumns(
                            (array) $query->getModel()->getKeyName(),
                            $query->getModel()
                        )
                    );

                    if ($negate) {
                        $filter->add(new NotIn($keyTuple, $subQuerySelect));
                    } else {
                        $filter->add(new In($keyTuple, $subQuerySelect));
                    }
                }
            }

            foreach ($outsourcedRules as $rule) {
                // Remove joins solely used for filter conditions
                foreach ($this->madeJoins as $joinPath => & $madeBy) {
                    $madeBy = array_filter(
                        $madeBy,
                        function ($relationFilter) use ($rule) {
                            return $rule !== $relationFilter
                                && (! $rule instanceof Filter\Chain || ! $rule->has($relationFilter));
                        }
                    );

                    if (empty($madeBy)) {
                        if (! isset($this->baseJoins[$joinPath])) {
                            $query->omit($joinPath);
                        }

                        unset($this->madeJoins[$joinPath]);
                    }
                }

                $filter->remove($rule);
            }
        }
    }
}