|
9 | 9 | except ImportError: |
10 | 10 | raise ImportError("You must have psycopg2 or pg8000 modules installed") |
11 | 11 |
|
| 12 | +from .enums import IsolationLevel |
| 13 | + |
12 | 14 | from .defaults import \ |
13 | 15 | default_dbname, \ |
14 | 16 | default_username |
15 | 17 |
|
16 | | -from enum import Enum |
17 | | - |
18 | 18 | from .exceptions import QueryException |
19 | 19 |
|
20 | 20 |
|
|
23 | 23 | ProgrammingError = pglib.ProgrammingError |
24 | 24 |
|
25 | 25 |
|
26 | | -class IsolationLevel(Enum): |
27 | | - """ |
28 | | - Transaction isolation level for NodeConnection |
29 | | - """ |
30 | | - |
31 | | - ReadUncommitted, ReadCommitted, RepeatableRead, Serializable = range(4) |
32 | | - |
33 | | - |
34 | 26 | class NodeConnection(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 | def begin(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 | | - assert level_idx in range(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 | + if not isinstance(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 | | - if level_str not in levels: |
| 73 | + try: |
| 74 | + isolation_level = IsolationLevel(level_str) |
| 75 | + except ValueError: |
99 | 76 | error = 'Invalid isolation level "{}"' |
100 | 77 | raise QueryException(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 | return self |
110 | 84 |
|
|
0 commit comments