Skip to content
Open
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
35 changes: 34 additions & 1 deletion src/Jackalope/Transport/DoctrineDBAL/Query/QOMWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is postgres specific (if it works at all)

: "$rightAlias.path"
;

return "CONCAT($replace, '/$append') = $leftAlias.path ";
}

/**
* @param \PHPCR\Query\QOM\ConstraintInterface $constraint
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Jackalope/Transport/DoctrineDBAL/Query/QOMWalkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TODO: assertions

}
}