async def execute_many(self, queries: typing.List[ClauseElement]) -> None: assert self._connection is not None, "Connection is not acquired" cursor = await self._connection.cursor() try: for single_query in queries: single_query, args, context = self._compile(single_query) await cursor.execute(single_query, args) except psycopg2.InterfaceError as error: raise exceptions.InterfaceError(str(single_query), None, error) from error except psycopg2.DataError as error: raise exceptions.DataError(str(single_query), None, error) from error except psycopg2.OperationalError as error: raise exceptions.OperationalError(str(single_query), None, error) from error except psycopg2.IntegrityError as error: raise exceptions.IntegrityError(str(single_query), None, error) from error except psycopg2.InternalError as error: raise exceptions.InternalError(str(single_query), None, error) from error except psycopg2.ProgrammingError as error: raise exceptions.ProgrammingError(str(single_query), None, error) from error except psycopg2.NotSupportedError as error: raise exceptions.NotSupportedError(str(single_query), None, error) from error except psycopg2.DatabaseError as error: raise exceptions.DatabaseError(str(single_query), None, error) from error finally: cursor.close()
async def iterate( self, query: ClauseElement ) -> typing.AsyncGenerator[typing.Any, None]: assert self._connection is not None, "Connection is not acquired" query, args, context = self._compile(query) cursor = await self._connection.cursor() try: await cursor.execute(query, args) metadata = ResultMetaData(context, cursor.description) async for row in cursor: yield RowProxy(metadata, row, metadata._processors, metadata._keymap) except psycopg2.InterfaceError as error: raise exceptions.InterfaceError(str(query), None, error) from error except psycopg2.DataError as error: raise exceptions.DataError(str(query), None, error) from error except psycopg2.OperationalError as error: raise exceptions.OperationalError(str(query), None, error) from error except psycopg2.IntegrityError as error: raise exceptions.IntegrityError(str(query), None, error) from error except psycopg2.InternalError as error: raise exceptions.InternalError(str(query), None, error) from error except psycopg2.ProgrammingError as error: raise exceptions.ProgrammingError(str(query), None, error) from error except psycopg2.NotSupportedError as error: raise exceptions.NotSupportedError(str(query), None, error) from error except psycopg2.DatabaseError as error: raise exceptions.DatabaseError(str(query), None, error) from error finally: cursor.close()
async def fetch_one(self, query: ClauseElement) -> typing.Optional[typing.Mapping]: assert self._connection is not None, "Connection is not acquired" query, args, context = self._compile(query) cursor = await self._connection.cursor() try: await cursor.execute(query, args) row = await cursor.fetchone() if row is None: return None metadata = ResultMetaData(context, cursor.description) return RowProxy(metadata, row, metadata._processors, metadata._keymap) except psycopg2.InterfaceError as error: raise exceptions.InterfaceError(str(query), None, error) from error except psycopg2.DataError as error: raise exceptions.DataError(str(query), None, error) from error except psycopg2.OperationalError as error: raise exceptions.OperationalError(str(query), None, error) from error except psycopg2.IntegrityError as error: raise exceptions.IntegrityError(str(query), None, error) from error except psycopg2.InternalError as error: raise exceptions.InternalError(str(query), None, error) from error except psycopg2.ProgrammingError as error: raise exceptions.ProgrammingError(str(query), None, error) from error except psycopg2.NotSupportedError as error: raise exceptions.NotSupportedError(str(query), None, error) from error except psycopg2.DatabaseError as error: raise exceptions.DatabaseError(str(query), None, error) from error finally: cursor.close()
async def execute(self, query: ClauseElement) -> typing.Any: assert self._connection is not None, "Connection is not acquired" query, args, context = self._compile(query) cursor = await self._connection.cursor() try: await cursor.execute(query, args) return cursor.lastrowid except psycopg2.InterfaceError as error: raise exceptions.InterfaceError(str(query), None, error) from error except psycopg2.DataError as error: raise exceptions.DataError(str(query), None, error) from error except psycopg2.OperationalError as error: raise exceptions.OperationalError(str(query), None, error) from error except psycopg2.IntegrityError as error: raise exceptions.IntegrityError(str(query), None, error) from error except psycopg2.InternalError as error: raise exceptions.InternalError(str(query), None, error) from error except psycopg2.ProgrammingError as error: raise exceptions.ProgrammingError(str(query), None, error) from error except psycopg2.NotSupportedError as error: raise exceptions.NotSupportedError(str(query), None, error) from error except psycopg2.DatabaseError as error: raise exceptions.DatabaseError(str(query), None, error) from error finally: cursor.close()
async def iterate( self, query: ClauseElement) -> typing.AsyncGenerator[typing.Any, None]: assert self._connection is not None, "Connection is not acquired" query, args, result_columns = self._compile(query) column_maps = self._create_column_maps(result_columns) try: async for row in self._connection.cursor(query, *args): yield Record(row, result_columns, self._dialect, column_maps) except asyncpg.IntegrityConstraintViolationError as error: raise exceptions.IntegrityError(str(query), None, error) from error except (asyncpg.InvalidTransactionStateError, asyncpg.InvalidCursorStateError) as error: raise exceptions.InternalError(str(query), None, error) from error except asyncpg.InterfaceError as error: raise exceptions.InterfaceError(str(query), None, error) from error except asyncpg.DataError as error: raise exceptions.DataError(str(query), None, error) from error except asyncpg.SyntaxOrAccessError as error: raise exceptions.ProgrammingError(str(query), None, error) from error except (asyncpg.PostgresConnectionError, asyncpg.InsufficientResourcesError, asyncpg.ProgramLimitExceededError) as error: raise exceptions.OperationalError(str(query), None, error) from error except asyncpg.FeatureNotSupportedError as error: raise exceptions.NotSupportedError(str(query), None, error) from error except asyncpg.PostgresError as error: raise exceptions.DatabaseError(str(query), None, error) from error
async def execute(self, query: ClauseElement) -> typing.Any: assert self._connection is not None, "Connection is not acquired" query, args, result_columns = self._compile(query) try: return await self._connection.fetchval(query, *args) except asyncpg.IntegrityConstraintViolationError as error: raise exceptions.IntegrityError(str(query), None, error) from error except (asyncpg.InvalidTransactionStateError, asyncpg.InvalidCursorStateError) as error: raise exceptions.InternalError(str(query), None, error) from error except asyncpg.InterfaceError as error: raise exceptions.InterfaceError(str(query), None, error) from error except asyncpg.DataError as error: raise exceptions.DataError(str(query), None, error) from error except asyncpg.SyntaxOrAccessError as error: raise exceptions.ProgrammingError(str(query), None, error) from error except (asyncpg.PostgresConnectionError, asyncpg.InsufficientResourcesError, asyncpg.ProgramLimitExceededError) as error: raise exceptions.OperationalError(str(query), None, error) from error except asyncpg.FeatureNotSupportedError as error: raise exceptions.NotSupportedError(str(query), None, error) from error except asyncpg.PostgresError as error: raise exceptions.DatabaseError(str(query), None, error) from error
async def fetch_all(self, query: ClauseElement) -> typing.List[typing.Mapping]: assert self._connection is not None, "Connection is not acquired" query, args, result_columns = self._compile(query) try: rows = await self._connection.fetch(query, *args) except asyncpg.IntegrityConstraintViolationError as error: raise exceptions.IntegrityError(str(query), None, error) from error except (asyncpg.InvalidTransactionStateError, asyncpg.InvalidCursorStateError) as error: raise exceptions.InternalError(str(query), None, error) from error except asyncpg.InterfaceError as error: raise exceptions.InterfaceError(str(query), None, error) from error except asyncpg.DataError as error: raise exceptions.DataError(str(query), None, error) from error except asyncpg.SyntaxOrAccessError as error: raise exceptions.ProgrammingError(str(query), None, error) from error except (asyncpg.PostgresConnectionError, asyncpg.InsufficientResourcesError, asyncpg.ProgramLimitExceededError) as error: raise exceptions.OperationalError(str(query), None, error) from error except asyncpg.FeatureNotSupportedError as error: raise exceptions.NotSupportedError(str(query), None, error) from error except asyncpg.PostgresError as error: raise exceptions.DatabaseError(str(query), None, error) from error dialect = self._dialect column_maps = self._create_column_maps(result_columns) return [ Record(row, result_columns, dialect, column_maps) for row in rows ]
async def fetch_all(self, query: ClauseElement) -> typing.List[typing.Mapping]: assert self._connection is not None, "Connection is not acquired" query, args, context = self._compile(query) try: async with self._connection.execute(query, args) as cursor: rows = await cursor.fetchall() metadata = ResultMetaData(context, cursor.description) return [ RowProxy(metadata, row, metadata._processors, metadata._keymap) for row in rows ] except sqlite3.InterfaceError as error: raise exceptions.InterfaceError(str(query), None, error) from error except sqlite3.DataError as error: raise exceptions.DataError(str(query), None, error) from error except sqlite3.OperationalError as error: raise exceptions.OperationalError(str(query), None, error) from error except sqlite3.IntegrityError as error: raise exceptions.IntegrityError(str(query), None, error) from error except sqlite3.InternalError as error: raise exceptions.InternalError(str(query), None, error) from error except sqlite3.ProgrammingError as error: raise exceptions.ProgrammingError(str(query), None, error) from error except sqlite3.NotSupportedError as error: raise exceptions.NotSupportedError(str(query), None, error) from error except sqlite3.DatabaseError as error: raise exceptions.DatabaseError(str(query), None, error) from error
async def execute_many(self, queries: typing.List[ClauseElement]) -> None: assert self._connection is not None, "Connection is not acquired" # asyncpg uses prepared statements under the hood, so we just # loop through multiple executes here, which should all end up # using the same prepared statement. for single_query in queries: single_query, args, result_columns = self._compile(single_query) try: await self._connection.execute(single_query, *args) except asyncpg.IntegrityConstraintViolationError as error: raise exceptions.IntegrityError(str(single_query), None, error) from error except (asyncpg.InvalidTransactionStateError, asyncpg.InvalidCursorStateError) as error: raise exceptions.InternalError(str(single_query), None, error) from error except asyncpg.InterfaceError as error: raise exceptions.InterfaceError(str(single_query), None, error) from error except asyncpg.DataError as error: raise exceptions.DataError(str(single_query), None, error) from error except asyncpg.SyntaxOrAccessError as error: raise exceptions.ProgrammingError(str(single_query), None, error) from error except (asyncpg.PostgresConnectionError, asyncpg.InsufficientResourcesError, asyncpg.ProgramLimitExceededError) as error: raise exceptions.OperationalError(str(single_query), None, error) from error except asyncpg.FeatureNotSupportedError as error: raise exceptions.NotSupportedError(str(single_query), None, error) from error except asyncpg.PostgresError as error: raise exceptions.DatabaseError(str(single_query), None, error) from error