FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlmodel/sqlmodel/sql/expression.py at main · Python-Repository-Hub/sqlmodel · GitHub
Python-Repository-Hub
sqlmodel
Repository navigation
Code
Pull requests
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlmodel
/
sqlmodel
/
sql
/
expression.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
215 lines (162 loc) · 6.22 KB
Breadcrumbs
sqlmodel
/
sqlmodel
/
sql
/
expression.py
Copy path
File metadata and controls
215 lines (162 loc) · 6.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from
typing
import
(
Any
,
Iterable
,
Mapping
,
Optional
,
Sequence
,
Tuple
,
Type
,
TypeVar
,
Union
,
)
import
sqlalchemy
from
sqlalchemy
import
(
Column
,
ColumnElement
,
Extract
,
FunctionElement
,
FunctionFilter
,
Label
,
Over
,
TypeCoerce
,
WithinGroup
,
)
from
sqlalchemy
.
orm
import
InstrumentedAttribute
,
Mapped
from
sqlalchemy
.
sql
.
_typing
import
(
_ColumnExpressionArgument
,
_ColumnExpressionOrLiteralArgument
,
_ColumnExpressionOrStrLabelArgument
,
)
from
sqlalchemy
.
sql
.
elements
import
(
BinaryExpression
,
Case
,
Cast
,
CollectionAggregate
,
ColumnClause
,
TryCast
,
UnaryExpression
,
)
from
sqlalchemy
.
sql
.
type_api
import
TypeEngine
from
typing_extensions
import
Literal
from
.
_expression_select_cls
import
Select
as
Select
from
.
_expression_select_cls
import
SelectOfScalar
as
SelectOfScalar
from
.
_expression_select_gen
import
select
as
select
_T
=
TypeVar
(
"_T"
)
_TypeEngineArgument
=
Union
[
Type
[
TypeEngine
[
_T
]],
TypeEngine
[
_T
]]
# Redefine operatos that would only take a column expresion to also take the (virtual)
# types of Pydantic models, e.g. str instead of only Mapped[str].
def
all_
(
expr
:
Union
[
_ColumnExpressionArgument
[
_T
],
_T
])
->
CollectionAggregate
[
bool
]:
return
sqlalchemy
.
all_
(
expr
)
# type: ignore[arg-type]
def
and_
(
initial_clause
:
Union
[
Literal
[
True
],
_ColumnExpressionArgument
[
bool
],
bool
],
*
clauses
:
Union
[
_ColumnExpressionArgument
[
bool
],
bool
],
)
->
ColumnElement
[
bool
]:
return
sqlalchemy
.
and_
(
initial_clause
,
*
clauses
)
# type: ignore[arg-type]
def
any_
(
expr
:
Union
[
_ColumnExpressionArgument
[
_T
],
_T
])
->
CollectionAggregate
[
bool
]:
return
sqlalchemy
.
any_
(
expr
)
# type: ignore[arg-type]
def
asc
(
column
:
Union
[
_ColumnExpressionOrStrLabelArgument
[
_T
],
_T
],
)
->
UnaryExpression
[
_T
]:
return
sqlalchemy
.
asc
(
column
)
# type: ignore[arg-type]
def
collate
(
expression
:
Union
[
_ColumnExpressionArgument
[
str
],
str
],
collation
:
str
)
->
BinaryExpression
[
str
]:
return
sqlalchemy
.
collate
(
expression
,
collation
)
# type: ignore[arg-type]
def
between
(
expr
:
Union
[
_ColumnExpressionOrLiteralArgument
[
_T
],
_T
],
lower_bound
:
Any
,
upper_bound
:
Any
,
symmetric
:
bool
=
False
,
)
->
BinaryExpression
[
bool
]:
return
sqlalchemy
.
between
(
expr
,
lower_bound
,
upper_bound
,
symmetric
=
symmetric
)
def
not_
(
clause
:
Union
[
_ColumnExpressionArgument
[
_T
],
_T
])
->
ColumnElement
[
_T
]:
return
sqlalchemy
.
not_
(
clause
)
# type: ignore[arg-type]
def
case
(
*
whens
:
Union
[
Tuple
[
Union
[
_ColumnExpressionArgument
[
bool
],
bool
],
Any
],
Mapping
[
Any
,
Any
]
],
value
:
Optional
[
Any
]
=
None
,
else_
:
Optional
[
Any
]
=
None
,
)
->
Case
[
Any
]:
return
sqlalchemy
.
case
(
*
whens
,
value
=
value
,
else_
=
else_
)
# type: ignore[arg-type]
def
cast
(
expression
:
Union
[
_ColumnExpressionOrLiteralArgument
[
Any
],
Any
],
type_
:
"_TypeEngineArgument[_T]"
,
)
->
Cast
[
_T
]:
return
sqlalchemy
.
cast
(
expression
,
type_
)
def
try_cast
(
expression
:
Union
[
_ColumnExpressionOrLiteralArgument
[
Any
],
Any
],
type_
:
"_TypeEngineArgument[_T]"
,
)
->
TryCast
[
_T
]:
return
sqlalchemy
.
try_cast
(
expression
,
type_
)
def
desc
(
column
:
Union
[
_ColumnExpressionOrStrLabelArgument
[
_T
],
_T
],
)
->
UnaryExpression
[
_T
]:
return
sqlalchemy
.
desc
(
column
)
# type: ignore[arg-type]
def
distinct
(
expr
:
Union
[
_ColumnExpressionArgument
[
_T
],
_T
])
->
UnaryExpression
[
_T
]:
return
sqlalchemy
.
distinct
(
expr
)
# type: ignore[arg-type]
def
bitwise_not
(
expr
:
Union
[
_ColumnExpressionArgument
[
_T
],
_T
])
->
UnaryExpression
[
_T
]:
return
sqlalchemy
.
bitwise_not
(
expr
)
# type: ignore[arg-type]
def
extract
(
field
:
str
,
expr
:
Union
[
_ColumnExpressionArgument
[
Any
],
Any
])
->
Extract
:
return
sqlalchemy
.
extract
(
field
,
expr
)
def
funcfilter
(
func
:
FunctionElement
[
_T
],
*
criterion
:
Union
[
_ColumnExpressionArgument
[
bool
],
bool
]
)
->
FunctionFilter
[
_T
]:
return
sqlalchemy
.
funcfilter
(
func
,
*
criterion
)
# type: ignore[arg-type]
def
label
(
name
:
str
,
element
:
Union
[
_ColumnExpressionArgument
[
_T
],
_T
],
type_
:
Optional
[
"_TypeEngineArgument[_T]"
]
=
None
,
)
->
Label
[
_T
]:
return
sqlalchemy
.
label
(
name
,
element
,
type_
=
type_
)
# type: ignore[arg-type]
def
nulls_first
(
column
:
Union
[
_ColumnExpressionArgument
[
_T
],
_T
],
)
->
UnaryExpression
[
_T
]:
return
sqlalchemy
.
nulls_first
(
column
)
# type: ignore[arg-type]
def
nulls_last
(
column
:
Union
[
_ColumnExpressionArgument
[
_T
],
_T
])
->
UnaryExpression
[
_T
]:
return
sqlalchemy
.
nulls_last
(
column
)
# type: ignore[arg-type]
def
or_
(
initial_clause
:
Union
[
Literal
[
False
],
_ColumnExpressionArgument
[
bool
],
bool
],
*
clauses
:
Union
[
_ColumnExpressionArgument
[
bool
],
bool
],
)
->
ColumnElement
[
bool
]:
return
sqlalchemy
.
or_
(
initial_clause
,
*
clauses
)
# type: ignore[arg-type]
def
over
(
element
:
FunctionElement
[
_T
],
partition_by
:
Optional
[
Union
[
Iterable
[
Union
[
_ColumnExpressionArgument
[
Any
],
Any
]],
_ColumnExpressionArgument
[
Any
],
Any
,
]
]
=
None
,
order_by
:
Optional
[
Union
[
Iterable
[
Union
[
_ColumnExpressionArgument
[
Any
],
Any
]],
_ColumnExpressionArgument
[
Any
],
Any
,
]
]
=
None
,
range_
:
Optional
[
Tuple
[
Optional
[
int
],
Optional
[
int
]]]
=
None
,
rows
:
Optional
[
Tuple
[
Optional
[
int
],
Optional
[
int
]]]
=
None
,
)
->
Over
[
_T
]:
return
sqlalchemy
.
over
(
element
,
partition_by
=
partition_by
,
order_by
=
order_by
,
range_
=
range_
,
rows
=
rows
)
def
tuple_
(
*
clauses
:
Union
[
_ColumnExpressionArgument
[
Any
],
Any
],
types
:
Optional
[
Sequence
[
"_TypeEngineArgument[Any]"
]]
=
None
,
)
->
Tuple
[
Any
, ...]:
return
sqlalchemy
.
tuple_
(
*
clauses
,
types
=
types
)
# type: ignore[return-value]
def
type_coerce
(
expression
:
Union
[
_ColumnExpressionOrLiteralArgument
[
Any
],
Any
],
type_
:
"_TypeEngineArgument[_T]"
,
)
->
TypeCoerce
[
_T
]:
return
sqlalchemy
.
type_coerce
(
expression
,
type_
)
def
within_group
(
element
:
FunctionElement
[
_T
],
*
order_by
:
Union
[
_ColumnExpressionArgument
[
Any
],
Any
]
)
->
WithinGroup
[
_T
]:
return
sqlalchemy
.
within_group
(
element
,
*
order_by
)
def
col
(
column_expression
:
_T
)
->
Mapped
[
_T
]:
if
not
isinstance
(
column_expression
, (
ColumnClause
,
Column
,
InstrumentedAttribute
)):
raise
RuntimeError
(
f"Not a SQLAlchemy column:
{
column_expression
}
"
)
return
column_expression
# type: ignore
Back
|
FazBrowse Home
|
New Git URL