-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathAbstractQueryBuilder.php
More file actions
256 lines (215 loc) · 8.11 KB
/
AbstractQueryBuilder.php
File metadata and controls
256 lines (215 loc) · 8.11 KB
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
<?php
declare(strict_types=1);
namespace Flowpack\SimpleSearch\ContentRepositoryAdaptor\Search;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Search\Search\QueryBuilderInterface;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\Exception\IllegalObjectTypeException;
use Psr\Log\LoggerInterface;
/**
* Abstract Query Builder for Content Repository searches
*/
abstract class AbstractQueryBuilder implements QueryBuilderInterface, ProtectedContextAwareInterface
{
/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;
/**
* The node inside which searching should happen
*
* @var NodeInterface
*/
protected $contextNode;
/**
* Optional message for a log entry on execution of this Query.
*
* @var string
*/
protected $logMessage;
/**
* Should this Query be logged?
*
* @var boolean
*/
protected $queryLogEnabled = false;
abstract protected function getSimpleSearchQueryBuilder(): \Flowpack\SimpleSearch\Search\QueryBuilderInterface;
public function sortDesc(string $propertyName): QueryBuilderInterface
{
$this->getSimpleSearchQueryBuilder()->sortDesc($propertyName);
return $this;
}
public function sortAsc(string $propertyName): QueryBuilderInterface
{
$this->getSimpleSearchQueryBuilder()->sortAsc($propertyName);
return $this;
}
public function limit($limit): QueryBuilderInterface
{
$this->getSimpleSearchQueryBuilder()->limit($limit);
return $this;
}
public function from($from): QueryBuilderInterface
{
$this->getSimpleSearchQueryBuilder()->from($from);
return $this;
}
public function fulltext(string $searchWord, array $options = []): QueryBuilderInterface
{
$this->getSimpleSearchQueryBuilder()->fulltext($searchWord);
return $this;
}
/**
* @return MysqlQueryBuilder
* @throws IllegalObjectTypeException
*/
public function query(NodeInterface $contextNode): QueryBuilderInterface
{
$this->getSimpleSearchQueryBuilder()->customCondition("(__parentPath LIKE '%#" . $contextNode->findNodePath() . "#%' OR __path LIKE '" . $contextNode->findNodePath() . "')");
$this->getSimpleSearchQueryBuilder()->like('__path', (string) $contextNode->findNodePath());
$this->getSimpleSearchQueryBuilder()->like('__workspace', "#" . $contextNode->getContext()->getWorkspace()->getName() . "#");
$this->getSimpleSearchQueryBuilder()->like('__dimensionshash', "#" . md5(json_encode($contextNode->getContext()->getDimensions())) . "#");
$this->contextNode = $contextNode;
return $this;
}
abstract public function getFindIdentifiersByNodeIdentifierQuery(string $nodeIdentifierPlaceholder): string;
/**
* HIGH-LEVEL API
*/
/**
* Filter by node type, taking inheritance into account.
*/
public function nodeType(string $nodeType): QueryBuilderInterface
{
$this->getSimpleSearchQueryBuilder()->like('__typeAndSuperTypes', "#" . $nodeType . "#");
return $this;
}
/**
* add an exact-match query for a given property
*/
public function exactMatch(string $propertyName, $propertyValue): QueryBuilderInterface
{
if ($propertyValue instanceof NodeInterface) {
$propertyValue = (string) $propertyValue->getNodeAggregateIdentifier();
}
$this->getSimpleSearchQueryBuilder()->exactMatch($propertyName, $propertyValue);
return $this;
}
/**
* add an like query for a given property
*/
public function like(string $propertyName, $propertyValue): QueryBuilderInterface
{
if ($propertyValue instanceof NodeInterface) {
$propertyValue = (string) $propertyValue->getNodeAggregateIdentifier();
}
$this->getSimpleSearchQueryBuilder()->like($propertyName, $propertyValue);
return $this;
}
/**
* add a greater than query for a given property
*/
public function greaterThan($propertyName, $propertyValue): QueryBuilderInterface
{
if ($propertyValue instanceof NodeInterface) {
$propertyValue = (string) $propertyValue->getNodeAggregateIdentifier();
}
$this->getSimpleSearchQueryBuilder()->greaterThan($propertyName, $propertyValue);
return $this;
}
/**
* add a greater than or equal query for a given property
*/
public function greaterThanOrEqual(string $propertyName, $propertyValue): QueryBuilderInterface
{
if ($propertyValue instanceof NodeInterface) {
$propertyValue = (string) $propertyValue->getNodeAggregateIdentifier();
}
$this->getSimpleSearchQueryBuilder()->greaterThanOrEqual($propertyName, $propertyValue);
return $this;
}
/**
* add a less than query for a given property
*/
public function lessThan(string $propertyName, $propertyValue): QueryBuilderInterface
{
if ($propertyValue instanceof NodeInterface) {
$propertyValue = (string) $propertyValue->getNodeAggregateIdentifier();
}
$this->getSimpleSearchQueryBuilder()->lessThan($propertyName, $propertyValue);
return $this;
}
/**
* add a less than query for a given property
*/
public function lessThanOrEqual(string $propertyName, $propertyValue): QueryBuilderInterface
{
if ($propertyValue instanceof NodeInterface) {
$propertyValue = (string) $propertyValue->getNodeAggregateIdentifier();
}
$this->getSimpleSearchQueryBuilder()->lessThanOrEqual($propertyName, $propertyValue);
return $this;
}
/**
* Execute the query and return the list of nodes as result
*
* @return array<\Neos\ContentRepository\Domain\Model\NodeInterface>
*/
public function execute(): \Traversable
{
// Adding implicit sorting by __sortIndex (as last fallback) as we can expect it to be there for nodes.
$this->getSimpleSearchQueryBuilder()->sortAsc("__sortIndex");
$timeBefore = microtime(true);
$result = $this->getSimpleSearchQueryBuilder()->execute();
$timeAfterwards = microtime(true);
if ($this->queryLogEnabled === true) {
$this->logger->debug('Query Log (' . $this->logMessage . '): -- execution time: ' . (($timeAfterwards - $timeBefore) * 1000) . ' ms -- Total Results: ' . count($result));
}
$nodes = [];
foreach ($result as $hit) {
$nodePath = $hit['__path'];
$node = $this->contextNode->getNode($nodePath);
if ($node instanceof NodeInterface) {
$nodes[(string) $node->getNodeAggregateIdentifier()] = $node;
}
}
return (new \ArrayObject(array_values($nodes)))->getIterator();
}
/**
* Log the current request for debugging after it has been executed.
*
* @param string $message an optional message to identify the log entry
*/
public function log(string $message = null): AbstractQueryBuilder
{
$this->queryLogEnabled = true;
$this->logMessage = $message;
return $this;
}
/**
* Return the total number of hits for the query.
*/
public function count(): int
{
$timeBefore = microtime(true);
$count = $this->getSimpleSearchQueryBuilder()->count();
$timeAfterwards = microtime(true);
if ($this->queryLogEnabled === true) {
$this->logger->debug('Query Log (' . $this->logMessage . '): -- execution time: ' . (($timeAfterwards - $timeBefore) * 1000) . ' ms -- Total Results: ' . $count);
}
return $count;
}
/**
* @param string $methodName
*/
public function allowsCallOfMethod($methodName): bool
{
if ($methodName !== 'getFindIdentifiersByNodeIdentifierQuery') {
// query must be called first to establish a context and starting point.
return !($this->contextNode === null && $methodName !== 'query');
}
return false;
}
}