Coverage for sqlmodel/sql/_expression_select_cls.py: 93%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-12 19:13 +0000

1from typing import ( 1abcdef

2 Tuple, 

3 TypeVar, 

4 Union, 

5) 

6 

7from sqlalchemy.sql._typing import ( 1abcdef

8 _ColumnExpressionArgument, 

9) 

10from sqlalchemy.sql.expression import Select as _Select 1abcdef

11from typing_extensions import Self 1abcdef

12 

13_T = TypeVar("_T") 1abcdef

14 

15 

16# Separate this class in SelectBase, Select, and SelectOfScalar so that they can share 

17# where and having without having type overlap incompatibility in session.exec(). 

18class SelectBase(_Select[Tuple[_T]]): 1abcdef

19 inherit_cache = True 1abcdef

20 

21 def where(self, *whereclause: Union[_ColumnExpressionArgument[bool], bool]) -> Self: 1abcdef

22 """Return a new `Select` construct with the given expression added to 

23 its `WHERE` clause, joined to the existing clause via `AND`, if any. 

24 """ 

25 return super().where(*whereclause) # type: ignore[arg-type] 1abcdef

26 

27 def having(self, *having: Union[_ColumnExpressionArgument[bool], bool]) -> Self: 1abcdef

28 """Return a new `Select` construct with the given expression added to 

29 its `HAVING` clause, joined to the existing clause via `AND`, if any. 

30 """ 

31 return super().having(*having) # type: ignore[arg-type] 

32 

33 

34class Select(SelectBase[_T]): 1abcdef

35 inherit_cache = True 1abcdef

36 

37 

38# This is not comparable to sqlalchemy.sql.selectable.ScalarSelect, that has a different 

39# purpose. This is the same as a normal SQLAlchemy Select class where there's only one 

40# entity, so the result will be converted to a scalar by default. This way writing 

41# for loops on the results will feel natural. 

42class SelectOfScalar(SelectBase[_T]): 1abcdef

43 inherit_cache = True 1abcdef