Skip to content

Commit 717e75e

Browse files
committed
Merge pull request #118 from phpcr/made_sql2_query_converter_more_extendable
Refactored Converter to make it more extensible
2 parents a485fd1 + d9eeb1f commit 717e75e

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/PHPCR/Util/QOM/Sql2ToQomQueryConverter.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ public function parse($sql2)
9696
while ($this->scanner->lookupNextToken() !== '') {
9797
switch (strtoupper($this->scanner->lookupNextToken())) {
9898
case 'SELECT':
99+
$this->scanner->expectToken('SELECT');
99100
$columnData = $this->scanColumns();
100101
break;
101102
case 'FROM':
103+
$this->scanner->expectToken('FROM');
102104
$source = $this->parseSource();
103105
break;
104106
case 'WHERE':
@@ -111,7 +113,7 @@ public function parse($sql2)
111113
$orderings = $this->parseOrderings();
112114
break;
113115
default:
114-
throw new InvalidQueryException('Expected end of query, got ' . $this->scanner->lookupNextToken() . ' in ' . $this->sql2);
116+
throw new InvalidQueryException('Error parsing query, unknown query part "' . $this->scanner->lookupNextToken() . '" in: ' . $this->sql2);
115117
}
116118
}
117119

@@ -134,8 +136,6 @@ public function parse($sql2)
134136
*/
135137
protected function parseSource()
136138
{
137-
$this->scanner->expectToken('FROM');
138-
139139
$selector = $this->parseSelector();
140140

141141
$next = $this->scanner->lookupNextToken();
@@ -528,7 +528,7 @@ protected function parseFullTextSearch()
528528

529529
list($selectorName, $propertyName) = $this->parseIdentifier();
530530
$this->scanner->expectToken(',');
531-
$expression = $this->parseLiteral()->getLiteralValue();
531+
$expression = $this->parseLiteralValue();
532532
$this->scanner->expectToken(')');
533533

534534
return $this->factory->fullTextSearch($selectorName, $propertyName, $expression);
@@ -600,7 +600,7 @@ protected function parseDescendantNode()
600600
*/
601601
protected function parsePath()
602602
{
603-
$path = $this->parseLiteral()->getLiteralValue();
603+
$path = $this->parseLiteralValue();
604604
if (substr($path, 0, 1) === '[' && substr($path, -1) === ']') {
605605
$path = substr($path, 1, -1);
606606
}
@@ -622,7 +622,7 @@ protected function parseStaticOperand()
622622
return $this->factory->bindVariable(substr($this->scanner->fetchNextToken(), 1));
623623
}
624624

625-
return $this->parseLiteral();
625+
return $this->factory->literal($this->parseLiteralValue());
626626
}
627627

628628
/**
@@ -749,7 +749,7 @@ protected function parseCastLiteral($token)
749749
}
750750

751751
if (substr($token, -1) !== $quoteString) {
752-
throw new InvalidQueryException("Syntax error: unterminated quoted string $token in '{$this->sql2}'");
752+
throw new InvalidQueryException("Syntax error: unterminated quoted string '$token' in '{$this->sql2}'");
753753
}
754754
$token = substr($token, 1, -1);
755755
$token = str_replace('\\'.$quoteString, $quoteString, $token);
@@ -782,7 +782,7 @@ protected function parseCastLiteral($token)
782782
*
783783
* @return LiteralInterface
784784
*/
785-
protected function parseLiteral()
785+
protected function parseLiteralValue()
786786
{
787787
$token = $this->scanner->fetchNextToken();
788788
if ($this->scanner->tokenIs($token, 'CAST')) {
@@ -826,7 +826,7 @@ protected function parseLiteral()
826826
$token = false;
827827
}
828828

829-
return $this->factory->literal($token);
829+
return $token;
830830
}
831831

832832
/**
@@ -930,8 +930,6 @@ protected function parseOrdering()
930930
*/
931931
protected function scanColumns()
932932
{
933-
$this->scanner->expectToken('SELECT');
934-
935933
// Wildcard
936934
if ($this->scanner->lookupNextToken() === '*') {
937935
$this->scanner->fetchNextToken();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace PHPCR\Tests\Util\QOM;
4+
5+
use PHPCR\Util\QOM\Sql2ToQomQueryConverter;
6+
7+
class Sql2ToQomQueryConverterTest extends \PHPUnit_Framework_TestCase
8+
{
9+
protected $qomFactory;
10+
protected $valueConverter;
11+
12+
public function setUp()
13+
{
14+
$this->qomFactory = $this->getMock('PHPCR\Query\QOM\QueryObjectModelFactoryInterface');
15+
$this->valueConverter = $this->getMock('PHPCR\Util\ValueConverter');
16+
$this->converter = new Sql2ToQomQueryConverter($this->qomFactory, $this->valueConverter);
17+
}
18+
19+
/**
20+
* @expectedException \PHPCR\Query\InvalidQueryException
21+
* @expectedExceptionMessage Error parsing query
22+
*/
23+
public function testInvalid()
24+
{
25+
$this->converter->parse('SELECTING WITH AN INVALID QUERY');
26+
}
27+
}

0 commit comments

Comments
 (0)