sa_retreive_one = retrieve_one.when(class_test)(sa_retrieve_one) def sa_update(obj, **kw): [setattr(obj, key, value) for key, value in kw.iteritems()] push_to_db(obj) sa_update = update.when(obj_test)(sa_update) def sa_delete(obj): session = session_for_obj(obj) session.delete(obj) session.flush() sa_delete = delete.when(obj_test)(sa_delete) def sa_count(class_, **kw): return session.query(class_).filter_by(**kw).count() sa_count = count.when(class_test)(sa_count) def push_to_db(obj): session = session_for_obj(obj) session.save_or_update(obj) session.flush((obj,)) def session_for_obj(obj): s = sa.orm.object_session(obj) if not s: s = session return s
raise LookupError, 'Received %d results.' % count so_retrieve_one = retrieve_one.when( 'issubclass(class_, so.SQLObject)')(so_retrieve_one) def so_update(obj, **kw): obj.set(**kw) so_update = update.when('isinstance(obj, so.SQLObject)')(so_update) def so_delete(obj): obj.destroySelf() so_delete = delete.when('isinstance(obj, so.SQLObject)')(so_delete) def so_count(class_, **kw): select = select_from_kw(class_, **kw) return select.count() so_count = count.when('issubclass(class_, so.SQLObject)')(so_count) def select_from_kw(class_, **kw): "Returns a select object for a given set of keywords." and_list = [] for key, value in kw.iteritems(): query_col = getattr(class_.q, key) and_list.append(query_col==kw[key]) if len(and_list) == 0: select = class_.select() elif len(and_list) == 1: select = class_.select(and_list[0]) else: select = class_.select(so.AND(*and_list)) return select