Show
Ignore:
Timestamp:
03/23/08 01:03:06 (8 months ago)
Author:
edsuom
Message:

VERSION 0.7: Updated to work with SA 0.4

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/sAsync/trunk/sasync/database.py

    r128 r129  
    2828from twisted.internet import defer 
    2929from twisted.python import failure 
     30 
    3031import sqlalchemy as SA 
    31 from sqlalchemy.orm import create_session 
     32 
     33###################################################################### 
     34# SA 0.4 support contributed by Ricky Iacovou,  based upon: 
     35
     36#   http://www.sqlalchemy.org/docs/04/intro.html#overview_migration 
     37
     38# Determine the version of SQLAlchemy used, 0.3 or 0.4, and set the 
     39# Boolean variable "SA04" accordingly. 
     40
     41# We could also use a capability-based approach, like: 
     42
     43# try: 
     44#     MetaData = SA.BoundMetaData 
     45# except AttributeError: 
     46#     MetaData = SA.MetaData 
     47
     48# However, late 0.3.x versions also supported some 0.4 constructs, 
     49# so better use an explicit 0.3.x -> 0.4.x cutoff in order to avoid 
     50# ambiguity. 
     51###################################################################### 
     52_sv = SA.__version__.split ('.') 
     53try: 
     54    _v = int (_sv[0]) + (int(_sv[1]) / 10.0) 
     55except: 
     56    # Not strictly an Import Error, but close enough. 
     57    raise ImportError("Failed to determine SQLAlchemy version: %s", _sv) 
     58if _v >= 0.4: 
     59    SA04 = True 
     60else: 
     61    SA04 = False 
     62del _sv, _v 
     63# End of version check 
     64 
    3265 
    3366from asynqueue import ThreadQueue 
     
    287320        def createEngine(): 
    288321            url, kw = self.engineParams 
     322            # The 'threadlocal' keyword value is unchanged from SA 0.3 to 0.4 
    289323            kw['strategy'] = 'threadlocal' 
    290324            return SA.create_engine(url, **kw) 
     
    331365        """ 
    332366        def gotConnection(connection): 
    333             d = self.q.call(create_session, connection, doNext=True) 
     367            if SA04: 
     368                d = self.q.call( 
     369                    SA.orm.create_session, bind=connection, doNext=True) 
     370            else: 
     371                d = self.q.call( 
     372                    SA.create_session, bind_to=connection, doNext=True) 
    334373            d.addCallback(gotSession) 
    335374            return d 
     
    360399        def _table(): 
    361400            if not hasattr(self, '_meta'): 
    362                 self._meta = SA.MetaData(self._engine) 
     401                if SA04: 
     402                    self._meta = SA.MetaData(self._engine) 
     403                else: 
     404                    self._meta = SA.BoundMetaData(self._engine) 
    363405            indexes = {} 
    364406            for key in kw.keys():