mytoolkit/mymanager.py
changeset 75 39f777341db4
parent 74 d4306261ddfb
child 77 2cfef775f518
equal deleted inserted replaced
74:d4306261ddfb 75:39f777341db4
    71 
    71 
    72 from pgtoolkit.pgmanager import RowDict
    72 from pgtoolkit.pgmanager import RowDict
    73 
    73 
    74 
    74 
    75 log_sql = logging.getLogger("mymanager_sql")
    75 log_sql = logging.getLogger("mymanager_sql")
       
    76 log_sql.addHandler(logging.NullHandler())
    76 
    77 
    77 
    78 
    78 class MyManagerError(Exception):
    79 class MyManagerError(Exception):
    79 
    80 
    80     pass
    81     pass
   106 
   107 
   107     def execute(self, query, args=None):
   108     def execute(self, query, args=None):
   108         try:
   109         try:
   109             return super(Cursor, self).execute(query, args)
   110             return super(Cursor, self).execute(query, args)
   110         finally:
   111         finally:
   111             log_sql.debug(self._executed.decode('utf8'))
   112             self._log_query(query, args)
   112 
   113 
   113     def callproc(self, procname, args=None):
   114     def callproc(self, procname, args=None):
   114         try:
   115         try:
   115             return super(Cursor, self).callproc(procname, args)
   116             return super(Cursor, self).callproc(procname, args)
   116         finally:
   117         finally:
   117             log_sql.debug(self._executed.decode('utf8'))
   118             self._log_query(query, args)
   118 
   119 
   119     def row_dict(self, row, lstrip=None):
   120     def row_dict(self, row, lstrip=None):
   120         adjustname = lambda a: a
   121         adjustname = lambda a: a
   121         if lstrip:
   122         if lstrip:
   122             adjustname = lambda a: a.lstrip(lstrip)
   123             adjustname = lambda a: a.lstrip(lstrip)
   129         return self.row_dict(row, lstrip)
   130         return self.row_dict(row, lstrip)
   130 
   131 
   131     def fetchall_dict(self, lstrip=None):
   132     def fetchall_dict(self, lstrip=None):
   132         rows = super(Cursor, self).fetchall()
   133         rows = super(Cursor, self).fetchall()
   133         return [self.row_dict(row, lstrip) for row in rows]
   134         return [self.row_dict(row, lstrip) for row in rows]
       
   135 
       
   136     def mogrify(self, query, args):
       
   137         """Get query with substituted args as it will be send to server."""
       
   138         if isinstance(query, bytes):
       
   139             query = query.decode()
       
   140         if args is not None:
       
   141             db = self._get_db()
       
   142             query = query % db.literal(args)
       
   143         return query
       
   144 
       
   145     def _log_query(self, query, args):
       
   146         name = self.connection.name if hasattr(self.connection, 'name') else '-'
       
   147         query = self._executed or self.mogrify(query, args)
       
   148         if isinstance(query, bytes):
       
   149             db = self._get_db()
       
   150             charset = db.character_set_name()
       
   151             query = query.decode(charset)
       
   152         log_sql.info('[%s] %s' % (name, query))
   134 
   153 
   135 
   154 
   136 class MyManager:
   155 class MyManager:
   137 
   156 
   138     def __init__(self):
   157     def __init__(self):