Identify sets of values to use in INSERT statements for a list of states.
(
table,
states_to_insert,
*,
bulk=False,
return_defaults=False,
render_nulls=False,
include_bulk_keys=(),
)
| 325 | |
| 326 | |
| 327 | def _collect_insert_commands( |
| 328 | table, |
| 329 | states_to_insert, |
| 330 | *, |
| 331 | bulk=False, |
| 332 | return_defaults=False, |
| 333 | render_nulls=False, |
| 334 | include_bulk_keys=(), |
| 335 | ): |
| 336 | """Identify sets of values to use in INSERT statements for a |
| 337 | list of states. |
| 338 | |
| 339 | """ |
| 340 | for state, state_dict, mapper, connection in states_to_insert: |
| 341 | if table not in mapper._pks_by_table: |
| 342 | continue |
| 343 | |
| 344 | params = {} |
| 345 | value_params = {} |
| 346 | |
| 347 | propkey_to_col = mapper._propkey_to_col[table] |
| 348 | |
| 349 | eval_none = mapper._insert_cols_evaluating_none[table] |
| 350 | |
| 351 | for propkey in set(propkey_to_col).intersection(state_dict): |
| 352 | value = state_dict[propkey] |
| 353 | col = propkey_to_col[propkey] |
| 354 | if value is None and col not in eval_none and not render_nulls: |
| 355 | continue |
| 356 | elif not bulk and ( |
| 357 | hasattr(value, "__clause_element__") |
| 358 | or isinstance(value, sql.ClauseElement) |
| 359 | ): |
| 360 | value_params[col] = ( |
| 361 | value.__clause_element__() |
| 362 | if hasattr(value, "__clause_element__") |
| 363 | else value |
| 364 | ) |
| 365 | else: |
| 366 | params[col.key] = value |
| 367 | |
| 368 | if not bulk: |
| 369 | # for all the columns that have no default and we don't have |
| 370 | # a value and where "None" is not a special value, add |
| 371 | # explicit None to the INSERT. This is a legacy behavior |
| 372 | # which might be worth removing, as it should not be necessary |
| 373 | # and also produces confusion, given that "missing" and None |
| 374 | # now have distinct meanings |
| 375 | for colkey in ( |
| 376 | mapper._insert_cols_as_none[table] |
| 377 | .difference(params) |
| 378 | .difference([c.key for c in value_params]) |
| 379 | ): |
| 380 | params[colkey] = None |
| 381 | |
| 382 | if not bulk or return_defaults: |
| 383 | # params are in terms of Column key objects, so |
| 384 | # compare to pk_keys_by_table |
no test coverage detected