pgtoolkit/pgmanager.py
changeset 46 037410ef2b6b
parent 45 6d8e4ee4bdd2
child 48 b82c7c2fb5af
equal deleted inserted replaced
45:6d8e4ee4bdd2 46:037410ef2b6b
   141 
   141 
   142 
   142 
   143 class Cursor(psycopg2.extensions.cursor):
   143 class Cursor(psycopg2.extensions.cursor):
   144 
   144 
   145     def execute(self, query, args=None):
   145     def execute(self, query, args=None):
   146         self._log_query(query, args)
       
   147         try:
   146         try:
   148             return super(Cursor, self).execute(query, args)
   147             return super(Cursor, self).execute(query, args)
   149         except DatabaseError:
   148         except DatabaseError:
       
   149             # log query and exception
       
   150             self._log_query(query, args)
   150             self._log_exception()
   151             self._log_exception()
       
   152             raise
       
   153         else:
       
   154             # log query from self.query
       
   155             self._log_query()
   151 
   156 
   152     def callproc(self, procname, args=None):
   157     def callproc(self, procname, args=None):
   153         self._log_query('CALL %s(%s)' % (procname, ','.join(args)))
       
   154         try:
   158         try:
   155             return super(Cursor, self).callproc(procname, args)
   159             return super(Cursor, self).callproc(procname, args)
   156         except DatabaseError:
   160         except DatabaseError:
       
   161             # log query and exception
       
   162             self._log_query(self._build_callproc_query(procname, len(args)), args)
   157             self._log_exception()
   163             self._log_exception()
       
   164             raise
       
   165         else:
       
   166             # log query from self.query
       
   167             self._log_query()
   158 
   168 
   159     def row_dict(self, row, lstrip=None):
   169     def row_dict(self, row, lstrip=None):
   160         adjustname = lambda a: a
   170         adjustname = lambda a: a
   161         if lstrip:
   171         if lstrip:
   162             adjustname = lambda a: a.lstrip(lstrip)
   172             adjustname = lambda a: a.lstrip(lstrip)
   200     def fetchall_adapted(self, lstrip=None):
   210     def fetchall_adapted(self, lstrip=None):
   201         '''Like fetchall_dict() but values are quoted for direct inclusion in SQL query.'''
   211         '''Like fetchall_dict() but values are quoted for direct inclusion in SQL query.'''
   202         rows = super(Cursor, self).fetchall()
   212         rows = super(Cursor, self).fetchall()
   203         return [self.row_dict([self.mogrify('%s', [x]).decode('utf8') for x in row], lstrip) for row in rows]
   213         return [self.row_dict([self.mogrify('%s', [x]).decode('utf8') for x in row], lstrip) for row in rows]
   204 
   214 
   205     def _log_query(self, query, args=None):
   215     def _log_query(self, query='?', args=None):
   206         name = self.connection.name if hasattr(self.connection, 'name') else '-'
   216         name = self.connection.name if hasattr(self.connection, 'name') else '-'
   207         log_sql.info('[%s] %s' % (name, self.mogrify(query, args).decode('utf8')))
   217         query = self.query or self.mogrify(query, args)
       
   218         log_sql.info('[%s] %s' % (name, query.decode('utf8')))
   208 
   219 
   209     def _log_exception(self):
   220     def _log_exception(self):
   210         name = self.connection.name if hasattr(self.connection, 'name') else '-'
   221         name = self.connection.name if hasattr(self.connection, 'name') else '-'
   211         log_sql.exception('[%s] exception:' % (name,))
   222         log_sql.exception('[%s] exception:' % (name,))
       
   223 
       
   224     def _build_callproc_query(self, procname, num_args):
       
   225         return 'SELECT * FROM %s(%s)' %(procname, ', '.join('%s'))
       
   226 
   212 
   227 
   213 class Connection(psycopg2.extensions.connection):
   228 class Connection(psycopg2.extensions.connection):
   214 
   229 
   215     def cursor(self, name=None):
   230     def cursor(self, name=None):
   216         if name is None:
   231         if name is None: