Source

bloodhound-mq / t288 / t288_r1438538_sql_translate_global_env_pending.diff

Full commit
# HG changeset patch
# Parent a103a7667c1345b7392b55ee894d699a67c4a7a5
BH Multiproduct #288 : SQL translate global environment (pending)

diff -r a103a7667c13 bloodhound_multiproduct/multiproduct/api.py
--- a/bloodhound_multiproduct/multiproduct/api.py	Tue Jan 29 19:47:18 2013 -0500
+++ b/bloodhound_multiproduct/multiproduct/api.py	Tue Jan 29 22:46:55 2013 -0500
@@ -97,7 +97,7 @@
         self.log.debug("upgrading existing environment for %s plugin." % 
                        PLUGIN_NAME)
         db_installed_version = self.get_version()
-        with self.env.db_transaction as db:
+        with self.env.db_direct_transaction as db:
             if db_installed_version < 1:
                 # Initial installation
                 db("ALTER TABLE ticket ADD COLUMN product TEXT")
diff -r a103a7667c13 bloodhound_multiproduct/multiproduct/dbcursor.py
--- a/bloodhound_multiproduct/multiproduct/dbcursor.py	Tue Jan 29 19:47:18 2013 -0500
+++ b/bloodhound_multiproduct/multiproduct/dbcursor.py	Tue Jan 29 22:46:55 2013 -0500
@@ -37,18 +37,25 @@
 PRODUCT_COLUMN = 'product'
 DEFAULT_PRODUCT = ''
 
+# Singleton used to mark products as unset
+class empty_product(object):
+    pass
+
+empty_product = empty_product()
+
 class BloodhoundIterableCursor(trac.db.util.IterableCursor):
-    __slots__ = trac.db.util.IterableCursor.__slots__ + ['_translator']
+    __slots__ = trac.db.util.IterableCursor.__slots__ + ['_translator', '_product_prefix']
     _tls = concurrency.ThreadLocal(env=None)
 
     def __init__(self, cursor, log=None):
         super(BloodhoundIterableCursor, self).__init__(cursor, log=log)
         self._translator = None
+        self._product_prefix = empty_product
 
     @property
     def translator(self):
         if not self._translator:
-            product_prefix = self.env.product.prefix if (self.env and self.env.product) else DEFAULT_PRODUCT
+            product_prefix = self._product_prefix
             self._translator = BloodhoundProductSQLTranslate(SKIP_TABLES,
                                                              TRANSLATE_TABLES,
                                                              PRODUCT_COLUMN,
@@ -56,7 +63,14 @@
         return self._translator
 
     def _translate_sql(self, sql):
-        return self.translator.translate(sql) if (self.env and self.env.product) else sql
+        if self._product_prefix is empty_product:
+            if self.env is None:
+                self._product_prefix = None
+            else:
+                self._product_prefix = self.env.product.prefix \
+                        if self.env.product else DEFAULT_PRODUCT
+        return self.translator.translate(sql) \
+                if self._product_prefix is not None else sql
 
     def execute(self, sql, args=None):
         return super(BloodhoundIterableCursor, self).execute(self._translate_sql(sql), args=args)