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