phpcr-utils /src/PHPCR/Util/QOM/Sql2Generator.php

Language PHP Lines 349
MD5 Hash 3dde95b63b3c7d9d5c35b5afae250376
Repository https://github.com/lyrixx/phpcr-utils.git View Raw File
  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
<?php

namespace PHPCR\Util\QOM;

use PHPCR\Query\QOM;
use PHPCR\Query\QOM\QueryObjectModelConstantsInterface as Constants;

/**
 * Generate SQL2 statements
 *
 * TODO: is eval... the best name for the functions here?
 */
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 $selectorName
     * @return string
     */
    public function evalSelector($nodeTypeName, $selectorName = null)
    {
        $sql2 = $nodeTypeName;
        if (substr($sql2, 0, 1) !== '[' && substr($sql2, -1) !== ']') {
            $sql2 = '[' . $sql2 . ']';
        }

        $name = $selectorName;
        if (! is_null($name)) {
            $sql2 .= ' AS ' . $name;
        }

        return $sql2;
    }

    /**
     * Join ::= left [JoinType] 'JOIN' right 'ON' JoinCondition
     *    // If JoinType is omitted INNER is assumed.
     * left ::= Source
     * right ::= Source
     *
     * @param  string $left
     * @param  string $right
     * @param  string $joinCondition
     * @param  string $joinType
     * @return string
     */
    public function evalJoin($left, $right, $joinCondition, $joinType = '')
    {
        return "$left {$joinType}JOIN $right ON $joinCondition";
    }

    /**
     * JoinType ::= Inner | LeftOuter | RightOuter
     * Inner ::= 'INNER'
     * LeftOuter ::= 'LEFT OUTER'
     * RightOuter ::= 'RIGHT OUTER'
     *
     * @param  string $joinType
     * @return string
     */
    public function evalJoinType($joinType)
    {
        switch ($joinType) {
            case Constants::JCR_JOIN_TYPE_INNER:
                return 'INNER ';
            case Constants::JCR_JOIN_TYPE_LEFT_OUTER:
                return 'LEFT OUTER ';
            case Constants::JCR_JOIN_TYPE_RIGHT_OUTER:
                return 'RIGHT OUTER ';
        }

        return $joinType;
    }

    /**
     * EquiJoinCondition ::= selector1Name'.'property1Name '='
     *                       selector2Name'.'property2Name
     *   selector1Name ::= selectorName
     *   selector2Name ::= selectorName
     *   property1Name ::= propertyName
     *   property2Name ::= propertyName
     *
     * @param  string $sel1Name
     * @param  string $prop1Name
     * @param  string $sel2Name
     * @param  string $prop2Name
     * @return string
     */
    public function evalEquiJoinCondition($sel1Name, $prop1Name, $sel2Name, $prop2Name)
    {
        return $this->evalPropertyValue($prop1Name, $sel1Name) . '=' .$this->evalPropertyValue($prop2Name, $sel2Name);
    }

    /**
     * SameNodeJoinCondition ::=
     *   'ISSAMENODE(' selector1Name ','
     *                  selector2Name
     *                  [',' selector2Path] ')'
     *   selector2Path ::= Path
     *
     * @param  string $sel1Name
     * @param  string $sel2Name
     * @param  string $sel2path
     * @return string
     */
    public function evalSameNodeJoinCondition($sel1Name, $sel2Name, $sel2Path = null)
    {
        $sql2 = "ISSAMENODE($sel1Name, $sel2Name";
        $sql2 .= ! is_null($sel2Path) ? ', ' . $sel2Path : '';
        $sql2 .= ')';

        return $sql2;
    }

    /**
     * ChildNodeJoinCondition ::=
     *   'ISCHILDNODE(' childSelectorName ','
     *                  parentSelectorName ')'
     *   childSelectorName ::= selectorName
     *   parentSelectorName ::= selectorName
     *
     * @param  string $childSelectorName
     * @param  string $parentSelectorName
     * @return string
     */
    public function evalChildNodeJoinCondition($childSelectorName, $parentSelectorName)
    {
        return "ISCHILDNODE($childSelectorName, $parentSelectorName)";
    }

    /**
     * DescendantNodeJoinCondition ::=
     *   'ISDESCENDANTNODE(' descendantSelectorName ','
     *                       ancestorSelectorName ')'
     *   descendantSelectorName ::= selectorName
     *   ancestorSelectorName ::= selectorName
     *
     * @param  string $descendantSelectorName
     * @param  string $ancestorselectorName
     * @return string
     */
    public function evalDescendantNodeJoinCondition($descendantSelectorName, $ancestorselectorName)
    {
        return "ISDESCENDANTNODE($descendantSelectorName, $ancestorselectorName)";
    }

    /**
     * SameNode ::= 'ISSAMENODE(' [selectorName ','] Path ')'
     *
     * @param string $path
     * @param string $selectorName
     */
    public function evalSameNode($path, $selectorName = null)
    {
        $sql2 = 'ISSAMENODE(';
        $sql2 .= is_null($selectorName) ? $path : $selectorName . ', ' . $path;
        $sql2 .= ')';

        return $sql2;
    }

    /**
     * SameNode ::= 'ISCHILDNODE(' [selectorName ','] Path ')'
     *
     * @param string $path
     * @param string $selectorName
     */
    public function evalChildNode($path, $selectorName = null)
    {
        $sql2 = 'ISCHILDNODE(';
        $sql2 .= is_null($selectorName) ? $path : $selectorName . ', ' . $path;
        $sql2 .= ')';

        return $sql2;
    }

    /**
     * SameNode ::= 'ISDESCENDANTNODE(' [selectorName ','] Path ')'
     *
     * @param string $path
     * @param string $selectorName
     */
    public function evalDescendantNode($path, $selectorName = null)
    {
        $sql2 = 'ISDESCENDANTNODE(';
        $sql2 .= is_null($selectorName) ? $path : $selectorName . ', ' . $path;
        $sql2 .= ')';

        return $sql2;
    }

    public function evalPropertyExistence($selectorName, $propertyName)
    {
        return $this->evalPropertyValue($propertyName, $selectorName) . " IS NOT NULL";
    }

    /**
     * FullTextSearch ::=
     *       'CONTAINS(' ([selectorName'.']propertyName |
     *                    selectorName'.*') ','
     *                    FullTextSearchExpression ')'
     * FullTextSearchExpression ::= BindVariable | ''' FullTextSearchLiteral '''
     * @param  string $selectorName     unusued
     * @param  string $searchExpression
     * @param  string $ropertyName
     * @return string
     */
    public function evalFullTextSearch($selectorName, $searchExpression, $propertyName = null)
    {
        $propertyName = $propertyName ?: '*';

        $sql2 = 'CONTAINS(';
        $sql2 .= $this->evalPropertyValue($propertyName, $selectorName);
        $sql2 .= ', ' . $searchExpression . ')';

        return $sql2;
    }

    /**
     * Length ::= 'LENGTH(' PropertyValue ')'
     *
     * @param  string $propertyValue
     * @return string
     */
    public function evalLength($propertyValue)
    {
        return "LENGTH($propertyValue)";
    }

    /**
     * NodeName ::= 'NAME(' [selectorName] ')'
     *
     * @param string $selectorValue
     */
    public function evalNodeName($selectorValue = null)
    {
        return "NAME($selectorValue)";
    }

    /**
     * NodeLocalName ::= 'LOCALNAME(' [selectorName] ')'
     *
     * @param string $selectorValue
     */
    public function evalNodeLocalName($selectorValue = null)
    {
        return "LOCALNAME($selectorValue)";
    }

    /**
     * FullTextSearchScore ::= 'SCORE(' [selectorName] ')'
     *
     * @param string $selectorValue
     */
    public function evalFullTextSearchScore($selectorValue = null)
    {
        return "SCORE($selectorValue)";
    }

    /**
     * PropertyValue ::= [selectorName'.'] propertyName     // If only one selector exists
     *
     * @param string $propertyName
     * @param string $selectorName
     */
    public function evalPropertyValue($propertyName, $selectorName = null)
    {
        if (false !== strpos($selectorName, ':')) {
            $selectorName = "[$selectorName]";
        }
        $sql2 = ! is_null($selectorName) ? $selectorName . '.' : '';
        if (false !== strpos($propertyName, ':')) {
            $propertyName = "[$propertyName]";
        }
        $sql2 .= $propertyName;

        return $sql2;
    }

    public function evalColumns($columns)
    {
        if (count($columns) === 0) {
            return '*';
        }

        $sql2 = '';
        foreach ($columns as $column) {
            if ($sql2 !== '') {
                $sql2 .= ', ';
            }

            $sql2 .= $column;
        }

        return $sql2;
    }

    public function evalColumn($selectorName, $propertyName = null, $colname = null)
    {
        $sql2 = '';
        if (! is_null($selectorName) && is_null($propertyName) && is_null($colname)) {
            $sql2 .= $selectorName . '.*';
        } else {
            $sql2 .= $this->evalPropertyValue($propertyName, $selectorName);
            $sql2 .= ! is_null($colname) ? ' 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
     *
     * @param  string $path
     * @return string
     */
    public function evalPath($path)
    {
        if ($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 (substr($path, 0,1) !== '[' && substr($path, -1) !== ']') {
                if (false !== strpos($sql2, ' ') || false !== strpos($sql2, '.')) {
                    $sql2 = '"' . $sql2 . '"';
                }
                $sql2 = '[' . $sql2 . ']';
            }

            return $sql2;
        }

        return null;
    }

    /**
     * @param string $literal
     * @param string $type
     */
    public function evalCastLiteral($literal, $type)
    {
        return "CAST('$literal' AS $type)";
    }
}
Back to Top