9
9
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
10
# See the License for the specific language governing permissions and
11
11
# limitations under the License.
12
+ from enum import Enum , unique
12
13
from typing import Iterable
13
14
14
- from trino import constants
15
15
import trino .client
16
16
import trino .exceptions
17
17
import trino .logging
18
-
18
+ from trino import constants
19
19
20
20
logger = trino .logging .get_logger (__name__ )
21
21
22
-
23
22
NO_TRANSACTION = "NONE"
24
23
START_TRANSACTION = "START TRANSACTION"
25
24
ROLLBACK = "ROLLBACK"
26
25
COMMIT = "COMMIT"
27
26
28
27
29
- class IsolationLevel (object ):
28
+ @unique
29
+ class IsolationLevel (Enum ):
30
30
AUTOCOMMIT = 0
31
31
READ_UNCOMMITTED = 1
32
32
READ_COMMITTED = 2
@@ -35,16 +35,16 @@ class IsolationLevel(object):
35
35
36
36
@classmethod
37
37
def levels (cls ) -> Iterable [str ]:
38
- return {k for k , v in cls . __dict__ . items () if not k . startswith ( "_" ) and isinstance ( v , int ) }
38
+ return {isolation_level . name for isolation_level in IsolationLevel }
39
39
40
40
@classmethod
41
41
def values (cls ) -> Iterable [int ]:
42
- return {getattr ( cls , level ) for level in cls . levels () }
42
+ return {isolation_level . value for isolation_level in IsolationLevel }
43
43
44
44
@classmethod
45
45
def check (cls , level : int ) -> int :
46
46
if level not in cls .values ():
47
- raise ValueError ("invalid isolation level {}" . format ( level ) )
47
+ raise ValueError (f "invalid isolation level { level } " )
48
48
return level
49
49
50
50
@@ -60,9 +60,7 @@ def id(self):
60
60
def begin (self ):
61
61
response = self ._request .post (START_TRANSACTION )
62
62
if not response .ok :
63
- raise trino .exceptions .DatabaseError (
64
- "failed to start transaction: {}" .format (response .status_code )
65
- )
63
+ raise trino .exceptions .DatabaseError (f"failed to start transaction: { response .status_code } " )
66
64
transaction_id = response .headers .get (constants .HEADER_STARTED_TRANSACTION )
67
65
if transaction_id and transaction_id != NO_TRANSACTION :
68
66
self ._id = response .headers [constants .HEADER_STARTED_TRANSACTION ]
@@ -74,16 +72,14 @@ def begin(self):
74
72
self ._id = response .headers [constants .HEADER_STARTED_TRANSACTION ]
75
73
status = self ._request .process (response )
76
74
self ._request .transaction_id = self ._id
77
- logger .info ("transaction started: " + self ._id )
75
+ logger .info ("transaction started: %s" , self ._id )
78
76
79
77
def commit (self ):
80
78
query = trino .client .TrinoQuery (self ._request , COMMIT )
81
79
try :
82
80
list (query .execute ())
83
81
except Exception as err :
84
- raise trino .exceptions .DatabaseError (
85
- "failed to commit transaction {}: {}" .format (self ._id , err )
86
- )
82
+ raise trino .exceptions .DatabaseError (f"failed to commit transaction { self ._id } " ) from err
87
83
self ._id = NO_TRANSACTION
88
84
self ._request .transaction_id = self ._id
89
85
@@ -92,8 +88,6 @@ def rollback(self):
92
88
try :
93
89
list (query .execute ())
94
90
except Exception as err :
95
- raise trino .exceptions .DatabaseError (
96
- "failed to rollback transaction {}: {}" .format (self ._id , err )
97
- )
91
+ raise trino .exceptions .DatabaseError (f"failed to rollback transaction { self ._id } " ) from err
98
92
self ._id = NO_TRANSACTION
99
93
self ._request .transaction_id = self ._id
0 commit comments