this is interesting, and it amounts to that you built your own ORM on top of SA's SQL construction language, with some help from the SQL generated by Mappers.

while you have addressed the issue of aggregates, which is important for large tables, i dont see that youve addressed the issue of dependencies/cascading here. I.e. what if some other tables depended on the User table, how will you handle those records before you delete all IDs less than 47 ?

if i were to integrate this with SA as an extension, i'd want it to be constructed first as a separate object that looks more like:

m = mapper(MyClass, mytable)
manager = SimplePersist(m)

manager.update(obj1, obj2, obj3...)
manager.insert(obj1, obj2, obj3...)
manger.delete(....)

since SA's basic design idea is based around decoupling classes from the way they are persisted.

then to provide the interface you describe below, you could either provide an enhanced version of "assign_mapper" to monkeypatch the additional methods, and/or provide a MixIn class that provides the methods using inheritance, insuring that it works nicely with the ActiveMapper since this falls right in with that whole thing.

also note that objects currently allow access to the "columns" that are mapped to them via the interface: User.c.user_id.

On Mar 15, 2006, at 9:56 AM, Aaron Bickell wrote:


I recently ported a pretty large project over sqlalchemy, and it was pretty smooth, but I kept running into the same few minor snags. Mainly, I found that I kept on wanting to use the lower level table api within the same transaction as mapped objects that I'm modifying. Because modifications to the objects aren't persisted until objectstore.commit() is called, I had to do a lot of juggling to make this work. What I came up with to solve this was a very thin, simple object layer on top of the table api that gives some nice object abstraction, but is able to act directly on the database rather than defer all operations to the objectstore. I whipped up a module that lets you do the following, using a metaclass:

class User(SimpleObject):
        class table:
                _table = user_table  # same old user table from all the examples

u = User()
u.user_name = 'jimbob'
u.password = 'biscuits'
u.insert()

print u.user_id # <- the insert() populated the id (good for logging)

users = User.select(User.user_name.like('jim%'))
# the User class has attributes are the columns from the table
print users # a list of user objects

u.user_name = 'steve-o'
u.update() # updates the object with the new values

u.delete() # delete the object i created

User.delete_where(User.user_id<47) # deletes a bunch of users, works the same for update

u = User.get(109) # gets the user by the primary key


This approach cuts out the objectstore which takes away the ability for sqlalchemy to manage relations for you, but because the objects have update/delete methods on the objects, so the problem is still manageable. The User object could pretty easily have a method that fetches the objects related to this object (preferences, addresses, etc). This approach was particularly useful for my application because I was modifying/deleting a lot of data in any given transaction, and it filled the gap between mapped objects and tables very nicely for me. Would others find this a useful abstraction of the table api? Is there already a better way of doing this that I missed? I'd gladly provide the code if anyone was interested (after i clean it up). SQLAlchemy has been great to use, and solves a lot of the problems I had continually fought for a while.

Aaron Bickell
Research & Development
Optio Software, Inc





-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to