5
5
from .execution_plan import ExecutionPlan
6
6
from .query_result import AsyncQueryResult , QueryResult
7
7
8
+ PROFILE_CMD = "GRAPH.PROFILE"
9
+ RO_QUERY_CMD = "GRAPH.RO_QUERY"
10
+ QUERY_CMD = "GRAPH.QUERY"
11
+ DELETE_CMD = "GRAPH.DELETE"
12
+ SLOWLOG_CMD = "GRAPH.SLOWLOG"
13
+ CONFIG_CMD = "GRAPH.CONFIG"
14
+ LIST_CMD = "GRAPH.LIST"
15
+ EXPLAIN_CMD = "GRAPH.EXPLAIN"
16
+
8
17
9
18
class GraphCommands :
10
19
"""RedisGraph Commands"""
@@ -58,26 +67,22 @@ def query(self, q, params=None, timeout=None, read_only=False, profile=False):
58
67
# ask for compact result-set format
59
68
# specify known graph version
60
69
if profile :
61
- cmd = "GRAPH.PROFILE"
70
+ cmd = PROFILE_CMD
62
71
else :
63
- cmd = "GRAPH.RO_QUERY" if read_only else "GRAPH.QUERY"
72
+ cmd = RO_QUERY_CMD if read_only else QUERY_CMD
64
73
command = [cmd , self .name , query , "--compact" ]
65
74
66
75
# include timeout is specified
67
- if timeout :
68
- if not isinstance ( timeout , int ):
69
- raise Exception ( "Timeout argument must be a positive integer" )
70
- command += [ "timeout" , timeout ]
76
+ if isinstance ( timeout , int ) :
77
+ command . extend ([ " timeout" , timeout ])
78
+ elif timeout is not None :
79
+ raise Exception ( "Timeout argument must be a positive integer" )
71
80
72
81
# issue query
73
82
try :
74
83
response = self .execute_command (* command )
75
84
return QueryResult (self , response , profile )
76
85
except ResponseError as e :
77
- if "wrong number of arguments" in str (e ):
78
- print (
79
- "Note: RedisGraph Python requires server version 2.2.8 or above"
80
- ) # noqa
81
86
if "unknown command" in str (e ) and read_only :
82
87
# `GRAPH.RO_QUERY` is unavailable in older versions.
83
88
return self .query (q , params , timeout , read_only = False )
@@ -105,7 +110,7 @@ def delete(self):
105
110
For more information see `DELETE <https://redis.io/commands/graph.delete>`_. # noqa
106
111
"""
107
112
self ._clear_schema ()
108
- return self .execute_command ("GRAPH.DELETE" , self .name )
113
+ return self .execute_command (DELETE_CMD , self .name )
109
114
110
115
# declared here, to override the built in redis.db.flush()
111
116
def flush (self ):
@@ -145,7 +150,7 @@ def slowlog(self):
145
150
3. The issued query.
146
151
4. The amount of time needed for its execution, in milliseconds.
147
152
"""
148
- return self .execute_command ("GRAPH.SLOWLOG" , self .name )
153
+ return self .execute_command (SLOWLOG_CMD , self .name )
149
154
150
155
def config (self , name , value = None , set = False ):
151
156
"""
@@ -169,14 +174,14 @@ def config(self, name, value=None, set=False):
169
174
raise DataError (
170
175
"``value`` can be provided only when ``set`` is True"
171
176
) # noqa
172
- return self .execute_command ("GRAPH.CONFIG" , * params )
177
+ return self .execute_command (CONFIG_CMD , * params )
173
178
174
179
def list_keys (self ):
175
180
"""
176
181
Lists all graph keys in the keyspace.
177
182
For more information see `GRAPH.LIST <https://redis.io/commands/graph.list>`_. # noqa
178
183
"""
179
- return self .execute_command ("GRAPH.LIST" )
184
+ return self .execute_command (LIST_CMD )
180
185
181
186
def execution_plan (self , query , params = None ):
182
187
"""
@@ -189,7 +194,7 @@ def execution_plan(self, query, params=None):
189
194
"""
190
195
query = self ._build_params_header (params ) + query
191
196
192
- plan = self .execute_command ("GRAPH.EXPLAIN" , self .name , query )
197
+ plan = self .execute_command (EXPLAIN_CMD , self .name , query )
193
198
if isinstance (plan [0 ], bytes ):
194
199
plan = [b .decode () for b in plan ]
195
200
return "\n " .join (plan )
@@ -206,7 +211,7 @@ def explain(self, query, params=None):
206
211
"""
207
212
query = self ._build_params_header (params ) + query
208
213
209
- plan = self .execute_command ("GRAPH.EXPLAIN" , self .name , query )
214
+ plan = self .execute_command (EXPLAIN_CMD , self .name , query )
210
215
return ExecutionPlan (plan )
211
216
212
217
@@ -241,26 +246,22 @@ async def query(self, q, params=None, timeout=None, read_only=False, profile=Fal
241
246
# ask for compact result-set format
242
247
# specify known graph version
243
248
if profile :
244
- cmd = "GRAPH.PROFILE"
249
+ cmd = PROFILE_CMD
245
250
else :
246
- cmd = "GRAPH.RO_QUERY" if read_only else "GRAPH.QUERY"
251
+ cmd = RO_QUERY_CMD if read_only else QUERY_CMD
247
252
command = [cmd , self .name , query , "--compact" ]
248
253
249
254
# include timeout is specified
250
- if timeout :
251
- if not isinstance ( timeout , int ):
252
- raise Exception ( "Timeout argument must be a positive integer" )
253
- command += [ "timeout" , timeout ]
255
+ if isinstance ( timeout , int ) :
256
+ command . extend ([ " timeout" , timeout ])
257
+ elif timeout is not None :
258
+ raise Exception ( "Timeout argument must be a positive integer" )
254
259
255
260
# issue query
256
261
try :
257
262
response = await self .execute_command (* command )
258
263
return await AsyncQueryResult ().initialize (self , response , profile )
259
264
except ResponseError as e :
260
- if "wrong number of arguments" in str (e ):
261
- print (
262
- "Note: RedisGraph Python requires server version 2.2.8 or above"
263
- ) # noqa
264
265
if "unknown command" in str (e ) and read_only :
265
266
# `GRAPH.RO_QUERY` is unavailable in older versions.
266
267
return await self .query (q , params , timeout , read_only = False )
@@ -284,7 +285,7 @@ async def execution_plan(self, query, params=None):
284
285
"""
285
286
query = self ._build_params_header (params ) + query
286
287
287
- plan = await self .execute_command ("GRAPH.EXPLAIN" , self .name , query )
288
+ plan = await self .execute_command (EXPLAIN_CMD , self .name , query )
288
289
if isinstance (plan [0 ], bytes ):
289
290
plan = [b .decode () for b in plan ]
290
291
return "\n " .join (plan )
@@ -300,7 +301,7 @@ async def explain(self, query, params=None):
300
301
"""
301
302
query = self ._build_params_header (params ) + query
302
303
303
- plan = await self .execute_command ("GRAPH.EXPLAIN" , self .name , query )
304
+ plan = await self .execute_command (EXPLAIN_CMD , self .name , query )
304
305
return ExecutionPlan (plan )
305
306
306
307
async def flush (self ):
0 commit comments