From c446b935701674c3ef01389ef932e8ecce549c4d Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Tue, 25 Feb 2014 11:03:31 +0100 Subject: [PATCH] WIP implement join on issamenode --- .../DoctrineDBAL/Query/QOMWalker.php | 35 ++++++++++++++++++- .../DoctrineDBAL/Query/QOMWalkerTest.php | 21 +++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php b/src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php index 8d10b0e8..9616e7e3 100644 --- a/src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php +++ b/src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php @@ -353,7 +353,7 @@ public function walkJoinCondition(QOM\SelectorInterface $left, QOM\SelectorInter } if ($condition instanceof QOM\SameNodeJoinConditionInterface) { - throw new NotImplementedException("SameNodeJoinCondtion"); + return $this->walkSameNodeJoinCondition($condition); } } @@ -395,6 +395,39 @@ public function walkEquiJoinCondition($leftSelectorName, $rightSelectorName, QOM $this->walkOperand(new PropertyValue($rightSelectorName, $condition->getProperty2Name())); } + /** + * @param QOM\SameNodeJoinConditionInterface $condition + * + * @return string + */ + public function walkSameNodeJoinCondition(QOM\SameNodeJoinConditionInterface $condition) + { + $rightAlias = $this->getTableAlias($condition->getSelector1Name()); + $leftAlias = $this->getTableAlias($condition->getSelector2Name()); + $path = $condition->getSelector2Path(); + + if (!$path) { + return "$rightAlias.path = $leftAlias.path "; + } + + $append = ''; + + $matches = array(); + $strip = preg_match_all('|^(\.\./)+|', $path, $matches); + if (strlen($path) >= 2 && '..' == substr($path, -2)) { + $strip++; + } else { + $append = substr($path, $strip*3); + } + + $replace = $strip + ? sprintf("REGEXP_REPLACE($rightAlias.path, '|(/[!/]+){,%s}$|', '')", $strip) + : "$rightAlias.path" + ; + + return "CONCAT($replace, '/$append') = $leftAlias.path "; + } + /** * @param \PHPCR\Query\QOM\ConstraintInterface $constraint * diff --git a/tests/Jackalope/Transport/DoctrineDBAL/Query/QOMWalkerTest.php b/tests/Jackalope/Transport/DoctrineDBAL/Query/QOMWalkerTest.php index 8a946a93..c88b98dd 100644 --- a/tests/Jackalope/Transport/DoctrineDBAL/Query/QOMWalkerTest.php +++ b/tests/Jackalope/Transport/DoctrineDBAL/Query/QOMWalkerTest.php @@ -2,6 +2,7 @@ namespace Jackalope\Transport\DoctrineDBAL\Query; +use Jackalope\Query\QOM\SameNodeJoinCondition; use Jackalope\Test\TestCase; use Jackalope\Query\QOM\Length; use Jackalope\Query\QOM\PropertyValue; @@ -253,4 +254,24 @@ public function testDescendantQueryTrailingSlash() $this->walker->walkQOMQuery($query); } + + public function sameNodeJoinConditions() + { + return array( + array(new SameNodeJoinCondition('file', 'nt:unstructured', 'jcr:content')), + array(new SameNodeJoinCondition('file', 'nt:unstructured', '..')), + array(new SameNodeJoinCondition('file', 'nt:unstructured', '../..')), + array(new SameNodeJoinCondition('file', 'nt:unstructured', '../something')), + ); + } + + /** + * @dataProvider sameNodeJoinConditions + */ + public function testWalkSameNodeJoinCondition($same) + { + $sql = $this->walker->walkSameNodeJoinCondition($same); + + var_dump($sql); + } }