99# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010# See the License for the specific language governing permissions and
1111# limitations under the License.
12+ from enum import Enum , unique
1213from typing import Iterable
1314
14- from trino import constants
1515import trino .client
1616import trino .exceptions
1717import trino .logging
18-
18+ from trino import constants
1919
2020logger = trino .logging .get_logger (__name__ )
2121
22-
2322NO_TRANSACTION = "NONE"
2423START_TRANSACTION = "START TRANSACTION"
2524ROLLBACK = "ROLLBACK"
2625COMMIT = "COMMIT"
2726
2827
29- class IsolationLevel (object ):
28+ @unique
29+ class IsolationLevel (Enum ):
3030 AUTOCOMMIT = 0
3131 READ_UNCOMMITTED = 1
3232 READ_COMMITTED = 2
@@ -35,16 +35,16 @@ class IsolationLevel(object):
3535
3636 @classmethod
3737 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 }
3939
4040 @classmethod
4141 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 }
4343
4444 @classmethod
4545 def check (cls , level : int ) -> int :
4646 if level not in cls .values ():
47- raise ValueError ("invalid isolation level {}" . format ( level ) )
47+ raise ValueError (f "invalid isolation level { level } " )
4848 return level
4949
5050
@@ -60,9 +60,7 @@ def id(self):
6060 def begin (self ):
6161 response = self ._request .post (START_TRANSACTION )
6262 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 } " )
6664 transaction_id = response .headers .get (constants .HEADER_STARTED_TRANSACTION )
6765 if transaction_id and transaction_id != NO_TRANSACTION :
6866 self ._id = response .headers [constants .HEADER_STARTED_TRANSACTION ]
@@ -74,16 +72,14 @@ def begin(self):
7472 self ._id = response .headers [constants .HEADER_STARTED_TRANSACTION ]
7573 status = self ._request .process (response )
7674 self ._request .transaction_id = self ._id
77- logger .info ("transaction started: " + self ._id )
75+ logger .info ("transaction started: %s" , self ._id )
7876
7977 def commit (self ):
8078 query = trino .client .TrinoQuery (self ._request , COMMIT )
8179 try :
8280 list (query .execute ())
8381 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
8783 self ._id = NO_TRANSACTION
8884 self ._request .transaction_id = self ._id
8985
@@ -92,8 +88,6 @@ def rollback(self):
9288 try :
9389 list (query .execute ())
9490 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
9892 self ._id = NO_TRANSACTION
9993 self ._request .transaction_id = self ._id
0 commit comments