diff --git a/changelog.rst b/changelog.rst index fc362092b..6175277ef 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. +* Treat table names in a comma-separated ``FROM`` list the same way as a single + table name, so upper-case names are folded like they are for one table. Internal: --------- diff --git a/pgcli/packages/parseutils/tables.py b/pgcli/packages/parseutils/tables.py index 4c1c71a1f..45d0f8116 100644 --- a/pgcli/packages/parseutils/tables.py +++ b/pgcli/packages/parseutils/tables.py @@ -106,13 +106,12 @@ def parse_identifier(item): # Sometimes Keywords (such as FROM ) are classified as # identifiers which don't have the get_real_name() method. try: - schema_name = identifier.get_parent_name() - real_name = identifier.get_real_name() + schema_name, real_name, alias = parse_identifier(identifier) is_function = allow_functions and _identifier_is_function(identifier) except AttributeError: continue if real_name: - yield TableReference(schema_name, real_name, identifier.get_alias(), is_function) + yield TableReference(schema_name, real_name, alias, is_function) elif isinstance(item, Identifier): schema_name, real_name, alias = parse_identifier(item) is_function = allow_functions and _identifier_is_function(item) diff --git a/tests/parseutils/test_parseutils.py b/tests/parseutils/test_parseutils.py index d1221f86e..598fc5542 100644 --- a/tests/parseutils/test_parseutils.py +++ b/tests/parseutils/test_parseutils.py @@ -325,3 +325,27 @@ def test_is_destructive_unconditional_update_string_literal(sql, expected): ) def test_parse_destructive_warning(warning_level, expected): assert parse_destructive_warning(warning_level) == expected + + +@pytest.mark.parametrize('schema', ['', 'PUBLIC.']) +def test_extract_tables_comma_list_matches_standalone_case_folding(schema): + first = schema + 'USERS' + second = schema + 'ORDERS' + assert extract_tables(f'SELECT * FROM {first}, {second}') == ( + extract_tables(f'SELECT * FROM {first}') + extract_tables(f'SELECT * FROM {second}') + ) + + +@pytest.mark.parametrize( + "identifier", + [ + '"public"."users"', + '"PUBLIC"."USERS"', + '"Foo"', + 'users "Alias"', + 'users "alias"', + '"users"', + ], +) +def test_comma_list_quoted_identifier_matches_standalone(identifier): + assert extract_tables(f"SELECT * FROM {identifier}, orders")[0] == extract_tables(f"SELECT * FROM {identifier}")[0] diff --git a/tests/test_smart_completion_public_schema_only.py b/tests/test_smart_completion_public_schema_only.py index d607dfae0..0ffa47126 100644 --- a/tests/test_smart_completion_public_schema_only.py +++ b/tests/test_smart_completion_public_schema_only.py @@ -1021,3 +1021,24 @@ def test_special_name_completion(completer): display_meta="Toggle timing of commands.", ) ]) + + +@parametrize("completer", completers(casing=False, qualify=no_qual)) +@parametrize( + "tables", + [ + ("USERS", "ORDERS"), + ('"PUBLIC"."USERS"', "ORDERS"), + ('"Foo"', "orders"), + ('"users"', 'orders'), + ('"Users"', 'orders'), + ('"public"."users"', '"public".orders'), + ('users "Alias"', 'orders "Other"'), + ('users "alias"', 'orders "other"'), + ], +) +def test_comma_list_preserves_identifier_column_completions(completer, tables): + first, second = tables + comma = get_result(completer, f"SELECT from {first}, {second}", len("SELECT ")) + joined = get_result(completer, f"SELECT from {first} CROSS JOIN {second}", len("SELECT ")) + assert completions_to_set(comma) == completions_to_set(joined)