Skip to content

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 1 commit into from
Jul 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/PHPCR/Util/QOM/Sql2ToQomQueryConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ public function parse($sql2)
while ($this->scanner->lookupNextToken() !== '') {
switch (strtoupper($this->scanner->lookupNextToken())) {
case 'SELECT':
$this->scanner->expectToken('SELECT');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the SELECT and FROM expectToken calls to here, as with the other cases. This enabled me to use the logic from parseSource for the UPDATE query in phpcr-shell.

$columnData = $this->scanColumns();
break;
case 'FROM':
$this->scanner->expectToken('FROM');
$source = $this->parseSource();
break;
case 'WHERE':
Expand All @@ -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);
}
}

Expand All @@ -134,8 +136,6 @@ public function parse($sql2)
*/
protected function parseSource()
{
$this->scanner->expectToken('FROM');

$selector = $this->parseSelector();

$next = $this->scanner->lookupNextToken();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -782,7 +782,7 @@ protected function parseCastLiteral($token)
*
* @return LiteralInterface
*/
protected function parseLiteral()
protected function parseLiteralValue()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method now simply returns the literal value not the LiteralValueIntreface object - which is only used directly once anyway.

{
$token = $this->scanner->fetchNextToken();
if ($this->scanner->tokenIs($token, 'CAST')) {
Expand Down Expand Up @@ -825,7 +825,7 @@ protected function parseLiteral()
$token = false;
}

return $this->factory->literal($token);
return $token;
}

/**
Expand Down Expand Up @@ -882,8 +882,6 @@ protected function parseOrdering()
*/
protected function scanColumns()
{
$this->scanner->expectToken('SELECT');

// Wildcard
if ($this->scanner->lookupNextToken() === '*') {
$this->scanner->fetchNextToken();
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPCR/Tests/Util/QOM/Sql2ToQomQueryConverterTest.php
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');
}
}