Commits

Anonymous committed 6a0b400

0.12.3dev: Improves `quote()` method of each database adapter. It should be able to specify a keyword including double-quote or grave accent.

Comments (0)

Files changed (4)

trac/db/mysql_backend.py

 
     def quote(self, identifier):
         """Return the quoted identifier."""
-        return "`%s`" % identifier
+        return "`%s`" % identifier.replace('`', '``')
 
     def get_last_id(self, cursor, table, column='id'):
         return cursor.lastrowid

trac/db/postgres_backend.py

 
     def quote(self, identifier):
         """Return the quoted identifier."""
-        return '"%s"' % identifier
+        return '"%s"' % identifier.replace('"', '""')
 
     def get_last_id(self, cursor, table, column='id'):
         cursor.execute("SELECT CURRVAL('%s_%s_seq')" % (table, column))

trac/db/sqlite_backend.py

 
     def quote(self, identifier):
         """Return the quoted identifier."""
-        return "`%s`" % identifier
+        return "`%s`" % identifier.replace('`', '``')
 
     def get_last_id(self, cursor, table, column='id'):
         return cursor.lastrowid

trac/db/tests/api.py

 import os
 import unittest
 
-from trac.db.api import _parse_db_str, with_transaction
+from trac.db.api import _parse_db_str, with_transaction, get_column_names
 from trac.test import EnvironmentStub, Mock
 
 
         cursor.execute("SELECT value FROM system WHERE name='test-markup'")
         self.assertEqual([(u'<em>märkup</em>',)], cursor.fetchall())
 
+    def test_quote(self):
+        db = self.env.get_db_cnx()
+        cursor = db.cursor()
+        cursor.execute('SELECT 1 AS %s' % \
+                       db.quote(r'alpha\`\"\'\\beta``gamma""delta'))
+        self.assertEqual(r'alpha\`\"\'\\beta``gamma""delta',
+                         get_column_names(cursor)[0])
+
 
 class ConnectionTestCase(unittest.TestCase):
     def setUp(self):