Skip to content

Commit

Permalink
Add custom class for SQLAlchemy + enable custom Select
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize committed Nov 15, 2023
1 parent c955c8d commit a0d0591
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/utils_flask_sqla/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from .sqlalchemy import CustomSelect
from flask_sqlalchemy.model import Model


class SelectModelMixin(Model):
__abstract__ = True

@classmethod
@property
def select(cls):
if hasattr(cls, "__select_class__"):
select_cls = cls.__select_class__
else:
select_cls = CustomSelect
return select_cls._create_future_select(cls) # SQLA 2.0: _create_future_select → _create
17 changes: 17 additions & 0 deletions src/utils_flask_sqla/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.util.langhelpers import public_factory
from sqlalchemy.sql.expression import Select


class CustomSelect(Select):
def where_if(self, condition_to_execute_where, whereclause):
if condition_to_execute_where:
return self.where(whereclause)
else:
return self


class CustomSQLAlchemy(SQLAlchemy):
@staticmethod
def select(*entities):
return CustomSelect._create_future_select(*entities)

0 comments on commit a0d0591

Please sign in to comment.