Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f199b74
Initial simpleQuery support
chregu Apr 12, 2012
837df30
More simpleQuery Support
chregu Apr 12, 2012
d1ff146
Remove the isSimple stuff from the QueryBuilder.
chregu Apr 12, 2012
7fa07b3
revert WS only changes from playing around
chregu Apr 13, 2012
d2f36f7
Fix an issue, if there's no selector in PropertyExistence
chregu Apr 13, 2012
8d0c6af
Remove not supported code
chregu Apr 13, 2012
86c9bc3
remove selector in propertyexistance
chregu Apr 13, 2012
379aef2
trailing WS only
chregu Apr 13, 2012
a180e9f
Add parenthesis to the output of SQL2
chregu Apr 15, 2012
77cc94d
Merge branch 'refs/heads/master' into addParenthesis
chregu Apr 15, 2012
f6e3e37
Merge branch 'refs/heads/addParenthesis' into QomToSql1
chregu Apr 15, 2012
5a92f3a
Get rid of all selector references. Not needed in SQL1
chregu Apr 15, 2012
c2c85d9
Added Support for ChildNodeConstraint and DescendantNodeConstrain…
chregu Apr 15, 2012
82e5e81
Removed not supported Operands
chregu Apr 16, 2012
5dae0f2
trim ' from path
chregu Apr 16, 2012
d7bf013
Merge branch 'refs/heads/master' into QomToSql1
chregu Apr 18, 2012
c1ab808
Revert "Add parenthesis to the output of SQL2"
chregu Apr 19, 2012
d340fcf
Cast DATE to TIMESTAMP (more to come)
chregu Apr 19, 2012
aeb0549
Added Bool Test and Tests for SQL1
chregu Apr 19, 2012
38e7a5f
Moved common methods to SqlGenerator
chregu Apr 19, 2012
b9a2183
Fix LONG and DOUBLE conversion in QOM to SQL
chregu Apr 19, 2012
5d411e4
readd a method
chregu Apr 19, 2012
0dc3824
Renamed SqlGenerator to BaseSqlGenerator and make it abstract
chregu Apr 19, 2012
277b862
Megre QomToSql(1|2)QueryConverter into a base class
chregu Apr 19, 2012
d874297
Added more tests
chregu Apr 19, 2012
7d4b8ad
CS fixes
lsmith77 Apr 19, 2012
19751cd
CS tweak
lsmith77 Apr 19, 2012
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
249 changes: 249 additions & 0 deletions src/PHPCR/Util/QOM/BaseQomToSqlQueryConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
<?php

namespace PHPCR\Util\QOM;

use PHPCR\Query\QOM;
use PHPCR\Query\QOM\QueryObjectModelConstantsInterface as Constants;

abstract class BaseQomToSqlQueryConverter {

/**
* @var \PHPCR\Util\QOM\Sql2Generator
*/
protected $generator;

public function __construct(BaseSqlGenerator $generator)
{
$this->generator = $generator;
}

/**
* Query ::= 'SELECT' columns
* 'FROM' Source
* ['WHERE' Constraint]
* ['ORDER BY' orderings]
*
* @param \PHPCR\Query\QOM\QueryObjectModelInterface $query
* @return string
*/
public function convert(QOM\QueryObjectModelInterface $query)
{
$columns = $this->convertColumns($query->getColumns());
$source = $this->convertSource($query->getSource());
$constraint = '';
$orderings = '';

if ($query->getConstraint() !== null) {
$constraint = $this->convertConstraint($query->getConstraint());
}

if (count($query->getOrderings())) {
$orderings = $this->convertOrderings($query->getOrderings());
}

return $this->generator->evalQuery($source, $columns, $constraint, $orderings);
}

/**
* Selector ::= nodeTypeName ['AS' selectorName]
* nodeTypeName ::= Name
*
* @param \PHPCR\Query\QOM\SelectorInterface $selector
* @return string
*/
protected function convertSelector(QOM\SelectorInterface $selector)
{
return $this->generator->evalSelector($selector->getNodeTypeName(), $selector->getSelectorName());
}

/**
* Comparison ::= DynamicOperand Operator StaticOperand
*
* Operator ::= EqualTo | NotEqualTo | LessThan |
* LessThanOrEqualTo | GreaterThan |
* GreaterThanOrEqualTo | Like
* EqualTo ::= '='
* NotEqualTo ::= '<>'
* LessThan ::= '<'
* LessThanOrEqualTo ::= '<='
* GreaterThan ::= '>'
* GreaterThanOrEqualTo ::= '>='
* Like ::= 'LIKE'
*
* @param \PHPCR\Query\QOM\ComparisonInterface $comparison
* @return string
*/
protected function convertComparison(QOM\ComparisonInterface $comparison)
{
$operand1 = $this->convertDynamicOperand($comparison->getOperand1());
$operand2 = $this->convertStaticOperand($comparison->getOperand2());
$operator = $this->generator->evalOperator($comparison->getOperator());

return $this->generator->evalComparison($operand1, $operator, $operand2);
}

/**
* PropertyExistence ::=
* selectorName'.'propertyName 'IS NOT NULL' |
* propertyName 'IS NOT NULL' If only one
* selector exists in
* this query
*
* Note: The negation, 'NOT x IS NOT NULL'
* can be written 'x IS NULL'
*
* @param \PHPCR\Query\QOM\PropertyExistenceInterface $constraint
* @return string
*/
protected function convertPropertyExistence(QOM\PropertyExistenceInterface $constraint)
{
return $this->generator->evalPropertyExistence(
$constraint->getSelectorName(),
$constraint->getPropertyName());
}

/**
* FullTextSearch ::=
* 'CONTAINS(' ([selectorName'.']propertyName |
* selectorName'.*') ','
* FullTextSearchExpression ')'
* // If only one selector exists in this query,
* explicit specification of the selectorName
* preceding the propertyName is optional
*
* @param \PHPCR\Query\QOM\FullTextSearchInterface $constraint
* @return string
*/
protected function convertFullTextSearch(QOM\FullTextSearchInterface $constraint)
{
$searchExpression = $this->convertFullTextSearchExpression($constraint->getFullTextSearchExpression());
return $this->generator->evalFullTextSearch($constraint->getSelectorName(), $searchExpression, $constraint->getPropertyName());
}

/**
* FullTextSearchExpression ::= BindVariable | ''' FullTextSearchLiteral '''
*
* @param string $expr
* @return string
*/
protected function convertFullTextSearchExpression($literal)
{
if ($literal instanceof QOM\BindVariableValue) {
return $this->convertBindVariable($literal);
}
if ($literal instanceof QOM\Literal) {
return $this->convertLiteral($literal);
}

return "'$literal'";
}


/**
* StaticOperand ::= Literal | BindVariableValue
*
* Literal ::= CastLiteral | UncastLiteral
* CastLiteral ::= 'CAST(' UncastLiteral ' AS ' PropertyType ')'
*
* PropertyType ::= 'STRING' | 'BINARY' | 'DATE' | 'LONG' | 'DOUBLE' |
* 'DECIMAL' | 'BOOLEAN' | 'NAME' | 'PATH' |
* 'REFERENCE' | 'WEAKREFERENCE' | 'URI'
* UncastLiteral ::= UnquotedLiteral | ''' UnquotedLiteral ''' | '“' UnquotedLiteral '“'
* UnquotedLiteral ::= // String form of a JCR Value
*
* BindVariableValue ::= '$'bindVariableName
* bindVariableName ::= Prefix
*
* @param \PHPCR\Query\QOM\StaticOperandInterface $operand
* @return string
*/
protected function convertStaticOperand(QOM\StaticOperandInterface $operand)
{
if ($operand instanceof QOM\BindVariableValueInterface) {
return $this->convertBindVariable($operand->getBindVariableName());
}
if ($operand instanceof QOM\LiteralInterface) {
return $this->convertLiteral($operand->getLiteralValue());
}

// This should not happen, but who knows...
throw new \InvalidArgumentException("Invalid operand");
}

/**
* PropertyValue ::= [selectorName'.'] propertyName // If only one selector exists
*
* @param \PHPCR\Query\QOM\PropertyValueInterface $value
* @return string
*/
protected function convertPropertyValue(QOM\PropertyValueInterface $operand)
{
return $this->generator->evalPropertyValue(
$operand->getPropertyName(),
$operand->getSelectorName());
}


/**
* orderings ::= Ordering {',' Ordering}
* Ordering ::= DynamicOperand [Order]
* Order ::= Ascending | Descending
* Ascending ::= 'ASC'
* Descending ::= 'DESC'
*
* @param array $orderings
* @return string
*/
protected function convertOrderings(array $orderings)
{
$list = array();
foreach ($orderings as $ordering) {
$order = $this->generator->evalOrder($ordering->getOrder());
$operand = $this->convertDynamicOperand($ordering->getOperand());
$list[] = $this->generator->evalOrdering($operand, $order);
}

return $this->generator->evalOrderings($list);
}

protected function convertPath($path)
{
return $this->generator->evalPath($path);
}

protected function convertBindVariable($var)
{
return $this->generator->evalBindVariable($var);
}

protected function convertLiteral($literal)
{
return $this->generator->evalLiteral($literal);
}

/**
* columns ::= (Column ',' {Column}) | '*'
* Column ::= ([selectorName'.']propertyName
* ['AS' columnName]) |
* (selectorName'.*') // If only one selector exists
* selectorName ::= Name
* propertyName ::= Name
* columnName ::= Name
*
* @param array $columns
* @return string
*/
protected function convertColumns(array $columns)
{
$list = array();
foreach ($columns as $column) {
$selector = $column->getSelectorName();
$property = $column->getPropertyName();
$colname = $column->getColumnName();
$list[] = $this->generator->evalColumn($selector, $property, $colname);
}

return $this->generator->evalColumns($list);
}

}
Loading