Skip to content

Commit c1c6169

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 f2e41d9 commit c1c6169

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/bin/psql/tab-complete.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,19 @@ Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
11691169
Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
11701170
"STRICT", "SUPPORT"
11711171

1172+
/* COPY options shared between FROM and TO */
1173+
#define Copy_common_options \
1174+
"DELIMITER", "ENCODING", "ESCAPE", "FORMAT", "HEADER", "NULL", "QUOTE"
1175+
1176+
/* COPY FROM options */
1177+
#define Copy_from_options \
1178+
Copy_common_options, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
1179+
"LOG_VERBOSITY", "ON_ERROR"
1180+
1181+
/* COPY TO options */
1182+
#define Copy_to_options \
1183+
Copy_common_options, "FORCE_QUOTE"
1184+
11721185
/*
11731186
* These object types were introduced later than our support cutoff of
11741187
* server version 9.2. We use the VersionedQuery infrastructure so that
@@ -2899,23 +2912,24 @@ psql_completion(const char *text, int start, int end)
28992912
else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny))
29002913
COMPLETE_WITH("WITH (", "WHERE");
29012914

2902-
/* Complete COPY <sth> FROM|TO filename WITH ( */
2903-
else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "("))
2904-
COMPLETE_WITH("FORMAT", "FREEZE", "DELIMITER", "NULL",
2905-
"HEADER", "QUOTE", "ESCAPE", "FORCE_QUOTE",
2906-
"FORCE_NOT_NULL", "FORCE_NULL", "ENCODING", "DEFAULT",
2907-
"ON_ERROR", "LOG_VERBOSITY");
2915+
/* Complete COPY <sth> FROM filename WITH ( */
2916+
else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "("))
2917+
COMPLETE_WITH(Copy_from_options);
2918+
2919+
/* Complete COPY <sth> TO filename WITH ( */
2920+
else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAny, "WITH", "("))
2921+
COMPLETE_WITH(Copy_to_options);
29082922

29092923
/* Complete COPY <sth> FROM|TO filename WITH (FORMAT */
29102924
else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "FORMAT"))
29112925
COMPLETE_WITH("binary", "csv", "text");
29122926

29132927
/* Complete COPY <sth> FROM filename WITH (ON_ERROR */
2914-
else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "ON_ERROR"))
2928+
else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "ON_ERROR"))
29152929
COMPLETE_WITH("stop", "ignore");
29162930

29172931
/* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
2918-
else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
2932+
else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAny, "WITH", "(", "LOG_VERBOSITY"))
29192933
COMPLETE_WITH("default", "verbose");
29202934

29212935
/* Complete COPY <sth> FROM <sth> WITH (<options>) */

0 commit comments

Comments
 (0)