def __exit__( self, exc_type: Type[BaseException], exc_val: BaseException, exc_tb: TracebackType, ) -> None: try: if exc_val: # Roll back all changes # if an exception occurs. self.connection.rollback() raise exc_val elif not self.commit: self.connection.rollback() else: self.connection.commit() except sqlite3.InterfaceError as e: raise InterfaceError(e) from e except sqlite3.DataError as e: raise DataError(e) from e except sqlite3.OperationalError as e: raise OperationalError(e) from e except sqlite3.IntegrityError as e: raise IntegrityError(e) from e except sqlite3.InternalError as e: raise InternalError(e) from e except sqlite3.ProgrammingError as e: raise ProgrammingError(e) from e except sqlite3.NotSupportedError as e: raise NotSupportedError(e) from e except sqlite3.DatabaseError as e: raise DatabaseError(e) from e except sqlite3.Error as e: raise PersistenceError(e) from e
def __exit__( self, exc_type: Type[BaseException], exc_val: BaseException, exc_tb: TracebackType, ) -> None: try: if exc_val: self.c.rollback() raise exc_val elif not self.commit: self.c.rollback() else: self.c.commit() except psycopg2.InterfaceError as e: self.c.close(timeout=0) raise InterfaceError(e) except psycopg2.DataError as e: raise DataError(e) except psycopg2.OperationalError as e: raise OperationalError(e) except psycopg2.IntegrityError as e: raise IntegrityError(e) except psycopg2.InternalError as e: raise InternalError(e) except psycopg2.ProgrammingError as e: raise ProgrammingError(e) except psycopg2.NotSupportedError as e: raise NotSupportedError(e) except psycopg2.DatabaseError as e: raise DatabaseError(e) except psycopg2.Error as e: raise PersistenceError(e) finally: self.c.is_idle.set()
def create_table(self) -> None: with self.datastore.transaction() as c: try: for statement in self.create_table_statements: c.execute(statement) except psycopg2.Error as e: raise OperationalError(e)
def insert_events(self, stored_events: List[StoredEvent], **kwargs: Any) -> None: with self.datastore.transaction() as c: try: self._insert_events(c, stored_events, **kwargs) except psycopg2.IntegrityError as e: raise RecordConflictError(e) except psycopg2.Error as e: raise OperationalError(e)
def max_notification_id(self) -> int: """ Returns the maximum notification ID. """ try: with self.datastore.transaction() as c: c.execute(self.select_max_notification_id_statement) return c.fetchone()[0] or 0 except psycopg2.Error as e: raise OperationalError(e)
def max_notification_id(self) -> int: """ Returns the maximum notification ID. """ try: c = self.datastore.get_connection().cursor() c.execute(self.select_max_notification_id_statement) return c.fetchone()[0] or 0 except psycopg2.Error as e: raise OperationalError(e)
def max_tracking_id(self, application_name: str) -> int: params = [application_name] statement = ("SELECT MAX(notification_id) " f"FROM {self.tracking_table_name} " "WHERE application_name=%s") try: with self.datastore.transaction() as c: c.execute(statement, params) return c.fetchone()[0] or 0 except psycopg2.Error as e: raise OperationalError(e)
def max_tracking_id(self, application_name: str) -> int: params = [application_name] try: c = self.datastore.get_connection().cursor() statement = ("SELECT MAX(notification_id)" "FROM tracking " "WHERE application_name=?") c.execute(statement, params) return c.fetchone()[0] or 0 except sqlite3.OperationalError as e: raise OperationalError(e)
def _create_connection(self) -> PostgresConnection: # Make a connection to a database. try: pg_conn = psycopg2.connect( dbname=self.dbname, host=self.host, port=self.port, user=self.user, password=self.password, connect_timeout=self.connect_timeout, ) except psycopg2.OperationalError as e: raise OperationalError(e) from e pg_conn.cursor().execute( f"SET idle_in_transaction_session_timeout = " f"'{self.idle_in_transaction_session_timeout}s'") return PostgresConnection(pg_conn, max_age=self.max_age)
def select_events( self, originator_id: UUID, gt: Optional[int] = None, lte: Optional[int] = None, desc: bool = False, limit: Optional[int] = None, ) -> List[StoredEvent]: statement = self.select_events_statement params: List[Any] = [originator_id] if gt is not None: statement += "AND originator_version > %s " params.append(gt) if lte is not None: statement += "AND originator_version <= %s " params.append(lte) statement += "ORDER BY originator_version " if desc is False: statement += "ASC " else: statement += "DESC " if limit is not None: statement += "LIMIT %s " params.append(limit) # statement += ";" stored_events = [] try: with self.datastore.transaction() as c: c.execute(statement, params) for row in c.fetchall(): stored_events.append( StoredEvent( originator_id=row["originator_id"], originator_version=row["originator_version"], topic=row["topic"], state=bytes(row["state"]), ) ) except psycopg2.Error as e: raise OperationalError(e) return stored_events
def select_events( self, originator_id: UUID, gt: Optional[int] = None, lte: Optional[int] = None, desc: bool = False, limit: Optional[int] = None, ) -> List[StoredEvent]: statement = self.select_events_statement params: List[Any] = [originator_id.hex] if gt is not None: statement += "AND originator_version>? " params.append(gt) if lte is not None: statement += "AND originator_version<=? " params.append(lte) statement += "ORDER BY originator_version " if desc is False: statement += "ASC " else: statement += "DESC " if limit is not None: statement += "LIMIT ? " params.append(limit) try: c = self.datastore.get_connection().cursor() c.execute(statement, params) stored_events = [] for row in c.fetchall(): stored_events.append( StoredEvent( originator_id=UUID(row["originator_id"]), originator_version=row["originator_version"], topic=row["topic"], state=row["state"], )) except sqlite3.OperationalError as e: raise OperationalError(e) return stored_events
def select_notifications(self, start: int, limit: int) -> List[Notification]: """ Returns a list of event notifications from 'start', limited by 'limit'. """ params = [start, limit] try: with self.datastore.transaction() as c: c.execute(self.statement_notifications_statement, params) notifications = [] for row in c.fetchall(): notifications.append( Notification( id=row["notification_id"], originator_id=row["originator_id"], originator_version=row["originator_version"], topic=row["topic"], state=bytes(row["state"]), )) except psycopg2.Error as e: raise OperationalError(e) return notifications
def select_notifications(self, start: int, limit: int) -> List[Notification]: """ Returns a list of event notifications from 'start', limited by 'limit'. """ params = [start, limit] try: c = self.datastore.get_connection().cursor() c.execute(self.select_notifications_statement, params) notifications = [] for row in c.fetchall(): notifications.append( Notification( id=row["rowid"], originator_id=UUID(row["originator_id"]), originator_version=row["originator_version"], topic=row["topic"], state=row["state"], )) except sqlite3.OperationalError as e: raise OperationalError(e) return notifications