Skip to content

Commit 48d05f2

Browse files
authored
Merge pull request #59 from drdee/escape-hstore-array
Escape string in hstore value
2 parents 61f1b78 + 8931d75 commit 48d05f2

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

tap_postgres/sync_strategies/logical_replication.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import tap_postgres.sync_strategies.common as sync_common
1111
from dateutil.parser import parse
1212
import psycopg2
13+
from psycopg2 import sql
1314
import copy
1415
from select import select
1516
from functools import reduce
@@ -68,11 +69,14 @@ def tuples_to_map(accum, t):
6869
accum[t[0]] = t[1]
6970
return accum
7071

72+
def create_hstore_elem_query(elem):
73+
return sql.SQL("SELECT hstore_to_array({})").format(sql.Literal(elem))
74+
7175
def create_hstore_elem(conn_info, elem):
7276
with post_db.open_connection(conn_info) as conn:
7377
with conn.cursor() as cur:
74-
sql = """SELECT hstore_to_array('{}')""".format(elem)
75-
cur.execute(sql)
78+
query = create_hstore_elem_query(elem)
79+
cur.execute(query)
7680
res = cur.fetchone()[0]
7781
hstore_elem = reduce(tuples_to_map, [res[i:i + 2] for i in range(0, len(res), 2)], {})
7882
return hstore_elem

tests/test_discovery.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,16 @@ def test_catalog(self):
365365
'definitions' : tap_postgres.BASE_RECURSIVE_SCHEMAS},
366366
stream_dict.get('schema'))
367367

368+
def test_escaping_values(self):
369+
key = 'nickname'
370+
value = "Dave's Courtyard"
371+
elem = '"{}"=>"{}"'.format(key, value)
372+
373+
with get_test_connection() as conn:
374+
with conn.cursor() as cur:
375+
query = tap_postgres.sync_strategies.logical_replication.create_hstore_elem_query(elem)
376+
self.assertEqual(query.as_string(cur), "SELECT hstore_to_array('\"nickname\"=>\"Dave''s Courtyard\"')")
377+
368378

369379
class TestEnumTable(unittest.TestCase):
370380
maxDiff = None

0 commit comments

Comments
 (0)