On 22 May 2006 at 17:43, Luís Bruno wrote:
> def factory(application, global_conf, database, tables):
> metadata = BoundMetaData(database)
> for name in tables.split():
> tableList[name] = Table(name, metadata, autoload=True)
>
> return application
>
> Any comments on this approach?
I stole code from paste.transaction to create a database filter, which sets
an environ variable to be used by lower level apps to get access to objectstore
if
they need to.
So, first the middleware (firebase specific due to the kinterbasdb.init() call):
----
from paste.httpexceptions import HTTPException
from paste.wsgilib import catch_errors
from paste.deploy.converters import asbool
"""Database access middleware, code
lifted from paste.Transaction
"""
from sqlalchemy import global_connect, objectstore, schema
import kinterbasdb
def DatabaseMiddlewareFactory(global_conf, db_uri=None, echo=False, **kw):
assert db_uri
return DatabaseMiddleware(global_conf,
database_uri=db_uri,
echo=echo)
class DatabaseMiddleware(object):
def __init__(self,
application,
global_conf,
db_uri=None,
echo=False,
**kw):
self.application = application
self.db_uri = db_uri
self.echo = asbool(echo)
# db specific grossness
kinterbasdb.init(type_conv=200)
# do this here just to catch db uri errors at startup
global_connect(db_uri, echo=self.echo)
def __call__(self, environ, start_response):
environ['p2pserver.transaction_manager'] = manager =
Manager(self.db_uri, echo=self.echo)
# This makes sure nothing else traps unexpected exceptions:
environ['paste.throw_errors'] = True
return catch_errors(self.application, environ, start_response,
error_callback=manager.error,
ok_callback=manager.finish)
class Manager(object):
def __init__(self, *args, **kw):
self.aborted = False
global_connect(*args, **kw )
self.engine = schema.default_engine.get_engine()
self.engine.begin()
def get_session(self):
"""return session object"""
return objectstore.get_session()
def abort(self):
self.aborted = True
def error(self, exc_info):
self.aborted = True
self.finish()
def finish(self):
if self.aborted:
objectstore.clear()
self.engine.rollback()
else:
objectstore.commit()
self.engine.commit()
--- end middleware
and, if a lower level app needs access to the objectstore or secction:
def __call__(self, environ, start_response):
"""Generate the response"""
transaction_manager = environ['p2pserver.transaction_manager']
session = transaction_manager.get_session()
--
Brad Clements, [EMAIL PROTECTED] (315)268-1000
http://www.murkworks.com
AOL-IM or SKYPE: BKClements
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users