Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
---------
Expand Down
10 changes: 5 additions & 5 deletions pgcli/packages/formatter/sqlformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions tests/formatter/test_sqlformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading