You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bloodhound.apache.org by ma...@apache.org on 2013/09/03 10:35:33 UTC

svn commit: r1519598 - in /bloodhound/trunk/bloodhound_multiproduct: multiproduct/api.py multiproduct/dbcursor.py multiproduct/util.py tests/db/cursor.py

Author: matevz
Date: Tue Sep  3 08:35:33 2013
New Revision: 1519598

URL: http://svn.apache.org/r1519598
Log:
fixes for MySQL issues (multiple autoincr fields)

Modified:
    bloodhound/trunk/bloodhound_multiproduct/multiproduct/api.py
    bloodhound/trunk/bloodhound_multiproduct/multiproduct/dbcursor.py
    bloodhound/trunk/bloodhound_multiproduct/multiproduct/util.py
    bloodhound/trunk/bloodhound_multiproduct/tests/db/cursor.py

Modified: bloodhound/trunk/bloodhound_multiproduct/multiproduct/api.py
URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_multiproduct/multiproduct/api.py?rev=1519598&r1=1519597&r2=1519598&view=diff
==============================================================================
--- bloodhound/trunk/bloodhound_multiproduct/multiproduct/api.py (original)
+++ bloodhound/trunk/bloodhound_multiproduct/multiproduct/api.py Tue Sep  3 08:35:33 2013
@@ -46,7 +46,7 @@ from trac.wiki.parser import WikiParser
 from multiproduct.dbcursor import GLOBAL_PRODUCT
 from multiproduct.model import Product, ProductResourceMap, ProductSetting
 from multiproduct.util import EmbeddedLinkFormatter, IDENTIFIER, \
-    using_sqlite_backend
+    using_sqlite_backend, using_mysql_backend
 
 __all__ = ['MultiProductSystem', 'PRODUCT_SYNTAX_DELIMITER']
 
@@ -533,8 +533,8 @@ class MultiProductSystem(Component):
 
         def add_new_id_column(table):
             id_column = Column('id', type='int', auto_increment=True)
-            if using_sqlite_backend(self.env):
-                # sqlite does not support multiple auto increment columns
+            if using_sqlite_backend(self.env) or using_mysql_backend(self.env):
+                # sqlite and mysql don't support multiple auto increment columns
                 id_column.auto_increment = False
             table.columns.append(id_column)
             table.indices.append(Index(['product', 'id'], unique=True))

Modified: bloodhound/trunk/bloodhound_multiproduct/multiproduct/dbcursor.py
URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_multiproduct/multiproduct/dbcursor.py?rev=1519598&r1=1519597&r2=1519598&view=diff
==============================================================================
--- bloodhound/trunk/bloodhound_multiproduct/multiproduct/dbcursor.py (original)
+++ bloodhound/trunk/bloodhound_multiproduct/multiproduct/dbcursor.py Tue Sep  3 08:35:33 2013
@@ -24,7 +24,7 @@ import sqlparse.tokens as Tokens
 import sqlparse.sql as Types
 
 from multiproduct.cache import lru_cache
-from multiproduct.util import using_sqlite_backend
+from multiproduct.util import using_sqlite_backend, using_mysql_backend
 
 __all__ = ['BloodhoundIterableCursor', 'BloodhoundConnectionWrapper', 'ProductEnvContextManager']
 
@@ -209,7 +209,8 @@ class BloodhoundProductSQLTranslate(obje
         self._translate_tables = translate_tables
         self._product_column = product_column
         self._product_prefix = product_prefix
-        self._using_sqlite = env is None or using_sqlite_backend(env)
+        self._id_calculated = env is None or using_sqlite_backend(env) \
+            or using_mysql_backend(env)
 
     def _sqlparse_underline_hack(self, token):
         underline_token = lambda token: token.ttype == Tokens.Token.Error and token.value == '_'
@@ -559,7 +560,7 @@ class BloodhoundProductSQLTranslate(obje
                 columns_to_insert = []
                 if not 'product' in columns_present:
                     columns_to_insert += [',', ' ', self._product_column]
-                if self._using_sqlite \
+                if self._id_calculated \
                    and tablename == 'ticket'\
                    and not 'id' in columns_present:
                     columns_to_insert += [',', ' ', 'id']
@@ -573,12 +574,13 @@ class BloodhoundProductSQLTranslate(obje
                 values_to_insert = []
                 if not 'product' in columns_present:
                     values_to_insert += [',', "'", self._product_prefix, "'"]
-                if self._using_sqlite \
+                if self._id_calculated \
                    and tablename == 'ticket' \
                    and not 'id' in columns_present:
                     values_to_insert += [
-                        ',', """COALESCE((SELECT MAX(id) FROM ticket
-                                           WHERE product='%s'), 0)+1""" %
+                        ',', """COALESCE((SELECT MAX(id) FROM
+                                           (SELECT * FROM ticket WHERE product='%s')
+                                           AS subquery), 0)+1""" %
                              (self._product_prefix,)
                     ]
                 for keyword in values_to_insert:

Modified: bloodhound/trunk/bloodhound_multiproduct/multiproduct/util.py
URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_multiproduct/multiproduct/util.py?rev=1519598&r1=1519597&r2=1519598&view=diff
==============================================================================
--- bloodhound/trunk/bloodhound_multiproduct/multiproduct/util.py (original)
+++ bloodhound/trunk/bloodhound_multiproduct/multiproduct/util.py Tue Sep  3 08:35:33 2013
@@ -141,11 +141,17 @@ class EmbeddedLinkFormatter(LinkFormatte
         return self._make_lhref_link(match, fullmatch, rel, ns, target, label)
 
 
-def using_sqlite_backend(env):
+def get_db_connector_from_uri(env):
     connector, arg = trac.db.api._parse_db_str(
         trac.db.api.DatabaseManager(env).connection_uri
     )
-    return connector == 'sqlite'
+    return connector
+    
+def using_sqlite_backend(env):
+    return get_db_connector_from_uri(env) == 'sqlite'
+
+def using_mysql_backend(env):
+    return get_db_connector_from_uri(env) == 'mysql'
 
 #----------------------
 # Useful regex

Modified: bloodhound/trunk/bloodhound_multiproduct/tests/db/cursor.py
URL: http://svn.apache.org/viewvc/bloodhound/trunk/bloodhound_multiproduct/tests/db/cursor.py?rev=1519598&r1=1519597&r2=1519598&view=diff
==============================================================================
--- bloodhound/trunk/bloodhound_multiproduct/tests/db/cursor.py (original)
+++ bloodhound/trunk/bloodhound_multiproduct/tests/db/cursor.py Tue Sep  3 08:35:33 2013
@@ -992,7 +992,7 @@ data = {
     'insert_with_product': [
         (
 """INSERT INTO ticket (summary, product) VALUES ('S', 'swlcu')""",
-"""INSERT INTO ticket (summary, product, id) VALUES ('S', 'swlcu',COALESCE((SELECT MAX(id) FROM ticket\nWHERE product='PRODUCT'), 0)+1)"""
+"""INSERT INTO ticket (summary, product, id) VALUES ('S', 'swlcu',COALESCE((SELECT MAX(id) FROM\n(SELECT * FROM ticket WHERE product='PRODUCT')\nAS subquery), 0)+1)"""
         ),
     ],