Skip to content

Commit

Permalink
Merge pull request #292 from jackalope/create_and_reference_nodes_in_…
Browse files Browse the repository at this point in the history
…transaction

make it possible to create and reference nodes within a single transaction
  • Loading branch information
lsmith77 committed Oct 18, 2015
2 parents 40e42b1 + eac319b commit f9aceb2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 43 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
"doctrine/dbal": ">=2.4.5,<3.0.x-dev",
"phpcr/phpcr": "~2.1.2",
"phpcr/phpcr-utils": "^1.2.8",
"jackalope/jackalope": "~1.2.2"
"jackalope/jackalope": "~1.2.4"
},
"provide": {
"jackalope/jackalope-transport": "1.1.0"
},
"require-dev": {
"psr/log": "~1.0",
"phpcr/phpcr-api-tests": "2.1.9",
"phpcr/phpcr-api-tests": "2.1.10",
"phpunit/phpunit": "4.7.*",
"phpunit/dbunit": "~1.3"
},
Expand Down
62 changes: 22 additions & 40 deletions src/Jackalope/Transport/DoctrineDBAL/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,16 +600,16 @@ public function copyNode($srcAbsPath, $dstAbsPath, $srcWorkspace = null)

PathHelper::assertValidAbsolutePath($dstAbsPath, true, true, $this->getNamespacePrefixes());

$srcNodeId = $this->getSystemIdForNodePath($srcAbsPath, $srcWorkspace);
$srcNodeId = $this->getSystemIdForNode($srcAbsPath, $srcWorkspace);
if (!$srcNodeId) {
throw new PathNotFoundException("Source path '$srcAbsPath' not found");
}

if ($this->getSystemIdForNodePath($dstAbsPath)) {
if ($this->getSystemIdForNode($dstAbsPath)) {
throw new ItemExistsException("Cannot copy to destination path '$dstAbsPath' that already exists.");
}

if (!$this->getSystemIdForNodePath(PathHelper::getParentPath($dstAbsPath))) {
if (!$this->getSystemIdForNode(PathHelper::getParentPath($dstAbsPath))) {
throw new PathNotFoundException("Parent of the destination path '" . $dstAbsPath . "' has to exist.");
}

Expand Down Expand Up @@ -804,7 +804,7 @@ private function syncNode($uuid, $path, $type, $isNewNode, $props = array(), $pr

$nodeId = $this->getConnection()->lastInsertId($this->sequenceNodeName);
} else {
$nodeId = $this->getSystemIdForNodePath($path);
$nodeId = $this->getSystemIdForNode($path);
if (!$nodeId) {
throw new RepositoryException("nodeId for $path not found");
}
Expand Down Expand Up @@ -874,7 +874,7 @@ private function syncReferences(array $referencesToUpdate)
$references = $referencesToUpdate[$nodeId];
foreach ($references['properties'] as $name => $data) {
foreach ($data['values'] as $value) {
$targetId = $this->getSystemIdForNodeUuid($value);
$targetId = $this->getSystemIdForNode($value);
if (false === $targetId) {
if (PropertyType::REFERENCE === $data['type']) {
throw new ReferentialIntegrityException(sprintf(
Expand Down Expand Up @@ -1447,52 +1447,34 @@ public function getNodes($paths)
*/
private function pathExists($path, $workspaceName = null)
{
return (boolean) $this->getSystemIdForNodePath($path, $workspaceName);
return (boolean) $this->getSystemIdForNode($path, $workspaceName);
}

/**
* Get the database primary key for node at path.
* Get the database primary key for node.
*
* @param string $path Path of the node
* @param string $identifier Path of the identifier
* @param string $workspaceName To overwrite the current workspace
*
* @return bool|string The database id
*/
private function getSystemIdForNodePath($path, $workspaceName = null)
private function getSystemIdForNode($identifier, $workspaceName = null)
{
if (null === $workspaceName) {
$workspaceName = $this->workspaceName;
}

if ($this->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOMySql\Driver) {
$query = 'SELECT id FROM phpcr_nodes WHERE path COLLATE utf8_bin = ? AND workspace_name = ?';
if (UUIDHelper::isUUID($identifier)) {
$query = 'SELECT id FROM phpcr_nodes WHERE identifier = ? AND workspace_name = ?';
} else {
$query = 'SELECT id FROM phpcr_nodes WHERE path = ? AND workspace_name = ?';
}

if ($nodeId = $this->getConnection()->fetchColumn($query, array($path, $workspaceName))) {
return $nodeId;
}

return false;
}

/**
* Get the database primary key for node at path.
*
* @param string $uuid Uuid of the node
* @param string $workspaceName To overwrite the current workspace
*
* @return bool|string The database id
*/
protected function getSystemIdForNodeUuid($uuid, $workspaceName = null)
{
if (null === $workspaceName) {
$workspaceName = $this->workspaceName;
if ($this->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOMySql\Driver) {
$query = 'SELECT id FROM phpcr_nodes WHERE path COLLATE utf8_bin = ? AND workspace_name = ?';
} else {
$query = 'SELECT id FROM phpcr_nodes WHERE path = ? AND workspace_name = ?';
}
}

$query = 'SELECT id FROM phpcr_nodes WHERE identifier = ? AND workspace_name = ?';
$nodeId = $this->getConnection()->fetchColumn($query, array($uuid, $workspaceName));
$nodeId = $this->getConnection()->fetchColumn($query, array($identifier, $workspaceName));

return $nodeId ?: false;
}
Expand Down Expand Up @@ -1692,7 +1674,7 @@ protected function deleteProperty($path)
$this->assertLoggedIn();

$nodePath = PathHelper::getParentPath($path);
$nodeId = $this->getSystemIdForNodePath($nodePath);
$nodeId = $this->getSystemIdForNode($nodePath);
if (!$nodeId) {
// no we really don't know that path
throw new ItemNotFoundException("No item found at ".$path);
Expand Down Expand Up @@ -1780,11 +1762,11 @@ protected function moveNode($srcAbsPath, $dstAbsPath)
throw new PathNotFoundException("Source path '$srcAbsPath' not found");
}

if ($this->getSystemIdForNodePath($dstAbsPath)) {
if ($this->getSystemIdForNode($dstAbsPath)) {
throw new ItemExistsException("Cannot move '$srcAbsPath' to '$dstAbsPath' because destination node already exists.");
}

if (!$this->getSystemIdForNodePath(PathHelper::getParentPath($dstAbsPath))) {
if (!$this->getSystemIdForNode(PathHelper::getParentPath($dstAbsPath))) {
throw new PathNotFoundException("Parent of the destination path '" . $dstAbsPath . "' has to exist.");
}

Expand Down Expand Up @@ -2192,7 +2174,7 @@ public function getBinaryStream($path)
$this->assertLoggedIn();

$nodePath = PathHelper::getParentPath($path);
$nodeId = $this->getSystemIdForNodePath($nodePath);
$nodeId = $this->getSystemIdForNode($nodePath);
$propertyName = PathHelper::getNodeName($path);

$data = $this->getConnection()->fetchAll(
Expand Down Expand Up @@ -2474,7 +2456,7 @@ public function getWeakReferences($path, $name = null)
*/
private function getNodeReferences($path, $name = null, $weakReference = false)
{
$targetId = $this->getSystemIdForNodePath($path);
$targetId = $this->getSystemIdForNode($path);
$params = array($targetId);

$table = $weakReference ? $this->referenceTables[PropertyType::WEAKREFERENCE] : $this->referenceTables[PropertyType::REFERENCE];
Expand Down
3 changes: 2 additions & 1 deletion tests/Jackalope/Test/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Jackalope\TestCase as BaseTestCase;

abstract class TestCase extends \Jackalope\TestCase
abstract class TestCase extends BaseTestCase
{
/**
* @var Connection
Expand Down

0 comments on commit f9aceb2

Please sign in to comment.