Index: trunk/plugins/open/timingandestimationplugin/timingandestimationplugin/api.py =================================================================== --- trunk/plugins/open/timingandestimationplugin/timingandestimationplugin/api.py (revision 325) +++ trunk/plugins/open/timingandestimationplugin/timingandestimationplugin/api.py (revision 452) @@ -72,6 +72,6 @@ # Legacy support hack (supports upgrades from 0.1.6 to 0.1.7) if self.db_installed_version == 0: + report_version = dbhelper.db_table_exists(self, 'report_version'); bill_date = dbhelper.db_table_exists(self, 'bill_date'); - report_version = dbhelper.db_table_exists(self, 'report_version'); if bill_date and report_version: self.db_installed_version = 1 Index: trunk/plugins/open/timingandestimationplugin/timingandestimationplugin/dbhelper.py =================================================================== --- trunk/plugins/open/timingandestimationplugin/timingandestimationplugin/dbhelper.py (revision 325) +++ trunk/plugins/open/timingandestimationplugin/timingandestimationplugin/dbhelper.py (revision 452) @@ -2,58 +2,31 @@ def get_all(com, sql, *params): """Executes the query and returns the (description, data)""" - db = com.env.get_db_cnx() + db = com.env.get_read_db() cur = db.cursor() desc = None data = None - try: - cur.execute(sql, params) - data = list(cur.fetchall()) - desc = cur.description - db.commit(); - except Exception, e: - com.log.error('There was a problem executing sql:%s \n \ -with parameters:%s\nException:%s'%(sql, params, e)); - db.rollback(); - try: - db.close() - except: - pass - + cur.execute(sql, params) + data = list(cur.fetchall()) + desc = cur.description return (desc, data) def execute_non_query(com, sql, *params): """Executes the query on the given project""" - db = com.env.get_db_cnx() + @com.env.with_transaction() + def do_execute(db): + cur = db.cursor() + cur.execute(sql, params) + +def get_first_row(com, sql, *params): + """ Returns the first row of the query results as a tuple of values (or None)""" + db = com.env.get_read_db() cur = db.cursor() try: cur.execute(sql, params) - db.commit() + return cur.fetchone() except Exception, e: com.log.error('There was a problem executing sql:%s \n \ -with parameters:%s\nException:%s'%(sql, params, e)); - db.rollback(); - try: - db.close() - except: - pass - -def get_first_row(com, sql,*params): - """ Returns the first row of the query results as a tuple of values (or None)""" - db = com.env.get_db_cnx() - cur = db.cursor() - data = None; - try: - cur.execute(sql, params) - data = cur.fetchone(); - db.commit(); - except Exception, e: - com.log.error('There was a problem executing sql:%s \n \ - with parameters:%s\nException:%s'%(sql, params, e)); - db.rollback() - try: - db.close() - except: - pass - return data; + with parameters:%s\nException:%s'%(sql, params, e)) + return None def get_scalar(com, sql, col=0, *params): @@ -66,39 +39,21 @@ def execute_in_trans(com, *args): - db = com.env.get_db_cnx() - cur = db.cursor() - result = True - try: + @com.env.with_transaction() + def do_execute(db): + cur = db.cursor() for sql, params in args: cur.execute(sql, params) - db.commit() - except Exception, e: - com.log.error('There was a problem executing sql:%s \n \ - with parameters:%s\nException:%s'%(sql, params, e)); - db.rollback(); - result = e - try: - db.close() - except: - pass - return result + return True def db_table_exists(com, table): - db = com.env.get_db_cnx() - sql = "SELECT * FROM %s LIMIT 1" % table; - cur = db.cursor() - has_table = True; - try: - cur.execute(sql) - db.commit() - except Exception, e: - has_table = False - db.rollback() - - try: - db.close() - except: - pass - return has_table + @com.env.with_transaction() + def do_check(db): + sql = "SELECT * FROM %s LIMIT 1" % table; + cur = db.cursor() + try: + cur.execute(sql) + except Exception, e: + return False + return True def get_column_as_list(com, sql, col=0, *params): @@ -110,10 +65,5 @@ def set_system_value(com, key, value): - if get_system_value(com, key): - execute_non_query(com, "UPDATE system SET value=%s WHERE name=%s", value, key) - else: - execute_non_query(com, "INSERT INTO system (value, name) VALUES (%s, %s)", - value, key) - + execute_non_query(com, "REPLACE INTO system (value, name) VALUES (%s, %s)", value, key) def get_result_set(com, sql, *params): Index: trunk/plugins/open/timingandestimationplugin/timingandestimationplugin/reportmanager.py =================================================================== --- trunk/plugins/open/timingandestimationplugin/timingandestimationplugin/reportmanager.py (revision 325) +++ trunk/plugins/open/timingandestimationplugin/timingandestimationplugin/reportmanager.py (revision 452) @@ -1,5 +1,4 @@ from trac.core import * - - +from trac.db.postgres_backend import PostgreSQLConnection class CustomReportManager: @@ -18,22 +17,22 @@ def upgrade(self): # Check to see what version we have - db = self.env.get_db_cnx() - cursor = db.cursor() - cursor.execute("SELECT value FROM system WHERE name=%s", (self.name,)) - try: - version = int(cursor.fetchone()[0]) - except: - version = 0 - cursor.execute("INSERT INTO system (name,value) VALUES(%s,%s)", - (self.name, version)) - - if version > self.version: - raise TracError("Fatal Error: You appear to be running two plugins with conflicting versions " - "of the CustomReportManager class. Please ensure that '%s' is updated to " - "version %s of the file reportmanager.py (currently using version %s)." - % (__name__, str(version), str(self.version))) - - # Do the staged updates - try: + @self.env.with_transaction() + def do_upgrade(db): + cursor = db.cursor() + cursor.execute("SELECT value FROM system WHERE name=%s", (self.name,)) + try: + version = int(cursor.fetchone()[0]) + except: + version = 0 + cursor.execute("INSERT INTO system (name,value) VALUES(%s,%s)", + (self.name, version)) + + if version > self.version: + raise TracError("Fatal Error: You appear to be running two plugins with conflicting versions " + "of the CustomReportManager class. Please ensure that '%s' is updated to " + "version %s of the file reportmanager.py (currently using version %s)." + % (__name__, str(version), str(self.version))) + + # Do the staged updates if version < 1: cursor.execute("CREATE TABLE custom_report (" @@ -51,10 +50,4 @@ cursor.execute("UPDATE system SET value=%s WHERE name=%s", (self.version, self.name)) - db.commit() - db.close() - - except Exception, e: - self.log.error("CustomReportManager Exception: %s" % (e,)); - db.rollback() def get_report_id_and_version (self, uuid): @@ -86,10 +79,12 @@ "VALUES (%s, %s, %s, %s, %s, %s)", (next_id, uuid, maingroup, subgroup, version, ordering))) - self.log.debug("Attempting to increment sequence (only works in postgres)") - try: - self.execute_in_trans(("SELECT nextval('report_id_seq');",[])); - self.log.debug("Sequence updated"); - except: - self.log.debug("Sequence failed to update, perhaps you are not running postgres?"); + + if isinstance(self.env.get_read_db().cnx, PostgreSQLConnection): + self.log.debug("Attempting to increment sequence (only works in postgres)") + try: + self.execute_in_trans(("SELECT nextval('report_id_seq');",[])); + self.log.debug("Sequence updated"); + except: + self.log.debug("Sequence failed to update, perhaps you are not running postgres?"); def _update_report (self, id, title, author, description, query, @@ -136,5 +131,5 @@ def get_reports_by_group(self, group): """Gets all of the reports for a given group""" - db = self.env.get_db_cnx() + db = self.env.get_read_db() cursor = db.cursor() rv = {} @@ -156,5 +151,5 @@ def get_version_hash_by_group(self, group): """Gets all of the reports for a given group as a uuid=>version hash""" - db = self.env.get_db_cnx() + db = self.env.get_read_db() cursor = db.cursor() rv = {} @@ -173,22 +168,15 @@ # similar functions are found in dbhelper, but this file should be fairly # stand alone so that it can be copied and pasted around - def get_first_row(self, sql,*params): + def get_first_row(self, sql, *params): """ Returns the first row of the query results as a tuple of values (or None)""" - db = self.env.get_db_cnx() + db = self.env.get_read_db() cur = db.cursor() - data = None; try: cur.execute(sql, params) - data = cur.fetchone(); - db.commit(); + return cur.fetchone() except Exception, e: self.log.error('There was a problem executing sql:%s \n \ with parameters:%s\nException:%s'%(sql, params, e)); - db.rollback() - try: - db.close() - except: - pass - return data; + return None def get_scalar(self, sql, col=0, *params): @@ -201,21 +189,8 @@ def execute_in_trans(self, *args): - success = True - db = self.env.get_db_cnx() - cur = db.cursor() - try: + @self.env.with_transaction() + def do_execute(db): + cur = db.cursor() for sql, params in args: cur.execute(sql, params) - db.commit() - except Exception, e: - self.log.error('There was a problem executing sql:%s \n \ - with parameters:%s\nException:%s'%(sql, params, e)); - db.rollback(); - success = False - try: - db.close() - except: - pass - return success - - + return True