diff --git a/changelog.rst b/changelog.rst index a1ade6bc2..b93a572fe 100644 --- a/changelog.rst +++ b/changelog.rst @@ -16,6 +16,8 @@ Bug fixes: created by ``uv venv``. * Read ``alias_map_file`` as UTF-8 (with or without a BOM) instead of the locale encoding, which made non-ASCII aliases fail on Windows. +* Double single quotes in values written by ``\T sql-insert`` and + ``sql-update``, so a value like ``O'Brien`` gives valid SQL. Internal: --------- diff --git a/pgcli/packages/formatter/sqlformatter.py b/pgcli/packages/formatter/sqlformatter.py index 6c4973db7..05a1204b5 100644 --- a/pgcli/packages/formatter/sqlformatter.py +++ b/pgcli/packages/formatter/sqlformatter.py @@ -20,7 +20,7 @@ def escape_for_sql_statement(value): if isinstance(value, bytes): return f"X'{value.hex()}'" - return "'{}'".format(value) + return "'{}'".format(str(value).replace("'", "''")) def adapter(data, headers, table_format=None, **kwargs): diff --git a/tests/formatter/test_sqlformatter.py b/tests/formatter/test_sqlformatter.py index 78ff5dc95..d7c027d19 100644 --- a/tests/formatter/test_sqlformatter.py +++ b/tests/formatter/test_sqlformatter.py @@ -24,6 +24,10 @@ def test_escape_for_sql_statement_str(): assert escaped_bytes == "'example str'" +def test_escape_for_sql_statement_str_with_quote(): + assert escape_for_sql_statement("O'Brien") == "'O''Brien'" + + def test_output_sql_insert(): global formatter formatter = TabularOutputFormatter