diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-05-29 18:08:28 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-05-29 18:34:29 -0400 |
| commit | f87c9d8167606da7f0bcd5d8e3f2f96be38c7100 (patch) | |
| tree | 661b6c3865d9f09dc03686d1f33ee09a4fc82e7e | |
| parent | e86b7df96cae94ad277bc1a40df51c77c7b30f4a (diff) | |
| download | sqlalchemy-f87c9d8167606da7f0bcd5d8e3f2f96be38c7100.tar.gz | |
hstores are text, and in py3k they seem to be implcitly unicode. so
add unicode encoding for py2k for the non-native hstore, pullreq for
native psycopg2 support coming....
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/hstore.py | 36 | ||||
| -rw-r--r-- | test/dialect/test_postgresql.py | 25 |
2 files changed, 51 insertions, 10 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/hstore.py b/lib/sqlalchemy/dialects/postgresql/hstore.py index e555a1afd..b2150bc44 100644 --- a/lib/sqlalchemy/dialects/postgresql/hstore.py +++ b/lib/sqlalchemy/dialects/postgresql/hstore.py @@ -260,19 +260,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 diff --git a/test/dialect/test_postgresql.py b/test/dialect/test_postgresql.py index 4fd5bc9c1..86e10c06e 100644 --- a/test/dialect/test_postgresql.py +++ b/test/dialect/test_postgresql.py @@ -3206,3 +3206,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) |
