forked from codingfinest/neo4j-go-ogm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeCypherbuilder.go
205 lines (175 loc) · 6.06 KB
/
nodeCypherbuilder.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package gogm
import (
"fmt"
"strconv"
"strings"
)
type nodeQueryBuilder struct {
n *node
registry *registry
deltaProperties map[string]any
isLabelsDirty bool
removedRelationships map[int64]graph
removedRelationshipsOtherNodes map[int64]graph
}
func (nqb nodeQueryBuilder) getGraph() graph {
return nqb.n
}
func (nqb nodeQueryBuilder) getGraphField(relatedGraph graph) (*field, error) {
var (
metadata metadata
field *field
err error
)
if metadata, err = nqb.registry.get(nqb.n.getValue().Type()); err != nil {
return nil, err
}
if field, err = metadata.getGraphField(nqb.n, relatedGraph); err != nil {
return nil, err
}
return field, nil
}
func (nqb nodeQueryBuilder) diffNodeRelatedGraphs(ref graph) (addedGraphs map[int64]graph, removedGraphs map[int64]graph, _err error) {
if ref == nil {
return nil, nil, nil
}
var (
field *field
err error
)
removedRelationships := map[int64]graph{}
removedRelationshipsOtherNodes := map[int64]graph{}
refNode := ref.(*node)
for internalID, storedRelationship := range refNode.relationships {
if nqb.n.relationships[internalID] == nil {
if field, err = nqb.getGraphField(storedRelationship); err != nil {
return nil, nil, err
}
if field == nil {
//The node n, doesn't have the field for this relationship. Hence,
//the relationship wasn't removed by a nil assignment to the field
continue
}
otherNode := storedRelationship.getRelatedGraphs()[startNode]
if nqb.n.getID() == otherNode.getID() {
otherNode = storedRelationship.getRelatedGraphs()[endNode]
}
removedRelationships[internalID] = storedRelationship
removedRelationshipsOtherNodes[internalID] = otherNode
}
}
return removedRelationships, removedRelationshipsOtherNodes, nil
}
func newNodeCypherBuilder(n *node, registry *registry, stored graph) (*nodeQueryBuilder, error) {
var err error
nqb := &nodeQueryBuilder{
n: n,
registry: registry,
deltaProperties: n.getProperties(),
isLabelsDirty: true}
if stored != nil {
nqb.deltaProperties = diffProperties(n.getProperties(), stored.getProperties())
nqb.isLabelsDirty = n.getLabel() != stored.getLabel()
nqb.removedRelationships, nqb.removedRelationshipsOtherNodes, err = nqb.diffNodeRelatedGraphs(stored)
if err != nil {
return nil, err
}
}
return nqb, nil
}
func (nqb nodeQueryBuilder) getRemovedGraphs() (map[int64]graph, map[int64]graph) {
return nqb.removedRelationships, nqb.removedRelationshipsOtherNodes
}
func (nqb nodeQueryBuilder) isGraphDirty() bool {
return nqb.n.getID() < 0 || len(nqb.deltaProperties) > 0 || len(nqb.removedRelationships) > 0 || nqb.isLabelsDirty
}
func (nqb nodeQueryBuilder) getCreate() (string, string, map[string]any, map[string]graph) {
create := `CREATE (` + nqb.n.getSignature() + `)
`
return create, emptyString, nil, nil
}
func (nqb nodeQueryBuilder) getMatch() (string, map[string]any, map[string]graph) {
var (
nSign = nqb.n.getSignature()
metadata, _ = nqb.registry.get(nqb.n.getValue().Type())
customIDPropertyName, customIDPropertyValue = metadata.getCustomID(*nqb.n.getValue())
idCQLRef = nSign + "ID"
parameters = map[string]any{idCQLRef: nqb.n.getID()}
)
match := `MATCH (` + nSign + `)
`
filter := ` WHERE ID(` + nSign + `) = $` + idCQLRef + `
`
if customIDPropertyName != emptyString {
filter = `WHERE ` + nSign + `.` + customIDPropertyName + ` = $` + idCQLRef + `
`
parameters[idCQLRef] = customIDPropertyValue.Interface()
}
return match + filter, parameters, nil
}
func (nqb nodeQueryBuilder) getSet() (string, map[string]any) {
var (
set string
nSign = nqb.n.getSignature()
properties = map[string]any{}
parameters = map[string]any{}
propCQLRef = nSign + "Properties"
)
for propertyName, propertyValue := range nqb.deltaProperties {
if !metaProperties[propertyName] {
properties[propertyName] = propertyValue
}
}
if len(properties) > 0 {
set += `SET ` + nSign + ` += $` + propCQLRef + `
`
parameters[propCQLRef] = properties
}
if nqb.isLabelsDirty {
set += `SET ` + nSign + `:` + nqb.n.getLabel() + `
`
}
return set, parameters
}
func (nqb nodeQueryBuilder) getLoadAll(IDs any, lo *LoadOptions) (string, map[string]any) {
var (
depth = strconv.Itoa(lo.Depth)
metadata, _ = nqb.registry.get(nqb.n.getValue().Type())
customIDPropertyName, _ = metadata.getCustomID(*nqb.n.getValue())
parameters = map[string]any{}
)
if lo.Depth == infiniteDepth {
depth = emptyString
}
matchOutString := fmt.Sprintf(`MATCH path = (n:%s)-[*0..%s]->()`, nqb.n.getLabel(), depth)
matchInString := fmt.Sprintf(`MATCH path = (n:%s)<-[*0..%s]-()`, nqb.n.getLabel(), depth)
unionString := `UNION`
var filter string
if IDs != nil {
filter = `WHERE ID(n) IN $ids
`
if customIDPropertyName != emptyString {
filter = `WHERE n.` + customIDPropertyName + ` IN $ids
`
}
parameters["ids"] = IDs
}
end := `WITH n, path, range(0, length(path) - 1) as index
WITH n, path, index, [i in index | CASE WHEN nodes(path)[i] = startNode(relationships(path)[i]) THEN false ELSE true END] as isDirectionInverted
RETURN path, ID(n), isDirectionInverted
`
match := fmt.Sprintf("%s", strings.Join([]string{matchOutString, filter, end, unionString, matchInString, filter, end}, " "))
return match, parameters
}
func (nqb nodeQueryBuilder) getDelete() (string, map[string]any, map[string]graph) {
match, parameters, _ := nqb.getMatch()
delete := `DETACH DELETE ` + nqb.n.getSignature() + ` RETURN ID(` + nqb.n.getSignature() + `)
`
return match + delete, parameters, nil
}
func (nqb nodeQueryBuilder) getDeleteAll() (string, map[string]any) {
return `MATCH (n:` + nqb.n.getLabel() + `) DETACH DELETE n RETURN ID(n)`, nil
}
func (nqb nodeQueryBuilder) getCountEntitiesOfType() (string, map[string]any) {
return `MATCH (n:` + nqb.n.getLabel() + `) RETURN count(n) as count`, nil
}