Skip to content

Commit 4288208

Browse files
committed
wip cleanup statement generation
1 parent 46c3b69 commit 4288208

10 files changed

+49
-196
lines changed

src/EventListener/OpenCypher/NodeDeleteToStatementEventListener.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use Laudis\Neo4j\Databags\Statement;
88
use Psr\Log\LoggerInterface;
99
use Syndesi\CypherDataStructures\Contract\NodeInterface;
10-
use Syndesi\CypherDataStructures\Contract\PropertyNameInterface;
1110
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
1211
use Syndesi\CypherEntityManager\Contract\NodeStatementInterface;
1312
use Syndesi\CypherEntityManager\Contract\OnActionCypherElementToStatementEventListenerInterface;
1413
use Syndesi\CypherEntityManager\Event\ActionCypherElementToStatementEvent;
14+
use Syndesi\CypherEntityManager\Helper\StructureHelper;
1515
use Syndesi\CypherEntityManager\Type\ActionType;
1616

1717
class NodeDeleteToStatementEventListener implements OnActionCypherElementToStatementEventListenerInterface, NodeStatementInterface
@@ -42,27 +42,16 @@ public function onActionCypherElementToStatementEvent(ActionCypherElementToState
4242

4343
public static function nodeStatement(NodeInterface $node): Statement
4444
{
45-
$identifyingStrings = [];
46-
$propertyValues = [];
47-
/** @var PropertyNameInterface $identifierName */
48-
foreach ($node->getIdentifiersWithPropertyValues() as $identifierName) {
49-
$identifyingStrings[] = sprintf(
50-
"%s: $%s",
51-
(string) $identifierName,
52-
(string) $identifierName
53-
);
54-
$propertyValues[(string) $identifierName] = $node->getProperty($identifierName);
55-
}
56-
$identifyingString = implode(", ", $identifyingStrings);
57-
5845
return new Statement(
5946
sprintf(
6047
"MATCH (node%s {%s})\n".
6148
"DETACH DELETE node",
6249
ToCypherHelper::nodeLabelStorageToCypherLabelString($node->getNodeLabels()),
63-
$identifyingString
50+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($node, '$identifier')
6451
),
65-
$propertyValues
52+
[
53+
'identifier' => StructureHelper::getIdentifiersFromElementAsArray($node),
54+
]
6655
);
6756
}
6857
}

src/EventListener/OpenCypher/NodeMergeToStatementEventListener.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use Laudis\Neo4j\Databags\Statement;
88
use Psr\Log\LoggerInterface;
99
use Syndesi\CypherDataStructures\Contract\NodeInterface;
10-
use Syndesi\CypherDataStructures\Contract\PropertyNameInterface;
1110
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
1211
use Syndesi\CypherEntityManager\Contract\NodeStatementInterface;
1312
use Syndesi\CypherEntityManager\Contract\OnActionCypherElementToStatementEventListenerInterface;
1413
use Syndesi\CypherEntityManager\Event\ActionCypherElementToStatementEvent;
14+
use Syndesi\CypherEntityManager\Helper\StructureHelper;
1515
use Syndesi\CypherEntityManager\Type\ActionType;
1616

1717
class NodeMergeToStatementEventListener implements OnActionCypherElementToStatementEventListenerInterface, NodeStatementInterface
@@ -42,44 +42,20 @@ public function onActionCypherElementToStatementEvent(ActionCypherElementToState
4242

4343
public static function nodeStatement(NodeInterface $node): Statement
4444
{
45-
$identifyingStrings = [];
46-
$setPropertyStrings = [];
47-
$propertyValues = [];
48-
/** @var PropertyNameInterface $propertyName */
49-
foreach ($node->getProperties() as $propertyName) {
50-
if ($node->hasIdentifier($propertyName)) {
51-
$identifyingStrings[] = sprintf(
52-
"%s: $%s",
53-
(string) $propertyName,
54-
(string) $propertyName
55-
);
56-
} else {
57-
$setPropertyStrings[] = sprintf(
58-
" node.%s = $%s",
59-
(string) $propertyName,
60-
(string) $propertyName
61-
);
62-
}
63-
$propertyValues[(string) $propertyName] = $node->getProperty($propertyName);
64-
}
65-
$identifyingString = implode(", ", $identifyingStrings);
66-
$setPropertyString = implode(",\n", $setPropertyStrings);
67-
6845
return new Statement(
6946
sprintf(
7047
"MERGE (node%s {%s})\n".
7148
"ON CREATE\n".
72-
" SET\n".
73-
"%s\n".
49+
" SET node += \$properties\n".
7450
"ON MATCH\n".
75-
" SET\n".
76-
"%s",
51+
" SET node += \$properties",
7752
ToCypherHelper::nodeLabelStorageToCypherLabelString($node->getNodeLabels()),
78-
$identifyingString,
79-
$setPropertyString,
80-
$setPropertyString
53+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($node, '$identifier')
8154
),
82-
$propertyValues
55+
[
56+
'identifier' => StructureHelper::getIdentifiersFromElementAsArray($node),
57+
'properties' => StructureHelper::getPropertiesFromElementAsArray($node),
58+
]
8359
);
8460
}
8561
}

src/EventListener/OpenCypher/RelationCreateToStatementEventListener.php

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Syndesi\CypherEntityManager\Contract\RelationStatementInterface;
1414
use Syndesi\CypherEntityManager\Event\ActionCypherElementToStatementEvent;
1515
use Syndesi\CypherEntityManager\Exception\InvalidArgumentException;
16+
use Syndesi\CypherEntityManager\Helper\StructureHelper;
1617
use Syndesi\CypherEntityManager\Type\ActionType;
1718

1819
class RelationCreateToStatementEventListener implements OnActionCypherElementToStatementEventListenerInterface, RelationStatementInterface
@@ -45,10 +46,6 @@ public static function relationStatement(RelationInterface $relation): Statement
4546
{
4647
$relationPropertyString = [];
4748
$relationPropertyValues = [];
48-
$startNodePropertyString = [];
49-
$startNodePropertyValues = [];
50-
$endNodePropertyString = [];
51-
$endNodePropertyValues = [];
5249
/** @var PropertyNameInterface $propertyName */
5350
foreach ($relation->getProperties() as $propertyName) {
5451
$relationPropertyString[] = sprintf(
@@ -62,28 +59,10 @@ public static function relationStatement(RelationInterface $relation): Statement
6259
if (null === $startNode) {
6360
throw new InvalidArgumentException('the start node of relations can not be null');
6461
}
65-
/** @var PropertyNameInterface $propertyName */
66-
foreach ($startNode->getIdentifiers() as $propertyName) {
67-
$startNodePropertyString[] = sprintf(
68-
"%s: \$startNode.%s",
69-
(string) $propertyName,
70-
(string) $propertyName
71-
);
72-
$startNodePropertyValues[(string) $propertyName] = $startNode->getProperty($propertyName);
73-
}
7462
$endNode = $relation->getEndNode();
7563
if (null === $endNode) {
7664
throw new InvalidArgumentException('the end node of relations can not be null');
7765
}
78-
/** @var PropertyNameInterface $propertyName */
79-
foreach ($endNode->getProperties() as $propertyName) {
80-
$endNodePropertyString[] = sprintf(
81-
"%s: \$endNode.%s",
82-
(string) $propertyName,
83-
(string) $propertyName
84-
);
85-
$endNodePropertyValues[(string) $propertyName] = $endNode->getProperty($propertyName);
86-
}
8766

8867
return new Statement(
8968
sprintf(
@@ -92,16 +71,16 @@ public static function relationStatement(RelationInterface $relation): Statement
9271
" (endNode%s {%s})\n".
9372
"CREATE (startNode)-[:%s {%s}]->(endNode)",
9473
ToCypherHelper::nodeLabelStorageToCypherLabelString($startNode->getNodeLabels()),
95-
implode(', ', $startNodePropertyString),
74+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($startNode, '$startNode'),
9675
ToCypherHelper::nodeLabelStorageToCypherLabelString($endNode->getNodeLabels()),
97-
implode(', ', $endNodePropertyString),
76+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($endNode, '$endNode'),
9877
(string) $relation->getRelationType(),
9978
implode(', ', $relationPropertyString)
10079
),
10180
[
10281
'relation' => $relationPropertyValues,
103-
'startNode' => $startNodePropertyValues,
104-
'endNode' => $endNodePropertyValues,
82+
'startNode' => StructureHelper::getIdentifiersFromElementAsArray($startNode),
83+
'endNode' => StructureHelper::getIdentifiersFromElementAsArray($endNode),
10584
]
10685
);
10786
}

src/EventListener/OpenCypher/RelationDeleteToStatementEventListener.php

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
use Laudis\Neo4j\Databags\Statement;
88
use Psr\Log\LoggerInterface;
9-
use Syndesi\CypherDataStructures\Contract\PropertyNameInterface;
109
use Syndesi\CypherDataStructures\Contract\RelationInterface;
1110
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
1211
use Syndesi\CypherEntityManager\Contract\OnActionCypherElementToStatementEventListenerInterface;
1312
use Syndesi\CypherEntityManager\Contract\RelationStatementInterface;
1413
use Syndesi\CypherEntityManager\Event\ActionCypherElementToStatementEvent;
1514
use Syndesi\CypherEntityManager\Exception\InvalidArgumentException;
15+
use Syndesi\CypherEntityManager\Helper\StructureHelper;
1616
use Syndesi\CypherEntityManager\Type\ActionType;
1717

1818
class RelationDeleteToStatementEventListener implements OnActionCypherElementToStatementEventListenerInterface, RelationStatementInterface
@@ -43,63 +43,30 @@ public function onActionCypherElementToStatementEvent(ActionCypherElementToState
4343

4444
public static function relationStatement(RelationInterface $relation): Statement
4545
{
46-
$relationPropertyString = [];
47-
$relationPropertyValues = [];
48-
$startNodePropertyString = [];
49-
$startNodePropertyValues = [];
50-
$endNodePropertyString = [];
51-
$endNodePropertyValues = [];
52-
/** @var PropertyNameInterface $propertyName */
53-
foreach ($relation->getIdentifiers() as $propertyName) {
54-
$relationPropertyString[] = sprintf(
55-
"%s: \$relation.%s",
56-
(string) $propertyName,
57-
(string) $propertyName
58-
);
59-
$relationPropertyValues[(string) $propertyName] = $relation->getProperty($propertyName);
60-
}
6146
$startNode = $relation->getStartNode();
6247
if (null === $startNode) {
6348
throw new InvalidArgumentException('the start node of relations can not be null');
6449
}
65-
/** @var PropertyNameInterface $propertyName */
66-
foreach ($startNode->getIdentifiers() as $propertyName) {
67-
$startNodePropertyString[] = sprintf(
68-
"%s: \$startNode.%s",
69-
(string) $propertyName,
70-
(string) $propertyName
71-
);
72-
$startNodePropertyValues[(string) $propertyName] = $startNode->getProperty($propertyName);
73-
}
7450
$endNode = $relation->getEndNode();
7551
if (null === $endNode) {
7652
throw new InvalidArgumentException('the end node of relations can not be null');
7753
}
78-
/** @var PropertyNameInterface $propertyName */
79-
foreach ($endNode->getProperties() as $propertyName) {
80-
$endNodePropertyString[] = sprintf(
81-
"%s: \$endNode.%s",
82-
(string) $propertyName,
83-
(string) $propertyName
84-
);
85-
$endNodePropertyValues[(string) $propertyName] = $endNode->getProperty($propertyName);
86-
}
8754

8855
return new Statement(
8956
sprintf(
9057
"MATCH (%s {%s})-[relation:%s {%s}]->(%s {%s})\n".
9158
"DELETE relation",
9259
ToCypherHelper::nodeLabelStorageToCypherLabelString($startNode->getNodeLabels()),
93-
implode(', ', $startNodePropertyString),
60+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($startNode, '$startNode'),
9461
(string) $relation->getRelationType(),
95-
implode(', ', $relationPropertyString),
62+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($relation, '$identifier'),
9663
ToCypherHelper::nodeLabelStorageToCypherLabelString($endNode->getNodeLabels()),
97-
implode(', ', $endNodePropertyString),
64+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($endNode, '$endNode')
9865
),
9966
[
100-
'relation' => $relationPropertyValues,
101-
'startNode' => $startNodePropertyValues,
102-
'endNode' => $endNodePropertyValues,
67+
'identifier' => StructureHelper::getIdentifiersFromElementAsArray($relation),
68+
'startNode' => StructureHelper::getIdentifiersFromElementAsArray($startNode),
69+
'endNode' => StructureHelper::getIdentifiersFromElementAsArray($endNode),
10370
]
10471
);
10572
}

src/EventListener/OpenCypher/RelationMergeToStatementEventListener.php

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
use Laudis\Neo4j\Databags\Statement;
88
use Psr\Log\LoggerInterface;
9-
use Syndesi\CypherDataStructures\Contract\PropertyNameInterface;
109
use Syndesi\CypherDataStructures\Contract\RelationInterface;
1110
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
1211
use Syndesi\CypherEntityManager\Contract\OnActionCypherElementToStatementEventListenerInterface;
1312
use Syndesi\CypherEntityManager\Contract\RelationStatementInterface;
1413
use Syndesi\CypherEntityManager\Event\ActionCypherElementToStatementEvent;
1514
use Syndesi\CypherEntityManager\Exception\InvalidArgumentException;
15+
use Syndesi\CypherEntityManager\Helper\StructureHelper;
1616
use Syndesi\CypherEntityManager\Type\ActionType;
1717

1818
class RelationMergeToStatementEventListener implements OnActionCypherElementToStatementEventListenerInterface, RelationStatementInterface
@@ -43,85 +43,34 @@ public function onActionCypherElementToStatementEvent(ActionCypherElementToState
4343

4444
public static function relationStatement(RelationInterface $relation): Statement
4545
{
46-
$relationIdentifierString = [];
47-
$relationIdentifierValues = [];
48-
$relationSetPropertyStrings = [];
49-
$relationPropertyValues = [];
50-
$startNodePropertyString = [];
51-
$startNodePropertyValues = [];
52-
$endNodePropertyString = [];
53-
$endNodePropertyValues = [];
54-
/** @var PropertyNameInterface $propertyName */
55-
foreach ($relation->getProperties() as $propertyName) {
56-
if ($relation->hasIdentifier($propertyName)) {
57-
$relationIdentifierString[] = sprintf(
58-
"%s: \$relationIdentifier.%s",
59-
(string) $propertyName,
60-
(string) $propertyName
61-
);
62-
$relationIdentifierValues[(string) $propertyName] = $relation->getProperty($propertyName);
63-
} else {
64-
$relationSetPropertyStrings[] = sprintf(
65-
" relation.%s = \$relationProperty.%s",
66-
(string) $propertyName,
67-
(string) $propertyName
68-
);
69-
$relationPropertyValues[(string) $propertyName] = $relation->getProperty($propertyName);
70-
}
71-
}
7246
$startNode = $relation->getStartNode();
7347
if (null === $startNode) {
7448
throw new InvalidArgumentException('the start node of relations can not be null');
7549
}
76-
/** @var PropertyNameInterface $propertyName */
77-
foreach ($startNode->getIdentifiers() as $propertyName) {
78-
$startNodePropertyString[] = sprintf(
79-
"%s: \$startNode.%s",
80-
(string) $propertyName,
81-
(string) $propertyName
82-
);
83-
$startNodePropertyValues[(string) $propertyName] = $startNode->getProperty($propertyName);
84-
}
8550
$endNode = $relation->getEndNode();
8651
if (null === $endNode) {
8752
throw new InvalidArgumentException('the end node of relations can not be null');
8853
}
89-
/** @var PropertyNameInterface $propertyName */
90-
foreach ($endNode->getProperties() as $propertyName) {
91-
$endNodePropertyString[] = sprintf(
92-
"%s: \$endNode.%s",
93-
(string) $propertyName,
94-
(string) $propertyName
95-
);
96-
$endNodePropertyValues[(string) $propertyName] = $endNode->getProperty($propertyName);
97-
}
9854

9955
return new Statement(
10056
sprintf(
10157
"MATCH\n".
10258
" (startNode%s {%s}),\n".
10359
" (endNode%s {%s})\n".
10460
"MERGE (startNode)-[relation:%s {%s}]->(endNode)\n".
105-
"ON CREATE\n".
106-
" SET\n".
107-
"%s\n".
108-
"ON MATCH\n".
109-
" SET\n".
110-
"%s",
61+
"SET relation += \$property",
11162
ToCypherHelper::nodeLabelStorageToCypherLabelString($startNode->getNodeLabels()),
112-
implode(', ', $startNodePropertyString),
63+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($startNode, '$startNode'),
11364
ToCypherHelper::nodeLabelStorageToCypherLabelString($endNode->getNodeLabels()),
114-
implode(', ', $endNodePropertyString),
65+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($endNode, '$endNode'),
11566
(string) $relation->getRelationType(),
116-
implode(', ', $relationIdentifierString),
117-
implode(",\n", $relationSetPropertyStrings),
118-
implode(",\n", $relationSetPropertyStrings),
67+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($relation, '$identifier')
11968
),
12069
[
121-
'relationIdentifier' => $relationIdentifierValues,
122-
'relationProperty' => $relationPropertyValues,
123-
'startNode' => $startNodePropertyValues,
124-
'endNode' => $endNodePropertyValues,
70+
'identifier' => StructureHelper::getIdentifiersFromElementAsArray($relation),
71+
'property' => StructureHelper::getPropertiesFromElementAsArray($relation),
72+
'startNode' => StructureHelper::getIdentifiersFromElementAsArray($startNode),
73+
'endNode' => StructureHelper::getIdentifiersFromElementAsArray($endNode),
12574
]
12675
);
12776
}

tests/EventListener/OpenCypher/NodeDeleteToStatementEventListenerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ public function testNodeStatement(): void
8383
$statement = NodeDeleteToStatementEventListener::nodeStatement($node);
8484

8585
$this->assertSame(
86-
"MATCH (node:NodeLabel {id: \$id})\n".
86+
"MATCH (node:NodeLabel {id: \$identifier.id})\n".
8787
"DETACH DELETE node",
8888
$statement->getText()
8989
);
9090
$this->assertCount(1, $statement->getParameters());
91-
$this->assertSame(1234, $statement->getParameters()['id']);
91+
$this->assertSame(1234, $statement->getParameters()['identifier']['id']);
9292
}
9393
}

0 commit comments

Comments
 (0)