-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom class for SQLAlchemy + enable custom Select
- Loading branch information
1 parent
c955c8d
commit a0d0591
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |