forked from codingfinest/neo4j-go-ogm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleter.go
173 lines (147 loc) · 5.13 KB
/
deleter.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
// MIT License
//
// Copyright (c) 2020 codingfinest
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package gogm
import (
"reflect"
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
)
type deleter struct {
cypherExecuter *cypherExecuter
store store
eventer eventer
registry *registry
graphFactory graphFactory
}
func newDeleter(cypherExecuter *cypherExecuter, store store, eventer eventer, registry *registry, graphFactory graphFactory) *deleter {
return &deleter{cypherExecuter, store, eventer, registry, graphFactory}
}
func (d *deleter) delete(object any) error {
var (
value = reflect.ValueOf(object)
IDer = getIDer(nil, nil)
err error
parameters = []map[string]any{}
graphDeleteClauses = map[clause][]string{}
graphs []graph
record *neo4j.Record
)
if graphs, err = d.graphFactory.get(value, map[int]bool{labels: true, relatedGraph: true}); err != nil {
return err
}
for _, graph := range graphs {
IDer(graph)
}
if graphs[0].getID() < 0 {
return nil
}
storedGraph := d.store.get(graphs[0])
if storedGraph == nil || storedGraph.getID() < 0 {
return nil
}
var cypherBuilder graphQueryBuilder
if cypherBuilder, err = newCypherBuilder(storedGraph, d.registry, nil); err != nil {
return err
}
delete, deleteParameters, depedencies := cypherBuilder.getDelete()
for _, depedency := range depedencies {
var depedencyCypherBuilder graphQueryBuilder
if depedencyCypherBuilder, err = newCypherBuilder(depedency, d.registry, nil); err != nil {
return err
}
match, matchParameters, _ := depedencyCypherBuilder.getMatch()
parameters = append(parameters, matchParameters)
graphDeleteClauses[matchClause] = append(graphDeleteClauses[matchClause], match)
}
parameters = append(parameters, deleteParameters)
graphDeleteClauses[deleteClause] = append(graphDeleteClauses[deleteClause], delete)
cypher := getCyhperFromClauses(graphDeleteClauses)
if cypher != emptyString {
typeOfGraphToDelete := reflect.TypeOf(storedGraph)
for _, eventListener := range d.eventer.eventListeners {
eventListener.OnPreDelete(event{storedGraph.getValue(), DELETE})
if typeOfPrivateNode == typeOfGraphToDelete {
for _, relationship := range storedGraph.getRelatedGraphs() {
if relationship.getValue().IsValid() {
eventListener.OnPreDelete(event{relationship.getValue(), DELETE})
}
}
}
}
if records, collectErr := d.cypherExecuter.exec(cypher, flattenParamters(parameters)); collectErr != nil {
return collectErr
} else if len(records) > 0 {
record = records[0]
}
if record != nil {
deletedGraphs, updatedGraphs := d.store.delete(storedGraph)
for _, updatedGraph := range updatedGraphs {
notifyPostDelete(d.eventer, updatedGraph, UPDATE)
}
for _, deletedGraph := range deletedGraphs {
notifyPostDelete(d.eventer, deletedGraph, DELETE)
}
}
}
return nil
}
func (d *deleter) deleteAll(object any, deleteOptions *DeleteOptions) error {
var (
value = reflect.ValueOf(object)
graphs []graph
err error
records []*neo4j.Record
)
if graphs, err = d.graphFactory.get(value, map[int]bool{labels: true}); err != nil {
return err
}
var cypherBuilder graphQueryBuilder
if cypherBuilder, err = newCypherBuilder(graphs[0], d.registry, nil); err != nil {
return err
}
cypher, parameter := cypherBuilder.getDeleteAll()
if cypher != emptyString {
if records, err = d.cypherExecuter.exec(cypher, parameter); err != nil {
return err
}
for _, record := range records {
graphs[0].setID(record.Values[0].(int64))
deletedGraphs, updatedGraphs := d.store.delete(graphs[0])
for _, updatedGraph := range updatedGraphs {
notifyPostDelete(d.eventer, updatedGraph, UPDATE)
}
for _, deletedGraph := range deletedGraphs {
notifyPostDelete(d.eventer, deletedGraph, DELETE)
}
}
}
return nil
}
func (d *deleter) purgeDatabase() error {
var err error
if _, err := d.cypherExecuter.exec("MATCH (n) DETACH DELETE n", nil); err != nil {
return err
}
for _, deletedGraph := range d.store.purge() {
notifyPostDelete(d.eventer, deletedGraph, DELETE)
}
return err
}