Skip to content

Commit e3584e4

Browse files
Fix tab-completion for COPY and \copy options.
Commit c273d9d reworked tab-completion of COPY and \copy in psql and added support for completing options within WITH clauses. However, the same COPY options were suggested for both COPY TO and COPY FROM commands, even though some options are only valid for one or the other. This commit separates the COPY options for COPY FROM and COPY TO commands to provide more accurate auto-completion suggestions. Back-patch to v14 where tab-completion for COPY and \copy options within WITH clauses was first supported. Author: Atsushi Torikoshi <torikoshia@oss.nttdata.com> Reviewed-by: Yugo Nagata <nagata@sraoss.co.jp> Discussion: https://postgr.es/m/079e7a2c801f252ae8d522b772790ed7@oss.nttdata.com Backpatch-through: 14
1 parent c65c36a commit e3584e4

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/bin/psql/tab-complete.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,18 @@ static const SchemaQuery Query_for_trigger_of_table = {
11431143
" FROM pg_catalog.pg_timezone_names() "\
11441144
" WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.lower('%s')"
11451145

1146+
/* COPY options shared between FROM and TO */
1147+
#define Copy_common_options \
1148+
"DELIMITER", "ENCODING", "ESCAPE", "FORMAT", "HEADER", "NULL", "QUOTE"
1149+
1150+
/* COPY FROM options */
1151+
#define Copy_from_options \
1152+
Copy_common_options, "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE"
1153+
1154+
/* COPY TO options */
1155+
#define Copy_to_options \
1156+
Copy_common_options, "FORCE_QUOTE"
1157+
11461158
/*
11471159
* These object types were introduced later than our support cutoff of
11481160
* server version 9.2. We use the VersionedQuery infrastructure so that
@@ -2738,11 +2750,13 @@ psql_completion(const char *text, int start, int end)
27382750
else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny))
27392751
COMPLETE_WITH("WITH (", "WHERE");
27402752

2741-
/* Complete COPY <sth> FROM|TO filename WITH ( */
2742-
else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "("))
2743-
COMPLETE_WITH("FORMAT", "FREEZE", "DELIMITER", "NULL",
2744-
"HEADER", "QUOTE", "ESCAPE", "FORCE_QUOTE",
2745-
"FORCE_NOT_NULL", "FORCE_NULL", "ENCODING");
2753+
/* Complete COPY <sth> FROM filename WITH ( */
2754+
else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "("))
2755+
COMPLETE_WITH(Copy_from_options);
2756+
2757+
/* Complete COPY <sth> TO filename WITH ( */
2758+
else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAny, "WITH", "("))
2759+
COMPLETE_WITH(Copy_to_options);
27462760

27472761
/* Complete COPY <sth> FROM|TO filename WITH (FORMAT */
27482762
else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "FORMAT"))

0 commit comments

Comments
 (0)