writes
Insert templates of a model: the single-row insert, the statements of
bulk_create and upserts. Updates, deletes and selects by primary key are
built by write_statements.
Each runs one cached statement per model and set of written columns::
| Text Only | |
|---|---|
1 2 3 | |
build_conflict_insert(table, names, rules, on_conflict, target, update_names, on_update, returning, model=None, update_values=None, update_variables=False)
Build the insert of the database's dialect that skips or updates a row conflicting with an existing one. The value binds are built here, on a cache miss only, so the rules get them ready made::
| Text Only | |
|---|---|
1 2 3 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table
|
written table |
required |
names
|
Tuple[str, ...]
|
inserted column names, sorted |
required |
rules
|
DialectRules
|
rules of the database dialect |
required |
on_conflict
|
str
|
|
required |
target
|
Tuple[str, ...]
|
conflicting column names (the primary key for |
required |
update_names
|
Tuple[str, ...]
|
columns updated from the inserted row |
required |
on_update
|
Tuple[str, ...]
|
columns updated with their |
required |
returning
|
Tuple[str, ...]
|
columns of the written row to return, if any |
required |
model
|
Optional[Type[Any]]
|
written model, resolving the fields of |
None
|
update_values
|
Optional[Mapping[str, Any]]
|
value or expression node set by column name |
None
|
update_variables
|
bool
|
read the bound update values from MySQL user variables |
False
|
Returns:
| Type | Description |
|---|---|
Executable
|
insert statement |
Source code in ormar/queryset/templates/writes.py
| Python | |
|---|---|
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
build_insert(table, names)
Build an insert of the given columns with named value binds::
| Text Only | |
|---|---|
1 | |
The names are sorted by the caller, so the same columns given in any order share one statement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table
|
written table |
required |
names
|
Tuple[str, ...]
|
written column names, sorted |
required |
Returns:
| Type | Description |
|---|---|
Executable
|
insert statement |
Source code in ormar/queryset/templates/writes.py
| Python | |
|---|---|
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
build_update_assignment(model, on_update, update_values)
Build the MySQL SET of the user variables a multi-row upsert reads its
on_update values and the bound update_values from (see
update_assignment).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Type[Any]
|
written model |
required |
on_update
|
Tuple[str, ...]
|
columns updated with their |
required |
update_values
|
Mapping[str, Any]
|
value or expression node by column name |
required |
Returns:
| Type | Description |
|---|---|
Executable
|
assignment statement |
Source code in ormar/queryset/templates/writes.py
| Python | |
|---|---|
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 | |
insert_statement(model_cls, names, on_conflict=None, conflict_target=None, update_fields=None, on_update=(), returning=(), update_values=None, update_variables=False)
Return the insert of a model for the given sorted column names, from the
model's template cache. save() binds one row to it and
bulk_create runs it with executemany for every row with the same
columns.
With on_conflict the insert skips or updates conflicting rows (see
build_conflict_insert): the target defaults to the primary key and the
updated columns to the inserted ones but the target, the key and
on_update, or to none with update_values. One statement is cached
per shape of update_values (see update_values_key).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cls
|
Type[Any]
|
inserted model |
required |
names
|
Tuple[str, ...]
|
inserted column names, sorted |
required |
on_conflict
|
Optional[str]
|
|
None
|
conflict_target
|
Optional[Sequence[str]]
|
names of the fields of the conflicting unique key |
None
|
update_fields
|
Optional[Sequence[str]]
|
names of the fields updated from the inserted row |
None
|
on_update
|
Tuple[str, ...]
|
columns updated with their |
()
|
returning
|
Tuple[str, ...]
|
columns of the written row to return, if any |
()
|
update_values
|
Optional[Mapping[str, Any]]
|
value or expression node set by column name |
None
|
update_variables
|
bool
|
read the bound update values from MySQL user
variables set by |
False
|
Returns:
| Type | Description |
|---|---|
Executable
|
insert statement with |
Source code in ormar/queryset/templates/writes.py
| Python | |
|---|---|
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
insert_template(model_cls, values, **statement_kwargs)
Return the insert of a model for the given column values, used by
save() and one-row bulk_create::
| Text Only | |
|---|---|
1 2 3 | |
Rows that leave out the primary key or a column with a server default write a different set of columns, so each set gets its own statement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cls
|
Type[Any]
|
inserted model |
required |
values
|
Mapping[str, Any]
|
values by column name |
required |
statement_kwargs
|
Any
|
conflict handling (see |
{}
|
Returns:
| Type | Description |
|---|---|
StatementTemplate
|
statement and values to bind |
Source code in ormar/queryset/templates/writes.py
| Python | |
|---|---|
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 | |
update_value_clauses(inserted, model_cls, update_values)
Build the SQL of the update_values of an upsert, in sorted column
order: a value binds slot ormar_u{i} typed like its column, an
expression reads F fields from the written table (the existing row) and
Inserted fields from inserted, with its literals bound as
ormar_u{i}_{k}::
| Text Only | |
|---|---|
1 2 3 4 5 6 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inserted
|
Any
|
the inserted row ( |
required |
model_cls
|
Type[Any]
|
written model |
required |
update_values
|
Mapping[str, Any]
|
value or expression node by column name |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
SQL by column name |
Source code in ormar/queryset/templates/writes.py
| Python | |
|---|---|
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 | |
update_values_key(update_values)
Return the shape of the update_values of an upsert for cache keys: the
column of a value, the column and expression_key of an expression, so
the literals do not split the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
update_values
|
Mapping[str, Any]
|
value or expression node by column name |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Any, ...]
|
hashable shape |
Source code in ormar/queryset/templates/writes.py
| Python | |
|---|---|
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
update_values_params(update_values)
Return the values bound by the update_values of an upsert, named like
update_value_clauses names them::
| Text Only | |
|---|---|
1 2 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
update_values
|
Mapping[str, Any]
|
value or expression node by column name |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
values by bind parameter name |
Source code in ormar/queryset/templates/writes.py
| Python | |
|---|---|
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |