wsgi_middleware.py :  » Database » SQLObject » SQLObject-0.12.4 » sqlobject » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » SQLObject 
SQLObject » SQLObject 0.12.4 » sqlobject » wsgi_middleware.py
from paste.deploy.converters import asbool
from paste.wsgilib import catch_errors
from paste.util import import_string
import sqlobject
import threading

def make_middleware(app, global_conf, database=None, use_transaction=False,
                    hub=None):
    """
    WSGI middleware that sets the connection for the request (using
    the database URI or connection object) and the given hub (or
    ``sqlobject.sqlhub`` if not given).

    If ``use_transaction`` is true, then the request will be run in a
    transaction.

    Applications can use the keys (which are all no-argument functions):

    ``sqlobject.get_connection()``:
      Returns the connection object
      
    ``sqlobject.abort()``:
      Aborts the transaction.  Does not raise an error, but at the *end*
      of the request there will be a rollback.
      
    ``sqlobject.begin()``:
      Starts a transaction.  First commits (or rolls back if aborted) if
      this is run in a transaction.

    ``sqlobject.in_transaction()``:
      Returns true or false, depending if we are currently in a
      transaction.
    """
    use_transaction = asbool(use_transaction)
    if database is None:
        database = global_conf.get('database')
    if not database:
        raise ValueError(
            "You must provide a 'database' configuration value")
    if isinstance(hub, basestring):
        hub = import_string.eval_import(hub)
    if not hub:
        hub = sqlobject.sqlhub
    if isinstance(database, basestring):
        database = sqlobject.connectionForURI(database)
    return SQLObjectMiddleware(app, database, use_transaction, hub)

class SQLObjectMiddleware(object):

    def __init__(self, app, conn, use_transaction, hub):
        self.app = app
        self.conn = conn
        self.use_transaction = use_transaction
        self.hub = hub

    def __call__(self, environ, start_response):
        conn = [self.conn]
        if self.use_transaction:
            conn[0] = conn[0].transaction()
        any_errors = []
        use_transaction = [self.use_transaction]
        self.hub.threadConnection = conn[0]
        def abort():
            assert use_transaction[0], (
                "You cannot abort, because a transaction is not being used")
            any_errors.append(None)
        def begin():
            if use_transaction[0]:
                if any_errors:
                    conn[0].rollback()
                else:
                    conn[0].commit()
            any_errors[:] = []
            use_transaction[0] = True
            conn[0] = self.conn.transaction()
            self.hub.threadConnection = conn[0]
        def error(exc_info=None):
            any_errors.append(None)
            ok()
        def ok():
            if use_transaction[0]:
                if any_errors:
                    conn[0].rollback()
                else:
                    conn[0].commit(close=True)
            self.hub.threadConnection = None
        def in_transaction():
            return use_transaction[0]
        def get_connection():
            return conn[0]
        environ['sqlobject.get_connection'] = get_connection
        environ['sqlobject.abort'] = abort
        environ['sqlobject.begin'] = begin
        environ['sqlobject.in_transaction'] = in_transaction
        return catch_errors(self.app, environ, start_response,
                            error_callback=error, ok_callback=ok)
        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.