diff options
author | tsauerwein <tobias.sauerwein@camptocamp.com> | 2016-02-20 17:20:50 +0100 |
---|---|---|
committer | tsauerwein <tobias.sauerwein@camptocamp.com> | 2016-02-20 17:20:50 +0100 |
commit | 1b60340c63c84aa478988a285450fa4d5e2f7bd0 (patch) | |
tree | b8eafca87cf55c2fd307759d7c0e7ea1093762d7 | |
parent | 60a9d2da25da68466130771afc3f35c9473aca02 (diff) | |
download | sqlalchemy-pr/243.tar.gz |
Add argument 'with_none' to Session.bulk_insert_mappingspr/243
-rw-r--r-- | lib/sqlalchemy/orm/persistence.py | 9 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/session.py | 18 | ||||
-rw-r--r-- | test/orm/test_bulk.py | 27 |
3 files changed, 42 insertions, 12 deletions
diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py index b9a1f9df9..01a75a6e6 100644 --- a/lib/sqlalchemy/orm/persistence.py +++ b/lib/sqlalchemy/orm/persistence.py @@ -25,7 +25,8 @@ from . import loading def _bulk_insert( - mapper, mappings, session_transaction, isstates, return_defaults): + mapper, mappings, session_transaction, isstates, return_defaults, + with_none): base_mapper = mapper.base_mapper cached_connections = _cached_connection_dict(base_mapper) @@ -58,7 +59,7 @@ def _bulk_insert( has_all_defaults in _collect_insert_commands(table, ( (None, mapping, mapper, connection) for mapping in mappings), - bulk=True, return_defaults=return_defaults + bulk=True, return_defaults=return_defaults, with_none=with_none ) ) _emit_insert_statements(base_mapper, None, @@ -363,7 +364,7 @@ def _organize_states_for_delete(base_mapper, states, uowtransaction): def _collect_insert_commands( table, states_to_insert, - bulk=False, return_defaults=False): + bulk=False, return_defaults=False, with_none=False): """Identify sets of values to use in INSERT statements for a list of states. @@ -382,7 +383,7 @@ def _collect_insert_commands( for propkey in set(propkey_to_col).intersection(state_dict): value = state_dict[propkey] col = propkey_to_col[propkey] - if value is None and propkey not in eval_none: + if value is None and propkey not in eval_none and not with_none: continue elif not bulk and isinstance(value, sql.ClauseElement): value_params[col.key] = value diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 48ff09b87..96896b0d8 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -2235,9 +2235,10 @@ class Session(_SessionClassMethods): ): self._bulk_save_mappings( mapper, states, isupdate, True, - return_defaults, update_changed_only) + return_defaults, update_changed_only, False) - def bulk_insert_mappings(self, mapper, mappings, return_defaults=False): + def bulk_insert_mappings( + self, mapper, mappings, return_defaults=False, with_none=False): """Perform a bulk insert of the given list of mapping dictionaries. The bulk insert feature allows plain Python dictionaries to be used as @@ -2290,6 +2291,9 @@ class Session(_SessionClassMethods): reason this flag should be set as the returned default information is not used. + :param with_none: when True, `None` values are not omitted which can + help in reducing the number of insert statements. + .. seealso:: @@ -2301,7 +2305,7 @@ class Session(_SessionClassMethods): """ self._bulk_save_mappings( - mapper, mappings, False, False, return_defaults, False) + mapper, mappings, False, False, return_defaults, False, with_none) def bulk_update_mappings(self, mapper, mappings): """Perform a bulk update of the given list of mapping dictionaries. @@ -2350,11 +2354,12 @@ class Session(_SessionClassMethods): :meth:`.Session.bulk_save_objects` """ - self._bulk_save_mappings(mapper, mappings, True, False, False, False) + self._bulk_save_mappings( + mapper, mappings, True, False, False, False, False) def _bulk_save_mappings( self, mapper, mappings, isupdate, isstates, - return_defaults, update_changed_only): + return_defaults, update_changed_only, with_none): mapper = _class_to_mapper(mapper) self._flushing = True @@ -2367,7 +2372,8 @@ class Session(_SessionClassMethods): isstates, update_changed_only) else: persistence._bulk_insert( - mapper, mappings, transaction, isstates, return_defaults) + mapper, mappings, transaction, + isstates, return_defaults, with_none) transaction.commit() except: diff --git a/test/orm/test_bulk.py b/test/orm/test_bulk.py index 878560cf6..4e1f9a592 100644 --- a/test/orm/test_bulk.py +++ b/test/orm/test_bulk.py @@ -17,11 +17,12 @@ class BulkInsertUpdateTest(BulkTest, _fixtures.FixtureTest): @classmethod def setup_mappers(cls): - User, Address = cls.classes("User", "Address") - u, a = cls.tables("users", "addresses") + User, Address, Order = cls.classes("User", "Address", "Order") + u, a, o = cls.tables("users", "addresses", "orders") mapper(User, u) mapper(Address, a) + mapper(Order, o) def test_bulk_save_return_defaults(self): User, = self.classes("User",) @@ -155,6 +156,28 @@ class BulkInsertUpdateTest(BulkTest, _fixtures.FixtureTest): ) ) + def test_bulk_insert_with_none(self): + Order, = self.classes("Order",) + + s = Session() + with self.sql_execution_asserter() as asserter: + s.bulk_insert_mappings( + Order, + [{'id': 1, 'description': 'u1new'}, + {'id': 2, 'description': None}, + {'id': 3, 'description': 'u3new'}], + with_none=True + ) + + asserter.assert_( + CompiledSQL( + "INSERT INTO orders (id, description) VALUES (:id, :description)", + [{'id': 1, 'description': 'u1new'}, + {'id': 2, 'description': None}, + {'id': 3, 'description': 'u3new'}] + ) + ) + class BulkUDPostfetchTest(BulkTest, fixtures.MappedTest): @classmethod |