|
|
@@ -36,41 +36,82 @@ class WSGIMySQL( object ): |
|
|
__metaclass__ = Singleton |
|
|
__metaclass__ = Singleton |
|
|
|
|
|
|
|
|
def __init__( self, dsn, *args ): |
|
|
def __init__( self, dsn, *args ): |
|
|
|
|
|
""" inizializza le connessioni a 1 o più database. |
|
|
|
|
|
In caso di connessioni a databese multipli, |
|
|
|
|
|
le connessioni sono identificate tramite ALIAS |
|
|
|
|
|
o in mancanza di questo tramite DB |
|
|
|
|
|
|
|
|
|
|
|
ogni singolo dsn deve essere un dizionario con le chiavi: |
|
|
|
|
|
- DB : nome del database |
|
|
|
|
|
- HOST : host a cui connettersi |
|
|
|
|
|
- USER : username da utilizzare per connettersi |
|
|
|
|
|
- PASSWORD : password da utilizzare per connettersi |
|
|
|
|
|
- ALIAS : (opzionale) identificativo della connessione |
|
|
|
|
|
""" |
|
|
import MySQLdb |
|
|
import MySQLdb |
|
|
|
|
|
|
|
|
self.__dsn = dsn |
|
|
|
|
|
self.__pool = [ self.__newconn() ] |
|
|
|
|
|
|
|
|
# |
|
|
|
|
|
# aggiungiamo il primo dsn in cima alla lista |
|
|
|
|
|
# |
|
|
|
|
|
args = list( args ) |
|
|
|
|
|
args.insert( 0, dsn ) |
|
|
|
|
|
|
|
|
|
|
|
# |
|
|
|
|
|
# creiamo il nostro dizionario di dizionari |
|
|
|
|
|
# |
|
|
|
|
|
self.__dsn = { dsndict.get( 'ALIAS', dsndict['DB'] ):dsndict for dsndict in args } |
|
|
|
|
|
|
|
|
|
|
|
# |
|
|
|
|
|
# verifichiamo che non ci siano alias duplicati |
|
|
|
|
|
# |
|
|
|
|
|
if len( self.__dsn.keys() ) != len( args ): |
|
|
|
|
|
raise Exception( "WSGIMySQL :: conflicting alias in dsn list" ) |
|
|
|
|
|
|
|
|
|
|
|
# |
|
|
|
|
|
# tentiamo di creare la prima connessione verso TUTTI i dsn passati |
|
|
|
|
|
# |
|
|
|
|
|
|
|
|
|
|
|
for alias, dsndict in self.__dsn.iteritems(): |
|
|
|
|
|
dsndict['pool'] = [ self.__newconn( alias ) ] |
|
|
|
|
|
|
|
|
self.__dict_cursor = MySQLdb.cursors.DictCursor |
|
|
self.__dict_cursor = MySQLdb.cursors.DictCursor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __newconn( self ): |
|
|
|
|
|
|
|
|
def __newconn( self, alias ): |
|
|
import MySQLdb |
|
|
import MySQLdb |
|
|
|
|
|
|
|
|
return MySQLdb.connect( |
|
|
return MySQLdb.connect( |
|
|
host = self.__dsn["HOST"], |
|
|
|
|
|
user = self.__dsn["USER"], |
|
|
|
|
|
passwd = self.__dsn["PASSWORD"], |
|
|
|
|
|
db = self.__dsn["DB"] |
|
|
|
|
|
|
|
|
host = self.__dsn[ alias ]["HOST"], |
|
|
|
|
|
user = self.__dsn[ alias ]["USER"], |
|
|
|
|
|
passwd = self.__dsn[ alias ]["PASSWORD"], |
|
|
|
|
|
db = self.__dsn[ alias ]["DB"] |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
def db( self, *args ): |
|
|
def db( self, *args ): |
|
|
def real_decorator( wsgi_application ): |
|
|
def real_decorator( wsgi_application ): |
|
|
def wrapper( environ, start_response ): |
|
|
def wrapper( environ, start_response ): |
|
|
try: |
|
|
|
|
|
conn = self.__pool.pop( 0 ) |
|
|
|
|
|
except IndexError: |
|
|
|
|
|
conn = self.__newconn() |
|
|
|
|
|
|
|
|
connections = [] |
|
|
|
|
|
|
|
|
|
|
|
for arg in args: |
|
|
|
|
|
try: |
|
|
|
|
|
conn = self.__dsn[ arg ]['pool'].pop( 0 ) |
|
|
|
|
|
except IndexError: |
|
|
|
|
|
conn = self.__newconn( arg ) |
|
|
|
|
|
|
|
|
|
|
|
connections.append( conn ) |
|
|
|
|
|
|
|
|
cur = conn.cursor( self.__dict_cursor ) |
|
|
|
|
|
|
|
|
cur = conn.cursor( self.__dict_cursor ) |
|
|
|
|
|
|
|
|
environ['mysql.cur'] = cur |
|
|
|
|
|
|
|
|
environ['mysql.' + arg + '.cur'] = cur |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
for item in wsgi_application( environ, start_response ): |
|
|
for item in wsgi_application( environ, start_response ): |
|
|
yield item |
|
|
yield item |
|
|
finally: |
|
|
finally: |
|
|
conn.commit() |
|
|
|
|
|
self.__pool.append( conn ) |
|
|
|
|
|
|
|
|
for arg in args: |
|
|
|
|
|
conn = connections.pop(0) |
|
|
|
|
|
conn.commit() |
|
|
|
|
|
self.__dsn[ arg ]['pool'].append( conn ) |
|
|
|
|
|
|
|
|
return wrapper |
|
|
return wrapper |
|
|
|
|
|
|
|
|
@@ -78,5 +119,9 @@ class WSGIMySQL( object ): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __del__( self ): |
|
|
def __del__( self ): |
|
|
for conn in self.__pool: |
|
|
|
|
|
conn.close() |
|
|
|
|
|
|
|
|
# |
|
|
|
|
|
# chiudiamo tutte le connessioni attualmente aperte |
|
|
|
|
|
# |
|
|
|
|
|
for dsndict in self.__dsn.items(): |
|
|
|
|
|
for conn in dsndict['pool']: |
|
|
|
|
|
conn.close() |