From eac319b8a70a74df18a289f59e2f3d92abfd386e Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Mon, 3 Aug 2015 14:42:53 +0200 Subject: [PATCH] make it possible to create and reference nodes within a single transaction --- composer.json | 4 +- .../Transport/DoctrineDBAL/Client.php | 62 +++++++------------ tests/Jackalope/Test/TestCase.php | 3 +- 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/composer.json b/composer.json index fa6f1acc..a920def6 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/src/Jackalope/Transport/DoctrineDBAL/Client.php b/src/Jackalope/Transport/DoctrineDBAL/Client.php index d5970ef8..08a84c9a 100644 --- a/src/Jackalope/Transport/DoctrineDBAL/Client.php +++ b/src/Jackalope/Transport/DoctrineDBAL/Client.php @@ -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."); } @@ -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"); } @@ -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( @@ -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; } @@ -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); @@ -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."); } @@ -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( @@ -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]; diff --git a/tests/Jackalope/Test/TestCase.php b/tests/Jackalope/Test/TestCase.php index c63ad77c..b4cdce32 100644 --- a/tests/Jackalope/Test/TestCase.php +++ b/tests/Jackalope/Test/TestCase.php @@ -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