-
Notifications
You must be signed in to change notification settings - Fork 5
/
pattern_comprehension.go
165 lines (138 loc) · 5.51 KB
/
pattern_comprehension.go
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package cypher
import "errors"
type PatternComprehension struct {
ExpressionContainer
pattern PatternElement
where Where
listDefinition Expression
key string
notNil bool
err error
}
func PatternComprehensionCreate(pattern PatternElement, where Where, listDefinition Expression) PatternComprehension {
patternComprehension := PatternComprehension{
pattern: pattern,
where: where,
listDefinition: listDefinition,
notNil: true,
}
patternComprehension.key = getAddress(&patternComprehension)
patternComprehension.ExpressionContainer = ExpressionWrap(patternComprehension)
return patternComprehension
}
func PatternComprehensionError(err error) PatternComprehension {
return PatternComprehension{err: err}
}
func PatternComprehensionBasedOn(pattern RelationshipPattern) PatternComprehensionOngoingDefinitionWithPattern {
if !pattern.isNotNil() {
return PatternComprehensionBuilder{err: errors.New("pattern comprehension builder: a pattern is required")}
}
return PatternComprehensionBuilderCreate(pattern)
}
func PatternComprehensionBasedOnNamePath(pattern NamedPath) PatternComprehensionOngoingDefinitionWithPattern {
if !pattern.isNotNil() {
return PatternComprehensionBuilder{err: errors.New("pattern comprehension builder: a pattern is required")}
}
return PatternComprehensionBuilderCreate(pattern)
}
func (p PatternComprehension) GetError() error {
return p.err
}
func (p PatternComprehension) accept(visitor *CypherRenderer) {
visitor.enter(p)
p.pattern.accept(visitor)
VisitIfNotNull(p.where, visitor)
PIPE.accept(visitor)
p.listDefinition.accept(visitor)
visitor.leave(p)
}
func (p PatternComprehension) enter(renderer *CypherRenderer) {
renderer.append("[")
}
func (p PatternComprehension) leave(renderer *CypherRenderer) {
renderer.append("]")
}
func (p PatternComprehension) getKey() string {
return p.key
}
func (p PatternComprehension) isNotNil() bool {
return p.notNil
}
func (p PatternComprehension) GetExpressionType() ExpressionType {
return "PatternComprehension"
}
type PatternComprehensionOngoingDefinitionWithoutReturn interface {
/**
* @param variables the elements to be returned from the pattern
* @return The final definition of the pattern comprehension
* @see #Returning(Expression...)
*/
ReturningByNamed(variables ...Named) PatternComprehension
/**
* @param listDefinition Defines the elements to be returned from the pattern
* @return The final definition of the pattern comprehension
*/
Returning(listDefinitions ...Expression) PatternComprehension
}
type PatternComprehensionOngoingDefinitionWithPattern interface {
PatternComprehensionOngoingDefinitionWithoutReturn
Where(condition Condition) PatternComprehensionOngoingDefinitionWithPatternAndWhere
WherePattern(pathPattern RelationshipPattern) PatternComprehensionOngoingDefinitionWithPatternAndWhere
}
type PatternComprehensionOngoingDefinitionWithPatternAndWhere interface {
PatternComprehensionOngoingDefinitionWithoutReturn
And(condition Condition) PatternComprehensionOngoingDefinitionWithPatternAndWhere
AndPattern(pattern RelationshipPattern) PatternComprehensionOngoingDefinitionWithPatternAndWhere
Or(condition Condition) PatternComprehensionOngoingDefinitionWithPatternAndWhere
OrPattern(pattern RelationshipPattern) PatternComprehensionOngoingDefinitionWithPatternAndWhere
GetError() error
}
type PatternComprehensionBuilder struct {
pattern PatternElement
conditionBuilder ConditionBuilder
err error
}
func PatternComprehensionBuilderCreate(pattern PatternElement) PatternComprehensionBuilder {
return PatternComprehensionBuilder{
pattern: pattern,
conditionBuilder: ConditionBuilderCreate(),
}
}
func (p PatternComprehensionBuilder) ReturningByNamed(variables ...Named) PatternComprehension {
return p.Returning(CreateSymbolicNameByNamed(variables...)...)
}
func (p PatternComprehensionBuilder) Returning(listDefinitions ...Expression) PatternComprehension {
where := Where{}
condition := p.conditionBuilder.buildCondition()
if condition != nil && condition.isNotNil() {
where = WhereCreate(condition)
}
return PatternComprehensionCreate(p.pattern, where, ListOrSingleExpression(listDefinitions...))
}
func (p PatternComprehensionBuilder) Where(condition Condition) PatternComprehensionOngoingDefinitionWithPatternAndWhere {
p.conditionBuilder.Where(condition)
return p
}
func (p PatternComprehensionBuilder) WherePattern(pathPattern RelationshipPattern) PatternComprehensionOngoingDefinitionWithPatternAndWhere {
if !pathPattern.isNotNil() {
return PatternComprehensionBuilder{err: errors.New("pattern comprehension builder: path pattern must not be nil")}
}
return p.Where(RelationshipPatternConditionCreate(pathPattern))
}
func (p PatternComprehensionBuilder) And(condition Condition) PatternComprehensionOngoingDefinitionWithPatternAndWhere {
p.conditionBuilder.And(condition)
return p
}
func (p PatternComprehensionBuilder) AndPattern(pattern RelationshipPattern) PatternComprehensionOngoingDefinitionWithPatternAndWhere {
return p.And(RelationshipPatternConditionCreate(pattern))
}
func (p PatternComprehensionBuilder) Or(condition Condition) PatternComprehensionOngoingDefinitionWithPatternAndWhere {
p.conditionBuilder.Or(condition)
return p
}
func (p PatternComprehensionBuilder) OrPattern(pattern RelationshipPattern) PatternComprehensionOngoingDefinitionWithPatternAndWhere {
return p.Or(RelationshipPatternConditionCreate(pattern))
}
func (p PatternComprehensionBuilder) GetError() error {
return p.err
}