Commits

Olemis Lang committed 7f125be

GViz providers : Upgrade ReportRPC to cope with ReportModule.sql_sub_vars API changes in Trac=1.0 ... (failures=3, errors=4)

  • Participants
  • Parent commits 9dcaff5
  • Branches providers

Comments (0)

Files changed (1)

File trac-dev/gviz/tracgviz/rpc.py

 Copyright 2009-2011 Olemis Lang <olemis at gmail.com>
 Licensed under the Apache License, Version 2.0 
 """
+from __future__ import with_statement
+
 __author__ = 'Olemis Lang'
 
 __all__ = 'TimelineRPC', 'ReportRPC', 'VersionControlRPC'
           cols, results = self._execute_sql(req, id, sql)
           return (dict(zip(cols, list(row))) for row in results)
     
-    def _sql_cursor(self, req, db, id, sql, args, limit=0, offset=0):
+    def _sql_cursor(self, req, id, sql, args, limit=0, offset=0):
       r"""Retrieve a cursor to access the data returned by a SQL 
       report.
       """
       # Copycat ! ReportModule.execute_paginated_report
       # I didnt want to but I had no other choice :(
       repmdl = self.repmdl
-      sql, args = repmdl.sql_sub_vars(sql, args, db)
+      sql, args, missing_args = repmdl.sql_sub_vars(sql, args)
+      if missing_args:
+        self.log.warning('The following arguments are missing: %s',
+            ", ".join(missing_args))
+
       if not sql:
           raise ValueError(_('Report %(num)s has no SQL query.', num=id))
       self.log.debug('IG: Executing report with SQL "%s"' % sql)
       self.log.debug('IG: Request args: %r' % req.args)
-      cursor = db.cursor()
-      
-      # The column name is obtained.
-      get_col_name_sql = 'SELECT * FROM ( ' + sql + ' ) AS tab LIMIT 1'
-      cursor.execute(get_col_name_sql, args)
-      self.env.log.debug("IG: Query SQL(Get col names): " + get_col_name_sql)
-      return cursor
+
+      with self.env.db_query as db:
+        cursor = db.cursor()
+        
+        # The column name is obtained.
+        get_col_name_sql = 'SELECT * FROM ( ' + sql + ' ) AS tab LIMIT 1'
+        cursor.execute(get_col_name_sql, args)
+        self.env.log.debug("IG: Query SQL(Get col names): " + get_col_name_sql)
+        return cursor
     
     def _sql_columns(self, req, id, sql):
       r"""Retrieve the description of columns returned by a SQL 
       report.
       """
       repmdl = self.repmdl
-      db = self.env.get_db_cnx()
       try:
         args = repmdl.get_var_args(req)
       except ValueError,e:
         raise ValueError(_('Report failed: %(error)s', error=e))
-      try:
-          cursor = self._sql_cursor(req, db, id, sql, args)
-      except Exception, e:
-          db.rollback()
-          raise 
-      else:
-        self.log.debug('IG: Cursor desc %s', cursor.description)
-        cols = list(get_column_desc(cursor, True))
-        cursor.close()
-        return cols
+      cursor = self._sql_cursor(req, id, sql, args)
+      self.log.debug('IG: Cursor desc %s', cursor.description)
+      cols = list(get_column_desc(cursor, True))
+      cursor.close()
+      return cols
     
     def enum_columns(self, req, id):
         r"""Retrieve the columns present in a custom report.