|
@@ -1,8 +1,12 @@
|
|
|
'''
|
|
'''
|
|
|
Convenient access to various databases
|
|
Convenient access to various databases
|
|
|
'''
|
|
'''
|
|
|
|
|
+import logging
|
|
|
|
|
+
|
|
|
from pypyodbc import Connection
|
|
from pypyodbc import Connection
|
|
|
|
|
|
|
|
|
|
+logger = logging.getLogger("database")
|
|
|
|
|
+
|
|
|
class CustomDb(Connection):
|
|
class CustomDb(Connection):
|
|
|
""" Connexion to a database """
|
|
""" Connexion to a database """
|
|
|
_cache = {}
|
|
_cache = {}
|
|
@@ -22,6 +26,7 @@ class CustomDb(Connection):
|
|
|
|
|
|
|
|
def connect(self, *args, **kwargs):
|
|
def connect(self, *args, **kwargs):
|
|
|
""" Establish the connexion to the database"""
|
|
""" Establish the connexion to the database"""
|
|
|
|
|
+ logger.info("Connection to %s: %s", self.__class__.__name__, self.connectString)
|
|
|
super(CustomDb, self).connect(*args, **kwargs)
|
|
super(CustomDb, self).connect(*args, **kwargs)
|
|
|
|
|
|
|
|
def read(self, sql, *args):
|
|
def read(self, sql, *args):
|
|
@@ -29,7 +34,7 @@ class CustomDb(Connection):
|
|
|
cursor = self.execute(sql)
|
|
cursor = self.execute(sql)
|
|
|
row = cursor.fetchone()
|
|
row = cursor.fetchone()
|
|
|
while row:
|
|
while row:
|
|
|
- yield row
|
|
|
|
|
|
|
+ yield dict(zip([column[0] for column in cursor.description], row))
|
|
|
row = cursor.fetchone()
|
|
row = cursor.fetchone()
|
|
|
cursor.close()
|
|
cursor.close()
|
|
|
|
|
|
|
@@ -40,6 +45,12 @@ class CustomDb(Connection):
|
|
|
cursor.close()
|
|
cursor.close()
|
|
|
return data
|
|
return data
|
|
|
|
|
|
|
|
|
|
+ def first(self, sql, *args):
|
|
|
|
|
+ try:
|
|
|
|
|
+ return next(self.read(sql, *args))
|
|
|
|
|
+ except StopIteration:
|
|
|
|
|
+ return None
|
|
|
|
|
+
|
|
|
def execute(self, sql, *args):
|
|
def execute(self, sql, *args):
|
|
|
cursor = self.cursor()
|
|
cursor = self.cursor()
|
|
|
args = [sql, tuple(args)] if args else [sql]
|
|
args = [sql, tuple(args)] if args else [sql]
|