Emit UPDATE statements corresponding to value lists collected by _collect_post_update_commands().
(
base_mapper, uowtransaction, mapper, table, update
)
| 1285 | |
| 1286 | |
| 1287 | def _emit_post_update_statements( |
| 1288 | base_mapper, uowtransaction, mapper, table, update |
| 1289 | ): |
| 1290 | """Emit UPDATE statements corresponding to value lists collected |
| 1291 | by _collect_post_update_commands().""" |
| 1292 | |
| 1293 | execution_options = {"compiled_cache": base_mapper._compiled_cache} |
| 1294 | |
| 1295 | needs_version_id = ( |
| 1296 | mapper.version_id_col is not None |
| 1297 | and mapper.version_id_col in mapper._cols_by_table[table] |
| 1298 | ) |
| 1299 | |
| 1300 | def update_stmt(): |
| 1301 | clauses = BooleanClauseList._construct_raw(operators.and_) |
| 1302 | |
| 1303 | for col in mapper._pks_by_table[table]: |
| 1304 | clauses._append_inplace( |
| 1305 | col == sql.bindparam(col._label, type_=col.type) |
| 1306 | ) |
| 1307 | |
| 1308 | if needs_version_id: |
| 1309 | clauses._append_inplace( |
| 1310 | mapper.version_id_col |
| 1311 | == sql.bindparam( |
| 1312 | mapper.version_id_col._label, |
| 1313 | type_=mapper.version_id_col.type, |
| 1314 | ) |
| 1315 | ) |
| 1316 | |
| 1317 | stmt = table.update().where(clauses) |
| 1318 | |
| 1319 | return stmt |
| 1320 | |
| 1321 | statement = base_mapper._memo(("post_update", table), update_stmt) |
| 1322 | |
| 1323 | if mapper._version_id_has_server_side_value: |
| 1324 | statement = statement.return_defaults(mapper.version_id_col) |
| 1325 | |
| 1326 | # execute each UPDATE in the order according to the original |
| 1327 | # list of states to guarantee row access order, but |
| 1328 | # also group them into common (connection, cols) sets |
| 1329 | # to support executemany(). |
| 1330 | for key, records in groupby( |
| 1331 | update, |
| 1332 | lambda rec: (rec[3], set(rec[4])), # connection # parameter keys |
| 1333 | ): |
| 1334 | rows = 0 |
| 1335 | |
| 1336 | records = list(records) |
| 1337 | connection = key[0] |
| 1338 | |
| 1339 | assert_singlerow = connection.dialect.supports_sane_rowcount |
| 1340 | assert_multirow = ( |
| 1341 | assert_singlerow |
| 1342 | and connection.dialect.supports_sane_multi_rowcount |
| 1343 | ) |
| 1344 | allow_executemany = not needs_version_id or assert_multirow |
no test coverage detected