]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
hstores are text, and in py3k they seem to be implcitly unicode. so
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 29 May 2013 22:08:28 +0000 (18:08 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 29 May 2013 22:08:28 +0000 (18:08 -0400)
    add unicode encoding for py2k for the non-native hstore, pullreq for
    native psycopg2 support coming....

lib/sqlalchemy/dialects/postgresql/hstore.py
test/dialect/test_postgresql.py

index adfb82da76142e5a252ad91b78a92c53c2b43717..b0b13eb0edffcc3b6f20d77ae7e91240edc9e143 100644 (file)
@@ -262,19 +262,35 @@ class HSTORE(sqltypes.Concatenable, sqltypes.TypeEngine):
                 _adapt_expression(self, op, other_comparator)
 
     def bind_processor(self, dialect):
-        def process(value):
-            if isinstance(value, dict):
-                return _serialize_hstore(value)
-            else:
-                return value
+        if util.py2k:
+            encoding = dialect.encoding
+            def process(value):
+                if isinstance(value, dict):
+                    return _serialize_hstore(value).encode(encoding)
+                else:
+                    return value
+        else:
+            def process(value):
+                if isinstance(value, dict):
+                    return _serialize_hstore(value)
+                else:
+                    return value
         return process
 
     def result_processor(self, dialect, coltype):
-        def process(value):
-            if value is not None:
-                return _parse_hstore(value)
-            else:
-                return value
+        if util.py2k:
+            encoding = dialect.encoding
+            def process(value):
+                if value is not None:
+                    return _parse_hstore(value.decode(encoding))
+                else:
+                    return value
+        else:
+            def process(value):
+                if value is not None:
+                    return _parse_hstore(value)
+                else:
+                    return value
         return process
 
 
index 286628d5e4d6abcb9f4d3b1333adcc80cafe7458..3931a1968e1e6e6df494dd124869be22248b2dcb 100644 (file)
@@ -3207,3 +3207,28 @@ class HStoreRoundTripTest(fixtures.TablesTest):
     def test_fixed_round_trip_native(self):
         engine = testing.db
         self._test_fixed_round_trip(engine)
+
+    def _test_unicode_round_trip(self, engine):
+        s = select([
+                hstore(
+                    array([u'réveillé', u'drôle', u'S’il']),
+                    array([u'réveillé', u'drôle', u'S’il'])
+                )
+            ])
+        eq_(
+            engine.scalar(s),
+            {
+                u'réveillé': u'réveillé',
+                u'drôle': u'drôle',
+                u'S’il': u'S’il'
+            }
+        )
+
+    def test_unicode_round_trip_python(self):
+        engine = self._non_native_engine()
+        self._test_unicode_round_trip(engine)
+
+    @testing.only_on("postgresql+psycopg2")
+    def test_unicode_round_trip_native(self):
+        engine = testing.db
+        self._test_unicode_round_trip(engine)