Skip to content

Commit b609e27

Browse files
committed
Escape backslashes in array values
1 parent e1b2e85 commit b609e27

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

target_postgres/sinks.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ def bulk_insert_records( # type: ignore[override]
179179
column.type.bind_processor(connection.dialect) or str for column in columns
180180
]
181181

182+
# Make translation table for escaping in array values.
183+
array_translate_table = str.maketrans(
184+
{
185+
'"': '\\""',
186+
"\\": "\\\\",
187+
}
188+
)
189+
182190
def process_column_value(data: Any, proc: Callable) -> str:
183191
# If the data is null, return an unquoted, empty value.
184192
# Unquoted is important here, for PostgreSQL to interpret as null.
@@ -200,7 +208,9 @@ def process_column_value(data: Any, proc: Callable) -> str:
200208
# for each member of value, escape double quotes as \".
201209
return (
202210
'"{'
203-
+ ",".join('""' + v.replace('"', r'\""') + '""' for v in value)
211+
+ ",".join(
212+
'""' + v.translate(array_translate_table) + '""' for v in value
213+
)
204214
+ '}"'
205215
)
206216

0 commit comments

Comments
 (0)