From ed4838f6793fa51cd75b29014039943b9bfa153b Mon Sep 17 00:00:00 2001 From: King Chung Huang Date: Fri, 28 Jun 2024 15:48:06 -0600 Subject: [PATCH] Escape backslashes in string values --- target_postgres/sinks.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/target_postgres/sinks.py b/target_postgres/sinks.py index 0f1523bb..c59e312d 100644 --- a/target_postgres/sinks.py +++ b/target_postgres/sinks.py @@ -180,6 +180,12 @@ def bulk_insert_records( # type: ignore[override] ] # Make translation table for escaping in array values. + str_translate_table = str.maketrans( + { + '"': '""', + "\\": "\\\\", + } + ) array_translate_table = str.maketrans( { '"': '\\""', @@ -200,7 +206,7 @@ def process_column_value(data: Any, proc: Callable) -> str: # a quoted value. if isinstance(value, str): # escape double quotes as "". - return '"' + value.replace('"', '""') + '"' + return '"' + value.translate(str_translate_table) + '"' # If the value is a list (for ARRAY), escape double-quotes as \" and return # a quoted value in literal array format.