pgtoolkit/pgmanager.py
changeset 26 7f219da7ab71
parent 24 5664afa530e5
child 31 c2e6e24b83d9
equal deleted inserted replaced
25:20a72a9a2d09 26:7f219da7ab71
    80 import psycopg2.extensions
    80 import psycopg2.extensions
    81 
    81 
    82 from psycopg2 import DatabaseError, IntegrityError, OperationalError
    82 from psycopg2 import DatabaseError, IntegrityError, OperationalError
    83 
    83 
    84 
    84 
    85 log = logging.getLogger("pgmanager")
    85 log_sql = logging.getLogger("pgmanager_sql")
       
    86 log_notices = logging.getLogger("pgmanager_notices")
    86 
    87 
    87 
    88 
    88 class PgManagerError(Exception):
    89 class PgManagerError(Exception):
    89 
    90 
    90     pass
    91     pass
   113     def execute(self, query, args=None):
   114     def execute(self, query, args=None):
   114         try:
   115         try:
   115             return super(Cursor, self).execute(query, args)
   116             return super(Cursor, self).execute(query, args)
   116         finally:
   117         finally:
   117             if self.query:
   118             if self.query:
   118                 log.debug(self.query.decode('utf8'))
   119                 log_sql.info(self.query.decode('utf8'))
   119 
   120 
   120     def callproc(self, procname, args=None):
   121     def callproc(self, procname, args=None):
   121         try:
   122         try:
   122             return super(Cursor, self).callproc(procname, args)
   123             return super(Cursor, self).callproc(procname, args)
   123         finally:
   124         finally:
   124             if self.query:
   125             if self.query:
   125                 log.debug(self.query.decode('utf8'))
   126                 log_sql.info(self.query.decode('utf8'))
   126 
   127 
   127     def row_dict(self, row, lstrip=None):
   128     def row_dict(self, row, lstrip=None):
   128         adjustname = lambda a: a
   129         adjustname = lambda a: a
   129         if lstrip:
   130         if lstrip:
   130             adjustname = lambda a: a.lstrip(lstrip)
   131             adjustname = lambda a: a.lstrip(lstrip)
   304         try:
   305         try:
   305             curs = conn.cursor()
   306             curs = conn.cursor()
   306             yield curs
   307             yield curs
   307         finally:
   308         finally:
   308             curs.close()
   309             curs.close()
       
   310             self.log_notices(conn)
   309             self.put_conn(conn, name)
   311             self.put_conn(conn, name)
       
   312 
       
   313     def log_notices(self, conn):
       
   314         for notice in conn.notices:
       
   315             log_notices.info(notice.rstrip())
       
   316         conn.notices[:] = []
   310 
   317 
   311     def wait_for_notify(self, name='default', timeout=5):
   318     def wait_for_notify(self, name='default', timeout=5):
   312         '''Wait for asynchronous notifies, return the last one.
   319         '''Wait for asynchronous notifies, return the last one.
   313         
   320         
   314         Returns None on timeout.
   321         Returns None on timeout.