def sa_retrieve_one(class_, **kw): query = session.query(class_).filter_by(**kw) count = query.count() if count == 0: return None elif count == 1: return query[0] else: raise LookupError, 'Received %d results.' % count 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 so_retrieve_one(class_, **kw): results = so_retrieve(class_, **kw) count = len(results) if count == 0: return None elif count == 1: return results[0] else: 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)