Skip to content

Commit f18b6b6

Browse files
Double single quotes in sql-insert and sql-update values
A value with an apostrophe, like O'Brien, was written as 'O'Brien', so the generated statements did not run.
1 parent 8b3b281 commit f18b6b6

3 files changed

Lines changed: 7 additions & 1 deletion

File tree

‎changelog.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Bug fixes:
1616
created by ``uv venv``.
1717
* Read ``alias_map_file`` as UTF-8 (with or without a BOM) instead of the
1818
locale encoding, which made non-ASCII aliases fail on Windows.
19+
* Double single quotes in values written by ``\T sql-insert`` and
20+
``sql-update``, so a value like ``O'Brien`` gives valid SQL.
1921

2022
Internal:
2123
---------

‎pgcli/packages/formatter/sqlformatter.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def escape_for_sql_statement(value):
2020
if isinstance(value, bytes):
2121
return f"X'{value.hex()}'"
2222

23-
return "'{}'".format(value)
23+
return "'{}'".format(str(value).replace("'", "''"))
2424

2525

2626
def adapter(data, headers, table_format=None, **kwargs):

‎tests/formatter/test_sqlformatter.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def test_escape_for_sql_statement_str():
2424
assert escaped_bytes == "'example str'"
2525

2626

27+
def test_escape_for_sql_statement_str_with_quote():
28+
assert escape_for_sql_statement("O'Brien") == "'O''Brien'"
29+
30+
2731
def test_output_sql_insert():
2832
global formatter
2933
formatter = TabularOutputFormatter

0 commit comments

Comments
 (0)