Skip to content

Commit

Permalink
Escape backslashes in string values
Browse files Browse the repository at this point in the history
  • Loading branch information
kinghuang committed Jun 28, 2024
1 parent 534a38a commit ed4838f
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion target_postgres/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
'"': '\\""',
Expand All @@ -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.
Expand Down

0 comments on commit ed4838f

Please sign in to comment.