From 3f3ba519ae3c3346945928b21ab36f7238e5387e Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Thu, 11 Aug 2022 09:39:19 +0200 Subject: save computed payloads into sqlite db --- src/hydrilla/proxy/state_impl/base.py | 38 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'src/hydrilla/proxy/state_impl/base.py') diff --git a/src/hydrilla/proxy/state_impl/base.py b/src/hydrilla/proxy/state_impl/base.py index 78a50c0..788a93d 100644 --- a/src/hydrilla/proxy/state_impl/base.py +++ b/src/hydrilla/proxy/state_impl/base.py @@ -72,40 +72,38 @@ class HaketiloStateWithFields(state.HaketiloState): lock: threading.RLock = dc.field(default_factory=threading.RLock) @contextmanager - def cursor(self, lock: bool = False, transaction: bool = False) \ + def cursor(self, transaction: bool = False) \ -> t.Iterator[sqlite3.Cursor]: """....""" start_transaction = transaction and not self.connection.in_transaction - assert lock or not start_transaction - try: - if lock: - self.lock.acquire() + self.lock.acquire() if self.current_cursor is not None: yield self.current_cursor return - self.current_cursor = self.connection.cursor() - - if start_transaction: - self.current_cursor.execute('BEGIN TRANSACTION;') - try: - yield self.current_cursor + self.current_cursor = self.connection.cursor() if start_transaction: - self.current_cursor.execute('COMMIT TRANSACTION;') - except: - if start_transaction: - self.current_cursor.execute('ROLLBACK TRANSACTION;') - raise + self.current_cursor.execute('BEGIN TRANSACTION;') + + try: + yield self.current_cursor + + if start_transaction: + assert self.connection.in_transaction + self.current_cursor.execute('COMMIT TRANSACTION;') + except: + if start_transaction: + self.current_cursor.execute('ROLLBACK TRANSACTION;') + raise + finally: + self.current_cursor = None finally: - self.current_cursor = None - - if lock: - self.lock.release() + self.lock.release() def rebuild_structures(self, cursor: sqlite3.Cursor) -> None: """....""" -- cgit v1.2.3