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