Ian Bicking wrote in Towards PHP that a successful Python PHP-killer (as Spyce aspires to be) will need to include a simple data-access tool that Just Works.
I had two thoughts:
- He's absolutely right
- I could do this with SqlAlchemy in an afternoon
SqlSoup is NOT an ORM per se, although naturally it plays nicely with SqlAlchemy. SqlSoup inspects your database and reflects its contents with no other work required; in particular, no model definitions are necessary.
Here's what SqlSoup looks like, given pyweboff-ish tables of users, books, and loans (SQL to set up this example is included in the test code, but I won't repeat it here):
>>> from sqlalchemy.ext.sqlsoup import SqlSoup >>> db = SqlSoup('sqlite:///:memory:') >>> users = db.users.select() >>> users.sort() >>> users [Class_Users(name='Bhargan Basepair',email@example.com',password='basepair',classname=None,admin=1), Class_Users(name='Joe Student',firstname.lastname@example.org',password='student',classname=None,admin=0)]Of course, letting the database do the sort is better (".c" is short for ".columns"):
>>> db.users.select(order_by=[db.users.c.name])Field access is intuitive:
>>> users.email email@example.com'Of course, you don't want to load all users very often. Let's add a WHERE clause. Let's also switch the order_by to DESC while we're at it.
>>> from sqlalchemy import or_, and_, desc >>> where = or_(db.users.c.name=='Bhargan Basepair', firstname.lastname@example.org') >>> db.users.select(where, order_by=[desc(db.users.c.name)]) [MappedUsers(name='Joe Student',email@example.com',password='student',classname=None,admin=0), MappedUsers(name='Bhargan Basepair',firstname.lastname@example.org',password='basepair',classname=None,admin=1)]You can also use the select...by methods if you're querying on a single column. This allows using keyword arguments as column names:
>>> db.users.selectone_by(name='Bhargan Basepair') MappedUsers(name='Bhargan Basepair',email@example.com',password='basepair',classname=None,admin=1)All the SqlAlchemy mapper select variants (select, select_by, selectone, selectone_by, selectfirst, selectfirst_by) are available. See the SqlAlchemy documentation (sql construction and data mapping) for details.
Modifying objects is also intuitive:
>>> user = _ >>> user.email = 'firstname.lastname@example.org' >>> db.flush()(SqlSoup leverages the sophisticated SqlAlchemy unit-of-work code, so multiple updates to a single object will be turned into a single UPDATE statement when you flush.)
To finish covering the basics, let's insert a new loan, then delete it:
>>> book_id = db.books.selectfirst(db.books.c.title=='Regional Variation in Moss').id >>> db.loans.insert(book_id=book_id, user_name=user.name) MappedLoans(book_id=2,user_name='Bhargan Basepair',loan_date=None) >>> db.flush()
>>> loan = db.loans.selectone_by(book_id=2, user_name='Bhargan Basepair') >>> db.delete(loan) >>> db.flush()