-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachedConditionGenerator.php
102 lines (81 loc) · 3.35 KB
/
CachedConditionGenerator.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
<?php
declare(strict_types=1);
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search\Doctrine\Dbal;
use Doctrine\DBAL\Query\QueryBuilder;
use Psr\SimpleCache\CacheInterface as Cache;
use Rollerworks\Component\Search\Doctrine\Dbal\Query\QueryGenerator;
use Rollerworks\Component\Search\SearchCondition;
/**
* Handles caching of a Doctrine DBAL ConditionGenerator.
*
* Instead of using the ConditionGenerator directly you use the
* CachedConditionGenerator as all related calls are delegated.
*
* The cache-key is a hashed (sha256) combination of the SearchCondition
* (root ValuesGroup and FieldSet name) and configured field mappings.
*
* Caution: Any noticeable changes to your (FieldSet's) configuration
* should purge all cached entries.
*/
final class CachedConditionGenerator extends AbstractCachedConditionGenerator implements ConditionGenerator
{
private QueryBuilder $qb;
private FieldConfigurationSet $fieldsConfig;
/**
* @param mixed|null $ttl
*/
public function __construct(QueryBuilder $queryBuilder, SearchCondition $searchCondition, Cache $cacheDriver, $ttl = null)
{
parent::__construct($cacheDriver, $searchCondition, $ttl);
$this->qb = $queryBuilder;
$this->fieldsConfig = new FieldConfigurationSet($searchCondition->getFieldSet());
}
public function setField(string $fieldName, string $column, ?string $alias = null, string $type = 'string')
{
$this->fieldsConfig->setField($fieldName, $column, $alias, $type);
return $this;
}
public function apply(): void
{
if ($this->isApplied) {
trigger_error('SearchCondition was already applied. Ignoring operation.', \E_USER_WARNING);
return;
}
$this->isApplied = true;
$fields = $this->fieldsConfig->fields;
$cacheKey = $this->getCacheKey($fields);
$cached = $this->getFromCache($cacheKey);
// Note that ordering is not part of the cache as this only applies at the root level
// And is handled by QueryBuilder itself, making it possible to reuse the same condition
// with a different ordering.
if ($cached !== null) {
[$whereClause, $parameters] = $cached;
} else {
$connection = $this->qb->getConnection();
$generator = new QueryGenerator($connection, SqlConditionGenerator::getQueryPlatform($connection), $fields);
$whereClause = $generator->getWhereClause($this->searchCondition);
$parameters = $generator->getParameters();
$this->storeInCache($whereClause, $cacheKey, $parameters);
}
QueryGenerator::applySortingTo($this->searchCondition->getPrimaryCondition()?->getOrder(), $this->qb, $fields);
QueryGenerator::applySortingTo($this->searchCondition->getOrder(), $this->qb, $fields);
if ($whereClause !== '') {
$this->qb->andWhere($whereClause);
foreach ($parameters as $name => [$value, $type]) {
$this->qb->setParameter($name, $value, $type);
}
}
}
public function getQueryBuilder(): QueryBuilder
{
return $this->qb;
}
}