From: Mike Bayer Date: Sat, 11 Feb 2006 15:26:24 +0000 (+0000) Subject: added before_update/after_update X-Git-Tag: rel_0_1_0~35 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9323e70225d93971e598ffeeb6cf7c86fa9cbe2e;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added before_update/after_update --- diff --git a/lib/sqlalchemy/mapping/mapper.py b/lib/sqlalchemy/mapping/mapper.py index 9e110b67b5..e5e4ab063b 100644 --- a/lib/sqlalchemy/mapping/mapper.py +++ b/lib/sqlalchemy/mapping/mapper.py @@ -497,7 +497,8 @@ class Mapper(object): isinsert = not hasattr(obj, "_instance_key") if isinsert: self.extension.before_insert(self, obj) - + else: + self.extension.before_update(self, obj) hasdata = False for col in table.columns: if self.pks_by_table[table].contains(col): @@ -536,7 +537,7 @@ class Mapper(object): if hasdata: # if none of the attributes changed, dont even # add the row to be updated. - update.append(params) + update.append((obj, params)) else: insert.append((obj, params)) if len(update): @@ -546,7 +547,9 @@ class Mapper(object): statement = table.update(clause) rows = 0 for rec in update: - c = statement.execute(rec) + (obj, params) = rec + c = statement.execute(params) + self.extension.after_update(self, obj) rows += c.cursor.rowcount if table.engine.supports_sane_rowcount() and rows != len(update): raise "ConcurrencyError - updated rowcount %d does not match number of objects updated %d" % (rows, len(update)) @@ -834,6 +837,14 @@ class MapperExtension(object): this is a good place to set up primary key values and such that arent handled otherwise.""" if self.next is not None: self.next.before_insert(mapper, instance) + def before_update(self, mapper, instance): + """called before an object instnace is UPDATED""" + if self.next is not None: + self.next.before_update(mapper, instance) + def after_update(self, mapper, instance): + """called after an object instnace is UPDATED""" + if self.next is not None: + self.next.after_update(mapper, instance) def after_insert(self, mapper, instance): """called after an object instance has been INSERTed""" if self.next is not None: