-
Notifications
You must be signed in to change notification settings - Fork 29
Refactored Converter to make it more extensible #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,9 +96,11 @@ public function parse($sql2) | |
while ($this->scanner->lookupNextToken() !== '') { | ||
switch (strtoupper($this->scanner->lookupNextToken())) { | ||
case 'SELECT': | ||
$this->scanner->expectToken('SELECT'); | ||
$columnData = $this->scanColumns(); | ||
break; | ||
case 'FROM': | ||
$this->scanner->expectToken('FROM'); | ||
$source = $this->parseSource(); | ||
break; | ||
case 'WHERE': | ||
|
@@ -111,7 +113,7 @@ public function parse($sql2) | |
$orderings = $this->parseOrderings(); | ||
break; | ||
default: | ||
throw new InvalidQueryException('Expected end of query, got ' . $this->scanner->lookupNextToken() . ' in ' . $this->sql2); | ||
throw new InvalidQueryException('Error parsing query, unknown query part "' . $this->scanner->lookupNextToken() . '" in: ' . $this->sql2); | ||
} | ||
} | ||
|
||
|
@@ -134,8 +136,6 @@ public function parse($sql2) | |
*/ | ||
protected function parseSource() | ||
{ | ||
$this->scanner->expectToken('FROM'); | ||
|
||
$selector = $this->parseSelector(); | ||
|
||
$next = $this->scanner->lookupNextToken(); | ||
|
@@ -528,7 +528,7 @@ protected function parseFullTextSearch() | |
|
||
list($selectorName, $propertyName) = $this->parseIdentifier(); | ||
$this->scanner->expectToken(','); | ||
$expression = $this->parseLiteral()->getLiteralValue(); | ||
$expression = $this->parseLiteralValue(); | ||
$this->scanner->expectToken(')'); | ||
|
||
return $this->factory->fullTextSearch($selectorName, $propertyName, $expression); | ||
|
@@ -600,7 +600,7 @@ protected function parseDescendantNode() | |
*/ | ||
protected function parsePath() | ||
{ | ||
$path = $this->parseLiteral()->getLiteralValue(); | ||
$path = $this->parseLiteralValue(); | ||
if (substr($path, 0, 1) === '[' && substr($path, -1) === ']') { | ||
$path = substr($path, 1, -1); | ||
} | ||
|
@@ -622,7 +622,7 @@ protected function parseStaticOperand() | |
return $this->factory->bindVariable(substr($this->scanner->fetchNextToken(), 1)); | ||
} | ||
|
||
return $this->parseLiteral(); | ||
return $this->factory->literal($this->parseLiteralValue()); | ||
} | ||
|
||
/** | ||
|
@@ -749,7 +749,7 @@ protected function parseCastLiteral($token) | |
} | ||
|
||
if (substr($token, -1) !== $quoteString) { | ||
throw new InvalidQueryException("Syntax error: unterminated quoted string $token in '{$this->sql2}'"); | ||
throw new InvalidQueryException("Syntax error: unterminated quoted string '$token' in '{$this->sql2}'"); | ||
} | ||
$token = substr($token, 1, -1); | ||
$token = str_replace('\\'.$quoteString, $quoteString, $token); | ||
|
@@ -782,7 +782,7 @@ protected function parseCastLiteral($token) | |
* | ||
* @return LiteralInterface | ||
*/ | ||
protected function parseLiteral() | ||
protected function parseLiteralValue() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method now simply returns the literal value not the |
||
{ | ||
$token = $this->scanner->fetchNextToken(); | ||
if ($this->scanner->tokenIs($token, 'CAST')) { | ||
|
@@ -825,7 +825,7 @@ protected function parseLiteral() | |
$token = false; | ||
} | ||
|
||
return $this->factory->literal($token); | ||
return $token; | ||
} | ||
|
||
/** | ||
|
@@ -882,8 +882,6 @@ protected function parseOrdering() | |
*/ | ||
protected function scanColumns() | ||
{ | ||
$this->scanner->expectToken('SELECT'); | ||
|
||
// Wildcard | ||
if ($this->scanner->lookupNextToken() === '*') { | ||
$this->scanner->fetchNextToken(); | ||
|
27 changes: 27 additions & 0 deletions
27
tests/PHPCR/Tests/Util/QOM/Sql2ToQomQueryConverterTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace PHPCR\Tests\Util\QOM; | ||
|
||
use PHPCR\Util\QOM\Sql2ToQomQueryConverter; | ||
|
||
class Sql2ToQomQueryConverterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $qomFactory; | ||
protected $valueConverter; | ||
|
||
public function setUp() | ||
{ | ||
$this->qomFactory = $this->getMock('PHPCR\Query\QOM\QueryObjectModelFactoryInterface'); | ||
$this->valueConverter = $this->getMock('PHPCR\Util\ValueConverter'); | ||
$this->converter = new Sql2ToQomQueryConverter($this->qomFactory, $this->valueConverter); | ||
} | ||
|
||
/** | ||
* @expectedException \PHPCR\Query\InvalidQueryException | ||
* @expectedExceptionMessage Error parsing query | ||
*/ | ||
public function testInvalid() | ||
{ | ||
$this->converter->parse('SELECTING WITH AN INVALID QUERY'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the
SELECT
andFROM
expectToken
calls to here, as with the other cases. This enabled me to use the logic fromparseSource
for theUPDATE
query in phpcr-shell.