Skip to content

Commit

Permalink
handle escaping fulltext search literal when converting from/to QOM/SQL2
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmith77 committed Jun 17, 2014
1 parent 37d4953 commit 25e62cd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

1.1.1
-----

* **2014-06-11**: handle escaping fulltext search literal when converting from/to QOM/SQL2

1.1.0
-----

Expand Down
5 changes: 2 additions & 3 deletions src/PHPCR/Util/QOM/BaseQomToSqlQueryConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,8 @@ protected function convertFullTextSearchExpression($expr)
if ($expr instanceof QOM\BindVariableValueInterface) {
return $this->convertBindVariable($expr);
}
if ($expr instanceof QOM\LiteralInterface) {
return $this->convertLiteral($expr);
}

$expr = $this->generator->evalFullText($expr);

return "'$expr'";
}
Expand Down
20 changes: 20 additions & 0 deletions src/PHPCR/Util/QOM/BaseSqlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,26 @@ public function evalBindVariable($var)
return '$' . $var;
}

/**
* Escape the illegal characters for inclusion in a fulltext statement. Escape Character is \\.
*
* @param string $string
*
* @return string Escaped String
*
* @see http://jackrabbit.apache.org/api/1.4/org/apache/jackrabbit/util/Text.html #escapeIllegalJcrChars
*/
public function evalFullText($string)
{
$illegalCharacters = array(
'!' => '\\!', '(' => '\\(', ':' => '\\:', '^' => '\\^',
'[' => '\\[', ']' => '\\]', '{' => '\\{', '}' => '\\}',
'\"' => '\\\"', '?' => '\\?', "'" => "''",
);

return strtr($string, $illegalCharacters);
}

/**
* Literal ::= CastLiteral | UncastLiteral
*
Expand Down
48 changes: 48 additions & 0 deletions src/PHPCR/Util/QOM/Sql2ToQomQueryConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ protected function parseLiteral()
}
$token = substr($token, 1, -1);
$token = str_replace('\\'.$quoteString, $quoteString, $token);
$token = str_replace("''", "'", $token);
if (preg_match('/^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d+)?$/', $token)) {
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $token)) {
$token.= ' 00:00:00';
Expand All @@ -828,6 +829,53 @@ protected function parseLiteral()
return $this->factory->literal($token);
}

/**
* 6.7.34 Fulltext Literal
* Parse an SQL2 literal value
*
* @return LiteralInterface
*/
protected function parseFulltextLiteral()
{
$token = $this->scanner->fetchNextToken();
if ($this->scanner->tokenIs($token, 'CAST')) {
return $this->parseCastLiteral($token);
}

$quoteString = false;
if (substr($token, 0, 1) === '\'') {
$quoteString = "'";
} elseif (substr($token, 0, 1) === '"') {
$quoteString = '"';
}

if ($quoteString) {
while (substr($token, -1) !== $quoteString) {
$nextToken = $this->scanner->fetchNextToken();
if ('' === $nextToken) {
break;
}
$token .= $this->scanner->getPreviousDelimiter();
$token .= $nextToken;
}

if (substr($token, -1) !== $quoteString) {
throw new InvalidQueryException("Syntax error: unterminated quoted string $token in '{$this->sql2}'");
}
$token = substr($token, 1, -1);
$token = str_replace('\\'.$quoteString, $quoteString, $token);
$illegalCharacters = array(
'\\!' => '!', '\\(' => '(', '\\:' => ':', '\\^' => '^',
'\\[' => '[', '\\]' => ']', '\\{' => '{', '\\}' => '}',
'\\\"' => '\"', '\\?' => '?', "''" => "'",
);

$token = strtr($token, $illegalCharacters);

}

return $this->factory->literal($token);
}
/**
* 6.7.37 Ordering
*/
Expand Down

0 comments on commit 25e62cd

Please sign in to comment.