From c0981b3230d1e7c5a6fd5885d4eea45bf17363dc Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:05:03 +0530 Subject: [PATCH] Quote schema and table separately in sql-insert and sql-update output The table name was built as schema.table and then quoted once, so a query on public.people gave INSERT INTO "public.people", a table that does not exist. --- changelog.rst | 2 ++ pgcli/packages/formatter/sqlformatter.py | 10 +++++----- tests/formatter/test_sqlformatter.py | 7 +++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/changelog.rst b/changelog.rst index a1ade6bc2..5aefe49bc 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. +* Quote the schema and the table separately in ``\T sql-insert`` and + ``sql-update`` output, so ``public.people`` gives ``"public"."people"``. Internal: --------- diff --git a/pgcli/packages/formatter/sqlformatter.py b/pgcli/packages/formatter/sqlformatter.py index 6c4973db7..4359974a5 100644 --- a/pgcli/packages/formatter/sqlformatter.py +++ b/pgcli/packages/formatter/sqlformatter.py @@ -28,14 +28,14 @@ def adapter(data, headers, table_format=None, **kwargs): if len(tables) > 0: table = tables[0] if table[0]: - table_name = "{}.{}".format(*table[:2]) + table_name = '"{}"."{}"'.format(*table[:2]) else: - table_name = table[1] + table_name = '"{}"'.format(table[1]) else: - table_name = "DUAL" + table_name = '"DUAL"' if table_format == "sql-insert": h = '", "'.join(headers) - yield 'INSERT INTO "{}" ("{}") VALUES'.format(table_name, h) + yield 'INSERT INTO {} ("{}") VALUES'.format(table_name, h) prefix = " " for d in data: values = ", ".join(escape_for_sql_statement(v) for i, v in enumerate(d)) @@ -49,7 +49,7 @@ def adapter(data, headers, table_format=None, **kwargs): if len(s) > 2: keys = int(s[-1]) for d in data: - yield 'UPDATE "{}" SET'.format(table_name) + yield "UPDATE {} SET".format(table_name) prefix = " " for i, v in enumerate(d[keys:], keys): yield '{}"{}" = {}'.format(prefix, headers[i], escape_for_sql_statement(v)) diff --git a/tests/formatter/test_sqlformatter.py b/tests/formatter/test_sqlformatter.py index 78ff5dc95..a0e571fe0 100644 --- a/tests/formatter/test_sqlformatter.py +++ b/tests/formatter/test_sqlformatter.py @@ -65,6 +65,13 @@ def test_output_sql_insert(): assert expected == output_list +def test_output_sql_insert_quotes_schema_and_table_apart(): + register_new_formatter(TabularOutputFormatter) + TabularOutputFormatter.query = "SELECT id FROM public.people;" + output = list(adapter([[1]], ["id"], table_format="sql-insert")) + assert output[0] == 'INSERT INTO "public"."people" ("id") VALUES' + + def test_output_sql_update(): global formatter formatter = TabularOutputFormatter