def _join_criterion(self, left, right, join_type): if join_type.upper() == "AND": return left & right elif join_type.upper() == "OR": return left | right raise OperationalError( 'join_type only support ("AND", "OR")') # pragma: nocoverage
def __init__(self, *args, join_type=AND, **kwargs) -> None: if args and kwargs: newarg = QueryExpression(join_type=join_type, **kwargs) args = (newarg, ) + args kwargs = {} if not all(isinstance(node, QueryExpression) for node in args): raise OperationalError( "All ordered arguments must be QueryExpression nodes") self.children: Tuple[QueryExpression, ...] = args self.filters: Dict[str, Any] = kwargs if join_type not in {self.AND, self.OR}: raise OperationalError("join_type must be AND or OR") self.join_type = join_type self._is_negated = False self._annotations: Dict[str, Any] = {} self._custom_filters: Dict[str, Dict[str, Any]] = {}
async def db_delete(self) -> None: await self.close() conn = await asyncpg.connect(self._db_url) try: await conn.execute(f'DROP DATABASE IF EXISTS "{self.database}"') except Exception as e: # pragma: nocoverage raise OperationalError( f"drop database {self.database}, error: {str(e)}") await conn.close()
async def db_create(self) -> None: conn = await asyncpg.connect(self._db_url) try: await conn.execute( f'CREATE DATABASE "{self.database}" OWNER "{self.user}"') except Exception as e: # pragma: nocoverage raise OperationalError( f"create database {self.database}, error: {str(e)}") await conn.close()
async def translate_exceptions_(self, *args): try: return await func(self, *args) except asyncpg.SyntaxOrAccessError as exc: # pragma: nocoverage raise OperationalError(exc) except asyncpg.IntegrityConstraintViolationError as exc: raise IntegrityError(exc) except asyncpg.InvalidTransactionStateError as exc: # pragma: nocoverage raise TransactionManagementError(exc)
async def delete(self) -> int: """ Deletes the current model object. :raises OperationalError: If object has never been persisted. """ if not self._saved_in_db: raise OperationalError("Can't delete unpersisted record") mapper = self.get_mapper() return await mapper.delete(self)
def __or__(self, other) -> "QueryExpression": if not isinstance(other, QueryExpression): raise OperationalError( "OR operation requires a QueryExpression node") return QueryExpression(self, other, join_type=self.OR)