Hello, I've implemented a 'read' event for attributes, similar to 'set'. It's called 'read' and not 'get' because it's fired only when data is fetched from the database.
The implementation looks a bit crude to me, as I'm not so familiar with sqlalchemy internals. (but this exercise did teach a lot) What do you think about it? I can write tests if you think this can be included in the mainline. Best regards, Burak -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
diff -r 3654d6a30454 lib/sqlalchemy/orm/events.py --- a/lib/sqlalchemy/orm/events.py Tue Jul 05 19:33:05 2011 -0400 +++ b/lib/sqlalchemy/orm/events.py Wed Jul 06 14:02:00 2011 +0300 @@ -1044,3 +1044,20 @@ """ + def read(self, target, value, old, initiator): + """Receive a scalar read event. + + :param target: the object instance receiving the event. + If the listener is registered with ``raw=True``, this will + be the :class:`.InstanceState` object. + :param value: the value being set. If this listener + is registered with ``retval=True``, the listener + function must return this value, or a new value which + replaces it. + :param oldvalue: the previous value being replaced. This + may also be the symbol ``NEVER_SET`` or ``NO_VALUE``. + :param initiator: the attribute implementation object + which initiated this event. + :return: if the event was registered with ``retval=True``, + the given value, or a new effective value, should be returned. + """ diff -r 3654d6a30454 lib/sqlalchemy/orm/strategies.py --- a/lib/sqlalchemy/orm/strategies.py Tue Jul 05 19:33:05 2011 -0400 +++ b/lib/sqlalchemy/orm/strategies.py Wed Jul 06 14:02:00 2011 +0300 @@ -147,7 +147,15 @@ col = adapter.columns[col] if col is not None and col in row: def new_execute(state, dict_, row): - dict_[key] = row[col] + value = row[col] + old = dict_.get(self.key, util.symbol('NO_VALUE')) + + attr = getattr(self.parent_property.parent.class_, key) + impl = attr.impl + for fn in impl.dispatch.read: + value = fn(state, value, old, impl) + + dict_[key] = value return new_execute, None, None else: def new_execute(state, dict_, row):