Author: cito
Date: Mon Feb  8 12:32:50 2016
New Revision: 840

Log:
Improve adaptation of hstore and empty array

Modified:
   trunk/pgdb.py
   trunk/tests/test_dbapi20.py

Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py       Mon Feb  8 11:00:17 2016        (r839)
+++ trunk/pgdb.py       Mon Feb  8 12:32:50 2016        (r840)
@@ -768,6 +768,8 @@
             # an array and not a string.  One issue with this syntax is that
             # you need to add an explicit typecast when passing empty arrays.
             # The ARRAY keyword is actually only necessary at the top level.
+            if not value:  # exception for empty array
+                return "'{}'"
             q = self._quote
             return 'ARRAY[%s]' % ','.join(str(q(v)) for v in value)
         if isinstance(value, tuple):
@@ -1585,6 +1587,7 @@
     """Wrapper class for marking hstore values."""
 
     _re_quote = regex('^[Nn][Uu][Ll][Ll]$|[ ,=>]')
+    _re_escape = regex(r'(["\\])')
 
     @classmethod
     def _quote(cls, s):
@@ -1592,8 +1595,9 @@
             return 'NULL'
         if not s:
             return '""'
-        s = s.replace('"', '\\"')
-        if cls._re_quote.search(s):
+        quote = cls._re_quote.search(s)
+        s = cls._re_escape.sub(r'\\\1', s)
+        if quote:
             s = '"%s"' % s
         return s
 

Modified: trunk/tests/test_dbapi20.py
==============================================================================
--- trunk/tests/test_dbapi20.py Mon Feb  8 11:00:17 2016        (r839)
+++ trunk/tests/test_dbapi20.py Mon Feb  8 12:32:50 2016        (r840)
@@ -627,7 +627,7 @@
                 self.skipTest("hstore extension not enabled")
         finally:
             con.close()
-        d = {'k': 'v', 'foo': 'bar', 'baz': 'whatever',
+        d = {'k': 'v', 'foo': 'bar', 'baz': 'whatever', 'back\\': '\\slash',
             '1a': 'anything at all', '2=b': 'value = 2', '3>c': 'value > 3',
             '4"c': 'value " 4', "5'c": "value ' 5", 'hello, world': '"hi!"',
             'None': None, 'NULL': 'NULL', 'empty': ''}
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to