-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMergeClause.php
More file actions
132 lines (112 loc) · 3.13 KB
/
Copy pathMergeClause.php
File metadata and controls
132 lines (112 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php declare(strict_types=1);
/*
* This file is part of php-cypher-dsl.
*
* Copyright (C) Wikibase Solutions
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace WikibaseSolutions\CypherDSL\Clauses;
use WikibaseSolutions\CypherDSL\Patterns\CompletePattern;
/**
* This class represents a MERGE clause.
*
* The MERGE clause ensures that a pattern exists in the graph. Either the pattern already exists, or it
* needs to be created.
*
* @see https://neo4j.com/docs/cypher-manual/current/clauses/merge/
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 115)
*/
final class MergeClause extends Clause
{
/**
* @var null|CompletePattern The pattern to merge
*/
private ?CompletePattern $pattern = null;
/**
* @var null|SetClause The clause to execute when the pattern is created
*/
private ?SetClause $createClause = null;
/**
* @var null|SetClause The clause to execute when the pattern is matched
*/
private ?SetClause $matchClause = null;
/**
* Sets the pattern to merge.
*
* @param CompletePattern $pattern The pattern to merge
*
* @return $this
*/
public function setPattern(CompletePattern $pattern): self
{
$this->pattern = $pattern;
return $this;
}
/**
* The clause to execute on all nodes that need to be created.
*
* @see https://neo4j.com/docs/cypher-manual/current/clauses/merge/#merge-merge-with-on-create
*/
public function setOnCreate(?SetClause $createClause): self
{
$this->createClause = $createClause;
return $this;
}
/**
* The clause to execute on all found nodes.
*
* @see https://neo4j.com/docs/cypher-manual/current/clauses/merge/#merge-merge-with-on-match
*/
public function setOnMatch(?SetClause $matchClause): self
{
$this->matchClause = $matchClause;
return $this;
}
/**
* Returns the pattern to MERGE.
*/
public function getPattern(): ?CompletePattern
{
return $this->pattern;
}
/**
* Returns the clause to execute when the pattern is matched.
*/
public function getOnCreateClause(): ?SetClause
{
return $this->createClause;
}
/**
* Returns the clause to execute when the pattern is matched.
*/
public function getOnMatchClause(): ?SetClause
{
return $this->matchClause;
}
/**
* @inheritDoc
*/
protected function getClause(): string
{
return "MERGE";
}
/**
* @inheritDoc
*/
protected function getSubject(): string
{
if (!isset($this->pattern)) {
return "";
}
$queryParts = [$this->pattern->toQuery()];
if (isset($this->createClause)) {
$queryParts[] = sprintf("ON CREATE %s", $this->createClause->toQuery());
}
if (isset($this->matchClause)) {
$queryParts[] = sprintf("ON MATCH %s", $this->matchClause->toQuery());
}
return implode(" ", $queryParts);
}
}