Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ exclude_lines = [
strict = true

[[tool.mypy.overrides]]
module = "sqlmodel.sql.expression"
module = "sqlmodel.sql._expression_select_gen"
warn_unused_ignores = false

[[tool.mypy.overrides]]
Expand Down
8 changes: 5 additions & 3 deletions scripts/generate_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from jinja2 import Template
from pydantic import BaseModel

template_path = Path(__file__).parent.parent / "sqlmodel/sql/expression.py.jinja2"
destiny_path = Path(__file__).parent.parent / "sqlmodel/sql/expression.py"
template_path = (
Path(__file__).parent.parent / "sqlmodel/sql/_expression_select_gen.py.jinja2"
)
destiny_path = Path(__file__).parent.parent / "sqlmodel/sql/_expression_select_gen.py"


number_of_types = 4
Expand Down Expand Up @@ -48,7 +50,7 @@ class Arg(BaseModel):

result = (
"# WARNING: do not modify this code, it is generated by "
"expression.py.jinja2\n\n" + result
"_expression_select_gen.py.jinja2\n\n" + result
)

result = black.format_str(result, mode=black.Mode())
Expand Down
43 changes: 43 additions & 0 deletions sqlmodel/sql/_expression_select_cls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import (
Tuple,
TypeVar,
Union,
)

from sqlalchemy.sql._typing import (
_ColumnExpressionArgument,
)
from sqlalchemy.sql.expression import Select as _Select
from typing_extensions import Self

_T = TypeVar("_T")


# Separate this class in SelectBase, Select, and SelectOfScalar so that they can share
# where and having without having type overlap incompatibility in session.exec().
class SelectBase(_Select[Tuple[_T]]):
inherit_cache = True

def where(self, *whereclause: Union[_ColumnExpressionArgument[bool], bool]) -> Self:
"""Return a new `Select` construct with the given expression added to
its `WHERE` clause, joined to the existing clause via `AND`, if any.
"""
return super().where(*whereclause) # type: ignore[arg-type]

def having(self, *having: Union[_ColumnExpressionArgument[bool], bool]) -> Self:
"""Return a new `Select` construct with the given expression added to
its `HAVING` clause, joined to the existing clause via `AND`, if any.
"""
return super().having(*having) # type: ignore[arg-type]


class Select(SelectBase[_T]):
inherit_cache = True


# This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different
# purpose. This is the same as a normal SQLAlchemy Select class where there's only one
# entity, so the result will be converted to a scalar by default. This way writing
# for loops on the results will feel natural.
class SelectOfScalar(SelectBase[_T]):
inherit_cache = True
Loading