Branch data Line data Source code
1 : : /*
2 : : * psql - the PostgreSQL interactive terminal
3 : : *
4 : : * Copyright (c) 2000-2026, PostgreSQL Global Development Group
5 : : *
6 : : * src/bin/psql/tab-complete.in.c
7 : : *
8 : : * Note: this will compile and work as-is if SWITCH_CONVERSION_APPLIED
9 : : * is not defined. However, the expected usage is that it's first run
10 : : * through gen_tabcomplete.pl, which will #define that symbol, fill in the
11 : : * tcpatterns[] array, and convert the else-if chain in match_previous_words()
12 : : * into a switch. See comments for match_previous_words() and the header
13 : : * comment in gen_tabcomplete.pl for more detail.
14 : : */
15 : :
16 : : /*----------------------------------------------------------------------
17 : : * This file implements a somewhat more sophisticated readline "TAB
18 : : * completion" in psql. It is not intended to be AI, to replace
19 : : * learning SQL, or to relieve you from thinking about what you're
20 : : * doing. Also it does not always give you all the syntactically legal
21 : : * completions, only those that are the most common or the ones that
22 : : * the programmer felt most like implementing.
23 : : *
24 : : * CAVEAT: Tab completion causes queries to be sent to the backend.
25 : : * The number of tuples returned gets limited, in most default
26 : : * installations to 1000, but if you still don't like this prospect,
27 : : * you can turn off tab completion in your ~/.inputrc (or else
28 : : * ${INPUTRC}) file so:
29 : : *
30 : : * $if psql
31 : : * set disable-completion on
32 : : * $endif
33 : : *
34 : : * See `man 3 readline' or `info readline' for the full details.
35 : : *
36 : : * BUGS:
37 : : * - Quotes, parentheses, and other funny characters are not handled
38 : : * all that gracefully.
39 : : *----------------------------------------------------------------------
40 : : */
41 : :
42 : : #include "postgres_fe.h"
43 : :
44 : : #include "input.h"
45 : : #include "tab-complete.h"
46 : :
47 : : /* If we don't have this, we might as well forget about the whole thing: */
48 : : #ifdef USE_READLINE
49 : :
50 : : #include <ctype.h>
51 : : #include <sys/stat.h>
52 : :
53 : : #include "catalog/pg_am_d.h"
54 : : #include "catalog/pg_class_d.h"
55 : : #include "common.h"
56 : : #include "common/keywords.h"
57 : : #include "libpq-fe.h"
58 : : #include "mb/pg_wchar.h"
59 : : #include "pqexpbuffer.h"
60 : : #include "settings.h"
61 : : #include "stringutils.h"
62 : :
63 : : /*
64 : : * Ancient versions of libedit provide filename_completion_function()
65 : : * instead of rl_filename_completion_function(). Likewise for
66 : : * [rl_]completion_matches().
67 : : */
68 : : #ifndef HAVE_RL_FILENAME_COMPLETION_FUNCTION
69 : : #define rl_filename_completion_function filename_completion_function
70 : : #endif
71 : :
72 : : #ifndef HAVE_RL_COMPLETION_MATCHES
73 : : #define rl_completion_matches completion_matches
74 : : #endif
75 : :
76 : : /*
77 : : * Currently we assume that rl_filename_dequoting_function exists if
78 : : * rl_filename_quoting_function does. If that proves not to be the case,
79 : : * we'd need to test for the former, or possibly both, in configure.
80 : : */
81 : : #ifdef HAVE_RL_FILENAME_QUOTING_FUNCTION
82 : : #define USE_FILENAME_QUOTING_FUNCTIONS 1
83 : : #endif
84 : :
85 : : /* word break characters */
86 : : #define WORD_BREAKS "\t\n@><=;|&() "
87 : :
88 : : /*
89 : : * Since readline doesn't let us pass any state through to the tab completion
90 : : * callback, we have to use this global variable to let get_previous_words()
91 : : * get at the previous lines of the current command. Ick.
92 : : */
93 : : PQExpBuffer tab_completion_query_buf = NULL;
94 : :
95 : : /*
96 : : * In some situations, the query to find out what names are available to
97 : : * complete with must vary depending on server version. We handle this by
98 : : * storing a list of queries, each tagged with the minimum server version
99 : : * it will work for. Each list must be stored in descending server version
100 : : * order, so that the first satisfactory query is the one to use.
101 : : *
102 : : * When the query string is otherwise constant, an array of VersionedQuery
103 : : * suffices. Terminate the array with an entry having min_server_version = 0.
104 : : * That entry's query string can be a query that works in all supported older
105 : : * server versions, or NULL to give up and do no completion.
106 : : */
107 : : typedef struct VersionedQuery
108 : : {
109 : : int min_server_version;
110 : : const char *query;
111 : : } VersionedQuery;
112 : :
113 : : /*
114 : : * This struct is used to define "schema queries", which are custom-built
115 : : * to obtain possibly-schema-qualified names of database objects. There is
116 : : * enough similarity in the structure that we don't want to repeat it each
117 : : * time. So we put the components of each query into this struct and
118 : : * assemble them with the common boilerplate in _complete_from_query().
119 : : *
120 : : * We also use this struct to define queries that use completion_ref_object,
121 : : * which is some object related to the one(s) we want to get the names of
122 : : * (for example, the table we want the indexes of). In that usage the
123 : : * objects we're completing might not have a schema of their own, but the
124 : : * reference object almost always does (passed in completion_ref_schema).
125 : : *
126 : : * As with VersionedQuery, we can use an array of these if the query details
127 : : * must vary across versions.
128 : : */
129 : : typedef struct SchemaQuery
130 : : {
131 : : /*
132 : : * If not zero, minimum server version this struct applies to. If not
133 : : * zero, there should be a following struct with a smaller minimum server
134 : : * version; use catname == NULL in the last entry if we should do nothing.
135 : : */
136 : : int min_server_version;
137 : :
138 : : /*
139 : : * Name of catalog or catalogs to be queried, with alias(es), eg.
140 : : * "pg_catalog.pg_class c". Note that "pg_namespace n" and/or
141 : : * "pg_namespace nr" will be added automatically when needed.
142 : : */
143 : : const char *catname;
144 : :
145 : : /*
146 : : * Selection condition --- only rows meeting this condition are candidates
147 : : * to display. If catname mentions multiple tables, include the necessary
148 : : * join condition here. For example, this might look like "c.relkind = "
149 : : * CppAsString2(RELKIND_RELATION). Write NULL (not an empty string) if
150 : : * not needed.
151 : : */
152 : : const char *selcondition;
153 : :
154 : : /*
155 : : * Visibility condition --- which rows are visible without schema
156 : : * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)".
157 : : * NULL if not needed.
158 : : */
159 : : const char *viscondition;
160 : :
161 : : /*
162 : : * Namespace --- name of field to join to pg_namespace.oid when there is
163 : : * schema qualification. For example, "c.relnamespace". NULL if we don't
164 : : * want to join to pg_namespace (then any schema part in the input word
165 : : * will be ignored).
166 : : */
167 : : const char *namespace;
168 : :
169 : : /*
170 : : * Result --- the base object name to return. For example, "c.relname".
171 : : */
172 : : const char *result;
173 : :
174 : : /*
175 : : * In some cases, it's difficult to keep the query from returning the same
176 : : * object multiple times. Specify use_distinct to filter out duplicates.
177 : : */
178 : : bool use_distinct;
179 : :
180 : : /*
181 : : * Additional literal strings (usually keywords) to be offered along with
182 : : * the query results. Provide a NULL-terminated array of constant
183 : : * strings, or NULL if none.
184 : : */
185 : : const char *const *keywords;
186 : :
187 : : /*
188 : : * If this query uses completion_ref_object/completion_ref_schema,
189 : : * populate the remaining fields, else leave them NULL. When using this
190 : : * capability, catname must include the catalog that defines the
191 : : * completion_ref_object, and selcondition must include the join condition
192 : : * that connects it to the result's catalog.
193 : : *
194 : : * refname is the field that should be equated to completion_ref_object,
195 : : * for example "cr.relname".
196 : : */
197 : : const char *refname;
198 : :
199 : : /*
200 : : * Visibility condition to use when completion_ref_schema is not set. For
201 : : * example, "pg_catalog.pg_table_is_visible(cr.oid)". NULL if not needed.
202 : : */
203 : : const char *refviscondition;
204 : :
205 : : /*
206 : : * Name of field to join to pg_namespace.oid when completion_ref_schema is
207 : : * set. For example, "cr.relnamespace". NULL if we don't want to
208 : : * consider completion_ref_schema.
209 : : */
210 : : const char *refnamespace;
211 : : } SchemaQuery;
212 : :
213 : :
214 : : /*
215 : : * Store maximum number of records we want from database queries
216 : : * (implemented via SELECT ... LIMIT xx).
217 : : */
218 : : static int completion_max_records;
219 : :
220 : : /*
221 : : * Communication variables set by psql_completion (mostly in COMPLETE_WITH_FOO
222 : : * macros) and then used by the completion callback functions. Ugly but there
223 : : * is no better way.
224 : : */
225 : : static char completion_last_char; /* last char of input word */
226 : : static const char *completion_charp; /* to pass a string */
227 : : static const char *const *completion_charpp; /* to pass a list of strings */
228 : : static const VersionedQuery *completion_vquery; /* to pass a VersionedQuery */
229 : : static const SchemaQuery *completion_squery; /* to pass a SchemaQuery */
230 : : static char *completion_ref_object; /* name of reference object */
231 : : static char *completion_ref_schema; /* schema name of reference object */
232 : : static bool completion_case_sensitive; /* completion is case sensitive */
233 : : static bool completion_verbatim; /* completion is verbatim */
234 : : static bool completion_force_quote; /* true to force-quote filenames */
235 : :
236 : : /*
237 : : * A few macros to ease typing. You can use these to complete the given
238 : : * string with
239 : : * 1) The result from a query you pass it. (Perhaps one of those below?)
240 : : * We support both simple and versioned queries.
241 : : * 2) The result from a schema query you pass it.
242 : : * We support both simple and versioned schema queries.
243 : : * 3) The items from a null-pointer-terminated list (with or without
244 : : * case-sensitive comparison); if the list is constant you can build it
245 : : * with COMPLETE_WITH() or COMPLETE_WITH_CS(). The QUERY_LIST and
246 : : * QUERY_PLUS forms combine such literal lists with a query result.
247 : : * 4) The list of attributes of the given table (possibly schema-qualified).
248 : : * 5) The list of arguments to the given function (possibly schema-qualified).
249 : : *
250 : : * The query is generally expected to return raw SQL identifiers; matching
251 : : * to what the user typed is done in a quoting-aware fashion. If what is
252 : : * returned is not SQL identifiers, use one of the VERBATIM forms, in which
253 : : * case the query results are matched to the user's text without double-quote
254 : : * processing (so if quoting is needed, you must provide it in the query
255 : : * results).
256 : : */
257 : : #define COMPLETE_WITH_QUERY(query) \
258 : : COMPLETE_WITH_QUERY_LIST(query, NULL)
259 : :
260 : : #define COMPLETE_WITH_QUERY_LIST(query, list) \
261 : : do { \
262 : : completion_charp = query; \
263 : : completion_charpp = list; \
264 : : completion_verbatim = false; \
265 : : matches = rl_completion_matches(text, complete_from_query); \
266 : : } while (0)
267 : :
268 : : #define COMPLETE_WITH_QUERY_PLUS(query, ...) \
269 : : do { \
270 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
271 : : COMPLETE_WITH_QUERY_LIST(query, list); \
272 : : } while (0)
273 : :
274 : : #define COMPLETE_WITH_QUERY_VERBATIM(query) \
275 : : COMPLETE_WITH_QUERY_VERBATIM_LIST(query, NULL)
276 : :
277 : : #define COMPLETE_WITH_QUERY_VERBATIM_LIST(query, list) \
278 : : do { \
279 : : completion_charp = query; \
280 : : completion_charpp = list; \
281 : : completion_verbatim = true; \
282 : : matches = rl_completion_matches(text, complete_from_query); \
283 : : } while (0)
284 : :
285 : : #define COMPLETE_WITH_QUERY_VERBATIM_PLUS(query, ...) \
286 : : do { \
287 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
288 : : COMPLETE_WITH_QUERY_VERBATIM_LIST(query, list); \
289 : : } while (0)
290 : :
291 : : #define COMPLETE_WITH_VERSIONED_QUERY(query) \
292 : : COMPLETE_WITH_VERSIONED_QUERY_LIST(query, NULL)
293 : :
294 : : #define COMPLETE_WITH_VERSIONED_QUERY_LIST(query, list) \
295 : : do { \
296 : : completion_vquery = query; \
297 : : completion_charpp = list; \
298 : : completion_verbatim = false; \
299 : : matches = rl_completion_matches(text, complete_from_versioned_query); \
300 : : } while (0)
301 : :
302 : : #define COMPLETE_WITH_VERSIONED_QUERY_PLUS(query, ...) \
303 : : do { \
304 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
305 : : COMPLETE_WITH_VERSIONED_QUERY_LIST(query, list); \
306 : : } while (0)
307 : :
308 : : #define COMPLETE_WITH_SCHEMA_QUERY(query) \
309 : : COMPLETE_WITH_SCHEMA_QUERY_LIST(query, NULL)
310 : :
311 : : #define COMPLETE_WITH_SCHEMA_QUERY_LIST(query, list) \
312 : : do { \
313 : : completion_squery = &(query); \
314 : : completion_charpp = list; \
315 : : completion_verbatim = false; \
316 : : matches = rl_completion_matches(text, complete_from_schema_query); \
317 : : } while (0)
318 : :
319 : : #define COMPLETE_WITH_SCHEMA_QUERY_PLUS(query, ...) \
320 : : do { \
321 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
322 : : COMPLETE_WITH_SCHEMA_QUERY_LIST(query, list); \
323 : : } while (0)
324 : :
325 : : #define COMPLETE_WITH_SCHEMA_QUERY_VERBATIM(query) \
326 : : do { \
327 : : completion_squery = &(query); \
328 : : completion_charpp = NULL; \
329 : : completion_verbatim = true; \
330 : : matches = rl_completion_matches(text, complete_from_schema_query); \
331 : : } while (0)
332 : :
333 : : #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(query) \
334 : : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, NULL)
335 : :
336 : : #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, list) \
337 : : do { \
338 : : completion_squery = query; \
339 : : completion_charpp = list; \
340 : : completion_verbatim = false; \
341 : : matches = rl_completion_matches(text, complete_from_versioned_schema_query); \
342 : : } while (0)
343 : :
344 : : #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_PLUS(query, ...) \
345 : : do { \
346 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
347 : : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, list); \
348 : : } while (0)
349 : :
350 : : /*
351 : : * Caution: COMPLETE_WITH_CONST is not for general-purpose use; you probably
352 : : * want COMPLETE_WITH() with one element, instead.
353 : : */
354 : : #define COMPLETE_WITH_CONST(cs, con) \
355 : : do { \
356 : : completion_case_sensitive = (cs); \
357 : : completion_charp = (con); \
358 : : matches = rl_completion_matches(text, complete_from_const); \
359 : : } while (0)
360 : :
361 : : #define COMPLETE_WITH_LIST_INT(cs, list) \
362 : : do { \
363 : : completion_case_sensitive = (cs); \
364 : : completion_charpp = (list); \
365 : : matches = rl_completion_matches(text, complete_from_list); \
366 : : } while (0)
367 : :
368 : : #define COMPLETE_WITH_LIST(list) COMPLETE_WITH_LIST_INT(false, list)
369 : : #define COMPLETE_WITH_LIST_CS(list) COMPLETE_WITH_LIST_INT(true, list)
370 : :
371 : : #define COMPLETE_WITH(...) \
372 : : do { \
373 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
374 : : COMPLETE_WITH_LIST(list); \
375 : : } while (0)
376 : :
377 : : #define COMPLETE_WITH_CS(...) \
378 : : do { \
379 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
380 : : COMPLETE_WITH_LIST_CS(list); \
381 : : } while (0)
382 : :
383 : : #define COMPLETE_WITH_ATTR(relation) \
384 : : COMPLETE_WITH_ATTR_LIST(relation, NULL)
385 : :
386 : : #define COMPLETE_WITH_ATTR_LIST(relation, list) \
387 : : do { \
388 : : set_completion_reference(relation); \
389 : : completion_squery = &(Query_for_list_of_attributes); \
390 : : completion_charpp = list; \
391 : : completion_verbatim = false; \
392 : : matches = rl_completion_matches(text, complete_from_schema_query); \
393 : : } while (0)
394 : :
395 : : #define COMPLETE_WITH_ATTR_PLUS(relation, ...) \
396 : : do { \
397 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
398 : : COMPLETE_WITH_ATTR_LIST(relation, list); \
399 : : } while (0)
400 : :
401 : : /*
402 : : * libedit will typically include the literal's leading single quote in
403 : : * "text", while readline will not. Adapt our offered strings to fit.
404 : : * But include a quote if there's not one just before "text", to get the
405 : : * user off to the right start.
406 : : */
407 : : #define COMPLETE_WITH_ENUM_VALUE(type) \
408 : : do { \
409 : : set_completion_reference(type); \
410 : : if (text[0] == '\'' || \
411 : : start == 0 || rl_line_buffer[start - 1] != '\'') \
412 : : completion_squery = &(Query_for_list_of_enum_values_quoted); \
413 : : else \
414 : : completion_squery = &(Query_for_list_of_enum_values_unquoted); \
415 : : completion_charpp = NULL; \
416 : : completion_verbatim = true; \
417 : : matches = rl_completion_matches(text, complete_from_schema_query); \
418 : : } while (0)
419 : :
420 : : /*
421 : : * Timezone completion is mostly like enum label completion, but we work
422 : : * a little harder since this is a more common use-case.
423 : : */
424 : : #define COMPLETE_WITH_TIMEZONE_NAME() \
425 : : do { \
426 : : static const char *const list[] = { "DEFAULT", NULL }; \
427 : : if (text[0] == '\'') \
428 : : completion_charp = Query_for_list_of_timezone_names_quoted_in; \
429 : : else if (start == 0 || rl_line_buffer[start - 1] != '\'') \
430 : : completion_charp = Query_for_list_of_timezone_names_quoted_out; \
431 : : else \
432 : : completion_charp = Query_for_list_of_timezone_names_unquoted; \
433 : : completion_charpp = list; \
434 : : completion_verbatim = true; \
435 : : matches = rl_completion_matches(text, complete_from_query); \
436 : : } while (0)
437 : :
438 : : #define COMPLETE_WITH_FUNCTION_ARG(function) \
439 : : do { \
440 : : set_completion_reference(function); \
441 : : completion_squery = &(Query_for_list_of_arguments); \
442 : : completion_charpp = NULL; \
443 : : completion_verbatim = true; \
444 : : matches = rl_completion_matches(text, complete_from_schema_query); \
445 : : } while (0)
446 : :
447 : : #define COMPLETE_WITH_FILES_LIST(escape, force_quote, list) \
448 : : do { \
449 : : completion_charp = escape; \
450 : : completion_charpp = list; \
451 : : completion_force_quote = force_quote; \
452 : : matches = rl_completion_matches(text, complete_from_files); \
453 : : } while (0)
454 : :
455 : : #define COMPLETE_WITH_FILES(escape, force_quote) \
456 : : COMPLETE_WITH_FILES_LIST(escape, force_quote, NULL)
457 : :
458 : : #define COMPLETE_WITH_FILES_PLUS(escape, force_quote, ...) \
459 : : do { \
460 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
461 : : COMPLETE_WITH_FILES_LIST(escape, force_quote, list); \
462 : : } while (0)
463 : :
464 : : #define COMPLETE_WITH_GENERATOR(generator) \
465 : : matches = rl_completion_matches(text, generator)
466 : :
467 : : /*
468 : : * Assembly instructions for schema queries
469 : : *
470 : : * Note that toast tables are not included in those queries to avoid
471 : : * unnecessary bloat in the completions generated.
472 : : */
473 : :
474 : : static const SchemaQuery Query_for_constraint_of_table = {
475 : : .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_class c1",
476 : : .selcondition = "con.conrelid=c1.oid",
477 : : .result = "con.conname",
478 : : .refname = "c1.relname",
479 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
480 : : .refnamespace = "c1.relnamespace",
481 : : };
482 : :
483 : : static const SchemaQuery Query_for_constraint_of_table_not_validated = {
484 : : .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_class c1",
485 : : .selcondition = "con.conrelid=c1.oid and not con.convalidated",
486 : : .result = "con.conname",
487 : : .refname = "c1.relname",
488 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
489 : : .refnamespace = "c1.relnamespace",
490 : : };
491 : :
492 : : static const SchemaQuery Query_for_constraint_of_type = {
493 : : .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_type t",
494 : : .selcondition = "con.contypid=t.oid",
495 : : .result = "con.conname",
496 : : .refname = "t.typname",
497 : : .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
498 : : .refnamespace = "t.typnamespace",
499 : : };
500 : :
501 : : static const SchemaQuery Query_for_index_of_table = {
502 : : .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i",
503 : : .selcondition = "c1.oid=i.indrelid and i.indexrelid=c2.oid",
504 : : .result = "c2.relname",
505 : : .refname = "c1.relname",
506 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
507 : : .refnamespace = "c1.relnamespace",
508 : : };
509 : :
510 : : static const SchemaQuery Query_for_unique_index_of_table = {
511 : : .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i",
512 : : .selcondition = "c1.oid=i.indrelid and i.indexrelid=c2.oid and i.indisunique",
513 : : .result = "c2.relname",
514 : : .refname = "c1.relname",
515 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
516 : : .refnamespace = "c1.relnamespace",
517 : : };
518 : :
519 : : static const SchemaQuery Query_for_list_of_aggregates[] = {
520 : : {
521 : : .min_server_version = 110000,
522 : : .catname = "pg_catalog.pg_proc p",
523 : : .selcondition = "p.prokind = 'a'",
524 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
525 : : .namespace = "p.pronamespace",
526 : : .result = "p.proname",
527 : : },
528 : : {
529 : : .catname = "pg_catalog.pg_proc p",
530 : : .selcondition = "p.proisagg",
531 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
532 : : .namespace = "p.pronamespace",
533 : : .result = "p.proname",
534 : : }
535 : : };
536 : :
537 : : static const SchemaQuery Query_for_list_of_arguments = {
538 : : .catname = "pg_catalog.pg_proc p",
539 : : .result = "pg_catalog.oidvectortypes(p.proargtypes)||')'",
540 : : .refname = "p.proname",
541 : : .refviscondition = "pg_catalog.pg_function_is_visible(p.oid)",
542 : : .refnamespace = "p.pronamespace",
543 : : };
544 : :
545 : : static const SchemaQuery Query_for_list_of_attributes = {
546 : : .catname = "pg_catalog.pg_attribute a, pg_catalog.pg_class c",
547 : : .selcondition = "c.oid = a.attrelid and a.attnum > 0 and not a.attisdropped",
548 : : .result = "a.attname",
549 : : .refname = "c.relname",
550 : : .refviscondition = "pg_catalog.pg_table_is_visible(c.oid)",
551 : : .refnamespace = "c.relnamespace",
552 : : };
553 : :
554 : : static const SchemaQuery Query_for_list_of_attribute_numbers = {
555 : : .catname = "pg_catalog.pg_attribute a, pg_catalog.pg_class c",
556 : : .selcondition = "c.oid = a.attrelid and a.attnum > 0 and not a.attisdropped",
557 : : .result = "a.attnum::pg_catalog.text",
558 : : .refname = "c.relname",
559 : : .refviscondition = "pg_catalog.pg_table_is_visible(c.oid)",
560 : : .refnamespace = "c.relnamespace",
561 : : };
562 : :
563 : : static const char *const Keywords_for_list_of_datatypes[] = {
564 : : "bigint",
565 : : "boolean",
566 : : "character",
567 : : "double precision",
568 : : "integer",
569 : : "real",
570 : : "smallint",
571 : :
572 : : /*
573 : : * Note: currently there's no value in offering the following multiword
574 : : * type names, because tab completion cannot succeed for them: we can't
575 : : * disambiguate until somewhere in the second word, at which point we
576 : : * won't have the first word as context. ("double precision" does work,
577 : : * as long as no other type name begins with "double".) Leave them out to
578 : : * encourage users to use the PG-specific aliases, which we can complete.
579 : : */
580 : : #ifdef NOT_USED
581 : : "bit varying",
582 : : "character varying",
583 : : "time with time zone",
584 : : "time without time zone",
585 : : "timestamp with time zone",
586 : : "timestamp without time zone",
587 : : #endif
588 : : NULL
589 : : };
590 : :
591 : : static const SchemaQuery Query_for_list_of_datatypes = {
592 : : .catname = "pg_catalog.pg_type t",
593 : : /* selcondition --- ignore table rowtypes and array types */
594 : : .selcondition = "(t.typrelid = 0 "
595 : : " OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
596 : : " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) "
597 : : "AND t.typname !~ '^_'",
598 : : .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
599 : : .namespace = "t.typnamespace",
600 : : .result = "t.typname",
601 : : .keywords = Keywords_for_list_of_datatypes,
602 : : };
603 : :
604 : : static const SchemaQuery Query_for_list_of_composite_datatypes = {
605 : : .catname = "pg_catalog.pg_type t",
606 : : /* selcondition --- only get composite types */
607 : : .selcondition = "(SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
608 : : " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid) "
609 : : "AND t.typname !~ '^_'",
610 : : .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
611 : : .namespace = "t.typnamespace",
612 : : .result = "t.typname",
613 : : };
614 : :
615 : : static const SchemaQuery Query_for_list_of_domains = {
616 : : .catname = "pg_catalog.pg_type t",
617 : : .selcondition = "t.typtype = 'd'",
618 : : .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
619 : : .namespace = "t.typnamespace",
620 : : .result = "t.typname",
621 : : };
622 : :
623 : : static const SchemaQuery Query_for_list_of_enum_values_quoted = {
624 : : .catname = "pg_catalog.pg_enum e, pg_catalog.pg_type t",
625 : : .selcondition = "t.oid = e.enumtypid",
626 : : .result = "pg_catalog.quote_literal(enumlabel)",
627 : : .refname = "t.typname",
628 : : .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
629 : : .refnamespace = "t.typnamespace",
630 : : };
631 : :
632 : : static const SchemaQuery Query_for_list_of_enum_values_unquoted = {
633 : : .catname = "pg_catalog.pg_enum e, pg_catalog.pg_type t",
634 : : .selcondition = "t.oid = e.enumtypid",
635 : : .result = "e.enumlabel",
636 : : .refname = "t.typname",
637 : : .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
638 : : .refnamespace = "t.typnamespace",
639 : : };
640 : :
641 : : /* Note: this intentionally accepts aggregates as well as plain functions */
642 : : static const SchemaQuery Query_for_list_of_functions[] = {
643 : : {
644 : : .min_server_version = 110000,
645 : : .catname = "pg_catalog.pg_proc p",
646 : : .selcondition = "p.prokind != 'p'",
647 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
648 : : .namespace = "p.pronamespace",
649 : : .result = "p.proname",
650 : : },
651 : : {
652 : : .catname = "pg_catalog.pg_proc p",
653 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
654 : : .namespace = "p.pronamespace",
655 : : .result = "p.proname",
656 : : }
657 : : };
658 : :
659 : : static const SchemaQuery Query_for_list_of_procedures[] = {
660 : : {
661 : : .min_server_version = 110000,
662 : : .catname = "pg_catalog.pg_proc p",
663 : : .selcondition = "p.prokind = 'p'",
664 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
665 : : .namespace = "p.pronamespace",
666 : : .result = "p.proname",
667 : : },
668 : : {
669 : : /* not supported in older versions */
670 : : .catname = NULL,
671 : : }
672 : : };
673 : :
674 : : static const SchemaQuery Query_for_list_of_routines = {
675 : : .catname = "pg_catalog.pg_proc p",
676 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
677 : : .namespace = "p.pronamespace",
678 : : .result = "p.proname",
679 : : };
680 : :
681 : : static const SchemaQuery Query_for_list_of_sequences = {
682 : : .catname = "pg_catalog.pg_class c",
683 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_SEQUENCE) ")",
684 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
685 : : .namespace = "c.relnamespace",
686 : : .result = "c.relname",
687 : : };
688 : :
689 : : static const SchemaQuery Query_for_list_of_foreign_tables = {
690 : : .catname = "pg_catalog.pg_class c",
691 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_FOREIGN_TABLE) ")",
692 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
693 : : .namespace = "c.relnamespace",
694 : : .result = "c.relname",
695 : : };
696 : :
697 : : static const SchemaQuery Query_for_list_of_tables = {
698 : : .catname = "pg_catalog.pg_class c",
699 : : .selcondition =
700 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
701 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
702 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
703 : : .namespace = "c.relnamespace",
704 : : .result = "c.relname",
705 : : };
706 : :
707 : : static const SchemaQuery Query_for_list_of_partitioned_tables = {
708 : : .catname = "pg_catalog.pg_class c",
709 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
710 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
711 : : .namespace = "c.relnamespace",
712 : : .result = "c.relname",
713 : : };
714 : :
715 : : static const SchemaQuery Query_for_list_of_tables_for_constraint = {
716 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_constraint con",
717 : : .selcondition = "c.oid=con.conrelid and c.relkind IN ("
718 : : CppAsString2(RELKIND_RELATION) ", "
719 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
720 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
721 : : .namespace = "c.relnamespace",
722 : : .result = "c.relname",
723 : : .use_distinct = true,
724 : : .refname = "con.conname",
725 : : };
726 : :
727 : : static const SchemaQuery Query_for_list_of_tables_for_policy = {
728 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_policy p",
729 : : .selcondition = "c.oid=p.polrelid",
730 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
731 : : .namespace = "c.relnamespace",
732 : : .result = "c.relname",
733 : : .use_distinct = true,
734 : : .refname = "p.polname",
735 : : };
736 : :
737 : : static const SchemaQuery Query_for_list_of_tables_for_rule = {
738 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_rewrite r",
739 : : .selcondition = "c.oid=r.ev_class",
740 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
741 : : .namespace = "c.relnamespace",
742 : : .result = "c.relname",
743 : : .use_distinct = true,
744 : : .refname = "r.rulename",
745 : : };
746 : :
747 : : static const SchemaQuery Query_for_list_of_tables_for_trigger = {
748 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_trigger t",
749 : : .selcondition = "c.oid=t.tgrelid",
750 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
751 : : .namespace = "c.relnamespace",
752 : : .result = "c.relname",
753 : : .use_distinct = true,
754 : : .refname = "t.tgname",
755 : : };
756 : :
757 : : static const SchemaQuery Query_for_list_of_ts_configurations = {
758 : : .catname = "pg_catalog.pg_ts_config c",
759 : : .viscondition = "pg_catalog.pg_ts_config_is_visible(c.oid)",
760 : : .namespace = "c.cfgnamespace",
761 : : .result = "c.cfgname",
762 : : };
763 : :
764 : : static const SchemaQuery Query_for_list_of_ts_dictionaries = {
765 : : .catname = "pg_catalog.pg_ts_dict d",
766 : : .viscondition = "pg_catalog.pg_ts_dict_is_visible(d.oid)",
767 : : .namespace = "d.dictnamespace",
768 : : .result = "d.dictname",
769 : : };
770 : :
771 : : static const SchemaQuery Query_for_list_of_ts_parsers = {
772 : : .catname = "pg_catalog.pg_ts_parser p",
773 : : .viscondition = "pg_catalog.pg_ts_parser_is_visible(p.oid)",
774 : : .namespace = "p.prsnamespace",
775 : : .result = "p.prsname",
776 : : };
777 : :
778 : : static const SchemaQuery Query_for_list_of_ts_templates = {
779 : : .catname = "pg_catalog.pg_ts_template t",
780 : : .viscondition = "pg_catalog.pg_ts_template_is_visible(t.oid)",
781 : : .namespace = "t.tmplnamespace",
782 : : .result = "t.tmplname",
783 : : };
784 : :
785 : : static const SchemaQuery Query_for_list_of_views = {
786 : : .catname = "pg_catalog.pg_class c",
787 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")",
788 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
789 : : .namespace = "c.relnamespace",
790 : : .result = "c.relname",
791 : : };
792 : :
793 : : static const SchemaQuery Query_for_list_of_matviews = {
794 : : .catname = "pg_catalog.pg_class c",
795 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")",
796 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
797 : : .namespace = "c.relnamespace",
798 : : .result = "c.relname",
799 : : };
800 : :
801 : : static const SchemaQuery Query_for_list_of_indexes = {
802 : : .catname = "pg_catalog.pg_class c",
803 : : .selcondition =
804 : : "c.relkind IN (" CppAsString2(RELKIND_INDEX) ", "
805 : : CppAsString2(RELKIND_PARTITIONED_INDEX) ")",
806 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
807 : : .namespace = "c.relnamespace",
808 : : .result = "c.relname",
809 : : };
810 : :
811 : : static const SchemaQuery Query_for_list_of_partitioned_indexes = {
812 : : .catname = "pg_catalog.pg_class c",
813 : : .selcondition = "c.relkind = " CppAsString2(RELKIND_PARTITIONED_INDEX),
814 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
815 : : .namespace = "c.relnamespace",
816 : : .result = "c.relname",
817 : : };
818 : :
819 : :
820 : : /* All relations */
821 : : static const SchemaQuery Query_for_list_of_relations = {
822 : : .catname = "pg_catalog.pg_class c",
823 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
824 : : .namespace = "c.relnamespace",
825 : : .result = "c.relname",
826 : : };
827 : :
828 : : /* partitioned relations */
829 : : static const SchemaQuery Query_for_list_of_partitioned_relations = {
830 : : .catname = "pg_catalog.pg_class c",
831 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE)
832 : : ", " CppAsString2(RELKIND_PARTITIONED_INDEX) ")",
833 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
834 : : .namespace = "c.relnamespace",
835 : : .result = "c.relname",
836 : : };
837 : :
838 : : static const SchemaQuery Query_for_list_of_operator_families = {
839 : : .catname = "pg_catalog.pg_opfamily c",
840 : : .viscondition = "pg_catalog.pg_opfamily_is_visible(c.oid)",
841 : : .namespace = "c.opfnamespace",
842 : : .result = "c.opfname",
843 : : };
844 : :
845 : : /* Relations supporting INSERT, UPDATE or DELETE */
846 : : static const SchemaQuery Query_for_list_of_updatables = {
847 : : .catname = "pg_catalog.pg_class c",
848 : : .selcondition =
849 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
850 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
851 : : CppAsString2(RELKIND_VIEW) ", "
852 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
853 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
854 : : .namespace = "c.relnamespace",
855 : : .result = "c.relname",
856 : : };
857 : :
858 : : /* Relations supporting MERGE */
859 : : static const SchemaQuery Query_for_list_of_mergetargets = {
860 : : .catname = "pg_catalog.pg_class c",
861 : : .selcondition =
862 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
863 : : CppAsString2(RELKIND_VIEW) ", "
864 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ") ",
865 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
866 : : .namespace = "c.relnamespace",
867 : : .result = "c.relname",
868 : : };
869 : :
870 : : /* Relations supporting SELECT */
871 : : static const SchemaQuery Query_for_list_of_selectables = {
872 : : .catname = "pg_catalog.pg_class c",
873 : : .selcondition =
874 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
875 : : CppAsString2(RELKIND_SEQUENCE) ", "
876 : : CppAsString2(RELKIND_VIEW) ", "
877 : : CppAsString2(RELKIND_MATVIEW) ", "
878 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
879 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
880 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
881 : : .namespace = "c.relnamespace",
882 : : .result = "c.relname",
883 : : };
884 : :
885 : : /* Relations supporting TRUNCATE */
886 : : static const SchemaQuery Query_for_list_of_truncatables = {
887 : : .catname = "pg_catalog.pg_class c",
888 : : .selcondition =
889 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
890 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
891 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
892 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
893 : : .namespace = "c.relnamespace",
894 : : .result = "c.relname",
895 : : };
896 : :
897 : : /* Relations supporting GRANT are currently same as those supporting SELECT */
898 : : #define Query_for_list_of_grantables Query_for_list_of_selectables
899 : :
900 : : /* Relations supporting ANALYZE */
901 : : static const SchemaQuery Query_for_list_of_analyzables = {
902 : : .catname = "pg_catalog.pg_class c",
903 : : .selcondition =
904 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
905 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
906 : : CppAsString2(RELKIND_MATVIEW) ", "
907 : : CppAsString2(RELKIND_FOREIGN_TABLE) ")",
908 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
909 : : .namespace = "c.relnamespace",
910 : : .result = "c.relname",
911 : : };
912 : :
913 : : /*
914 : : * Relations supporting COPY TO/FROM are currently almost the same as
915 : : * those supporting ANALYZE. Although views with INSTEAD OF INSERT triggers
916 : : * can be used with COPY FROM, they are rarely used for this purpose,
917 : : * so plain views are intentionally excluded from this tab completion.
918 : : */
919 : : #define Query_for_list_of_tables_for_copy Query_for_list_of_analyzables
920 : :
921 : : /* Relations supporting index creation */
922 : : static const SchemaQuery Query_for_list_of_indexables = {
923 : : .catname = "pg_catalog.pg_class c",
924 : : .selcondition =
925 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
926 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
927 : : CppAsString2(RELKIND_MATVIEW) ")",
928 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
929 : : .namespace = "c.relnamespace",
930 : : .result = "c.relname",
931 : : };
932 : :
933 : : /*
934 : : * Relations supporting VACUUM are currently same as those supporting
935 : : * indexing.
936 : : */
937 : : #define Query_for_list_of_vacuumables Query_for_list_of_indexables
938 : :
939 : : /* Relations supporting CLUSTER */
940 : : static const SchemaQuery Query_for_list_of_clusterables = {
941 : : .catname = "pg_catalog.pg_class c",
942 : : .selcondition =
943 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
944 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
945 : : CppAsString2(RELKIND_MATVIEW) ")",
946 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
947 : : .namespace = "c.relnamespace",
948 : : .result = "c.relname",
949 : : };
950 : :
951 : : static const SchemaQuery Query_for_list_of_constraints_with_schema = {
952 : : .catname = "pg_catalog.pg_constraint c",
953 : : .selcondition = "c.conrelid <> 0",
954 : : .namespace = "c.connamespace",
955 : : .result = "c.conname",
956 : : };
957 : :
958 : : static const SchemaQuery Query_for_list_of_statistics = {
959 : : .catname = "pg_catalog.pg_statistic_ext s",
960 : : .viscondition = "pg_catalog.pg_statistics_obj_is_visible(s.oid)",
961 : : .namespace = "s.stxnamespace",
962 : : .result = "s.stxname",
963 : : };
964 : :
965 : : static const SchemaQuery Query_for_list_of_collations = {
966 : : .catname = "pg_catalog.pg_collation c",
967 : : .selcondition = "c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))",
968 : : .viscondition = "pg_catalog.pg_collation_is_visible(c.oid)",
969 : : .namespace = "c.collnamespace",
970 : : .result = "c.collname",
971 : : };
972 : :
973 : : static const SchemaQuery Query_for_partition_of_table = {
974 : : .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_inherits i",
975 : : .selcondition = "c1.oid=i.inhparent and i.inhrelid=c2.oid and c2.relispartition",
976 : : .viscondition = "pg_catalog.pg_table_is_visible(c2.oid)",
977 : : .namespace = "c2.relnamespace",
978 : : .result = "c2.relname",
979 : : .refname = "c1.relname",
980 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
981 : : .refnamespace = "c1.relnamespace",
982 : : };
983 : :
984 : : static const SchemaQuery Query_for_rule_of_table = {
985 : : .catname = "pg_catalog.pg_rewrite r, pg_catalog.pg_class c1",
986 : : .selcondition = "r.ev_class=c1.oid",
987 : : .result = "r.rulename",
988 : : .refname = "c1.relname",
989 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
990 : : .refnamespace = "c1.relnamespace",
991 : : };
992 : :
993 : : static const SchemaQuery Query_for_trigger_of_table = {
994 : : .catname = "pg_catalog.pg_trigger t, pg_catalog.pg_class c1",
995 : : .selcondition = "t.tgrelid=c1.oid and not t.tgisinternal",
996 : : .result = "t.tgname",
997 : : .refname = "c1.relname",
998 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
999 : : .refnamespace = "c1.relnamespace",
1000 : : };
1001 : :
1002 : :
1003 : : /*
1004 : : * Queries to get lists of names of various kinds of things, possibly
1005 : : * restricted to names matching a partially entered name. Don't use
1006 : : * this method where the user might wish to enter a schema-qualified
1007 : : * name; make a SchemaQuery instead.
1008 : : *
1009 : : * In these queries, there must be a restriction clause of the form
1010 : : * output LIKE '%s'
1011 : : * where "output" is the same string that the query returns. The %s
1012 : : * will be replaced by a LIKE pattern to match the already-typed text.
1013 : : *
1014 : : * There can be a second '%s', which will be replaced by a suitably-escaped
1015 : : * version of the string provided in completion_ref_object. If there is a
1016 : : * third '%s', it will be replaced by a suitably-escaped version of the string
1017 : : * provided in completion_ref_schema. NOTE: using completion_ref_object
1018 : : * that way is usually the wrong thing, and using completion_ref_schema
1019 : : * that way is always the wrong thing. Make a SchemaQuery instead.
1020 : : */
1021 : :
1022 : : #define Query_for_list_of_template_databases \
1023 : : "SELECT d.datname "\
1024 : : " FROM pg_catalog.pg_database d "\
1025 : : " WHERE d.datname LIKE '%s' "\
1026 : : " AND (d.datistemplate OR pg_catalog.pg_has_role(d.datdba, 'USAGE'))"
1027 : :
1028 : : #define Query_for_list_of_databases \
1029 : : "SELECT datname FROM pg_catalog.pg_database "\
1030 : : " WHERE datname LIKE '%s'"
1031 : :
1032 : : #define Query_for_list_of_database_vars \
1033 : : "SELECT conf FROM ("\
1034 : : " SELECT setdatabase, pg_catalog.split_part(pg_catalog.unnest(setconfig),'=',1) conf"\
1035 : : " FROM pg_catalog.pg_db_role_setting "\
1036 : : " ) s, pg_catalog.pg_database d "\
1037 : : " WHERE s.setdatabase = d.oid "\
1038 : : " AND conf LIKE '%s'"\
1039 : : " AND d.datname LIKE '%s'"
1040 : :
1041 : : #define Query_for_list_of_tablespaces \
1042 : : "SELECT spcname FROM pg_catalog.pg_tablespace "\
1043 : : " WHERE spcname LIKE '%s'"
1044 : :
1045 : : #define Query_for_list_of_encodings \
1046 : : " SELECT DISTINCT pg_catalog.pg_encoding_to_char(conforencoding) "\
1047 : : " FROM pg_catalog.pg_conversion "\
1048 : : " WHERE pg_catalog.pg_encoding_to_char(conforencoding) LIKE pg_catalog.upper('%s')"
1049 : :
1050 : : #define Query_for_list_of_languages \
1051 : : "SELECT lanname "\
1052 : : " FROM pg_catalog.pg_language "\
1053 : : " WHERE lanname != 'internal' "\
1054 : : " AND lanname LIKE '%s'"
1055 : :
1056 : : #define Query_for_list_of_schemas \
1057 : : "SELECT nspname FROM pg_catalog.pg_namespace "\
1058 : : " WHERE nspname LIKE '%s'"
1059 : :
1060 : : /* Use COMPLETE_WITH_QUERY_VERBATIM with these queries for GUC names: */
1061 : : #define Query_for_list_of_alter_system_set_vars \
1062 : : "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1063 : : " WHERE context != 'internal' "\
1064 : : " AND pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1065 : :
1066 : : #define Query_for_list_of_set_vars \
1067 : : "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1068 : : " WHERE context IN ('user', 'superuser') "\
1069 : : " AND pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1070 : :
1071 : : #define Query_for_list_of_show_vars \
1072 : : "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1073 : : " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1074 : :
1075 : : #define Query_for_list_of_roles \
1076 : : " SELECT rolname "\
1077 : : " FROM pg_catalog.pg_roles "\
1078 : : " WHERE rolname LIKE '%s'"
1079 : :
1080 : : /* add these to Query_for_list_of_roles in OWNER contexts */
1081 : : #define Keywords_for_list_of_owner_roles \
1082 : : "CURRENT_ROLE", "CURRENT_USER", "SESSION_USER"
1083 : :
1084 : : /* add these to Query_for_list_of_roles in GRANT contexts */
1085 : : #define Keywords_for_list_of_grant_roles \
1086 : : Keywords_for_list_of_owner_roles, "PUBLIC"
1087 : :
1088 : : #define Query_for_all_table_constraints \
1089 : : "SELECT conname "\
1090 : : " FROM pg_catalog.pg_constraint c "\
1091 : : " WHERE c.conrelid <> 0 "\
1092 : : " and conname LIKE '%s'"
1093 : :
1094 : : #define Query_for_list_of_fdws \
1095 : : " SELECT fdwname "\
1096 : : " FROM pg_catalog.pg_foreign_data_wrapper "\
1097 : : " WHERE fdwname LIKE '%s'"
1098 : :
1099 : : #define Query_for_list_of_servers \
1100 : : " SELECT srvname "\
1101 : : " FROM pg_catalog.pg_foreign_server "\
1102 : : " WHERE srvname LIKE '%s'"
1103 : :
1104 : : #define Query_for_list_of_user_mappings \
1105 : : " SELECT usename "\
1106 : : " FROM pg_catalog.pg_user_mappings "\
1107 : : " WHERE usename LIKE '%s'"
1108 : :
1109 : : #define Query_for_list_of_user_vars \
1110 : : "SELECT conf FROM ("\
1111 : : " SELECT rolname, pg_catalog.split_part(pg_catalog.unnest(rolconfig),'=',1) conf"\
1112 : : " FROM pg_catalog.pg_roles"\
1113 : : " ) s"\
1114 : : " WHERE s.conf like '%s' "\
1115 : : " AND s.rolname LIKE '%s'"
1116 : :
1117 : : #define Query_for_list_of_access_methods \
1118 : : " SELECT amname "\
1119 : : " FROM pg_catalog.pg_am "\
1120 : : " WHERE amname LIKE '%s'"
1121 : :
1122 : : #define Query_for_list_of_index_access_methods \
1123 : : " SELECT amname "\
1124 : : " FROM pg_catalog.pg_am "\
1125 : : " WHERE amname LIKE '%s' AND "\
1126 : : " amtype=" CppAsString2(AMTYPE_INDEX)
1127 : :
1128 : : #define Query_for_list_of_table_access_methods \
1129 : : " SELECT amname "\
1130 : : " FROM pg_catalog.pg_am "\
1131 : : " WHERE amname LIKE '%s' AND "\
1132 : : " amtype=" CppAsString2(AMTYPE_TABLE)
1133 : :
1134 : : #define Query_for_list_of_extensions \
1135 : : " SELECT extname "\
1136 : : " FROM pg_catalog.pg_extension "\
1137 : : " WHERE extname LIKE '%s'"
1138 : :
1139 : : #define Query_for_list_of_available_extensions \
1140 : : " SELECT name "\
1141 : : " FROM pg_catalog.pg_available_extensions "\
1142 : : " WHERE name LIKE '%s' AND installed_version IS NULL"
1143 : :
1144 : : #define Query_for_list_of_available_extension_versions \
1145 : : " SELECT version "\
1146 : : " FROM pg_catalog.pg_available_extension_versions "\
1147 : : " WHERE version LIKE '%s' AND name='%s'"
1148 : :
1149 : : #define Query_for_list_of_prepared_statements \
1150 : : " SELECT name "\
1151 : : " FROM pg_catalog.pg_prepared_statements "\
1152 : : " WHERE name LIKE '%s'"
1153 : :
1154 : : #define Query_for_list_of_event_triggers \
1155 : : " SELECT evtname "\
1156 : : " FROM pg_catalog.pg_event_trigger "\
1157 : : " WHERE evtname LIKE '%s'"
1158 : :
1159 : : #define Query_for_list_of_tablesample_methods \
1160 : : " SELECT proname "\
1161 : : " FROM pg_catalog.pg_proc "\
1162 : : " WHERE prorettype = 'pg_catalog.tsm_handler'::pg_catalog.regtype AND "\
1163 : : " proargtypes[0] = 'pg_catalog.internal'::pg_catalog.regtype AND "\
1164 : : " proname LIKE '%s'"
1165 : :
1166 : : #define Query_for_list_of_policies \
1167 : : " SELECT polname "\
1168 : : " FROM pg_catalog.pg_policy "\
1169 : : " WHERE polname LIKE '%s'"
1170 : :
1171 : : #define Query_for_values_of_enum_GUC \
1172 : : " SELECT val FROM ( "\
1173 : : " SELECT name, pg_catalog.unnest(enumvals) AS val "\
1174 : : " FROM pg_catalog.pg_settings "\
1175 : : " ) ss "\
1176 : : " WHERE val LIKE '%s'"\
1177 : : " and pg_catalog.lower(name)=pg_catalog.lower('%s')"
1178 : :
1179 : : #define Query_for_list_of_channels \
1180 : : " SELECT channel "\
1181 : : " FROM pg_catalog.pg_listening_channels() AS channel "\
1182 : : " WHERE channel LIKE '%s'"
1183 : :
1184 : : #define Query_for_list_of_cursors \
1185 : : " SELECT name "\
1186 : : " FROM pg_catalog.pg_cursors "\
1187 : : " WHERE name LIKE '%s'"
1188 : :
1189 : : #define Query_for_list_of_timezone_names_unquoted \
1190 : : " SELECT name "\
1191 : : " FROM pg_catalog.pg_timezone_names() "\
1192 : : " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1193 : :
1194 : : #define Query_for_list_of_timezone_names_quoted_out \
1195 : : "SELECT pg_catalog.quote_literal(name) AS name "\
1196 : : " FROM pg_catalog.pg_timezone_names() "\
1197 : : " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1198 : :
1199 : : #define Query_for_list_of_timezone_names_quoted_in \
1200 : : "SELECT pg_catalog.quote_literal(name) AS name "\
1201 : : " FROM pg_catalog.pg_timezone_names() "\
1202 : : " WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.lower('%s')"
1203 : :
1204 : : #define Query_for_list_of_publications \
1205 : : "SELECT pubname "\
1206 : : " FROM pg_catalog.pg_publication "\
1207 : : " WHERE pubname LIKE '%s'"
1208 : :
1209 : : #define Query_for_list_of_subscriptions \
1210 : : "SELECT s.subname "\
1211 : : " FROM pg_catalog.pg_subscription s, pg_catalog.pg_database d"\
1212 : : " WHERE s.subname LIKE '%s' "\
1213 : : " AND d.datname = pg_catalog.current_database() "\
1214 : : " AND s.subdbid = d.oid"
1215 : :
1216 : : /* Privilege options shared between GRANT and REVOKE */
1217 : : #define Privilege_options_of_grant_and_revoke \
1218 : : "SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", \
1219 : : "CREATE", "CONNECT", "TEMPORARY", "EXECUTE", "USAGE", "SET", "ALTER SYSTEM", \
1220 : : "MAINTAIN", "ALL"
1221 : :
1222 : : /* ALTER PROCEDURE options */
1223 : : #define Alter_procedure_options \
1224 : : "DEPENDS ON EXTENSION", "EXTERNAL SECURITY", "NO DEPENDS ON EXTENSION", \
1225 : : "OWNER TO", "RENAME TO", "RESET", "SECURITY", "SET"
1226 : :
1227 : : /* ALTER ROUTINE options */
1228 : : #define Alter_routine_options \
1229 : : Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
1230 : : "PARALLEL", "ROWS", "STABLE", "VOLATILE"
1231 : :
1232 : : /* ALTER FUNCTION options */
1233 : : #define Alter_function_options \
1234 : : Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
1235 : : "STRICT", "SUPPORT"
1236 : :
1237 : : /* COPY options shared between FROM and TO */
1238 : : #define Copy_common_options \
1239 : : "DELIMITER", "ENCODING", "ESCAPE", "FORMAT", "HEADER", "NULL", "QUOTE"
1240 : :
1241 : : /* COPY FROM options */
1242 : : #define Copy_from_options \
1243 : : Copy_common_options, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
1244 : : "LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"
1245 : :
1246 : : /* COPY TO options */
1247 : : #define Copy_to_options \
1248 : : Copy_common_options, "FORCE_QUOTE", "FORCE_ARRAY"
1249 : :
1250 : : /* Known command-starting keywords. */
1251 : : static const char *const sql_commands[] = {
1252 : : "ABORT", "ALTER", "ANALYZE", "BEGIN", "CALL", "CHECKPOINT", "CLOSE", "CLUSTER",
1253 : : "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
1254 : : "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN",
1255 : : "FETCH", "GRANT", "IMPORT FOREIGN SCHEMA", "INSERT INTO", "LISTEN", "LOAD", "LOCK",
1256 : : "MERGE INTO", "MOVE", "NOTIFY", "PREPARE",
1257 : : "REASSIGN", "REFRESH MATERIALIZED VIEW", "REINDEX", "RELEASE", "REPACK",
1258 : : "RESET", "REVOKE", "ROLLBACK",
1259 : : "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START",
1260 : : "TABLE", "TRUNCATE", "UNLISTEN", "UPDATE", "VACUUM", "VALUES",
1261 : : "WAIT FOR", "WITH",
1262 : : NULL
1263 : : };
1264 : :
1265 : : /*
1266 : : * This is a list of all "things" in Pgsql, which can show up after CREATE or
1267 : : * DROP; and there is also a query to get a list of them.
1268 : : */
1269 : :
1270 : : typedef struct
1271 : : {
1272 : : const char *name;
1273 : : /* Provide at most one of these three types of query: */
1274 : : const char *query; /* simple query, or NULL */
1275 : : const VersionedQuery *vquery; /* versioned query, or NULL */
1276 : : const SchemaQuery *squery; /* schema query, or NULL */
1277 : : const char *const *keywords; /* keywords to be offered as well */
1278 : : const uint32 flags; /* visibility flags, see below */
1279 : : } pgsql_thing_t;
1280 : :
1281 : : #define THING_NO_CREATE (1 << 0) /* should not show up after CREATE */
1282 : : #define THING_NO_DROP (1 << 1) /* should not show up after DROP */
1283 : : #define THING_NO_ALTER (1 << 2) /* should not show up after ALTER */
1284 : : #define THING_NO_SHOW (THING_NO_CREATE | THING_NO_DROP | THING_NO_ALTER)
1285 : :
1286 : : /* When we have DROP USER etc, also offer MAPPING FOR */
1287 : : static const char *const Keywords_for_user_thing[] = {
1288 : : "MAPPING FOR",
1289 : : NULL
1290 : : };
1291 : :
1292 : : static const pgsql_thing_t words_after_create[] = {
1293 : : {"ACCESS METHOD", NULL, NULL, NULL, NULL, THING_NO_ALTER},
1294 : : {"AGGREGATE", NULL, NULL, Query_for_list_of_aggregates},
1295 : : {"CAST", NULL, NULL, NULL}, /* Casts have complex structures for names, so
1296 : : * skip it */
1297 : : {"COLLATION", NULL, NULL, &Query_for_list_of_collations},
1298 : :
1299 : : /*
1300 : : * CREATE CONSTRAINT TRIGGER is not supported here because it is designed
1301 : : * to be used only by pg_dump.
1302 : : */
1303 : : {"CONFIGURATION", NULL, NULL, &Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW},
1304 : : {"CONVERSION", "SELECT conname FROM pg_catalog.pg_conversion WHERE conname LIKE '%s'"},
1305 : : {"DATABASE", Query_for_list_of_databases},
1306 : : {"DEFAULT PRIVILEGES", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1307 : : {"DICTIONARY", NULL, NULL, &Query_for_list_of_ts_dictionaries, NULL, THING_NO_SHOW},
1308 : : {"DOMAIN", NULL, NULL, &Query_for_list_of_domains},
1309 : : {"EVENT TRIGGER", NULL, NULL, NULL},
1310 : : {"EXTENSION", Query_for_list_of_extensions},
1311 : : {"FOREIGN DATA WRAPPER", NULL, NULL, NULL},
1312 : : {"FOREIGN TABLE", NULL, NULL, NULL},
1313 : : {"FUNCTION", NULL, NULL, Query_for_list_of_functions},
1314 : : {"GROUP", Query_for_list_of_roles},
1315 : : {"INDEX", NULL, NULL, &Query_for_list_of_indexes},
1316 : : {"LANGUAGE", Query_for_list_of_languages},
1317 : : {"LARGE OBJECT", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1318 : : {"MATERIALIZED VIEW", NULL, NULL, &Query_for_list_of_matviews},
1319 : : {"OPERATOR", NULL, NULL, NULL}, /* Querying for this is probably not such
1320 : : * a good idea. */
1321 : : {"OR REPLACE", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER},
1322 : : {"OWNED", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_ALTER}, /* for DROP OWNED BY ... */
1323 : : {"PARSER", NULL, NULL, &Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW},
1324 : : {"POLICY", NULL, NULL, NULL},
1325 : : {"PROCEDURE", NULL, NULL, Query_for_list_of_procedures},
1326 : : {"PUBLICATION", Query_for_list_of_publications},
1327 : : {"ROLE", Query_for_list_of_roles},
1328 : : {"ROUTINE", NULL, NULL, &Query_for_list_of_routines, NULL, THING_NO_CREATE},
1329 : : {"RULE", "SELECT rulename FROM pg_catalog.pg_rules WHERE rulename LIKE '%s'"},
1330 : : {"SCHEMA", Query_for_list_of_schemas},
1331 : : {"SEQUENCE", NULL, NULL, &Query_for_list_of_sequences},
1332 : : {"SERVER", Query_for_list_of_servers},
1333 : : {"STATISTICS", NULL, NULL, &Query_for_list_of_statistics},
1334 : : {"SUBSCRIPTION", Query_for_list_of_subscriptions},
1335 : : {"SYSTEM", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1336 : : {"TABLE", NULL, NULL, &Query_for_list_of_tables},
1337 : : {"TABLESPACE", Query_for_list_of_tablespaces},
1338 : : {"TEMP", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMP TABLE
1339 : : * ... */
1340 : : {"TEMPLATE", NULL, NULL, &Query_for_list_of_ts_templates, NULL, THING_NO_SHOW},
1341 : : {"TEMPORARY", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMPORARY
1342 : : * TABLE ... */
1343 : : {"TEXT SEARCH", NULL, NULL, NULL},
1344 : : {"TRANSFORM", NULL, NULL, NULL, NULL, THING_NO_ALTER},
1345 : : {"TRIGGER", "SELECT tgname FROM pg_catalog.pg_trigger WHERE tgname LIKE '%s' AND NOT tgisinternal"},
1346 : : {"TYPE", NULL, NULL, &Query_for_list_of_datatypes},
1347 : : {"UNIQUE", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNIQUE
1348 : : * INDEX ... */
1349 : : {"UNLOGGED", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNLOGGED
1350 : : * TABLE ... */
1351 : : {"USER", Query_for_list_of_roles, NULL, NULL, Keywords_for_user_thing},
1352 : : {"USER MAPPING FOR", NULL, NULL, NULL},
1353 : : {"VIEW", NULL, NULL, &Query_for_list_of_views},
1354 : : {NULL} /* end of list */
1355 : : };
1356 : :
1357 : : /*
1358 : : * The tcpatterns[] table provides the initial pattern-match rule for each
1359 : : * switch case in match_previous_words(). The contents of the table
1360 : : * are constructed by gen_tabcomplete.pl.
1361 : : */
1362 : :
1363 : : /* Basic match rules appearing in tcpatterns[].kind */
1364 : : enum TCPatternKind
1365 : : {
1366 : : Match,
1367 : : MatchCS,
1368 : : HeadMatch,
1369 : : HeadMatchCS,
1370 : : TailMatch,
1371 : : TailMatchCS,
1372 : : };
1373 : :
1374 : : /* Things besides string literals that can appear in tcpatterns[].words */
1375 : : #define MatchAny NULL
1376 : : #define MatchAnyExcept(pattern) ("!" pattern)
1377 : : #define MatchAnyN ""
1378 : :
1379 : : /* One entry in tcpatterns[] */
1380 : : typedef struct
1381 : : {
1382 : : int id; /* case label used in match_previous_words */
1383 : : enum TCPatternKind kind; /* match kind, see above */
1384 : : int nwords; /* length of words[] array */
1385 : : const char *const *words; /* array of match words */
1386 : : } TCPattern;
1387 : :
1388 : : /* Macro emitted by gen_tabcomplete.pl to fill a tcpatterns[] entry */
1389 : : #define TCPAT(id, kind, ...) \
1390 : : { (id), (kind), VA_ARGS_NARGS(__VA_ARGS__), \
1391 : : (const char * const []) { __VA_ARGS__ } }
1392 : :
1393 : : #ifdef SWITCH_CONVERSION_APPLIED
1394 : :
1395 : : static const TCPattern tcpatterns[] =
1396 : : {
1397 : : /* Insert tab-completion pattern data here. */
1398 : : };
1399 : :
1400 : : #endif /* SWITCH_CONVERSION_APPLIED */
1401 : :
1402 : : /* Storage parameters for CREATE TABLE and ALTER TABLE */
1403 : : static const char *const table_storage_parameters[] = {
1404 : : "autovacuum_analyze_scale_factor",
1405 : : "autovacuum_analyze_threshold",
1406 : : "autovacuum_enabled",
1407 : : "autovacuum_freeze_max_age",
1408 : : "autovacuum_freeze_min_age",
1409 : : "autovacuum_freeze_table_age",
1410 : : "autovacuum_multixact_freeze_max_age",
1411 : : "autovacuum_multixact_freeze_min_age",
1412 : : "autovacuum_multixact_freeze_table_age",
1413 : : "autovacuum_parallel_workers",
1414 : : "autovacuum_vacuum_cost_delay",
1415 : : "autovacuum_vacuum_cost_limit",
1416 : : "autovacuum_vacuum_insert_scale_factor",
1417 : : "autovacuum_vacuum_insert_threshold",
1418 : : "autovacuum_vacuum_max_threshold",
1419 : : "autovacuum_vacuum_scale_factor",
1420 : : "autovacuum_vacuum_threshold",
1421 : : "fillfactor",
1422 : : "log_autovacuum_min_duration",
1423 : : "log_autoanalyze_min_duration",
1424 : : "parallel_workers",
1425 : : "toast.autovacuum_enabled",
1426 : : "toast.autovacuum_freeze_max_age",
1427 : : "toast.autovacuum_freeze_min_age",
1428 : : "toast.autovacuum_freeze_table_age",
1429 : : "toast.autovacuum_multixact_freeze_max_age",
1430 : : "toast.autovacuum_multixact_freeze_min_age",
1431 : : "toast.autovacuum_multixact_freeze_table_age",
1432 : : "toast.autovacuum_vacuum_cost_delay",
1433 : : "toast.autovacuum_vacuum_cost_limit",
1434 : : "toast.autovacuum_vacuum_insert_scale_factor",
1435 : : "toast.autovacuum_vacuum_insert_threshold",
1436 : : "toast.autovacuum_vacuum_max_threshold",
1437 : : "toast.autovacuum_vacuum_scale_factor",
1438 : : "toast.autovacuum_vacuum_threshold",
1439 : : "toast.log_autovacuum_min_duration",
1440 : : "toast.vacuum_index_cleanup",
1441 : : "toast.vacuum_max_eager_freeze_failure_rate",
1442 : : "toast.vacuum_truncate",
1443 : : "toast_tuple_target",
1444 : : "toast_value_type",
1445 : : "user_catalog_table",
1446 : : "vacuum_index_cleanup",
1447 : : "vacuum_max_eager_freeze_failure_rate",
1448 : : "vacuum_truncate",
1449 : : NULL
1450 : : };
1451 : :
1452 : : /* Optional parameters for CREATE VIEW and ALTER VIEW */
1453 : : static const char *const view_optional_parameters[] = {
1454 : : "check_option",
1455 : : "security_barrier",
1456 : : "security_invoker",
1457 : : NULL
1458 : : };
1459 : :
1460 : : /* Forward declaration of functions */
1461 : : static char **psql_completion(const char *text, int start, int end);
1462 : : static char **match_previous_words(int pattern_id,
1463 : : const char *text, int start, int end,
1464 : : char **previous_words,
1465 : : int previous_words_count);
1466 : : static char *create_command_generator(const char *text, int state);
1467 : : static char *drop_command_generator(const char *text, int state);
1468 : : static char *alter_command_generator(const char *text, int state);
1469 : : static char *complete_from_query(const char *text, int state);
1470 : : static char *complete_from_versioned_query(const char *text, int state);
1471 : : static char *complete_from_schema_query(const char *text, int state);
1472 : : static char *complete_from_versioned_schema_query(const char *text, int state);
1473 : : static char *_complete_from_query(const char *simple_query,
1474 : : const SchemaQuery *schema_query,
1475 : : const char *const *keywords,
1476 : : bool verbatim,
1477 : : const char *text, int state);
1478 : : static void set_completion_reference(const char *word);
1479 : : static void set_completion_reference_verbatim(const char *word);
1480 : : static char *complete_from_list(const char *text, int state);
1481 : : static char *complete_from_const(const char *text, int state);
1482 : : static void append_variable_names(char ***varnames, int *nvars,
1483 : : int *maxvars, const char *varname,
1484 : : const char *prefix, const char *suffix);
1485 : : static char **complete_from_variables(const char *text,
1486 : : const char *prefix, const char *suffix, bool need_value);
1487 : : static char *complete_from_files(const char *text, int state);
1488 : : static char *_complete_from_files(const char *text, int state);
1489 : :
1490 : : static char *pg_strdup_keyword_case(const char *s, const char *ref);
1491 : : static char *escape_string(const char *text);
1492 : : static char *make_like_pattern(const char *word);
1493 : : static void parse_identifier(const char *ident,
1494 : : char **schemaname, char **objectname,
1495 : : bool *schemaquoted, bool *objectquoted);
1496 : : static char *requote_identifier(const char *schemaname, const char *objectname,
1497 : : bool quote_schema, bool quote_object);
1498 : : static bool identifier_needs_quotes(const char *ident);
1499 : : static PGresult *exec_query(const char *query);
1500 : :
1501 : : static char **get_previous_words(int point, char **buffer, int *nwords);
1502 : :
1503 : : static char *get_guctype(const char *varname);
1504 : :
1505 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
1506 : : static char *quote_file_name(char *fname, int match_type, char *quote_pointer);
1507 : : static char *dequote_file_name(char *fname, int quote_char);
1508 : : #endif
1509 : :
1510 : :
1511 : : /*
1512 : : * Initialize the readline library for our purposes.
1513 : : */
1514 : : void
1515 : 3 : initialize_readline(void)
1516 : : {
1517 : 3 : rl_readline_name = unconstify(char *, pset.progname);
1518 : 3 : rl_attempted_completion_function = psql_completion;
1519 : :
1520 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
1521 : 3 : rl_filename_quoting_function = quote_file_name;
1522 : 3 : rl_filename_dequoting_function = dequote_file_name;
1523 : : #endif
1524 : :
1525 : 3 : rl_basic_word_break_characters = WORD_BREAKS;
1526 : :
1527 : : /*
1528 : : * Ideally we'd include '"' in rl_completer_quote_characters too, which
1529 : : * should allow us to complete quoted identifiers that include spaces.
1530 : : * However, the library support for rl_completer_quote_characters is
1531 : : * presently too inconsistent to want to mess with that. (Note in
1532 : : * particular that libedit has this variable but completely ignores it.)
1533 : : */
1534 : 3 : rl_completer_quote_characters = "'";
1535 : :
1536 : : /*
1537 : : * Set rl_filename_quote_characters to "all possible characters",
1538 : : * otherwise Readline will skip filename quoting if it thinks a filename
1539 : : * doesn't need quoting. Readline actually interprets this as bytes, so
1540 : : * there are no encoding considerations here.
1541 : : */
1542 : : #ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS
1543 : : {
1544 : 3 : unsigned char *fqc = (unsigned char *) pg_malloc(256);
1545 : :
1546 [ + + ]: 768 : for (int i = 0; i < 255; i++)
1547 : 765 : fqc[i] = (unsigned char) (i + 1);
1548 : 3 : fqc[255] = '\0';
1549 : 3 : rl_filename_quote_characters = (const char *) fqc;
1550 : : }
1551 : : #endif
1552 : :
1553 : 3 : completion_max_records = 1000;
1554 : :
1555 : : /*
1556 : : * There is a variable rl_completion_query_items for this but apparently
1557 : : * it's not defined everywhere.
1558 : : */
1559 : 3 : }
1560 : :
1561 : : /*
1562 : : * Check if 'word' matches any of the '|'-separated strings in 'pattern',
1563 : : * using case-insensitive or case-sensitive comparisons.
1564 : : *
1565 : : * If pattern is NULL, it's a wild card that matches any word.
1566 : : * If pattern begins with '!', the result is negated, ie we check that 'word'
1567 : : * does *not* match any alternative appearing in the rest of 'pattern'.
1568 : : * Any alternative can contain '*' which is a wild card, i.e., it can match
1569 : : * any substring; however, we allow at most one '*' per alternative.
1570 : : *
1571 : : * For readability, callers should use the macros MatchAny and MatchAnyExcept
1572 : : * to invoke those two special cases for 'pattern'. (But '|' and '*' must
1573 : : * just be written directly in patterns.) There is also MatchAnyN, but that
1574 : : * is supported only in Matches/MatchesCS and is not handled here.
1575 : : */
1576 : : static bool
1577 : 8006 : word_matches(const char *pattern,
1578 : : const char *word,
1579 : : bool case_sensitive)
1580 : : {
1581 : : size_t wordlen;
1582 : :
1583 : : #define cimatch(s1, s2, n) \
1584 : : (case_sensitive ? strncmp(s1, s2, n) == 0 : pg_strncasecmp(s1, s2, n) == 0)
1585 : :
1586 : : /* NULL pattern matches anything. */
1587 [ + + ]: 8006 : if (pattern == NULL)
1588 : 179 : return true;
1589 : :
1590 : : /* Handle negated patterns from the MatchAnyExcept macro. */
1591 [ + + ]: 7827 : if (*pattern == '!')
1592 : 2 : return !word_matches(pattern + 1, word, case_sensitive);
1593 : :
1594 : : /* Else consider each alternative in the pattern. */
1595 : 7825 : wordlen = strlen(word);
1596 : : for (;;)
1597 : 805 : {
1598 : 8630 : const char *star = NULL;
1599 : : const char *c;
1600 : :
1601 : : /* Find end of current alternative, and locate any wild card. */
1602 : 8630 : c = pattern;
1603 [ + + + + ]: 57665 : while (*c != '\0' && *c != '|')
1604 : : {
1605 [ + + ]: 49035 : if (*c == '*')
1606 : 409 : star = c;
1607 : 49035 : c++;
1608 : : }
1609 : : /* Was there a wild card? */
1610 [ + + ]: 8630 : if (star)
1611 : : {
1612 : : /* Yes, wildcard match? */
1613 : 409 : size_t beforelen = star - pattern,
1614 : 409 : afterlen = c - star - 1;
1615 : :
1616 [ + + + + : 809 : if (wordlen >= (beforelen + afterlen) &&
+ + ]
1617 [ + + + - ]: 414 : cimatch(word, pattern, beforelen) &&
1618 : 7 : cimatch(word + wordlen - afterlen, star + 1, afterlen))
1619 : 7 : return true;
1620 : : }
1621 : : else
1622 : : {
1623 : : /* No, plain match? */
1624 [ + + + + : 10462 : if (wordlen == (c - pattern) &&
+ + ]
1625 : 2241 : cimatch(word, pattern, wordlen))
1626 : 1255 : return true;
1627 : : }
1628 : : /* Out of alternatives? */
1629 [ + + ]: 7368 : if (*c == '\0')
1630 : 6563 : break;
1631 : : /* Nope, try next alternative. */
1632 : 805 : pattern = c + 1;
1633 : : }
1634 : :
1635 : 6563 : return false;
1636 : : }
1637 : :
1638 : : /*
1639 : : * Implementation of TailMatches and TailMatchesCS tests: do the last N words
1640 : : * in previous_words match the pattern arguments?
1641 : : *
1642 : : * The array indexing might look backwards, but remember that
1643 : : * previous_words[0] contains the *last* word on the line, not the first.
1644 : : */
1645 : : static bool
1646 : 7393 : TailMatchesArray(bool case_sensitive,
1647 : : int previous_words_count, char **previous_words,
1648 : : int narg, const char *const *args)
1649 : : {
1650 [ + + ]: 7393 : if (previous_words_count < narg)
1651 : 5267 : return false;
1652 : :
1653 [ + + ]: 2231 : for (int argno = 0; argno < narg; argno++)
1654 : : {
1655 : 2194 : const char *arg = args[argno];
1656 : :
1657 [ + + ]: 2194 : if (!word_matches(arg, previous_words[narg - argno - 1],
1658 : : case_sensitive))
1659 : 2089 : return false;
1660 : : }
1661 : :
1662 : 37 : return true;
1663 : : }
1664 : :
1665 : : /*
1666 : : * As above, but the pattern is passed as a variadic argument list.
1667 : : */
1668 : : static bool
1669 : 38 : TailMatchesImpl(bool case_sensitive,
1670 : : int previous_words_count, char **previous_words,
1671 : : int narg, ...)
1672 : : {
1673 : : const char *argarray[64];
1674 : : va_list args;
1675 : :
1676 : : Assert(narg <= lengthof(argarray));
1677 : :
1678 [ + + ]: 38 : if (previous_words_count < narg)
1679 : 12 : return false;
1680 : :
1681 : 26 : va_start(args, narg);
1682 [ + + ]: 64 : for (int argno = 0; argno < narg; argno++)
1683 : 38 : argarray[argno] = va_arg(args, const char *);
1684 : 26 : va_end(args);
1685 : :
1686 : 26 : return TailMatchesArray(case_sensitive,
1687 : : previous_words_count, previous_words,
1688 : : narg, argarray);
1689 : : }
1690 : :
1691 : : /*
1692 : : * Implementation of HeadMatches and HeadMatchesCS tests: do the first N
1693 : : * words in previous_words match the pattern arguments?
1694 : : */
1695 : : static bool
1696 : 4957 : HeadMatchesArray(bool case_sensitive,
1697 : : int previous_words_count, char **previous_words,
1698 : : int narg, const char *const *args)
1699 : : {
1700 [ + + ]: 4957 : if (previous_words_count < narg)
1701 : 450 : return false;
1702 : :
1703 [ + + ]: 5845 : for (int argno = 0; argno < narg; argno++)
1704 : : {
1705 : 5810 : const char *arg = args[argno];
1706 : :
1707 [ + + ]: 5810 : if (!word_matches(arg, previous_words[previous_words_count - argno - 1],
1708 : : case_sensitive))
1709 : 4472 : return false;
1710 : : }
1711 : :
1712 : 35 : return true;
1713 : : }
1714 : :
1715 : : /*
1716 : : * As above, but the pattern is passed as a variadic argument list.
1717 : : */
1718 : : static bool
1719 : 10 : HeadMatchesImpl(bool case_sensitive,
1720 : : int previous_words_count, char **previous_words,
1721 : : int narg, ...)
1722 : : {
1723 : : const char *argarray[64];
1724 : : va_list args;
1725 : :
1726 : : Assert(narg <= lengthof(argarray));
1727 : :
1728 [ + + ]: 10 : if (previous_words_count < narg)
1729 : 2 : return false;
1730 : :
1731 : 8 : va_start(args, narg);
1732 [ + + ]: 25 : for (int argno = 0; argno < narg; argno++)
1733 : 17 : argarray[argno] = va_arg(args, const char *);
1734 : 8 : va_end(args);
1735 : :
1736 : 8 : return HeadMatchesArray(case_sensitive,
1737 : : previous_words_count, previous_words,
1738 : : narg, argarray);
1739 : : }
1740 : :
1741 : : /*
1742 : : * Implementation of Matches and MatchesCS tests: do all of the words
1743 : : * in previous_words match the pattern arguments?
1744 : : *
1745 : : * This supports an additional kind of wildcard: MatchAnyN (represented as "")
1746 : : * can match any number of words, including zero, in the middle of the list.
1747 : : */
1748 : : static bool
1749 : 27581 : MatchesArray(bool case_sensitive,
1750 : : int previous_words_count, char **previous_words,
1751 : : int narg, const char *const *args)
1752 : : {
1753 : 27581 : int match_any_pos = -1;
1754 : :
1755 : : /* Even with MatchAnyN, there must be at least N-1 words */
1756 [ + + ]: 27581 : if (previous_words_count < narg - 1)
1757 : 15455 : return false;
1758 : :
1759 : : /* Check for MatchAnyN */
1760 [ + + ]: 48789 : for (int argno = 0; argno < narg; argno++)
1761 : : {
1762 : 37302 : const char *arg = args[argno];
1763 : :
1764 [ + + + + ]: 37302 : if (arg != NULL && arg[0] == '\0')
1765 : : {
1766 : 639 : match_any_pos = argno;
1767 : 639 : break;
1768 : : }
1769 : : }
1770 : :
1771 [ + + ]: 12126 : if (match_any_pos < 0)
1772 : : {
1773 : : /* Standard case without MatchAnyN */
1774 [ + + ]: 11487 : if (previous_words_count != narg)
1775 : 7948 : return false;
1776 : :
1777 : : /* Either Head or Tail match will do for the rest */
1778 [ + + ]: 3539 : if (!HeadMatchesArray(case_sensitive,
1779 : : previous_words_count, previous_words,
1780 : : narg, args))
1781 : 3512 : return false;
1782 : : }
1783 : : else
1784 : : {
1785 : : /* Match against head */
1786 [ + - ]: 639 : if (!HeadMatchesArray(case_sensitive,
1787 : : previous_words_count, previous_words,
1788 : : match_any_pos, args))
1789 : 639 : return false;
1790 : :
1791 : : /* Match against tail */
1792 [ # # ]: 0 : if (!TailMatchesArray(case_sensitive,
1793 : : previous_words_count, previous_words,
1794 : 0 : narg - match_any_pos - 1,
1795 : 0 : args + match_any_pos + 1))
1796 : 0 : return false;
1797 : : }
1798 : :
1799 : 27 : return true;
1800 : : }
1801 : :
1802 : : /*
1803 : : * As above, but the pattern is passed as a variadic argument list.
1804 : : */
1805 : : static bool
1806 : 14 : MatchesImpl(bool case_sensitive,
1807 : : int previous_words_count, char **previous_words,
1808 : : int narg, ...)
1809 : : {
1810 : : const char *argarray[64];
1811 : : va_list args;
1812 : :
1813 : : Assert(narg <= lengthof(argarray));
1814 : :
1815 : : /* Even with MatchAnyN, there must be at least N-1 words */
1816 [ - + ]: 14 : if (previous_words_count < narg - 1)
1817 : 0 : return false;
1818 : :
1819 : 14 : va_start(args, narg);
1820 [ + + ]: 56 : for (int argno = 0; argno < narg; argno++)
1821 : 42 : argarray[argno] = va_arg(args, const char *);
1822 : 14 : va_end(args);
1823 : :
1824 : 14 : return MatchesArray(case_sensitive,
1825 : : previous_words_count, previous_words,
1826 : : narg, argarray);
1827 : : }
1828 : :
1829 : : /*
1830 : : * Check if the final character of 's' is 'c'.
1831 : : */
1832 : : static bool
1833 : 3 : ends_with(const char *s, char c)
1834 : : {
1835 : 3 : size_t length = strlen(s);
1836 : :
1837 [ + - + - ]: 3 : return (length > 0 && s[length - 1] == c);
1838 : : }
1839 : :
1840 : : /*
1841 : : * The completion function.
1842 : : *
1843 : : * According to readline spec this gets passed the text entered so far and its
1844 : : * start and end positions in the readline buffer. The return value is some
1845 : : * partially obscure list format that can be generated by readline's
1846 : : * rl_completion_matches() function, so we don't have to worry about it.
1847 : : */
1848 : : static char **
1849 : 67 : psql_completion(const char *text, int start, int end)
1850 : : {
1851 : : /* This is the variable we'll return. */
1852 : 67 : char **matches = NULL;
1853 : :
1854 : : /* Workspace for parsed words. */
1855 : : char *words_buffer;
1856 : :
1857 : : /* This array will contain pointers to parsed words. */
1858 : : char **previous_words;
1859 : :
1860 : : /* The number of words found on the input line. */
1861 : : int previous_words_count;
1862 : :
1863 : : /*
1864 : : * For compactness, we use these macros to reference previous_words[].
1865 : : * Caution: do not access a previous_words[] entry without having checked
1866 : : * previous_words_count to be sure it's valid. In most cases below, that
1867 : : * check is implicit in a TailMatches() or similar macro, but in some
1868 : : * places we have to check it explicitly.
1869 : : */
1870 : : #define prev_wd (previous_words[0])
1871 : : #define prev2_wd (previous_words[1])
1872 : : #define prev3_wd (previous_words[2])
1873 : : #define prev4_wd (previous_words[3])
1874 : : #define prev5_wd (previous_words[4])
1875 : : #define prev6_wd (previous_words[5])
1876 : : #define prev7_wd (previous_words[6])
1877 : : #define prev8_wd (previous_words[7])
1878 : : #define prev9_wd (previous_words[8])
1879 : :
1880 : : /* Match the last N words before point, case-insensitively. */
1881 : : #define TailMatches(...) \
1882 : : TailMatchesImpl(false, previous_words_count, previous_words, \
1883 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1884 : :
1885 : : /* Match the last N words before point, case-sensitively. */
1886 : : #define TailMatchesCS(...) \
1887 : : TailMatchesImpl(true, previous_words_count, previous_words, \
1888 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1889 : :
1890 : : /* Match N words representing all of the line, case-insensitively. */
1891 : : #define Matches(...) \
1892 : : MatchesImpl(false, previous_words_count, previous_words, \
1893 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1894 : :
1895 : : /* Match N words representing all of the line, case-sensitively. */
1896 : : #define MatchesCS(...) \
1897 : : MatchesImpl(true, previous_words_count, previous_words, \
1898 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1899 : :
1900 : : /* Match the first N words on the line, case-insensitively. */
1901 : : #define HeadMatches(...) \
1902 : : HeadMatchesImpl(false, previous_words_count, previous_words, \
1903 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1904 : :
1905 : : /* Match the first N words on the line, case-sensitively. */
1906 : : #define HeadMatchesCS(...) \
1907 : : HeadMatchesImpl(true, previous_words_count, previous_words, \
1908 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1909 : :
1910 : : /* psql's backslash commands. */
1911 : : static const char *const backslash_commands[] = {
1912 : : "\\a",
1913 : : "\\bind", "\\bind_named",
1914 : : "\\connect", "\\conninfo", "\\C", "\\cd", "\\close_prepared", "\\copy",
1915 : : "\\copyright", "\\crosstabview",
1916 : : "\\d", "\\da", "\\dA", "\\dAc", "\\dAf", "\\dAo", "\\dAp",
1917 : : "\\db", "\\dc", "\\dconfig", "\\dC", "\\dd", "\\ddp", "\\dD",
1918 : : "\\des", "\\det", "\\deu", "\\dew", "\\dE", "\\df",
1919 : : "\\dF", "\\dFd", "\\dFp", "\\dFt", "\\dg", "\\di", "\\dl", "\\dL",
1920 : : "\\dm", "\\dn", "\\do", "\\dO", "\\dp", "\\dP", "\\dPi", "\\dPt",
1921 : : "\\drds", "\\drg", "\\dRs", "\\dRp", "\\ds",
1922 : : "\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dX", "\\dy",
1923 : : "\\echo", "\\edit", "\\ef", "\\elif", "\\else", "\\encoding",
1924 : : "\\endif", "\\endpipeline", "\\errverbose", "\\ev",
1925 : : "\\f", "\\flush", "\\flushrequest",
1926 : : "\\g", "\\gdesc", "\\getenv", "\\getresults", "\\gexec", "\\gset", "\\gx",
1927 : : "\\help", "\\html",
1928 : : "\\if", "\\include", "\\include_relative", "\\ir",
1929 : : "\\list", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
1930 : : "\\out",
1931 : : "\\parse", "\\password", "\\print", "\\prompt", "\\pset",
1932 : : "\\qecho", "\\quit",
1933 : : "\\reset", "\\restrict",
1934 : : "\\s", "\\sendpipeline", "\\set", "\\setenv", "\\sf",
1935 : : "\\startpipeline", "\\sv", "\\syncpipeline",
1936 : : "\\t", "\\T", "\\timing",
1937 : : "\\unrestrict", "\\unset",
1938 : : "\\x",
1939 : : "\\warn", "\\watch", "\\write",
1940 : : "\\z",
1941 : : "\\!", "\\?",
1942 : : NULL
1943 : : };
1944 : :
1945 : : /*
1946 : : * Temporary workaround for a bug in recent (2019) libedit: it incorrectly
1947 : : * de-escapes the input "text", causing us to fail to recognize backslash
1948 : : * commands. So get the string to look at from rl_line_buffer instead.
1949 : : */
1950 : 67 : char *text_copy = pnstrdup(rl_line_buffer + start, end - start);
1951 : 67 : text = text_copy;
1952 : :
1953 : : /* Remember last char of the given input word. */
1954 [ + + ]: 67 : completion_last_char = (end > start) ? text[end - start - 1] : '\0';
1955 : :
1956 : : /* We usually want the append character to be a space. */
1957 : 67 : rl_completion_append_character = ' ';
1958 : :
1959 : : /* Clear a few things. */
1960 : 67 : completion_charp = NULL;
1961 : 67 : completion_charpp = NULL;
1962 : 67 : completion_vquery = NULL;
1963 : 67 : completion_squery = NULL;
1964 : 67 : completion_ref_object = NULL;
1965 : 67 : completion_ref_schema = NULL;
1966 : :
1967 : : /*
1968 : : * Scan the input line to extract the words before our current position.
1969 : : * According to those we'll make some smart decisions on what the user is
1970 : : * probably intending to type.
1971 : : */
1972 : 67 : previous_words = get_previous_words(start,
1973 : : &words_buffer,
1974 : : &previous_words_count);
1975 : :
1976 : : /* If current word is a backslash command, offer completions for that */
1977 [ + + ]: 67 : if (text[0] == '\\')
1978 : 1 : COMPLETE_WITH_LIST_CS(backslash_commands);
1979 : :
1980 : : /* If current word is a variable interpolation, handle that case */
1981 [ + + + - ]: 66 : else if (text[0] == ':' && text[1] != ':')
1982 : : {
1983 [ - + ]: 2 : if (text[1] == '\'')
1984 : 0 : matches = complete_from_variables(text, ":'", "'", true);
1985 [ - + ]: 2 : else if (text[1] == '"')
1986 : 0 : matches = complete_from_variables(text, ":\"", "\"", true);
1987 [ + + + - ]: 2 : else if (text[1] == '{' && text[2] == '?')
1988 : 1 : matches = complete_from_variables(text, ":{?", "}", true);
1989 : : else
1990 : 1 : matches = complete_from_variables(text, ":", "", true);
1991 : : }
1992 : :
1993 : : /* If no previous word, suggest one of the basic sql commands */
1994 [ + + ]: 64 : else if (previous_words_count == 0)
1995 : 2 : COMPLETE_WITH_LIST(sql_commands);
1996 : :
1997 : : /* Else try completions based on matching patterns of previous words */
1998 : : else
1999 : : {
2000 : : #ifdef SWITCH_CONVERSION_APPLIED
2001 : : /*
2002 : : * If we have transformed match_previous_words into a switch, iterate
2003 : : * through tcpatterns[] to see which pattern ids match.
2004 : : *
2005 : : * For now, we have to try the patterns in the order they are stored
2006 : : * (matching the order of switch cases in match_previous_words),
2007 : : * because some of the logic in match_previous_words assumes that
2008 : : * previous matches have been eliminated. This is fairly
2009 : : * unprincipled, and it is likely that there are undesirable as well
2010 : : * as desirable interactions hidden in the order of the pattern
2011 : : * checks. TODO: think about a better way to manage that.
2012 : : */
2013 [ + + ]: 35711 : for (size_t tindx = 0; tindx < lengthof(tcpatterns); tindx++)
2014 : : {
2015 : 35705 : const TCPattern *tcpat = tcpatterns + tindx;
2016 : 35705 : bool match = false;
2017 : :
2018 [ + - + + : 35705 : switch (tcpat->kind)
+ + - ]
2019 : : {
2020 : 27567 : case Match:
2021 : 27567 : match = MatchesArray(false,
2022 : : previous_words_count,
2023 : : previous_words,
2024 : 27567 : tcpat->nwords, tcpat->words);
2025 : 27567 : break;
2026 : 0 : case MatchCS:
2027 : 0 : match = MatchesArray(true,
2028 : : previous_words_count,
2029 : : previous_words,
2030 : 0 : tcpat->nwords, tcpat->words);
2031 : 0 : break;
2032 : 751 : case HeadMatch:
2033 : 751 : match = HeadMatchesArray(false,
2034 : : previous_words_count,
2035 : : previous_words,
2036 : 751 : tcpat->nwords, tcpat->words);
2037 : 751 : break;
2038 : 20 : case HeadMatchCS:
2039 : 20 : match = HeadMatchesArray(true,
2040 : : previous_words_count,
2041 : : previous_words,
2042 : 20 : tcpat->nwords, tcpat->words);
2043 : 20 : break;
2044 : 6768 : case TailMatch:
2045 : 6768 : match = TailMatchesArray(false,
2046 : : previous_words_count,
2047 : : previous_words,
2048 : 6768 : tcpat->nwords, tcpat->words);
2049 : 6768 : break;
2050 : 599 : case TailMatchCS:
2051 : 599 : match = TailMatchesArray(true,
2052 : : previous_words_count,
2053 : : previous_words,
2054 : 599 : tcpat->nwords, tcpat->words);
2055 : 599 : break;
2056 : : }
2057 [ + + ]: 35705 : if (match)
2058 : : {
2059 : 58 : matches = match_previous_words(tcpat->id, text, start, end,
2060 : : previous_words,
2061 : : previous_words_count);
2062 [ + + ]: 58 : if (matches != NULL)
2063 : 56 : break;
2064 : : }
2065 : : }
2066 : : #else /* !SWITCH_CONVERSION_APPLIED */
2067 : : /*
2068 : : * If gen_tabcomplete.pl hasn't been applied to this code, just let
2069 : : * match_previous_words scan through all its patterns.
2070 : : */
2071 : : matches = match_previous_words(0, text, start, end,
2072 : : previous_words,
2073 : : previous_words_count);
2074 : : #endif /* SWITCH_CONVERSION_APPLIED */
2075 : : }
2076 : :
2077 : : /*
2078 : : * Finally, we look through the list of "things", such as TABLE, INDEX and
2079 : : * check if that was the previous word. If so, execute the query to get a
2080 : : * list of them.
2081 : : */
2082 [ + + + - ]: 67 : if (matches == NULL && previous_words_count > 0)
2083 : : {
2084 : : const pgsql_thing_t *wac;
2085 : :
2086 [ + + ]: 248 : for (wac = words_after_create; wac->name != NULL; wac++)
2087 : : {
2088 [ + + ]: 246 : if (pg_strcasecmp(prev_wd, wac->name) == 0)
2089 : : {
2090 [ + + ]: 4 : if (wac->query)
2091 : 1 : COMPLETE_WITH_QUERY_LIST(wac->query,
2092 : : wac->keywords);
2093 [ - + ]: 3 : else if (wac->vquery)
2094 : 0 : COMPLETE_WITH_VERSIONED_QUERY_LIST(wac->vquery,
2095 : : wac->keywords);
2096 [ + - ]: 3 : else if (wac->squery)
2097 : 3 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(wac->squery,
2098 : : wac->keywords);
2099 : 4 : break;
2100 : : }
2101 : : }
2102 : : }
2103 : :
2104 : : /*
2105 : : * If we still don't have anything to match we have to fabricate some sort
2106 : : * of default list. If we were to just return NULL, readline automatically
2107 : : * attempts filename completion, and that's usually no good.
2108 : : */
2109 [ + + ]: 67 : if (matches == NULL)
2110 : : {
2111 : 2 : COMPLETE_WITH_CONST(true, "");
2112 : : /* Also, prevent Readline from appending stuff to the non-match */
2113 : 2 : rl_completion_append_character = '\0';
2114 : : #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
2115 : 2 : rl_completion_suppress_quote = 1;
2116 : : #endif
2117 : : }
2118 : :
2119 : : /* free storage */
2120 : 67 : pg_free(previous_words);
2121 : 67 : pg_free(words_buffer);
2122 : 67 : pfree(text_copy);
2123 : 67 : pg_free(completion_ref_object);
2124 : 67 : completion_ref_object = NULL;
2125 : 67 : pg_free(completion_ref_schema);
2126 : 67 : completion_ref_schema = NULL;
2127 : :
2128 : : /* Return our Grand List O' Matches */
2129 : 67 : return matches;
2130 : : }
2131 : :
2132 : : /*
2133 : : * Subroutine to try matches based on previous_words.
2134 : : *
2135 : : * This can operate in one of two modes. As presented, the body of the
2136 : : * function is a long if-else-if chain that sequentially tries each known
2137 : : * match rule. That works, but some C compilers have trouble with such a long
2138 : : * else-if chain, either taking extra time to compile or failing altogether.
2139 : : * Therefore, we prefer to transform the else-if chain into a switch, and then
2140 : : * each call of this function considers just one match rule (under control of
2141 : : * a loop in psql_completion()). Compilers tend to be more ready to deal
2142 : : * with many-arm switches than many-arm else-if chains.
2143 : : *
2144 : : * Each if-condition in this function must begin with a call of one of the
2145 : : * functions Matches, HeadMatches, TailMatches, MatchesCS, HeadMatchesCS, or
2146 : : * TailMatchesCS. The preprocessor gen_tabcomplete.pl strips out those
2147 : : * calls and converts them into entries in tcpatterns[], which are evaluated
2148 : : * by the calling loop in psql_completion(). Successful matches result in
2149 : : * calls to this function with the appropriate pattern_id, causing just the
2150 : : * corresponding switch case to be executed.
2151 : : *
2152 : : * If-conditions in this function can be more complex than a single *Matches
2153 : : * function call in one of two ways (but not both!). They can be OR's
2154 : : * of *Matches calls, such as
2155 : : * else if (Matches("ALTER", "VIEW", MatchAny, "ALTER", MatchAny) ||
2156 : : * Matches("ALTER", "VIEW", MatchAny, "ALTER", "COLUMN", MatchAny))
2157 : : * or they can be a *Matches call AND'ed with some other condition, e.g.
2158 : : * else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE", MatchAny) &&
2159 : : * !ends_with(prev_wd, ','))
2160 : : * The former case is transformed into multiple tcpatterns[] entries and
2161 : : * multiple case labels for the same bit of code. The latter case is
2162 : : * transformed into a case label and a contained if-statement.
2163 : : *
2164 : : * This is split out of psql_completion() primarily to separate code that
2165 : : * gen_tabcomplete.pl should process from code that it should not, although
2166 : : * doing so also helps to avoid extra indentation of this code.
2167 : : *
2168 : : * Returns a matches list, or NULL if no match.
2169 : : */
2170 : : static char **
2171 : 58 : match_previous_words(int pattern_id,
2172 : : const char *text, int start, int end,
2173 : : char **previous_words, int previous_words_count)
2174 : : {
2175 : : /* This is the variable we'll return. */
2176 : 58 : char **matches = NULL;
2177 : :
2178 : : /* Dummy statement, allowing all the match rules to look like "else if" */
2179 : : if (0)
2180 : : {
2181 : : /* skip */
2182 : : }
2183 : :
2184 : : /* gen_tabcomplete.pl begins special processing here */
2185 [ + - + - : 58 : /* BEGIN GEN_TABCOMPLETE */
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - -
+ - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - - -
- + + - -
+ - ]
2186 : :
2187 : : /* CREATE */
2188 : :
2189 : : /*
2190 : : * Complete with something you can create. If CREATE is a privilege name,
2191 : : * leave it to the GRANT/REVOKE rules below.
2192 : : */
2193 : 1 : else if (TailMatches("CREATE") &&
2194 : : !TailMatches("GRANT|REVOKE", "CREATE") &&
2195 [ + - + - ]: 1 : !TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "CREATE"))
2196 : : {
2197 : : /* only some object types can be created as part of CREATE SCHEMA */
2198 [ - + ]: 1 : if (HeadMatches("CREATE", "SCHEMA"))
2199 : 0 : COMPLETE_WITH("TABLE", "VIEW", "INDEX", "SEQUENCE", "TRIGGER",
2200 : : /* for INDEX and TABLE/SEQUENCE, respectively */
2201 : : "UNIQUE", "UNLOGGED");
2202 : : else
2203 : 1 : COMPLETE_WITH_GENERATOR(create_command_generator);
2204 : : }
2205 : : /* complete with something you can create or replace */
2206 : 1 : else if (TailMatches("CREATE", "OR", "REPLACE"))
2207 : 0 : COMPLETE_WITH("FUNCTION", "PROCEDURE", "LANGUAGE", "RULE", "VIEW",
2208 : : "AGGREGATE", "TRANSFORM", "TRIGGER");
2209 : :
2210 : : /* DROP, but not DROP embedded in other commands */
2211 : : /* complete with something you can drop */
2212 : 0 : else if (Matches("DROP"))
2213 : 1 : COMPLETE_WITH_GENERATOR(drop_command_generator);
2214 : :
2215 : : /* ALTER */
2216 : :
2217 : : /* ALTER TABLE */
2218 : 1 : else if (Matches("ALTER", "TABLE"))
2219 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
2220 : : "ALL IN TABLESPACE");
2221 : :
2222 : : /* ALTER something */
2223 : 0 : else if (Matches("ALTER"))
2224 : 0 : COMPLETE_WITH_GENERATOR(alter_command_generator);
2225 : : /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx */
2226 : 0 : else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny))
2227 : 0 : COMPLETE_WITH("SET TABLESPACE", "OWNED BY");
2228 : : /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY */
2229 : 0 : else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY"))
2230 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2231 : : /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY xxx */
2232 : 0 : else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY", MatchAny))
2233 : 0 : COMPLETE_WITH("SET TABLESPACE");
2234 : : /* ALTER AGGREGATE,FUNCTION,PROCEDURE,ROUTINE <name> */
2235 : 0 : else if (Matches("ALTER", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny))
2236 : 0 : COMPLETE_WITH("(");
2237 : : /* ALTER AGGREGATE <name> (...) */
2238 : 0 : else if (Matches("ALTER", "AGGREGATE", MatchAny, MatchAny))
2239 : : {
2240 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
2241 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA");
2242 : : else
2243 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2244 : : }
2245 : : /* ALTER FUNCTION <name> (...) */
2246 : 0 : else if (Matches("ALTER", "FUNCTION", MatchAny, MatchAny))
2247 : : {
2248 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
2249 : 0 : COMPLETE_WITH(Alter_function_options);
2250 : : else
2251 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2252 : : }
2253 : : /* ALTER PROCEDURE <name> (...) */
2254 : 0 : else if (Matches("ALTER", "PROCEDURE", MatchAny, MatchAny))
2255 : : {
2256 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
2257 : 0 : COMPLETE_WITH(Alter_procedure_options);
2258 : : else
2259 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2260 : : }
2261 : : /* ALTER ROUTINE <name> (...) */
2262 : 0 : else if (Matches("ALTER", "ROUTINE", MatchAny, MatchAny))
2263 : : {
2264 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
2265 : 0 : COMPLETE_WITH(Alter_routine_options);
2266 : : else
2267 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2268 : : }
2269 : : /* ALTER FUNCTION|ROUTINE <name> (...) PARALLEL */
2270 : 0 : else if (Matches("ALTER", "FUNCTION|ROUTINE", MatchAny, MatchAny, "PARALLEL"))
2271 : 0 : COMPLETE_WITH("RESTRICTED", "SAFE", "UNSAFE");
2272 : : /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) [EXTERNAL] SECURITY */
2273 : 0 : else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "SECURITY") ||
2274 : : Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "EXTERNAL", "SECURITY"))
2275 : 0 : COMPLETE_WITH("DEFINER", "INVOKER");
2276 : : /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) RESET */
2277 : 0 : else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "RESET"))
2278 : 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
2279 : : "ALL");
2280 : : /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) SET */
2281 : 0 : else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "SET"))
2282 : 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
2283 : : "SCHEMA");
2284 : :
2285 : : /* ALTER PUBLICATION <name> */
2286 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny))
2287 : 0 : COMPLETE_WITH("ADD", "DROP", "OWNER TO", "RENAME TO", "SET");
2288 : : /* ALTER PUBLICATION <name> ADD */
2289 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD"))
2290 : 0 : COMPLETE_WITH("TABLES IN SCHEMA", "TABLE");
2291 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE"))
2292 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2293 : 0 : else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE") &&
2294 [ # # ]: 0 : ends_with(prev_wd, ','))
2295 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2296 : :
2297 : : /*
2298 : : * "ALTER PUBLICATION <name> SET TABLE <name> WHERE (" - complete with
2299 : : * table attributes
2300 : : *
2301 : : * "ALTER PUBLICATION <name> ADD TABLE <name> WHERE (" - complete with
2302 : : * table attributes
2303 : : */
2304 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "WHERE"))
2305 : 0 : COMPLETE_WITH("(");
2306 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "("))
2307 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2308 : 0 : else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE") &&
2309 [ # # ]: 0 : !TailMatches("WHERE", "(*)"))
2310 : 0 : COMPLETE_WITH(",", "WHERE (");
2311 : 0 : else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE"))
2312 : 0 : COMPLETE_WITH(",");
2313 : : /* ALTER PUBLICATION <name> DROP */
2314 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "DROP"))
2315 : 0 : COMPLETE_WITH("TABLES IN SCHEMA", "TABLE");
2316 : : /* ALTER PUBLICATION <name> SET */
2317 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET"))
2318 : 0 : COMPLETE_WITH("(", "ALL SEQUENCES", "ALL TABLES", "TABLES IN SCHEMA", "TABLE");
2319 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL"))
2320 : 0 : COMPLETE_WITH("SEQUENCES", "TABLES");
2321 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES"))
2322 : 0 : COMPLETE_WITH("EXCEPT ( TABLE");
2323 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT"))
2324 : 0 : COMPLETE_WITH("( TABLE");
2325 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "("))
2326 : 0 : COMPLETE_WITH("TABLE");
2327 : : /* Complete "ALTER PUBLICATION <name> FOR TABLE" with "<table>, ..." */
2328 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "(", "TABLE"))
2329 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2330 [ # # ]: 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && ends_with(prev_wd, ','))
2331 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2332 [ # # ]: 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && !ends_with(prev_wd, ','))
2333 : 0 : COMPLETE_WITH(")");
2334 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD|DROP|SET", "TABLES", "IN", "SCHEMA"))
2335 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
2336 : : " AND nspname NOT LIKE E'pg\\\\_%%'",
2337 : : "CURRENT_SCHEMA");
2338 : : /* ALTER PUBLICATION <name> SET ( */
2339 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "SET", "("))
2340 : 0 : COMPLETE_WITH("publish", "publish_generated_columns", "publish_via_partition_root");
2341 : : /* ALTER SUBSCRIPTION <name> */
2342 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny))
2343 : 0 : COMPLETE_WITH("CONNECTION", "ENABLE", "DISABLE", "OWNER TO",
2344 : : "RENAME TO", "REFRESH PUBLICATION", "REFRESH SEQUENCES",
2345 : : "SERVER", "SET", "SKIP (", "ADD PUBLICATION", "DROP PUBLICATION");
2346 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, "SERVER"))
2347 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
2348 : : /* ALTER SUBSCRIPTION <name> REFRESH */
2349 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "REFRESH"))
2350 : 0 : COMPLETE_WITH("PUBLICATION", "SEQUENCES");
2351 : : /* ALTER SUBSCRIPTION <name> REFRESH PUBLICATION */
2352 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "REFRESH", "PUBLICATION"))
2353 : 0 : COMPLETE_WITH("WITH (");
2354 : : /* ALTER SUBSCRIPTION <name> REFRESH PUBLICATION WITH ( */
2355 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "REFRESH", "PUBLICATION", "WITH", "("))
2356 : 0 : COMPLETE_WITH("copy_data");
2357 : : /* ALTER SUBSCRIPTION <name> SET */
2358 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, "SET"))
2359 : 0 : COMPLETE_WITH("(", "PUBLICATION");
2360 : : /* ALTER SUBSCRIPTION <name> SET ( */
2361 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SET", "("))
2362 : 0 : COMPLETE_WITH("binary", "conflict_log_destination", "disable_on_error",
2363 : : "failover", "max_retention_duration", "origin",
2364 : : "password_required", "retain_dead_tuples",
2365 : : "run_as_owner", "slot_name", "streaming",
2366 : : "synchronous_commit", "two_phase",
2367 : : "wal_receiver_timeout");
2368 : : /* ALTER SUBSCRIPTION <name> SKIP ( */
2369 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SKIP", "("))
2370 : 0 : COMPLETE_WITH("lsn");
2371 : : /* ALTER SUBSCRIPTION <name> SET PUBLICATION */
2372 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SET", "PUBLICATION"))
2373 : : {
2374 : : /* complete with nothing here as this refers to remote publications */
2375 : : }
2376 : : /* ALTER SUBSCRIPTION <name> ADD|DROP|SET PUBLICATION <name> */
2377 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN,
2378 : : "ADD|DROP|SET", "PUBLICATION", MatchAny))
2379 : 0 : COMPLETE_WITH("WITH (");
2380 : : /* ALTER SUBSCRIPTION <name> ADD|DROP|SET PUBLICATION <name> WITH ( */
2381 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN,
2382 : : "ADD|DROP|SET", "PUBLICATION", MatchAny, "WITH", "("))
2383 : 0 : COMPLETE_WITH("copy_data", "refresh");
2384 : :
2385 : : /* ALTER SCHEMA <name> */
2386 : 0 : else if (Matches("ALTER", "SCHEMA", MatchAny))
2387 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO");
2388 : :
2389 : : /* ALTER COLLATION <name> */
2390 : 0 : else if (Matches("ALTER", "COLLATION", MatchAny))
2391 : 0 : COMPLETE_WITH("OWNER TO", "REFRESH VERSION", "RENAME TO", "SET SCHEMA");
2392 : :
2393 : : /* ALTER CONVERSION <name> */
2394 : 0 : else if (Matches("ALTER", "CONVERSION", MatchAny))
2395 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA");
2396 : :
2397 : : /* ALTER DATABASE <name> */
2398 : 0 : else if (Matches("ALTER", "DATABASE", MatchAny))
2399 : 0 : COMPLETE_WITH("RESET", "SET", "OWNER TO", "REFRESH COLLATION VERSION", "RENAME TO",
2400 : : "IS_TEMPLATE", "ALLOW_CONNECTIONS",
2401 : : "CONNECTION LIMIT");
2402 : :
2403 : : /* ALTER DATABASE <name> RESET */
2404 : 0 : else if (Matches("ALTER", "DATABASE", MatchAny, "RESET"))
2405 : : {
2406 : 0 : set_completion_reference(prev2_wd);
2407 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_database_vars, "ALL");
2408 : : }
2409 : :
2410 : : /* ALTER DATABASE <name> SET TABLESPACE */
2411 : 0 : else if (Matches("ALTER", "DATABASE", MatchAny, "SET", "TABLESPACE"))
2412 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
2413 : :
2414 : : /* ALTER EVENT TRIGGER */
2415 : 0 : else if (Matches("ALTER", "EVENT", "TRIGGER"))
2416 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
2417 : :
2418 : : /* ALTER EVENT TRIGGER <name> */
2419 : 0 : else if (Matches("ALTER", "EVENT", "TRIGGER", MatchAny))
2420 : 0 : COMPLETE_WITH("DISABLE", "ENABLE", "OWNER TO", "RENAME TO");
2421 : :
2422 : : /* ALTER EVENT TRIGGER <name> ENABLE */
2423 : 0 : else if (Matches("ALTER", "EVENT", "TRIGGER", MatchAny, "ENABLE"))
2424 : 0 : COMPLETE_WITH("REPLICA", "ALWAYS");
2425 : :
2426 : : /* ALTER EXTENSION <name> */
2427 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny))
2428 : 0 : COMPLETE_WITH("ADD", "DROP", "UPDATE", "SET SCHEMA");
2429 : :
2430 : : /* ALTER EXTENSION <name> ADD|DROP */
2431 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP"))
2432 : 0 : COMPLETE_WITH("ACCESS METHOD", "AGGREGATE", "CAST", "COLLATION",
2433 : : "CONVERSION", "DOMAIN", "EVENT TRIGGER", "FOREIGN",
2434 : : "FUNCTION", "MATERIALIZED VIEW", "OPERATOR",
2435 : : "LANGUAGE", "PROCEDURE", "ROUTINE", "SCHEMA",
2436 : : "SEQUENCE", "SERVER", "TABLE", "TEXT SEARCH",
2437 : : "TRANSFORM FOR", "TYPE", "VIEW");
2438 : :
2439 : : /* ALTER EXTENSION <name> ADD|DROP FOREIGN */
2440 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "FOREIGN"))
2441 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
2442 : :
2443 : : /* ALTER EXTENSION <name> ADD|DROP OPERATOR */
2444 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "OPERATOR"))
2445 : 0 : COMPLETE_WITH("CLASS", "FAMILY");
2446 : :
2447 : : /* ALTER EXTENSION <name> ADD|DROP TEXT SEARCH */
2448 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "TEXT", "SEARCH"))
2449 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
2450 : :
2451 : : /* ALTER EXTENSION <name> UPDATE */
2452 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "UPDATE"))
2453 : 0 : COMPLETE_WITH("TO");
2454 : :
2455 : : /* ALTER EXTENSION <name> UPDATE TO */
2456 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "UPDATE", "TO"))
2457 : : {
2458 : 0 : set_completion_reference(prev3_wd);
2459 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions);
2460 : : }
2461 : :
2462 : : /* ALTER FOREIGN */
2463 : 0 : else if (Matches("ALTER", "FOREIGN"))
2464 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
2465 : :
2466 : : /* ALTER FOREIGN DATA WRAPPER <name> */
2467 : 0 : else if (Matches("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny))
2468 : 0 : COMPLETE_WITH("CONNECTION", "HANDLER", "NO",
2469 : : "OPTIONS", "OWNER TO", "RENAME TO", "VALIDATOR");
2470 : 0 : else if (Matches("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny, "NO"))
2471 : 0 : COMPLETE_WITH("CONNECTION", "HANDLER", "VALIDATOR");
2472 : :
2473 : : /* ALTER FOREIGN TABLE <name> */
2474 : 0 : else if (Matches("ALTER", "FOREIGN", "TABLE", MatchAny))
2475 : 0 : COMPLETE_WITH("ADD", "ALTER", "DISABLE TRIGGER", "DROP", "ENABLE",
2476 : : "INHERIT", "NO INHERIT", "OPTIONS", "OWNER TO",
2477 : : "RENAME", "SET", "VALIDATE CONSTRAINT");
2478 : :
2479 : : /* ALTER INDEX */
2480 : 0 : else if (Matches("ALTER", "INDEX"))
2481 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
2482 : : "ALL IN TABLESPACE");
2483 : : /* ALTER INDEX <name> */
2484 : 0 : else if (Matches("ALTER", "INDEX", MatchAny))
2485 : 0 : COMPLETE_WITH("ALTER COLUMN", "OWNER TO", "RENAME TO", "SET",
2486 : : "RESET", "ATTACH PARTITION",
2487 : : "DEPENDS ON EXTENSION", "NO DEPENDS ON EXTENSION");
2488 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ATTACH"))
2489 : 0 : COMPLETE_WITH("PARTITION");
2490 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ATTACH", "PARTITION"))
2491 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
2492 : : /* ALTER INDEX <name> ALTER */
2493 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER"))
2494 : 0 : COMPLETE_WITH("COLUMN");
2495 : : /* ALTER INDEX <name> ALTER COLUMN */
2496 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN"))
2497 : : {
2498 : 0 : set_completion_reference(prev3_wd);
2499 : 0 : COMPLETE_WITH_SCHEMA_QUERY_VERBATIM(Query_for_list_of_attribute_numbers);
2500 : : }
2501 : : /* ALTER INDEX <name> ALTER COLUMN <colnum> */
2502 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny))
2503 : 0 : COMPLETE_WITH("SET STATISTICS");
2504 : : /* ALTER INDEX <name> ALTER COLUMN <colnum> SET */
2505 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny, "SET"))
2506 : 0 : COMPLETE_WITH("STATISTICS");
2507 : : /* ALTER INDEX <name> ALTER COLUMN <colnum> SET STATISTICS */
2508 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STATISTICS"))
2509 : : {
2510 : : /* Enforce no completion here, as an integer has to be specified */
2511 : : }
2512 : : /* ALTER INDEX <name> SET */
2513 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "SET"))
2514 : 0 : COMPLETE_WITH("(", "TABLESPACE");
2515 : : /* ALTER INDEX <name> RESET */
2516 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "RESET"))
2517 : 0 : COMPLETE_WITH("(");
2518 : : /* ALTER INDEX <foo> SET|RESET ( */
2519 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "RESET", "("))
2520 : 0 : COMPLETE_WITH("fillfactor",
2521 : : "deduplicate_items", /* BTREE */
2522 : : "fastupdate", "gin_pending_list_limit", /* GIN */
2523 : : "buffering", /* GiST */
2524 : : "pages_per_range", "autosummarize" /* BRIN */
2525 : : );
2526 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "SET", "("))
2527 : 0 : COMPLETE_WITH("fillfactor =",
2528 : : "deduplicate_items =", /* BTREE */
2529 : : "fastupdate =", "gin_pending_list_limit =", /* GIN */
2530 : : "buffering =", /* GiST */
2531 : : "pages_per_range =", "autosummarize =" /* BRIN */
2532 : : );
2533 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "NO", "DEPENDS"))
2534 : 0 : COMPLETE_WITH("ON EXTENSION");
2535 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "DEPENDS"))
2536 : 0 : COMPLETE_WITH("ON EXTENSION");
2537 : :
2538 : : /* ALTER LANGUAGE <name> */
2539 : 0 : else if (Matches("ALTER", "LANGUAGE", MatchAny))
2540 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO");
2541 : :
2542 : : /* ALTER LARGE OBJECT <oid> */
2543 : 0 : else if (Matches("ALTER", "LARGE", "OBJECT", MatchAny))
2544 : 0 : COMPLETE_WITH("OWNER TO");
2545 : :
2546 : : /* ALTER MATERIALIZED VIEW */
2547 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW"))
2548 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_matviews,
2549 : : "ALL IN TABLESPACE");
2550 : :
2551 : : /* ALTER USER,ROLE <name> */
2552 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny) &&
2553 [ # # ]: 0 : !TailMatches("USER", "MAPPING"))
2554 : 0 : COMPLETE_WITH("BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE",
2555 : : "ENCRYPTED PASSWORD", "IN", "INHERIT", "LOGIN", "NOBYPASSRLS",
2556 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
2557 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
2558 : : "RENAME TO", "REPLICATION", "RESET", "SET", "SUPERUSER",
2559 : : "VALID UNTIL", "WITH");
2560 : : /* ALTER USER,ROLE <name> IN */
2561 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN"))
2562 : 0 : COMPLETE_WITH("DATABASE");
2563 : : /* ALTER USER,ROLE <name> IN DATABASE */
2564 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN", "DATABASE"))
2565 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2566 : : /* ALTER USER,ROLE <name> IN DATABASE <dbname> */
2567 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN", "DATABASE", MatchAny))
2568 : 0 : COMPLETE_WITH("SET", "RESET");
2569 : : /* ALTER USER,ROLE <name> IN DATABASE <dbname> SET */
2570 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN", "DATABASE", MatchAny, "SET"))
2571 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_set_vars);
2572 : : /* XXX missing support for ALTER ROLE <name> IN DATABASE <dbname> RESET */
2573 : : /* ALTER USER,ROLE <name> RESET */
2574 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "RESET"))
2575 : : {
2576 : 0 : set_completion_reference(prev2_wd);
2577 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_user_vars, "ALL");
2578 : : }
2579 : :
2580 : : /* ALTER USER,ROLE <name> WITH */
2581 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "WITH"))
2582 : : /* Similar to the above, but don't complete "WITH" again. */
2583 : 0 : COMPLETE_WITH("BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE",
2584 : : "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS",
2585 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
2586 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
2587 : : "RENAME TO", "REPLICATION", "RESET", "SET", "SUPERUSER",
2588 : : "VALID UNTIL");
2589 : :
2590 : : /* ALTER DEFAULT PRIVILEGES */
2591 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES"))
2592 : 0 : COMPLETE_WITH("FOR", "GRANT", "IN SCHEMA", "REVOKE");
2593 : : /* ALTER DEFAULT PRIVILEGES FOR */
2594 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR"))
2595 : 0 : COMPLETE_WITH("ROLE");
2596 : : /* ALTER DEFAULT PRIVILEGES IN */
2597 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN"))
2598 : 0 : COMPLETE_WITH("SCHEMA");
2599 : : /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... */
2600 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER",
2601 : : MatchAny))
2602 : 0 : COMPLETE_WITH("GRANT", "REVOKE", "IN SCHEMA");
2603 : : /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... */
2604 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2605 : : MatchAny))
2606 : 0 : COMPLETE_WITH("GRANT", "REVOKE", "FOR ROLE");
2607 : : /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR */
2608 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2609 : : MatchAny, "FOR"))
2610 : 0 : COMPLETE_WITH("ROLE");
2611 : : /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... IN SCHEMA ... */
2612 : : /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR ROLE|USER ... */
2613 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER",
2614 : : MatchAny, "IN", "SCHEMA", MatchAny) ||
2615 : : Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2616 : : MatchAny, "FOR", "ROLE|USER", MatchAny))
2617 : 0 : COMPLETE_WITH("GRANT", "REVOKE");
2618 : : /* ALTER DOMAIN <name> */
2619 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny))
2620 : 0 : COMPLETE_WITH("ADD", "DROP", "OWNER TO", "RENAME", "SET",
2621 : : "VALIDATE CONSTRAINT");
2622 : : /* ALTER DOMAIN <sth> ADD */
2623 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "ADD"))
2624 : 0 : COMPLETE_WITH("CONSTRAINT", "NOT NULL", "CHECK (");
2625 : : /* ALTER DOMAIN <sth> ADD CONSTRAINT <sth> */
2626 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "ADD", "CONSTRAINT", MatchAny))
2627 : 0 : COMPLETE_WITH("NOT NULL", "CHECK (");
2628 : : /* ALTER DOMAIN <sth> DROP */
2629 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "DROP"))
2630 : 0 : COMPLETE_WITH("CONSTRAINT", "DEFAULT", "NOT NULL");
2631 : : /* ALTER DOMAIN <sth> DROP|RENAME|VALIDATE CONSTRAINT */
2632 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "DROP|RENAME|VALIDATE", "CONSTRAINT"))
2633 : : {
2634 : 0 : set_completion_reference(prev3_wd);
2635 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_type);
2636 : : }
2637 : : /* ALTER DOMAIN <sth> RENAME */
2638 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "RENAME"))
2639 : 0 : COMPLETE_WITH("CONSTRAINT", "TO");
2640 : : /* ALTER DOMAIN <sth> RENAME CONSTRAINT <sth> */
2641 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "RENAME", "CONSTRAINT", MatchAny))
2642 : 0 : COMPLETE_WITH("TO");
2643 : :
2644 : : /* ALTER DOMAIN <sth> SET */
2645 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "SET"))
2646 : 0 : COMPLETE_WITH("DEFAULT", "NOT NULL", "SCHEMA");
2647 : : /* ALTER SEQUENCE <name> */
2648 : 0 : else if (Matches("ALTER", "SEQUENCE", MatchAny))
2649 : 0 : COMPLETE_WITH("AS", "INCREMENT", "MINVALUE", "MAXVALUE", "RESTART",
2650 : : "START", "NO", "CACHE", "CYCLE", "SET", "OWNED BY",
2651 : : "OWNER TO", "RENAME TO");
2652 : : /* ALTER SEQUENCE <name> AS */
2653 : 0 : else if (TailMatches("ALTER", "SEQUENCE", MatchAny, "AS"))
2654 : 0 : COMPLETE_WITH_CS("smallint", "integer", "bigint");
2655 : : /* ALTER SEQUENCE <name> NO */
2656 : 0 : else if (Matches("ALTER", "SEQUENCE", MatchAny, "NO"))
2657 : 0 : COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
2658 : : /* ALTER SEQUENCE <name> SET */
2659 : 0 : else if (Matches("ALTER", "SEQUENCE", MatchAny, "SET"))
2660 : 0 : COMPLETE_WITH("SCHEMA", "LOGGED", "UNLOGGED");
2661 : : /* ALTER SERVER <name> */
2662 : 0 : else if (Matches("ALTER", "SERVER", MatchAny))
2663 : 0 : COMPLETE_WITH("VERSION", "OPTIONS", "OWNER TO", "RENAME TO");
2664 : : /* ALTER SERVER <name> VERSION <version> */
2665 : 0 : else if (Matches("ALTER", "SERVER", MatchAny, "VERSION", MatchAny))
2666 : 0 : COMPLETE_WITH("OPTIONS");
2667 : : /* ALTER SYSTEM SET, RESET, RESET ALL */
2668 : 0 : else if (Matches("ALTER", "SYSTEM"))
2669 : 0 : COMPLETE_WITH("SET", "RESET");
2670 : 0 : else if (Matches("ALTER", "SYSTEM", "SET|RESET"))
2671 : 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_alter_system_set_vars,
2672 : : "ALL");
2673 : 0 : else if (Matches("ALTER", "SYSTEM", "SET", MatchAny))
2674 : 0 : COMPLETE_WITH("TO");
2675 : : /* ALTER VIEW <name> */
2676 : 0 : else if (Matches("ALTER", "VIEW", MatchAny))
2677 : 0 : COMPLETE_WITH("ALTER COLUMN", "OWNER TO", "RENAME", "RESET", "SET");
2678 : : /* ALTER VIEW xxx RENAME */
2679 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RENAME"))
2680 : 0 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "TO");
2681 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "ALTER|RENAME", "COLUMN"))
2682 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2683 : : /* ALTER VIEW xxx ALTER [ COLUMN ] yyy */
2684 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "ALTER", MatchAny) ||
2685 : : Matches("ALTER", "VIEW", MatchAny, "ALTER", "COLUMN", MatchAny))
2686 : 0 : COMPLETE_WITH("SET DEFAULT", "DROP DEFAULT");
2687 : : /* ALTER VIEW xxx RENAME yyy */
2688 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RENAME", MatchAnyExcept("TO")))
2689 : 0 : COMPLETE_WITH("TO");
2690 : : /* ALTER VIEW xxx RENAME COLUMN yyy */
2691 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RENAME", "COLUMN", MatchAnyExcept("TO")))
2692 : 0 : COMPLETE_WITH("TO");
2693 : : /* ALTER VIEW xxx RESET ( */
2694 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RESET"))
2695 : 0 : COMPLETE_WITH("(");
2696 : : /* Complete ALTER VIEW xxx SET with "(" or "SCHEMA" */
2697 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET"))
2698 : 0 : COMPLETE_WITH("(", "SCHEMA");
2699 : : /* ALTER VIEW xxx SET|RESET ( yyy [= zzz] ) */
2700 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET|RESET", "("))
2701 : 0 : COMPLETE_WITH_LIST(view_optional_parameters);
2702 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", MatchAny))
2703 : 0 : COMPLETE_WITH("=");
2704 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", "check_option", "="))
2705 : 0 : COMPLETE_WITH("local", "cascaded");
2706 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", "security_barrier|security_invoker", "="))
2707 : 0 : COMPLETE_WITH("true", "false");
2708 : :
2709 : : /* ALTER MATERIALIZED VIEW <name> */
2710 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny))
2711 : 0 : COMPLETE_WITH("ALTER COLUMN", "CLUSTER ON", "DEPENDS ON EXTENSION",
2712 : : "NO DEPENDS ON EXTENSION", "OWNER TO", "RENAME",
2713 : : "RESET (", "SET");
2714 : : /* ALTER MATERIALIZED VIEW xxx RENAME */
2715 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME"))
2716 : 0 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "TO");
2717 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "ALTER|RENAME", "COLUMN"))
2718 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2719 : : /* ALTER MATERIALIZED VIEW xxx RENAME yyy */
2720 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME", MatchAnyExcept("TO")))
2721 : 0 : COMPLETE_WITH("TO");
2722 : : /* ALTER MATERIALIZED VIEW xxx RENAME COLUMN yyy */
2723 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME", "COLUMN", MatchAnyExcept("TO")))
2724 : 0 : COMPLETE_WITH("TO");
2725 : : /* ALTER MATERIALIZED VIEW xxx SET */
2726 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "SET"))
2727 : 0 : COMPLETE_WITH("(", "ACCESS METHOD", "SCHEMA", "TABLESPACE", "WITHOUT CLUSTER");
2728 : : /* ALTER MATERIALIZED VIEW xxx SET ACCESS METHOD */
2729 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "SET", "ACCESS", "METHOD"))
2730 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
2731 : :
2732 : : /* ALTER POLICY <name> */
2733 : 0 : else if (Matches("ALTER", "POLICY"))
2734 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_policies);
2735 : : /* ALTER POLICY <name> ON */
2736 : 0 : else if (Matches("ALTER", "POLICY", MatchAny))
2737 : 0 : COMPLETE_WITH("ON");
2738 : : /* ALTER POLICY <name> ON <table> */
2739 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON"))
2740 : : {
2741 : 0 : set_completion_reference(prev2_wd);
2742 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
2743 : : }
2744 : : /* ALTER POLICY <name> ON <table> - show options */
2745 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny))
2746 : 0 : COMPLETE_WITH("RENAME TO", "TO", "USING (", "WITH CHECK (");
2747 : : /* ALTER POLICY <name> ON <table> TO <role> */
2748 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "TO"))
2749 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
2750 : : Keywords_for_list_of_grant_roles);
2751 : : /* ALTER POLICY <name> ON <table> USING ( */
2752 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "USING"))
2753 : 0 : COMPLETE_WITH("(");
2754 : : /* ALTER POLICY <name> ON <table> WITH CHECK ( */
2755 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "WITH", "CHECK"))
2756 : 0 : COMPLETE_WITH("(");
2757 : :
2758 : : /* ALTER RULE <name>, add ON */
2759 : 0 : else if (Matches("ALTER", "RULE", MatchAny))
2760 : 0 : COMPLETE_WITH("ON");
2761 : :
2762 : : /* If we have ALTER RULE <name> ON, then add the correct tablename */
2763 : 0 : else if (Matches("ALTER", "RULE", MatchAny, "ON"))
2764 : : {
2765 : 0 : set_completion_reference(prev2_wd);
2766 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
2767 : : }
2768 : :
2769 : : /* ALTER RULE <name> ON <name> */
2770 : 0 : else if (Matches("ALTER", "RULE", MatchAny, "ON", MatchAny))
2771 : 0 : COMPLETE_WITH("RENAME TO");
2772 : :
2773 : : /* ALTER STATISTICS <name> */
2774 : 0 : else if (Matches("ALTER", "STATISTICS", MatchAny))
2775 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA", "SET STATISTICS");
2776 : : /* ALTER STATISTICS <name> SET */
2777 : 0 : else if (Matches("ALTER", "STATISTICS", MatchAny, "SET"))
2778 : 0 : COMPLETE_WITH("SCHEMA", "STATISTICS");
2779 : :
2780 : : /* ALTER TRIGGER <name>, add ON */
2781 : 0 : else if (Matches("ALTER", "TRIGGER", MatchAny))
2782 : 0 : COMPLETE_WITH("ON");
2783 : :
2784 : 0 : else if (Matches("ALTER", "TRIGGER", MatchAny, "ON"))
2785 : : {
2786 : 0 : set_completion_reference(prev2_wd);
2787 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
2788 : : }
2789 : :
2790 : : /* ALTER TRIGGER <name> ON <name> */
2791 : 0 : else if (Matches("ALTER", "TRIGGER", MatchAny, "ON", MatchAny))
2792 : 0 : COMPLETE_WITH("RENAME TO", "DEPENDS ON EXTENSION",
2793 : : "NO DEPENDS ON EXTENSION");
2794 : :
2795 : : /*
2796 : : * If we detect ALTER TABLE <name>, suggest sub commands
2797 : : */
2798 : 0 : else if (Matches("ALTER", "TABLE", MatchAny))
2799 : 0 : COMPLETE_WITH("ADD", "ALTER", "CLUSTER ON", "DISABLE", "DROP",
2800 : : "ENABLE", "INHERIT", "NO", "RENAME", "RESET",
2801 : : "OWNER TO", "SET", "VALIDATE CONSTRAINT",
2802 : : "REPLICA IDENTITY", "ATTACH PARTITION",
2803 : : "DETACH PARTITION", "FORCE ROW LEVEL SECURITY",
2804 : : "OF", "NOT OF");
2805 : : /* ALTER TABLE xxx ADD */
2806 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD"))
2807 : : {
2808 : : /*
2809 : : * make sure to keep this list and the MatchAnyExcept() below in sync
2810 : : */
2811 : 0 : COMPLETE_WITH("COLUMN", "CONSTRAINT", "CHECK (", "NOT NULL", "UNIQUE",
2812 : : "PRIMARY KEY", "EXCLUDE", "FOREIGN KEY");
2813 : : }
2814 : : /* ALTER TABLE xxx ADD [COLUMN] yyy */
2815 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny) ||
2816 : : Matches("ALTER", "TABLE", MatchAny, "ADD", MatchAnyExcept("COLUMN|CONSTRAINT|CHECK|UNIQUE|PRIMARY|NOT|EXCLUDE|FOREIGN")))
2817 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2818 : : /* ALTER TABLE xxx ADD CONSTRAINT yyy */
2819 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny))
2820 : 0 : COMPLETE_WITH("CHECK (", "NOT NULL", "UNIQUE", "PRIMARY KEY", "EXCLUDE", "FOREIGN KEY");
2821 : : /* ALTER TABLE xxx ADD NOT NULL */
2822 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "NOT", "NULL"))
2823 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
2824 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "NOT", "NULL"))
2825 : 0 : COMPLETE_WITH_ATTR(prev6_wd);
2826 : : /* ALTER TABLE xxx ADD [CONSTRAINT yyy] (PRIMARY KEY|UNIQUE) */
2827 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "PRIMARY", "KEY") ||
2828 : : Matches("ALTER", "TABLE", MatchAny, "ADD", "UNIQUE") ||
2829 : : Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "PRIMARY", "KEY") ||
2830 : : Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "UNIQUE"))
2831 : 0 : COMPLETE_WITH("(", "USING INDEX");
2832 : : /* ALTER TABLE xxx ADD PRIMARY KEY USING INDEX */
2833 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "PRIMARY", "KEY", "USING", "INDEX"))
2834 : : {
2835 : 0 : set_completion_reference(prev6_wd);
2836 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2837 : : }
2838 : : /* ALTER TABLE xxx ADD UNIQUE USING INDEX */
2839 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "UNIQUE", "USING", "INDEX"))
2840 : : {
2841 : 0 : set_completion_reference(prev5_wd);
2842 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2843 : : }
2844 : : /* ALTER TABLE xxx ADD CONSTRAINT yyy PRIMARY KEY USING INDEX */
2845 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny,
2846 : : "PRIMARY", "KEY", "USING", "INDEX"))
2847 : : {
2848 : 0 : set_completion_reference(prev8_wd);
2849 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2850 : : }
2851 : : /* ALTER TABLE xxx ADD CONSTRAINT yyy UNIQUE USING INDEX */
2852 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny,
2853 : : "UNIQUE", "USING", "INDEX"))
2854 : : {
2855 : 0 : set_completion_reference(prev7_wd);
2856 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2857 : : }
2858 : : /* ALTER TABLE xxx ENABLE */
2859 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE"))
2860 : 0 : COMPLETE_WITH("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE",
2861 : : "TRIGGER");
2862 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "REPLICA|ALWAYS"))
2863 : 0 : COMPLETE_WITH("RULE", "TRIGGER");
2864 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "RULE"))
2865 : : {
2866 : 0 : set_completion_reference(prev3_wd);
2867 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2868 : : }
2869 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "RULE"))
2870 : : {
2871 : 0 : set_completion_reference(prev4_wd);
2872 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2873 : : }
2874 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "TRIGGER"))
2875 : : {
2876 : 0 : set_completion_reference(prev3_wd);
2877 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2878 : : }
2879 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "TRIGGER"))
2880 : : {
2881 : 0 : set_completion_reference(prev4_wd);
2882 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2883 : : }
2884 : : /* ALTER TABLE xxx INHERIT */
2885 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "INHERIT"))
2886 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2887 : : /* ALTER TABLE xxx NO */
2888 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "NO"))
2889 : 0 : COMPLETE_WITH("FORCE ROW LEVEL SECURITY", "INHERIT");
2890 : : /* ALTER TABLE xxx NO INHERIT */
2891 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "NO", "INHERIT"))
2892 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2893 : : /* ALTER TABLE xxx DISABLE */
2894 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE"))
2895 : 0 : COMPLETE_WITH("ROW LEVEL SECURITY", "RULE", "TRIGGER");
2896 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE", "RULE"))
2897 : : {
2898 : 0 : set_completion_reference(prev3_wd);
2899 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2900 : : }
2901 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE", "TRIGGER"))
2902 : : {
2903 : 0 : set_completion_reference(prev3_wd);
2904 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2905 : : }
2906 : :
2907 : : /* ALTER TABLE xxx ALTER */
2908 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER"))
2909 : 0 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "CONSTRAINT");
2910 : :
2911 : : /* ALTER TABLE xxx RENAME */
2912 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RENAME"))
2913 : 12 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "CONSTRAINT", "TO");
2914 : 12 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|RENAME", "COLUMN"))
2915 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2916 : :
2917 : : /* ALTER TABLE xxx RENAME yyy */
2918 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RENAME", MatchAnyExcept("CONSTRAINT|TO")))
2919 : 0 : COMPLETE_WITH("TO");
2920 : :
2921 : : /* ALTER TABLE xxx RENAME COLUMN/CONSTRAINT yyy */
2922 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RENAME", "COLUMN|CONSTRAINT", MatchAnyExcept("TO")))
2923 : 0 : COMPLETE_WITH("TO");
2924 : :
2925 : : /* If we have ALTER TABLE <sth> DROP, provide COLUMN or CONSTRAINT */
2926 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DROP"))
2927 : 0 : COMPLETE_WITH("COLUMN", "CONSTRAINT");
2928 : : /* If we have ALTER TABLE <sth> DROP COLUMN, provide list of columns */
2929 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DROP", "COLUMN"))
2930 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2931 : : /* ALTER TABLE <sth> ALTER|DROP|RENAME CONSTRAINT <constraint> */
2932 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME", "CONSTRAINT"))
2933 : : {
2934 : 3 : set_completion_reference(prev3_wd);
2935 : 3 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_table);
2936 : : }
2937 : : /* ALTER TABLE <sth> VALIDATE CONSTRAINT <non-validated constraint> */
2938 : 3 : else if (Matches("ALTER", "TABLE", MatchAny, "VALIDATE", "CONSTRAINT"))
2939 : : {
2940 : 0 : set_completion_reference(prev3_wd);
2941 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_table_not_validated);
2942 : : }
2943 : : /* ALTER TABLE ALTER [COLUMN] <foo> */
2944 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny) ||
2945 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny))
2946 : 0 : COMPLETE_WITH("TYPE", "SET", "RESET", "RESTART", "ADD", "DROP");
2947 : : /* ALTER TABLE ALTER [COLUMN] <foo> ADD */
2948 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD") ||
2949 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD"))
2950 : 0 : COMPLETE_WITH("GENERATED");
2951 : : /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED */
2952 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED") ||
2953 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED"))
2954 : 0 : COMPLETE_WITH("ALWAYS", "BY DEFAULT");
2955 : : /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED */
2956 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED", "ALWAYS") ||
2957 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED", "ALWAYS") ||
2958 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED", "BY", "DEFAULT") ||
2959 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED", "BY", "DEFAULT"))
2960 : 0 : COMPLETE_WITH("AS IDENTITY");
2961 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET */
2962 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET") ||
2963 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET"))
2964 : 0 : COMPLETE_WITH("(", "COMPRESSION", "DATA TYPE", "DEFAULT", "EXPRESSION", "GENERATED", "NOT NULL",
2965 : : "STATISTICS", "STORAGE",
2966 : : /* a subset of ALTER SEQUENCE options */
2967 : : "INCREMENT", "MINVALUE", "MAXVALUE", "START", "NO", "CACHE", "CYCLE");
2968 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET ( */
2969 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "(") ||
2970 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "("))
2971 : 0 : COMPLETE_WITH("n_distinct", "n_distinct_inherited");
2972 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET COMPRESSION */
2973 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "COMPRESSION") ||
2974 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "COMPRESSION"))
2975 : 0 : COMPLETE_WITH("DEFAULT", "PGLZ", "LZ4");
2976 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET EXPRESSION */
2977 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "EXPRESSION") ||
2978 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "EXPRESSION"))
2979 : 0 : COMPLETE_WITH("AS");
2980 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET EXPRESSION AS */
2981 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "EXPRESSION", "AS") ||
2982 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "EXPRESSION", "AS"))
2983 : 0 : COMPLETE_WITH("(");
2984 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET GENERATED */
2985 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "GENERATED") ||
2986 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "GENERATED"))
2987 : 0 : COMPLETE_WITH("ALWAYS", "BY DEFAULT");
2988 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET NO */
2989 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "NO") ||
2990 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "NO"))
2991 : 0 : COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
2992 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET STORAGE */
2993 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STORAGE") ||
2994 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STORAGE"))
2995 : 0 : COMPLETE_WITH("DEFAULT", "PLAIN", "EXTERNAL", "EXTENDED", "MAIN");
2996 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET STATISTICS */
2997 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STATISTICS") ||
2998 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STATISTICS"))
2999 : : {
3000 : : /* Enforce no completion here, as an integer has to be specified */
3001 : : }
3002 : : /* ALTER TABLE ALTER [COLUMN] <foo> DROP */
3003 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "DROP") ||
3004 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "DROP"))
3005 : 0 : COMPLETE_WITH("DEFAULT", "EXPRESSION", "IDENTITY", "NOT NULL");
3006 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "CLUSTER"))
3007 : 0 : COMPLETE_WITH("ON");
3008 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "CLUSTER", "ON"))
3009 : : {
3010 : 0 : set_completion_reference(prev3_wd);
3011 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
3012 : : }
3013 : : /* If we have ALTER TABLE <sth> SET, provide list of attributes and '(' */
3014 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET"))
3015 : 0 : COMPLETE_WITH("(", "ACCESS METHOD", "LOGGED", "SCHEMA",
3016 : : "TABLESPACE", "UNLOGGED", "WITH", "WITHOUT");
3017 : :
3018 : : /*
3019 : : * If we have ALTER TABLE <sth> SET ACCESS METHOD provide a list of table
3020 : : * AMs.
3021 : : */
3022 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET", "ACCESS", "METHOD"))
3023 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_table_access_methods,
3024 : : "DEFAULT");
3025 : :
3026 : : /*
3027 : : * If we have ALTER TABLE <sth> SET TABLESPACE provide a list of
3028 : : * tablespaces
3029 : : */
3030 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET", "TABLESPACE"))
3031 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
3032 : : /* If we have ALTER TABLE <sth> SET WITHOUT provide CLUSTER or OIDS */
3033 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET", "WITHOUT"))
3034 : 0 : COMPLETE_WITH("CLUSTER", "OIDS");
3035 : : /* ALTER TABLE <foo> RESET */
3036 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RESET"))
3037 : 0 : COMPLETE_WITH("(");
3038 : : /* ALTER TABLE <foo> SET|RESET ( */
3039 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET|RESET", "("))
3040 : 0 : COMPLETE_WITH_LIST(table_storage_parameters);
3041 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING", "INDEX"))
3042 : : {
3043 : 0 : set_completion_reference(prev5_wd);
3044 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
3045 : : }
3046 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING"))
3047 : 0 : COMPLETE_WITH("INDEX");
3048 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY"))
3049 : 0 : COMPLETE_WITH("FULL", "NOTHING", "DEFAULT", "USING");
3050 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA"))
3051 : 0 : COMPLETE_WITH("IDENTITY");
3052 : :
3053 : : /*
3054 : : * If we have ALTER TABLE <foo> ATTACH PARTITION, provide a list of
3055 : : * tables.
3056 : : */
3057 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ATTACH", "PARTITION"))
3058 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3059 : : /* Limited completion support for partition bound specification */
3060 : 0 : else if (TailMatches("ATTACH", "PARTITION", MatchAny))
3061 : 0 : COMPLETE_WITH("FOR VALUES", "DEFAULT");
3062 : 0 : else if (TailMatches("FOR", "VALUES"))
3063 : 0 : COMPLETE_WITH("FROM (", "IN (", "WITH (");
3064 : :
3065 : : /*
3066 : : * If we have ALTER TABLE <foo> DETACH PARTITION, provide a list of
3067 : : * partitions of <foo>.
3068 : : */
3069 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DETACH", "PARTITION"))
3070 : : {
3071 : 0 : set_completion_reference(prev3_wd);
3072 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_partition_of_table);
3073 : : }
3074 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DETACH", "PARTITION", MatchAny))
3075 : 0 : COMPLETE_WITH("CONCURRENTLY", "FINALIZE");
3076 : :
3077 : : /* ALTER TABLE <name> OF */
3078 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "OF"))
3079 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_composite_datatypes);
3080 : :
3081 : : /* ALTER TABLESPACE <foo> with RENAME TO, OWNER TO, SET, RESET */
3082 : 0 : else if (Matches("ALTER", "TABLESPACE", MatchAny))
3083 : 0 : COMPLETE_WITH("RENAME TO", "OWNER TO", "SET", "RESET");
3084 : : /* ALTER TABLESPACE <foo> SET|RESET */
3085 : 0 : else if (Matches("ALTER", "TABLESPACE", MatchAny, "SET|RESET"))
3086 : 0 : COMPLETE_WITH("(");
3087 : : /* ALTER TABLESPACE <foo> SET|RESET ( */
3088 : 0 : else if (Matches("ALTER", "TABLESPACE", MatchAny, "SET|RESET", "("))
3089 : 0 : COMPLETE_WITH("seq_page_cost", "random_page_cost",
3090 : : "effective_io_concurrency", "maintenance_io_concurrency");
3091 : :
3092 : : /* ALTER TEXT SEARCH */
3093 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH"))
3094 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
3095 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH", "TEMPLATE|PARSER", MatchAny))
3096 : 0 : COMPLETE_WITH("RENAME TO", "SET SCHEMA");
3097 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH", "DICTIONARY", MatchAny))
3098 : 0 : COMPLETE_WITH("(", "OWNER TO", "RENAME TO", "SET SCHEMA");
3099 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH", "CONFIGURATION", MatchAny))
3100 : 0 : COMPLETE_WITH("ADD MAPPING FOR", "ALTER MAPPING",
3101 : : "DROP MAPPING FOR",
3102 : : "OWNER TO", "RENAME TO", "SET SCHEMA");
3103 : :
3104 : : /* complete ALTER TYPE <foo> with actions */
3105 : 0 : else if (Matches("ALTER", "TYPE", MatchAny))
3106 : 0 : COMPLETE_WITH("ADD ATTRIBUTE", "ADD VALUE", "ALTER ATTRIBUTE",
3107 : : "DROP ATTRIBUTE",
3108 : : "OWNER TO", "RENAME", "SET SCHEMA", "SET (");
3109 : : /* complete ALTER TYPE <foo> ADD with actions */
3110 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ADD"))
3111 : 0 : COMPLETE_WITH("ATTRIBUTE", "VALUE");
3112 : : /* ALTER TYPE <foo> RENAME */
3113 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME"))
3114 : 0 : COMPLETE_WITH("ATTRIBUTE", "TO", "VALUE");
3115 : : /* ALTER TYPE xxx RENAME (ATTRIBUTE|VALUE) yyy */
3116 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE|VALUE", MatchAny))
3117 : 0 : COMPLETE_WITH("TO");
3118 : : /* ALTER TYPE xxx RENAME ATTRIBUTE yyy TO zzz */
3119 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE", MatchAny, "TO", MatchAny))
3120 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3121 : :
3122 : : /*
3123 : : * If we have ALTER TYPE <sth> ALTER/DROP/RENAME ATTRIBUTE, provide list
3124 : : * of attributes
3125 : : */
3126 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ALTER|DROP|RENAME", "ATTRIBUTE"))
3127 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
3128 : : /* complete ALTER TYPE ADD ATTRIBUTE <foo> with list of types */
3129 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ADD", "ATTRIBUTE", MatchAny))
3130 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
3131 : : /* complete ALTER TYPE ADD ATTRIBUTE <foo> <footype> with CASCADE/RESTRICT */
3132 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ADD", "ATTRIBUTE", MatchAny, MatchAny))
3133 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3134 : : /* complete ALTER TYPE DROP ATTRIBUTE <foo> with CASCADE/RESTRICT */
3135 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "DROP", "ATTRIBUTE", MatchAny))
3136 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3137 : : /* ALTER TYPE ALTER ATTRIBUTE <foo> */
3138 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny))
3139 : 0 : COMPLETE_WITH("TYPE");
3140 : : /* ALTER TYPE ALTER ATTRIBUTE <foo> TYPE <footype> */
3141 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny, "TYPE", MatchAny))
3142 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3143 : : /* complete ALTER TYPE <sth> RENAME VALUE with list of enum values */
3144 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "VALUE"))
3145 [ + - + - : 3 : COMPLETE_WITH_ENUM_VALUE(prev3_wd);
- + ]
3146 : : /* ALTER TYPE <foo> SET */
3147 : 3 : else if (Matches("ALTER", "TYPE", MatchAny, "SET"))
3148 : 0 : COMPLETE_WITH("(", "SCHEMA");
3149 : : /* complete ALTER TYPE <foo> SET ( with settable properties */
3150 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "SET", "("))
3151 : 0 : COMPLETE_WITH("ANALYZE", "RECEIVE", "SEND", "STORAGE", "SUBSCRIPT",
3152 : : "TYPMOD_IN", "TYPMOD_OUT");
3153 : :
3154 : : /* complete ALTER GROUP <foo> */
3155 : 0 : else if (Matches("ALTER", "GROUP", MatchAny))
3156 : 0 : COMPLETE_WITH("ADD USER", "DROP USER", "RENAME TO");
3157 : : /* complete ALTER GROUP <foo> ADD|DROP with USER */
3158 : 0 : else if (Matches("ALTER", "GROUP", MatchAny, "ADD|DROP"))
3159 : 0 : COMPLETE_WITH("USER");
3160 : : /* complete ALTER GROUP <foo> ADD|DROP USER with a user name */
3161 : 0 : else if (Matches("ALTER", "GROUP", MatchAny, "ADD|DROP", "USER"))
3162 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
3163 : :
3164 : : /*
3165 : : * ANALYZE [ ( option [, ...] ) ] [ [ ONLY ] table_and_columns [, ...] ]
3166 : : * ANALYZE [ VERBOSE ] [ [ ONLY ] table_and_columns [, ...] ]
3167 : : */
3168 : 0 : else if (Matches("ANALYZE"))
3169 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
3170 : : "(", "VERBOSE", "ONLY");
3171 : 0 : else if (Matches("ANALYZE", "VERBOSE"))
3172 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
3173 : : "ONLY");
3174 : 0 : else if (HeadMatches("ANALYZE", "(*") &&
3175 [ + - ]: 2 : !HeadMatches("ANALYZE", "(*)"))
3176 : : {
3177 : : /*
3178 : : * This fires if we're in an unfinished parenthesized option list.
3179 : : * get_previous_words treats a completed parenthesized option list as
3180 : : * one word, so the above test is correct.
3181 : : */
3182 [ - + - - ]: 2 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3183 : 2 : COMPLETE_WITH("VERBOSE", "SKIP_LOCKED", "BUFFER_USAGE_LIMIT");
3184 [ # # ]: 0 : else if (TailMatches("VERBOSE|SKIP_LOCKED"))
3185 : 0 : COMPLETE_WITH("ON", "OFF");
3186 : : }
3187 : 2 : else if (Matches("ANALYZE", "(*)"))
3188 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
3189 : : "ONLY");
3190 : 0 : else if (Matches("ANALYZE", MatchAnyN, "("))
3191 : : /* "ANALYZE (" should be caught above, so assume we want columns */
3192 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
3193 : 0 : else if (HeadMatches("ANALYZE"))
3194 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_analyzables);
3195 : :
3196 : : /* BEGIN */
3197 : 0 : else if (Matches("BEGIN"))
3198 : 0 : COMPLETE_WITH("WORK", "TRANSACTION", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
3199 : : /* END, ABORT */
3200 : 0 : else if (Matches("END|ABORT"))
3201 : 0 : COMPLETE_WITH("AND", "WORK", "TRANSACTION");
3202 : : /* COMMIT */
3203 : 0 : else if (Matches("COMMIT"))
3204 : 0 : COMPLETE_WITH("AND", "WORK", "TRANSACTION", "PREPARED");
3205 : : /* RELEASE SAVEPOINT */
3206 : 0 : else if (Matches("RELEASE"))
3207 : 0 : COMPLETE_WITH("SAVEPOINT");
3208 : : /* ROLLBACK */
3209 : 0 : else if (Matches("ROLLBACK"))
3210 : 0 : COMPLETE_WITH("AND", "WORK", "TRANSACTION", "TO SAVEPOINT", "PREPARED");
3211 : 0 : else if (Matches("ABORT|END|COMMIT|ROLLBACK", "AND"))
3212 : 0 : COMPLETE_WITH("CHAIN");
3213 : : /* CALL */
3214 : 0 : else if (Matches("CALL"))
3215 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_procedures);
3216 : 0 : else if (Matches("CALL", MatchAny))
3217 : 0 : COMPLETE_WITH("(");
3218 : : /* CHECKPOINT */
3219 : 0 : else if (Matches("CHECKPOINT"))
3220 : 0 : COMPLETE_WITH("(");
3221 : 0 : else if (HeadMatches("CHECKPOINT", "(*") &&
3222 [ # # ]: 0 : !HeadMatches("CHECKPOINT", "(*)"))
3223 : : {
3224 : : /*
3225 : : * This fires if we're in an unfinished parenthesized option list.
3226 : : * get_previous_words treats a completed parenthesized option list as
3227 : : * one word, so the above test is correct.
3228 : : */
3229 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3230 : 0 : COMPLETE_WITH("MODE", "FLUSH_UNLOGGED");
3231 [ # # ]: 0 : else if (TailMatches("MODE"))
3232 : 0 : COMPLETE_WITH("FAST", "SPREAD");
3233 [ # # ]: 0 : else if (TailMatches("FLUSH_UNLOGGED"))
3234 : 0 : COMPLETE_WITH("ON", "OFF");
3235 : : }
3236 : : /* CLOSE */
3237 : 0 : else if (Matches("CLOSE"))
3238 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
3239 : : "ALL");
3240 : : /* CLUSTER */
3241 : 0 : else if (Matches("CLUSTER"))
3242 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
3243 : : "VERBOSE");
3244 : 0 : else if (Matches("CLUSTER", "VERBOSE") ||
3245 : : Matches("CLUSTER", "(*)"))
3246 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_clusterables);
3247 : : /* If we have CLUSTER <sth>, then add "USING" */
3248 : 0 : else if (Matches("CLUSTER", MatchAnyExcept("VERBOSE|ON|(|(*)")))
3249 : 0 : COMPLETE_WITH("USING");
3250 : : /* If we have CLUSTER VERBOSE <sth>, then add "USING" */
3251 : 0 : else if (Matches("CLUSTER", "VERBOSE|(*)", MatchAny))
3252 : 0 : COMPLETE_WITH("USING");
3253 : : /* If we have CLUSTER <sth> USING, then add the index as well */
3254 : 0 : else if (Matches("CLUSTER", MatchAny, "USING") ||
3255 : : Matches("CLUSTER", "VERBOSE|(*)", MatchAny, "USING"))
3256 : : {
3257 : 0 : set_completion_reference(prev2_wd);
3258 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
3259 : : }
3260 : 0 : else if (HeadMatches("CLUSTER", "(*") &&
3261 [ # # ]: 0 : !HeadMatches("CLUSTER", "(*)"))
3262 : : {
3263 : : /*
3264 : : * This fires if we're in an unfinished parenthesized option list.
3265 : : * get_previous_words treats a completed parenthesized option list as
3266 : : * one word, so the above test is correct.
3267 : : */
3268 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3269 : 0 : COMPLETE_WITH("VERBOSE");
3270 : : }
3271 : :
3272 : : /* COMMENT */
3273 : 0 : else if (Matches("COMMENT"))
3274 : 0 : COMPLETE_WITH("ON");
3275 : 0 : else if (Matches("COMMENT", "ON"))
3276 : 0 : COMPLETE_WITH("ACCESS METHOD", "AGGREGATE", "CAST", "COLLATION",
3277 : : "COLUMN", "CONSTRAINT", "CONVERSION", "DATABASE",
3278 : : "DOMAIN", "EXTENSION", "EVENT TRIGGER",
3279 : : "FOREIGN DATA WRAPPER", "FOREIGN TABLE",
3280 : : "FUNCTION", "INDEX", "LANGUAGE", "LARGE OBJECT",
3281 : : "MATERIALIZED VIEW", "OPERATOR", "POLICY",
3282 : : "PROCEDURE", "PROCEDURAL LANGUAGE", "PUBLICATION", "ROLE",
3283 : : "ROUTINE", "RULE", "SCHEMA", "SEQUENCE", "SERVER",
3284 : : "STATISTICS", "SUBSCRIPTION", "TABLE",
3285 : : "TABLESPACE", "TEXT SEARCH", "TRANSFORM FOR",
3286 : : "TRIGGER", "TYPE", "VIEW");
3287 : 0 : else if (Matches("COMMENT", "ON", "ACCESS", "METHOD"))
3288 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
3289 : 0 : else if (Matches("COMMENT", "ON", "CONSTRAINT"))
3290 : 0 : COMPLETE_WITH_QUERY(Query_for_all_table_constraints);
3291 : 0 : else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny))
3292 : 0 : COMPLETE_WITH("ON");
3293 : 0 : else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON"))
3294 : : {
3295 : 1 : set_completion_reference(prev2_wd);
3296 : 1 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables_for_constraint,
3297 : : "DOMAIN");
3298 : : }
3299 : 1 : else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON", "DOMAIN"))
3300 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
3301 : 0 : else if (Matches("COMMENT", "ON", "EVENT", "TRIGGER"))
3302 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
3303 : 0 : else if (Matches("COMMENT", "ON", "FOREIGN"))
3304 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
3305 : 0 : else if (Matches("COMMENT", "ON", "FOREIGN", "TABLE"))
3306 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
3307 : 0 : else if (Matches("COMMENT", "ON", "MATERIALIZED", "VIEW"))
3308 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
3309 : 0 : else if (Matches("COMMENT", "ON", "POLICY"))
3310 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_policies);
3311 : 0 : else if (Matches("COMMENT", "ON", "POLICY", MatchAny))
3312 : 0 : COMPLETE_WITH("ON");
3313 : 0 : else if (Matches("COMMENT", "ON", "POLICY", MatchAny, "ON"))
3314 : : {
3315 : 0 : set_completion_reference(prev2_wd);
3316 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
3317 : : }
3318 : 0 : else if (Matches("COMMENT", "ON", "PROCEDURAL", "LANGUAGE"))
3319 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3320 : 0 : else if (Matches("COMMENT", "ON", "RULE", MatchAny))
3321 : 0 : COMPLETE_WITH("ON");
3322 : 0 : else if (Matches("COMMENT", "ON", "RULE", MatchAny, "ON"))
3323 : : {
3324 : 0 : set_completion_reference(prev2_wd);
3325 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
3326 : : }
3327 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH"))
3328 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
3329 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "CONFIGURATION"))
3330 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_configurations);
3331 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "DICTIONARY"))
3332 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_dictionaries);
3333 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "PARSER"))
3334 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_parsers);
3335 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "TEMPLATE"))
3336 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_templates);
3337 : 0 : else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR"))
3338 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
3339 : 0 : else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR", MatchAny))
3340 : 0 : COMPLETE_WITH("LANGUAGE");
3341 : 0 : else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
3342 : : {
3343 : 0 : set_completion_reference(prev2_wd);
3344 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3345 : : }
3346 : 0 : else if (Matches("COMMENT", "ON", "TRIGGER", MatchAny))
3347 : 0 : COMPLETE_WITH("ON");
3348 : 0 : else if (Matches("COMMENT", "ON", "TRIGGER", MatchAny, "ON"))
3349 : : {
3350 : 0 : set_completion_reference(prev2_wd);
3351 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
3352 : : }
3353 : 0 : else if (Matches("COMMENT", "ON", MatchAny, MatchAnyExcept("IS")) ||
3354 : : Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAnyExcept("IS")) ||
3355 : : Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS")) ||
3356 : : Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS")))
3357 : 0 : COMPLETE_WITH("IS");
3358 : :
3359 : : /* COPY */
3360 : :
3361 : : /*
3362 : : * If we have COPY, offer list of tables or "(" (Also cover the analogous
3363 : : * backslash command).
3364 : : */
3365 : 0 : else if (Matches("COPY|\\copy"))
3366 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables_for_copy, "(");
3367 : : /* Complete COPY ( with legal query commands */
3368 : 0 : else if (Matches("COPY|\\copy", "("))
3369 : 0 : COMPLETE_WITH("SELECT", "TABLE", "VALUES", "INSERT INTO", "UPDATE", "DELETE FROM", "MERGE INTO", "WITH");
3370 : : /* Complete COPY <sth> */
3371 : 0 : else if (Matches("COPY|\\copy", MatchAny))
3372 : 0 : COMPLETE_WITH("FROM", "TO");
3373 : : /* Complete COPY|\copy <sth> FROM|TO with filename or STDIN/STDOUT/PROGRAM */
3374 : 0 : else if (Matches("COPY|\\copy", MatchAny, "FROM|TO"))
3375 : : {
3376 [ + - ]: 4 : if (HeadMatches("COPY"))
3377 : : {
3378 : : /* COPY requires quoted filename */
3379 [ + - ]: 4 : if (TailMatches("FROM"))
3380 : 4 : COMPLETE_WITH_FILES_PLUS("", true, "STDIN", "PROGRAM");
3381 : : else
3382 : 0 : COMPLETE_WITH_FILES_PLUS("", true, "STDOUT", "PROGRAM");
3383 : : }
3384 : : else
3385 : : {
3386 : : /* \copy supports pstdin and pstdout */
3387 [ # # ]: 0 : if (TailMatches("FROM"))
3388 : 0 : COMPLETE_WITH_FILES_PLUS("", false, "STDIN", "PSTDIN", "PROGRAM");
3389 : : else
3390 : 0 : COMPLETE_WITH_FILES_PLUS("", false, "STDOUT", "PSTDOUT", "PROGRAM");
3391 : : }
3392 : : }
3393 : :
3394 : : /* Complete COPY|\copy <sth> FROM|TO PROGRAM */
3395 : 4 : else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", "PROGRAM"))
3396 : 0 : COMPLETE_WITH_FILES("", HeadMatches("COPY")); /* COPY requires quoted
3397 : : * filename */
3398 : :
3399 : : /* Complete COPY <sth> TO [PROGRAM] <sth> */
3400 : 0 : else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAnyExcept("PROGRAM")) ||
3401 : : Matches("COPY|\\copy", MatchAny, "TO", "PROGRAM", MatchAny))
3402 : 0 : COMPLETE_WITH("WITH (");
3403 : :
3404 : : /* Complete COPY <sth> FROM [PROGRAM] <sth> */
3405 : 0 : else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAnyExcept("PROGRAM")) ||
3406 : : Matches("COPY|\\copy", MatchAny, "FROM", "PROGRAM", MatchAny))
3407 : 0 : COMPLETE_WITH("WITH (", "WHERE");
3408 : :
3409 : : /* Complete COPY <sth> FROM|TO [PROGRAM] filename WITH ( */
3410 : 0 : else if (HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", MatchAnyExcept("PROGRAM"), "WITH", "(*") ||
3411 : : HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", "PROGRAM", MatchAny, "WITH", "(*"))
3412 : : {
3413 [ + - ]: 1 : if (!HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", MatchAnyExcept("PROGRAM"), "WITH", "(*)") &&
3414 [ + - ]: 1 : !HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", "PROGRAM", MatchAny, "WITH", "(*)"))
3415 : : {
3416 : : /*
3417 : : * This fires if we're in an unfinished parenthesized option list.
3418 : : * get_previous_words treats a completed parenthesized option list
3419 : : * as one word, so the above tests are correct.
3420 : : */
3421 : :
3422 [ - + - - ]: 1 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3423 : : {
3424 [ + - ]: 2 : if (HeadMatches("COPY|\\copy", MatchAny, "FROM"))
3425 : 1 : COMPLETE_WITH(Copy_from_options);
3426 : : else
3427 : 0 : COMPLETE_WITH(Copy_to_options);
3428 : : }
3429 : :
3430 : : /* Complete COPY <sth> FROM|TO filename WITH (FORMAT */
3431 [ # # ]: 0 : else if (TailMatches("FORMAT"))
3432 : 0 : COMPLETE_WITH("binary", "csv", "text", "json");
3433 : :
3434 : : /* Complete COPY <sth> FROM|TO filename WITH (FREEZE */
3435 [ # # ]: 0 : else if (TailMatches("FREEZE"))
3436 : 0 : COMPLETE_WITH("true", "false");
3437 : :
3438 : : /* Complete COPY <sth> FROM|TO filename WITH (HEADER */
3439 [ # # ]: 0 : else if (TailMatches("HEADER"))
3440 : 0 : COMPLETE_WITH("true", "false", "MATCH");
3441 : :
3442 : : /* Complete COPY <sth> FROM filename WITH (ON_ERROR */
3443 [ # # ]: 0 : else if (TailMatches("ON_ERROR"))
3444 : 0 : COMPLETE_WITH("stop", "ignore", "set_null");
3445 : :
3446 : : /* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
3447 [ # # ]: 0 : else if (TailMatches("LOG_VERBOSITY"))
3448 : 0 : COMPLETE_WITH("silent", "default", "verbose");
3449 : : }
3450 : :
3451 : : /* A completed parenthesized option list should be caught below */
3452 : : }
3453 : :
3454 : : /* Complete COPY <sth> FROM [PROGRAM] <sth> WITH (<options>) */
3455 : 1 : else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAnyExcept("PROGRAM"), "WITH", MatchAny) ||
3456 : : Matches("COPY|\\copy", MatchAny, "FROM", "PROGRAM", MatchAny, "WITH", MatchAny))
3457 : 0 : COMPLETE_WITH("WHERE");
3458 : :
3459 : : /* CREATE ACCESS METHOD */
3460 : : /* Complete "CREATE ACCESS METHOD <name>" */
3461 : 0 : else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny))
3462 : 0 : COMPLETE_WITH("TYPE");
3463 : : /* Complete "CREATE ACCESS METHOD <name> TYPE" */
3464 : 0 : else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE"))
3465 : 0 : COMPLETE_WITH("INDEX", "TABLE");
3466 : : /* Complete "CREATE ACCESS METHOD <name> TYPE <type>" */
3467 : 0 : else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE", MatchAny))
3468 : 0 : COMPLETE_WITH("HANDLER");
3469 : :
3470 : : /* CREATE COLLATION */
3471 : 0 : else if (Matches("CREATE", "COLLATION", MatchAny))
3472 : 0 : COMPLETE_WITH("(", "FROM");
3473 : 0 : else if (Matches("CREATE", "COLLATION", MatchAny, "FROM"))
3474 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_collations);
3475 : 0 : else if (HeadMatches("CREATE", "COLLATION", MatchAny, "(*"))
3476 : : {
3477 [ # # ]: 0 : if (TailMatches("(|*,"))
3478 : 0 : COMPLETE_WITH("LOCALE =", "LC_COLLATE =", "LC_CTYPE =",
3479 : : "PROVIDER =", "DETERMINISTIC =");
3480 [ # # ]: 0 : else if (TailMatches("PROVIDER", "="))
3481 : 0 : COMPLETE_WITH("libc", "icu");
3482 [ # # ]: 0 : else if (TailMatches("DETERMINISTIC", "="))
3483 : 0 : COMPLETE_WITH("true", "false");
3484 : : }
3485 : :
3486 : : /* CREATE DATABASE */
3487 : 0 : else if (Matches("CREATE", "DATABASE", MatchAny))
3488 : 0 : COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
3489 : : "IS_TEMPLATE", "STRATEGY",
3490 : : "ALLOW_CONNECTIONS", "CONNECTION LIMIT",
3491 : : "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID",
3492 : : "LOCALE_PROVIDER", "ICU_LOCALE");
3493 : :
3494 : 0 : else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
3495 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
3496 : 0 : else if (Matches("CREATE", "DATABASE", MatchAny, "STRATEGY"))
3497 : 0 : COMPLETE_WITH("WAL_LOG", "FILE_COPY");
3498 : :
3499 : : /* CREATE DOMAIN */
3500 : 0 : else if (Matches("CREATE", "DOMAIN", MatchAny))
3501 : 0 : COMPLETE_WITH("AS");
3502 : 0 : else if (Matches("CREATE", "DOMAIN", MatchAny, "AS"))
3503 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
3504 : 0 : else if (Matches("CREATE", "DOMAIN", MatchAny, "AS", MatchAny))
3505 : 0 : COMPLETE_WITH("COLLATE", "DEFAULT", "CONSTRAINT",
3506 : : "NOT NULL", "NULL", "CHECK (");
3507 : 0 : else if (Matches("CREATE", "DOMAIN", MatchAny, "COLLATE"))
3508 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_collations);
3509 : :
3510 : : /* CREATE EXTENSION */
3511 : : /* Complete with available extensions rather than installed ones. */
3512 : 0 : else if (Matches("CREATE", "EXTENSION"))
3513 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions);
3514 : : /* CREATE EXTENSION <name> */
3515 : 0 : else if (Matches("CREATE", "EXTENSION", MatchAny))
3516 : 0 : COMPLETE_WITH("WITH SCHEMA", "CASCADE", "VERSION");
3517 : : /* CREATE EXTENSION <name> VERSION */
3518 : 0 : else if (Matches("CREATE", "EXTENSION", MatchAny, "VERSION"))
3519 : : {
3520 : 0 : set_completion_reference(prev2_wd);
3521 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions);
3522 : : }
3523 : :
3524 : : /* CREATE FOREIGN */
3525 : 0 : else if (Matches("CREATE", "FOREIGN"))
3526 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
3527 : :
3528 : : /* CREATE FOREIGN DATA WRAPPER */
3529 : 0 : else if (Matches("CREATE", "FOREIGN", "DATA", "WRAPPER", MatchAny))
3530 : 0 : COMPLETE_WITH("CONNECTION", "HANDLER", "OPTIONS", "VALIDATOR");
3531 : :
3532 : : /* CREATE FOREIGN TABLE */
3533 : 0 : else if (Matches("CREATE", "FOREIGN", "TABLE", MatchAny))
3534 : 0 : COMPLETE_WITH("(", "PARTITION OF");
3535 : :
3536 : : /* CREATE INDEX --- is allowed inside CREATE SCHEMA, so use TailMatches */
3537 : : /* First off we complete CREATE UNIQUE with "INDEX" */
3538 : 0 : else if (TailMatches("CREATE", "UNIQUE"))
3539 : 0 : COMPLETE_WITH("INDEX");
3540 : :
3541 : : /*
3542 : : * If we have CREATE|UNIQUE INDEX, then add "ON", "CONCURRENTLY", and
3543 : : * existing indexes
3544 : : */
3545 : 0 : else if (TailMatches("CREATE|UNIQUE", "INDEX"))
3546 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
3547 : : "ON", "CONCURRENTLY");
3548 : :
3549 : : /*
3550 : : * Complete ... INDEX|CONCURRENTLY [<name>] ON with a list of relations
3551 : : * that indexes can be created on
3552 : : */
3553 : 0 : else if (TailMatches("INDEX|CONCURRENTLY", MatchAny, "ON") ||
3554 : : TailMatches("INDEX|CONCURRENTLY", "ON"))
3555 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables);
3556 : :
3557 : : /*
3558 : : * Complete CREATE|UNIQUE INDEX CONCURRENTLY with "ON" and existing
3559 : : * indexes
3560 : : */
3561 : 0 : else if (TailMatches("CREATE|UNIQUE", "INDEX", "CONCURRENTLY"))
3562 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
3563 : : "ON");
3564 : : /* Complete CREATE|UNIQUE INDEX [CONCURRENTLY] <sth> with "ON" */
3565 : 0 : else if (TailMatches("CREATE|UNIQUE", "INDEX", MatchAny) ||
3566 : : TailMatches("CREATE|UNIQUE", "INDEX", "CONCURRENTLY", MatchAny))
3567 : 0 : COMPLETE_WITH("ON");
3568 : :
3569 : : /*
3570 : : * Complete INDEX <name> ON <table> with a list of table columns (which
3571 : : * should really be in parens)
3572 : : */
3573 : 0 : else if (TailMatches("INDEX", MatchAny, "ON", MatchAny) ||
3574 : : TailMatches("INDEX|CONCURRENTLY", "ON", MatchAny))
3575 : 0 : COMPLETE_WITH("(", "USING");
3576 : 0 : else if (TailMatches("INDEX", MatchAny, "ON", MatchAny, "(") ||
3577 : : TailMatches("INDEX|CONCURRENTLY", "ON", MatchAny, "("))
3578 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
3579 : : /* same if you put in USING */
3580 : 0 : else if (TailMatches("ON", MatchAny, "USING", MatchAny, "("))
3581 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
3582 : : /* Complete USING with an index method */
3583 : 0 : else if (TailMatches("INDEX", MatchAny, MatchAny, "ON", MatchAny, "USING") ||
3584 : : TailMatches("INDEX", MatchAny, "ON", MatchAny, "USING") ||
3585 : : TailMatches("INDEX", "ON", MatchAny, "USING"))
3586 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_index_access_methods);
3587 : 0 : else if (TailMatches("ON", MatchAny, "USING", MatchAny) &&
3588 : : !TailMatches("POLICY", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny) &&
3589 [ # # # # ]: 0 : !TailMatches("FOR", MatchAny, MatchAny, MatchAny))
3590 : 0 : COMPLETE_WITH("(");
3591 : :
3592 : : /* CREATE OR REPLACE */
3593 : 0 : else if (Matches("CREATE", "OR"))
3594 : 0 : COMPLETE_WITH("REPLACE");
3595 : :
3596 : : /* CREATE POLICY */
3597 : : /* Complete "CREATE POLICY <name> ON" */
3598 : 0 : else if (Matches("CREATE", "POLICY", MatchAny))
3599 : 0 : COMPLETE_WITH("ON");
3600 : : /* Complete "CREATE POLICY <name> ON <table>" */
3601 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON"))
3602 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3603 : : /* Complete "CREATE POLICY <name> ON <table> AS|FOR|TO|USING|WITH CHECK" */
3604 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny))
3605 : 0 : COMPLETE_WITH("AS", "FOR", "TO", "USING (", "WITH CHECK (");
3606 : : /* CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE */
3607 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS"))
3608 : 0 : COMPLETE_WITH("PERMISSIVE", "RESTRICTIVE");
3609 : :
3610 : : /*
3611 : : * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE
3612 : : * FOR|TO|USING|WITH CHECK
3613 : : */
3614 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny))
3615 : 0 : COMPLETE_WITH("FOR", "TO", "USING", "WITH CHECK");
3616 : : /* CREATE POLICY <name> ON <table> FOR ALL|SELECT|INSERT|UPDATE|DELETE */
3617 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR"))
3618 : 0 : COMPLETE_WITH("ALL", "SELECT", "INSERT", "UPDATE", "DELETE");
3619 : : /* Complete "CREATE POLICY <name> ON <table> FOR INSERT TO|WITH CHECK" */
3620 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "INSERT"))
3621 : 0 : COMPLETE_WITH("TO", "WITH CHECK (");
3622 : : /* Complete "CREATE POLICY <name> ON <table> FOR SELECT|DELETE TO|USING" */
3623 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "SELECT|DELETE"))
3624 : 0 : COMPLETE_WITH("TO", "USING (");
3625 : : /* CREATE POLICY <name> ON <table> FOR ALL|UPDATE TO|USING|WITH CHECK */
3626 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "ALL|UPDATE"))
3627 : 0 : COMPLETE_WITH("TO", "USING (", "WITH CHECK (");
3628 : : /* Complete "CREATE POLICY <name> ON <table> TO <role>" */
3629 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "TO"))
3630 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3631 : : Keywords_for_list_of_grant_roles);
3632 : : /* Complete "CREATE POLICY <name> ON <table> USING (" */
3633 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "USING"))
3634 : 0 : COMPLETE_WITH("(");
3635 : :
3636 : : /*
3637 : : * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3638 : : * ALL|SELECT|INSERT|UPDATE|DELETE
3639 : : */
3640 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR"))
3641 : 0 : COMPLETE_WITH("ALL", "SELECT", "INSERT", "UPDATE", "DELETE");
3642 : :
3643 : : /*
3644 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3645 : : * INSERT TO|WITH CHECK"
3646 : : */
3647 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "INSERT"))
3648 : 0 : COMPLETE_WITH("TO", "WITH CHECK (");
3649 : :
3650 : : /*
3651 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3652 : : * SELECT|DELETE TO|USING"
3653 : : */
3654 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "SELECT|DELETE"))
3655 : 0 : COMPLETE_WITH("TO", "USING (");
3656 : :
3657 : : /*
3658 : : * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3659 : : * ALL|UPDATE TO|USING|WITH CHECK
3660 : : */
3661 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "ALL|UPDATE"))
3662 : 0 : COMPLETE_WITH("TO", "USING (", "WITH CHECK (");
3663 : :
3664 : : /*
3665 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE TO
3666 : : * <role>"
3667 : : */
3668 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "TO"))
3669 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3670 : : Keywords_for_list_of_grant_roles);
3671 : :
3672 : : /*
3673 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE
3674 : : * USING ("
3675 : : */
3676 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "USING"))
3677 : 0 : COMPLETE_WITH("(");
3678 : :
3679 : :
3680 : : /* CREATE PUBLICATION */
3681 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny))
3682 : 0 : COMPLETE_WITH("FOR TABLE", "FOR TABLES IN SCHEMA", "FOR ALL TABLES", "FOR ALL SEQUENCES", "WITH (");
3683 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR"))
3684 : 0 : COMPLETE_WITH("TABLE", "TABLES IN SCHEMA", "ALL TABLES", "ALL SEQUENCES");
3685 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL"))
3686 : 0 : COMPLETE_WITH("TABLES", "SEQUENCES");
3687 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES"))
3688 : 0 : COMPLETE_WITH("EXCEPT ( TABLE", "WITH (");
3689 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT"))
3690 : 0 : COMPLETE_WITH("( TABLE");
3691 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "("))
3692 : 0 : COMPLETE_WITH("TABLE");
3693 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "(", "TABLE"))
3694 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3695 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && ends_with(prev_wd, ','))
3696 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3697 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && !ends_with(prev_wd, ','))
3698 : 0 : COMPLETE_WITH(")");
3699 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES"))
3700 : 0 : COMPLETE_WITH("IN SCHEMA");
3701 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE", MatchAny) && !ends_with(prev_wd, ','))
3702 : 0 : COMPLETE_WITH("WHERE (", "WITH (");
3703 : : /* Complete "CREATE PUBLICATION <name> FOR TABLE" with "<table>, ..." */
3704 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE"))
3705 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3706 : :
3707 : : /*
3708 : : * "CREATE PUBLICATION <name> FOR TABLE <name> WHERE (" - complete with
3709 : : * table attributes
3710 : : */
3711 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE"))
3712 : 0 : COMPLETE_WITH("(");
3713 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "("))
3714 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
3715 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "(*)"))
3716 : 0 : COMPLETE_WITH(" WITH (");
3717 : :
3718 : : /*
3719 : : * Complete "CREATE PUBLICATION <name> FOR TABLES IN SCHEMA <schema>, ..."
3720 : : */
3721 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES", "IN", "SCHEMA"))
3722 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
3723 : : " AND nspname NOT LIKE E'pg\\\\_%%'",
3724 : : "CURRENT_SCHEMA");
3725 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES", "IN", "SCHEMA", MatchAny) && (!ends_with(prev_wd, ',')))
3726 : 0 : COMPLETE_WITH("WITH (");
3727 : : /* Complete "CREATE PUBLICATION <name> [...] WITH" */
3728 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAnyN, "WITH", "("))
3729 : 0 : COMPLETE_WITH("publish", "publish_generated_columns", "publish_via_partition_root");
3730 : :
3731 : : /* CREATE RULE */
3732 : : /* Complete "CREATE [ OR REPLACE ] RULE <sth>" with "AS ON" */
3733 : 0 : else if (Matches("CREATE", "RULE", MatchAny) ||
3734 : : Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny))
3735 : 0 : COMPLETE_WITH("AS ON");
3736 : : /* Complete "CREATE [ OR REPLACE ] RULE <sth> AS" with "ON" */
3737 : 0 : else if (Matches("CREATE", "RULE", MatchAny, "AS") ||
3738 : : Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny, "AS"))
3739 : 0 : COMPLETE_WITH("ON");
3740 : :
3741 : : /*
3742 : : * Complete "CREATE [ OR REPLACE ] RULE <sth> AS ON" with
3743 : : * SELECT|UPDATE|INSERT|DELETE
3744 : : */
3745 : 0 : else if (Matches("CREATE", "RULE", MatchAny, "AS", "ON") ||
3746 : : Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny, "AS", "ON"))
3747 : 0 : COMPLETE_WITH("SELECT", "UPDATE", "INSERT", "DELETE");
3748 : : /* Complete "AS ON SELECT|UPDATE|INSERT|DELETE" with a "TO" */
3749 : 0 : else if (TailMatches("AS", "ON", "SELECT|UPDATE|INSERT|DELETE"))
3750 : 0 : COMPLETE_WITH("TO");
3751 : : /* Complete "AS ON <sth> TO" with a table name */
3752 : 0 : else if (TailMatches("AS", "ON", "SELECT|UPDATE|INSERT|DELETE", "TO"))
3753 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3754 : :
3755 : : /* CREATE SCHEMA [ <name> ] [ AUTHORIZATION ] */
3756 : 0 : else if (Matches("CREATE", "SCHEMA"))
3757 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas,
3758 : : "AUTHORIZATION");
3759 : 0 : else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION") ||
3760 : : Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION"))
3761 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3762 : : Keywords_for_list_of_owner_roles);
3763 : 0 : else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION", MatchAny) ||
3764 : : Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION", MatchAny))
3765 : 0 : COMPLETE_WITH("CREATE", "GRANT");
3766 : 0 : else if (Matches("CREATE", "SCHEMA", MatchAny))
3767 : 0 : COMPLETE_WITH("AUTHORIZATION", "CREATE", "GRANT");
3768 : :
3769 : : /* CREATE SEQUENCE --- is allowed inside CREATE SCHEMA, so use TailMatches */
3770 : 0 : else if (TailMatches("CREATE", "SEQUENCE", MatchAny) ||
3771 : : TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny))
3772 : 0 : COMPLETE_WITH("AS", "INCREMENT BY", "MINVALUE", "MAXVALUE", "NO",
3773 : : "CACHE", "CYCLE", "OWNED BY", "START WITH");
3774 : 0 : else if (TailMatches("CREATE", "SEQUENCE", MatchAny, "AS") ||
3775 : : TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "AS"))
3776 : 0 : COMPLETE_WITH_CS("smallint", "integer", "bigint");
3777 : 0 : else if (TailMatches("CREATE", "SEQUENCE", MatchAny, "NO") ||
3778 : : TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "NO"))
3779 : 0 : COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
3780 : :
3781 : : /* CREATE SERVER <name> */
3782 : 0 : else if (Matches("CREATE", "SERVER", MatchAny))
3783 : 0 : COMPLETE_WITH("TYPE", "VERSION", "FOREIGN DATA WRAPPER");
3784 : :
3785 : : /* CREATE STATISTICS <name> */
3786 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny))
3787 : 0 : COMPLETE_WITH("(", "ON");
3788 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny, "("))
3789 : 0 : COMPLETE_WITH("ndistinct", "dependencies", "mcv");
3790 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny, "(*)"))
3791 : 0 : COMPLETE_WITH("ON");
3792 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny, MatchAnyN, "FROM"))
3793 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3794 : :
3795 : : /* CREATE TABLE --- is allowed inside CREATE SCHEMA, so use TailMatches */
3796 : : /* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */
3797 : 0 : else if (TailMatches("CREATE", "TEMP|TEMPORARY"))
3798 : 0 : COMPLETE_WITH("SEQUENCE", "TABLE", "VIEW");
3799 : : /* Complete "CREATE UNLOGGED" with TABLE or SEQUENCE */
3800 : 0 : else if (TailMatches("CREATE", "UNLOGGED"))
3801 : 0 : COMPLETE_WITH("TABLE", "SEQUENCE");
3802 : : /* Complete PARTITION BY with RANGE ( or LIST ( or ... */
3803 : 0 : else if (TailMatches("PARTITION", "BY"))
3804 : 0 : COMPLETE_WITH("RANGE (", "LIST (", "HASH (");
3805 : : /* If we have xxx PARTITION OF, provide a list of partitioned tables */
3806 : 0 : else if (TailMatches("PARTITION", "OF"))
3807 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables);
3808 : : /* Limited completion support for partition bound specification */
3809 : 0 : else if (TailMatches("PARTITION", "OF", MatchAny))
3810 : 0 : COMPLETE_WITH("FOR VALUES", "DEFAULT");
3811 : : /* Complete CREATE TABLE <name> with '(', AS, OF or PARTITION OF */
3812 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny) ||
3813 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny))
3814 : 0 : COMPLETE_WITH("(", "AS", "OF", "PARTITION OF");
3815 : : /* Complete CREATE TABLE <name> OF with list of composite types */
3816 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "OF") ||
3817 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "OF"))
3818 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_composite_datatypes);
3819 : : /* Complete CREATE TABLE <name> [ (...) ] AS with list of keywords */
3820 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "AS") ||
3821 : : TailMatches("CREATE", "TABLE", MatchAny, "(*)", "AS") ||
3822 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "AS") ||
3823 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "AS"))
3824 : 0 : COMPLETE_WITH("EXECUTE", "SELECT", "TABLE", "VALUES", "WITH");
3825 : : /* Complete CREATE TABLE name (...) with supported options */
3826 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)"))
3827 : 0 : COMPLETE_WITH("AS", "INHERITS (", "PARTITION BY", "USING", "TABLESPACE", "WITH (");
3828 : 0 : else if (TailMatches("CREATE", "UNLOGGED", "TABLE", MatchAny, "(*)"))
3829 : 0 : COMPLETE_WITH("AS", "INHERITS (", "USING", "TABLESPACE", "WITH (");
3830 : 0 : else if (TailMatches("CREATE", "TEMP|TEMPORARY", "TABLE", MatchAny, "(*)"))
3831 : 0 : COMPLETE_WITH("AS", "INHERITS (", "ON COMMIT", "PARTITION BY", "USING",
3832 : : "TABLESPACE", "WITH (");
3833 : : /* Complete CREATE TABLE (...) USING with table access methods */
3834 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)", "USING") ||
3835 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "USING"))
3836 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
3837 : : /* Complete CREATE TABLE (...) WITH with storage parameters */
3838 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)", "WITH", "(") ||
3839 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "WITH", "("))
3840 : 0 : COMPLETE_WITH_LIST(table_storage_parameters);
3841 : : /* Complete CREATE TABLE ON COMMIT with actions */
3842 : 0 : else if (TailMatches("CREATE", "TEMP|TEMPORARY", "TABLE", MatchAny, "(*)", "ON", "COMMIT"))
3843 : 0 : COMPLETE_WITH("DELETE ROWS", "DROP", "PRESERVE ROWS");
3844 : :
3845 : : /* CREATE TABLESPACE */
3846 : 0 : else if (Matches("CREATE", "TABLESPACE", MatchAny))
3847 : 0 : COMPLETE_WITH("OWNER", "LOCATION");
3848 : : /* Complete CREATE TABLESPACE name OWNER name with "LOCATION" */
3849 : 0 : else if (Matches("CREATE", "TABLESPACE", MatchAny, "OWNER", MatchAny))
3850 : 0 : COMPLETE_WITH("LOCATION");
3851 : :
3852 : : /* CREATE TEXT SEARCH */
3853 : 0 : else if (Matches("CREATE", "TEXT", "SEARCH"))
3854 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
3855 : 0 : else if (Matches("CREATE", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny))
3856 : 0 : COMPLETE_WITH("(");
3857 : :
3858 : : /* CREATE TRANSFORM */
3859 : 0 : else if (Matches("CREATE", "TRANSFORM") ||
3860 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM"))
3861 : 0 : COMPLETE_WITH("FOR");
3862 : 0 : else if (Matches("CREATE", "TRANSFORM", "FOR") ||
3863 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR"))
3864 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
3865 : 0 : else if (Matches("CREATE", "TRANSFORM", "FOR", MatchAny) ||
3866 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR", MatchAny))
3867 : 0 : COMPLETE_WITH("LANGUAGE");
3868 : 0 : else if (Matches("CREATE", "TRANSFORM", "FOR", MatchAny, "LANGUAGE") ||
3869 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
3870 : : {
3871 : 0 : set_completion_reference(prev2_wd);
3872 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3873 : : }
3874 : :
3875 : : /* CREATE SUBSCRIPTION */
3876 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny))
3877 : 0 : COMPLETE_WITH("CONNECTION", "SERVER");
3878 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "SERVER"))
3879 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
3880 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "SERVER", MatchAny))
3881 : 0 : COMPLETE_WITH("PUBLICATION");
3882 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION", MatchAny))
3883 : 0 : COMPLETE_WITH("PUBLICATION");
3884 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "SERVER",
3885 : : MatchAny, "PUBLICATION"))
3886 : : {
3887 : : /* complete with nothing here as this refers to remote publications */
3888 : : }
3889 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION",
3890 : : MatchAny, "PUBLICATION"))
3891 : : {
3892 : : /* complete with nothing here as this refers to remote publications */
3893 : : }
3894 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAnyN, "PUBLICATION", MatchAny))
3895 : 0 : COMPLETE_WITH("WITH (");
3896 : : /* Complete "CREATE SUBSCRIPTION <name> ... WITH ( <opt>" */
3897 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAnyN, "WITH", "("))
3898 : 0 : COMPLETE_WITH("binary", "conflict_log_destination", "connect", "copy_data",
3899 : : "create_slot", "disable_on_error", "enabled", "failover",
3900 : : "max_retention_duration", "origin",
3901 : : "password_required", "retain_dead_tuples",
3902 : : "run_as_owner", "slot_name", "streaming",
3903 : : "synchronous_commit", "two_phase",
3904 : : "wal_receiver_timeout");
3905 : :
3906 : : /* CREATE TRIGGER --- is allowed inside CREATE SCHEMA, so use TailMatches */
3907 : :
3908 : : /*
3909 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> with BEFORE|AFTER|INSTEAD
3910 : : * OF.
3911 : : */
3912 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny) ||
3913 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny))
3914 : 0 : COMPLETE_WITH("BEFORE", "AFTER", "INSTEAD OF");
3915 : :
3916 : : /*
3917 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER with an
3918 : : * event.
3919 : : */
3920 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER") ||
3921 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER"))
3922 : 0 : COMPLETE_WITH("INSERT", "DELETE", "UPDATE", "TRUNCATE");
3923 : : /* Complete CREATE [ OR REPLACE ] TRIGGER <name> INSTEAD OF with an event */
3924 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF") ||
3925 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF"))
3926 : 0 : COMPLETE_WITH("INSERT", "DELETE", "UPDATE");
3927 : :
3928 : : /*
3929 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER sth with
3930 : : * OR|ON.
3931 : : */
3932 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) ||
3933 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) ||
3934 : : TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny) ||
3935 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny))
3936 : 0 : COMPLETE_WITH("ON", "OR");
3937 : :
3938 : : /*
3939 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER event ON
3940 : : * with a list of tables. EXECUTE FUNCTION is the recommended grammar
3941 : : * instead of EXECUTE PROCEDURE in version 11 and upwards.
3942 : : */
3943 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON") ||
3944 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON"))
3945 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3946 : :
3947 : : /*
3948 : : * Complete CREATE [ OR REPLACE ] TRIGGER ... INSTEAD OF event ON with a
3949 : : * list of views.
3950 : : */
3951 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON") ||
3952 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON"))
3953 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
3954 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3955 : : "ON", MatchAny) ||
3956 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3957 : : "ON", MatchAny))
3958 : : {
3959 [ # # ]: 0 : if (pset.sversion >= 110000)
3960 : 0 : COMPLETE_WITH("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY",
3961 : : "REFERENCING", "FOR", "WHEN (", "EXECUTE FUNCTION");
3962 : : else
3963 : 0 : COMPLETE_WITH("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY",
3964 : : "REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE");
3965 : : }
3966 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3967 : : "DEFERRABLE") ||
3968 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3969 : : "DEFERRABLE") ||
3970 : : Matches("CREATE", "TRIGGER", MatchAnyN,
3971 : : "INITIALLY", "IMMEDIATE|DEFERRED") ||
3972 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3973 : : "INITIALLY", "IMMEDIATE|DEFERRED"))
3974 : : {
3975 [ # # ]: 0 : if (pset.sversion >= 110000)
3976 : 0 : COMPLETE_WITH("REFERENCING", "FOR", "WHEN (", "EXECUTE FUNCTION");
3977 : : else
3978 : 0 : COMPLETE_WITH("REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE");
3979 : : }
3980 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3981 : : "REFERENCING") ||
3982 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3983 : : "REFERENCING"))
3984 : 0 : COMPLETE_WITH("OLD TABLE", "NEW TABLE");
3985 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3986 : : "OLD|NEW", "TABLE") ||
3987 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3988 : : "OLD|NEW", "TABLE"))
3989 : 0 : COMPLETE_WITH("AS");
3990 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3991 : : "REFERENCING", "OLD", "TABLE", "AS", MatchAny) ||
3992 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3993 : : "REFERENCING", "OLD", "TABLE", "AS", MatchAny) ||
3994 : : Matches("CREATE", "TRIGGER", MatchAnyN,
3995 : : "REFERENCING", "OLD", "TABLE", MatchAny) ||
3996 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3997 : : "REFERENCING", "OLD", "TABLE", MatchAny))
3998 : : {
3999 [ # # ]: 0 : if (pset.sversion >= 110000)
4000 : 0 : COMPLETE_WITH("NEW TABLE", "FOR", "WHEN (", "EXECUTE FUNCTION");
4001 : : else
4002 : 0 : COMPLETE_WITH("NEW TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE");
4003 : : }
4004 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4005 : : "REFERENCING", "NEW", "TABLE", "AS", MatchAny) ||
4006 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4007 : : "REFERENCING", "NEW", "TABLE", "AS", MatchAny) ||
4008 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4009 : : "REFERENCING", "NEW", "TABLE", MatchAny) ||
4010 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4011 : : "REFERENCING", "NEW", "TABLE", MatchAny))
4012 : : {
4013 [ # # ]: 0 : if (pset.sversion >= 110000)
4014 : 0 : COMPLETE_WITH("OLD TABLE", "FOR", "WHEN (", "EXECUTE FUNCTION");
4015 : : else
4016 : 0 : COMPLETE_WITH("OLD TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE");
4017 : : }
4018 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4019 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4020 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4021 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4022 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4023 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4024 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4025 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4026 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4027 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
4028 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4029 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
4030 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4031 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
4032 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4033 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny))
4034 : : {
4035 [ # # ]: 0 : if (pset.sversion >= 110000)
4036 : 0 : COMPLETE_WITH("FOR", "WHEN (", "EXECUTE FUNCTION");
4037 : : else
4038 : 0 : COMPLETE_WITH("FOR", "WHEN (", "EXECUTE PROCEDURE");
4039 : : }
4040 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4041 : : "FOR") ||
4042 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4043 : : "FOR"))
4044 : 0 : COMPLETE_WITH("EACH", "ROW", "STATEMENT");
4045 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4046 : : "FOR", "EACH") ||
4047 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4048 : : "FOR", "EACH"))
4049 : 0 : COMPLETE_WITH("ROW", "STATEMENT");
4050 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4051 : : "FOR", "EACH", "ROW|STATEMENT") ||
4052 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4053 : : "FOR", "EACH", "ROW|STATEMENT") ||
4054 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4055 : : "FOR", "ROW|STATEMENT") ||
4056 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4057 : : "FOR", "ROW|STATEMENT"))
4058 : : {
4059 [ # # ]: 0 : if (pset.sversion >= 110000)
4060 : 0 : COMPLETE_WITH("WHEN (", "EXECUTE FUNCTION");
4061 : : else
4062 : 0 : COMPLETE_WITH("WHEN (", "EXECUTE PROCEDURE");
4063 : : }
4064 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4065 : : "WHEN", "(*)") ||
4066 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4067 : : "WHEN", "(*)"))
4068 : : {
4069 [ # # ]: 0 : if (pset.sversion >= 110000)
4070 : 0 : COMPLETE_WITH("EXECUTE FUNCTION");
4071 : : else
4072 : 0 : COMPLETE_WITH("EXECUTE PROCEDURE");
4073 : : }
4074 : :
4075 : : /*
4076 : : * Complete CREATE [ OR REPLACE ] TRIGGER ... EXECUTE with
4077 : : * PROCEDURE|FUNCTION.
4078 : : */
4079 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4080 : : "EXECUTE") ||
4081 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4082 : : "EXECUTE"))
4083 : : {
4084 [ # # ]: 0 : if (pset.sversion >= 110000)
4085 : 0 : COMPLETE_WITH("FUNCTION");
4086 : : else
4087 : 0 : COMPLETE_WITH("PROCEDURE");
4088 : : }
4089 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4090 : : "EXECUTE", "FUNCTION|PROCEDURE") ||
4091 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4092 : : "EXECUTE", "FUNCTION|PROCEDURE"))
4093 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
4094 : :
4095 : : /* CREATE ROLE,USER,GROUP <name> */
4096 : 0 : else if (Matches("CREATE", "ROLE|GROUP|USER", MatchAny) &&
4097 [ # # ]: 0 : !TailMatches("USER", "MAPPING"))
4098 : 0 : COMPLETE_WITH("ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB",
4099 : : "CREATEROLE", "ENCRYPTED PASSWORD", "IN", "INHERIT",
4100 : : "LOGIN", "NOBYPASSRLS",
4101 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
4102 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
4103 : : "REPLICATION", "ROLE", "SUPERUSER", "SYSID",
4104 : : "VALID UNTIL", "WITH");
4105 : :
4106 : : /* CREATE ROLE,USER,GROUP <name> WITH */
4107 : 0 : else if (Matches("CREATE", "ROLE|GROUP|USER", MatchAny, "WITH"))
4108 : : /* Similar to the above, but don't complete "WITH" again. */
4109 : 0 : COMPLETE_WITH("ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB",
4110 : : "CREATEROLE", "ENCRYPTED PASSWORD", "IN", "INHERIT",
4111 : : "LOGIN", "NOBYPASSRLS",
4112 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
4113 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
4114 : : "REPLICATION", "ROLE", "SUPERUSER", "SYSID",
4115 : : "VALID UNTIL");
4116 : :
4117 : : /* complete CREATE ROLE,USER,GROUP <name> IN with ROLE,GROUP */
4118 : 0 : else if (Matches("CREATE", "ROLE|USER|GROUP", MatchAny, "IN"))
4119 : 0 : COMPLETE_WITH("GROUP", "ROLE");
4120 : :
4121 : : /* CREATE TYPE */
4122 : 0 : else if (Matches("CREATE", "TYPE", MatchAny))
4123 : 0 : COMPLETE_WITH("(", "AS");
4124 : 0 : else if (Matches("CREATE", "TYPE", MatchAny, "AS"))
4125 : 0 : COMPLETE_WITH("ENUM", "RANGE", "(");
4126 : 0 : else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "("))
4127 : : {
4128 [ # # ]: 0 : if (TailMatches("(|*,", MatchAny))
4129 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
4130 [ # # ]: 0 : else if (TailMatches("(|*,", MatchAny, MatchAnyExcept("*)")))
4131 : 0 : COMPLETE_WITH("COLLATE", ",", ")");
4132 : : }
4133 : 0 : else if (Matches("CREATE", "TYPE", MatchAny, "AS", "ENUM|RANGE"))
4134 : 0 : COMPLETE_WITH("(");
4135 : 0 : else if (HeadMatches("CREATE", "TYPE", MatchAny, "("))
4136 : : {
4137 [ # # ]: 0 : if (TailMatches("(|*,"))
4138 : 0 : COMPLETE_WITH("INPUT", "OUTPUT", "RECEIVE", "SEND",
4139 : : "TYPMOD_IN", "TYPMOD_OUT", "ANALYZE", "SUBSCRIPT",
4140 : : "INTERNALLENGTH", "PASSEDBYVALUE", "ALIGNMENT",
4141 : : "STORAGE", "LIKE", "CATEGORY", "PREFERRED",
4142 : : "DEFAULT", "ELEMENT", "DELIMITER",
4143 : : "COLLATABLE");
4144 [ # # ]: 0 : else if (TailMatches("(*|*,", MatchAnyExcept("*=")))
4145 : 0 : COMPLETE_WITH("=");
4146 [ # # ]: 0 : else if (TailMatches("=", MatchAnyExcept("*)")))
4147 : 0 : COMPLETE_WITH(",", ")");
4148 : : }
4149 : 0 : else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "RANGE", "("))
4150 : : {
4151 [ # # ]: 0 : if (TailMatches("(|*,"))
4152 : 0 : COMPLETE_WITH("SUBTYPE", "SUBTYPE_OPCLASS", "COLLATION",
4153 : : "CANONICAL", "SUBTYPE_DIFF",
4154 : : "MULTIRANGE_TYPE_NAME");
4155 [ # # ]: 0 : else if (TailMatches("(*|*,", MatchAnyExcept("*=")))
4156 : 0 : COMPLETE_WITH("=");
4157 [ # # ]: 0 : else if (TailMatches("=", MatchAnyExcept("*)")))
4158 : 0 : COMPLETE_WITH(",", ")");
4159 : : }
4160 : :
4161 : : /* CREATE VIEW --- is allowed inside CREATE SCHEMA, so use TailMatches */
4162 : : /* Complete CREATE [ OR REPLACE ] VIEW <name> with AS or WITH */
4163 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny) ||
4164 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny))
4165 : 0 : COMPLETE_WITH("AS", "WITH");
4166 : : /* Complete "CREATE [ OR REPLACE ] VIEW <sth> AS with "SELECT" */
4167 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "AS") ||
4168 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "AS"))
4169 : 0 : COMPLETE_WITH("SELECT");
4170 : : /* CREATE [ OR REPLACE ] VIEW <name> WITH ( yyy [= zzz] ) */
4171 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH") ||
4172 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH"))
4173 : 0 : COMPLETE_WITH("(");
4174 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(") ||
4175 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "("))
4176 : 0 : COMPLETE_WITH_LIST(view_optional_parameters);
4177 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(", "check_option") ||
4178 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(", "check_option"))
4179 : 0 : COMPLETE_WITH("=");
4180 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(", "check_option", "=") ||
4181 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(", "check_option", "="))
4182 : 0 : COMPLETE_WITH("local", "cascaded");
4183 : : /* CREATE [ OR REPLACE ] VIEW <name> WITH ( ... ) AS */
4184 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(*)") ||
4185 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(*)"))
4186 : 0 : COMPLETE_WITH("AS");
4187 : : /* CREATE [ OR REPLACE ] VIEW <name> WITH ( ... ) AS SELECT */
4188 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(*)", "AS") ||
4189 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(*)", "AS"))
4190 : 0 : COMPLETE_WITH("SELECT");
4191 : :
4192 : : /* CREATE MATERIALIZED VIEW */
4193 : 0 : else if (Matches("CREATE", "MATERIALIZED"))
4194 : 0 : COMPLETE_WITH("VIEW");
4195 : : /* Complete CREATE MATERIALIZED VIEW <name> with AS or USING */
4196 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny))
4197 : 0 : COMPLETE_WITH("AS", "USING");
4198 : :
4199 : : /*
4200 : : * Complete CREATE MATERIALIZED VIEW <name> USING with list of access
4201 : : * methods
4202 : : */
4203 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING"))
4204 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
4205 : : /* Complete CREATE MATERIALIZED VIEW <name> USING <access method> with AS */
4206 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny))
4207 : 0 : COMPLETE_WITH("AS");
4208 : :
4209 : : /*
4210 : : * Complete CREATE MATERIALIZED VIEW <name> [USING <access method> ] AS
4211 : : * with "SELECT"
4212 : : */
4213 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "AS") ||
4214 : : Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny, "AS"))
4215 : 0 : COMPLETE_WITH("SELECT");
4216 : :
4217 : : /* CREATE EVENT TRIGGER */
4218 : 0 : else if (Matches("CREATE", "EVENT"))
4219 : 0 : COMPLETE_WITH("TRIGGER");
4220 : : /* Complete CREATE EVENT TRIGGER <name> with ON */
4221 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny))
4222 : 0 : COMPLETE_WITH("ON");
4223 : : /* Complete CREATE EVENT TRIGGER <name> ON with event_type */
4224 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny, "ON"))
4225 : 0 : COMPLETE_WITH("ddl_command_start", "ddl_command_end", "login",
4226 : : "sql_drop", "table_rewrite");
4227 : :
4228 : : /*
4229 : : * Complete CREATE EVENT TRIGGER <name> ON <event_type>. EXECUTE FUNCTION
4230 : : * is the recommended grammar instead of EXECUTE PROCEDURE in version 11
4231 : : * and upwards.
4232 : : */
4233 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny, "ON", MatchAny))
4234 : : {
4235 [ # # ]: 0 : if (pset.sversion >= 110000)
4236 : 0 : COMPLETE_WITH("WHEN TAG IN (", "EXECUTE FUNCTION");
4237 : : else
4238 : 0 : COMPLETE_WITH("WHEN TAG IN (", "EXECUTE PROCEDURE");
4239 : : }
4240 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAnyN, "WHEN|AND", MatchAny, "IN", "(*)"))
4241 : : {
4242 [ # # ]: 0 : if (pset.sversion >= 110000)
4243 : 0 : COMPLETE_WITH("EXECUTE FUNCTION");
4244 : : else
4245 : 0 : COMPLETE_WITH("EXECUTE PROCEDURE");
4246 : : }
4247 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAnyN, "EXECUTE", "FUNCTION|PROCEDURE"))
4248 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
4249 : :
4250 : : /* DEALLOCATE */
4251 : 0 : else if (Matches("DEALLOCATE"))
4252 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_prepared_statements,
4253 : : "ALL");
4254 : :
4255 : : /* DECLARE */
4256 : :
4257 : : /*
4258 : : * Complete DECLARE <name> with one of BINARY, ASENSITIVE, INSENSITIVE,
4259 : : * SCROLL, NO SCROLL, and CURSOR.
4260 : : */
4261 : 0 : else if (Matches("DECLARE", MatchAny))
4262 : 0 : COMPLETE_WITH("BINARY", "ASENSITIVE", "INSENSITIVE", "SCROLL", "NO SCROLL",
4263 : : "CURSOR");
4264 : :
4265 : : /*
4266 : : * Complete DECLARE ... <option> with other options. The PostgreSQL parser
4267 : : * allows DECLARE options to be specified in any order. But the
4268 : : * tab-completion follows the ordering of them that the SQL standard
4269 : : * provides, like the syntax of DECLARE command in the documentation
4270 : : * indicates.
4271 : : */
4272 : 0 : else if (Matches("DECLARE", MatchAnyN, "BINARY"))
4273 : 0 : COMPLETE_WITH("ASENSITIVE", "INSENSITIVE", "SCROLL", "NO SCROLL", "CURSOR");
4274 : 0 : else if (Matches("DECLARE", MatchAnyN, "ASENSITIVE|INSENSITIVE"))
4275 : 0 : COMPLETE_WITH("SCROLL", "NO SCROLL", "CURSOR");
4276 : 0 : else if (Matches("DECLARE", MatchAnyN, "SCROLL"))
4277 : 0 : COMPLETE_WITH("CURSOR");
4278 : : /* Complete DECLARE ... [options] NO with SCROLL */
4279 : 0 : else if (Matches("DECLARE", MatchAnyN, "NO"))
4280 : 0 : COMPLETE_WITH("SCROLL");
4281 : :
4282 : : /*
4283 : : * Complete DECLARE ... CURSOR with one of WITH HOLD, WITHOUT HOLD, and
4284 : : * FOR
4285 : : */
4286 : 0 : else if (Matches("DECLARE", MatchAnyN, "CURSOR"))
4287 : 0 : COMPLETE_WITH("WITH HOLD", "WITHOUT HOLD", "FOR");
4288 : : /* Complete DECLARE ... CURSOR WITH|WITHOUT with HOLD */
4289 : 0 : else if (Matches("DECLARE", MatchAnyN, "CURSOR", "WITH|WITHOUT"))
4290 : 0 : COMPLETE_WITH("HOLD");
4291 : : /* Complete DECLARE ... CURSOR WITH|WITHOUT HOLD with FOR */
4292 : 0 : else if (Matches("DECLARE", MatchAnyN, "CURSOR", "WITH|WITHOUT", "HOLD"))
4293 : 0 : COMPLETE_WITH("FOR");
4294 : :
4295 : : /* DELETE --- can be inside EXPLAIN, RULE, etc */
4296 : : /* Complete DELETE with "FROM" */
4297 : 0 : else if (Matches("DELETE"))
4298 : 0 : COMPLETE_WITH("FROM");
4299 : : /* Complete DELETE FROM with a list of tables */
4300 : 0 : else if (TailMatches("DELETE", "FROM"))
4301 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
4302 : : /* Complete DELETE FROM <table> */
4303 : 0 : else if (TailMatches("DELETE", "FROM", MatchAny))
4304 : 0 : COMPLETE_WITH("USING", "WHERE");
4305 : : /* Complete DELETE FROM <table> USING with relations supporting SELECT */
4306 : 0 : else if (TailMatches("DELETE", "FROM", MatchAny, "USING"))
4307 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
4308 : :
4309 : : /* DISCARD */
4310 : 0 : else if (Matches("DISCARD"))
4311 : 0 : COMPLETE_WITH("ALL", "PLANS", "SEQUENCES", "TEMP");
4312 : :
4313 : : /* DO */
4314 : 0 : else if (Matches("DO"))
4315 : 0 : COMPLETE_WITH("LANGUAGE");
4316 : :
4317 : : /* DROP */
4318 : : /* Complete DROP object with CASCADE / RESTRICT */
4319 : 0 : else if (Matches("DROP",
4320 : : "COLLATION|CONVERSION|DOMAIN|EXTENSION|LANGUAGE|PUBLICATION|SCHEMA|SEQUENCE|SERVER|SUBSCRIPTION|STATISTICS|TABLE|TYPE|VIEW",
4321 : : MatchAny) ||
4322 : : Matches("DROP", "ACCESS", "METHOD", MatchAny) ||
4323 : : Matches("DROP", "EVENT", "TRIGGER", MatchAny) ||
4324 : : Matches("DROP", "FOREIGN", "DATA", "WRAPPER", MatchAny) ||
4325 : : Matches("DROP", "FOREIGN", "TABLE", MatchAny) ||
4326 : : Matches("DROP", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny))
4327 : 1 : COMPLETE_WITH("CASCADE", "RESTRICT");
4328 : 1 : else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny) &&
4329 [ # # ]: 0 : ends_with(prev_wd, ')'))
4330 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4331 : :
4332 : : /* help completing some of the variants */
4333 : 0 : else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny))
4334 : 0 : COMPLETE_WITH("(");
4335 : 0 : else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny, "("))
4336 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
4337 : 0 : else if (Matches("DROP", "FOREIGN"))
4338 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
4339 : 0 : else if (Matches("DROP", "DATABASE", MatchAny))
4340 : 0 : COMPLETE_WITH("WITH (");
4341 [ # # ]: 0 : else if (HeadMatches("DROP", "DATABASE") && (ends_with(prev_wd, '(')))
4342 : 0 : COMPLETE_WITH("FORCE");
4343 : :
4344 : : /* DROP INDEX */
4345 : 0 : else if (Matches("DROP", "INDEX"))
4346 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
4347 : : "CONCURRENTLY");
4348 : 0 : else if (Matches("DROP", "INDEX", "CONCURRENTLY"))
4349 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
4350 : 0 : else if (Matches("DROP", "INDEX", MatchAny))
4351 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4352 : 0 : else if (Matches("DROP", "INDEX", "CONCURRENTLY", MatchAny))
4353 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4354 : :
4355 : : /* DROP MATERIALIZED VIEW */
4356 : 0 : else if (Matches("DROP", "MATERIALIZED"))
4357 : 0 : COMPLETE_WITH("VIEW");
4358 : 0 : else if (Matches("DROP", "MATERIALIZED", "VIEW"))
4359 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
4360 : 0 : else if (Matches("DROP", "MATERIALIZED", "VIEW", MatchAny))
4361 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4362 : :
4363 : : /* DROP OWNED BY */
4364 : 0 : else if (Matches("DROP", "OWNED"))
4365 : 0 : COMPLETE_WITH("BY");
4366 : 0 : else if (Matches("DROP", "OWNED", "BY"))
4367 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
4368 : 0 : else if (Matches("DROP", "OWNED", "BY", MatchAny))
4369 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4370 : :
4371 : : /* DROP TEXT SEARCH */
4372 : 0 : else if (Matches("DROP", "TEXT", "SEARCH"))
4373 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
4374 : :
4375 : : /* DROP TRIGGER */
4376 : 0 : else if (Matches("DROP", "TRIGGER", MatchAny))
4377 : 0 : COMPLETE_WITH("ON");
4378 : 0 : else if (Matches("DROP", "TRIGGER", MatchAny, "ON"))
4379 : : {
4380 : 0 : set_completion_reference(prev2_wd);
4381 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
4382 : : }
4383 : 0 : else if (Matches("DROP", "TRIGGER", MatchAny, "ON", MatchAny))
4384 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4385 : :
4386 : : /* DROP ACCESS METHOD */
4387 : 0 : else if (Matches("DROP", "ACCESS"))
4388 : 0 : COMPLETE_WITH("METHOD");
4389 : 0 : else if (Matches("DROP", "ACCESS", "METHOD"))
4390 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
4391 : :
4392 : : /* DROP EVENT TRIGGER */
4393 : 0 : else if (Matches("DROP", "EVENT"))
4394 : 0 : COMPLETE_WITH("TRIGGER");
4395 : 0 : else if (Matches("DROP", "EVENT", "TRIGGER"))
4396 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
4397 : :
4398 : : /* DROP POLICY <name> */
4399 : 0 : else if (Matches("DROP", "POLICY"))
4400 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_policies);
4401 : : /* DROP POLICY <name> ON */
4402 : 0 : else if (Matches("DROP", "POLICY", MatchAny))
4403 : 0 : COMPLETE_WITH("ON");
4404 : : /* DROP POLICY <name> ON <table> */
4405 : 0 : else if (Matches("DROP", "POLICY", MatchAny, "ON"))
4406 : : {
4407 : 0 : set_completion_reference(prev2_wd);
4408 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
4409 : : }
4410 : 0 : else if (Matches("DROP", "POLICY", MatchAny, "ON", MatchAny))
4411 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4412 : :
4413 : : /* DROP RULE */
4414 : 0 : else if (Matches("DROP", "RULE", MatchAny))
4415 : 0 : COMPLETE_WITH("ON");
4416 : 0 : else if (Matches("DROP", "RULE", MatchAny, "ON"))
4417 : : {
4418 : 0 : set_completion_reference(prev2_wd);
4419 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
4420 : : }
4421 : 0 : else if (Matches("DROP", "RULE", MatchAny, "ON", MatchAny))
4422 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4423 : :
4424 : : /* DROP TRANSFORM */
4425 : 0 : else if (Matches("DROP", "TRANSFORM"))
4426 : 0 : COMPLETE_WITH("FOR");
4427 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR"))
4428 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
4429 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny))
4430 : 0 : COMPLETE_WITH("LANGUAGE");
4431 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
4432 : : {
4433 : 0 : set_completion_reference(prev2_wd);
4434 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
4435 : : }
4436 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny, "LANGUAGE", MatchAny))
4437 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4438 : :
4439 : : /* EXECUTE */
4440 : 0 : else if (Matches("EXECUTE"))
4441 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements);
4442 : :
4443 : : /*
4444 : : * EXPLAIN [ ( option [, ...] ) ] statement
4445 : : * EXPLAIN [ ANALYZE ] [ VERBOSE ] statement
4446 : : */
4447 : 0 : else if (Matches("EXPLAIN"))
4448 : 0 : COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4449 : : "MERGE INTO", "EXECUTE", "ANALYZE", "VERBOSE");
4450 : 0 : else if (HeadMatches("EXPLAIN", "(*") &&
4451 [ # # ]: 0 : !HeadMatches("EXPLAIN", "(*)"))
4452 : : {
4453 : : /*
4454 : : * This fires if we're in an unfinished parenthesized option list.
4455 : : * get_previous_words treats a completed parenthesized option list as
4456 : : * one word, so the above test is correct.
4457 : : */
4458 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
4459 : 0 : COMPLETE_WITH("ANALYZE", "VERBOSE", "COSTS", "SETTINGS", "GENERIC_PLAN",
4460 : : "BUFFERS", "SERIALIZE", "WAL", "TIMING", "SUMMARY",
4461 : : "MEMORY", "IO", "FORMAT");
4462 [ # # ]: 0 : else if (TailMatches("ANALYZE|VERBOSE|COSTS|SETTINGS|GENERIC_PLAN|BUFFERS|WAL|TIMING|SUMMARY|MEMORY|IO"))
4463 : 0 : COMPLETE_WITH("ON", "OFF");
4464 [ # # ]: 0 : else if (TailMatches("SERIALIZE"))
4465 : 0 : COMPLETE_WITH("TEXT", "NONE", "BINARY");
4466 [ # # ]: 0 : else if (TailMatches("FORMAT"))
4467 : 0 : COMPLETE_WITH("TEXT", "XML", "JSON", "YAML");
4468 : : }
4469 : 0 : else if (Matches("EXPLAIN", "ANALYZE"))
4470 : 0 : COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4471 : : "MERGE INTO", "EXECUTE", "VERBOSE");
4472 : 0 : else if (Matches("EXPLAIN", "(*)") ||
4473 : : Matches("EXPLAIN", "VERBOSE") ||
4474 : : Matches("EXPLAIN", "ANALYZE", "VERBOSE"))
4475 : 0 : COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4476 : : "MERGE INTO", "EXECUTE");
4477 : :
4478 : : /* FETCH && MOVE */
4479 : :
4480 : : /*
4481 : : * Complete FETCH with one of ABSOLUTE, BACKWARD, FORWARD, RELATIVE, ALL,
4482 : : * NEXT, PRIOR, FIRST, LAST, FROM, IN, and a list of cursors
4483 : : */
4484 : 0 : else if (Matches("FETCH|MOVE"))
4485 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4486 : : "ABSOLUTE",
4487 : : "BACKWARD",
4488 : : "FORWARD",
4489 : : "RELATIVE",
4490 : : "ALL",
4491 : : "NEXT",
4492 : : "PRIOR",
4493 : : "FIRST",
4494 : : "LAST",
4495 : : "FROM",
4496 : : "IN");
4497 : :
4498 : : /*
4499 : : * Complete FETCH BACKWARD or FORWARD with one of ALL, FROM, IN, and a
4500 : : * list of cursors
4501 : : */
4502 : 0 : else if (Matches("FETCH|MOVE", "BACKWARD|FORWARD"))
4503 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4504 : : "ALL",
4505 : : "FROM",
4506 : : "IN");
4507 : :
4508 : : /*
4509 : : * Complete FETCH <direction> with "FROM" or "IN". These are equivalent,
4510 : : * but we may as well tab-complete both: perhaps some users prefer one
4511 : : * variant or the other.
4512 : : */
4513 : 0 : else if (Matches("FETCH|MOVE", "ABSOLUTE|BACKWARD|FORWARD|RELATIVE",
4514 : : MatchAnyExcept("FROM|IN")) ||
4515 : : Matches("FETCH|MOVE", "ALL|NEXT|PRIOR|FIRST|LAST"))
4516 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4517 : : "FROM",
4518 : : "IN");
4519 : : /* Complete FETCH <direction> "FROM" or "IN" with a list of cursors */
4520 : 0 : else if (Matches("FETCH|MOVE", MatchAnyN, "FROM|IN"))
4521 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_cursors);
4522 : :
4523 : : /* FOREIGN DATA WRAPPER */
4524 : : /* applies in ALTER/DROP FDW and in CREATE SERVER */
4525 : 0 : else if (TailMatches("FOREIGN", "DATA", "WRAPPER") &&
4526 [ # # ]: 0 : !TailMatches("CREATE", MatchAny, MatchAny, MatchAny))
4527 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
4528 : : /* applies in CREATE SERVER */
4529 : 0 : else if (Matches("CREATE", "SERVER", MatchAnyN, "FOREIGN", "DATA", "WRAPPER", MatchAny))
4530 : 0 : COMPLETE_WITH("OPTIONS");
4531 : :
4532 : : /* FOREIGN TABLE */
4533 : 0 : else if (TailMatches("FOREIGN", "TABLE") &&
4534 [ # # ]: 0 : !TailMatches("CREATE", MatchAny, MatchAny))
4535 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
4536 : :
4537 : : /* FOREIGN SERVER */
4538 : 0 : else if (TailMatches("FOREIGN", "SERVER"))
4539 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
4540 : :
4541 : : /*
4542 : : * GRANT and REVOKE are allowed inside CREATE SCHEMA and
4543 : : * ALTER DEFAULT PRIVILEGES, so use TailMatches
4544 : : */
4545 : : /* Complete GRANT/REVOKE with a list of roles and privileges */
4546 : 0 : else if (TailMatches("GRANT|REVOKE") ||
4547 : : TailMatches("REVOKE", "ADMIN|GRANT|INHERIT|SET", "OPTION", "FOR"))
4548 : : {
4549 : : /*
4550 : : * With ALTER DEFAULT PRIVILEGES, restrict completion to grantable
4551 : : * privileges (can't grant roles)
4552 : : */
4553 [ # # ]: 0 : if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
4554 : : {
4555 [ # # # # ]: 0 : if (TailMatches("GRANT") ||
4556 : 0 : TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
4557 : 0 : COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
4558 : : "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
4559 : : "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL");
4560 [ # # ]: 0 : else if (TailMatches("REVOKE"))
4561 : 0 : COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
4562 : : "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
4563 : : "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL",
4564 : : "GRANT OPTION FOR");
4565 : : }
4566 [ # # ]: 0 : else if (TailMatches("GRANT"))
4567 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4568 : : Privilege_options_of_grant_and_revoke);
4569 [ # # ]: 0 : else if (TailMatches("REVOKE"))
4570 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4571 : : Privilege_options_of_grant_and_revoke,
4572 : : "GRANT OPTION FOR",
4573 : : "ADMIN OPTION FOR",
4574 : : "INHERIT OPTION FOR",
4575 : : "SET OPTION FOR");
4576 [ # # ]: 0 : else if (TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
4577 : 0 : COMPLETE_WITH(Privilege_options_of_grant_and_revoke);
4578 [ # # ]: 0 : else if (TailMatches("REVOKE", "ADMIN|INHERIT|SET", "OPTION", "FOR"))
4579 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
4580 : : }
4581 : :
4582 : 0 : else if (TailMatches("GRANT|REVOKE", "ALTER") ||
4583 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER"))
4584 : 0 : COMPLETE_WITH("SYSTEM");
4585 : :
4586 : 0 : else if (TailMatches("REVOKE", "SET"))
4587 : 0 : COMPLETE_WITH("ON PARAMETER", "OPTION FOR");
4588 : 0 : else if (TailMatches("GRANT", "SET") ||
4589 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "SET") ||
4590 : : TailMatches("GRANT|REVOKE", "ALTER", "SYSTEM") ||
4591 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER", "SYSTEM"))
4592 : 0 : COMPLETE_WITH("ON PARAMETER");
4593 : :
4594 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "PARAMETER") ||
4595 : : TailMatches("GRANT|REVOKE", MatchAny, MatchAny, "ON", "PARAMETER") ||
4596 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "PARAMETER") ||
4597 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, MatchAny, "ON", "PARAMETER"))
4598 : 0 : COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_alter_system_set_vars);
4599 : :
4600 : 0 : else if (TailMatches("GRANT", MatchAny, "ON", "PARAMETER", MatchAny) ||
4601 : : TailMatches("GRANT", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny))
4602 : 0 : COMPLETE_WITH("TO");
4603 : :
4604 : 0 : else if (TailMatches("REVOKE", MatchAny, "ON", "PARAMETER", MatchAny) ||
4605 : : TailMatches("REVOKE", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny) ||
4606 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "PARAMETER", MatchAny) ||
4607 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny))
4608 : 0 : COMPLETE_WITH("FROM");
4609 : :
4610 : : /*
4611 : : * Complete GRANT/REVOKE <privilege> with "ON", GRANT/REVOKE <role> with
4612 : : * TO/FROM
4613 : : */
4614 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny) ||
4615 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny))
4616 : : {
4617 [ # # ]: 0 : if (TailMatches("SELECT|INSERT|UPDATE|DELETE|TRUNCATE|REFERENCES|TRIGGER|CREATE|CONNECT|TEMPORARY|TEMP|EXECUTE|USAGE|MAINTAIN|ALL"))
4618 : 0 : COMPLETE_WITH("ON");
4619 [ # # ]: 0 : else if (TailMatches("GRANT", MatchAny))
4620 : 0 : COMPLETE_WITH("TO");
4621 : : else
4622 : 0 : COMPLETE_WITH("FROM");
4623 : : }
4624 : :
4625 : : /*
4626 : : * Complete GRANT/REVOKE <sth> ON with a list of appropriate relations.
4627 : : *
4628 : : * Note: GRANT/REVOKE can get quite complex; tab-completion as implemented
4629 : : * here will only work if the privilege list contains exactly one
4630 : : * privilege.
4631 : : */
4632 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON") ||
4633 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON"))
4634 : : {
4635 : : /*
4636 : : * With ALTER DEFAULT PRIVILEGES, restrict completion to the kinds of
4637 : : * objects supported.
4638 : : */
4639 [ # # ]: 0 : if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
4640 : 0 : COMPLETE_WITH("TABLES", "SEQUENCES", "FUNCTIONS", "PROCEDURES", "ROUTINES", "TYPES", "SCHEMAS", "LARGE OBJECTS");
4641 : : else
4642 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_grantables,
4643 : : "ALL FUNCTIONS IN SCHEMA",
4644 : : "ALL PROCEDURES IN SCHEMA",
4645 : : "ALL ROUTINES IN SCHEMA",
4646 : : "ALL SEQUENCES IN SCHEMA",
4647 : : "ALL TABLES IN SCHEMA",
4648 : : "DATABASE",
4649 : : "DOMAIN",
4650 : : "FOREIGN DATA WRAPPER",
4651 : : "FOREIGN SERVER",
4652 : : "FUNCTION",
4653 : : "LANGUAGE",
4654 : : "LARGE OBJECT",
4655 : : "PARAMETER",
4656 : : "PROCEDURE",
4657 : : "ROUTINE",
4658 : : "SCHEMA",
4659 : : "SEQUENCE",
4660 : : "TABLE",
4661 : : "TABLESPACE",
4662 : : "TYPE");
4663 : : }
4664 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "ALL") ||
4665 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "ALL"))
4666 : 0 : COMPLETE_WITH("FUNCTIONS IN SCHEMA",
4667 : : "PROCEDURES IN SCHEMA",
4668 : : "ROUTINES IN SCHEMA",
4669 : : "SEQUENCES IN SCHEMA",
4670 : : "TABLES IN SCHEMA");
4671 : :
4672 : : /*
4673 : : * Complete "GRANT/REVOKE * ON DATABASE/DOMAIN/..." with a list of
4674 : : * appropriate objects or keywords.
4675 : : *
4676 : : * Complete "GRANT/REVOKE * ON *" with "TO/FROM".
4677 : : */
4678 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", MatchAny) ||
4679 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", MatchAny))
4680 : : {
4681 [ # # ]: 0 : if (TailMatches("DATABASE"))
4682 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
4683 [ # # ]: 0 : else if (TailMatches("DOMAIN"))
4684 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
4685 [ # # ]: 0 : else if (TailMatches("FUNCTION"))
4686 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
4687 [ # # ]: 0 : else if (TailMatches("FOREIGN"))
4688 : 0 : COMPLETE_WITH("DATA WRAPPER", "SERVER");
4689 [ # # ]: 0 : else if (TailMatches("LANGUAGE"))
4690 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
4691 [ # # ]: 0 : else if (TailMatches("LARGE"))
4692 : : {
4693 [ # # ]: 0 : if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
4694 : 0 : COMPLETE_WITH("OBJECTS");
4695 : : else
4696 : 0 : COMPLETE_WITH("OBJECT");
4697 : : }
4698 [ # # ]: 0 : else if (TailMatches("PROCEDURE"))
4699 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_procedures);
4700 [ # # ]: 0 : else if (TailMatches("ROUTINE"))
4701 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
4702 [ # # ]: 0 : else if (TailMatches("SCHEMA"))
4703 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
4704 [ # # ]: 0 : else if (TailMatches("SEQUENCE"))
4705 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences);
4706 [ # # ]: 0 : else if (TailMatches("TABLE"))
4707 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables);
4708 [ # # ]: 0 : else if (TailMatches("TABLESPACE"))
4709 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
4710 [ # # ]: 0 : else if (TailMatches("TYPE"))
4711 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
4712 [ # # ]: 0 : else if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny))
4713 : 0 : COMPLETE_WITH("TO");
4714 : : else
4715 : 0 : COMPLETE_WITH("FROM");
4716 : : }
4717 : :
4718 : : /*
4719 : : * Complete "GRANT/REVOKE ... TO/FROM" with username, PUBLIC,
4720 : : * CURRENT_ROLE, CURRENT_USER, or SESSION_USER.
4721 : : */
4722 : 0 : else if (Matches("GRANT", MatchAnyN, "TO") ||
4723 : : Matches("REVOKE", MatchAnyN, "FROM"))
4724 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4725 : : Keywords_for_list_of_grant_roles);
4726 : :
4727 : : /*
4728 : : * Offer grant options after that.
4729 : : */
4730 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny))
4731 : 0 : COMPLETE_WITH("WITH ADMIN",
4732 : : "WITH INHERIT",
4733 : : "WITH SET",
4734 : : "WITH GRANT OPTION",
4735 : : "GRANTED BY");
4736 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH"))
4737 : 0 : COMPLETE_WITH("ADMIN",
4738 : : "INHERIT",
4739 : : "SET",
4740 : : "GRANT OPTION");
4741 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", "ADMIN|INHERIT|SET"))
4742 : 0 : COMPLETE_WITH("OPTION", "TRUE", "FALSE");
4743 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", MatchAny, "OPTION"))
4744 : 0 : COMPLETE_WITH("GRANTED BY");
4745 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", MatchAny, "OPTION", "GRANTED", "BY"))
4746 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4747 : : Keywords_for_list_of_grant_roles);
4748 : : /* Complete "ALTER DEFAULT PRIVILEGES ... GRANT/REVOKE ... TO/FROM */
4749 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", MatchAnyN, "TO|FROM"))
4750 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4751 : : Keywords_for_list_of_grant_roles);
4752 : : /* Offer WITH GRANT OPTION after that */
4753 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", MatchAnyN, "TO", MatchAny))
4754 : 0 : COMPLETE_WITH("WITH GRANT OPTION");
4755 : : /* Complete "GRANT/REVOKE ... ON * *" with TO/FROM */
4756 : 0 : else if (Matches("GRANT|REVOKE", MatchAnyN, "ON", MatchAny, MatchAny) &&
4757 [ # # # # ]: 0 : !TailMatches("FOREIGN", "SERVER") && !TailMatches("LARGE", "OBJECT"))
4758 : : {
4759 [ # # ]: 0 : if (Matches("GRANT", MatchAnyN, "ON", MatchAny, MatchAny))
4760 : 0 : COMPLETE_WITH("TO");
4761 : : else
4762 : 0 : COMPLETE_WITH("FROM");
4763 : : }
4764 : :
4765 : : /* Complete "GRANT/REVOKE * ON ALL * IN SCHEMA *" with TO/FROM */
4766 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny) ||
4767 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny))
4768 : : {
4769 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4770 : 0 : COMPLETE_WITH("TO");
4771 : : else
4772 : 0 : COMPLETE_WITH("FROM");
4773 : : }
4774 : :
4775 : : /* Complete "GRANT/REVOKE * ON FOREIGN DATA WRAPPER *" with TO/FROM */
4776 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny) ||
4777 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny))
4778 : : {
4779 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4780 : 0 : COMPLETE_WITH("TO");
4781 : : else
4782 : 0 : COMPLETE_WITH("FROM");
4783 : : }
4784 : :
4785 : : /* Complete "GRANT/REVOKE * ON FOREIGN SERVER *" with TO/FROM */
4786 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny) ||
4787 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny))
4788 : : {
4789 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4790 : 0 : COMPLETE_WITH("TO");
4791 : : else
4792 : 0 : COMPLETE_WITH("FROM");
4793 : : }
4794 : :
4795 : : /* Complete "GRANT/REVOKE * ON LARGE OBJECT *" with TO/FROM */
4796 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "LARGE", "OBJECT", MatchAny) ||
4797 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "LARGE", "OBJECT", MatchAny))
4798 : : {
4799 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4800 : 0 : COMPLETE_WITH("TO");
4801 : : else
4802 : 0 : COMPLETE_WITH("FROM");
4803 : : }
4804 : :
4805 : : /* Complete "GRANT/REVOKE * ON LARGE OBJECTS" with TO/FROM */
4806 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "LARGE", "OBJECTS") ||
4807 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "LARGE", "OBJECTS"))
4808 : : {
4809 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny))
4810 : 0 : COMPLETE_WITH("TO");
4811 : : else
4812 : 0 : COMPLETE_WITH("FROM");
4813 : : }
4814 : :
4815 : : /* GROUP BY */
4816 : 0 : else if (TailMatches("FROM", MatchAny, "GROUP"))
4817 : 0 : COMPLETE_WITH("BY");
4818 : :
4819 : : /* IMPORT FOREIGN SCHEMA */
4820 : 0 : else if (Matches("IMPORT"))
4821 : 0 : COMPLETE_WITH("FOREIGN SCHEMA");
4822 : 0 : else if (Matches("IMPORT", "FOREIGN"))
4823 : 0 : COMPLETE_WITH("SCHEMA");
4824 : 0 : else if (Matches("IMPORT", "FOREIGN", "SCHEMA", MatchAny))
4825 : 0 : COMPLETE_WITH("EXCEPT (", "FROM SERVER", "LIMIT TO (");
4826 : 0 : else if (TailMatches("LIMIT", "TO", "(*)") ||
4827 : : Matches("IMPORT", "FOREIGN", "SCHEMA", MatchAny, "EXCEPT", "(*)"))
4828 : 0 : COMPLETE_WITH("FROM SERVER");
4829 : 0 : else if (TailMatches("FROM", "SERVER", MatchAny))
4830 : 0 : COMPLETE_WITH("INTO");
4831 : 0 : else if (TailMatches("FROM", "SERVER", MatchAny, "INTO"))
4832 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
4833 : 0 : else if (TailMatches("FROM", "SERVER", MatchAny, "INTO", MatchAny))
4834 : 0 : COMPLETE_WITH("OPTIONS (");
4835 : :
4836 : : /* INSERT --- can be inside EXPLAIN, RULE, etc */
4837 : : /* Complete NOT MATCHED THEN INSERT */
4838 : 0 : else if (TailMatches("NOT", "MATCHED", "THEN", "INSERT"))
4839 : 0 : COMPLETE_WITH("VALUES", "(");
4840 : : /* Complete INSERT with "INTO" */
4841 : 0 : else if (TailMatches("INSERT"))
4842 : 0 : COMPLETE_WITH("INTO");
4843 : : /* Complete INSERT INTO with table names */
4844 : 0 : else if (TailMatches("INSERT", "INTO"))
4845 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
4846 : : /* Complete "INSERT INTO <table> (" with attribute names */
4847 : 0 : else if (TailMatches("INSERT", "INTO", MatchAny, "("))
4848 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
4849 : :
4850 : : /*
4851 : : * Complete INSERT INTO <table> with "(" or "VALUES" or "SELECT" or
4852 : : * "TABLE" or "DEFAULT VALUES" or "OVERRIDING"
4853 : : */
4854 : 0 : else if (TailMatches("INSERT", "INTO", MatchAny))
4855 : 0 : COMPLETE_WITH("(", "DEFAULT VALUES", "SELECT", "TABLE", "VALUES", "OVERRIDING");
4856 : :
4857 : : /*
4858 : : * Complete INSERT INTO <table> (attribs) with "VALUES" or "SELECT" or
4859 : : * "TABLE" or "OVERRIDING"
4860 : : */
4861 : 0 : else if (TailMatches("INSERT", "INTO", MatchAny, MatchAny) &&
4862 [ # # ]: 0 : ends_with(prev_wd, ')'))
4863 : 0 : COMPLETE_WITH("SELECT", "TABLE", "VALUES", "OVERRIDING");
4864 : :
4865 : : /* Complete OVERRIDING */
4866 : 0 : else if (TailMatches("OVERRIDING"))
4867 : 0 : COMPLETE_WITH("SYSTEM VALUE", "USER VALUE");
4868 : :
4869 : : /* Complete after OVERRIDING clause */
4870 : 0 : else if (TailMatches("OVERRIDING", MatchAny, "VALUE"))
4871 : 0 : COMPLETE_WITH("SELECT", "TABLE", "VALUES");
4872 : :
4873 : : /* Insert an open parenthesis after "VALUES" */
4874 [ # # ]: 0 : else if (TailMatches("VALUES") && !TailMatches("DEFAULT", "VALUES"))
4875 : 0 : COMPLETE_WITH("(");
4876 : :
4877 : : /* LOCK */
4878 : : /* Complete LOCK [TABLE] [ONLY] with a list of tables */
4879 : 0 : else if (Matches("LOCK"))
4880 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
4881 : : "TABLE", "ONLY");
4882 : 0 : else if (Matches("LOCK", "TABLE"))
4883 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
4884 : : "ONLY");
4885 : 0 : else if (Matches("LOCK", "TABLE", "ONLY") || Matches("LOCK", "ONLY"))
4886 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
4887 : : /* For the following, handle the case of a single table only for now */
4888 : :
4889 : : /* Complete LOCK [TABLE] [ONLY] <table> with IN or NOWAIT */
4890 : 0 : else if (Matches("LOCK", MatchAnyExcept("TABLE|ONLY")) ||
4891 : : Matches("LOCK", "TABLE", MatchAnyExcept("ONLY")) ||
4892 : : Matches("LOCK", "ONLY", MatchAny) ||
4893 : : Matches("LOCK", "TABLE", "ONLY", MatchAny))
4894 : 0 : COMPLETE_WITH("IN", "NOWAIT");
4895 : :
4896 : : /* Complete LOCK [TABLE] [ONLY] <table> IN with a lock mode */
4897 : 0 : else if (Matches("LOCK", MatchAnyN, "IN"))
4898 : 0 : COMPLETE_WITH("ACCESS SHARE MODE",
4899 : : "ROW SHARE MODE", "ROW EXCLUSIVE MODE",
4900 : : "SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE",
4901 : : "SHARE ROW EXCLUSIVE MODE",
4902 : : "EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE");
4903 : :
4904 : : /*
4905 : : * Complete LOCK [TABLE][ONLY] <table> IN ACCESS|ROW with rest of lock
4906 : : * mode
4907 : : */
4908 : 0 : else if (Matches("LOCK", MatchAnyN, "IN", "ACCESS|ROW"))
4909 : 0 : COMPLETE_WITH("EXCLUSIVE MODE", "SHARE MODE");
4910 : :
4911 : : /* Complete LOCK [TABLE] [ONLY] <table> IN SHARE with rest of lock mode */
4912 : 0 : else if (Matches("LOCK", MatchAnyN, "IN", "SHARE"))
4913 : 0 : COMPLETE_WITH("MODE", "ROW EXCLUSIVE MODE",
4914 : : "UPDATE EXCLUSIVE MODE");
4915 : :
4916 : : /* Complete LOCK [TABLE] [ONLY] <table> [IN lockmode MODE] with "NOWAIT" */
4917 : 0 : else if (Matches("LOCK", MatchAnyN, "MODE"))
4918 : 0 : COMPLETE_WITH("NOWAIT");
4919 : :
4920 : : /* MERGE --- can be inside EXPLAIN */
4921 : 0 : else if (TailMatches("MERGE"))
4922 : 0 : COMPLETE_WITH("INTO");
4923 : 0 : else if (TailMatches("MERGE", "INTO"))
4924 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_mergetargets);
4925 : :
4926 : : /* Complete MERGE INTO <table> [[AS] <alias>] with USING */
4927 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny))
4928 : 0 : COMPLETE_WITH("USING", "AS");
4929 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny) ||
4930 : : TailMatches("MERGE", "INTO", MatchAny, MatchAnyExcept("USING|AS")))
4931 : 0 : COMPLETE_WITH("USING");
4932 : :
4933 : : /*
4934 : : * Complete MERGE INTO ... USING with a list of relations supporting
4935 : : * SELECT
4936 : : */
4937 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "USING") ||
4938 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING") ||
4939 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING"))
4940 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
4941 : :
4942 : : /*
4943 : : * Complete MERGE INTO <table> [[AS] <alias>] USING <relations> [[AS]
4944 : : * alias] with ON
4945 : : */
4946 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny) ||
4947 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny) ||
4948 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny))
4949 : 0 : COMPLETE_WITH("AS", "ON");
4950 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny, "AS", MatchAny) ||
4951 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, "AS", MatchAny) ||
4952 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny, "AS", MatchAny) ||
4953 : : TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")) ||
4954 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")) ||
4955 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")))
4956 : 0 : COMPLETE_WITH("ON");
4957 : :
4958 : : /* Complete MERGE INTO ... ON with target table attributes */
4959 : 0 : else if (TailMatches("INTO", MatchAny, "USING", MatchAny, "ON"))
4960 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
4961 : 0 : else if (TailMatches("INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, "AS", MatchAny, "ON"))
4962 : 0 : COMPLETE_WITH_ATTR(prev8_wd);
4963 : 0 : else if (TailMatches("INTO", MatchAny, MatchAny, "USING", MatchAny, MatchAny, "ON"))
4964 : 0 : COMPLETE_WITH_ATTR(prev6_wd);
4965 : :
4966 : : /*
4967 : : * Complete ... USING <relation> [[AS] alias] ON join condition
4968 : : * (consisting of one or three words typically used) with WHEN [NOT]
4969 : : * MATCHED
4970 : : */
4971 : 0 : else if (TailMatches("USING", MatchAny, "ON", MatchAny) ||
4972 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny) ||
4973 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny) ||
4974 : : TailMatches("USING", MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")) ||
4975 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")) ||
4976 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")))
4977 : 0 : COMPLETE_WITH("WHEN MATCHED", "WHEN NOT MATCHED");
4978 : 0 : else if (TailMatches("USING", MatchAny, "ON", MatchAny, "WHEN") ||
4979 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, "WHEN") ||
4980 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, "WHEN") ||
4981 : : TailMatches("USING", MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN") ||
4982 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN") ||
4983 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN"))
4984 : 0 : COMPLETE_WITH("MATCHED", "NOT MATCHED");
4985 : :
4986 : : /*
4987 : : * Complete ... WHEN MATCHED and WHEN NOT MATCHED BY SOURCE|TARGET with
4988 : : * THEN/AND
4989 : : */
4990 : 0 : else if (TailMatches("WHEN", "MATCHED") ||
4991 : : TailMatches("WHEN", "NOT", "MATCHED", "BY", "SOURCE|TARGET"))
4992 : 0 : COMPLETE_WITH("THEN", "AND");
4993 : :
4994 : : /* Complete ... WHEN NOT MATCHED with BY/THEN/AND */
4995 : 0 : else if (TailMatches("WHEN", "NOT", "MATCHED"))
4996 : 0 : COMPLETE_WITH("BY", "THEN", "AND");
4997 : :
4998 : : /* Complete ... WHEN NOT MATCHED BY with SOURCE/TARGET */
4999 : 0 : else if (TailMatches("WHEN", "NOT", "MATCHED", "BY"))
5000 : 0 : COMPLETE_WITH("SOURCE", "TARGET");
5001 : :
5002 : : /*
5003 : : * Complete ... WHEN MATCHED THEN and WHEN NOT MATCHED BY SOURCE THEN with
5004 : : * UPDATE SET/DELETE/DO NOTHING
5005 : : */
5006 : 0 : else if (TailMatches("WHEN", "MATCHED", "THEN") ||
5007 : : TailMatches("WHEN", "NOT", "MATCHED", "BY", "SOURCE", "THEN"))
5008 : 0 : COMPLETE_WITH("UPDATE SET", "DELETE", "DO NOTHING");
5009 : :
5010 : : /*
5011 : : * Complete ... WHEN NOT MATCHED [BY TARGET] THEN with INSERT/DO NOTHING
5012 : : */
5013 : 0 : else if (TailMatches("WHEN", "NOT", "MATCHED", "THEN") ||
5014 : : TailMatches("WHEN", "NOT", "MATCHED", "BY", "TARGET", "THEN"))
5015 : 0 : COMPLETE_WITH("INSERT", "DO NOTHING");
5016 : :
5017 : : /* NOTIFY --- can be inside EXPLAIN, RULE, etc */
5018 : 0 : else if (TailMatches("NOTIFY"))
5019 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_channels);
5020 : :
5021 : : /* OPTIONS */
5022 : 0 : else if (TailMatches("OPTIONS"))
5023 : 0 : COMPLETE_WITH("(");
5024 : :
5025 : : /* OWNER TO - complete with available roles */
5026 : 0 : else if (TailMatches("OWNER", "TO"))
5027 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
5028 : : Keywords_for_list_of_owner_roles);
5029 : :
5030 : : /* ORDER BY */
5031 : 0 : else if (TailMatches("FROM", MatchAny, "ORDER"))
5032 : 0 : COMPLETE_WITH("BY");
5033 : 0 : else if (TailMatches("FROM", MatchAny, "ORDER", "BY"))
5034 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
5035 : :
5036 : : /* PREPARE xx AS */
5037 : 0 : else if (Matches("PREPARE", MatchAny, "AS"))
5038 : 0 : COMPLETE_WITH("SELECT", "UPDATE", "INSERT INTO", "DELETE FROM",
5039 : : "MERGE INTO", "VALUES", "WITH", "TABLE");
5040 : :
5041 : : /*
5042 : : * PREPARE TRANSACTION is missing on purpose. It's intended for transaction
5043 : : * managers, not for manual use in interactive sessions.
5044 : : */
5045 : :
5046 : : /* REASSIGN OWNED BY xxx TO yyy */
5047 : 0 : else if (Matches("REASSIGN"))
5048 : 0 : COMPLETE_WITH("OWNED BY");
5049 : 0 : else if (Matches("REASSIGN", "OWNED"))
5050 : 0 : COMPLETE_WITH("BY");
5051 : 0 : else if (Matches("REASSIGN", "OWNED", "BY"))
5052 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5053 : 0 : else if (Matches("REASSIGN", "OWNED", "BY", MatchAny))
5054 : 0 : COMPLETE_WITH("TO");
5055 : 0 : else if (Matches("REASSIGN", "OWNED", "BY", MatchAny, "TO"))
5056 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5057 : :
5058 : : /* REFRESH MATERIALIZED VIEW */
5059 : 0 : else if (Matches("REFRESH"))
5060 : 0 : COMPLETE_WITH("MATERIALIZED VIEW");
5061 : 0 : else if (Matches("REFRESH", "MATERIALIZED"))
5062 : 0 : COMPLETE_WITH("VIEW");
5063 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW"))
5064 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_matviews,
5065 : : "CONCURRENTLY");
5066 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY"))
5067 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
5068 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny))
5069 : 0 : COMPLETE_WITH("WITH");
5070 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny))
5071 : 0 : COMPLETE_WITH("WITH");
5072 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH"))
5073 : 0 : COMPLETE_WITH("NO DATA", "DATA");
5074 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH"))
5075 : 0 : COMPLETE_WITH("NO DATA", "DATA");
5076 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH", "NO"))
5077 : 0 : COMPLETE_WITH("DATA");
5078 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH", "NO"))
5079 : 0 : COMPLETE_WITH("DATA");
5080 : :
5081 : : /* REINDEX */
5082 : 0 : else if (Matches("REINDEX") ||
5083 : : Matches("REINDEX", "(*)"))
5084 : 0 : COMPLETE_WITH("TABLE", "INDEX", "SYSTEM", "SCHEMA", "DATABASE");
5085 : 0 : else if (Matches("REINDEX", "TABLE") ||
5086 : : Matches("REINDEX", "(*)", "TABLE"))
5087 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexables,
5088 : : "CONCURRENTLY");
5089 : 0 : else if (Matches("REINDEX", "INDEX") ||
5090 : : Matches("REINDEX", "(*)", "INDEX"))
5091 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
5092 : : "CONCURRENTLY");
5093 : 0 : else if (Matches("REINDEX", "SCHEMA") ||
5094 : : Matches("REINDEX", "(*)", "SCHEMA"))
5095 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas,
5096 : : "CONCURRENTLY");
5097 : 0 : else if (Matches("REINDEX", "SYSTEM|DATABASE") ||
5098 : : Matches("REINDEX", "(*)", "SYSTEM|DATABASE"))
5099 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_databases,
5100 : : "CONCURRENTLY");
5101 : 0 : else if (Matches("REINDEX", "TABLE", "CONCURRENTLY") ||
5102 : : Matches("REINDEX", "(*)", "TABLE", "CONCURRENTLY"))
5103 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables);
5104 : 0 : else if (Matches("REINDEX", "INDEX", "CONCURRENTLY") ||
5105 : : Matches("REINDEX", "(*)", "INDEX", "CONCURRENTLY"))
5106 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
5107 : 0 : else if (Matches("REINDEX", "SCHEMA", "CONCURRENTLY") ||
5108 : : Matches("REINDEX", "(*)", "SCHEMA", "CONCURRENTLY"))
5109 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
5110 : 0 : else if (Matches("REINDEX", "SYSTEM|DATABASE", "CONCURRENTLY") ||
5111 : : Matches("REINDEX", "(*)", "SYSTEM|DATABASE", "CONCURRENTLY"))
5112 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
5113 : 0 : else if (HeadMatches("REINDEX", "(*") &&
5114 [ # # ]: 0 : !HeadMatches("REINDEX", "(*)"))
5115 : : {
5116 : : /*
5117 : : * This fires if we're in an unfinished parenthesized option list.
5118 : : * get_previous_words treats a completed parenthesized option list as
5119 : : * one word, so the above test is correct.
5120 : : */
5121 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
5122 : 0 : COMPLETE_WITH("CONCURRENTLY", "TABLESPACE", "VERBOSE");
5123 [ # # ]: 0 : else if (TailMatches("TABLESPACE"))
5124 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
5125 : : }
5126 : :
5127 : : /* REPACK */
5128 : 0 : else if (Matches("REPACK"))
5129 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
5130 : : "(", "USING INDEX");
5131 : 0 : else if (Matches("REPACK", "(*)"))
5132 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
5133 : : "USING INDEX");
5134 : 0 : else if (Matches("REPACK", MatchAnyExcept("(")))
5135 : 0 : COMPLETE_WITH("USING INDEX");
5136 : 0 : else if (Matches("REPACK", "(*)", MatchAnyExcept("(")))
5137 : 0 : COMPLETE_WITH("USING INDEX");
5138 : 0 : else if (Matches("REPACK", MatchAny, "USING", "INDEX") ||
5139 : : Matches("REPACK", "(*)", MatchAny, "USING", "INDEX"))
5140 : : {
5141 : 0 : set_completion_reference(prev3_wd);
5142 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
5143 : : }
5144 : :
5145 : : /*
5146 : : * Complete ... [ (*) ] <sth> USING INDEX, with a list of indexes for
5147 : : * <sth>.
5148 : : */
5149 : 0 : else if (TailMatches(MatchAny, "USING", "INDEX"))
5150 : : {
5151 : 0 : set_completion_reference(prev3_wd);
5152 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
5153 : : }
5154 : 0 : else if (HeadMatches("REPACK", "(*") &&
5155 [ # # ]: 0 : !HeadMatches("REPACK", "(*)"))
5156 : : {
5157 : : /*
5158 : : * This fires if we're in an unfinished parenthesized option list.
5159 : : * get_previous_words treats a completed parenthesized option list as
5160 : : * one word, so the above test is correct.
5161 : : */
5162 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
5163 : 0 : COMPLETE_WITH("ANALYZE", "CONCURRENTLY", "VERBOSE");
5164 [ # # ]: 0 : else if (TailMatches("ANALYZE|CONCURRENTLY|VERBOSE"))
5165 : 0 : COMPLETE_WITH("ON", "OFF");
5166 : : }
5167 : :
5168 : : /* SECURITY LABEL */
5169 : 0 : else if (Matches("SECURITY"))
5170 : 0 : COMPLETE_WITH("LABEL");
5171 : 0 : else if (Matches("SECURITY", "LABEL"))
5172 : 0 : COMPLETE_WITH("ON", "FOR");
5173 : 0 : else if (Matches("SECURITY", "LABEL", "FOR", MatchAny))
5174 : 0 : COMPLETE_WITH("ON");
5175 : 0 : else if (Matches("SECURITY", "LABEL", "ON") ||
5176 : : Matches("SECURITY", "LABEL", "FOR", MatchAny, "ON"))
5177 : 0 : COMPLETE_WITH("TABLE", "COLUMN", "AGGREGATE", "DATABASE", "DOMAIN",
5178 : : "EVENT TRIGGER", "FOREIGN TABLE", "FUNCTION",
5179 : : "LARGE OBJECT", "MATERIALIZED VIEW", "LANGUAGE",
5180 : : "PUBLICATION", "PROCEDURE", "ROLE", "ROUTINE", "SCHEMA",
5181 : : "SEQUENCE", "SUBSCRIPTION", "TABLESPACE", "TYPE", "VIEW");
5182 : 0 : else if (Matches("SECURITY", "LABEL", "ON", MatchAny, MatchAny))
5183 : 0 : COMPLETE_WITH("IS");
5184 : :
5185 : : /* SELECT */
5186 : : /* naah . . . */
5187 : :
5188 : : /* SET, RESET, SHOW */
5189 : : /* Complete with a variable name */
5190 : 0 : else if (TailMatches("SET|RESET") &&
5191 : : !TailMatches("UPDATE", MatchAny, "SET") &&
5192 [ + - + - ]: 3 : !TailMatches("ALTER", "DATABASE|USER|ROLE", MatchAny, "RESET"))
5193 : 3 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
5194 : : "CONSTRAINTS",
5195 : : "TRANSACTION",
5196 : : "SESSION",
5197 : : "ROLE",
5198 : : "TABLESPACE",
5199 : : "ALL");
5200 : 3 : else if (Matches("SHOW"))
5201 : 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_show_vars,
5202 : : "SESSION AUTHORIZATION",
5203 : : "ALL");
5204 : 0 : else if (Matches("SHOW", "SESSION"))
5205 : 0 : COMPLETE_WITH("AUTHORIZATION");
5206 : : /* Complete "SET TRANSACTION" */
5207 : 0 : else if (Matches("SET", "TRANSACTION"))
5208 : 0 : COMPLETE_WITH("SNAPSHOT", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
5209 : 0 : else if (Matches("BEGIN|START", "TRANSACTION") ||
5210 : : Matches("BEGIN", "WORK") ||
5211 : : Matches("BEGIN") ||
5212 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION"))
5213 : 0 : COMPLETE_WITH("ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
5214 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "NOT") ||
5215 : : Matches("BEGIN", "NOT") ||
5216 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "NOT"))
5217 : 0 : COMPLETE_WITH("DEFERRABLE");
5218 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION") ||
5219 : : Matches("BEGIN", "ISOLATION") ||
5220 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION"))
5221 : 0 : COMPLETE_WITH("LEVEL");
5222 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL") ||
5223 : : Matches("BEGIN", "ISOLATION", "LEVEL") ||
5224 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL"))
5225 : 0 : COMPLETE_WITH("READ", "REPEATABLE READ", "SERIALIZABLE");
5226 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "READ") ||
5227 : : Matches("BEGIN", "ISOLATION", "LEVEL", "READ") ||
5228 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "READ"))
5229 : 0 : COMPLETE_WITH("UNCOMMITTED", "COMMITTED");
5230 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "REPEATABLE") ||
5231 : : Matches("BEGIN", "ISOLATION", "LEVEL", "REPEATABLE") ||
5232 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "REPEATABLE"))
5233 : 0 : COMPLETE_WITH("READ");
5234 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "READ") ||
5235 : : Matches("BEGIN", "READ") ||
5236 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "READ"))
5237 : 0 : COMPLETE_WITH("ONLY", "WRITE");
5238 : : /* SET CONSTRAINTS */
5239 : 0 : else if (Matches("SET", "CONSTRAINTS"))
5240 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_constraints_with_schema,
5241 : : "ALL");
5242 : : /* Complete SET CONSTRAINTS <foo> with DEFERRED|IMMEDIATE */
5243 : 0 : else if (Matches("SET", "CONSTRAINTS", MatchAny))
5244 : 0 : COMPLETE_WITH("DEFERRED", "IMMEDIATE");
5245 : : /* Complete SET ROLE */
5246 : 0 : else if (Matches("SET", "ROLE"))
5247 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5248 : : /* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */
5249 : 0 : else if (Matches("SET", "SESSION"))
5250 : 0 : COMPLETE_WITH("AUTHORIZATION", "CHARACTERISTICS AS TRANSACTION");
5251 : : /* Complete SET SESSION AUTHORIZATION with username */
5252 : 0 : else if (Matches("SET", "SESSION", "AUTHORIZATION"))
5253 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
5254 : : "DEFAULT");
5255 : : /* Complete RESET SESSION with AUTHORIZATION */
5256 : 0 : else if (Matches("RESET", "SESSION"))
5257 : 0 : COMPLETE_WITH("AUTHORIZATION");
5258 : : /* Complete SET <var> with "TO" */
5259 : 0 : else if (Matches("SET", MatchAny))
5260 : 2 : COMPLETE_WITH("TO");
5261 : :
5262 : : /*
5263 : : * Complete ALTER DATABASE|FUNCTION|PROCEDURE|ROLE|ROUTINE|USER ... SET
5264 : : * <name>
5265 : : */
5266 : 2 : else if (Matches("ALTER", "DATABASE|FUNCTION|PROCEDURE|ROLE|ROUTINE|USER", MatchAnyN, "SET", MatchAnyExcept("SCHEMA")))
5267 : 0 : COMPLETE_WITH("FROM CURRENT", "TO");
5268 : :
5269 : : /*
5270 : : * Suggest possible variable values in SET variable TO|=, along with the
5271 : : * preceding ALTER syntaxes.
5272 : : */
5273 : 0 : else if (TailMatches("SET", MatchAny, "TO|=") &&
5274 [ + - ]: 4 : !TailMatches("UPDATE", MatchAny, "SET", MatchAny, "TO|="))
5275 : : {
5276 : : /* special cased code for individual GUCs */
5277 [ - + ]: 4 : if (TailMatches("DateStyle", "TO|="))
5278 : 0 : COMPLETE_WITH("ISO", "SQL", "Postgres", "German",
5279 : : "YMD", "DMY", "MDY",
5280 : : "US", "European", "NonEuropean",
5281 : : "DEFAULT");
5282 [ - + ]: 4 : else if (TailMatches("search_path", "TO|="))
5283 : : {
5284 : : /* Here, we want to allow pg_catalog, so use narrower exclusion */
5285 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
5286 : : " AND nspname NOT LIKE E'pg\\\\_toast%%'"
5287 : : " AND nspname NOT LIKE E'pg\\\\_temp%%'",
5288 : : "DEFAULT");
5289 : : }
5290 [ + + ]: 4 : else if (TailMatches("TimeZone", "TO|="))
5291 [ - + + - : 2 : COMPLETE_WITH_TIMEZONE_NAME();
+ + ]
5292 : : else
5293 : : {
5294 : : /* generic, type based, GUC support */
5295 : 2 : char *guctype = get_guctype(prev2_wd);
5296 : :
5297 : : /*
5298 : : * Note: if we don't recognize the GUC name, it's important to not
5299 : : * offer any completions, as most likely we've misinterpreted the
5300 : : * context and this isn't a GUC-setting command at all.
5301 : : */
5302 [ + - ]: 2 : if (guctype)
5303 : : {
5304 [ + - ]: 2 : if (strcmp(guctype, "enum") == 0)
5305 : : {
5306 : 2 : set_completion_reference_verbatim(prev2_wd);
5307 : 2 : COMPLETE_WITH_QUERY_PLUS(Query_for_values_of_enum_GUC,
5308 : : "DEFAULT");
5309 : : }
5310 [ # # ]: 0 : else if (strcmp(guctype, "bool") == 0)
5311 : 0 : COMPLETE_WITH("on", "off", "true", "false", "yes", "no",
5312 : : "1", "0", "DEFAULT");
5313 : : else
5314 : 0 : COMPLETE_WITH("DEFAULT");
5315 : :
5316 : 2 : free(guctype);
5317 : : }
5318 : : }
5319 : : }
5320 : :
5321 : : /* START TRANSACTION */
5322 : 4 : else if (Matches("START"))
5323 : 0 : COMPLETE_WITH("TRANSACTION");
5324 : :
5325 : : /* TABLE, but not TABLE embedded in other commands */
5326 : 0 : else if (Matches("TABLE"))
5327 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
5328 : :
5329 : : /* TABLESAMPLE */
5330 : 0 : else if (TailMatches("TABLESAMPLE"))
5331 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablesample_methods);
5332 : 0 : else if (TailMatches("TABLESAMPLE", MatchAny))
5333 : 0 : COMPLETE_WITH("(");
5334 : :
5335 : : /* TRUNCATE */
5336 : 0 : else if (Matches("TRUNCATE"))
5337 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_truncatables,
5338 : : "TABLE", "ONLY");
5339 : 0 : else if (Matches("TRUNCATE", "TABLE"))
5340 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_truncatables,
5341 : : "ONLY");
5342 : 0 : else if (Matches("TRUNCATE", MatchAnyN, "ONLY"))
5343 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_truncatables);
5344 : 0 : else if (Matches("TRUNCATE", MatchAny) ||
5345 : : Matches("TRUNCATE", "TABLE|ONLY", MatchAny) ||
5346 : : Matches("TRUNCATE", "TABLE", "ONLY", MatchAny))
5347 : 0 : COMPLETE_WITH("RESTART IDENTITY", "CONTINUE IDENTITY", "CASCADE", "RESTRICT");
5348 : 0 : else if (Matches("TRUNCATE", MatchAnyN, "IDENTITY"))
5349 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
5350 : :
5351 : : /* UNLISTEN */
5352 : 0 : else if (Matches("UNLISTEN"))
5353 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_channels, "*");
5354 : :
5355 : : /* UPDATE --- can be inside EXPLAIN, RULE, etc */
5356 : : /* If prev. word is UPDATE suggest a list of tables */
5357 : 0 : else if (TailMatches("UPDATE"))
5358 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
5359 : : /* Complete UPDATE <table> with "SET" */
5360 : 0 : else if (TailMatches("UPDATE", MatchAny))
5361 : 0 : COMPLETE_WITH("SET");
5362 : : /* Complete UPDATE <table> SET with list of attributes */
5363 : 0 : else if (TailMatches("UPDATE", MatchAny, "SET"))
5364 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
5365 : : /* UPDATE <table> SET <attr> = */
5366 : 0 : else if (TailMatches("UPDATE", MatchAny, "SET", MatchAnyExcept("*=")))
5367 : 0 : COMPLETE_WITH("=");
5368 : :
5369 : : /* USER MAPPING */
5370 : 0 : else if (Matches("ALTER|CREATE|DROP", "USER", "MAPPING"))
5371 : 0 : COMPLETE_WITH("FOR");
5372 : 0 : else if (Matches("CREATE", "USER", "MAPPING", "FOR"))
5373 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
5374 : : "CURRENT_ROLE",
5375 : : "CURRENT_USER",
5376 : : "PUBLIC",
5377 : : "USER");
5378 : 0 : else if (Matches("ALTER|DROP", "USER", "MAPPING", "FOR"))
5379 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
5380 : 0 : else if (Matches("CREATE|ALTER|DROP", "USER", "MAPPING", "FOR", MatchAny))
5381 : 0 : COMPLETE_WITH("SERVER");
5382 : 0 : else if (Matches("CREATE|ALTER", "USER", "MAPPING", "FOR", MatchAny, "SERVER", MatchAny))
5383 : 0 : COMPLETE_WITH("OPTIONS");
5384 : :
5385 : : /*
5386 : : * VACUUM [ ( option [, ...] ) ] [ [ ONLY ] table_and_columns [, ...] ]
5387 : : * VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ [ ONLY ] table_and_columns [, ...] ]
5388 : : */
5389 : 0 : else if (Matches("VACUUM"))
5390 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5391 : : "(",
5392 : : "FULL",
5393 : : "FREEZE",
5394 : : "VERBOSE",
5395 : : "ANALYZE",
5396 : : "ONLY");
5397 : 0 : else if (HeadMatches("VACUUM", "(*") &&
5398 [ # # ]: 0 : !HeadMatches("VACUUM", "(*)"))
5399 : : {
5400 : : /*
5401 : : * This fires if we're in an unfinished parenthesized option list.
5402 : : * get_previous_words treats a completed parenthesized option list as
5403 : : * one word, so the above test is correct.
5404 : : */
5405 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
5406 : 0 : COMPLETE_WITH("FULL", "FREEZE", "ANALYZE", "VERBOSE",
5407 : : "DISABLE_PAGE_SKIPPING", "SKIP_LOCKED",
5408 : : "INDEX_CLEANUP", "PROCESS_MAIN", "PROCESS_TOAST",
5409 : : "TRUNCATE", "PARALLEL", "SKIP_DATABASE_STATS",
5410 : : "ONLY_DATABASE_STATS", "BUFFER_USAGE_LIMIT");
5411 [ # # ]: 0 : else if (TailMatches("FULL|FREEZE|ANALYZE|VERBOSE|DISABLE_PAGE_SKIPPING|SKIP_LOCKED|PROCESS_MAIN|PROCESS_TOAST|TRUNCATE|SKIP_DATABASE_STATS|ONLY_DATABASE_STATS"))
5412 : 0 : COMPLETE_WITH("ON", "OFF");
5413 [ # # ]: 0 : else if (TailMatches("INDEX_CLEANUP"))
5414 : 0 : COMPLETE_WITH("AUTO", "ON", "OFF");
5415 : : }
5416 : 0 : else if (Matches("VACUUM", "(*)"))
5417 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5418 : : "ONLY");
5419 : 0 : else if (Matches("VACUUM", "FULL"))
5420 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5421 : : "FREEZE",
5422 : : "VERBOSE",
5423 : : "ANALYZE",
5424 : : "ONLY");
5425 : 0 : else if (Matches("VACUUM", MatchAnyN, "FREEZE"))
5426 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5427 : : "VERBOSE",
5428 : : "ANALYZE",
5429 : : "ONLY");
5430 : 0 : else if (Matches("VACUUM", MatchAnyN, "VERBOSE"))
5431 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5432 : : "ANALYZE",
5433 : : "ONLY");
5434 : 0 : else if (Matches("VACUUM", MatchAnyN, "ANALYZE"))
5435 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5436 : : "ONLY");
5437 : 0 : else if (Matches("VACUUM", MatchAnyN, "("))
5438 : : /* "VACUUM (" should be caught above, so assume we want columns */
5439 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
5440 : 0 : else if (HeadMatches("VACUUM"))
5441 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables);
5442 : :
5443 : : /*
5444 : : * WAIT FOR LSN '<lsn>' [ WITH ( option [, ...] ) ]
5445 : : * where option can be:
5446 : : * MODE '<mode>'
5447 : : * TIMEOUT '<timeout>'
5448 : : * NO_THROW
5449 : : * and mode can be:
5450 : : * standby_replay | standby_write | standby_flush | primary_flush
5451 : : */
5452 : 0 : else if (Matches("WAIT"))
5453 : 0 : COMPLETE_WITH("FOR");
5454 : 0 : else if (Matches("WAIT", "FOR"))
5455 : 0 : COMPLETE_WITH("LSN");
5456 : 0 : else if (Matches("WAIT", "FOR", "LSN"))
5457 : : /* No completion for LSN value - user must provide manually */
5458 : : ;
5459 : 0 : else if (Matches("WAIT", "FOR", "LSN", MatchAny))
5460 : 0 : COMPLETE_WITH("WITH");
5461 : 0 : else if (Matches("WAIT", "FOR", "LSN", MatchAny, "WITH"))
5462 : 0 : COMPLETE_WITH("(");
5463 : :
5464 : : /*
5465 : : * Handle parenthesized option list. This fires when we're in an
5466 : : * unfinished parenthesized option list. get_previous_words treats a
5467 : : * completed parenthesized option list as one word, so the above test is
5468 : : * correct.
5469 : : *
5470 : : * 'mode' takes a string value (one of the listed above), 'timeout' takes
5471 : : * a string value, and 'no_throw' takes no value. We do not offer
5472 : : * completions for the *values* of 'timeout' or 'no_throw'.
5473 : : */
5474 : 0 : else if (HeadMatches("WAIT", "FOR", "LSN", MatchAny, "WITH", "(*") &&
5475 [ # # ]: 0 : !HeadMatches("WAIT", "FOR", "LSN", MatchAny, "WITH", "(*)"))
5476 : : {
5477 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
5478 : 0 : COMPLETE_WITH("mode", "timeout", "no_throw");
5479 [ # # ]: 0 : else if (TailMatches("mode"))
5480 : 0 : COMPLETE_WITH("'standby_replay'", "'standby_write'", "'standby_flush'", "'primary_flush'");
5481 : : }
5482 : :
5483 : : /* WITH [RECURSIVE] */
5484 : :
5485 : : /*
5486 : : * Only match when WITH is the first word, as WITH may appear in many
5487 : : * other contexts.
5488 : : */
5489 : 0 : else if (Matches("WITH"))
5490 : 0 : COMPLETE_WITH("RECURSIVE");
5491 : :
5492 : : /* WHERE */
5493 : : /* Simple case of the word before the where being the table name */
5494 : 0 : else if (TailMatches(MatchAny, "WHERE"))
5495 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
5496 : :
5497 : : /* ... FROM ... */
5498 : : /* TODO: also include SRF ? */
5499 [ + - ]: 14 : else if (TailMatches("FROM") && !Matches("COPY|\\copy", MatchAny, "FROM"))
5500 : 14 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
5501 : :
5502 : : /* ... JOIN ... */
5503 : 14 : else if (TailMatches("JOIN"))
5504 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_selectables, "LATERAL");
5505 [ # # ]: 0 : else if (TailMatches("JOIN", MatchAny) && !TailMatches("CROSS|NATURAL", "JOIN", MatchAny))
5506 : 0 : COMPLETE_WITH("ON", "USING (");
5507 : 0 : else if (TailMatches("JOIN", MatchAny, MatchAny) &&
5508 [ # # # # ]: 0 : !TailMatches("CROSS|NATURAL", "JOIN", MatchAny, MatchAny) && !TailMatches("ON|USING"))
5509 : 0 : COMPLETE_WITH("ON", "USING (");
5510 : 0 : else if (TailMatches("JOIN", "LATERAL", MatchAny, MatchAny) &&
5511 [ # # # # ]: 0 : !TailMatches("CROSS|NATURAL", "JOIN", "LATERAL", MatchAny, MatchAny) && !TailMatches("ON|USING"))
5512 : 0 : COMPLETE_WITH("ON", "USING (");
5513 : 0 : else if (TailMatches("JOIN", MatchAny, "USING") ||
5514 : : TailMatches("JOIN", MatchAny, MatchAny, "USING") ||
5515 : : TailMatches("JOIN", "LATERAL", MatchAny, MatchAny, "USING"))
5516 : 0 : COMPLETE_WITH("(");
5517 : 0 : else if (TailMatches("JOIN", MatchAny, "USING", "("))
5518 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
5519 : 0 : else if (TailMatches("JOIN", MatchAny, MatchAny, "USING", "("))
5520 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
5521 : :
5522 : : /* ... AT [ LOCAL | TIME ZONE ] ... */
5523 : 0 : else if (TailMatches("AT"))
5524 : 0 : COMPLETE_WITH("LOCAL", "TIME ZONE");
5525 : 0 : else if (TailMatches("AT", "TIME", "ZONE"))
5526 [ # # # # : 0 : COMPLETE_WITH_TIMEZONE_NAME();
# # ]
5527 : :
5528 : : /* Backslash commands */
5529 : : /* TODO: \dc \dd \dl */
5530 : 0 : else if (TailMatchesCS("\\?"))
5531 : 0 : COMPLETE_WITH_CS("commands", "options", "variables");
5532 : 0 : else if (TailMatchesCS("\\connect|\\c"))
5533 : : {
5534 [ # # ]: 0 : if (!recognized_connection_string(text))
5535 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
5536 : : }
5537 : 0 : else if (TailMatchesCS("\\connect|\\c", MatchAny))
5538 : : {
5539 [ # # ]: 0 : if (!recognized_connection_string(prev_wd))
5540 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5541 : : }
5542 : 0 : else if (TailMatchesCS("\\da*"))
5543 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_aggregates);
5544 : 0 : else if (TailMatchesCS("\\dAc*", MatchAny) ||
5545 : : TailMatchesCS("\\dAf*", MatchAny))
5546 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
5547 : 0 : else if (TailMatchesCS("\\dAo*", MatchAny) ||
5548 : : TailMatchesCS("\\dAp*", MatchAny))
5549 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_operator_families);
5550 : 0 : else if (TailMatchesCS("\\dA*"))
5551 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
5552 : 0 : else if (TailMatchesCS("\\db*"))
5553 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
5554 : 0 : else if (TailMatchesCS("\\dconfig*"))
5555 : 0 : COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_show_vars);
5556 : 0 : else if (TailMatchesCS("\\dD*"))
5557 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
5558 : 0 : else if (TailMatchesCS("\\des*"))
5559 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
5560 : 0 : else if (TailMatchesCS("\\deu*"))
5561 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
5562 : 0 : else if (TailMatchesCS("\\dew*"))
5563 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
5564 : 0 : else if (TailMatchesCS("\\df*"))
5565 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
5566 : 0 : else if (HeadMatchesCS("\\df*"))
5567 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
5568 : :
5569 : 0 : else if (TailMatchesCS("\\dFd*"))
5570 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_dictionaries);
5571 : 0 : else if (TailMatchesCS("\\dFp*"))
5572 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_parsers);
5573 : 0 : else if (TailMatchesCS("\\dFt*"))
5574 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_templates);
5575 : : /* must be at end of \dF alternatives: */
5576 : 0 : else if (TailMatchesCS("\\dF*"))
5577 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_configurations);
5578 : :
5579 : 0 : else if (TailMatchesCS("\\di*"))
5580 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
5581 : 0 : else if (TailMatchesCS("\\dL*"))
5582 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
5583 : 0 : else if (TailMatchesCS("\\dn*"))
5584 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
5585 : : /* no support for completing operators, but we can complete types: */
5586 : 0 : else if (HeadMatchesCS("\\do*", MatchAny))
5587 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
5588 : 0 : else if (TailMatchesCS("\\dp") || TailMatchesCS("\\z"))
5589 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables);
5590 : 0 : else if (TailMatchesCS("\\dPi*"))
5591 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_indexes);
5592 : 0 : else if (TailMatchesCS("\\dPt*"))
5593 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables);
5594 : 0 : else if (TailMatchesCS("\\dP*"))
5595 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_relations);
5596 : 0 : else if (TailMatchesCS("\\dRp*"))
5597 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_publications);
5598 : 0 : else if (TailMatchesCS("\\dRs*"))
5599 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_subscriptions);
5600 : 0 : else if (TailMatchesCS("\\ds*"))
5601 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences);
5602 : 0 : else if (TailMatchesCS("\\dt*"))
5603 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
5604 : 0 : else if (TailMatchesCS("\\dT*"))
5605 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
5606 : 0 : else if (TailMatchesCS("\\du*") ||
5607 : : TailMatchesCS("\\dg*") ||
5608 : : TailMatchesCS("\\drg*"))
5609 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5610 : 0 : else if (TailMatchesCS("\\dv*"))
5611 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
5612 : 0 : else if (TailMatchesCS("\\dx*"))
5613 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_extensions);
5614 : 0 : else if (TailMatchesCS("\\dX*"))
5615 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_statistics);
5616 : 0 : else if (TailMatchesCS("\\dm*"))
5617 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
5618 : 0 : else if (TailMatchesCS("\\dE*"))
5619 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
5620 : 0 : else if (TailMatchesCS("\\dy*"))
5621 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
5622 : :
5623 : : /* must be at end of \d alternatives: */
5624 : 0 : else if (TailMatchesCS("\\d*"))
5625 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations);
5626 : :
5627 : 0 : else if (TailMatchesCS("\\ef"))
5628 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
5629 : 0 : else if (TailMatchesCS("\\ev"))
5630 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
5631 : :
5632 : 0 : else if (TailMatchesCS("\\encoding"))
5633 : 0 : COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_encodings);
5634 : 0 : else if (TailMatchesCS("\\h|\\help"))
5635 : 0 : COMPLETE_WITH_LIST(sql_commands);
5636 : 0 : else if (TailMatchesCS("\\h|\\help", MatchAny))
5637 : : {
5638 [ # # ]: 0 : if (TailMatches("DROP"))
5639 : 0 : COMPLETE_WITH_GENERATOR(drop_command_generator);
5640 [ # # ]: 0 : else if (TailMatches("ALTER"))
5641 : 0 : COMPLETE_WITH_GENERATOR(alter_command_generator);
5642 : :
5643 : : /*
5644 : : * CREATE is recognized by tail match elsewhere, so doesn't need to be
5645 : : * repeated here
5646 : : */
5647 : : }
5648 : 0 : else if (TailMatchesCS("\\h|\\help", MatchAny, MatchAny))
5649 : : {
5650 [ # # ]: 0 : if (TailMatches("CREATE|DROP", "ACCESS"))
5651 : 0 : COMPLETE_WITH("METHOD");
5652 [ # # ]: 0 : else if (TailMatches("ALTER", "DEFAULT"))
5653 : 0 : COMPLETE_WITH("PRIVILEGES");
5654 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "EVENT"))
5655 : 0 : COMPLETE_WITH("TRIGGER");
5656 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "FOREIGN"))
5657 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
5658 [ # # ]: 0 : else if (TailMatches("ALTER", "LARGE"))
5659 : 0 : COMPLETE_WITH("OBJECT");
5660 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "MATERIALIZED"))
5661 : 0 : COMPLETE_WITH("VIEW");
5662 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "TEXT"))
5663 : 0 : COMPLETE_WITH("SEARCH");
5664 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "USER"))
5665 : 0 : COMPLETE_WITH("MAPPING FOR");
5666 : : }
5667 : 0 : else if (TailMatchesCS("\\h|\\help", MatchAny, MatchAny, MatchAny))
5668 : : {
5669 [ # # ]: 0 : if (TailMatches("CREATE|ALTER|DROP", "FOREIGN", "DATA"))
5670 : 0 : COMPLETE_WITH("WRAPPER");
5671 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "TEXT", "SEARCH"))
5672 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
5673 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "USER", "MAPPING"))
5674 : 0 : COMPLETE_WITH("FOR");
5675 : : }
5676 [ - + ]: 2 : else if (TailMatchesCS("\\l*") && !TailMatchesCS("\\lo*"))
5677 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
5678 : 2 : else if (TailMatchesCS("\\password"))
5679 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5680 : 0 : else if (TailMatchesCS("\\pset"))
5681 : 0 : COMPLETE_WITH_CS("border", "columns", "csv_fieldsep",
5682 : : "display_false", "display_true", "expanded",
5683 : : "fieldsep", "fieldsep_zero", "footer", "format",
5684 : : "linestyle", "null", "numericlocale",
5685 : : "pager", "pager_min_lines",
5686 : : "recordsep", "recordsep_zero",
5687 : : "tableattr", "title", "tuples_only",
5688 : : "unicode_border_linestyle",
5689 : : "unicode_column_linestyle",
5690 : : "unicode_header_linestyle",
5691 : : "xheader_width");
5692 : 0 : else if (TailMatchesCS("\\pset", MatchAny))
5693 : : {
5694 [ # # ]: 0 : if (TailMatchesCS("format"))
5695 : 0 : COMPLETE_WITH_CS("aligned", "asciidoc", "csv", "html", "latex",
5696 : : "latex-longtable", "troff-ms", "unaligned",
5697 : : "wrapped");
5698 [ # # ]: 0 : else if (TailMatchesCS("xheader_width"))
5699 : 0 : COMPLETE_WITH_CS("full", "column", "page");
5700 [ # # ]: 0 : else if (TailMatchesCS("linestyle"))
5701 : 0 : COMPLETE_WITH_CS("ascii", "old-ascii", "unicode");
5702 [ # # ]: 0 : else if (TailMatchesCS("pager"))
5703 : 0 : COMPLETE_WITH_CS("on", "off", "always");
5704 [ # # ]: 0 : else if (TailMatchesCS("unicode_border_linestyle|"
5705 : : "unicode_column_linestyle|"
5706 : : "unicode_header_linestyle"))
5707 : 0 : COMPLETE_WITH_CS("single", "double");
5708 : : }
5709 : 0 : else if (TailMatchesCS("\\unset"))
5710 : 0 : matches = complete_from_variables(text, "", "", true);
5711 : 0 : else if (TailMatchesCS("\\set"))
5712 : 1 : matches = complete_from_variables(text, "", "", false);
5713 : 1 : else if (TailMatchesCS("\\set", MatchAny))
5714 : : {
5715 [ - + ]: 1 : if (TailMatchesCS("AUTOCOMMIT|ON_ERROR_STOP|QUIET|SHOW_ALL_RESULTS|"
5716 : : "SINGLELINE|SINGLESTEP"))
5717 : 0 : COMPLETE_WITH_CS("on", "off");
5718 [ - + ]: 1 : else if (TailMatchesCS("COMP_KEYWORD_CASE"))
5719 : 0 : COMPLETE_WITH_CS("lower", "upper",
5720 : : "preserve-lower", "preserve-upper");
5721 [ - + ]: 1 : else if (TailMatchesCS("ECHO"))
5722 : 0 : COMPLETE_WITH_CS("errors", "queries", "all", "none");
5723 [ - + ]: 1 : else if (TailMatchesCS("ECHO_HIDDEN"))
5724 : 0 : COMPLETE_WITH_CS("noexec", "off", "on");
5725 [ - + ]: 1 : else if (TailMatchesCS("HISTCONTROL"))
5726 : 0 : COMPLETE_WITH_CS("ignorespace", "ignoredups",
5727 : : "ignoreboth", "none");
5728 [ - + ]: 1 : else if (TailMatchesCS("ON_ERROR_ROLLBACK"))
5729 : 0 : COMPLETE_WITH_CS("on", "off", "interactive");
5730 [ - + ]: 1 : else if (TailMatchesCS("SHOW_CONTEXT"))
5731 : 0 : COMPLETE_WITH_CS("never", "errors", "always");
5732 [ + - ]: 1 : else if (TailMatchesCS("VERBOSITY"))
5733 : 1 : COMPLETE_WITH_CS("default", "verbose", "terse", "sqlstate");
5734 : : }
5735 : 1 : else if (TailMatchesCS("\\sf*"))
5736 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
5737 : 0 : else if (TailMatchesCS("\\sv*"))
5738 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
5739 : 0 : else if (TailMatchesCS("\\cd|\\e|\\edit|\\g|\\gx|\\i|\\include|"
5740 : : "\\ir|\\include_relative|\\o|\\out|"
5741 : : "\\s|\\w|\\write|\\lo_import") ||
5742 : : TailMatchesCS("\\lo_export", MatchAny))
5743 : 2 : COMPLETE_WITH_FILES("\\", false);
5744 : :
5745 : : /* gen_tabcomplete.pl ends special processing here */
5746 : 2 : /* END GEN_TABCOMPLETE */
5747 : 0 :
5748 : 58 : return matches;
5749 : 0 : }
5750 : :
5751 : :
5752 : : /*
5753 : : * GENERATOR FUNCTIONS
5754 : : *
5755 : : * These functions do all the actual work of completing the input. They get
5756 : : * passed the text so far and the count how many times they have been called
5757 : : * so far with the same text.
5758 : : * If you read the above carefully, you'll see that these don't get called
5759 : : * directly but through the readline interface.
5760 : : * The return value is expected to be the full completion of the text, going
5761 : : * through a list each time, or NULL if there are no more matches. The string
5762 : : * will be free()'d by readline, so you must run it through strdup() or
5763 : : * something of that sort.
5764 : : */
5765 : :
5766 : : /*
5767 : : * Common routine for create_command_generator and drop_command_generator.
5768 : : * Entries that have 'excluded' flags are not returned.
5769 : : */
5770 : : static char *
5771 : 4 : create_or_drop_command_generator(const char *text, int state, uint32 excluded)
5772 : : {
5773 : : static int list_index,
5774 : : string_length;
5775 : : const char *name;
5776 : :
5777 : : /* If this is the first time for this completion, init some values */
5778 [ + + ]: 4 : if (state == 0)
5779 : : {
5780 : 2 : list_index = 0;
5781 : 2 : string_length = strlen(text);
5782 : : }
5783 : :
5784 : : /* find something that matches */
5785 [ + + ]: 102 : while ((name = words_after_create[list_index++].name))
5786 : : {
5787 [ + + ]: 100 : if ((pg_strncasecmp(name, text, string_length) == 0) &&
5788 [ + - ]: 2 : !(words_after_create[list_index - 1].flags & excluded))
5789 : 2 : return pg_strdup_keyword_case(name, text);
5790 : : }
5791 : : /* if nothing matches, return NULL */
5792 : 2 : return NULL;
5793 : : }
5794 : :
5795 : : /*
5796 : : * This one gives you one from a list of things you can put after CREATE
5797 : : * as defined above.
5798 : : */
5799 : : static char *
5800 : 2 : create_command_generator(const char *text, int state)
5801 : : {
5802 : 2 : return create_or_drop_command_generator(text, state, THING_NO_CREATE);
5803 : : }
5804 : :
5805 : : /*
5806 : : * This function gives you a list of things you can put after a DROP command.
5807 : : */
5808 : : static char *
5809 : 2 : drop_command_generator(const char *text, int state)
5810 : : {
5811 : 2 : return create_or_drop_command_generator(text, state, THING_NO_DROP);
5812 : : }
5813 : :
5814 : : /*
5815 : : * This function gives you a list of things you can put after an ALTER command.
5816 : : */
5817 : : static char *
5818 : 0 : alter_command_generator(const char *text, int state)
5819 : : {
5820 : 0 : return create_or_drop_command_generator(text, state, THING_NO_ALTER);
5821 : : }
5822 : :
5823 : : /*
5824 : : * These functions generate lists using server queries.
5825 : : * They are all wrappers for _complete_from_query.
5826 : : */
5827 : :
5828 : : static char *
5829 : 188 : complete_from_query(const char *text, int state)
5830 : : {
5831 : : /* query is assumed to work for any server version */
5832 : 188 : return _complete_from_query(completion_charp, NULL, completion_charpp,
5833 : : completion_verbatim, text, state);
5834 : : }
5835 : :
5836 : : static char *
5837 : 0 : complete_from_versioned_query(const char *text, int state)
5838 : : {
5839 : 0 : const VersionedQuery *vquery = completion_vquery;
5840 : :
5841 : : /* Find appropriate array element */
5842 [ # # ]: 0 : while (pset.sversion < vquery->min_server_version)
5843 : 0 : vquery++;
5844 : : /* Fail completion if server is too old */
5845 [ # # ]: 0 : if (vquery->query == NULL)
5846 : 0 : return NULL;
5847 : :
5848 : 0 : return _complete_from_query(vquery->query, NULL, completion_charpp,
5849 : : completion_verbatim, text, state);
5850 : : }
5851 : :
5852 : : static char *
5853 : 86 : complete_from_schema_query(const char *text, int state)
5854 : : {
5855 : : /* query is assumed to work for any server version */
5856 : 86 : return _complete_from_query(NULL, completion_squery, completion_charpp,
5857 : : completion_verbatim, text, state);
5858 : : }
5859 : :
5860 : : static char *
5861 : 8 : complete_from_versioned_schema_query(const char *text, int state)
5862 : : {
5863 : 8 : const SchemaQuery *squery = completion_squery;
5864 : :
5865 : : /* Find appropriate array element */
5866 [ - + ]: 8 : while (pset.sversion < squery->min_server_version)
5867 : 0 : squery++;
5868 : : /* Fail completion if server is too old */
5869 [ - + ]: 8 : if (squery->catname == NULL)
5870 : 0 : return NULL;
5871 : :
5872 : 8 : return _complete_from_query(NULL, squery, completion_charpp,
5873 : : completion_verbatim, text, state);
5874 : : }
5875 : :
5876 : :
5877 : : /*
5878 : : * This creates a list of matching things, according to a query described by
5879 : : * the initial arguments. The caller has already done any work needed to
5880 : : * select the appropriate query for the server's version.
5881 : : *
5882 : : * The query can be one of two kinds:
5883 : : *
5884 : : * 1. A simple query, which must contain a restriction clause of the form
5885 : : * output LIKE '%s'
5886 : : * where "output" is the same string that the query returns. The %s
5887 : : * will be replaced by a LIKE pattern to match the already-typed text.
5888 : : * There can be a second '%s', which will be replaced by a suitably-escaped
5889 : : * version of the string provided in completion_ref_object. If there is a
5890 : : * third '%s', it will be replaced by a suitably-escaped version of the string
5891 : : * provided in completion_ref_schema. Those strings should be set up
5892 : : * by calling set_completion_reference or set_completion_reference_verbatim.
5893 : : * Simple queries should return a single column of matches. If "verbatim"
5894 : : * is true, the matches are returned as-is; otherwise, they are taken to
5895 : : * be SQL identifiers and quoted if necessary.
5896 : : *
5897 : : * 2. A schema query used for completion of both schema and relation names.
5898 : : * This is represented by a SchemaQuery object; see that typedef for details.
5899 : : *
5900 : : * See top of file for examples of both kinds of query.
5901 : : *
5902 : : * In addition to the query itself, we accept a null-terminated array of
5903 : : * literal keywords, which will be returned if they match the input-so-far
5904 : : * (case insensitively). (These are in addition to keywords specified
5905 : : * within the schema_query, if any.)
5906 : : *
5907 : : * If "verbatim" is true, then we use the given text as-is to match the
5908 : : * query results; otherwise we parse it as a possibly-qualified identifier,
5909 : : * and reconstruct suitable quoting afterward.
5910 : : *
5911 : : * "text" and "state" are supplied by Readline. "text" is the word we are
5912 : : * trying to complete. "state" is zero on first call, nonzero later.
5913 : : *
5914 : : * readline will call this repeatedly with the same text and varying
5915 : : * state. On each call, we are supposed to return a malloc'd string
5916 : : * that is a candidate completion. Return NULL when done.
5917 : : */
5918 : : static char *
5919 : 282 : _complete_from_query(const char *simple_query,
5920 : : const SchemaQuery *schema_query,
5921 : : const char *const *keywords,
5922 : : bool verbatim,
5923 : : const char *text, int state)
5924 : : {
5925 : : static int list_index,
5926 : : num_schema_only,
5927 : : num_query_other,
5928 : : num_keywords;
5929 : : static PGresult *result = NULL;
5930 : : static bool non_empty_object;
5931 : : static bool schemaquoted;
5932 : : static bool objectquoted;
5933 : :
5934 : : /*
5935 : : * If this is the first time for this completion, we fetch a list of our
5936 : : * "things" from the backend.
5937 : : */
5938 [ + + ]: 282 : if (state == 0)
5939 : : {
5940 : : PQExpBufferData query_buffer;
5941 : : char *schemaname;
5942 : : char *objectname;
5943 : : char *e_object_like;
5944 : : char *e_schemaname;
5945 : : char *e_ref_object;
5946 : : char *e_ref_schema;
5947 : :
5948 : : /* Reset static state, ensuring no memory leaks */
5949 : 44 : list_index = 0;
5950 : 44 : num_schema_only = 0;
5951 : 44 : num_query_other = 0;
5952 : 44 : num_keywords = 0;
5953 : 44 : PQclear(result);
5954 : 44 : result = NULL;
5955 : :
5956 : : /* Parse text, splitting into schema and object name if needed */
5957 [ + + ]: 44 : if (verbatim)
5958 : : {
5959 : 8 : objectname = pg_strdup(text);
5960 : 8 : schemaname = NULL;
5961 : : }
5962 : : else
5963 : : {
5964 : 36 : parse_identifier(text,
5965 : : &schemaname, &objectname,
5966 : : &schemaquoted, &objectquoted);
5967 : : }
5968 : :
5969 : : /* Remember whether the user has typed anything in the object part */
5970 : 44 : non_empty_object = (*objectname != '\0');
5971 : :
5972 : : /*
5973 : : * Convert objectname to a LIKE prefix pattern (e.g. 'foo%'), and set
5974 : : * up suitably-escaped copies of all the strings we need.
5975 : : */
5976 : 44 : e_object_like = make_like_pattern(objectname);
5977 : :
5978 [ + + ]: 44 : if (schemaname)
5979 : 3 : e_schemaname = escape_string(schemaname);
5980 : : else
5981 : 41 : e_schemaname = NULL;
5982 : :
5983 [ + + ]: 44 : if (completion_ref_object)
5984 : 21 : e_ref_object = escape_string(completion_ref_object);
5985 : : else
5986 : 23 : e_ref_object = NULL;
5987 : :
5988 [ + + ]: 44 : if (completion_ref_schema)
5989 : 1 : e_ref_schema = escape_string(completion_ref_schema);
5990 : : else
5991 : 43 : e_ref_schema = NULL;
5992 : :
5993 : 44 : initPQExpBuffer(&query_buffer);
5994 : :
5995 [ + + ]: 44 : if (schema_query)
5996 : : {
5997 : : Assert(simple_query == NULL);
5998 : :
5999 : : /*
6000 : : * We issue different queries depending on whether the input is
6001 : : * already qualified or not. schema_query gives us the pieces to
6002 : : * assemble.
6003 : : */
6004 [ + + - + ]: 36 : if (schemaname == NULL || schema_query->namespace == NULL)
6005 : : {
6006 : : /* Get unqualified names matching the input-so-far */
6007 : 33 : appendPQExpBufferStr(&query_buffer, "SELECT ");
6008 [ - + ]: 33 : if (schema_query->use_distinct)
6009 : 0 : appendPQExpBufferStr(&query_buffer, "DISTINCT ");
6010 : 33 : appendPQExpBuffer(&query_buffer,
6011 : : "%s, NULL::pg_catalog.text FROM %s",
6012 : 33 : schema_query->result,
6013 : 33 : schema_query->catname);
6014 [ + + + + ]: 33 : if (schema_query->refnamespace && completion_ref_schema)
6015 : 1 : appendPQExpBufferStr(&query_buffer,
6016 : : ", pg_catalog.pg_namespace nr");
6017 : 33 : appendPQExpBufferStr(&query_buffer, " WHERE ");
6018 [ + - ]: 33 : if (schema_query->selcondition)
6019 : 33 : appendPQExpBuffer(&query_buffer, "%s AND ",
6020 : 33 : schema_query->selcondition);
6021 : 33 : appendPQExpBuffer(&query_buffer, "(%s) LIKE '%s'",
6022 : 33 : schema_query->result,
6023 : : e_object_like);
6024 [ + + ]: 33 : if (schema_query->viscondition)
6025 : 15 : appendPQExpBuffer(&query_buffer, " AND %s",
6026 : 15 : schema_query->viscondition);
6027 [ + + ]: 33 : if (schema_query->refname)
6028 : : {
6029 : : Assert(completion_ref_object);
6030 : 18 : appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
6031 : 18 : schema_query->refname, e_ref_object);
6032 [ + - + + ]: 18 : if (schema_query->refnamespace && completion_ref_schema)
6033 : 1 : appendPQExpBuffer(&query_buffer,
6034 : : " AND %s = nr.oid AND nr.nspname = '%s'",
6035 : 1 : schema_query->refnamespace,
6036 : : e_ref_schema);
6037 [ + - ]: 17 : else if (schema_query->refviscondition)
6038 : 17 : appendPQExpBuffer(&query_buffer,
6039 : : " AND %s",
6040 : 17 : schema_query->refviscondition);
6041 : : }
6042 : :
6043 : : /*
6044 : : * When fetching relation names, suppress system catalogs
6045 : : * unless the input-so-far begins with "pg_". This is a
6046 : : * compromise between not offering system catalogs for
6047 : : * completion at all, and having them swamp the result when
6048 : : * the input is just "p".
6049 : : */
6050 [ + + ]: 33 : if (strcmp(schema_query->catname,
6051 : 14 : "pg_catalog.pg_class c") == 0 &&
6052 [ + - ]: 14 : strncmp(objectname, "pg_", 3) != 0)
6053 : : {
6054 : 14 : appendPQExpBufferStr(&query_buffer,
6055 : : " AND c.relnamespace <> (SELECT oid FROM"
6056 : : " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')");
6057 : : }
6058 : :
6059 : : /*
6060 : : * If the target object type can be schema-qualified, add in
6061 : : * schema names matching the input-so-far.
6062 : : */
6063 [ + + ]: 33 : if (schema_query->namespace)
6064 : : {
6065 : 15 : appendPQExpBuffer(&query_buffer, "\nUNION ALL\n"
6066 : : "SELECT NULL::pg_catalog.text, n.nspname "
6067 : : "FROM pg_catalog.pg_namespace n "
6068 : : "WHERE n.nspname LIKE '%s'",
6069 : : e_object_like);
6070 : :
6071 : : /*
6072 : : * Likewise, suppress system schemas unless the
6073 : : * input-so-far begins with "pg_".
6074 : : */
6075 [ + - ]: 15 : if (strncmp(objectname, "pg_", 3) != 0)
6076 : 15 : appendPQExpBufferStr(&query_buffer,
6077 : : " AND n.nspname NOT LIKE E'pg\\\\_%'");
6078 : :
6079 : : /*
6080 : : * Since we're matching these schema names to the object
6081 : : * name, handle their quoting using the object name's
6082 : : * quoting state.
6083 : : */
6084 : 15 : schemaquoted = objectquoted;
6085 : : }
6086 : : }
6087 : : else
6088 : : {
6089 : : /* Input is qualified, so produce only qualified names */
6090 : 3 : appendPQExpBufferStr(&query_buffer, "SELECT ");
6091 [ + + ]: 3 : if (schema_query->use_distinct)
6092 : 1 : appendPQExpBufferStr(&query_buffer, "DISTINCT ");
6093 : 3 : appendPQExpBuffer(&query_buffer, "%s, n.nspname "
6094 : : "FROM %s, pg_catalog.pg_namespace n",
6095 : 3 : schema_query->result,
6096 : 3 : schema_query->catname);
6097 [ - + - - ]: 3 : if (schema_query->refnamespace && completion_ref_schema)
6098 : 0 : appendPQExpBufferStr(&query_buffer,
6099 : : ", pg_catalog.pg_namespace nr");
6100 : 3 : appendPQExpBuffer(&query_buffer, " WHERE %s = n.oid AND ",
6101 : 3 : schema_query->namespace);
6102 [ + - ]: 3 : if (schema_query->selcondition)
6103 : 3 : appendPQExpBuffer(&query_buffer, "%s AND ",
6104 : 3 : schema_query->selcondition);
6105 : 3 : appendPQExpBuffer(&query_buffer, "(%s) LIKE '%s' AND ",
6106 : 3 : schema_query->result,
6107 : : e_object_like);
6108 : 3 : appendPQExpBuffer(&query_buffer, "n.nspname = '%s'",
6109 : : e_schemaname);
6110 [ + + ]: 3 : if (schema_query->refname)
6111 : : {
6112 : : Assert(completion_ref_object);
6113 : 1 : appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
6114 : 1 : schema_query->refname, e_ref_object);
6115 [ - + - - ]: 1 : if (schema_query->refnamespace && completion_ref_schema)
6116 : 0 : appendPQExpBuffer(&query_buffer,
6117 : : " AND %s = nr.oid AND nr.nspname = '%s'",
6118 : 0 : schema_query->refnamespace,
6119 : : e_ref_schema);
6120 [ - + ]: 1 : else if (schema_query->refviscondition)
6121 : 0 : appendPQExpBuffer(&query_buffer,
6122 : : " AND %s",
6123 : 0 : schema_query->refviscondition);
6124 : : }
6125 : : }
6126 : : }
6127 : : else
6128 : : {
6129 : : Assert(simple_query);
6130 : : /* simple_query is an sprintf-style format string */
6131 : 8 : appendPQExpBuffer(&query_buffer, simple_query,
6132 : : e_object_like,
6133 : : e_ref_object, e_ref_schema);
6134 : : }
6135 : :
6136 : : /* Limit the number of records in the result */
6137 : 44 : appendPQExpBuffer(&query_buffer, "\nLIMIT %d",
6138 : : completion_max_records);
6139 : :
6140 : : /* Finally, we can issue the query */
6141 : 44 : result = exec_query(query_buffer.data);
6142 : :
6143 : : /* Clean up */
6144 : 44 : termPQExpBuffer(&query_buffer);
6145 : 44 : pg_free(schemaname);
6146 : 44 : pg_free(objectname);
6147 : 44 : pg_free(e_object_like);
6148 : 44 : pg_free(e_schemaname);
6149 : 44 : pg_free(e_ref_object);
6150 : 44 : pg_free(e_ref_schema);
6151 : : }
6152 : :
6153 : : /* Return the next result, if any, but not if the query failed */
6154 [ + - + - ]: 282 : if (result && PQresultStatus(result) == PGRES_TUPLES_OK)
6155 : : {
6156 : : int nskip;
6157 : :
6158 [ + + ]: 282 : while (list_index < PQntuples(result))
6159 : : {
6160 : 213 : const char *item = NULL;
6161 : 213 : const char *nsp = NULL;
6162 : :
6163 [ + + ]: 213 : if (!PQgetisnull(result, list_index, 0))
6164 : 212 : item = PQgetvalue(result, list_index, 0);
6165 [ + + + + ]: 246 : if (PQnfields(result) > 1 &&
6166 : 33 : !PQgetisnull(result, list_index, 1))
6167 : 4 : nsp = PQgetvalue(result, list_index, 1);
6168 : 213 : list_index++;
6169 : :
6170 : : /* In verbatim mode, we return all the items as-is */
6171 [ + + ]: 213 : if (verbatim)
6172 : : {
6173 : 182 : num_query_other++;
6174 : 182 : return pg_strdup(item);
6175 : : }
6176 : :
6177 : : /*
6178 : : * In normal mode, a name requiring quoting will be returned only
6179 : : * if the input was empty or quoted. Otherwise the user might see
6180 : : * completion inserting a quote she didn't type, which is
6181 : : * surprising. This restriction also dodges some odd behaviors of
6182 : : * some versions of readline/libedit.
6183 : : */
6184 [ + + ]: 31 : if (non_empty_object)
6185 : : {
6186 [ + + + + : 29 : if (item && !objectquoted && identifier_needs_quotes(item))
- + ]
6187 : 0 : continue;
6188 [ + + + - : 29 : if (nsp && !schemaquoted && identifier_needs_quotes(nsp))
- + ]
6189 : 0 : continue;
6190 : : }
6191 : :
6192 : : /* Count schema-only results for hack below */
6193 [ + + + - ]: 31 : if (item == NULL && nsp != NULL)
6194 : 1 : num_schema_only++;
6195 : : else
6196 : 30 : num_query_other++;
6197 : :
6198 : 31 : return requote_identifier(nsp, item, schemaquoted, objectquoted);
6199 : : }
6200 : :
6201 : : /*
6202 : : * When the query result is exhausted, check for hard-wired keywords.
6203 : : * These will only be returned if they match the input-so-far,
6204 : : * ignoring case.
6205 : : */
6206 : 69 : nskip = list_index - PQntuples(result);
6207 [ + + + + ]: 69 : if (schema_query && schema_query->keywords)
6208 : : {
6209 : 2 : const char *const *itemp = schema_query->keywords;
6210 : :
6211 [ + + ]: 9 : while (*itemp)
6212 : : {
6213 : 8 : const char *item = *itemp++;
6214 : :
6215 [ + + ]: 8 : if (nskip-- > 0)
6216 : 1 : continue;
6217 : 7 : list_index++;
6218 [ + + ]: 7 : if (pg_strncasecmp(text, item, strlen(text)) == 0)
6219 : : {
6220 : 1 : num_keywords++;
6221 : 1 : return pg_strdup_keyword_case(item, text);
6222 : : }
6223 : : }
6224 : : }
6225 [ + + ]: 68 : if (keywords)
6226 : : {
6227 : 44 : const char *const *itemp = keywords;
6228 : :
6229 [ + + ]: 115 : while (*itemp)
6230 : : {
6231 : 95 : const char *item = *itemp++;
6232 : :
6233 [ + + ]: 95 : if (nskip-- > 0)
6234 : 36 : continue;
6235 : 59 : list_index++;
6236 [ + + ]: 59 : if (pg_strncasecmp(text, item, strlen(text)) == 0)
6237 : : {
6238 : 24 : num_keywords++;
6239 : 24 : return pg_strdup_keyword_case(item, text);
6240 : : }
6241 : : }
6242 : : }
6243 : : }
6244 : :
6245 : : /*
6246 : : * Hack: if we returned only bare schema names, don't let Readline add a
6247 : : * space afterwards. Otherwise the schema will stop being part of the
6248 : : * completion subject text, which is not what we want.
6249 : : */
6250 [ + + + - : 44 : if (num_schema_only > 0 && num_query_other == 0 && num_keywords == 0)
+ - ]
6251 : 1 : rl_completion_append_character = '\0';
6252 : :
6253 : : /* No more matches, so free the result structure and return null */
6254 : 44 : PQclear(result);
6255 : 44 : result = NULL;
6256 : 44 : return NULL;
6257 : : }
6258 : :
6259 : :
6260 : : /*
6261 : : * Set up completion_ref_object and completion_ref_schema
6262 : : * by parsing the given word. These variables can then be
6263 : : * used in a query passed to _complete_from_query.
6264 : : */
6265 : : static void
6266 : 19 : set_completion_reference(const char *word)
6267 : : {
6268 : : bool schemaquoted,
6269 : : objectquoted;
6270 : :
6271 : 19 : parse_identifier(word,
6272 : : &completion_ref_schema, &completion_ref_object,
6273 : : &schemaquoted, &objectquoted);
6274 : 19 : }
6275 : :
6276 : : /*
6277 : : * Set up completion_ref_object when it should just be
6278 : : * the given word verbatim.
6279 : : */
6280 : : static void
6281 : 2 : set_completion_reference_verbatim(const char *word)
6282 : : {
6283 : 2 : completion_ref_schema = NULL;
6284 : 2 : completion_ref_object = pg_strdup(word);
6285 : 2 : }
6286 : :
6287 : :
6288 : : /*
6289 : : * This function returns in order one of a fixed, NULL pointer terminated list
6290 : : * of strings (if matching). This can be used if there are only a fixed number
6291 : : * SQL words that can appear at certain spot.
6292 : : */
6293 : : static char *
6294 : 32 : complete_from_list(const char *text, int state)
6295 : : {
6296 : : static int string_length,
6297 : : list_index,
6298 : : matches;
6299 : : static bool casesensitive;
6300 : : const char *item;
6301 : :
6302 : : /* need to have a list */
6303 : : Assert(completion_charpp != NULL);
6304 : :
6305 : : /* Initialization */
6306 [ + + ]: 32 : if (state == 0)
6307 : : {
6308 : 13 : list_index = 0;
6309 : 13 : string_length = strlen(text);
6310 : 13 : casesensitive = completion_case_sensitive;
6311 : 13 : matches = 0;
6312 : : }
6313 : :
6314 [ + + ]: 546 : while ((item = completion_charpp[list_index++]))
6315 : : {
6316 : : /* First pass is case sensitive */
6317 [ + + + + ]: 500 : if (casesensitive && strncmp(text, item, string_length) == 0)
6318 : : {
6319 : 4 : matches++;
6320 : 4 : return pg_strdup(item);
6321 : : }
6322 : :
6323 : : /* Second pass is case insensitive, don't bother counting matches */
6324 [ + + + + ]: 496 : if (!casesensitive && pg_strncasecmp(text, item, string_length) == 0)
6325 : : {
6326 [ + + ]: 14 : if (completion_case_sensitive)
6327 : 1 : return pg_strdup(item);
6328 : : else
6329 : :
6330 : : /*
6331 : : * If case insensitive matching was requested initially,
6332 : : * adjust the case according to setting.
6333 : : */
6334 : 13 : return pg_strdup_keyword_case(item, text);
6335 : : }
6336 : : }
6337 : :
6338 : : /*
6339 : : * No matches found. If we're not case insensitive already, lets switch to
6340 : : * being case insensitive and try again
6341 : : */
6342 [ + + + + ]: 14 : if (casesensitive && matches == 0)
6343 : : {
6344 : 1 : casesensitive = false;
6345 : 1 : list_index = 0;
6346 : 1 : state++;
6347 : 1 : return complete_from_list(text, state);
6348 : : }
6349 : :
6350 : : /* If no more matches, return null. */
6351 : 13 : return NULL;
6352 : : }
6353 : :
6354 : :
6355 : : /*
6356 : : * This function returns one fixed string the first time even if it doesn't
6357 : : * match what's there, and nothing the second time. The string
6358 : : * to be used must be in completion_charp.
6359 : : *
6360 : : * If the given string is "", this has the effect of preventing readline
6361 : : * from doing any completion. (Without this, readline tries to do filename
6362 : : * completion which is seldom the right thing.)
6363 : : *
6364 : : * If the given string is not empty, readline will replace whatever the
6365 : : * user typed with that string. This behavior might be useful if it's
6366 : : * completely certain that we know what must appear at a certain spot,
6367 : : * so that it's okay to overwrite misspellings. In practice, given the
6368 : : * relatively lame parsing technology used in this file, the level of
6369 : : * certainty is seldom that high, so that you probably don't want to
6370 : : * use this. Use complete_from_list with a one-element list instead;
6371 : : * that won't try to auto-correct "misspellings".
6372 : : */
6373 : : static char *
6374 : 4 : complete_from_const(const char *text, int state)
6375 : : {
6376 : : Assert(completion_charp != NULL);
6377 [ + + ]: 4 : if (state == 0)
6378 : : {
6379 [ + - ]: 2 : if (completion_case_sensitive)
6380 : 2 : return pg_strdup(completion_charp);
6381 : : else
6382 : :
6383 : : /*
6384 : : * If case insensitive matching was requested initially, adjust
6385 : : * the case according to setting.
6386 : : */
6387 : 0 : return pg_strdup_keyword_case(completion_charp, text);
6388 : : }
6389 : : else
6390 : 2 : return NULL;
6391 : : }
6392 : :
6393 : :
6394 : : /*
6395 : : * This function appends the variable name with prefix and suffix to
6396 : : * the variable names array.
6397 : : */
6398 : : static void
6399 : 124 : append_variable_names(char ***varnames, int *nvars,
6400 : : int *maxvars, const char *varname,
6401 : : const char *prefix, const char *suffix)
6402 : : {
6403 [ - + ]: 124 : if (*nvars >= *maxvars)
6404 : : {
6405 : 0 : *maxvars *= 2;
6406 : 0 : *varnames = pg_realloc_array(*varnames, char *, (*maxvars) + 1);
6407 : : }
6408 : :
6409 : 124 : (*varnames)[(*nvars)++] = psprintf("%s%s%s", prefix, varname, suffix);
6410 : 124 : }
6411 : :
6412 : :
6413 : : /*
6414 : : * This function supports completion with the name of a psql variable.
6415 : : * The variable names can be prefixed and suffixed with additional text
6416 : : * to support quoting usages. If need_value is true, only variables
6417 : : * that are currently set are included; otherwise, special variables
6418 : : * (those that have hooks) are included even if currently unset.
6419 : : */
6420 : : static char **
6421 : 3 : complete_from_variables(const char *text, const char *prefix, const char *suffix,
6422 : : bool need_value)
6423 : : {
6424 : : char **matches;
6425 : : char **varnames;
6426 : 3 : int nvars = 0;
6427 : 3 : int maxvars = 100;
6428 : : int i;
6429 : : struct _variable *ptr;
6430 : :
6431 : 3 : varnames = pg_malloc_array(char *, maxvars + 1);
6432 : :
6433 [ + + ]: 129 : for (ptr = pset.vars->next; ptr; ptr = ptr->next)
6434 : : {
6435 [ + + + + ]: 126 : if (need_value && !(ptr->value))
6436 : 2 : continue;
6437 : 124 : append_variable_names(&varnames, &nvars, &maxvars, ptr->name,
6438 : : prefix, suffix);
6439 : : }
6440 : :
6441 : 3 : varnames[nvars] = NULL;
6442 : 3 : COMPLETE_WITH_LIST_CS((const char *const *) varnames);
6443 : :
6444 [ + + ]: 127 : for (i = 0; i < nvars; i++)
6445 : 124 : pg_free(varnames[i]);
6446 : 3 : pg_free(varnames);
6447 : :
6448 : 3 : return matches;
6449 : : }
6450 : :
6451 : :
6452 : : /*
6453 : : * This function returns in order one of a fixed, NULL pointer terminated list
6454 : : * of string that matches file names or optionally specified list of keywords.
6455 : : *
6456 : : * If completion_charpp is set to a null-terminated array of literal keywords,
6457 : : * those keywords are added to the completion results alongside filenames if
6458 : : * they case-insensitively match the current input.
6459 : : */
6460 : : static char *
6461 : 16 : complete_from_files(const char *text, int state)
6462 : : {
6463 : : static int list_index;
6464 : : static bool files_done;
6465 : : const char *item;
6466 : :
6467 : : /* Initialization */
6468 [ + + ]: 16 : if (state == 0)
6469 : : {
6470 : 6 : list_index = 0;
6471 : 6 : files_done = false;
6472 : : }
6473 : :
6474 [ + - ]: 16 : if (!files_done)
6475 : : {
6476 : 16 : char *result = _complete_from_files(text, state);
6477 : :
6478 : : /* Return a filename that matches */
6479 [ + + ]: 16 : if (result)
6480 : 10 : return result;
6481 : :
6482 : : /* There are no more matching files */
6483 : 6 : files_done = true;
6484 : : }
6485 : :
6486 [ + + ]: 6 : if (!completion_charpp)
6487 : 2 : return NULL;
6488 : :
6489 : : /*
6490 : : * Check for hard-wired keywords. These will only be returned if they
6491 : : * match the input-so-far, ignoring case.
6492 : : */
6493 [ + + ]: 12 : while ((item = completion_charpp[list_index++]))
6494 : : {
6495 [ - + ]: 8 : if (pg_strncasecmp(text, item, strlen(text)) == 0)
6496 : : {
6497 : 0 : completion_force_quote = false;
6498 : 0 : return pg_strdup_keyword_case(item, text);
6499 : : }
6500 : : }
6501 : :
6502 : 4 : return NULL;
6503 : : }
6504 : :
6505 : : /*
6506 : : * This function wraps rl_filename_completion_function() to strip quotes from
6507 : : * the input before searching for matches and to quote any matches for which
6508 : : * the consuming command will require it.
6509 : : *
6510 : : * Caller must set completion_charp to a zero- or one-character string
6511 : : * containing the escape character. This is necessary since \copy has no
6512 : : * escape character, but every other backslash command recognizes "\" as an
6513 : : * escape character.
6514 : : *
6515 : : * Caller must also set completion_force_quote to indicate whether to force
6516 : : * quotes around the result. (The SQL COPY command requires that.)
6517 : : */
6518 : : static char *
6519 : 16 : _complete_from_files(const char *text, int state)
6520 : : {
6521 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
6522 : :
6523 : : /*
6524 : : * If we're using a version of Readline that supports filename quoting
6525 : : * hooks, rely on those, and invoke rl_filename_completion_function()
6526 : : * without messing with its arguments. Readline does stuff internally
6527 : : * that does not work well at all if we try to handle dequoting here.
6528 : : * Instead, Readline will call quote_file_name() and dequote_file_name()
6529 : : * (see below) at appropriate times.
6530 : : *
6531 : : * ... or at least, mostly it will. There are some paths involving
6532 : : * unmatched file names in which Readline never calls quote_file_name(),
6533 : : * and if left to its own devices it will incorrectly append a quote
6534 : : * anyway. Set rl_completion_suppress_quote to prevent that. If we do
6535 : : * get to quote_file_name(), we'll clear this again. (Yes, this seems
6536 : : * like it's working around Readline bugs.)
6537 : : */
6538 : : #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
6539 : 16 : rl_completion_suppress_quote = 1;
6540 : : #endif
6541 : :
6542 : : /* If user typed a quote, force quoting (never remove user's quote) */
6543 [ - + ]: 16 : if (*text == '\'')
6544 : 0 : completion_force_quote = true;
6545 : :
6546 : 16 : return rl_filename_completion_function(text, state);
6547 : : #else
6548 : :
6549 : : /*
6550 : : * Otherwise, we have to do the best we can.
6551 : : */
6552 : : static const char *unquoted_text;
6553 : : char *unquoted_match;
6554 : : char *ret = NULL;
6555 : :
6556 : : /* If user typed a quote, force quoting (never remove user's quote) */
6557 : : if (*text == '\'')
6558 : : completion_force_quote = true;
6559 : :
6560 : : if (state == 0)
6561 : : {
6562 : : /* Initialization: stash the unquoted input. */
6563 : : unquoted_text = strtokx(text, "", NULL, "'", *completion_charp,
6564 : : false, true, pset.encoding);
6565 : : /* expect a NULL return for the empty string only */
6566 : : if (!unquoted_text)
6567 : : {
6568 : : Assert(*text == '\0');
6569 : : unquoted_text = text;
6570 : : }
6571 : : }
6572 : :
6573 : : unquoted_match = rl_filename_completion_function(unquoted_text, state);
6574 : : if (unquoted_match)
6575 : : {
6576 : : struct stat statbuf;
6577 : : bool is_dir = (stat(unquoted_match, &statbuf) == 0 &&
6578 : : S_ISDIR(statbuf.st_mode) != 0);
6579 : :
6580 : : /* Re-quote the result, if needed. */
6581 : : ret = quote_if_needed(unquoted_match, " \t\r\n\"`",
6582 : : '\'', *completion_charp,
6583 : : completion_force_quote,
6584 : : pset.encoding);
6585 : : if (ret)
6586 : : free(unquoted_match);
6587 : : else
6588 : : ret = unquoted_match;
6589 : :
6590 : : /*
6591 : : * If it's a directory, replace trailing quote with a slash; this is
6592 : : * usually more convenient. (If we didn't quote, leave this to
6593 : : * libedit.)
6594 : : */
6595 : : if (*ret == '\'' && is_dir)
6596 : : {
6597 : : char *retend = ret + strlen(ret) - 1;
6598 : :
6599 : : Assert(*retend == '\'');
6600 : : *retend = '/';
6601 : : /* Prevent libedit from adding a space, too */
6602 : : rl_completion_append_character = '\0';
6603 : : }
6604 : : }
6605 : :
6606 : : return ret;
6607 : : #endif /* USE_FILENAME_QUOTING_FUNCTIONS */
6608 : : }
6609 : :
6610 : :
6611 : : /* HELPER FUNCTIONS */
6612 : :
6613 : :
6614 : : /*
6615 : : * Make a pg_strdup copy of s and convert the case according to
6616 : : * COMP_KEYWORD_CASE setting, using ref as the text that was already entered.
6617 : : */
6618 : : static char *
6619 : 40 : pg_strdup_keyword_case(const char *s, const char *ref)
6620 : : {
6621 : : char *ret,
6622 : : *p;
6623 : 40 : unsigned char first = ref[0];
6624 : :
6625 : 40 : ret = pg_strdup(s);
6626 : :
6627 [ + + ]: 40 : if (pset.comp_case == PSQL_COMP_CASE_LOWER ||
6628 [ + + ]: 34 : ((pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER ||
6629 [ + + + + ]: 34 : pset.comp_case == PSQL_COMP_CASE_PRESERVE_UPPER) && islower(first)) ||
6630 [ - + - - ]: 26 : (pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER && !isalpha(first)))
6631 : : {
6632 [ + + ]: 122 : for (p = ret; *p; p++)
6633 : 108 : *p = pg_tolower((unsigned char) *p);
6634 : : }
6635 : : else
6636 : : {
6637 [ + + ]: 241 : for (p = ret; *p; p++)
6638 : 215 : *p = pg_toupper((unsigned char) *p);
6639 : : }
6640 : :
6641 : 40 : return ret;
6642 : : }
6643 : :
6644 : :
6645 : : /*
6646 : : * escape_string - Escape argument for use as string literal.
6647 : : *
6648 : : * The returned value has to be freed.
6649 : : */
6650 : : static char *
6651 : 71 : escape_string(const char *text)
6652 : : {
6653 : : size_t text_length;
6654 : : char *result;
6655 : :
6656 : 71 : text_length = strlen(text);
6657 : :
6658 : 71 : result = pg_malloc(text_length * 2 + 1);
6659 : 71 : PQescapeStringConn(pset.db, result, text, text_length, NULL);
6660 : :
6661 : 71 : return result;
6662 : : }
6663 : :
6664 : :
6665 : : /*
6666 : : * make_like_pattern - Convert argument to a LIKE prefix pattern.
6667 : : *
6668 : : * We escape _ and % in the given text by backslashing, append a % to
6669 : : * represent "any subsequent characters", and then pass the string through
6670 : : * escape_string() so it's ready to insert in a query. The result needs
6671 : : * to be freed.
6672 : : */
6673 : : static char *
6674 : 44 : make_like_pattern(const char *word)
6675 : : {
6676 : : char *result;
6677 : 44 : char *buffer = pg_malloc(strlen(word) * 2 + 2);
6678 : 44 : char *bptr = buffer;
6679 : :
6680 [ + + ]: 183 : while (*word)
6681 : : {
6682 [ + + - + ]: 139 : if (*word == '_' || *word == '%')
6683 : 2 : *bptr++ = '\\';
6684 [ - + ]: 139 : if (IS_HIGHBIT_SET(*word))
6685 : : {
6686 : : /*
6687 : : * Transfer multibyte characters without further processing, to
6688 : : * avoid getting confused in unsafe client encodings.
6689 : : */
6690 : 0 : int chlen = PQmblenBounded(word, pset.encoding);
6691 : :
6692 [ # # ]: 0 : while (chlen-- > 0)
6693 : 0 : *bptr++ = *word++;
6694 : : }
6695 : : else
6696 : 139 : *bptr++ = *word++;
6697 : : }
6698 : 44 : *bptr++ = '%';
6699 : 44 : *bptr = '\0';
6700 : :
6701 : 44 : result = escape_string(buffer);
6702 : 44 : pg_free(buffer);
6703 : 44 : return result;
6704 : : }
6705 : :
6706 : :
6707 : : /*
6708 : : * parse_identifier - Parse a possibly-schema-qualified SQL identifier.
6709 : : *
6710 : : * This involves splitting off the schema name if present, de-quoting,
6711 : : * and downcasing any unquoted text. We are a bit laxer than the backend
6712 : : * in that we allow just portions of a name to be quoted --- that's because
6713 : : * psql metacommands have traditionally behaved that way.
6714 : : *
6715 : : * Outputs are a malloc'd schema name (NULL if none), malloc'd object name,
6716 : : * and booleans telling whether any part of the schema and object name was
6717 : : * double-quoted.
6718 : : */
6719 : : static void
6720 : 55 : parse_identifier(const char *ident,
6721 : : char **schemaname, char **objectname,
6722 : : bool *schemaquoted, bool *objectquoted)
6723 : : {
6724 : 55 : size_t buflen = strlen(ident) + 1;
6725 : 55 : bool enc_is_single_byte = (pg_encoding_max_length(pset.encoding) == 1);
6726 : : char *sname;
6727 : : char *oname;
6728 : : char *optr;
6729 : : bool inquotes;
6730 : :
6731 : : /* Initialize, making a certainly-large-enough output buffer */
6732 : 55 : sname = NULL;
6733 : 55 : oname = pg_malloc(buflen);
6734 : 55 : *schemaquoted = *objectquoted = false;
6735 : : /* Scan */
6736 : 55 : optr = oname;
6737 : 55 : inquotes = false;
6738 [ + + ]: 271 : while (*ident)
6739 : : {
6740 : 216 : unsigned char ch = (unsigned char) *ident++;
6741 : :
6742 [ + + ]: 216 : if (ch == '"')
6743 : : {
6744 [ + + - + ]: 7 : if (inquotes && *ident == '"')
6745 : : {
6746 : : /* two quote marks within a quoted identifier = emit quote */
6747 : 0 : *optr++ = '"';
6748 : 0 : ident++;
6749 : : }
6750 : : else
6751 : : {
6752 : 7 : inquotes = !inquotes;
6753 : 7 : *objectquoted = true;
6754 : : }
6755 : : }
6756 [ + + + - ]: 209 : else if (ch == '.' && !inquotes)
6757 : : {
6758 : : /* Found a schema name, transfer it to sname / *schemaquoted */
6759 : 4 : *optr = '\0';
6760 : 4 : free(sname); /* drop any catalog name */
6761 : 4 : sname = oname;
6762 : 4 : oname = pg_malloc(buflen);
6763 : 4 : optr = oname;
6764 : 4 : *schemaquoted = *objectquoted;
6765 : 4 : *objectquoted = false;
6766 : : }
6767 [ + - - + ]: 205 : else if (!enc_is_single_byte && IS_HIGHBIT_SET(ch))
6768 : 0 : {
6769 : : /*
6770 : : * Transfer multibyte characters without further processing. They
6771 : : * wouldn't be affected by our downcasing rule anyway, and this
6772 : : * avoids possibly doing the wrong thing in unsafe client
6773 : : * encodings.
6774 : : */
6775 : 0 : int chlen = PQmblenBounded(ident - 1, pset.encoding);
6776 : :
6777 : 0 : *optr++ = (char) ch;
6778 [ # # ]: 0 : while (--chlen > 0)
6779 : 0 : *optr++ = *ident++;
6780 : : }
6781 : : else
6782 : : {
6783 [ + + ]: 205 : if (!inquotes)
6784 : : {
6785 : : /*
6786 : : * This downcasing transformation should match the backend's
6787 : : * downcase_identifier() as best we can. We do not know the
6788 : : * backend's locale, though, so it's necessarily approximate.
6789 : : * We assume that psql is operating in the same locale and
6790 : : * encoding as the backend.
6791 : : */
6792 [ + + + + ]: 181 : if (ch >= 'A' && ch <= 'Z')
6793 : 28 : ch += 'a' - 'A';
6794 [ - + - - : 153 : else if (enc_is_single_byte && IS_HIGHBIT_SET(ch) && isupper(ch))
- - ]
6795 : 0 : ch = tolower(ch);
6796 : : }
6797 : 205 : *optr++ = (char) ch;
6798 : : }
6799 : : }
6800 : :
6801 : 55 : *optr = '\0';
6802 : 55 : *schemaname = sname;
6803 : 55 : *objectname = oname;
6804 : 55 : }
6805 : :
6806 : :
6807 : : /*
6808 : : * requote_identifier - Reconstruct a possibly-schema-qualified SQL identifier.
6809 : : *
6810 : : * Build a malloc'd string containing the identifier, with quoting applied
6811 : : * as necessary. This is more or less the inverse of parse_identifier;
6812 : : * in particular, if an input component was quoted, we'll quote the output
6813 : : * even when that isn't strictly required.
6814 : : *
6815 : : * Unlike parse_identifier, we handle the case where a schema and no
6816 : : * object name is provided, producing just "schema.".
6817 : : */
6818 : : static char *
6819 : 31 : requote_identifier(const char *schemaname, const char *objectname,
6820 : : bool quote_schema, bool quote_object)
6821 : : {
6822 : : char *result;
6823 : 31 : size_t buflen = 1; /* count the trailing \0 */
6824 : : char *ptr;
6825 : :
6826 : : /*
6827 : : * We could use PQescapeIdentifier for some of this, but not all, and it
6828 : : * adds more notational cruft than it seems worth.
6829 : : */
6830 [ + + ]: 31 : if (schemaname)
6831 : : {
6832 : 4 : buflen += strlen(schemaname) + 1; /* +1 for the dot */
6833 [ + - ]: 4 : if (!quote_schema)
6834 : 4 : quote_schema = identifier_needs_quotes(schemaname);
6835 [ - + ]: 4 : if (quote_schema)
6836 : : {
6837 : 0 : buflen += 2; /* account for quote marks */
6838 [ # # ]: 0 : for (const char *p = schemaname; *p; p++)
6839 : : {
6840 [ # # ]: 0 : if (*p == '"')
6841 : 0 : buflen++;
6842 : : }
6843 : : }
6844 : : }
6845 [ + + ]: 31 : if (objectname)
6846 : : {
6847 : 30 : buflen += strlen(objectname);
6848 [ + + ]: 30 : if (!quote_object)
6849 : 22 : quote_object = identifier_needs_quotes(objectname);
6850 [ + + ]: 30 : if (quote_object)
6851 : : {
6852 : 8 : buflen += 2; /* account for quote marks */
6853 [ + + ]: 73 : for (const char *p = objectname; *p; p++)
6854 : : {
6855 [ - + ]: 65 : if (*p == '"')
6856 : 0 : buflen++;
6857 : : }
6858 : : }
6859 : : }
6860 : 31 : result = pg_malloc(buflen);
6861 : 31 : ptr = result;
6862 [ + + ]: 31 : if (schemaname)
6863 : : {
6864 [ - + ]: 4 : if (quote_schema)
6865 : 0 : *ptr++ = '"';
6866 [ + + ]: 28 : for (const char *p = schemaname; *p; p++)
6867 : : {
6868 : 24 : *ptr++ = *p;
6869 [ - + ]: 24 : if (*p == '"')
6870 : 0 : *ptr++ = '"';
6871 : : }
6872 [ - + ]: 4 : if (quote_schema)
6873 : 0 : *ptr++ = '"';
6874 : 4 : *ptr++ = '.';
6875 : : }
6876 [ + + ]: 31 : if (objectname)
6877 : : {
6878 [ + + ]: 30 : if (quote_object)
6879 : 8 : *ptr++ = '"';
6880 [ + + ]: 264 : for (const char *p = objectname; *p; p++)
6881 : : {
6882 : 234 : *ptr++ = *p;
6883 [ - + ]: 234 : if (*p == '"')
6884 : 0 : *ptr++ = '"';
6885 : : }
6886 [ + + ]: 30 : if (quote_object)
6887 : 8 : *ptr++ = '"';
6888 : : }
6889 : 31 : *ptr = '\0';
6890 : 31 : return result;
6891 : : }
6892 : :
6893 : :
6894 : : /*
6895 : : * Detect whether an identifier must be double-quoted.
6896 : : *
6897 : : * Note we'll quote anything that's not ASCII; the backend's quote_ident()
6898 : : * does the same. Perhaps this could be relaxed in future.
6899 : : */
6900 : : static bool
6901 : 49 : identifier_needs_quotes(const char *ident)
6902 : : {
6903 : : int kwnum;
6904 : :
6905 : : /* Check syntax. */
6906 [ + - - + : 49 : if (!((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_'))
- - ]
6907 : 0 : return true;
6908 [ - + ]: 49 : if (strspn(ident, "abcdefghijklmnopqrstuvwxyz0123456789_$") != strlen(ident))
6909 : 0 : return true;
6910 : :
6911 : : /*
6912 : : * Check for keyword. We quote keywords except for unreserved ones.
6913 : : *
6914 : : * It is possible that our keyword list doesn't quite agree with the
6915 : : * server's, but this should be close enough for tab-completion purposes.
6916 : : *
6917 : : * Note: ScanKeywordLookup() does case-insensitive comparison, but that's
6918 : : * fine, since we already know we have all-lower-case.
6919 : : */
6920 : 49 : kwnum = ScanKeywordLookup(ident, &ScanKeywords);
6921 : :
6922 [ - + - - ]: 49 : if (kwnum >= 0 && ScanKeywordCategories[kwnum] != UNRESERVED_KEYWORD)
6923 : 0 : return true;
6924 : :
6925 : 49 : return false;
6926 : : }
6927 : :
6928 : :
6929 : : /*
6930 : : * Execute a query, returning NULL if there was any error.
6931 : : * This should be the preferred way of talking to the database in this file.
6932 : : */
6933 : : static PGresult *
6934 : 46 : exec_query(const char *query)
6935 : : {
6936 : : PGresult *result;
6937 : :
6938 [ + - + - : 46 : if (query == NULL || !pset.db || PQstatus(pset.db) != CONNECTION_OK)
- + ]
6939 : 0 : return NULL;
6940 : :
6941 : 46 : result = PQexec(pset.db, query);
6942 : :
6943 [ - + ]: 46 : if (PQresultStatus(result) != PGRES_TUPLES_OK)
6944 : : {
6945 : : /*
6946 : : * Printing an error while the user is typing would be quite annoying,
6947 : : * so we don't. This does complicate debugging of this code; but you
6948 : : * can look in the server log instead.
6949 : : */
6950 : : #ifdef NOT_USED
6951 : : pg_log_error("tab completion query failed: %s\nQuery was:\n%s",
6952 : : PQerrorMessage(pset.db), query);
6953 : : #endif
6954 : 0 : PQclear(result);
6955 : 0 : result = NULL;
6956 : : }
6957 : :
6958 : 46 : return result;
6959 : : }
6960 : :
6961 : :
6962 : : /*
6963 : : * Parse all the word(s) before point.
6964 : : *
6965 : : * Returns a malloc'd array of character pointers that point into the malloc'd
6966 : : * data array returned to *buffer; caller must free() both of these when done.
6967 : : * *nwords receives the number of words found, ie, the valid length of the
6968 : : * return array.
6969 : : *
6970 : : * Words are returned right to left, that is, previous_words[0] gets the last
6971 : : * word before point, previous_words[1] the next-to-last, etc.
6972 : : */
6973 : : static char **
6974 : 67 : get_previous_words(int point, char **buffer, int *nwords)
6975 : : {
6976 : : char **previous_words;
6977 : : char *buf;
6978 : : char *outptr;
6979 : 67 : int words_found = 0;
6980 : : int i;
6981 : :
6982 : : /*
6983 : : * If we have anything in tab_completion_query_buf, paste it together with
6984 : : * rl_line_buffer to construct the full query. Otherwise we can just use
6985 : : * rl_line_buffer as the input string.
6986 : : */
6987 [ + - + + ]: 67 : if (tab_completion_query_buf && tab_completion_query_buf->len > 0)
6988 : : {
6989 : 3 : i = tab_completion_query_buf->len;
6990 : 3 : buf = pg_malloc(point + i + 2);
6991 : 3 : memcpy(buf, tab_completion_query_buf->data, i);
6992 : 3 : buf[i++] = '\n';
6993 : 3 : memcpy(buf + i, rl_line_buffer, point);
6994 : 3 : i += point;
6995 : 3 : buf[i] = '\0';
6996 : : /* Readjust point to reference appropriate offset in buf */
6997 : 3 : point = i;
6998 : : }
6999 : : else
7000 : 64 : buf = rl_line_buffer;
7001 : :
7002 : : /*
7003 : : * Allocate an array of string pointers and a buffer to hold the strings
7004 : : * themselves. The worst case is that the line contains only
7005 : : * non-whitespace WORD_BREAKS characters, making each one a separate word.
7006 : : * This is usually much more space than we need, but it's cheaper than
7007 : : * doing a separate malloc() for each word.
7008 : : */
7009 : 67 : previous_words = pg_malloc_array(char *, point);
7010 : 67 : *buffer = outptr = (char *) pg_malloc(point * 2);
7011 : :
7012 : : /*
7013 : : * First we look for a non-word char before the current point. (This is
7014 : : * probably useless, if readline is on the same page as we are about what
7015 : : * is a word, but if so it's cheap.)
7016 : : */
7017 [ + + ]: 73 : for (i = point - 1; i >= 0; i--)
7018 : : {
7019 [ + + ]: 70 : if (strchr(WORD_BREAKS, buf[i]))
7020 : 64 : break;
7021 : : }
7022 : 67 : point = i;
7023 : :
7024 : : /*
7025 : : * Now parse words, working backwards, until we hit start of line. The
7026 : : * backwards scan has some interesting but intentional properties
7027 : : * concerning parenthesis handling.
7028 : : */
7029 [ + + ]: 255 : while (point >= 0)
7030 : : {
7031 : : int start,
7032 : : end;
7033 : 188 : bool inquotes = false;
7034 : 188 : int parentheses = 0;
7035 : :
7036 : : /* now find the first non-space which then constitutes the end */
7037 : 188 : end = -1;
7038 [ + - ]: 382 : for (i = point; i >= 0; i--)
7039 : : {
7040 [ + + ]: 382 : if (!isspace((unsigned char) buf[i]))
7041 : : {
7042 : 188 : end = i;
7043 : 188 : break;
7044 : : }
7045 : : }
7046 : : /* if no end found, we're done */
7047 [ - + ]: 188 : if (end < 0)
7048 : 0 : break;
7049 : :
7050 : : /*
7051 : : * Otherwise we now look for the start. The start is either the last
7052 : : * character before any word-break character going backwards from the
7053 : : * end, or it's simply character 0. We also handle open quotes and
7054 : : * parentheses.
7055 : : */
7056 [ + + ]: 943 : for (start = end; start > 0; start--)
7057 : : {
7058 [ + + ]: 879 : if (buf[start] == '"')
7059 : 2 : inquotes = !inquotes;
7060 [ + + ]: 879 : if (!inquotes)
7061 : : {
7062 [ - + ]: 874 : if (buf[start] == ')')
7063 : 0 : parentheses++;
7064 [ + + ]: 874 : else if (buf[start] == '(')
7065 : : {
7066 [ + - ]: 3 : if (--parentheses <= 0)
7067 : 3 : break;
7068 : : }
7069 [ + - ]: 871 : else if (parentheses == 0 &&
7070 [ + + ]: 871 : strchr(WORD_BREAKS, buf[start - 1]))
7071 : 121 : break;
7072 : : }
7073 : : }
7074 : :
7075 : : /* Return the word located at start to end inclusive */
7076 : 188 : previous_words[words_found++] = outptr;
7077 : 188 : i = end - start + 1;
7078 : 188 : memcpy(outptr, &buf[start], i);
7079 : 188 : outptr += i;
7080 : 188 : *outptr++ = '\0';
7081 : :
7082 : : /* Continue searching */
7083 : 188 : point = start - 1;
7084 : : }
7085 : :
7086 : : /* Release parsing input workspace, if we made one above */
7087 [ + + ]: 67 : if (buf != rl_line_buffer)
7088 : 3 : pg_free(buf);
7089 : :
7090 : 67 : *nwords = words_found;
7091 : 67 : return previous_words;
7092 : : }
7093 : :
7094 : : /*
7095 : : * Look up the type for the GUC variable with the passed name.
7096 : : *
7097 : : * Returns NULL if the variable is unknown. Otherwise the returned string,
7098 : : * containing the type, has to be freed.
7099 : : */
7100 : : static char *
7101 : 2 : get_guctype(const char *varname)
7102 : : {
7103 : : PQExpBufferData query_buffer;
7104 : : char *e_varname;
7105 : : PGresult *result;
7106 : 2 : char *guctype = NULL;
7107 : :
7108 : 2 : e_varname = escape_string(varname);
7109 : :
7110 : 2 : initPQExpBuffer(&query_buffer);
7111 : 2 : appendPQExpBuffer(&query_buffer,
7112 : : "SELECT vartype FROM pg_catalog.pg_settings "
7113 : : "WHERE pg_catalog.lower(name) = pg_catalog.lower('%s')",
7114 : : e_varname);
7115 : :
7116 : 2 : result = exec_query(query_buffer.data);
7117 : 2 : termPQExpBuffer(&query_buffer);
7118 : 2 : free(e_varname);
7119 : :
7120 [ + - + - ]: 2 : if (PQresultStatus(result) == PGRES_TUPLES_OK && PQntuples(result) > 0)
7121 : 2 : guctype = pg_strdup(PQgetvalue(result, 0, 0));
7122 : :
7123 : 2 : PQclear(result);
7124 : :
7125 : 2 : return guctype;
7126 : : }
7127 : :
7128 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
7129 : :
7130 : : /*
7131 : : * Quote a filename according to SQL rules, returning a malloc'd string.
7132 : : * completion_charp must point to escape character or '\0', and
7133 : : * completion_force_quote must be set correctly, as per comments for
7134 : : * complete_from_files().
7135 : : */
7136 : : static char *
7137 : 5 : quote_file_name(char *fname, int match_type, char *quote_pointer)
7138 : : {
7139 : : char *s;
7140 : : struct stat statbuf;
7141 : :
7142 : : /* Quote if needed. */
7143 : 5 : s = quote_if_needed(fname, " \t\r\n\"`",
7144 : 5 : '\'', *completion_charp,
7145 : : completion_force_quote,
7146 : : pset.encoding);
7147 [ + + ]: 5 : if (!s)
7148 : 2 : s = pg_strdup(fname);
7149 : :
7150 : : /*
7151 : : * However, some of the time we have to strip the trailing quote from what
7152 : : * we send back. Never strip the trailing quote if the user already typed
7153 : : * one; otherwise, suppress the trailing quote if we have multiple/no
7154 : : * matches (because we don't want to add a quote if the input is seemingly
7155 : : * unfinished), or if the input was already quoted (because Readline will
7156 : : * do arguably-buggy things otherwise), or if the file does not exist, or
7157 : : * if it's a directory.
7158 : : */
7159 [ + + ]: 5 : if (*s == '\'' &&
7160 [ + - + + ]: 3 : completion_last_char != '\'' &&
7161 [ + - ]: 1 : (match_type != SINGLE_MATCH ||
7162 [ + - + - ]: 2 : (quote_pointer && *quote_pointer == '\'') ||
7163 : 1 : stat(fname, &statbuf) != 0 ||
7164 [ - + ]: 1 : S_ISDIR(statbuf.st_mode)))
7165 : : {
7166 : 2 : char *send = s + strlen(s) - 1;
7167 : :
7168 : : Assert(*send == '\'');
7169 : 2 : *send = '\0';
7170 : : }
7171 : :
7172 : : /*
7173 : : * And now we can let Readline do its thing with possibly adding a quote
7174 : : * on its own accord. (This covers some additional cases beyond those
7175 : : * dealt with above.)
7176 : : */
7177 : : #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
7178 : 5 : rl_completion_suppress_quote = 0;
7179 : : #endif
7180 : :
7181 : : /*
7182 : : * If user typed a leading quote character other than single quote (i.e.,
7183 : : * double quote), zap it, so that we replace it with the correct single
7184 : : * quote.
7185 : : */
7186 [ + - + + ]: 5 : if (quote_pointer && *quote_pointer != '\'')
7187 : 4 : *quote_pointer = '\0';
7188 : :
7189 : 5 : return s;
7190 : : }
7191 : :
7192 : : /*
7193 : : * Dequote a filename, if it's quoted.
7194 : : * completion_charp must point to escape character or '\0', as per
7195 : : * comments for complete_from_files().
7196 : : */
7197 : : static char *
7198 : 12 : dequote_file_name(char *fname, int quote_char)
7199 : : {
7200 : : char *unquoted_fname;
7201 : :
7202 : : /*
7203 : : * If quote_char is set, it's not included in "fname". We have to add it
7204 : : * or strtokx will not interpret the string correctly (notably, it won't
7205 : : * recognize escapes).
7206 : : */
7207 [ + + ]: 12 : if (quote_char == '\'')
7208 : : {
7209 : 6 : char *workspace = (char *) pg_malloc(strlen(fname) + 2);
7210 : :
7211 : 6 : workspace[0] = quote_char;
7212 : 6 : strcpy(workspace + 1, fname);
7213 : 6 : unquoted_fname = strtokx(workspace, "", NULL, "'", *completion_charp,
7214 : : false, true, pset.encoding);
7215 : 6 : pg_free(workspace);
7216 : : }
7217 : : else
7218 : 6 : unquoted_fname = strtokx(fname, "", NULL, "'", *completion_charp,
7219 : : false, true, pset.encoding);
7220 : :
7221 : : /* expect a NULL return for the empty string only */
7222 [ - + ]: 12 : if (!unquoted_fname)
7223 : : {
7224 : : Assert(*fname == '\0');
7225 : 0 : unquoted_fname = fname;
7226 : : }
7227 : :
7228 : : /* readline expects a malloc'd result that it is to free */
7229 : 12 : return pg_strdup(unquoted_fname);
7230 : : }
7231 : :
7232 : : #endif /* USE_FILENAME_QUOTING_FUNCTIONS */
7233 : :
7234 : : #endif /* USE_READLINE */
|