From 8986897292f43b3f76fd2cb643169e707978887f Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 1 Nov 2020 02:26:27 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20Remove=20Alchemist=20class,=20cr?= =?UTF-8?q?eate=20a=20few=20utility=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet/alchemist/__init__.py | 24 +----------------------- royalnet/alchemist/func.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 royalnet/alchemist/func.py diff --git a/royalnet/alchemist/__init__.py b/royalnet/alchemist/__init__.py index 1050b2d3..8ce214bb 100644 --- a/royalnet/alchemist/__init__.py +++ b/royalnet/alchemist/__init__.py @@ -1,23 +1 @@ -from royalnet.typing import * -import sqlalchemy as sa -import sqlalchemy.orm as saorm - - -__all__ = ( - "Alchemist", -) - - -class Alchemist: - """The Alchemist module connects to a relational database with SQLAlchemy.""" - - def __init__(self, - engine_args: Iterable[Any], - engine_kwargs: Mapping[str, Any]): - self.engine: sa.engine.Engine = sa.create_engine(*engine_args, **engine_kwargs) - self.Session: Union[saorm.sessionmaker, Type[saorm.Session]] = saorm.sessionmaker(bind=self.engine) - - def add_metadata(self, metadata: sa.MetaData): - """Bind a MetaData object to the engine, and create all tables linked with it.""" - metadata.bind = self.engine - metadata.create_all() +from .func import * diff --git a/royalnet/alchemist/func.py b/royalnet/alchemist/func.py new file mode 100644 index 00000000..9038b63d --- /dev/null +++ b/royalnet/alchemist/func.py @@ -0,0 +1,30 @@ +from ..typing import * +import sqlalchemy + + +def ieq(one, two): + """ + Create a case-insensitive equality filter for SQLAlchemy. :: + + lower(one) == lower(two) + + + """ + return sqlalchemy.func.lower(one) == sqlalchemy.func.lower(two) + + +def ineq(one, two): + """ + Create a case-insensitive inequality filter for SQLAlchemy. :: + + lower(one) != lower(two) + + + """ + return sqlalchemy.func.lower(one) != sqlalchemy.func.lower(two) + + +__all__ = ( + "ieq", + "ineq", +)