Skip to content

Commit 57b5d55

Browse files
angazvir-mir
authored andcommitted
Fix #189 hstore when using ReadDictCursor (#512)
* Fixed #189 * Added test
1 parent 514f878 commit 57b5d55

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

aiopg/connection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ def _enable_hstore(conn):
3939
""")
4040
rv0, rv1 = [], []
4141
for oids in (yield from cur.fetchall()):
42-
rv0.append(oids[0])
43-
rv1.append(oids[1])
42+
if isinstance(oids, dict):
43+
rv0.append(oids['oid'])
44+
rv1.append(oids['typarray'])
45+
else:
46+
rv0.append(oids[0])
47+
rv1.append(oids[1])
4448

4549
cur.close()
4650
return tuple(rv0), tuple(rv1)

tests/test_connection.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ def test_simple_select(connect):
5252
assert (1,) == ret
5353

5454

55+
@asyncio.coroutine
56+
def test_simple_select_with_hstore(connect):
57+
conn = yield from connect()
58+
cur = yield from conn.cursor()
59+
yield from cur.execute("""
60+
CREATE EXTENSION IF NOT EXISTS hstore;
61+
CREATE TABLE hfoo (id serial, hcol hstore);
62+
INSERT INTO hfoo (hcol) VALUES ('"col1"=>"456", "col2"=>"zzz"');
63+
""")
64+
65+
# Reconnect because this is where the problem happens.
66+
cur.close()
67+
conn.close()
68+
conn = yield from connect(cursor_factory=psycopg2.extras.RealDictCursor)
69+
cur = yield from conn.cursor()
70+
yield from cur.execute("SELECT * FROM hfoo;")
71+
ret = yield from cur.fetchone()
72+
yield from cur.execute("DROP TABLE hfoo;")
73+
assert {'hcol': {'col1': '456', 'col2': 'zzz'}, 'id': 1} == ret
74+
75+
5576
@asyncio.coroutine
5677
def test_default_event_loop(connect, loop):
5778
asyncio.set_event_loop(loop)

0 commit comments

Comments
 (0)