|
9 | 9 | exceptImportError: |
10 | 10 | raiseImportError("You must have psycopg2 or pg8000 modules installed") |
11 | 11 |
|
| 12 | +from .enumsimportIsolationLevel |
| 13 | + |
12 | 14 | from .defaultsimport \ |
13 | 15 | default_dbname, \ |
14 | 16 | default_username |
15 | 17 |
|
16 | | -fromenumimportEnum |
17 | | - |
18 | 18 | from .exceptionsimportQueryException |
19 | 19 |
|
20 | 20 |
|
|
23 | 23 | ProgrammingError=pglib.ProgrammingError |
24 | 24 |
|
25 | 25 |
|
26 | | -classIsolationLevel(Enum): |
27 | | -""" |
28 | | - Transaction isolation level for NodeConnection |
29 | | - """ |
30 | | - |
31 | | -ReadUncommitted,ReadCommitted,RepeatableRead,Serializable=range(4) |
32 | | - |
33 | | - |
34 | 26 | classNodeConnection(object): |
35 | 27 | """ |
36 | 28 | Transaction wrapper returned by Node |
@@ -72,39 +64,21 @@ def __exit__(self, type, value, traceback): |
72 | 64 | self.close() |
73 | 65 |
|
74 | 66 | defbegin(self,isolation_level=IsolationLevel.ReadCommitted): |
75 | | -# yapf: disable |
76 | | -levels= [ |
77 | | -'read uncommitted', |
78 | | -'read committed', |
79 | | -'repeatable read', |
80 | | -'serializable' |
81 | | - ] |
82 | | - |
83 | | -# Check if level is an IsolationLevel |
84 | | -if (isinstance(isolation_level,IsolationLevel)): |
85 | | - |
86 | | -# Get index of isolation level |
87 | | -level_idx=isolation_level.value |
88 | | -assertlevel_idxinrange(4) |
89 | | - |
90 | | -# Replace isolation level with its name |
91 | | -isolation_level=levels[level_idx] |
92 | | - |
93 | | -else: |
| 67 | +# Check if level isn't an IsolationLevel |
| 68 | +ifnotisinstance(isolation_level,IsolationLevel): |
94 | 69 | # Get name of isolation level |
95 | 70 | level_str=str(isolation_level).lower() |
96 | 71 |
|
97 | 72 | # Validate level string |
98 | | -iflevel_strnotinlevels: |
| 73 | +try: |
| 74 | +isolation_level=IsolationLevel(level_str) |
| 75 | +exceptValueError: |
99 | 76 | error='Invalid isolation level "{}"' |
100 | 77 | raiseQueryException(error.format(level_str)) |
101 | 78 |
|
102 | | -# Replace isolation level with its name |
103 | | -isolation_level=level_str |
104 | | - |
105 | 79 | # Set isolation level |
106 | 80 | cmd='SET TRANSACTION ISOLATION LEVEL {}' |
107 | | -self.cursor.execute(cmd.format(isolation_level)) |
| 81 | +self.cursor.execute(cmd.format(isolation_level.value)) |
108 | 82 |
|
109 | 83 | returnself |
110 | 84 |
|
|