From b18f150b6b6823776ae5ca6ce6bbc098c8467ba2 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Thu, 17 Oct 2024 09:20:28 +0200 Subject: [PATCH] move compile_sql_or_scalar meth to decorators.py --- app/models/stuff.py | 18 +----------------- app/utils/decorators.py | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 app/utils/decorators.py diff --git a/app/models/stuff.py b/app/models/stuff.py index 8c5fe7c..d9e63a7 100644 --- a/app/models/stuff.py +++ b/app/models/stuff.py @@ -1,6 +1,5 @@ import uuid -from sqlalchemy.dialects import postgresql from sqlalchemy import String, select, ForeignKey from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.ext.asyncio import AsyncSession @@ -8,22 +7,7 @@ from sqlalchemy.orm import mapped_column, Mapped, relationship, joinedload from app.models.base import Base from app.models.nonsense import Nonsense - -from functools import wraps - - -def compile_sql_or_scalar(func): - @wraps(func) - async def wrapper(cls, db_session, name, compile_sql=False, *args, **kwargs): - stmt = await func(cls, db_session, name, *args, **kwargs) - if compile_sql: - return stmt.compile( - dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True} - ) - result = await db_session.execute(stmt) - return result.scalars().first() - - return wrapper +from app.utils.decorators import compile_sql_or_scalar class Stuff(Base): diff --git a/app/utils/decorators.py b/app/utils/decorators.py new file mode 100644 index 0000000..f3a6f59 --- /dev/null +++ b/app/utils/decorators.py @@ -0,0 +1,42 @@ +from sqlalchemy.dialects import postgresql +from sqlalchemy.ext.asyncio import AsyncSession + +from functools import wraps + + +def compile_sql_or_scalar(func): + """ + A decorator that compiles an SQL statement or executes it and returns the first scalar result. + + Args: + func (Callable): The function to be decorated. It should return an SQLAlchemy statement. + + Returns: + Callable: A wrapper function that either compiles the SQL statement or executes it and returns the first scalar result. + """ + + @wraps(func) + async def wrapper(cls, db_session, name, compile_sql=False, *args, **kwargs): + """ + Wrapper function that either compiles the SQL statement or executes it. + + Args: + cls (Type): The class on which the method is called. + db_session (AsyncSession): The SQLAlchemy async session. + name (str): The name to be used in the SQL statement. + compile_sql (bool, optional): If True, compiles the SQL statement. Defaults to False. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + + Returns: + Compiled SQL statement or the first scalar result of the executed statement. + """ + stmt = await func(cls, db_session, name, *args, **kwargs) + if compile_sql: + return stmt.compile( + dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True} + ) + result = await db_session.execute(stmt) + return result.scalars().first() + + return wrapper