-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathSql2Generator.php
More file actions
334 lines (294 loc) · 10.4 KB
/
Sql2Generator.php
File metadata and controls
334 lines (294 loc) · 10.4 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
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
<?php
declare(strict_types=1);
namespace PHPCR\Util\QOM;
use PHPCR\Query\QOM\QueryObjectModelConstantsInterface as Constants;
/**
* Generate SQL2 statements.
*
* @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
* @license http://opensource.org/licenses/MIT MIT License
*/
class Sql2Generator extends BaseSqlGenerator
{
/**
* Selector ::= nodeTypeName ['AS' selectorName]
* nodeTypeName ::= Name.
*
* @param string $nodeTypeName The node type of the selector. If it
* does not contain starting and ending brackets ([]) they will be
* added automatically.
* @param string|null $selectorName The selector name. If it is different than
* the nodeTypeName, the alias is declared.
*/
public function evalSelector(string $nodeTypeName, ?string $selectorName = null): string
{
$sql2 = $this->addBracketsIfNeeded($nodeTypeName);
if (null !== $selectorName && $nodeTypeName !== $selectorName) {
// if the selector name is the same as the type name, this is implicit for sql2
$sql2 .= ' AS '.$selectorName;
}
return $sql2;
}
/**
* Join ::= left [JoinType] 'JOIN' right 'ON' JoinCondition
* // If JoinType is omitted INNER is assumed.
* left ::= Source
* right ::= Source.
*/
public function evalJoin(string $left, string $right, string $joinCondition, string $joinType = ''): string
{
return "$left {$joinType}JOIN $right ON $joinCondition";
}
/**
* JoinType ::= Inner | LeftOuter | RightOuter
* Inner ::= 'INNER'
* LeftOuter ::= 'LEFT OUTER'
* RightOuter ::= 'RIGHT OUTER'.
*/
public function evalJoinType(string $joinType): string
{
return match ($joinType) {
Constants::JCR_JOIN_TYPE_INNER => 'INNER ',
Constants::JCR_JOIN_TYPE_LEFT_OUTER => 'LEFT OUTER ',
Constants::JCR_JOIN_TYPE_RIGHT_OUTER => 'RIGHT OUTER ',
default => $joinType,
};
}
/**
* EquiJoinCondition ::= selector1Name'.'property1Name '='
* selector2Name'.'property2Name
* selector1Name ::= selectorName
* selector2Name ::= selectorName
* property1Name ::= propertyName
* property2Name ::= propertyName.
*/
public function evalEquiJoinCondition(string $sel1Name, string $prop1Name, string $sel2Name, string $prop2Name): string
{
return $this->evalPropertyValue($prop1Name, $sel1Name).'='.$this->evalPropertyValue($prop2Name, $sel2Name);
}
/**
* SameNodeJoinCondition ::=
* 'ISSAMENODE(' selector1Name ','
* selector2Name
* [',' selector2Path] ')'
* selector2Path ::= Path.
*/
public function evalSameNodeJoinCondition(string $sel1Name, string $sel2Name, ?string $sel2Path = null): string
{
$sql2 = 'ISSAMENODE('
.$this->addBracketsIfNeeded($sel1Name).', '
.$this->addBracketsIfNeeded($sel2Name);
if (null !== $sel2Path) {
$sql2 .= ', '.$sel2Path;
}
$sql2 .= ')';
return $sql2;
}
/**
* ChildNodeJoinCondition ::=
* 'ISCHILDNODE(' childSelectorName ','
* parentSelectorName ')'
* childSelectorName ::= selectorName
* parentSelectorName ::= selectorName.
*/
public function evalChildNodeJoinCondition(string $childSelectorName, string $parentSelectorName): string
{
return 'ISCHILDNODE('
.$this->addBracketsIfNeeded($childSelectorName).', '
.$this->addBracketsIfNeeded($parentSelectorName).')';
}
/**
* DescendantNodeJoinCondition ::=
* 'ISDESCENDANTNODE(' descendantSelectorName ','
* ancestorSelectorName ')'
* descendantSelectorName ::= selectorName
* ancestorSelectorName ::= selectorName.
*/
public function evalDescendantNodeJoinCondition(string $descendantSelectorName, string $ancestorselectorName): string
{
return 'ISDESCENDANTNODE('
.$this->addBracketsIfNeeded($descendantSelectorName).', '
.$this->addBracketsIfNeeded($ancestorselectorName).')';
}
/**
* SameNode ::= 'ISSAMENODE(' [selectorName ','] Path ')'.
*/
public function evalSameNode(string $path, ?string $selectorName = null): string
{
$sql2 = 'ISSAMENODE(';
$sql2 .= null === $selectorName ? $path : $this->addBracketsIfNeeded($selectorName).', '.$path;
$sql2 .= ')';
return $sql2;
}
/**
* SameNode ::= 'ISCHILDNODE(' [selectorName ','] Path ')'.
*/
public function evalChildNode(string $path, ?string $selectorName = null): string
{
$sql2 = 'ISCHILDNODE(';
$sql2 .= null === $selectorName ? $path : $this->addBracketsIfNeeded($selectorName).', '.$path;
$sql2 .= ')';
return $sql2;
}
/**
* SameNode ::= 'ISDESCENDANTNODE(' [selectorName ','] Path ')'.
*/
public function evalDescendantNode(string $path, ?string $selectorName = null): string
{
$sql2 = 'ISDESCENDANTNODE(';
$sql2 .= null === $selectorName ? $path : $this->addBracketsIfNeeded($selectorName).', '.$path;
$sql2 .= ')';
return $sql2;
}
/**
* PropertyExistence ::=
* selectorName'.'propertyName 'IS NOT NULL' |
* propertyName 'IS NOT NULL' If only one
* selector exists in
* this query.
*/
public function evalPropertyExistence(?string $selectorName, string $propertyName): string
{
return $this->evalPropertyValue($propertyName, $selectorName).' IS NOT NULL';
}
/**
* FullTextSearch ::=
* 'CONTAINS(' ([selectorName'.']propertyName |
* selectorName'.*') ','
* FullTextSearchExpression ')'
* FullTextSearchExpression ::= BindVariable | ''' FullTextSearchLiteral '''.
*/
public function evalFullTextSearch(string $selectorName, string $searchExpression, ?string $propertyName = null): string
{
$propertyName = $propertyName ?: '*';
$sql2 = 'CONTAINS(';
$sql2 .= $this->evalPropertyValue($propertyName, $selectorName);
$sql2 .= ', '.$searchExpression.')';
return $sql2;
}
/**
* Length ::= 'LENGTH(' PropertyValue ')'.
*/
public function evalLength(string $propertyValue): string
{
return "LENGTH($propertyValue)";
}
/**
* NodeName ::= 'NAME(' [selectorName] ')'.
*/
public function evalNodeName(?string $selectorValue = null): string
{
return "NAME($selectorValue)";
}
/**
* NodeLocalName ::= 'LOCALNAME(' [selectorName] ')'.
*/
public function evalNodeLocalName(?string $selectorValue = null): string
{
return "LOCALNAME($selectorValue)";
}
/**
* FullTextSearchScore ::= 'SCORE(' [selectorName] ')'.
*/
public function evalFullTextSearchScore(?string $selectorValue = null): string
{
return "SCORE($selectorValue)";
}
/**
* PropertyValue ::= [selectorName'.'] propertyName // If only one selector exists.
*/
public function evalPropertyValue(string $propertyName, ?string $selectorName = null): string
{
$sql2 = null !== $selectorName ? $this->addBracketsIfNeeded($selectorName).'.' : '';
if ('*' !== $propertyName && !str_starts_with($propertyName, '[')) {
$propertyName = "[$propertyName]";
}
$sql2 .= $propertyName;
return $sql2;
}
/**
* columns ::= (Column ',' {Column}) | '*'.
*/
public function evalColumns(iterable $columns): string
{
if ((!is_array($columns) && !$columns instanceof \Countable)
|| 0 === count($columns)
) {
return '*';
}
$sql2 = '';
foreach ($columns as $column) {
if ('' !== $sql2) {
$sql2 .= ', ';
}
$sql2 .= $column;
}
return $sql2;
}
/**
* Column ::= ([selectorName'.']propertyName
* ['AS' columnName]) |
* (selectorName'.*') // If only one selector exists
* selectorName ::= Name
* propertyName ::= Name
* columnName ::= Name.
*/
public function evalColumn(string $selectorName, ?string $propertyName = null, ?string $colname = null): string
{
$sql2 = '';
if (null === $propertyName && null === $colname) {
$sql2 .= $this->addBracketsIfNeeded($selectorName).'.*';
} else {
$sql2 .= $this->evalPropertyValue($propertyName, $selectorName);
if (null !== $colname && $colname !== $propertyName) {
// if the column name is the same as the property name, this is implicit for sql2
$sql2 .= ' AS '.$colname;
}
}
return $sql2;
}
/**
* Path ::= '[' quotedPath ']' | '[' simplePath ']' | simplePath
* quotedPath ::= A JCR Path that contains non-SQL-legal characters
* simplePath ::= A JCR Name that contains only SQL-legal characters.
*/
public function evalPath(string $path): string
{
if (!$path) {
return $path;
}
$sql2 = $path;
// only ensure proper quoting if the user did not quote himself, we trust him to get it right if he did.
if (!str_starts_with($path, '[') && !str_ends_with($path, ']')) {
if (str_contains($sql2, ' ') || str_contains($sql2, '.')) {
$sql2 = '"'.$sql2.'"';
}
$sql2 = '['.$sql2.']';
}
return $sql2;
}
/**
* {@inheritdoc}
*
* CastLiteral ::= 'CAST(' UncastLiteral ' AS ' PropertyType ')'
*/
public function evalCastLiteral(string $literal, string $type): string
{
return "CAST('$literal' AS $type)";
}
/**
* Add square brackets around a selector if needed.
*
* @return string $selector guaranteed to have [] around it if needed
*/
private function addBracketsIfNeeded(string $selector): string
{
if (!str_starts_with($selector, '[')
&& !str_ends_with($selector, ']')
&& str_contains($selector, ':')
) {
return "[$selector]";
}
return $selector;
}
}