From a90e4663bbde37a8642400eaa961a0aab6481cad Mon Sep 17 00:00:00 2001 From: Erik Erlandson Date: Wed, 20 Apr 2022 07:47:05 -0700 Subject: [PATCH] Fix 26 - escaping colons (#27) Signed-off-by: Erik Erlandson --- osc_ingest_trino/trino_utils.py | 3 +++ tests/test_trino_utils.py | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/osc_ingest_trino/trino_utils.py b/osc_ingest_trino/trino_utils.py index 5177672..088e7fe 100644 --- a/osc_ingest_trino/trino_utils.py +++ b/osc_ingest_trino/trino_utils.py @@ -97,6 +97,9 @@ def _sqlform(x): if isinstance(x, str): # escape any single quotes in the string t = x.replace("'", "''") + # colons are mostly a problem for ':some_id_name', which is interpreted as + # a parameter requiring binding, but just escaping them all works + t = t.replace(":", "\\:") # enclose string with single quotes return f"'{t}'" if isinstance(x, datetime): diff --git a/tests/test_trino_utils.py b/tests/test_trino_utils.py index 123218e..994a33c 100644 --- a/tests/test_trino_utils.py +++ b/tests/test_trino_utils.py @@ -33,7 +33,7 @@ def test_trino_batch_insert(): cxn = mock.MagicMock() # tuple data, in form supplied to __call__ as specified in 'method' param docs: # https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_sql.html - rows = [("a", 4.5), ("b'c", math.nan), (None, math.inf), ("d", -math.inf), ("e", datetime(2022, 1, 1))] + rows = [("a", 4.5), ("b'c", math.nan), (None, math.inf), ("d", -math.inf), ("e", datetime(2022, 1, 1)), (":f", 1.0)] # invoke the __call__ method, simulating df.to_sql call tbi = TrinoBatchInsert(catalog="test", schema="test", batch_size=2, verbose=True) tbi(tbl, cxn, [], rows) @@ -42,4 +42,7 @@ def test_trino_batch_insert(): xcalls = cxn.execute.call_args_list assert xcalls[0].args[0].text == "insert into test.test.test values\n('a', 4.5),\n('b''c', nan())" assert xcalls[1].args[0].text == "insert into test.test.test values\n(NULL, infinity()),\n('d', -infinity())" - assert xcalls[2].args[0].text == "insert into test.test.test values\n('e', TIMESTAMP '2022-01-01 00:00:00')" + assert ( + xcalls[2].args[0].text + == "insert into test.test.test values\n('e', TIMESTAMP '2022-01-01 00:00:00'),\n('\\:f', 1.0)" + )