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