Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c273d9d

Browse files
committed
Rework tab completion of COPY and \copy in psql
This corrects and simplifies $subject in a number of ways: - Remove from the completion the pre-9.0 grammar still supported for compatibility purposes. This simplifies the code, and allows to extend it more easily with new patterns. - Add completion for the options of FORMAT within a WITH clause. - Complete WHERE and WITH clauses correctly depending on if TO or FROM are used, WHERE being only available with COPY FROM. Author: Vignesh C, Michael Paquier Reviewed-by: Ahsan Hadi Discussion: https://postgr.es/m/CALDaNm3zWr=OmxeNqOqfT=uZTSdam_j-gkX94CL8eTNfgUtf6A@mail.gmail.com
1 parent a4faef8 commit c273d9d

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

src/bin/psql/tab-complete.c

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,19 +2316,14 @@ psql_completion(const char *text, int start, int end)
23162316
else if (Matches("COPY|\\copy"))
23172317
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
23182318
" UNION ALL SELECT '('");
2319-
/* If we have COPY BINARY, complete with list of tables */
2320-
else if (Matches("COPY", "BINARY"))
2321-
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
2322-
/* If we have COPY (, complete it with legal commands */
2319+
/* Complete COPY ( with legal query commands */
23232320
else if (Matches("COPY|\\copy", "("))
23242321
COMPLETE_WITH("SELECT", "TABLE", "VALUES", "INSERT", "UPDATE", "DELETE", "WITH");
2325-
/* If we have COPY [BINARY] <sth>, complete it with "TO" or "FROM" */
2326-
else if (Matches("COPY|\\copy", MatchAny) ||
2327-
Matches("COPY", "BINARY", MatchAny))
2322+
/* Complete COPY <sth> */
2323+
else if (Matches("COPY|\\copy", MatchAny))
23282324
COMPLETE_WITH("FROM", "TO");
2329-
/* If we have COPY [BINARY] <sth> FROM|TO, complete with filename */
2330-
else if (Matches("COPY", MatchAny, "FROM|TO") ||
2331-
Matches("COPY", "BINARY", MatchAny, "FROM|TO"))
2325+
/* Complete COPY <sth> FROM|TO with filename */
2326+
else if (Matches("COPY", MatchAny, "FROM|TO"))
23322327
{
23332328
completion_charp = "";
23342329
completion_force_quote = true; /* COPY requires quoted filename */
@@ -2340,17 +2335,28 @@ psql_completion(const char *text, int start, int end)
23402335
completion_force_quote = false;
23412336
matches = rl_completion_matches(text, complete_from_files);
23422337
}
2343-
/* Offer options after COPY [BINARY] <sth> FROM|TO filename */
2344-
else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny) ||
2345-
Matches("COPY", "BINARY", MatchAny, "FROM|TO", MatchAny))
2346-
COMPLETE_WITH("BINARY", "DELIMITER", "NULL", "CSV",
2347-
"ENCODING");
2348-
2349-
/* Offer options after COPY [BINARY] <sth> FROM|TO filename CSV */
2350-
else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "CSV") ||
2351-
Matches("COPY", "BINARY", MatchAny, "FROM|TO", MatchAny, "CSV"))
2352-
COMPLETE_WITH("HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE",
2353-
"FORCE NOT NULL");
2338+
2339+
/* Complete COPY <sth> TO <sth> */
2340+
else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAny))
2341+
COMPLETE_WITH("WITH (");
2342+
2343+
/* Complete COPY <sth> FROM <sth> */
2344+
else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny))
2345+
COMPLETE_WITH("WITH (", "WHERE");
2346+
2347+
/* Complete COPY <sth> FROM|TO filename WITH ( */
2348+
else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "("))
2349+
COMPLETE_WITH("FORMAT", "FREEZE", "DELIMITER", "NULL",
2350+
"HEADER", "QUOTE", "ESCAPE", "FORCE_QUOTE",
2351+
"FORCE_NOT_NULL", "FORCE_NULL", "ENCODING");
2352+
2353+
/* Complete COPY <sth> FROM|TO filename WITH (FORMAT */
2354+
else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "FORMAT"))
2355+
COMPLETE_WITH("binary", "csv", "text");
2356+
2357+
/* Complete COPY <sth> FROM <sth> WITH (<options>) */
2358+
else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", MatchAny))
2359+
COMPLETE_WITH("WHERE");
23542360

23552361
/* CREATE ACCESS METHOD */
23562362
/* Complete "CREATE ACCESS METHOD <name>" */

0 commit comments

Comments
 (0)