pgtoolkit/pgmanager.py
changeset 48 b82c7c2fb5af
parent 46 037410ef2b6b
child 54 291473ab847c
equal deleted inserted replaced
47:bb8c729ae6ce 48:b82c7c2fb5af
   137             raise AttributeError(key)
   137             raise AttributeError(key)
   138 
   138 
   139     def __contains__(self, key):
   139     def __contains__(self, key):
   140         return key in self._dict
   140         return key in self._dict
   141 
   141 
       
   142     def keys(self):
       
   143         return self._dict.keys()
       
   144 
   142 
   145 
   143 class Cursor(psycopg2.extensions.cursor):
   146 class Cursor(psycopg2.extensions.cursor):
   144 
   147 
   145     def execute(self, query, args=None):
   148     def execute(self, query, args=None):
       
   149         # log query before executing
       
   150         self._log_query(query, args)
   146         try:
   151         try:
   147             return super(Cursor, self).execute(query, args)
   152             return super(Cursor, self).execute(query, args)
   148         except DatabaseError:
   153         except DatabaseError:
   149             # log query and exception
       
   150             self._log_query(query, args)
       
   151             self._log_exception()
   154             self._log_exception()
   152             raise
   155             raise
   153         else:
       
   154             # log query from self.query
       
   155             self._log_query()
       
   156 
   156 
   157     def callproc(self, procname, args=None):
   157     def callproc(self, procname, args=None):
       
   158         # log query before executing (not query actually executed but should correspond)
       
   159         self._log_query(self._build_callproc_query(procname, len(args)), args)
   158         try:
   160         try:
   159             return super(Cursor, self).callproc(procname, args)
   161             return super(Cursor, self).callproc(procname, args)
   160         except DatabaseError:
   162         except DatabaseError:
   161             # log query and exception
       
   162             self._log_query(self._build_callproc_query(procname, len(args)), args)
       
   163             self._log_exception()
   163             self._log_exception()
   164             raise
   164             raise
   165         else:
       
   166             # log query from self.query
       
   167             self._log_query()
       
   168 
   165 
   169     def row_dict(self, row, lstrip=None):
   166     def row_dict(self, row, lstrip=None):
   170         adjustname = lambda a: a
   167         adjustname = lambda a: a
   171         if lstrip:
   168         if lstrip:
   172             adjustname = lambda a: a.lstrip(lstrip)
   169             adjustname = lambda a: a.lstrip(lstrip)
   220     def _log_exception(self):
   217     def _log_exception(self):
   221         name = self.connection.name if hasattr(self.connection, 'name') else '-'
   218         name = self.connection.name if hasattr(self.connection, 'name') else '-'
   222         log_sql.exception('[%s] exception:' % (name,))
   219         log_sql.exception('[%s] exception:' % (name,))
   223 
   220 
   224     def _build_callproc_query(self, procname, num_args):
   221     def _build_callproc_query(self, procname, num_args):
   225         return 'SELECT * FROM %s(%s)' %(procname, ', '.join('%s'))
   222         return 'SELECT * FROM %s(%s)' % (procname, ', '.join(['%s'] * num_args))
   226 
   223 
   227 
   224 
   228 class Connection(psycopg2.extensions.connection):
   225 class Connection(psycopg2.extensions.connection):
   229 
   226 
   230     def cursor(self, name=None):
   227     def cursor(self, name=None):