From f18b6b6ac14945ce09d5eee4e777b28570847a89 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:04:06 +0530 Subject: [PATCH] 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. --- changelog.rst | 2 ++ pgcli/packages/formatter/sqlformatter.py | 2 +- tests/formatter/test_sqlformatter.py | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) 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