SQLAlchemy integration

Not sure if it's worth converting this function to use query API. Thoughts?