From: Mike Bayer Date: Wed, 14 Sep 2005 07:49:33 +0000 (+0000) Subject: (no commit message) X-Git-Tag: rel_0_1_0~740 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d99f2ea5d09921f6eb162f869287c16cc15fb2e1;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git --- diff --git a/lib/sqlalchemy/mapper.py b/lib/sqlalchemy/mapper.py index 09e6b09623..40462b1f9b 100644 --- a/lib/sqlalchemy/mapper.py +++ b/lib/sqlalchemy/mapper.py @@ -213,34 +213,38 @@ class Mapper(object): def _setattrbycolumn(self, obj, column, value): self.columntoproperty[column][0].setattr(obj, value) - def save_obj(self, obj): - # TODO: start breaking down the individual updates/inserts into the UOW or something, - # so they can be combined into a multiple execute - if hasattr(obj, "_instance_key"): - isinsert = False - else: - isinsert = True - + def save_obj(self, objects): + # ok, multiple save works sorta, make this more efficient by only + # creating inserts/updates when they are definitely needed + + # also try to get inserts to be en-masse with the "guess-the-id" thing maybe + work = {} for table in self.tables: + clause = sql.and_() + for col in table.primary_keys: + clause.clauses.append(col == sql.bindparam(col.key)) + + work[table] = {'insert': [], 'update': [], 'istmt': table.insert(), 'ustmt': table.update(clause)} + work[table]['istmt'].echo = self.echo + work[table]['ustmt'].echo = self.echo + + for obj in objects: params = {} + for col in table.columns: + params[col.key] = self._getattrbycolumn(obj, col) - if isinsert: - for col in table.columns: - params[col.key] = self._getattrbycolumn(obj, col) - statement = table.insert() + if hasattr(obj, "_instance_key"): + work[table]['update'].append(params) else: - clause = sql.and_() - for col in table.columns: - if col.primary_key: - clause.clauses.append(col == self._getattrbycolumn(obj, col)) - else: - params[col.key] = self._getattrbycolumn(obj, col) - statement = table.update(clause) - - statement.echo = self.echo - statement.execute(**params) - - if isinstance(statement, sql.Insert): + work[table]['insert'].append((obj, params)) + + for table, stuff in work.iteritems(): + if len(stuff['update']): + stuff['ustmt'].execute(*stuff['update']) + + for rec in stuff['insert']: + (obj, params) = rec + stuff['istmt'].execute(**params) primary_key = table.engine.last_inserted_ids()[0] found = False for col in table.primary_keys: