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