MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / save_obj

Function save_obj

lib/sqlalchemy/orm/persistence.py:41–121  ·  view source on GitHub ↗

Issue ``INSERT`` and/or ``UPDATE`` statements for a list of objects. This is called within the context of a UOWTransaction during a flush operation, given a list of states to be flushed. The base mapper in an inheritance hierarchy handles the inserts/ updates for all descendant

(base_mapper, states, uowtransaction, single=False)

Source from the content-addressed store, hash-verified

39
40
41def save_obj(base_mapper, states, uowtransaction, single=False):
42 """Issue ``INSERT`` and/or ``UPDATE`` statements for a list
43 of objects.
44
45 This is called within the context of a UOWTransaction during a
46 flush operation, given a list of states to be flushed. The
47 base mapper in an inheritance hierarchy handles the inserts/
48 updates for all descendant mappers.
49
50 """
51
52 # if batch=false, call _save_obj separately for each object
53 if not single and not base_mapper.batch:
54 for state in _sort_states(base_mapper, states):
55 save_obj(base_mapper, [state], uowtransaction, single=True)
56 return
57
58 states_to_update = []
59 states_to_insert = []
60
61 for (
62 state,
63 dict_,
64 mapper,
65 connection,
66 has_identity,
67 row_switch,
68 update_version_id,
69 ) in _organize_states_for_save(base_mapper, states, uowtransaction):
70 if has_identity or row_switch:
71 states_to_update.append(
72 (state, dict_, mapper, connection, update_version_id)
73 )
74 else:
75 states_to_insert.append((state, dict_, mapper, connection))
76
77 for table, mapper in base_mapper._sorted_tables.items():
78 if table not in mapper._pks_by_table:
79 continue
80 insert = _collect_insert_commands(table, states_to_insert)
81
82 update = _collect_update_commands(
83 uowtransaction, table, states_to_update
84 )
85
86 _emit_update_statements(
87 base_mapper,
88 uowtransaction,
89 mapper,
90 table,
91 update,
92 )
93
94 _emit_insert_statements(
95 base_mapper,
96 uowtransaction,
97 mapper,
98 table,

Callers

nothing calls this directly

Calls 9

_sort_statesFunction · 0.85
_collect_insert_commandsFunction · 0.85
_collect_update_commandsFunction · 0.85
_emit_update_statementsFunction · 0.85
_emit_insert_statementsFunction · 0.85
appendMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected