@@ -27,7 +27,10 @@ class IsolationLevel(Enum):
27
27
Transaction isolation level for NodeConnection
28
28
"""
29
29
30
- ReadUncommitted , ReadCommitted , RepeatableRead , Serializable = range (4 )
30
+ ReadUncommitted = 'read uncommitted'
31
+ ReadCommitted = 'read committed'
32
+ RepeatableRead = 'repeatable read'
33
+ Serializable = 'serializable'
31
34
32
35
33
36
class NodeConnection (object ):
@@ -71,39 +74,21 @@ def __exit__(self, type, value, traceback):
71
74
self .close ()
72
75
73
76
def begin (self , isolation_level = IsolationLevel .ReadCommitted ):
74
- # yapf: disable
75
- levels = [
76
- 'read uncommitted' ,
77
- 'read committed' ,
78
- 'repeatable read' ,
79
- 'serializable'
80
- ]
81
-
82
- # Check if level is an IsolationLevel
83
- if (isinstance (isolation_level , IsolationLevel )):
84
-
85
- # Get index of isolation level
86
- level_idx = isolation_level .value
87
- assert level_idx in range (4 )
88
-
89
- # Replace isolation level with its name
90
- isolation_level = levels [level_idx ]
91
-
92
- else :
77
+ # Check if level isn't an IsolationLevel
78
+ if not isinstance (isolation_level , IsolationLevel ):
93
79
# Get name of isolation level
94
80
level_str = str (isolation_level ).lower ()
95
81
96
82
# Validate level string
97
- if level_str not in levels :
83
+ try :
84
+ isolation_level = IsolationLevel (level_str )
85
+ except ValueError :
98
86
error = 'Invalid isolation level "{}"'
99
87
raise QueryException (error .format (level_str ))
100
88
101
- # Replace isolation level with its name
102
- isolation_level = level_str
103
-
104
89
# Set isolation level
105
90
cmd = 'SET TRANSACTION ISOLATION LEVEL {}'
106
- self .cursor .execute (cmd .format (isolation_level ))
91
+ self .cursor .execute (cmd .format (isolation_level . value ))
107
92
108
93
return self
109
94
0 commit comments