1
1
from .node import Node
2
2
from .edge import Edge
3
3
from .path import Path
4
+ from .exceptions import VersionMismatchException
4
5
from prettytable import PrettyTable
5
6
from redis import ResponseError
6
7
8
+ LABELS_ADDED = 'Labels added'
9
+ NODES_CREATED = 'Nodes created'
10
+ NODES_DELETED = 'Nodes deleted'
11
+ RELATIONSHIPS_DELETED = 'Relationships deleted'
12
+ PROPERTIES_SET = 'Properties set'
13
+ RELATIONSHIPS_CREATED = 'Relationships created'
14
+ INDICES_CREATED = "Indices created"
15
+ INDICES_DELETED = "Indices deleted"
16
+ CACHED_EXECUTION = "Cached execution"
17
+ INTERNAL_EXECUTION_TIME = 'internal execution time'
18
+
19
+ STATS = [LABELS_ADDED , NODES_CREATED , PROPERTIES_SET , RELATIONSHIPS_CREATED ,
20
+ NODES_DELETED , RELATIONSHIPS_DELETED , INDICES_CREATED , INDICES_DELETED ,
21
+ CACHED_EXECUTION , INTERNAL_EXECUTION_TIME ]
7
22
8
23
class ResultSetColumnTypes (object ):
9
24
COLUMN_UNKNOWN = 0
10
25
COLUMN_SCALAR = 1
11
26
COLUMN_NODE = 2 # Unused as of RedisGraph v2.1.0, retained for backwards compatibility.
12
27
COLUMN_RELATION = 3 # Unused as of RedisGraph v2.1.0, retained for backwards compatibility.
13
28
14
-
15
29
class ResultSetScalarTypes (object ):
16
30
VALUE_UNKNOWN = 0
17
31
VALUE_NULL = 1
@@ -25,31 +39,33 @@ class ResultSetScalarTypes(object):
25
39
VALUE_PATH = 9
26
40
27
41
class QueryResult (object ):
28
- LABELS_ADDED = 'Labels added'
29
- NODES_CREATED = 'Nodes created'
30
- NODES_DELETED = 'Nodes deleted'
31
- RELATIONSHIPS_DELETED = 'Relationships deleted'
32
- PROPERTIES_SET = 'Properties set'
33
- RELATIONSHIPS_CREATED = 'Relationships created'
34
- INDICES_CREATED = "Indices created"
35
- INDICES_DELETED = "Indices deleted"
36
- CACHED_EXECUTION = "Cached execution"
37
- INTERNAL_EXECUTION_TIME = 'internal execution time'
38
42
39
43
def __init__ (self , graph , response ):
40
44
self .graph = graph
41
45
self .header = []
42
46
self .result_set = []
43
47
44
- # If we encountered a run-time error, the last response element will be an exception.
45
- if isinstance (response [- 1 ], ResponseError ):
46
- raise response [- 1 ]
48
+ # incase of an error an exception will be raised
49
+ self ._check_for_errors (response )
47
50
48
51
if len (response ) == 1 :
49
52
self .parse_statistics (response [0 ])
50
53
else :
51
- self . parse_results ( response )
54
+ # start by parsing statistics, matches the one we have
52
55
self .parse_statistics (response [- 1 ]) # Last element.
56
+ self .parse_results (response )
57
+
58
+ def _check_for_errors (self , response ):
59
+ if isinstance (response [0 ], ResponseError ):
60
+ error = response [0 ]
61
+ if str (error ) == "version mismatch" :
62
+ version = response [1 ]
63
+ error = VersionMismatchException (version )
64
+ raise error
65
+
66
+ # If we encountered a run-time error, the last response element will be an exception.
67
+ if isinstance (response [- 1 ], ResponseError ):
68
+ raise response [- 1 ]
53
69
54
70
def parse_results (self , raw_result_set ):
55
71
self .header = self .parse_header (raw_result_set )
@@ -63,10 +79,12 @@ def parse_results(self, raw_result_set):
63
79
def parse_statistics (self , raw_statistics ):
64
80
self .statistics = {}
65
81
66
- stats = [self .LABELS_ADDED , self .NODES_CREATED , self .PROPERTIES_SET , self .RELATIONSHIPS_CREATED ,
67
- self .NODES_DELETED , self .RELATIONSHIPS_DELETED , self .INDICES_CREATED , self .INDICES_DELETED ,
68
- self .CACHED_EXECUTION , self .INTERNAL_EXECUTION_TIME ]
69
- for s in stats :
82
+ # decode statistics
83
+ for idx , stat in enumerate (raw_statistics ):
84
+ if isinstance (stat , bytes ):
85
+ raw_statistics [idx ] = stat .decode ()
86
+
87
+ for s in STATS :
70
88
v = self ._get_value (s , raw_statistics )
71
89
if v is not None :
72
90
self .statistics [s ] = v
@@ -223,53 +241,51 @@ def is_empty(self):
223
241
@staticmethod
224
242
def _get_value (prop , statistics ):
225
243
for stat in statistics :
226
- if isinstance (stat , bytes ):
227
- stat = stat .decode ()
228
244
if prop in stat :
229
245
return float (stat .split (': ' )[1 ].split (' ' )[0 ])
230
246
231
-
232
247
return None
233
248
234
249
def _get_stat (self , stat ):
235
250
return self .statistics [stat ] if stat in self .statistics else 0
236
251
237
252
@property
238
253
def labels_added (self ):
239
- return self ._get_stat (self . LABELS_ADDED )
254
+ return self ._get_stat (LABELS_ADDED )
240
255
241
256
@property
242
257
def nodes_created (self ):
243
- return self ._get_stat (self . NODES_CREATED )
258
+ return self ._get_stat (NODES_CREATED )
244
259
245
260
@property
246
261
def nodes_deleted (self ):
247
- return self ._get_stat (self . NODES_DELETED )
262
+ return self ._get_stat (NODES_DELETED )
248
263
249
264
@property
250
265
def properties_set (self ):
251
- return self ._get_stat (self . PROPERTIES_SET )
266
+ return self ._get_stat (PROPERTIES_SET )
252
267
253
268
@property
254
269
def relationships_created (self ):
255
- return self ._get_stat (self . RELATIONSHIPS_CREATED )
270
+ return self ._get_stat (RELATIONSHIPS_CREATED )
256
271
257
272
@property
258
273
def relationships_deleted (self ):
259
- return self ._get_stat (self . RELATIONSHIPS_DELETED )
274
+ return self ._get_stat (RELATIONSHIPS_DELETED )
260
275
261
276
@property
262
277
def indices_created (self ):
263
- return self ._get_stat (self . INDICES_CREATED )
278
+ return self ._get_stat (INDICES_CREATED )
264
279
265
280
@property
266
281
def indices_deleted (self ):
267
- return self ._get_stat (self . INDICES_DELETED )
282
+ return self ._get_stat (INDICES_DELETED )
268
283
269
284
@property
270
285
def cached_execution (self ):
271
- return self ._get_stat (self . CACHED_EXECUTION ) == 1
286
+ return self ._get_stat (CACHED_EXECUTION ) == 1
272
287
273
288
@property
274
289
def run_time_ms (self ):
275
- return self ._get_stat (self .INTERNAL_EXECUTION_TIME )
290
+ return self ._get_stat (INTERNAL_EXECUTION_TIME )
291
+
0 commit comments