File size: 24,722 Bytes
5fcd275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
diff --git a/src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php b/src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php
index f3d945c7..70e630fd 100644
--- a/src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php
+++ b/src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php
@@ -385,28 +385,28 @@ public function walkJoinSource(QOM\JoinInterface $source, $root = true)
 
         switch ($source->getJoinType()) {
             case QOM\QueryObjectModelConstantsInterface::JCR_JOIN_TYPE_INNER:
-                $sql .= "INNER JOIN phpcr_nodes $rightAlias ";
+                $sql .= sprintf("INNER JOIN phpcr_nodes %s ", $rightAlias);
                 break;
             case QOM\QueryObjectModelConstantsInterface::JCR_JOIN_TYPE_LEFT_OUTER:
-                $sql .= "LEFT JOIN phpcr_nodes $rightAlias ";
+                $sql .= sprintf("LEFT JOIN phpcr_nodes %s ", $rightAlias);
                 break;
             case QOM\QueryObjectModelConstantsInterface::JCR_JOIN_TYPE_RIGHT_OUTER:
-                $sql .= "RIGHT JOIN phpcr_nodes $rightAlias ";
+                $sql .= sprintf("RIGHT JOIN phpcr_nodes %s ", $rightAlias);
                 break;
         }
 
-        $sql .= "ON ( $leftAlias.workspace_name = $rightAlias.workspace_name AND $nodeTypeClause ";
+        $sql .= sprintf("ON ( %s.workspace_name = %s.workspace_name AND %s ", $leftAlias, $rightAlias, $nodeTypeClause);
         $sql .= 'AND ' . $this->walkJoinCondition($source->getLeft(), $source->getRight(), $source->getJoinCondition()) . ' ';
         $sql .= ') '; // close on-clause
 
 
         if ($root) { // The method call is not recursed when $root is true, so we can add a WHERE clause
             // TODO: revise this part for alternatives
-            $sql .= "WHERE $leftAlias.workspace_name = ? AND $leftAlias.type IN ('" . $left->getNodeTypeName() . "'";
+            $sql .= sprintf("WHERE %s.workspace_name = ? AND %s.type IN ('%s'", $leftAlias, $leftAlias, $left->getNodeTypeName());
             $subTypes = $this->nodeTypeManager->getSubtypes($left->getNodeTypeName());
             foreach ($subTypes as $subType) {
                 /* @var $subType NodeTypeInterface */
-                $sql .= ", '" . $subType->getName() . "'";
+                $sql .= sprintf(", '%s'", $subType->getName());
             }
             $sql .= ')';
         }
@@ -455,7 +455,7 @@ public function walkChildNodeJoinCondition(QOM\ChildNodeJoinConditionInterface $
         $leftAlias = $this->getTableAlias($condition->getParentSelectorName());
         $concatExpression = $this->platform->getConcatExpression("$leftAlias.path", "'/%'");
 
-        return "($rightAlias.path LIKE " . $concatExpression . " AND $rightAlias.depth = $leftAlias.depth + 1) ";
+        return sprintf("(%s.path LIKE %s AND %s.depth = %s.depth + 1) ", $rightAlias, $concatExpression, $rightAlias, $leftAlias);
     }
 
     /**
@@ -469,7 +469,7 @@ public function walkDescendantNodeJoinCondition(QOM\DescendantNodeJoinConditionI
         $leftAlias = $this->getTableAlias($condition->getAncestorSelectorName());
         $concatExpression = $this->platform->getConcatExpression("$leftAlias.path", "'/%'");
 
-        return "$rightAlias.path LIKE " . $concatExpression . " ";
+        return sprintf("%s.path LIKE %s ", $rightAlias, $concatExpression);
     }
 
     /**
@@ -521,7 +521,7 @@ public function walkConstraint(QOM\ConstraintInterface $constraint)
             return $this->walkFullTextSearchConstraint($constraint);
         }
 
-        throw new InvalidQueryException("Constraint " . get_class($constraint) . " not yet supported.");
+        throw new InvalidQueryException(sprintf("Constraint %s not yet supported.", get_class($constraint)));
     }
 
     /**
@@ -531,7 +531,11 @@ public function walkConstraint(QOM\ConstraintInterface $constraint)
      */
     public function walkSameNodeConstraint(QOM\SameNodeInterface $constraint)
     {
-        return $this->getTableAlias($constraint->getSelectorName()) . ".path = '" . $constraint->getPath() . "'";
+        return sprintf(
+            "%s.path = '%s'",
+            $this->getTableAlias($constraint->getSelectorName()),
+            $constraint->getPath()
+        );
     }
 
     /**
@@ -541,7 +545,10 @@ public function walkSameNodeConstraint(QOM\SameNodeInterface $constraint)
      */
     public function walkFullTextSearchConstraint(QOM\FullTextSearchInterface $constraint)
     {
-        return $this->sqlXpathExtractValue($this->getTableAlias($constraint->getSelectorName()), $constraint->getPropertyName()).' LIKE '. $this->conn->quote('%'.$constraint->getFullTextSearchExpression().'%');
+        return sprintf('%s LIKE %s',
+            $this->sqlXpathExtractValue($this->getTableAlias($constraint->getSelectorName()), $constraint->getPropertyName()),
+            $this->conn->quote('%'.$constraint->getFullTextSearchExpression().'%')
+        );
     }
 
     /**
@@ -568,7 +575,11 @@ public function walkDescendantNodeConstraint(QOM\DescendantNodeInterface $constr
             throw new InvalidQueryException("Trailing slash in $ancestorPath");
         }
 
-        return $this->getTableAlias($constraint->getSelectorName()) . ".path LIKE '" . $ancestorPath . "/%'";
+        return sprintf(
+            "%s.path LIKE '%s/%%'",
+            $this->getTableAlias($constraint->getSelectorName()),
+            addcslashes($ancestorPath, "'")
+        );
     }
 
     /**
@@ -578,7 +589,11 @@ public function walkDescendantNodeConstraint(QOM\DescendantNodeInterface $constr
      */
     public function walkChildNodeConstraint(QOM\ChildNodeInterface $constraint)
     {
-        return $this->getTableAlias($constraint->getSelectorName()) . ".parent = '" . $constraint->getParentPath() . "'";
+        return sprintf(
+            "%s.parent = '%s'",
+            $this->getTableAlias($constraint->getSelectorName()),
+            addcslashes($constraint->getParentPath(), "'")
+        );
     }
 
     /**
@@ -588,7 +603,11 @@ public function walkChildNodeConstraint(QOM\ChildNodeInterface $constraint)
      */
     public function walkAndConstraint(QOM\AndInterface $constraint)
     {
-        return "(" . $this->walkConstraint($constraint->getConstraint1()) . " AND " . $this->walkConstraint($constraint->getConstraint2()) . ")";
+        return sprintf(
+            "(%s AND %s)",
+            $this->walkConstraint($constraint->getConstraint1()),
+            $this->walkConstraint($constraint->getConstraint2())
+        );
     }
 
     /**
@@ -598,7 +617,11 @@ public function walkAndConstraint(QOM\AndInterface $constraint)
      */
     public function walkOrConstraint(QOM\OrInterface $constraint)
     {
-        return "(" . $this->walkConstraint($constraint->getConstraint1()) . " OR " . $this->walkConstraint($constraint->getConstraint2()) . ")";
+        return sprintf(
+            "(%s OR %s)",
+            $this->walkConstraint($constraint->getConstraint1()),
+            $this->walkConstraint($constraint->getConstraint2())
+        );
     }
 
     /**
@@ -608,7 +631,10 @@ public function walkOrConstraint(QOM\OrInterface $constraint)
      */
     public function walkNotConstraint(QOM\NotInterface $constraint)
     {
-        return "NOT (" . $this->walkConstraint($constraint->getConstraint()) . ")";
+        return sprintf(
+            "NOT (%s)",
+            $this->walkConstraint($constraint->getConstraint())
+        );
     }
 
     /**
@@ -670,9 +696,16 @@ public function walkComparisonConstraint(QOM\ComparisonInterface $constraint)
                     $literal = implode(':', $parts);
                 }
 
-                return $this->platform->getConcatExpression("$alias.namespace", "(CASE $alias.namespace WHEN '' THEN '' ELSE ':' END)", "$alias.local_name") . " " .
-                    $operator . " " .
-                    $this->conn->quote($literal);
+                return sprintf(
+                    '%s %s %s',
+                    $this->platform->getConcatExpression(
+                        sprintf("%s.namespace", $alias),
+                        sprintf("(CASE %s.namespace WHEN '' THEN '' ELSE ':' END)", $alias),
+                        sprintf("%s.local_name", $alias)
+                    ),
+                    $operator,
+                    $this->conn->quote($literal)
+                ) ;
             }
 
             if ('jcr:path' !== $operand->getPropertyName() && 'jcr:uuid' !== $operand->getPropertyName()) {
@@ -687,10 +720,12 @@ public function walkComparisonConstraint(QOM\ComparisonInterface $constraint)
             }
         }
 
-        return
-            $this->walkOperand($operator1) . ' ' .
-            $operator . ' ' .
-            $this->walkOperand($operator2);
+        return sprintf(
+            '%s %s %s',
+            $this->walkOperand($operator1),
+            $operator,
+            $this->walkOperand($operator2)
+        );
     }
 
     /**
@@ -723,10 +758,10 @@ public function walkNumComparisonConstraint(QOM\PropertyValueInterface $property
 
         if ($this->platform instanceof MySQLPlatform && '=' === $operator) {
             return sprintf(
-                '0 != FIND_IN_SET("%s", REPLACE(EXTRACTVALUE(%s.props, \'//sv:property[@sv:name="%s"]/sv:value\'), " ", ","))',
+                '0 != FIND_IN_SET("%s", REPLACE(EXTRACTVALUE(%s.props, \'//sv:property[@sv:name=%s]/sv:value\'), " ", ","))',
                 $literalOperand->getLiteralValue(),
                 $alias,
-                $property
+                Xpath::escape($property)
             );
         }
 
@@ -787,14 +822,18 @@ public function walkOperand(QOM\OperandInterface $operand)
             $selectorName = $operand->getSelectorName();
             $alias = $this->getTableAlias($selectorName);
 
-            return $this->platform->getConcatExpression("$alias.namespace", "(CASE $alias.namespace WHEN '' THEN '' ELSE ':' END)", "$alias.local_name");
+            return $this->platform->getConcatExpression(
+                sprintf("%s.namespace", $alias),
+                sprintf("(CASE %s.namespace WHEN '' THEN '' ELSE ':' END)", $alias),
+                sprintf("%s.local_name", $alias)
+            );
         }
 
         if ($operand instanceof QOM\NodeLocalNameInterface) {
             $selectorName = $operand->getSelectorName();
             $alias = $this->getTableAlias($selectorName);
 
-            return "$alias.local_name";
+            return sprintf("%s.local_name", $alias);
         }
 
         if ($operand instanceof QOM\LowerCaseInterface) {
@@ -813,10 +852,10 @@ public function walkOperand(QOM\OperandInterface $operand)
             $alias = $this->getTableAlias($operand->getSelectorName() . '.' . $operand->getPropertyName());
             $property = $operand->getPropertyName();
             if ($property === 'jcr:path') {
-                return "$alias.path";
+                return sprintf("%s.path", $alias);
             }
             if ($property === "jcr:uuid") {
-                return "$alias.identifier";
+                return sprintf("%s.identifier", $alias);
             }
 
             return $this->sqlXpathExtractValue($alias, $property);
@@ -829,7 +868,7 @@ public function walkOperand(QOM\OperandInterface $operand)
             return $this->sqlXpathExtractValueAttribute($alias, $property, 'length');
         }
 
-        throw new InvalidQueryException("Dynamic operand " . get_class($operand) . " not yet supported.");
+        throw new InvalidQueryException(sprintf("Dynamic operand %s not yet supported.", get_class($operand)));
     }
 
     /**
@@ -920,18 +959,18 @@ private function getLiteralValue(QOM\LiteralInterface $operand)
     private function sqlXpathValueExists($alias, $property)
     {
         if ($this->platform instanceof MySQLPlatform) {
-            return "EXTRACTVALUE($alias.props, 'count(//sv:property[@sv:name=\"" . $property . "\"]/sv:value[1])') = 1";
+            return sprintf("EXTRACTVALUE(%s.props, 'count(//sv:property[@sv:name=%s]/sv:value[1])') = 1", $alias, Xpath::escape($property));
         }
 
         if ($this->platform instanceof PostgreSQL94Platform || $this->platform instanceof PostgreSqlPlatform) {
-            return "xpath_exists('//sv:property[@sv:name=\"" . $property . "\"]/sv:value[1]', CAST($alias.props AS xml), ".$this->sqlXpathPostgreSQLNamespaces().") = 't'";
+            return sprintf("xpath_exists('//sv:property[@sv:name=%s]/sv:value[1]', CAST(%s.props AS xml), ".$this->sqlXpathPostgreSQLNamespaces().") = 't'", Xpath::escape($property), $alias);
         }
 
         if ($this->platform instanceof SqlitePlatform) {
-            return "EXTRACTVALUE($alias.props, 'count(//sv:property[@sv:name=\"" . $property . "\"]/sv:value[1])') = 1";
+            return sprintf("EXTRACTVALUE(%s.props, 'count(//sv:property[@sv:name=%s]/sv:value[1])') = 1", $alias, Xpath::escape($property));
         }
 
-        throw new NotImplementedException("Xpath evaluations cannot be executed with '" . $this->platform->getName() . "' yet.");
+        throw new NotImplementedException(sprintf("Xpath evaluations cannot be executed with '%s' yet.", $this->platform->getName()));
     }
 
     /**
@@ -945,44 +984,44 @@ private function sqlXpathValueExists($alias, $property)
     private function sqlXpathExtractValue($alias, $property, $column = 'props')
     {
         if ($this->platform instanceof MySQLPlatform) {
-            return "EXTRACTVALUE($alias.$column, '//sv:property[@sv:name=\"" . $property . "\"]/sv:value[1]')";
+            return sprintf("EXTRACTVALUE(%s.%s, '//sv:property[@sv:name=%s]/sv:value[1]')", $alias, $column, Xpath::escape($property));
         }
 
         if ($this->platform instanceof PostgreSQL94Platform || $this->platform instanceof PostgreSqlPlatform) {
-            return "(xpath('//sv:property[@sv:name=\"" . $property . "\"]/sv:value[1]/text()', CAST($alias.$column AS xml), ".$this->sqlXpathPostgreSQLNamespaces()."))[1]::text";
+            return sprintf("(xpath('//sv:property[@sv:name=%s]/sv:value[1]/text()', CAST(%s.%s AS xml), %s))[1]::text", Xpath::escape($property), $alias, $column, $this->sqlXpathPostgreSQLNamespaces());
         }
 
         if ($this->platform instanceof SqlitePlatform) {
-            return "EXTRACTVALUE($alias.$column, '//sv:property[@sv:name=\"" . $property . "\"]/sv:value[1]')";
+            return sprintf("EXTRACTVALUE(%s.%s, '//sv:property[@sv:name=%s]/sv:value[1]')", $alias, $column, Xpath::escape($property));
         }
 
-        throw new NotImplementedException("Xpath evaluations cannot be executed with '" . $this->platform->getName() . "' yet.");
+        throw new NotImplementedException(sprintf("Xpath evaluations cannot be executed with '%s' yet.", $this->platform->getName()));
     }
 
     private function sqlXpathExtractNumValue($alias, $property)
     {
         if ($this->platform instanceof PostgreSQL94Platform || $this->platform instanceof PostgreSqlPlatform) {
-            return "(xpath('//sv:property[@sv:name=\"" . $property . "\"]/sv:value[1]/text()', CAST($alias.props AS xml), ".$this->sqlXpathPostgreSQLNamespaces()."))[1]::text::int";
+            return sprintf("(xpath('//sv:property[@sv:name=%s]/sv:value[1]/text()', CAST(%s.props AS xml), %s))[1]::text::int", Xpath::escape($property), $alias, $this->sqlXpathPostgreSQLNamespaces());
         }
 
-        return 'CAST(' . $this->sqlXpathExtractValue($alias, $property) . ' AS DECIMAL)';
+        return sprintf('CAST(%s AS DECIMAL)', $this->sqlXpathExtractValue($alias, $property));
     }
 
     private function sqlXpathExtractValueAttribute($alias, $property, $attribute, $valueIndex = 1)
     {
         if ($this->platform instanceof MySQLPlatform) {
-            return sprintf("EXTRACTVALUE(%s.props, '//sv:property[@sv:name=\"%s\"]/sv:value[%d]/@%s')", $alias, $property, $valueIndex, $attribute);
+            return sprintf("EXTRACTVALUE(%s.props, '//sv:property[@sv:name=%s]/sv:value[%d]/@%s')", $alias, Xpath::escape($property), $valueIndex, $attribute);
         }
 
         if ($this->platform instanceof PostgreSQL94Platform || $this->platform instanceof PostgreSqlPlatform) {
-            return sprintf("CAST((xpath('//sv:property[@sv:name=\"%s\"]/sv:value[%d]/@%s', CAST(%s.props AS xml), %s))[1]::text AS bigint)", $property, $valueIndex, $attribute, $alias, $this->sqlXpathPostgreSQLNamespaces());
+            return sprintf("CAST((xpath('//sv:property[@sv:name=%s]/sv:value[%d]/@%s', CAST(%s.props AS xml), %s))[1]::text AS bigint)", Xpath::escape($property), $valueIndex, $attribute, $alias, $this->sqlXpathPostgreSQLNamespaces());
         }
 
         if ($this->platform instanceof SqlitePlatform) {
-            return sprintf("EXTRACTVALUE(%s.props, '//sv:property[@sv:name=\"%s\"]/sv:value[%d]/@%s')", $alias, $property, $valueIndex, $attribute);
+            return sprintf("EXTRACTVALUE(%s.props, '//sv:property[@sv:name=%s]/sv:value[%d]/@%s')", $alias, Xpath::escape($property), $valueIndex, $attribute);
         }
 
-        throw new NotImplementedException("Xpath evaluations cannot be executed with '" . $this->platform->getName() . "' yet.");
+        throw new NotImplementedException(sprintf("Xpath evaluations cannot be executed with '%s' yet.", $this->platform->getName()));
     }
 
     /**
@@ -1001,15 +1040,15 @@ private function sqlXpathComparePropertyValue($alias, $property, $value, $operat
         $expression = null;
 
         if ($this->platform instanceof MySQLPlatform) {
-            $expression = "EXTRACTVALUE($alias.props, 'count(//sv:property[@sv:name=\"" . $property . "\"]/sv:value[text()%s%s]) > 0')";
+            $expression = sprintf("EXTRACTVALUE(%s.props, 'count(//sv:property[@sv:name=%s]/sv:value[text()%%s%%s]) > 0')", $alias, Xpath::escape($property));
             // mysql does not escape the backslashes for us, while postgres and sqlite do
             $value = Xpath::escapeBackslashes($value);
         } elseif ($this->platform instanceof PostgreSQL94Platform || $this->platform instanceof PostgreSqlPlatform) {
-            $expression = "xpath_exists('//sv:property[@sv:name=\"" . $property . "\"]/sv:value[text()%s%s]', CAST($alias.props AS xml), ".$this->sqlXpathPostgreSQLNamespaces().") = 't'";
+            $expression = sprintf("xpath_exists('//sv:property[@sv:name=%s]/sv:value[text()%s%s]', CAST(%%s.props AS xml), %%s) = 't'", Xpath::escape($property), $alias, $this->sqlXpathPostgreSQLNamespaces());
         } elseif ($this->platform instanceof SqlitePlatform) {
-            $expression = "EXTRACTVALUE($alias.props, 'count(//sv:property[@sv:name=\"" . $property . "\"]/sv:value[text()%s%s]) > 0')";
+            $expression = sprintf("EXTRACTVALUE(%s.props, 'count(//sv:property[@sv:name=%s]/sv:value[text()%%s%%s]) > 0')", $alias, Xpath::escape($property));
         } else {
-            throw new NotImplementedException("Xpath evaluations cannot be executed with '" . $this->platform->getName() . "' yet.");
+            throw new NotImplementedException(sprintf("Xpath evaluations cannot be executed with '%s' yet.", $this->platform->getName()));
         }
 
         return sprintf($expression, $this->walkOperator($operator), Xpath::escape($value));
@@ -1031,12 +1070,12 @@ private function sqlXpathPostgreSQLNamespaces()
      */
     private function sqlNodeTypeClause($alias, QOM\SelectorInterface $source)
     {
-        $sql = "$alias.type IN ('" . $source->getNodeTypeName() ."'";
+        $sql = sprintf("%s.type IN ('%s'", $alias, $source->getNodeTypeName());
 
         $subTypes = $this->nodeTypeManager->getSubtypes($source->getNodeTypeName());
         foreach ($subTypes as $subType) {
             /* @var $subType NodeTypeInterface */
-            $sql .= ", '" . $subType->getName() . "'";
+            $sql .= sprintf(", '%s'", $subType->getName());
         }
         $sql .= ')';
 
diff --git a/tests/Jackalope/Transport/DoctrineDBAL/ClientTest.php b/tests/Jackalope/Transport/DoctrineDBAL/ClientTest.php
index f5b2f6b2..5c57b422 100644
--- a/tests/Jackalope/Transport/DoctrineDBAL/ClientTest.php
+++ b/tests/Jackalope/Transport/DoctrineDBAL/ClientTest.php
@@ -9,9 +9,11 @@
 use Jackalope\Test\FunctionalTestCase;
 use PDO;
 use PHPCR\PropertyType;
+use PHPCR\Query\QOM\QueryObjectModelConstantsInterface;
 use PHPCR\Query\QueryInterface;
 use PHPCR\Util\NodeHelper;
 use PHPCR\Util\PathHelper;
+use PHPCR\Util\QOM\QueryBuilder;
 use PHPCR\ValueFormatException;
 use ReflectionClass;
 
@@ -435,6 +437,7 @@ public function provideOrder()
                         'value' => 'CCC',
                     ],
                 ],
+                'value',
                 'value DESC',
                 ['three', 'two', 'one'],
             ],
@@ -453,6 +456,7 @@ public function provideOrder()
                     ],
                 ],
                 'value',
+                'value',
                 ['three', 'two', 'one'],
             ],
 
@@ -470,6 +474,7 @@ public function provideOrder()
                     ],
                 ],
                 'value',
+                'value',
                 ['one', 'three', 'two'],
             ],
 
@@ -487,6 +492,7 @@ public function provideOrder()
                     ],
                 ],
                 'value',
+                'value',
                 ['two', 'three', 'one'],
             ],
 
@@ -510,16 +516,71 @@ public function provideOrder()
                         'value' => 5.05,
                     ],
                 ],
+                'value',
                 'title, value ASC',
                 ['two', 'one', 'four', 'three'],
             ],
+
+            // property with double quotes
+            [
+                [
+                    'one' => [
+                        'val"ue' => 'AAA',
+                    ],
+                    'two' => [
+                        'val"ue' => 'BBB',
+                    ],
+                    'three' => [
+                        'val"ue' => 'CCC',
+                    ],
+                ],
+                'val"ue',
+                'val"ue DESC',
+                ['three', 'two', 'one'],
+            ],
+
+            // property with single quotes
+            [
+                [
+                    'one' => [
+                        'val\'ue' => 'AAA',
+                    ],
+                    'two' => [
+                        'val\'ue' => 'BBB',
+                    ],
+                    'three' => [
+                        'val\'ue' => 'CCC',
+                    ],
+                ],
+                'val\'ue',
+                'val\'ue DESC',
+                ['three', 'two', 'one'],
+            ],
+
+            // property with semicolon quotes
+            [
+                [
+                    'one' => [
+                        'val;ue' => 'AAA',
+                    ],
+                    'two' => [
+                        'val;ue' => 'BBB',
+                    ],
+                    'three' => [
+                        'val;ue' => 'CCC',
+                    ],
+                ],
+                'val;ue',
+                'val;ue DESC',
+                ['three', 'two', 'one'],
+            ],
         ];
     }
 
     /**
      * @dataProvider provideOrder
      */
-    public function testOrder($nodes, $orderBy, $expectedOrder)
+    public function testOrder($nodes, $propertyName, $orderBy, $expectedOrder)
     {
         $rootNode = $this->session->getNode('/');
 
@@ -533,7 +594,30 @@ public function testOrder($nodes, $orderBy, $expectedOrder)
         $this->session->save();
 
         $qm = $this->session->getWorkspace()->getQueryManager();
-        $query = $qm->createQuery('SELECT * FROM [nt:unstructured] WHERE value IS NOT NULL ORDER BY ' . $orderBy, QueryInterface::JCR_SQL2);
+        $qf = $qm->getQOMFactory();
+        $qb = new QueryBuilder($qf);
+        $qb->from(
+            $qb->qomf()->selector('a', 'nt:unstructured')
+        );
+        $qb->where($qf->comparison(
+            $qf->propertyValue('a', $propertyName),
+            QueryObjectModelConstantsInterface::JCR_OPERATOR_NOT_EQUAL_TO,
+            $qf->literal('NULL')
+        ));
+
+        $orderBys = explode(',', $orderBy);
+        foreach ($orderBys as $orderByItem) {
+            $orderByParts = explode(' ', trim($orderByItem));
+            $propertyName = $orderByParts[0];
+            $order = isset($orderByParts[1]) ? $orderByParts[1] : 'ASC';
+
+            $qb->addOrderBy(
+                $qb->qomf()->propertyValue('a', $propertyName),
+                $order
+            );
+        }
+
+        $query = $qb->getQuery();
         $result = $query->execute();
 
         $rows = $result->getRows();