expressions
Column expressions: arithmetic and functions over columns evaluated by the
database, used as filter values and as values of QuerySet.update()::
| Text Only | |
|---|---|
1 2 3 4 | |
and in update_values of upserts, where Inserted("path") is the value
the insert tried to write::
| Text Only | |
|---|---|
1 2 3 4 5 6 7 8 | |
Model.field accessors are expressions themselves, F("path") names a
field by its path. Expression classes define only dunders and underscored
names, so they never shadow a field reached through a FieldAccessor.
Coalesce
Bases: Function
The first argument that is not NULL.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
185 186 187 188 | |
Combined
Bases: Expression
Two operands combined with an arithmetic operator.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
__init__(left, op, right)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
Expression
|
left operand |
required |
op
|
Callable[[Any, Any], Any]
|
arithmetic operator |
required |
right
|
Any
|
right operand, an expression or a literal |
required |
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
Expression
Base of column expressions, combined with +, - and *. The
literal operand has to be on the right side (F("a") + 1).
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
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 | |
__add__(other)
SQL expression + other
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
73 74 75 | |
__mul__(other)
SQL expression * other
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
81 82 83 | |
__sub__(other)
SQL expression - other
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
77 78 79 | |
F
Bases: Expression
Field by its path from the queried model: F("capacity"), or in filters
also a field of a related model (filter(taken__lt=F("owner__limit"))).
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
86 87 88 89 90 91 92 93 94 95 96 97 | |
__init__(path)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
field name, prefixed by its relation path if any |
required |
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
92 93 94 95 96 97 | |
Function
Bases: Expression
SQL function of two or more arguments, at least one of them an expression; the others may be literals.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
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 | |
__init__(*arguments)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments
|
Any
|
expressions and literals |
()
|
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
if there are less than two arguments, none of them is an expression or a literal is None or a bool |
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
Greatest
Bases: Function
The largest of the arguments. PostgreSQL ignores NULL arguments,
MySQL and SQLite return NULL when any argument is NULL.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
167 168 169 170 171 172 173 | |
GreatestOf
Bases: ReturnTypeFromArgs
SQL greatest(), the scalar max() on SQLite.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
198 199 200 201 202 203 | |
Inserted
Bases: Expression
Value a conflicting insert tried to write into a field, only in
update_values of create() / bulk_create() with
on_conflict="update": excluded.<column> on PostgreSQL and SQLite,
VALUES(<column>) on MySQL. F() there is the existing row's column.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
__init__(path)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
name of a non-relation field of the inserted model |
required |
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
108 109 110 111 112 113 | |
Least
Bases: Function
The smallest of the arguments. PostgreSQL ignores NULL arguments,
MySQL and SQLite return NULL when any argument is NULL.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
176 177 178 179 180 181 182 | |
LeastOf
Bases: ReturnTypeFromArgs
SQL least(), the scalar min() on SQLite.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
206 207 208 209 210 211 | |
Random
Bases: Expression
Random float from 0 (inclusive) to 1 (exclusive), drawn by the database
for each row: update(each=True, score=Random()), Random() * 100.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
191 192 193 194 195 | |
RandomFloat
Bases: GenericFunction
SQL random() returning a float in [0, 1): rand() on MySQL, the
low 53 bits of SQLite's 64-bit integer random() scaled into the range.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
236 237 238 239 240 241 242 243 244 245 | |
compile_greatest_on_sqlite(element, compiler, **kwargs)
SQLite has no greatest(), its max() of several arguments is the same
function.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
214 215 216 217 218 219 220 221 222 | |
compile_least_on_sqlite(element, compiler, **kwargs)
SQLite has no least(), its min() of several arguments is the same
function.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
225 226 227 228 229 230 231 232 233 | |
compile_random_on_mysql(element, compiler, **kwargs)
MySQL names the function rand().
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
248 249 250 251 252 253 | |
compile_random_on_sqlite(element, compiler, **kwargs)
SQLite's random() is a 64-bit integer; its remainder by 253 is exact
as a double, so the absolute value divided by 253 stays below 1.
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
256 257 258 259 260 261 262 263 264 | |
expression_clause(node, columns, slot, index=None, inserted=None)
Build the SQL of an expression, reading each field from the aliased column
of its action in columns. With a slot the literals become bind
parameters named {slot}_{k} and typed from the left operand and the
literal (release_values drops the literal afterwards), without one
they stay plain values.
Inserted fields are read from inserted, the row a conflicting
insert tried to write (only upserts resolve them, see
expression_columns).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Any
|
expression node |
required |
columns
|
Mapping[str, QueryAction]
|
resolved field actions by field path
(see |
required |
slot
|
Optional[str]
|
slot name of a statement template, if built for one |
required |
index
|
Optional[Iterator[int]]
|
counter of the literals already named |
None
|
inserted
|
Any
|
columns of the inserted row ( |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
SQL expression |
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
expression_key(node)
Return the shape of an expression for template cache keys: operators, fields and the types of the literals, which decide how they are bound::
| Text Only | |
|---|---|
1 2 3 4 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Any
|
expression node |
required |
Returns:
| Type | Description |
|---|---|
Any
|
hashable shape |
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
expression_leaves(node)
Yield the fields (F, Inserted) and literals of an expression, left
to right; Random() is neither.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Any
|
expression node |
required |
Returns:
| Type | Description |
|---|---|
Iterator[Any]
|
leaves in order |
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
expression_params(value, slot)
Return the values bound for a slot: the value itself, or for an expression
its literals named {slot}_{k} (as expression_clause names them).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
bound value or expression |
required |
slot
|
str
|
name of the slot |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
values by bind parameter name |
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
function_clause(node, columns, slot, index, inserted=None)
Build the SQL of a function. Literal arguments are bound like the literals
of expression_clause: named {slot}_{k} in order and typed from the
first expression argument and the literal, so Greatest(F("count"), 2.5)
binds a float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Function
|
function node |
required |
columns
|
Mapping[str, QueryAction]
|
resolved field actions by field path |
required |
slot
|
Optional[str]
|
slot name of a statement template, if built for one |
required |
index
|
Iterator[int]
|
counter of the literals already named |
required |
inserted
|
Any
|
columns of the inserted row of an upsert |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
SQL function call |
Source code in ormar/queryset/expressions.py
| Python | |
|---|---|
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |