python-eigen-ingenuity-nocli 0.5.6b4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- eigeningenuity/__init__.py +29 -0
- eigeningenuity/assetmodel.py +433 -0
- eigeningenuity/auth.py +178 -0
- eigeningenuity/core/__init__.py +236 -0
- eigeningenuity/elastic.py +171 -0
- eigeningenuity/events.py +88 -0
- eigeningenuity/historian.py +786 -0
- eigeningenuity/historianmulti.py +481 -0
- eigeningenuity/settings.py +87 -0
- eigeningenuity/smartdash.py +103 -0
- eigeningenuity/sql.py +132 -0
- eigeningenuity/util.py +548 -0
- modelbuilder/__init__.py +7 -0
- modelbuilder/src/__init__.py +0 -0
- modelbuilder/src/assetmodelbuilder.py +164 -0
- modelbuilder/src/assetmodelcsvwriter.py +31 -0
- modelbuilder/src/assetmodelutilities.py +51 -0
- modelbuilder/src/configmanager.py +500 -0
- modelbuilder/src/connectors/__init__.py +0 -0
- modelbuilder/src/connectors/boltconnector.py +108 -0
- modelbuilder/src/connectors/dbconnectors.py +40 -0
- modelbuilder/src/connectors/genericconnector.py +30 -0
- modelbuilder/src/connectors/httpsconnector.py +132 -0
- modelbuilder/src/csvcontentsmanager.py +135 -0
- modelbuilder/src/csvwriter.py +305 -0
- modelbuilder/src/fileanalyser.py +91 -0
- modelbuilder/src/filemanager.py +400 -0
- modelbuilder/src/messages.py +61 -0
- modelbuilder/src/processors/__init__.py +0 -0
- modelbuilder/src/processors/cypherfileprocessor.py +73 -0
- modelbuilder/src/processors/labelmanager.py +133 -0
- modelbuilder/src/processors/nodefileprocessor.py +282 -0
- modelbuilder/src/processors/propertymanager.py +129 -0
- modelbuilder/src/processors/relationshipfileprocessor.py +285 -0
- modelbuilder/src/processors/relationshipmanager.py +31 -0
- modelbuilder/src/queries/__init__.py +0 -0
- modelbuilder/src/queries/cypherbuilder.py +323 -0
- modelbuilder/src/queries/query.py +82 -0
- modelbuilder/src/queries/typedefs.py +180 -0
- modelbuilder/src/versionmanager.py +356 -0
- python_eigen_ingenuity_nocli-0.5.6b4.dist-info/METADATA +30 -0
- python_eigen_ingenuity_nocli-0.5.6b4.dist-info/RECORD +44 -0
- python_eigen_ingenuity_nocli-0.5.6b4.dist-info/WHEEL +5 -0
- python_eigen_ingenuity_nocli-0.5.6b4.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import messages
|
|
2
|
+
from queries.cypherbuilder import QueryBuilder
|
|
3
|
+
from queries.typedefs import Properties
|
|
4
|
+
import assetmodelutilities as amu
|
|
5
|
+
from queries.query import Neo4jQuery
|
|
6
|
+
import processors.labelmanager as lm
|
|
7
|
+
import processors.relationshipmanager as rm
|
|
8
|
+
import processors.propertymanager as pm
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class RelationshipFileProcessor:
|
|
12
|
+
|
|
13
|
+
def __init__(self, filename, config, contents):
|
|
14
|
+
self.filename = filename
|
|
15
|
+
self.contents = contents
|
|
16
|
+
|
|
17
|
+
self.primary_property = config.get_primary_property()
|
|
18
|
+
self.add_system_properties = not config.get_no_system_properties()
|
|
19
|
+
|
|
20
|
+
self.required_labels = config.get_required_labels()
|
|
21
|
+
self.creation_time_property = config.get_creation_time_property()
|
|
22
|
+
self.update_time_property = config.get_update_time_property()
|
|
23
|
+
|
|
24
|
+
self.from_node_alias = config.get_from_alias()
|
|
25
|
+
self.from_label_alias = config.get_from_label_alias()
|
|
26
|
+
self.to_node_alias = config.get_to_alias()
|
|
27
|
+
self.to_label_alias = config.get_to_label_alias()
|
|
28
|
+
self.relationship_alias = config.get_relationship_alias()
|
|
29
|
+
|
|
30
|
+
self.validate_only = config.in_validation_mode()
|
|
31
|
+
self.summarise = config.in_summary_mode()
|
|
32
|
+
|
|
33
|
+
self.blank_property_list = config.get_blank_property_list()
|
|
34
|
+
self.batch_rate = config.get_batch_rate()
|
|
35
|
+
self.start_from = config.get_start_from()
|
|
36
|
+
self.last_row = config.get_end_at()
|
|
37
|
+
|
|
38
|
+
self.from_types = amu.from_types
|
|
39
|
+
self.to_types = amu.to_types
|
|
40
|
+
self.relation_types = amu.relation_types
|
|
41
|
+
self.all_types = amu.all_types
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def get_last_row(self, num_rows):
|
|
45
|
+
if self.last_row < 0:
|
|
46
|
+
last_row = num_rows
|
|
47
|
+
else:
|
|
48
|
+
last_row = min(num_rows, self.last_row)
|
|
49
|
+
return last_row
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def process_relationship_file(self):
|
|
53
|
+
|
|
54
|
+
# For each line in the file, build a cypher query to MERGE a relationship
|
|
55
|
+
# Each query is added to a list of queries, which are returned to the caller. They are NOT actioned here
|
|
56
|
+
|
|
57
|
+
num_data_rows = self.contents.get_row_count()
|
|
58
|
+
from_columns = self.contents.get_column_numbers_list(self.from_node_alias, self.from_types)
|
|
59
|
+
from_label_columns = self.contents.get_column_numbers_list(self.from_label_alias)
|
|
60
|
+
to_columns = self.contents.get_column_numbers_list(self.to_node_alias, self.to_types)
|
|
61
|
+
to_label_columns = self.contents.get_column_numbers_list(self.to_label_alias)
|
|
62
|
+
relation_columns = self.contents.get_column_numbers_list(self.relationship_alias, self.relation_types)
|
|
63
|
+
other_columns = self.from_node_alias + self.from_label_alias + self.to_node_alias + self.to_label_alias + self.relationship_alias
|
|
64
|
+
property_columns = self.contents.get_other_column_numbers_list(other_columns, self.all_types)
|
|
65
|
+
|
|
66
|
+
queries = []
|
|
67
|
+
from_ref = 'from'
|
|
68
|
+
relation_ref = 'rel'
|
|
69
|
+
to_ref = 'to'
|
|
70
|
+
relation_ref_dot = relation_ref + '.'
|
|
71
|
+
|
|
72
|
+
num_rows = num_data_rows - self.start_from + 1
|
|
73
|
+
this_row = self.start_from-1
|
|
74
|
+
last_row = self.get_last_row(num_data_rows)
|
|
75
|
+
while this_row < last_row:
|
|
76
|
+
|
|
77
|
+
relationships = rm.validate_relationships(self.contents.get_column_values_list(this_row, relation_columns))
|
|
78
|
+
|
|
79
|
+
properties = self.contents.get_property_values_dict(this_row, property_columns, self.blank_property_list, prefix=relation_ref_dot)
|
|
80
|
+
# If there are multiple From or To nodes, create a relationship query for each combination
|
|
81
|
+
for from_node in self.contents.get_property_values_list(this_row, from_columns, self.primary_property, self.primary_property):
|
|
82
|
+
validated_csv_from_labels = lm.validate_labels(self.contents.get_column_values_list(this_row, from_label_columns))
|
|
83
|
+
from_labels = lm.sort_relationship_labels(self.required_labels, validated_csv_from_labels)
|
|
84
|
+
for to_node in self.contents.get_property_values_list(this_row, to_columns, self.primary_property, self.primary_property):
|
|
85
|
+
validated_csv_to_labels = lm.validate_labels(self.contents.get_column_values_list(this_row, to_label_columns))
|
|
86
|
+
to_labels = lm.sort_relationship_labels(self.required_labels, validated_csv_to_labels)
|
|
87
|
+
|
|
88
|
+
# Build a MERGE query for the relationship(s)
|
|
89
|
+
for relationship in relationships:
|
|
90
|
+
for relation in relationship.split(':'):
|
|
91
|
+
if relation:
|
|
92
|
+
|
|
93
|
+
time_now = amu.get_formatted_time_now()
|
|
94
|
+
query_text = (QueryBuilder()
|
|
95
|
+
.match()
|
|
96
|
+
.node(labels=from_labels, ref_name=from_ref, properties=Properties(from_node))
|
|
97
|
+
.match()
|
|
98
|
+
.node(labels=to_labels, ref_name=to_ref, properties=Properties(to_node))
|
|
99
|
+
.merge()
|
|
100
|
+
.node(ref_name=from_ref)
|
|
101
|
+
.related_to(label=relation, ref_name=relation_ref)
|
|
102
|
+
.node(ref_name=to_ref)
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
# Add in System properties
|
|
106
|
+
if self.add_system_properties:
|
|
107
|
+
create_time = relation_ref_dot + self.creation_time_property + '=datetime("' + time_now + '")'
|
|
108
|
+
update_time = relation_ref_dot + self.update_time_property + '=datetime("' + time_now + '")'
|
|
109
|
+
query_text = query_text.on_create().set_literal(create_time).set_literal(update_time)
|
|
110
|
+
|
|
111
|
+
if len(properties) > 0:
|
|
112
|
+
query_text = query_text.set(properties=properties, property_ref=relation_ref_dot)
|
|
113
|
+
query_text = query_text.toStr()
|
|
114
|
+
query = Neo4jQuery('relationship', query_text, time_now)
|
|
115
|
+
|
|
116
|
+
if self.validate_only and not self.summarise:
|
|
117
|
+
print(query.text)
|
|
118
|
+
|
|
119
|
+
queries.append(query)
|
|
120
|
+
|
|
121
|
+
this_row += 1
|
|
122
|
+
messages.message_no_cr(f'Generating queries... {100 * (this_row - self.start_from + 1) / num_rows:.0f}%{chr(13)}')
|
|
123
|
+
|
|
124
|
+
messages.message('', False)
|
|
125
|
+
return queries
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def process_relationship_file_batch(self):
|
|
129
|
+
|
|
130
|
+
# For each line in the file, build a cypher query to MERGE a relationship
|
|
131
|
+
# Each query is added to a list of queries, which are returned to the caller. They are NOT actioned here
|
|
132
|
+
|
|
133
|
+
num_data_rows = self.contents.get_row_count()
|
|
134
|
+
from_columns = self.contents.get_column_numbers_list(self.from_node_alias, self.from_types)
|
|
135
|
+
from_label_columns = self.contents.get_column_numbers_list(self.from_label_alias)
|
|
136
|
+
to_columns = self.contents.get_column_numbers_list(self.to_node_alias, self.to_types)
|
|
137
|
+
to_label_columns = self.contents.get_column_numbers_list(self.to_label_alias)
|
|
138
|
+
relation_columns = self.contents.get_column_numbers_list(self.relationship_alias, self.relation_types)
|
|
139
|
+
other_columns = self.from_node_alias + self.from_label_alias + self.to_node_alias + self.to_label_alias + self.relationship_alias
|
|
140
|
+
property_columns = self.contents.get_other_column_numbers_list(other_columns, self.all_types)
|
|
141
|
+
|
|
142
|
+
queries = []
|
|
143
|
+
from_ref = 'from'
|
|
144
|
+
relation_ref = 'rel'
|
|
145
|
+
to_ref = 'to'
|
|
146
|
+
relation_ref_dot = relation_ref + '.'
|
|
147
|
+
payload_ref = 'payload'
|
|
148
|
+
payload_ref_dot = payload_ref + '.'
|
|
149
|
+
|
|
150
|
+
time_now = amu.get_formatted_time_now()
|
|
151
|
+
|
|
152
|
+
# For best optimisation, we need to create a batch query for all the update with the same labels
|
|
153
|
+
# So we will create a dictionary of queries based on the labels and relationships for each query
|
|
154
|
+
# Since the dictionary key can't be a list, we will create a list (of lists) and use the index in that list as the key
|
|
155
|
+
# When we have worked through all the input and grouped the queries by label and relationship combinations, we will create the queries
|
|
156
|
+
|
|
157
|
+
batch_keys = []
|
|
158
|
+
query_dictionary = {}
|
|
159
|
+
dict_size = 0
|
|
160
|
+
|
|
161
|
+
num_rows = num_data_rows - self.start_from + 1
|
|
162
|
+
this_row = self.start_from-1
|
|
163
|
+
last_row = self.get_last_row(num_data_rows)
|
|
164
|
+
while this_row < last_row:
|
|
165
|
+
relationships = rm.validate_relationships(self.contents.get_column_values_list(this_row, relation_columns))
|
|
166
|
+
properties = self.contents.get_property_values_dict(this_row, property_columns, self.blank_property_list)
|
|
167
|
+
# If there are multiple From or To nodes, create a relationship query for each combination
|
|
168
|
+
for from_node in self.contents.get_property_values_list(this_row, from_columns, self.primary_property, self.primary_property):
|
|
169
|
+
validated_csv_from_labels = lm.validate_labels(self.contents.get_column_values_list(this_row, from_label_columns))
|
|
170
|
+
from_labels = lm.sort_relationship_labels(self.required_labels, validated_csv_from_labels)
|
|
171
|
+
for to_node in self.contents.get_property_values_list(this_row, to_columns, self.primary_property, self.primary_property):
|
|
172
|
+
validated_csv_to_labels = lm.validate_labels(self.contents.get_column_values_list(this_row, to_label_columns))
|
|
173
|
+
to_labels = lm.sort_relationship_labels(self.required_labels, validated_csv_to_labels)
|
|
174
|
+
|
|
175
|
+
# Build a MERGE query for the relationship(s)
|
|
176
|
+
for relationship in relationships:
|
|
177
|
+
for relation in relationship.split(':'):
|
|
178
|
+
if relation:
|
|
179
|
+
|
|
180
|
+
explicit_properties, calculated_properties = pm.split_out_functional_properties(properties)
|
|
181
|
+
new_labels = [from_labels, to_labels, relation, list(from_node)[0], list(to_node)[0], calculated_properties]
|
|
182
|
+
query_data = [list(from_node.values())[0], list(to_node.values())[0], explicit_properties, calculated_properties, time_now]
|
|
183
|
+
|
|
184
|
+
# So now we know all about this particular update. Let's put it in the query dictionary
|
|
185
|
+
try:
|
|
186
|
+
dictionary_index = batch_keys.index(new_labels)
|
|
187
|
+
query_dictionary[dictionary_index].append(query_data)
|
|
188
|
+
except:
|
|
189
|
+
dictionary_index = len(batch_keys)
|
|
190
|
+
query_dictionary[dictionary_index] = [query_data]
|
|
191
|
+
batch_keys.append(new_labels)
|
|
192
|
+
dict_size += 1
|
|
193
|
+
|
|
194
|
+
this_row += 1
|
|
195
|
+
messages.message_no_cr(f'Analysing file... {100 * (this_row - self.start_from + 1) / num_rows:.0f}%{chr(13)}')
|
|
196
|
+
|
|
197
|
+
messages.message('', False)
|
|
198
|
+
|
|
199
|
+
# Right, so now we have a dictionary of updates grouped by labels. Let's create the queries
|
|
200
|
+
query_count = 0
|
|
201
|
+
for from_to_labels, updates in query_dictionary.items():
|
|
202
|
+
num_updates_for_this_label = len(updates)
|
|
203
|
+
num_batches = int((num_updates_for_this_label - 1) / self.batch_rate) + 1
|
|
204
|
+
from_labels = batch_keys[from_to_labels][0]
|
|
205
|
+
to_labels = batch_keys[from_to_labels][1]
|
|
206
|
+
relationship = batch_keys[from_to_labels][2]
|
|
207
|
+
from_key, from_type = pm.split_node_key(batch_keys[from_to_labels][3])
|
|
208
|
+
to_key, to_type = pm.split_node_key(batch_keys[from_to_labels][4])
|
|
209
|
+
|
|
210
|
+
batch_num = 1
|
|
211
|
+
while batch_num <= num_batches:
|
|
212
|
+
if batch_num == num_batches:
|
|
213
|
+
# Last batch, so
|
|
214
|
+
batch_size = 1 + (num_updates_for_this_label - 1) % self.batch_rate
|
|
215
|
+
else:
|
|
216
|
+
batch_size = self.batch_rate
|
|
217
|
+
|
|
218
|
+
this_size = 0
|
|
219
|
+
payload = []
|
|
220
|
+
while this_size < batch_size:
|
|
221
|
+
# Combine all the update data...
|
|
222
|
+
update = updates[(batch_num - 1) * self.batch_rate + this_size]
|
|
223
|
+
update_properties = update[2] or {} # In case update_properties[2] is {}
|
|
224
|
+
func_properties = update[3] or {} # In case update_properties[3] is {}
|
|
225
|
+
update_properties[from_ref + from_type] = update[0]
|
|
226
|
+
update_properties[to_ref + to_type] = update[1]
|
|
227
|
+
if self.add_system_properties:
|
|
228
|
+
update_properties[self.update_time_property] = update[4]
|
|
229
|
+
payload.append(update_properties)
|
|
230
|
+
this_size += 1
|
|
231
|
+
query_count += 1
|
|
232
|
+
|
|
233
|
+
formatted_payload = []
|
|
234
|
+
for a_payload in payload:
|
|
235
|
+
formatted_payload.append(Properties(a_payload).format())
|
|
236
|
+
# Build a MERGE query using queries
|
|
237
|
+
from_keys = " {" + from_key + ":" + payload_ref_dot + from_ref + "}"
|
|
238
|
+
to_keys = " {" + to_key + ":" + payload_ref_dot + to_ref + "}"
|
|
239
|
+
create_time = relation_ref_dot + self.creation_time_property + '=datetime("' + time_now + '")'
|
|
240
|
+
update_time = relation_ref_dot + self.update_time_property + '=datetime("' + time_now + '")'
|
|
241
|
+
|
|
242
|
+
query_text = (QueryBuilder()
|
|
243
|
+
.unwind_list_as(formatted_payload, payload_ref)
|
|
244
|
+
.match()
|
|
245
|
+
.node(labels=from_labels, ref_name=from_ref, properties_literal=from_keys)
|
|
246
|
+
.match()
|
|
247
|
+
.node(labels=to_labels, ref_name=to_ref, properties_literal=to_keys)
|
|
248
|
+
.merge()
|
|
249
|
+
.node(ref_name=from_ref)
|
|
250
|
+
.related_to(label=relationship, ref_name=relation_ref)
|
|
251
|
+
.node(ref_name=to_ref)
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
if self.add_system_properties:
|
|
255
|
+
query_text = query_text.on_create().set_literal(create_time)
|
|
256
|
+
|
|
257
|
+
query_text = query_text.set_literal(relation_ref + ' += ' + payload_ref)
|
|
258
|
+
if func_properties:
|
|
259
|
+
query_text = query_text.set_functional(func_properties, relation_ref_dot)
|
|
260
|
+
|
|
261
|
+
if self.add_system_properties:
|
|
262
|
+
query_text = query_text.set_literal(update_time)
|
|
263
|
+
|
|
264
|
+
query_text = query_text.remove_literal(relation_ref_dot + from_ref + ', ' + relation_ref_dot + to_ref)
|
|
265
|
+
query_text = query_text.toStr()
|
|
266
|
+
|
|
267
|
+
query = Neo4jQuery('batch relationship', query_text, time_now, batch_size=this_size)
|
|
268
|
+
|
|
269
|
+
if self.validate_only and not self.summarise:
|
|
270
|
+
print(query.text)
|
|
271
|
+
|
|
272
|
+
queries.append(query)
|
|
273
|
+
messages.message_no_cr(f'Generating queries... {100 * query_count / dict_size:.0f}%{chr(13)}')
|
|
274
|
+
|
|
275
|
+
batch_num += 1
|
|
276
|
+
|
|
277
|
+
messages.message('', False)
|
|
278
|
+
return queries
|
|
279
|
+
|
|
280
|
+
def get_filename(self):
|
|
281
|
+
return self.filename
|
|
282
|
+
|
|
283
|
+
def is_inline_query(self):
|
|
284
|
+
return False
|
|
285
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import assetmodelutilities as amu
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def validate_relationship_string(relationship_string):
|
|
5
|
+
# The input string can be comma or colon separated.
|
|
6
|
+
# We will return a string of colon separated relationships
|
|
7
|
+
# Labels that do not conform to Neo4j naming rules will be back-ticked (`)
|
|
8
|
+
# For example:
|
|
9
|
+
# fruit --> fruit
|
|
10
|
+
# apple,tree --> apple:tree
|
|
11
|
+
# birds&bees --> `birds&bees`
|
|
12
|
+
# one,two,!three --> one:two:`!three`
|
|
13
|
+
|
|
14
|
+
result = ''
|
|
15
|
+
for a_relationship in relationship_string.replace(",", ":").split(':'):
|
|
16
|
+
result += ':' + amu.validate(a_relationship)
|
|
17
|
+
|
|
18
|
+
# Remove the leading ':' from the result because we don't want it
|
|
19
|
+
if result.startswith(':'):
|
|
20
|
+
result = result[1:]
|
|
21
|
+
|
|
22
|
+
return result
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def validate_relationships(relationships):
|
|
26
|
+
# We either get a string, or a list of strings.
|
|
27
|
+
# Process as appropriate and return a string or list of strings to match input
|
|
28
|
+
if isinstance(relationships, str):
|
|
29
|
+
return validate_relationship_string(relationships)
|
|
30
|
+
else:
|
|
31
|
+
return [validate_relationship_string(i) for i in relationships]
|
|
File without changes
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
# Query Builder based on Cymple's OpenSource code
|
|
2
|
+
from typing import List
|
|
3
|
+
from queries.typedefs import Mapping, Properties
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Query():
|
|
7
|
+
|
|
8
|
+
def __init__(self, query):
|
|
9
|
+
self.query = query
|
|
10
|
+
|
|
11
|
+
def __str__(self) -> str:
|
|
12
|
+
return self.query.strip()
|
|
13
|
+
|
|
14
|
+
def __add__(self, other):
|
|
15
|
+
return Query(self.query.strip() + ' ' + other.query.strip())
|
|
16
|
+
|
|
17
|
+
def __iadd__(self, other):
|
|
18
|
+
self.query = self.query.strip() + ' ' + other.query.strip()
|
|
19
|
+
return self
|
|
20
|
+
|
|
21
|
+
def toStr(self):
|
|
22
|
+
return str(self)
|
|
23
|
+
|
|
24
|
+
class QueryStart(Query):
|
|
25
|
+
""" """
|
|
26
|
+
|
|
27
|
+
class Call(Query):
|
|
28
|
+
|
|
29
|
+
def call(self):
|
|
30
|
+
return ValidAfterCall(self.query + ' CALL')
|
|
31
|
+
|
|
32
|
+
class CaseWhen(Query):
|
|
33
|
+
|
|
34
|
+
def case_when(self, filters: Properties, on_true: str, on_false: str, ref_name: str, comparison_operator: str = "=", boolean_operator: str = "AND"):
|
|
35
|
+
filt = ' CASE WHEN ' + Properties(filters).to_str(comparison_operator, boolean_operator)
|
|
36
|
+
filt += f' THEN {on_true} ELSE {on_false} END as {ref_name}'
|
|
37
|
+
return ValidAfterCaseWhen(self.query + filt)
|
|
38
|
+
|
|
39
|
+
class Delete(Query):
|
|
40
|
+
|
|
41
|
+
def delete(self, ref_name: str):
|
|
42
|
+
ret = f' DELETE {ref_name}'
|
|
43
|
+
return Query(self.query + ret)
|
|
44
|
+
|
|
45
|
+
def detach_delete(self, ref_name: str):
|
|
46
|
+
ret = f' DETACH DELETE {ref_name}'
|
|
47
|
+
return Query(self.query + ret)
|
|
48
|
+
|
|
49
|
+
class Where(Query):
|
|
50
|
+
|
|
51
|
+
def where_literal(self, literal: str):
|
|
52
|
+
if literal:
|
|
53
|
+
return ValidAfterWhere(self.query + f' WHERE {literal}')
|
|
54
|
+
else:
|
|
55
|
+
return ValidAfterWhere(self.query)
|
|
56
|
+
|
|
57
|
+
def where(self, name: str, comparison_operator: str, value: str):
|
|
58
|
+
return self.where_multiple({name: value}, comparison_operator)
|
|
59
|
+
|
|
60
|
+
def where_multiple(self, filters: Properties, comparison_operator: str = "=", boolean_operator: str = ' AND '):
|
|
61
|
+
filt = ' WHERE ' + Properties(filters).to_str(comparison_operator, boolean_operator)
|
|
62
|
+
return ValidAfterWhere(self.query + filt)
|
|
63
|
+
|
|
64
|
+
def where_additional(self, filters: Properties, comparison_operator: str = "=", boolean_operator: str = ' AND '):
|
|
65
|
+
filt = boolean_operator + Properties(filters).to_str(comparison_operator, boolean_operator)
|
|
66
|
+
return ValidAfterWhere(self.query + filt)
|
|
67
|
+
|
|
68
|
+
class Match(Query):
|
|
69
|
+
|
|
70
|
+
def match(self):
|
|
71
|
+
return ValidAfterMatch(self.query + ' MATCH')
|
|
72
|
+
|
|
73
|
+
def match_optional(self):
|
|
74
|
+
return ValidAfterMatch(self.query + ' OPTIONAL MATCH')
|
|
75
|
+
|
|
76
|
+
class Merge(Query):
|
|
77
|
+
|
|
78
|
+
def merge(self):
|
|
79
|
+
return ValidAfterMerge(self.query + ' MERGE')
|
|
80
|
+
|
|
81
|
+
class Node(Query):
|
|
82
|
+
|
|
83
|
+
def node(self, labels: List[str] = None, ref_name: str = None, properties: Properties = None, properties_literal: str = ''):
|
|
84
|
+
if not labels:
|
|
85
|
+
labels_string = ''
|
|
86
|
+
elif isinstance(labels, str):
|
|
87
|
+
labels_string = labels
|
|
88
|
+
else:
|
|
89
|
+
labels_string = f':{":".join(labels).strip()}'
|
|
90
|
+
|
|
91
|
+
if properties_literal:
|
|
92
|
+
property_string = properties_literal
|
|
93
|
+
else:
|
|
94
|
+
if not properties:
|
|
95
|
+
property_string = ''
|
|
96
|
+
else:
|
|
97
|
+
property_string = f' {{{Properties(properties).to_str(property_ref=ref_name)}}}'
|
|
98
|
+
|
|
99
|
+
ref_name = ref_name or ''
|
|
100
|
+
|
|
101
|
+
if not (self.query.endswith('-') or self.query.endswith('>') or self.query.endswith('<')):
|
|
102
|
+
self.query += ' '
|
|
103
|
+
|
|
104
|
+
return ValidAfterNode(self.query + f'({ref_name}{labels_string}{property_string})')
|
|
105
|
+
|
|
106
|
+
def node_batch(self, labels: List[str] = None, ref_name: str = None, properties: str = None):
|
|
107
|
+
if not labels:
|
|
108
|
+
labels_string = ''
|
|
109
|
+
elif isinstance(labels, str):
|
|
110
|
+
labels_string = labels
|
|
111
|
+
else:
|
|
112
|
+
labels_string = f':{":".join(labels).strip()}'.replace('::', ':')
|
|
113
|
+
|
|
114
|
+
ref_name = ref_name or ''
|
|
115
|
+
|
|
116
|
+
if not (self.query.endswith('-') or self.query.endswith('>') or self.query.endswith('<')):
|
|
117
|
+
self.query += ' '
|
|
118
|
+
|
|
119
|
+
return ValidAfterNode(self.query + f'({ref_name}{labels_string} {{{properties}}})')
|
|
120
|
+
|
|
121
|
+
class OnCreate(Query):
|
|
122
|
+
|
|
123
|
+
def on_create(self):
|
|
124
|
+
return ValidAfterOnCreate(self.query + ' ON CREATE')
|
|
125
|
+
|
|
126
|
+
class OnMatch(Query):
|
|
127
|
+
|
|
128
|
+
def on_match(self):
|
|
129
|
+
return ValidAfterOnMatch(self.query + ' ON MATCH')
|
|
130
|
+
|
|
131
|
+
class OperatorEnd(Query):
|
|
132
|
+
|
|
133
|
+
def operator_end(self):
|
|
134
|
+
return ValidAfterOperatorEnd(self.query + ' )')
|
|
135
|
+
|
|
136
|
+
class OperatorStart(Query):
|
|
137
|
+
|
|
138
|
+
def operator_start(self, operator: str, ref_name: str = None, args: Properties = None):
|
|
139
|
+
result_name = '' if ref_name is None else f'{ref_name} = '
|
|
140
|
+
arguments = '' if args is None else f' {args}'
|
|
141
|
+
|
|
142
|
+
return ValidAfterOperatorStartValidAs(self.query + f' {result_name}{operator}({arguments}')
|
|
143
|
+
|
|
144
|
+
class Relation(Query):
|
|
145
|
+
|
|
146
|
+
def related(self, label: str = None, ref_name: str = None, properties: Properties = None):
|
|
147
|
+
return ValidAfterRelation(self.query + self._directed_relation('none', label, ref_name, properties))
|
|
148
|
+
|
|
149
|
+
def related_to(self, label: str = None, ref_name: str = None, properties: Properties = {}):
|
|
150
|
+
return ValidAfterRelation(self.query + self._directed_relation('forward', label, ref_name, properties))
|
|
151
|
+
|
|
152
|
+
def related_from(self, label: str = None, ref_name: str = None, properties: Properties = {}):
|
|
153
|
+
return ValidAfterRelation(self.query + self._directed_relation('backward', label, ref_name, properties))
|
|
154
|
+
|
|
155
|
+
def related_variable_len(self, min_hops: int = -1, max_hops: int = -1):
|
|
156
|
+
min_hops_str = '' if min_hops == -1 else str(min_hops)
|
|
157
|
+
max_hops_str = '' if max_hops == -1 else str(max_hops)
|
|
158
|
+
|
|
159
|
+
relation_length = '*' if min_hops == -1 and max_hops == - \
|
|
160
|
+
1 else (f'*{min_hops_str}'if min_hops == max_hops else f'*{min_hops_str}..{max_hops_str}')
|
|
161
|
+
|
|
162
|
+
if relation_length:
|
|
163
|
+
relation_str = f'[{relation_length}]'
|
|
164
|
+
else:
|
|
165
|
+
relation_str = ''
|
|
166
|
+
|
|
167
|
+
return ValidAfterRelation(self.query + f'-{relation_str}-')
|
|
168
|
+
|
|
169
|
+
def _directed_relation(self, direction: str, label: str, ref_name: str = None, properties: Properties = {}):
|
|
170
|
+
relation_type = '' if label is None else f':{label}'
|
|
171
|
+
relation_ref_name = '' if ref_name is None else f'{ref_name}'
|
|
172
|
+
relation_properties = f' {{{str(Properties(properties))}}}' if properties else ''
|
|
173
|
+
|
|
174
|
+
if relation_ref_name or relation_type:
|
|
175
|
+
relation_str = f'[{relation_ref_name}{relation_type}{relation_properties}]'
|
|
176
|
+
else:
|
|
177
|
+
relation_str = ''
|
|
178
|
+
|
|
179
|
+
if direction == 'forward':
|
|
180
|
+
return f'-{relation_str}->'
|
|
181
|
+
if direction == 'backward':
|
|
182
|
+
return f'<-{relation_str}-'
|
|
183
|
+
|
|
184
|
+
return f'-{relation_str}-'
|
|
185
|
+
|
|
186
|
+
class Remove(Query):
|
|
187
|
+
|
|
188
|
+
def remove_literal(self, set_literal):
|
|
189
|
+
return ValidAfterRemove(self.query + ' REMOVE ' + set_literal)
|
|
190
|
+
|
|
191
|
+
class Return(Query):
|
|
192
|
+
|
|
193
|
+
def return_literal(self, literal: str):
|
|
194
|
+
ret = f' RETURN {literal}'
|
|
195
|
+
return Query(self.query + ret)
|
|
196
|
+
|
|
197
|
+
def return_mapping(self, mappings: List[Mapping]):
|
|
198
|
+
if not isinstance(mappings, list):
|
|
199
|
+
mappings = [mappings]
|
|
200
|
+
|
|
201
|
+
ret = ' RETURN ' + \
|
|
202
|
+
', '.join(
|
|
203
|
+
f'{mapping[0]} as {mapping[1]}' if mapping[1] else mapping[0].replace(".", "_")
|
|
204
|
+
for mapping in mappings)
|
|
205
|
+
|
|
206
|
+
return Query(self.query + ret)
|
|
207
|
+
|
|
208
|
+
class Set(Query):
|
|
209
|
+
|
|
210
|
+
def set(self, properties: Properties, property_ref=''):
|
|
211
|
+
return ValidAfterSet(self.query + ' SET ' + Properties(properties).to_str("=", ",", property_ref))
|
|
212
|
+
|
|
213
|
+
def set_functional(self, properties: Properties, property_ref=''):
|
|
214
|
+
return ValidAfterSet(self.query + ' SET ' + Properties(properties).to_str("=", ",", property_ref, property_ref))
|
|
215
|
+
|
|
216
|
+
def set_labels(self, ref_name: str = '', labels: List[str] = None):
|
|
217
|
+
if not labels:
|
|
218
|
+
labels_string = ''
|
|
219
|
+
elif isinstance(labels, str):
|
|
220
|
+
labels_string = f'{labels}'
|
|
221
|
+
else:
|
|
222
|
+
labels_string = f':{":".join(labels).strip()}'
|
|
223
|
+
|
|
224
|
+
return ValidAfterSet(self.query + ' SET ' + ref_name + labels_string)
|
|
225
|
+
|
|
226
|
+
def set_literal(self, set_literal):
|
|
227
|
+
return ValidAfterSet(self.query + ' SET ' + set_literal)
|
|
228
|
+
|
|
229
|
+
class Unwind(Query):
|
|
230
|
+
|
|
231
|
+
def unwind(self, variables: str):
|
|
232
|
+
return ValidAfterUnwind(self.query + f' UNWIND {variables}')
|
|
233
|
+
|
|
234
|
+
def unwind_list_as(self, variables: list, ref):
|
|
235
|
+
unwind = variables[0]
|
|
236
|
+
next = 1
|
|
237
|
+
while next < len(variables):
|
|
238
|
+
unwind += ','+variables[next]
|
|
239
|
+
next += 1
|
|
240
|
+
|
|
241
|
+
return ValidAfterUnwind(self.query + f' UNWIND [{unwind}] AS {ref}')
|
|
242
|
+
|
|
243
|
+
class With(Query):
|
|
244
|
+
|
|
245
|
+
def with_(self, variables: str):
|
|
246
|
+
return ValidAfterWith(self.query + f' WITH {variables}')
|
|
247
|
+
|
|
248
|
+
class Yield(Query):
|
|
249
|
+
|
|
250
|
+
def yield_(self, mappings: List[Mapping]):
|
|
251
|
+
if not isinstance(mappings, list):
|
|
252
|
+
mappings = [mappings]
|
|
253
|
+
|
|
254
|
+
query = ' YIELD ' + \
|
|
255
|
+
', '.join(f'{mapping[0]} as '
|
|
256
|
+
f'{mapping[1] if mapping[1] else mapping[0].replace(".", "_")}'
|
|
257
|
+
for mapping in mappings)
|
|
258
|
+
return ValidAfterYield(self.query + query)
|
|
259
|
+
|
|
260
|
+
class ValidAsQueryStart(Match, Merge, Call, Unwind):
|
|
261
|
+
""" """
|
|
262
|
+
|
|
263
|
+
class ValidAfterCall(Node, Return, OperatorStart):
|
|
264
|
+
""" """
|
|
265
|
+
|
|
266
|
+
class ValidAfterCaseWhen(ValidAsQueryStart, With, Unwind, Where, CaseWhen, Return):
|
|
267
|
+
""" """
|
|
268
|
+
|
|
269
|
+
class ValidAfterDelete(Query):
|
|
270
|
+
""" """
|
|
271
|
+
|
|
272
|
+
class ValidAfterWhere(Return, Delete, With, Where, OperatorStart, ValidAsQueryStart, Set, Remove):
|
|
273
|
+
""" """
|
|
274
|
+
|
|
275
|
+
class ValidAfterMatch(Node, Return, OperatorStart):
|
|
276
|
+
""" """
|
|
277
|
+
|
|
278
|
+
class ValidAfterMerge(Node, Return, OperatorStart):
|
|
279
|
+
""" """
|
|
280
|
+
|
|
281
|
+
class ValidAfterNode(Relation, Return, Delete, With, Where, OperatorStart, OperatorEnd, Remove, Set, OnCreate, OnMatch, ValidAsQueryStart):
|
|
282
|
+
""" """
|
|
283
|
+
|
|
284
|
+
class ValidAfterOnCreate(Set, OperatorStart):
|
|
285
|
+
""" """
|
|
286
|
+
|
|
287
|
+
class ValidAfterOnMatch(Set, OperatorStart):
|
|
288
|
+
""" """
|
|
289
|
+
|
|
290
|
+
class ValidAfterOperatorEnd(ValidAsQueryStart, Yield, With, Return):
|
|
291
|
+
""" """
|
|
292
|
+
|
|
293
|
+
class ValidAfterOperatorStartValidAs(ValidAsQueryStart, Node, With, OperatorEnd):
|
|
294
|
+
""" """
|
|
295
|
+
|
|
296
|
+
class ValidAfterRelation(Node):
|
|
297
|
+
""" """
|
|
298
|
+
|
|
299
|
+
class ValidAfterReturn(ValidAsQueryStart, With, Unwind, Return):
|
|
300
|
+
""" """
|
|
301
|
+
|
|
302
|
+
class ValidAfterSet(ValidAsQueryStart, OnMatch, OnCreate, Set, With, Unwind, Remove, Return):
|
|
303
|
+
""" """
|
|
304
|
+
|
|
305
|
+
class ValidAfterRemove(Set, With, Unwind, Remove, Return):
|
|
306
|
+
""" """
|
|
307
|
+
|
|
308
|
+
class ValidAfterUnwind(ValidAsQueryStart, With, Unwind, Return):
|
|
309
|
+
""" """
|
|
310
|
+
|
|
311
|
+
class ValidAfterWith(ValidAsQueryStart, With, Unwind, Where, CaseWhen, Return):
|
|
312
|
+
""" """
|
|
313
|
+
|
|
314
|
+
class ValidAfterYield(ValidAsQueryStart, Node, With):
|
|
315
|
+
""" """
|
|
316
|
+
|
|
317
|
+
class QueryBuilder(ValidAsQueryStart):
|
|
318
|
+
def __init__(self) -> None:
|
|
319
|
+
super().__init__('')
|
|
320
|
+
|
|
321
|
+
def reset(self):
|
|
322
|
+
self.query = ''
|
|
323
|
+
return self
|