Skip to content

Commit f2f87d2

Browse files
committed
Merge pull request #144 from phpcr/cleanup-query-converter
cleanup the query converter a bit
2 parents 8f056e0 + 7dbd80b commit f2f87d2

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

src/PHPCR/Util/QOM/Sql2ToQomQueryConverter.php

+54-40
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
use PHPCR\Query\QOM\EquiJoinConditionInterface;
1414
use PHPCR\Query\QOM\FullTextSearchInterface;
1515
use PHPCR\Query\QOM\JoinConditionInterface;
16-
use PHPCR\Query\QOM\LiteralInterface;
16+
use PHPCR\Query\QOM\JoinInterface;
1717
use PHPCR\Query\QOM\NotInterface;
18+
use PHPCR\Query\QOM\OrderingInterface;
1819
use PHPCR\Query\QOM\PropertyExistenceInterface;
1920
use PHPCR\Query\QOM\PropertyValueInterface;
2021
use PHPCR\Query\QOM\QueryObjectModelConstantsInterface as Constants;
@@ -68,6 +69,7 @@ class Sql2ToQomQueryConverter
6869
* Instantiate a converter
6970
*
7071
* @param QueryObjectModelFactoryInterface $factory
72+
* @param ValueConverter $valueConverter To override default converter.
7173
*/
7274
public function __construct(QueryObjectModelFactoryInterface $factory, ValueConverter $valueConverter = null)
7375
{
@@ -183,10 +185,11 @@ protected function parseName()
183185
* 6.7.6. Join type
184186
* Parse an SQL2 join source and return a QOM\Join
185187
*
186-
* @param string $leftSelector the left selector as it has been read by parseSource
187-
* return \PHPCR\Query\QOM\JoinInterface
188+
* @param SourceInterface $leftSelector the left selector as it has been read by parseSource
189+
*
190+
* @return JoinInterface
188191
*/
189-
protected function parseJoin($leftSelector)
192+
protected function parseJoin(SourceInterface $leftSelector)
190193
{
191194
$joinType = $this->parseJoinType();
192195
$right = $this->parseSelector();
@@ -332,7 +335,12 @@ protected function parseDescendantNodeJoinCondition()
332335
* 6.7.13 And
333336
* 6.7.14 Or
334337
*
338+
* @param ConstraintInterface $lhs Left hand side
339+
* @param int $minprec Precedence
340+
*
335341
* @return ConstraintInterface
342+
*
343+
* @throws \Exception
336344
*/
337345
protected function parseConstraint($lhs = null, $minprec = 0)
338346
{
@@ -728,52 +736,54 @@ protected function parsePropertyValue()
728736

729737
protected function parseCastLiteral($token)
730738
{
731-
if ($this->scanner->tokenIs($token, 'CAST')) {
732-
$this->scanner->expectToken('(');
733-
$token = $this->scanner->fetchNextToken();
739+
if (!$this->scanner->tokenIs($token, 'CAST')) {
740+
throw new \LogicException('parseCastLiteral when not a CAST');
741+
}
734742

735-
$quoteString = false;
736-
if (substr($token, 0, 1) === '\'') {
737-
$quoteString = "'";
738-
} elseif (substr($token, 0, 1) === '"') {
739-
$quoteString = '"';
740-
}
743+
$this->scanner->expectToken('(');
744+
$token = $this->scanner->fetchNextToken();
741745

742-
if ($quoteString) {
743-
while (substr($token, -1) !== $quoteString) {
744-
$nextToken = $this->scanner->fetchNextToken();
745-
if ('' === $nextToken) {
746-
break;
747-
}
748-
$token .= $nextToken;
749-
}
746+
$quoteString = false;
747+
if (substr($token, 0, 1) === '\'') {
748+
$quoteString = "'";
749+
} elseif (substr($token, 0, 1) === '"') {
750+
$quoteString = '"';
751+
}
750752

751-
if (substr($token, -1) !== $quoteString) {
752-
throw new InvalidQueryException("Syntax error: unterminated quoted string '$token' in '{$this->sql2}'");
753+
if ($quoteString) {
754+
while (substr($token, -1) !== $quoteString) {
755+
$nextToken = $this->scanner->fetchNextToken();
756+
if ('' === $nextToken) {
757+
break;
753758
}
754-
$token = substr($token, 1, -1);
755-
$token = str_replace('\\'.$quoteString, $quoteString, $token);
759+
$token .= $nextToken;
756760
}
757761

758-
$this->scanner->expectToken('AS');
759-
760-
$type = $this->scanner->fetchNextToken();
761-
try {
762-
$typeValue = PropertyType::valueFromName($type);
763-
} catch (\InvalidArgumentException $e) {
764-
throw new InvalidQueryException("Syntax error: attempting to cast to an invalid type '$type'");
762+
if (substr($token, -1) !== $quoteString) {
763+
throw new InvalidQueryException("Syntax error: unterminated quoted string '$token' in '{$this->sql2}'");
765764
}
765+
$token = substr($token, 1, -1);
766+
$token = str_replace('\\'.$quoteString, $quoteString, $token);
767+
}
766768

767-
$this->scanner->expectToken(')');
769+
$this->scanner->expectToken('AS');
768770

769-
try {
770-
$token = $this->valueConverter->convertType($token, $typeValue, PropertyType::STRING);
771-
} catch (\Exception $e) {
772-
throw new InvalidQueryException("Syntax error: attempting to cast string '$token' to type '$type'");
773-
}
771+
$type = $this->scanner->fetchNextToken();
772+
try {
773+
$typeValue = PropertyType::valueFromName($type);
774+
} catch (\InvalidArgumentException $e) {
775+
throw new InvalidQueryException("Syntax error: attempting to cast to an invalid type '$type'");
776+
}
777+
778+
$this->scanner->expectToken(')');
774779

775-
return $token;
780+
try {
781+
$token = $this->valueConverter->convertType($token, $typeValue, PropertyType::STRING);
782+
} catch (\Exception $e) {
783+
throw new InvalidQueryException("Syntax error: attempting to cast string '$token' to type '$type'");
776784
}
785+
786+
return $token;
777787
}
778788

779789
/**
@@ -850,6 +860,8 @@ protected function parseOrderings()
850860

851861
/**
852862
* 6.7.38 Order
863+
*
864+
* @return OrderingInterface
853865
*/
854866
protected function parseOrdering()
855867
{
@@ -1044,9 +1056,11 @@ protected function scanColumn()
10441056
/**
10451057
* Build a single SQL2 column definition
10461058
*
1059+
* @param array $data with selector name, property name and column name.
1060+
*
10471061
* @return ColumnInterface
10481062
*/
1049-
protected function buildColumn($data)
1063+
protected function buildColumn(array $data)
10501064
{
10511065
list($selectorName, $propertyName, $columnName) = $data;
10521066
$selectorName = $this->ensureSelectorName($selectorName);

0 commit comments

Comments
 (0)