Author: cito
Date: Mon Feb  8 16:02:10 2016
New Revision: 842

Log:
Treat percent signs in SQL strings always the same

Modified:
   trunk/docs/contents/changelog.rst
   trunk/pgdb.py
   trunk/tests/test_dbapi20.py

Modified: trunk/docs/contents/changelog.rst
==============================================================================
--- trunk/docs/contents/changelog.rst   Mon Feb  8 15:05:37 2016        (r841)
+++ trunk/docs/contents/changelog.rst   Mon Feb  8 16:02:10 2016        (r842)
@@ -92,6 +92,9 @@
       a new fast built-in parser to achieve this. Anonymous composite types are
       also supported, but yield only an ordinary tuple containing text strings.
     - New type helpers Interval() and Uuid() have been added.
+    - SQL commands are always handled as if they include parameters, i.e.
+      literal percent signs must always be doubled. This consistent behavior
+      is necessary for using pgdb with wrappers like SQLAlchemy.
 - Changes concerning both modules:
     - PyGreSQL now tries to raise more specific and appropriate subclasses of
       DatabaseError than just ProgrammingError. Particularly, when database

Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py       Mon Feb  8 15:05:37 2016        (r841)
+++ trunk/pgdb.py       Mon Feb  8 16:02:10 2016        (r842)
@@ -795,12 +795,18 @@
         """Quote parameters.
 
         This function works for both mappings and sequences.
+
+        The function should be used even when there are no parameters,
+        so that we have a consistent behavior regarding percent signs.
         """
-        if isinstance(parameters, dict):
-            parameters = _quotedict(parameters)
-            parameters.quote = self._quote
+        if parameters:
+            if isinstance(parameters, dict):
+                parameters = _quotedict(parameters)
+                parameters.quote = self._quote
+            else:
+                parameters = tuple(map(self._quote, parameters))
         else:
-            parameters = tuple(map(self._quote, parameters))
+            parameters = {}
         return string % parameters
 
     def _make_description(self, info):
@@ -884,8 +890,7 @@
                 self._dbcnx._tnx = True
             for parameters in seq_of_parameters:
                 sql = operation
-                if parameters:
-                    sql = self._quoteparams(sql, parameters)
+                sql = self._quoteparams(sql, parameters)
                 rows = self._src.execute(sql)
                 if rows:  # true if not DML
                     rowcount += rows

Modified: trunk/tests/test_dbapi20.py
==============================================================================
--- trunk/tests/test_dbapi20.py Mon Feb  8 15:05:37 2016        (r841)
+++ trunk/tests/test_dbapi20.py Mon Feb  8 16:02:10 2016        (r842)
@@ -90,6 +90,14 @@
         self.assertIn('.', v)
         self.assertEqual(pgdb.__version__, v)
 
+    def test_percent_sign(self):
+        con = self._connect()
+        cur = con.cursor()
+        cur.execute("select %s, 'a %% sign'", ('a % sign',))
+        self.assertEqual(cur.fetchone(), ('a % sign', 'a % sign'))
+        cur.execute("select 'a %% sign'")
+        self.assertEqual(cur.fetchone(), ('a % sign',))
+
     def test_callproc_no_params(self):
         con = self._connect()
         cur = con.cursor()
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to