Skip to content

Commit 11cac16

Browse files
committed
Merge pull request #86 from phpcr/no-empty-selector
qom always has a selector name
2 parents 4576947 + 209cba2 commit 11cac16

9 files changed

+244
-116
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
],
2929
"require": {
3030
"php": ">=5.3.0",
31-
"phpcr/phpcr": "~2.1.0-beta10",
31+
"phpcr/phpcr": "~2.1.0-RC1",
3232
"symfony/console": "~2.0"
3333
},
3434
"autoload": {

src/PHPCR/Util/CND/Scanner/AbstractScanner.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function applyFilters(Token $token)
4141

4242
$token = $filter->filter($token);
4343

44-
if (is_null($token)) {
44+
if (null === $token) {
4545
break;
4646
}
4747
}

src/PHPCR/Util/QOM/QomToSql2QueryConverter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function convertSameNodeJoinCondition(QOM\SameNodeJoinConditionInterfa
116116
return $this->generator->evalSameNodeJoinCondition(
117117
$condition->getSelector1Name(),
118118
$condition->getSelector2Name(),
119-
! is_null($condition->getSelector2Path()) ? $this->convertPath($condition->getSelector2Path()) : null);
119+
null !== $condition->getSelector2Path() ? $this->convertPath($condition->getSelector2Path()) : null);
120120
}
121121

122122
/**

src/PHPCR/Util/QOM/QueryBuilder.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -352,33 +352,33 @@ public function setColumns(array $columns)
352352
* Identifies a property in the specified or default selector to include in the tabular view of query results.
353353
* Replaces any previously specified columns to be selected if any.
354354
*
355+
* @param string $selectorName
355356
* @param string $propertyName
356357
* @param string $columnName
357-
* @param string $selectorName
358358
*
359359
* @return QueryBuilder This QueryBuilder instance.
360360
*/
361-
public function select($propertyName, $columnName = null, $selectorName = null)
361+
public function select($selectorName, $propertyName, $columnName = null)
362362
{
363363
$this->state = self::STATE_DIRTY;
364-
$this->columns = array($this->qomFactory->column($propertyName, $columnName, $selectorName));
364+
$this->columns = array($this->qomFactory->column($selectorName, $propertyName, $columnName));
365365

366366
return $this;
367367
}
368368

369369
/**
370370
* Adds a property in the specified or default selector to include in the tabular view of query results.
371371
*
372+
* @param string $selectorName
372373
* @param string $propertyName
373374
* @param string $columnName
374-
* @param string $selectorName
375375
*
376376
* @return QueryBuilder This QueryBuilder instance.
377377
*/
378-
public function addSelect($propertyName, $columnName = null, $selectorName = null)
378+
public function addSelect($selectorName, $propertyName, $columnName = null)
379379
{
380380
$this->state = self::STATE_DIRTY;
381-
$this->columns[] = $this->qomFactory->column($propertyName, $columnName, $selectorName);
381+
$this->columns[] = $this->qomFactory->column($selectorName, $propertyName, $columnName);
382382

383383
return $this;
384384
}

src/PHPCR/Util/QOM/Sql2Generator.php

+20-12
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@ class Sql2Generator extends BaseSqlGenerator
1717
* Selector ::= nodeTypeName ['AS' selectorName]
1818
* nodeTypeName ::= Name
1919
*
20-
* @param string $nodeTypeName The node type of the selector. If it does not contain starting and ending brackets ([]) they will be added automatically
21-
* @param string $selectorName
20+
* @param string $nodeTypeName The node type of the selector. If it
21+
* does not contain starting and ending brackets ([]) they will be
22+
* added automatically.
23+
* @param string|null $selectorName The selector name. If it is different than
24+
* the nodeTypeName, the alias is declared.
2225
*
2326
* @return string
2427
*/
2528
public function evalSelector($nodeTypeName, $selectorName = null)
2629
{
2730
$sql2 = $this->addBracketsIfNeeded($nodeTypeName);
2831

29-
$name = $selectorName;
30-
if (! is_null($name)) {
31-
$sql2 .= ' AS ' . $name;
32+
if (null !== $selectorName && $nodeTypeName !== $selectorName) {
33+
// if the selector name is the same as the type name, this is implicit for sql2
34+
$sql2 .= ' AS ' . $selectorName;
3235
}
3336

3437
return $sql2;
@@ -112,7 +115,9 @@ public function evalSameNodeJoinCondition($sel1Name, $sel2Name, $sel2Path = null
112115
. $this->addBracketsIfNeeded($sel1Name) . ', '
113116
. $this->addBracketsIfNeeded($sel2Name)
114117
;
115-
$sql2 .= ! is_null($sel2Path) ? ', ' . $sel2Path : '';
118+
if (null !== $sel2Path) {
119+
$sql2 .= ', ' . $sel2Path;
120+
}
116121
$sql2 .= ')';
117122

118123
return $sql2;
@@ -165,7 +170,7 @@ public function evalDescendantNodeJoinCondition($descendantSelectorName, $ancest
165170
public function evalSameNode($path, $selectorName = null)
166171
{
167172
$sql2 = 'ISSAMENODE(';
168-
$sql2 .= is_null($selectorName) ? $path : $this->addBracketsIfNeeded($selectorName) . ', ' . $path;
173+
$sql2 .= null === $selectorName ? $path : $this->addBracketsIfNeeded($selectorName) . ', ' . $path;
169174
$sql2 .= ')';
170175

171176
return $sql2;
@@ -180,7 +185,7 @@ public function evalSameNode($path, $selectorName = null)
180185
public function evalChildNode($path, $selectorName = null)
181186
{
182187
$sql2 = 'ISCHILDNODE(';
183-
$sql2 .= is_null($selectorName) ? $path : $this->addBracketsIfNeeded($selectorName) . ', ' . $path;
188+
$sql2 .= null === $selectorName ? $path : $this->addBracketsIfNeeded($selectorName) . ', ' . $path;
184189
$sql2 .= ')';
185190

186191
return $sql2;
@@ -195,7 +200,7 @@ public function evalChildNode($path, $selectorName = null)
195200
public function evalDescendantNode($path, $selectorName = null)
196201
{
197202
$sql2 = 'ISDESCENDANTNODE(';
198-
$sql2 .= is_null($selectorName) ? $path : $this->addBracketsIfNeeded($selectorName) . ', ' . $path;
203+
$sql2 .= null === $selectorName ? $path : $this->addBracketsIfNeeded($selectorName) . ', ' . $path;
199204
$sql2 .= ')';
200205

201206
return $sql2;
@@ -289,7 +294,7 @@ public function evalFullTextSearchScore($selectorValue = null)
289294
*/
290295
public function evalPropertyValue($propertyName, $selectorName = null)
291296
{
292-
$sql2 = ! is_null($selectorName) ? $this->addBracketsIfNeeded($selectorName) . '.' : '';
297+
$sql2 = null !== $selectorName ? $this->addBracketsIfNeeded($selectorName) . '.' : '';
293298
if (false !== strpos($propertyName, ':')) {
294299
$propertyName = "[$propertyName]";
295300
}
@@ -340,11 +345,14 @@ public function evalColumns($columns)
340345
public function evalColumn($selectorName, $propertyName = null, $colname = null)
341346
{
342347
$sql2 = '';
343-
if (! is_null($selectorName) && is_null($propertyName) && is_null($colname)) {
348+
if (null !== $selectorName && null === $propertyName && null === $colname) {
344349
$sql2 .= $this->addBracketsIfNeeded($selectorName) . '.*';
345350
} else {
346351
$sql2 .= $this->evalPropertyValue($propertyName, $selectorName);
347-
$sql2 .= ! is_null($colname) ? ' AS ' . $colname : '';
352+
if (null !== $colname && $colname !== $propertyName) {
353+
// if the column name is the same as the property name, this is implicit for sql2
354+
$sql2 .= ' AS ' . $colname;
355+
}
348356
}
349357

350358
return $sql2;

src/PHPCR/Util/QOM/Sql2Scanner.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PHPCR\Util\QOM;
44

5+
use PHPCR\Query\InvalidQueryException;
6+
57
/**
68
* Split an SQL2 statement into string tokens. Allows lookup and fetching of tokens.
79
*
@@ -104,7 +106,7 @@ public function expectToken($token, $case_insensitive = true)
104106
{
105107
$nextToken = $this->fetchNextToken();
106108
if (! $this->tokenIs($nextToken, $token, $case_insensitive)) {
107-
throw new \Exception("Syntax error: Expected $token, found $nextToken");
109+
throw new InvalidQueryException("Syntax error: Expected '$token', found '$nextToken' in {$this->sql2}");
108110
}
109111
}
110112

0 commit comments

Comments
 (0)