Age Owner Branch data TLA Line data Source code
1 : : /*
2 : : * psql - the PostgreSQL interactive terminal
3 : : *
4 : : * Copyright (c) 2000-2026, PostgreSQL Global Development Group
5 : : *
6 : : * src/bin/psql/tab-complete.in.c
7 : : *
8 : : * Note: this will compile and work as-is if SWITCH_CONVERSION_APPLIED
9 : : * is not defined. However, the expected usage is that it's first run
10 : : * through gen_tabcomplete.pl, which will #define that symbol, fill in the
11 : : * tcpatterns[] array, and convert the else-if chain in match_previous_words()
12 : : * into a switch. See comments for match_previous_words() and the header
13 : : * comment in gen_tabcomplete.pl for more detail.
14 : : */
15 : :
16 : : /*----------------------------------------------------------------------
17 : : * This file implements a somewhat more sophisticated readline "TAB
18 : : * completion" in psql. It is not intended to be AI, to replace
19 : : * learning SQL, or to relieve you from thinking about what you're
20 : : * doing. Also it does not always give you all the syntactically legal
21 : : * completions, only those that are the most common or the ones that
22 : : * the programmer felt most like implementing.
23 : : *
24 : : * CAVEAT: Tab completion causes queries to be sent to the backend.
25 : : * The number of tuples returned gets limited, in most default
26 : : * installations to 1000, but if you still don't like this prospect,
27 : : * you can turn off tab completion in your ~/.inputrc (or else
28 : : * ${INPUTRC}) file so:
29 : : *
30 : : * $if psql
31 : : * set disable-completion on
32 : : * $endif
33 : : *
34 : : * See `man 3 readline' or `info readline' for the full details.
35 : : *
36 : : * BUGS:
37 : : * - Quotes, parentheses, and other funny characters are not handled
38 : : * all that gracefully.
39 : : *----------------------------------------------------------------------
40 : : */
41 : :
42 : : #include "postgres_fe.h"
43 : :
44 : : #include "input.h"
45 : : #include "tab-complete.h"
46 : :
47 : : /* If we don't have this, we might as well forget about the whole thing: */
48 : : #ifdef USE_READLINE
49 : :
50 : : #include <ctype.h>
51 : : #include <sys/stat.h>
52 : :
53 : : #include "catalog/pg_am_d.h"
54 : : #include "catalog/pg_class_d.h"
55 : : #include "common.h"
56 : : #include "common/keywords.h"
57 : : #include "libpq-fe.h"
58 : : #include "mb/pg_wchar.h"
59 : : #include "pqexpbuffer.h"
60 : : #include "settings.h"
61 : : #include "stringutils.h"
62 : :
63 : : /*
64 : : * Ancient versions of libedit provide filename_completion_function()
65 : : * instead of rl_filename_completion_function(). Likewise for
66 : : * [rl_]completion_matches().
67 : : */
68 : : #ifndef HAVE_RL_FILENAME_COMPLETION_FUNCTION
69 : : #define rl_filename_completion_function filename_completion_function
70 : : #endif
71 : :
72 : : #ifndef HAVE_RL_COMPLETION_MATCHES
73 : : #define rl_completion_matches completion_matches
74 : : #endif
75 : :
76 : : /*
77 : : * Currently we assume that rl_filename_dequoting_function exists if
78 : : * rl_filename_quoting_function does. If that proves not to be the case,
79 : : * we'd need to test for the former, or possibly both, in configure.
80 : : */
81 : : #ifdef HAVE_RL_FILENAME_QUOTING_FUNCTION
82 : : #define USE_FILENAME_QUOTING_FUNCTIONS 1
83 : : #endif
84 : :
85 : : /* word break characters */
86 : : #define WORD_BREAKS "\t\n@><=;|&() "
87 : :
88 : : /*
89 : : * Since readline doesn't let us pass any state through to the tab completion
90 : : * callback, we have to use this global variable to let get_previous_words()
91 : : * get at the previous lines of the current command. Ick.
92 : : */
93 : : PQExpBuffer tab_completion_query_buf = NULL;
94 : :
95 : : /*
96 : : * In some situations, the query to find out what names are available to
97 : : * complete with must vary depending on server version. We handle this by
98 : : * storing a list of queries, each tagged with the minimum server version
99 : : * it will work for. Each list must be stored in descending server version
100 : : * order, so that the first satisfactory query is the one to use.
101 : : *
102 : : * When the query string is otherwise constant, an array of VersionedQuery
103 : : * suffices. Terminate the array with an entry having min_server_version = 0.
104 : : * That entry's query string can be a query that works in all supported older
105 : : * server versions, or NULL to give up and do no completion.
106 : : */
107 : : typedef struct VersionedQuery
108 : : {
109 : : int min_server_version;
110 : : const char *query;
111 : : } VersionedQuery;
112 : :
113 : : /*
114 : : * This struct is used to define "schema queries", which are custom-built
115 : : * to obtain possibly-schema-qualified names of database objects. There is
116 : : * enough similarity in the structure that we don't want to repeat it each
117 : : * time. So we put the components of each query into this struct and
118 : : * assemble them with the common boilerplate in _complete_from_query().
119 : : *
120 : : * We also use this struct to define queries that use completion_ref_object,
121 : : * which is some object related to the one(s) we want to get the names of
122 : : * (for example, the table we want the indexes of). In that usage the
123 : : * objects we're completing might not have a schema of their own, but the
124 : : * reference object almost always does (passed in completion_ref_schema).
125 : : *
126 : : * As with VersionedQuery, we can use an array of these if the query details
127 : : * must vary across versions.
128 : : */
129 : : typedef struct SchemaQuery
130 : : {
131 : : /*
132 : : * If not zero, minimum server version this struct applies to. If not
133 : : * zero, there should be a following struct with a smaller minimum server
134 : : * version; use catname == NULL in the last entry if we should do nothing.
135 : : */
136 : : int min_server_version;
137 : :
138 : : /*
139 : : * Name of catalog or catalogs to be queried, with alias(es), eg.
140 : : * "pg_catalog.pg_class c". Note that "pg_namespace n" and/or
141 : : * "pg_namespace nr" will be added automatically when needed.
142 : : */
143 : : const char *catname;
144 : :
145 : : /*
146 : : * Selection condition --- only rows meeting this condition are candidates
147 : : * to display. If catname mentions multiple tables, include the necessary
148 : : * join condition here. For example, this might look like "c.relkind = "
149 : : * CppAsString2(RELKIND_RELATION). Write NULL (not an empty string) if
150 : : * not needed.
151 : : */
152 : : const char *selcondition;
153 : :
154 : : /*
155 : : * Visibility condition --- which rows are visible without schema
156 : : * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)".
157 : : * NULL if not needed.
158 : : */
159 : : const char *viscondition;
160 : :
161 : : /*
162 : : * Namespace --- name of field to join to pg_namespace.oid when there is
163 : : * schema qualification. For example, "c.relnamespace". NULL if we don't
164 : : * want to join to pg_namespace (then any schema part in the input word
165 : : * will be ignored).
166 : : */
167 : : const char *namespace;
168 : :
169 : : /*
170 : : * Result --- the base object name to return. For example, "c.relname".
171 : : */
172 : : const char *result;
173 : :
174 : : /*
175 : : * In some cases, it's difficult to keep the query from returning the same
176 : : * object multiple times. Specify use_distinct to filter out duplicates.
177 : : */
178 : : bool use_distinct;
179 : :
180 : : /*
181 : : * Additional literal strings (usually keywords) to be offered along with
182 : : * the query results. Provide a NULL-terminated array of constant
183 : : * strings, or NULL if none.
184 : : */
185 : : const char *const *keywords;
186 : :
187 : : /*
188 : : * If this query uses completion_ref_object/completion_ref_schema,
189 : : * populate the remaining fields, else leave them NULL. When using this
190 : : * capability, catname must include the catalog that defines the
191 : : * completion_ref_object, and selcondition must include the join condition
192 : : * that connects it to the result's catalog.
193 : : *
194 : : * refname is the field that should be equated to completion_ref_object,
195 : : * for example "cr.relname".
196 : : */
197 : : const char *refname;
198 : :
199 : : /*
200 : : * Visibility condition to use when completion_ref_schema is not set. For
201 : : * example, "pg_catalog.pg_table_is_visible(cr.oid)". NULL if not needed.
202 : : */
203 : : const char *refviscondition;
204 : :
205 : : /*
206 : : * Name of field to join to pg_namespace.oid when completion_ref_schema is
207 : : * set. For example, "cr.relnamespace". NULL if we don't want to
208 : : * consider completion_ref_schema.
209 : : */
210 : : const char *refnamespace;
211 : : } SchemaQuery;
212 : :
213 : :
214 : : /*
215 : : * Store maximum number of records we want from database queries
216 : : * (implemented via SELECT ... LIMIT xx).
217 : : */
218 : : static int completion_max_records;
219 : :
220 : : /*
221 : : * Communication variables set by psql_completion (mostly in COMPLETE_WITH_FOO
222 : : * macros) and then used by the completion callback functions. Ugly but there
223 : : * is no better way.
224 : : */
225 : : static char completion_last_char; /* last char of input word */
226 : : static const char *completion_charp; /* to pass a string */
227 : : static const char *const *completion_charpp; /* to pass a list of strings */
228 : : static const VersionedQuery *completion_vquery; /* to pass a VersionedQuery */
229 : : static const SchemaQuery *completion_squery; /* to pass a SchemaQuery */
230 : : static char *completion_ref_object; /* name of reference object */
231 : : static char *completion_ref_schema; /* schema name of reference object */
232 : : static bool completion_case_sensitive; /* completion is case sensitive */
233 : : static bool completion_verbatim; /* completion is verbatim */
234 : : static bool completion_force_quote; /* true to force-quote filenames */
235 : :
236 : : /*
237 : : * A few macros to ease typing. You can use these to complete the given
238 : : * string with
239 : : * 1) The result from a query you pass it. (Perhaps one of those below?)
240 : : * We support both simple and versioned queries.
241 : : * 2) The result from a schema query you pass it.
242 : : * We support both simple and versioned schema queries.
243 : : * 3) The items from a null-pointer-terminated list (with or without
244 : : * case-sensitive comparison); if the list is constant you can build it
245 : : * with COMPLETE_WITH() or COMPLETE_WITH_CS(). The QUERY_LIST and
246 : : * QUERY_PLUS forms combine such literal lists with a query result.
247 : : * 4) The list of attributes of the given table (possibly schema-qualified).
248 : : * 5) The list of arguments to the given function (possibly schema-qualified).
249 : : *
250 : : * The query is generally expected to return raw SQL identifiers; matching
251 : : * to what the user typed is done in a quoting-aware fashion. If what is
252 : : * returned is not SQL identifiers, use one of the VERBATIM forms, in which
253 : : * case the query results are matched to the user's text without double-quote
254 : : * processing (so if quoting is needed, you must provide it in the query
255 : : * results).
256 : : */
257 : : #define COMPLETE_WITH_QUERY(query) \
258 : : COMPLETE_WITH_QUERY_LIST(query, NULL)
259 : :
260 : : #define COMPLETE_WITH_QUERY_LIST(query, list) \
261 : : do { \
262 : : completion_charp = query; \
263 : : completion_charpp = list; \
264 : : completion_verbatim = false; \
265 : : matches = rl_completion_matches(text, complete_from_query); \
266 : : } while (0)
267 : :
268 : : #define COMPLETE_WITH_QUERY_PLUS(query, ...) \
269 : : do { \
270 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
271 : : COMPLETE_WITH_QUERY_LIST(query, list); \
272 : : } while (0)
273 : :
274 : : #define COMPLETE_WITH_QUERY_VERBATIM(query) \
275 : : COMPLETE_WITH_QUERY_VERBATIM_LIST(query, NULL)
276 : :
277 : : #define COMPLETE_WITH_QUERY_VERBATIM_LIST(query, list) \
278 : : do { \
279 : : completion_charp = query; \
280 : : completion_charpp = list; \
281 : : completion_verbatim = true; \
282 : : matches = rl_completion_matches(text, complete_from_query); \
283 : : } while (0)
284 : :
285 : : #define COMPLETE_WITH_QUERY_VERBATIM_PLUS(query, ...) \
286 : : do { \
287 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
288 : : COMPLETE_WITH_QUERY_VERBATIM_LIST(query, list); \
289 : : } while (0)
290 : :
291 : : #define COMPLETE_WITH_VERSIONED_QUERY(query) \
292 : : COMPLETE_WITH_VERSIONED_QUERY_LIST(query, NULL)
293 : :
294 : : #define COMPLETE_WITH_VERSIONED_QUERY_LIST(query, list) \
295 : : do { \
296 : : completion_vquery = query; \
297 : : completion_charpp = list; \
298 : : completion_verbatim = false; \
299 : : matches = rl_completion_matches(text, complete_from_versioned_query); \
300 : : } while (0)
301 : :
302 : : #define COMPLETE_WITH_VERSIONED_QUERY_PLUS(query, ...) \
303 : : do { \
304 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
305 : : COMPLETE_WITH_VERSIONED_QUERY_LIST(query, list); \
306 : : } while (0)
307 : :
308 : : #define COMPLETE_WITH_SCHEMA_QUERY(query) \
309 : : COMPLETE_WITH_SCHEMA_QUERY_LIST(query, NULL)
310 : :
311 : : #define COMPLETE_WITH_SCHEMA_QUERY_LIST(query, list) \
312 : : do { \
313 : : completion_squery = &(query); \
314 : : completion_charpp = list; \
315 : : completion_verbatim = false; \
316 : : matches = rl_completion_matches(text, complete_from_schema_query); \
317 : : } while (0)
318 : :
319 : : #define COMPLETE_WITH_SCHEMA_QUERY_PLUS(query, ...) \
320 : : do { \
321 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
322 : : COMPLETE_WITH_SCHEMA_QUERY_LIST(query, list); \
323 : : } while (0)
324 : :
325 : : #define COMPLETE_WITH_SCHEMA_QUERY_VERBATIM(query) \
326 : : do { \
327 : : completion_squery = &(query); \
328 : : completion_charpp = NULL; \
329 : : completion_verbatim = true; \
330 : : matches = rl_completion_matches(text, complete_from_schema_query); \
331 : : } while (0)
332 : :
333 : : #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(query) \
334 : : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, NULL)
335 : :
336 : : #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, list) \
337 : : do { \
338 : : completion_squery = query; \
339 : : completion_charpp = list; \
340 : : completion_verbatim = false; \
341 : : matches = rl_completion_matches(text, complete_from_versioned_schema_query); \
342 : : } while (0)
343 : :
344 : : #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_PLUS(query, ...) \
345 : : do { \
346 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
347 : : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, list); \
348 : : } while (0)
349 : :
350 : : /*
351 : : * Caution: COMPLETE_WITH_CONST is not for general-purpose use; you probably
352 : : * want COMPLETE_WITH() with one element, instead.
353 : : */
354 : : #define COMPLETE_WITH_CONST(cs, con) \
355 : : do { \
356 : : completion_case_sensitive = (cs); \
357 : : completion_charp = (con); \
358 : : matches = rl_completion_matches(text, complete_from_const); \
359 : : } while (0)
360 : :
361 : : #define COMPLETE_WITH_LIST_INT(cs, list) \
362 : : do { \
363 : : completion_case_sensitive = (cs); \
364 : : completion_charpp = (list); \
365 : : matches = rl_completion_matches(text, complete_from_list); \
366 : : } while (0)
367 : :
368 : : #define COMPLETE_WITH_LIST(list) COMPLETE_WITH_LIST_INT(false, list)
369 : : #define COMPLETE_WITH_LIST_CS(list) COMPLETE_WITH_LIST_INT(true, list)
370 : :
371 : : #define COMPLETE_WITH(...) \
372 : : do { \
373 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
374 : : COMPLETE_WITH_LIST(list); \
375 : : } while (0)
376 : :
377 : : #define COMPLETE_WITH_CS(...) \
378 : : do { \
379 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
380 : : COMPLETE_WITH_LIST_CS(list); \
381 : : } while (0)
382 : :
383 : : #define COMPLETE_WITH_ATTR(relation) \
384 : : COMPLETE_WITH_ATTR_LIST(relation, NULL)
385 : :
386 : : #define COMPLETE_WITH_ATTR_LIST(relation, list) \
387 : : do { \
388 : : set_completion_reference(relation); \
389 : : completion_squery = &(Query_for_list_of_attributes); \
390 : : completion_charpp = list; \
391 : : completion_verbatim = false; \
392 : : matches = rl_completion_matches(text, complete_from_schema_query); \
393 : : } while (0)
394 : :
395 : : #define COMPLETE_WITH_ATTR_PLUS(relation, ...) \
396 : : do { \
397 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
398 : : COMPLETE_WITH_ATTR_LIST(relation, list); \
399 : : } while (0)
400 : :
401 : : /*
402 : : * libedit will typically include the literal's leading single quote in
403 : : * "text", while readline will not. Adapt our offered strings to fit.
404 : : * But include a quote if there's not one just before "text", to get the
405 : : * user off to the right start.
406 : : */
407 : : #define COMPLETE_WITH_ENUM_VALUE(type) \
408 : : do { \
409 : : set_completion_reference(type); \
410 : : if (text[0] == '\'' || \
411 : : start == 0 || rl_line_buffer[start - 1] != '\'') \
412 : : completion_squery = &(Query_for_list_of_enum_values_quoted); \
413 : : else \
414 : : completion_squery = &(Query_for_list_of_enum_values_unquoted); \
415 : : completion_charpp = NULL; \
416 : : completion_verbatim = true; \
417 : : matches = rl_completion_matches(text, complete_from_schema_query); \
418 : : } while (0)
419 : :
420 : : /*
421 : : * Timezone completion is mostly like enum label completion, but we work
422 : : * a little harder since this is a more common use-case.
423 : : */
424 : : #define COMPLETE_WITH_TIMEZONE_NAME() \
425 : : do { \
426 : : static const char *const list[] = { "DEFAULT", NULL }; \
427 : : if (text[0] == '\'') \
428 : : completion_charp = Query_for_list_of_timezone_names_quoted_in; \
429 : : else if (start == 0 || rl_line_buffer[start - 1] != '\'') \
430 : : completion_charp = Query_for_list_of_timezone_names_quoted_out; \
431 : : else \
432 : : completion_charp = Query_for_list_of_timezone_names_unquoted; \
433 : : completion_charpp = list; \
434 : : completion_verbatim = true; \
435 : : matches = rl_completion_matches(text, complete_from_query); \
436 : : } while (0)
437 : :
438 : : #define COMPLETE_WITH_FUNCTION_ARG(function) \
439 : : do { \
440 : : set_completion_reference(function); \
441 : : completion_squery = &(Query_for_list_of_arguments); \
442 : : completion_charpp = NULL; \
443 : : completion_verbatim = true; \
444 : : matches = rl_completion_matches(text, complete_from_schema_query); \
445 : : } while (0)
446 : :
447 : : #define COMPLETE_WITH_FILES_LIST(escape, force_quote, list) \
448 : : do { \
449 : : completion_charp = escape; \
450 : : completion_charpp = list; \
451 : : completion_force_quote = force_quote; \
452 : : matches = rl_completion_matches(text, complete_from_files); \
453 : : } while (0)
454 : :
455 : : #define COMPLETE_WITH_FILES(escape, force_quote) \
456 : : COMPLETE_WITH_FILES_LIST(escape, force_quote, NULL)
457 : :
458 : : #define COMPLETE_WITH_FILES_PLUS(escape, force_quote, ...) \
459 : : do { \
460 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
461 : : COMPLETE_WITH_FILES_LIST(escape, force_quote, list); \
462 : : } while (0)
463 : :
464 : : #define COMPLETE_WITH_GENERATOR(generator) \
465 : : matches = rl_completion_matches(text, generator)
466 : :
467 : : /*
468 : : * Assembly instructions for schema queries
469 : : *
470 : : * Note that toast tables are not included in those queries to avoid
471 : : * unnecessary bloat in the completions generated.
472 : : */
473 : :
474 : : static const SchemaQuery Query_for_constraint_of_table = {
475 : : .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_class c1",
476 : : .selcondition = "con.conrelid=c1.oid",
477 : : .result = "con.conname",
478 : : .refname = "c1.relname",
479 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
480 : : .refnamespace = "c1.relnamespace",
481 : : };
482 : :
483 : : static const SchemaQuery Query_for_constraint_of_table_not_validated = {
484 : : .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_class c1",
485 : : .selcondition = "con.conrelid=c1.oid and not con.convalidated",
486 : : .result = "con.conname",
487 : : .refname = "c1.relname",
488 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
489 : : .refnamespace = "c1.relnamespace",
490 : : };
491 : :
492 : : static const SchemaQuery Query_for_constraint_of_type = {
493 : : .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_type t",
494 : : .selcondition = "con.contypid=t.oid",
495 : : .result = "con.conname",
496 : : .refname = "t.typname",
497 : : .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
498 : : .refnamespace = "t.typnamespace",
499 : : };
500 : :
501 : : static const SchemaQuery Query_for_index_of_table = {
502 : : .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i",
503 : : .selcondition = "c1.oid=i.indrelid and i.indexrelid=c2.oid",
504 : : .result = "c2.relname",
505 : : .refname = "c1.relname",
506 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
507 : : .refnamespace = "c1.relnamespace",
508 : : };
509 : :
510 : : static const SchemaQuery Query_for_unique_index_of_table = {
511 : : .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i",
512 : : .selcondition = "c1.oid=i.indrelid and i.indexrelid=c2.oid and i.indisunique",
513 : : .result = "c2.relname",
514 : : .refname = "c1.relname",
515 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
516 : : .refnamespace = "c1.relnamespace",
517 : : };
518 : :
519 : : static const SchemaQuery Query_for_list_of_aggregates[] = {
520 : : {
521 : : .min_server_version = 110000,
522 : : .catname = "pg_catalog.pg_proc p",
523 : : .selcondition = "p.prokind = 'a'",
524 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
525 : : .namespace = "p.pronamespace",
526 : : .result = "p.proname",
527 : : },
528 : : {
529 : : .catname = "pg_catalog.pg_proc p",
530 : : .selcondition = "p.proisagg",
531 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
532 : : .namespace = "p.pronamespace",
533 : : .result = "p.proname",
534 : : }
535 : : };
536 : :
537 : : static const SchemaQuery Query_for_list_of_arguments = {
538 : : .catname = "pg_catalog.pg_proc p",
539 : : .result = "pg_catalog.oidvectortypes(p.proargtypes)||')'",
540 : : .refname = "p.proname",
541 : : .refviscondition = "pg_catalog.pg_function_is_visible(p.oid)",
542 : : .refnamespace = "p.pronamespace",
543 : : };
544 : :
545 : : static const SchemaQuery Query_for_list_of_attributes = {
546 : : .catname = "pg_catalog.pg_attribute a, pg_catalog.pg_class c",
547 : : .selcondition = "c.oid = a.attrelid and a.attnum > 0 and not a.attisdropped",
548 : : .result = "a.attname",
549 : : .refname = "c.relname",
550 : : .refviscondition = "pg_catalog.pg_table_is_visible(c.oid)",
551 : : .refnamespace = "c.relnamespace",
552 : : };
553 : :
554 : : static const SchemaQuery Query_for_list_of_attribute_numbers = {
555 : : .catname = "pg_catalog.pg_attribute a, pg_catalog.pg_class c",
556 : : .selcondition = "c.oid = a.attrelid and a.attnum > 0 and not a.attisdropped",
557 : : .result = "a.attnum::pg_catalog.text",
558 : : .refname = "c.relname",
559 : : .refviscondition = "pg_catalog.pg_table_is_visible(c.oid)",
560 : : .refnamespace = "c.relnamespace",
561 : : };
562 : :
563 : : static const char *const Keywords_for_list_of_datatypes[] = {
564 : : "bigint",
565 : : "boolean",
566 : : "character",
567 : : "double precision",
568 : : "integer",
569 : : "real",
570 : : "smallint",
571 : :
572 : : /*
573 : : * Note: currently there's no value in offering the following multiword
574 : : * type names, because tab completion cannot succeed for them: we can't
575 : : * disambiguate until somewhere in the second word, at which point we
576 : : * won't have the first word as context. ("double precision" does work,
577 : : * as long as no other type name begins with "double".) Leave them out to
578 : : * encourage users to use the PG-specific aliases, which we can complete.
579 : : */
580 : : #ifdef NOT_USED
581 : : "bit varying",
582 : : "character varying",
583 : : "time with time zone",
584 : : "time without time zone",
585 : : "timestamp with time zone",
586 : : "timestamp without time zone",
587 : : #endif
588 : : NULL
589 : : };
590 : :
591 : : static const SchemaQuery Query_for_list_of_datatypes = {
592 : : .catname = "pg_catalog.pg_type t",
593 : : /* selcondition --- ignore table rowtypes and array types */
594 : : .selcondition = "(t.typrelid = 0 "
595 : : " OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
596 : : " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) "
597 : : "AND t.typname !~ '^_'",
598 : : .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
599 : : .namespace = "t.typnamespace",
600 : : .result = "t.typname",
601 : : .keywords = Keywords_for_list_of_datatypes,
602 : : };
603 : :
604 : : static const SchemaQuery Query_for_list_of_composite_datatypes = {
605 : : .catname = "pg_catalog.pg_type t",
606 : : /* selcondition --- only get composite types */
607 : : .selcondition = "(SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
608 : : " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid) "
609 : : "AND t.typname !~ '^_'",
610 : : .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
611 : : .namespace = "t.typnamespace",
612 : : .result = "t.typname",
613 : : };
614 : :
615 : : static const SchemaQuery Query_for_list_of_domains = {
616 : : .catname = "pg_catalog.pg_type t",
617 : : .selcondition = "t.typtype = 'd'",
618 : : .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
619 : : .namespace = "t.typnamespace",
620 : : .result = "t.typname",
621 : : };
622 : :
623 : : static const SchemaQuery Query_for_list_of_enum_values_quoted = {
624 : : .catname = "pg_catalog.pg_enum e, pg_catalog.pg_type t",
625 : : .selcondition = "t.oid = e.enumtypid",
626 : : .result = "pg_catalog.quote_literal(enumlabel)",
627 : : .refname = "t.typname",
628 : : .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
629 : : .refnamespace = "t.typnamespace",
630 : : };
631 : :
632 : : static const SchemaQuery Query_for_list_of_enum_values_unquoted = {
633 : : .catname = "pg_catalog.pg_enum e, pg_catalog.pg_type t",
634 : : .selcondition = "t.oid = e.enumtypid",
635 : : .result = "e.enumlabel",
636 : : .refname = "t.typname",
637 : : .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
638 : : .refnamespace = "t.typnamespace",
639 : : };
640 : :
641 : : /* Note: this intentionally accepts aggregates as well as plain functions */
642 : : static const SchemaQuery Query_for_list_of_functions[] = {
643 : : {
644 : : .min_server_version = 110000,
645 : : .catname = "pg_catalog.pg_proc p",
646 : : .selcondition = "p.prokind != 'p'",
647 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
648 : : .namespace = "p.pronamespace",
649 : : .result = "p.proname",
650 : : },
651 : : {
652 : : .catname = "pg_catalog.pg_proc p",
653 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
654 : : .namespace = "p.pronamespace",
655 : : .result = "p.proname",
656 : : }
657 : : };
658 : :
659 : : static const SchemaQuery Query_for_list_of_procedures[] = {
660 : : {
661 : : .min_server_version = 110000,
662 : : .catname = "pg_catalog.pg_proc p",
663 : : .selcondition = "p.prokind = 'p'",
664 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
665 : : .namespace = "p.pronamespace",
666 : : .result = "p.proname",
667 : : },
668 : : {
669 : : /* not supported in older versions */
670 : : .catname = NULL,
671 : : }
672 : : };
673 : :
674 : : static const SchemaQuery Query_for_list_of_routines = {
675 : : .catname = "pg_catalog.pg_proc p",
676 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
677 : : .namespace = "p.pronamespace",
678 : : .result = "p.proname",
679 : : };
680 : :
681 : : static const SchemaQuery Query_for_list_of_sequences = {
682 : : .catname = "pg_catalog.pg_class c",
683 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_SEQUENCE) ")",
684 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
685 : : .namespace = "c.relnamespace",
686 : : .result = "c.relname",
687 : : };
688 : :
689 : : static const SchemaQuery Query_for_list_of_foreign_tables = {
690 : : .catname = "pg_catalog.pg_class c",
691 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_FOREIGN_TABLE) ")",
692 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
693 : : .namespace = "c.relnamespace",
694 : : .result = "c.relname",
695 : : };
696 : :
697 : : static const SchemaQuery Query_for_list_of_tables = {
698 : : .catname = "pg_catalog.pg_class c",
699 : : .selcondition =
700 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
701 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
702 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
703 : : .namespace = "c.relnamespace",
704 : : .result = "c.relname",
705 : : };
706 : :
707 : : static const SchemaQuery Query_for_list_of_partitioned_tables = {
708 : : .catname = "pg_catalog.pg_class c",
709 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
710 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
711 : : .namespace = "c.relnamespace",
712 : : .result = "c.relname",
713 : : };
714 : :
715 : : static const SchemaQuery Query_for_list_of_tables_for_constraint = {
716 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_constraint con",
717 : : .selcondition = "c.oid=con.conrelid and c.relkind IN ("
718 : : CppAsString2(RELKIND_RELATION) ", "
719 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
720 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
721 : : .namespace = "c.relnamespace",
722 : : .result = "c.relname",
723 : : .use_distinct = true,
724 : : .refname = "con.conname",
725 : : };
726 : :
727 : : static const SchemaQuery Query_for_list_of_tables_for_policy = {
728 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_policy p",
729 : : .selcondition = "c.oid=p.polrelid",
730 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
731 : : .namespace = "c.relnamespace",
732 : : .result = "c.relname",
733 : : .use_distinct = true,
734 : : .refname = "p.polname",
735 : : };
736 : :
737 : : static const SchemaQuery Query_for_list_of_tables_for_rule = {
738 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_rewrite r",
739 : : .selcondition = "c.oid=r.ev_class",
740 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
741 : : .namespace = "c.relnamespace",
742 : : .result = "c.relname",
743 : : .use_distinct = true,
744 : : .refname = "r.rulename",
745 : : };
746 : :
747 : : static const SchemaQuery Query_for_list_of_tables_for_trigger = {
748 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_trigger t",
749 : : .selcondition = "c.oid=t.tgrelid",
750 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
751 : : .namespace = "c.relnamespace",
752 : : .result = "c.relname",
753 : : .use_distinct = true,
754 : : .refname = "t.tgname",
755 : : };
756 : :
757 : : static const SchemaQuery Query_for_list_of_ts_configurations = {
758 : : .catname = "pg_catalog.pg_ts_config c",
759 : : .viscondition = "pg_catalog.pg_ts_config_is_visible(c.oid)",
760 : : .namespace = "c.cfgnamespace",
761 : : .result = "c.cfgname",
762 : : };
763 : :
764 : : static const SchemaQuery Query_for_list_of_ts_dictionaries = {
765 : : .catname = "pg_catalog.pg_ts_dict d",
766 : : .viscondition = "pg_catalog.pg_ts_dict_is_visible(d.oid)",
767 : : .namespace = "d.dictnamespace",
768 : : .result = "d.dictname",
769 : : };
770 : :
771 : : static const SchemaQuery Query_for_list_of_ts_parsers = {
772 : : .catname = "pg_catalog.pg_ts_parser p",
773 : : .viscondition = "pg_catalog.pg_ts_parser_is_visible(p.oid)",
774 : : .namespace = "p.prsnamespace",
775 : : .result = "p.prsname",
776 : : };
777 : :
778 : : static const SchemaQuery Query_for_list_of_ts_templates = {
779 : : .catname = "pg_catalog.pg_ts_template t",
780 : : .viscondition = "pg_catalog.pg_ts_template_is_visible(t.oid)",
781 : : .namespace = "t.tmplnamespace",
782 : : .result = "t.tmplname",
783 : : };
784 : :
785 : : static const SchemaQuery Query_for_list_of_views = {
786 : : .catname = "pg_catalog.pg_class c",
787 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")",
788 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
789 : : .namespace = "c.relnamespace",
790 : : .result = "c.relname",
791 : : };
792 : :
793 : : static const SchemaQuery Query_for_list_of_matviews = {
794 : : .catname = "pg_catalog.pg_class c",
795 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")",
796 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
797 : : .namespace = "c.relnamespace",
798 : : .result = "c.relname",
799 : : };
800 : :
801 : : static const SchemaQuery Query_for_list_of_indexes = {
802 : : .catname = "pg_catalog.pg_class c",
803 : : .selcondition =
804 : : "c.relkind IN (" CppAsString2(RELKIND_INDEX) ", "
805 : : CppAsString2(RELKIND_PARTITIONED_INDEX) ")",
806 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
807 : : .namespace = "c.relnamespace",
808 : : .result = "c.relname",
809 : : };
810 : :
811 : : static const SchemaQuery Query_for_list_of_partitioned_indexes = {
812 : : .catname = "pg_catalog.pg_class c",
813 : : .selcondition = "c.relkind = " CppAsString2(RELKIND_PARTITIONED_INDEX),
814 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
815 : : .namespace = "c.relnamespace",
816 : : .result = "c.relname",
817 : : };
818 : :
819 : : static const SchemaQuery Query_for_list_of_propgraphs = {
820 : : .catname = "pg_catalog.pg_class c",
821 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_PROPGRAPH) ")",
822 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
823 : : .namespace = "c.relnamespace",
824 : : .result = "pg_catalog.quote_ident(c.relname)",
825 : : };
826 : :
827 : :
828 : : /* All relations */
829 : : static const SchemaQuery Query_for_list_of_relations = {
830 : : .catname = "pg_catalog.pg_class c",
831 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
832 : : .namespace = "c.relnamespace",
833 : : .result = "c.relname",
834 : : };
835 : :
836 : : /* partitioned relations */
837 : : static const SchemaQuery Query_for_list_of_partitioned_relations = {
838 : : .catname = "pg_catalog.pg_class c",
839 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE)
840 : : ", " CppAsString2(RELKIND_PARTITIONED_INDEX) ")",
841 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
842 : : .namespace = "c.relnamespace",
843 : : .result = "c.relname",
844 : : };
845 : :
846 : : static const SchemaQuery Query_for_list_of_operator_families = {
847 : : .catname = "pg_catalog.pg_opfamily c",
848 : : .viscondition = "pg_catalog.pg_opfamily_is_visible(c.oid)",
849 : : .namespace = "c.opfnamespace",
850 : : .result = "c.opfname",
851 : : };
852 : :
853 : : /* Relations supporting INSERT, UPDATE or DELETE */
854 : : static const SchemaQuery Query_for_list_of_updatables = {
855 : : .catname = "pg_catalog.pg_class c",
856 : : .selcondition =
857 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
858 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
859 : : CppAsString2(RELKIND_VIEW) ", "
860 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
861 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
862 : : .namespace = "c.relnamespace",
863 : : .result = "c.relname",
864 : : };
865 : :
866 : : /* Relations supporting MERGE */
867 : : static const SchemaQuery Query_for_list_of_mergetargets = {
868 : : .catname = "pg_catalog.pg_class c",
869 : : .selcondition =
870 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
871 : : CppAsString2(RELKIND_VIEW) ", "
872 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ") ",
873 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
874 : : .namespace = "c.relnamespace",
875 : : .result = "c.relname",
876 : : };
877 : :
878 : : /* Relations supporting SELECT */
879 : : static const SchemaQuery Query_for_list_of_selectables = {
880 : : .catname = "pg_catalog.pg_class c",
881 : : .selcondition =
882 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
883 : : CppAsString2(RELKIND_SEQUENCE) ", "
884 : : CppAsString2(RELKIND_VIEW) ", "
885 : : CppAsString2(RELKIND_MATVIEW) ", "
886 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
887 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
888 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
889 : : .namespace = "c.relnamespace",
890 : : .result = "c.relname",
891 : : };
892 : :
893 : : /* Relations supporting TRUNCATE */
894 : : static const SchemaQuery Query_for_list_of_truncatables = {
895 : : .catname = "pg_catalog.pg_class c",
896 : : .selcondition =
897 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
898 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
899 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
900 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
901 : : .namespace = "c.relnamespace",
902 : : .result = "c.relname",
903 : : };
904 : :
905 : : /* Relations supporting GRANT are currently same as those supporting SELECT */
906 : : #define Query_for_list_of_grantables Query_for_list_of_selectables
907 : :
908 : : /* Relations supporting ANALYZE */
909 : : static const SchemaQuery Query_for_list_of_analyzables = {
910 : : .catname = "pg_catalog.pg_class c",
911 : : .selcondition =
912 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
913 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
914 : : CppAsString2(RELKIND_MATVIEW) ", "
915 : : CppAsString2(RELKIND_FOREIGN_TABLE) ")",
916 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
917 : : .namespace = "c.relnamespace",
918 : : .result = "c.relname",
919 : : };
920 : :
921 : : /*
922 : : * Relations supporting COPY TO/FROM are currently almost the same as
923 : : * those supporting ANALYZE. Although views with INSTEAD OF INSERT triggers
924 : : * can be used with COPY FROM, they are rarely used for this purpose,
925 : : * so plain views are intentionally excluded from this tab completion.
926 : : */
927 : : #define Query_for_list_of_tables_for_copy Query_for_list_of_analyzables
928 : :
929 : : /* Relations supporting index creation */
930 : : static const SchemaQuery Query_for_list_of_indexables = {
931 : : .catname = "pg_catalog.pg_class c",
932 : : .selcondition =
933 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
934 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
935 : : CppAsString2(RELKIND_MATVIEW) ")",
936 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
937 : : .namespace = "c.relnamespace",
938 : : .result = "c.relname",
939 : : };
940 : :
941 : : /*
942 : : * Relations supporting VACUUM are currently same as those supporting
943 : : * indexing.
944 : : */
945 : : #define Query_for_list_of_vacuumables Query_for_list_of_indexables
946 : :
947 : : /* Relations supporting CLUSTER */
948 : : static const SchemaQuery Query_for_list_of_clusterables = {
949 : : .catname = "pg_catalog.pg_class c",
950 : : .selcondition =
951 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
952 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
953 : : CppAsString2(RELKIND_MATVIEW) ")",
954 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
955 : : .namespace = "c.relnamespace",
956 : : .result = "c.relname",
957 : : };
958 : :
959 : : static const SchemaQuery Query_for_list_of_constraints_with_schema = {
960 : : .catname = "pg_catalog.pg_constraint c",
961 : : .selcondition = "c.conrelid <> 0",
962 : : .namespace = "c.connamespace",
963 : : .result = "c.conname",
964 : : };
965 : :
966 : : static const SchemaQuery Query_for_list_of_statistics = {
967 : : .catname = "pg_catalog.pg_statistic_ext s",
968 : : .viscondition = "pg_catalog.pg_statistics_obj_is_visible(s.oid)",
969 : : .namespace = "s.stxnamespace",
970 : : .result = "s.stxname",
971 : : };
972 : :
973 : : static const SchemaQuery Query_for_list_of_collations = {
974 : : .catname = "pg_catalog.pg_collation c",
975 : : .selcondition = "c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))",
976 : : .viscondition = "pg_catalog.pg_collation_is_visible(c.oid)",
977 : : .namespace = "c.collnamespace",
978 : : .result = "c.collname",
979 : : };
980 : :
981 : : static const SchemaQuery Query_for_partition_of_table = {
982 : : .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_inherits i",
983 : : .selcondition = "c1.oid=i.inhparent and i.inhrelid=c2.oid and c2.relispartition",
984 : : .viscondition = "pg_catalog.pg_table_is_visible(c2.oid)",
985 : : .namespace = "c2.relnamespace",
986 : : .result = "c2.relname",
987 : : .refname = "c1.relname",
988 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
989 : : .refnamespace = "c1.relnamespace",
990 : : };
991 : :
992 : : static const SchemaQuery Query_for_rule_of_table = {
993 : : .catname = "pg_catalog.pg_rewrite r, pg_catalog.pg_class c1",
994 : : .selcondition = "r.ev_class=c1.oid",
995 : : .result = "r.rulename",
996 : : .refname = "c1.relname",
997 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
998 : : .refnamespace = "c1.relnamespace",
999 : : };
1000 : :
1001 : : static const SchemaQuery Query_for_trigger_of_table = {
1002 : : .catname = "pg_catalog.pg_trigger t, pg_catalog.pg_class c1",
1003 : : .selcondition = "t.tgrelid=c1.oid and not t.tgisinternal",
1004 : : .result = "t.tgname",
1005 : : .refname = "c1.relname",
1006 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
1007 : : .refnamespace = "c1.relnamespace",
1008 : : };
1009 : :
1010 : :
1011 : : /*
1012 : : * Queries to get lists of names of various kinds of things, possibly
1013 : : * restricted to names matching a partially entered name. Don't use
1014 : : * this method where the user might wish to enter a schema-qualified
1015 : : * name; make a SchemaQuery instead.
1016 : : *
1017 : : * In these queries, there must be a restriction clause of the form
1018 : : * output LIKE '%s'
1019 : : * where "output" is the same string that the query returns. The %s
1020 : : * will be replaced by a LIKE pattern to match the already-typed text.
1021 : : *
1022 : : * There can be a second '%s', which will be replaced by a suitably-escaped
1023 : : * version of the string provided in completion_ref_object. If there is a
1024 : : * third '%s', it will be replaced by a suitably-escaped version of the string
1025 : : * provided in completion_ref_schema. NOTE: using completion_ref_object
1026 : : * that way is usually the wrong thing, and using completion_ref_schema
1027 : : * that way is always the wrong thing. Make a SchemaQuery instead.
1028 : : */
1029 : :
1030 : : #define Query_for_list_of_template_databases \
1031 : : "SELECT d.datname "\
1032 : : " FROM pg_catalog.pg_database d "\
1033 : : " WHERE d.datname LIKE '%s' "\
1034 : : " AND (d.datistemplate OR pg_catalog.pg_has_role(d.datdba, 'USAGE'))"
1035 : :
1036 : : #define Query_for_list_of_databases \
1037 : : "SELECT datname FROM pg_catalog.pg_database "\
1038 : : " WHERE datname LIKE '%s'"
1039 : :
1040 : : #define Query_for_list_of_database_vars \
1041 : : "SELECT conf FROM ("\
1042 : : " SELECT setdatabase, pg_catalog.split_part(pg_catalog.unnest(setconfig),'=',1) conf"\
1043 : : " FROM pg_db_role_setting "\
1044 : : " ) s, pg_database d "\
1045 : : " WHERE s.setdatabase = d.oid "\
1046 : : " AND conf LIKE '%s'"\
1047 : : " AND d.datname LIKE '%s'"
1048 : :
1049 : : #define Query_for_list_of_tablespaces \
1050 : : "SELECT spcname FROM pg_catalog.pg_tablespace "\
1051 : : " WHERE spcname LIKE '%s'"
1052 : :
1053 : : #define Query_for_list_of_encodings \
1054 : : " SELECT DISTINCT pg_catalog.pg_encoding_to_char(conforencoding) "\
1055 : : " FROM pg_catalog.pg_conversion "\
1056 : : " WHERE pg_catalog.pg_encoding_to_char(conforencoding) LIKE pg_catalog.upper('%s')"
1057 : :
1058 : : #define Query_for_list_of_languages \
1059 : : "SELECT lanname "\
1060 : : " FROM pg_catalog.pg_language "\
1061 : : " WHERE lanname != 'internal' "\
1062 : : " AND lanname LIKE '%s'"
1063 : :
1064 : : #define Query_for_list_of_schemas \
1065 : : "SELECT nspname FROM pg_catalog.pg_namespace "\
1066 : : " WHERE nspname LIKE '%s'"
1067 : :
1068 : : /* Use COMPLETE_WITH_QUERY_VERBATIM with these queries for GUC names: */
1069 : : #define Query_for_list_of_alter_system_set_vars \
1070 : : "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1071 : : " WHERE context != 'internal' "\
1072 : : " AND pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1073 : :
1074 : : #define Query_for_list_of_set_vars \
1075 : : "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1076 : : " WHERE context IN ('user', 'superuser') "\
1077 : : " AND pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1078 : :
1079 : : #define Query_for_list_of_show_vars \
1080 : : "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1081 : : " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1082 : :
1083 : : #define Query_for_list_of_roles \
1084 : : " SELECT rolname "\
1085 : : " FROM pg_catalog.pg_roles "\
1086 : : " WHERE rolname LIKE '%s'"
1087 : :
1088 : : /* add these to Query_for_list_of_roles in OWNER contexts */
1089 : : #define Keywords_for_list_of_owner_roles \
1090 : : "CURRENT_ROLE", "CURRENT_USER", "SESSION_USER"
1091 : :
1092 : : /* add these to Query_for_list_of_roles in GRANT contexts */
1093 : : #define Keywords_for_list_of_grant_roles \
1094 : : Keywords_for_list_of_owner_roles, "PUBLIC"
1095 : :
1096 : : #define Query_for_all_table_constraints \
1097 : : "SELECT conname "\
1098 : : " FROM pg_catalog.pg_constraint c "\
1099 : : " WHERE c.conrelid <> 0 "\
1100 : : " and conname LIKE '%s'"
1101 : :
1102 : : #define Query_for_list_of_fdws \
1103 : : " SELECT fdwname "\
1104 : : " FROM pg_catalog.pg_foreign_data_wrapper "\
1105 : : " WHERE fdwname LIKE '%s'"
1106 : :
1107 : : #define Query_for_list_of_servers \
1108 : : " SELECT srvname "\
1109 : : " FROM pg_catalog.pg_foreign_server "\
1110 : : " WHERE srvname LIKE '%s'"
1111 : :
1112 : : #define Query_for_list_of_user_mappings \
1113 : : " SELECT usename "\
1114 : : " FROM pg_catalog.pg_user_mappings "\
1115 : : " WHERE usename LIKE '%s'"
1116 : :
1117 : : #define Query_for_list_of_user_vars \
1118 : : "SELECT conf FROM ("\
1119 : : " SELECT rolname, pg_catalog.split_part(pg_catalog.unnest(rolconfig),'=',1) conf"\
1120 : : " FROM pg_catalog.pg_roles"\
1121 : : " ) s"\
1122 : : " WHERE s.conf like '%s' "\
1123 : : " AND s.rolname LIKE '%s'"
1124 : :
1125 : : #define Query_for_list_of_access_methods \
1126 : : " SELECT amname "\
1127 : : " FROM pg_catalog.pg_am "\
1128 : : " WHERE amname LIKE '%s'"
1129 : :
1130 : : #define Query_for_list_of_index_access_methods \
1131 : : " SELECT amname "\
1132 : : " FROM pg_catalog.pg_am "\
1133 : : " WHERE amname LIKE '%s' AND "\
1134 : : " amtype=" CppAsString2(AMTYPE_INDEX)
1135 : :
1136 : : #define Query_for_list_of_table_access_methods \
1137 : : " SELECT amname "\
1138 : : " FROM pg_catalog.pg_am "\
1139 : : " WHERE amname LIKE '%s' AND "\
1140 : : " amtype=" CppAsString2(AMTYPE_TABLE)
1141 : :
1142 : : #define Query_for_list_of_extensions \
1143 : : " SELECT extname "\
1144 : : " FROM pg_catalog.pg_extension "\
1145 : : " WHERE extname LIKE '%s'"
1146 : :
1147 : : #define Query_for_list_of_available_extensions \
1148 : : " SELECT name "\
1149 : : " FROM pg_catalog.pg_available_extensions "\
1150 : : " WHERE name LIKE '%s' AND installed_version IS NULL"
1151 : :
1152 : : #define Query_for_list_of_available_extension_versions \
1153 : : " SELECT version "\
1154 : : " FROM pg_catalog.pg_available_extension_versions "\
1155 : : " WHERE version LIKE '%s' AND name='%s'"
1156 : :
1157 : : #define Query_for_list_of_prepared_statements \
1158 : : " SELECT name "\
1159 : : " FROM pg_catalog.pg_prepared_statements "\
1160 : : " WHERE name LIKE '%s'"
1161 : :
1162 : : #define Query_for_list_of_event_triggers \
1163 : : " SELECT evtname "\
1164 : : " FROM pg_catalog.pg_event_trigger "\
1165 : : " WHERE evtname LIKE '%s'"
1166 : :
1167 : : #define Query_for_list_of_tablesample_methods \
1168 : : " SELECT proname "\
1169 : : " FROM pg_catalog.pg_proc "\
1170 : : " WHERE prorettype = 'pg_catalog.tsm_handler'::pg_catalog.regtype AND "\
1171 : : " proargtypes[0] = 'pg_catalog.internal'::pg_catalog.regtype AND "\
1172 : : " proname LIKE '%s'"
1173 : :
1174 : : #define Query_for_list_of_policies \
1175 : : " SELECT polname "\
1176 : : " FROM pg_catalog.pg_policy "\
1177 : : " WHERE polname LIKE '%s'"
1178 : :
1179 : : #define Query_for_values_of_enum_GUC \
1180 : : " SELECT val FROM ( "\
1181 : : " SELECT name, pg_catalog.unnest(enumvals) AS val "\
1182 : : " FROM pg_catalog.pg_settings "\
1183 : : " ) ss "\
1184 : : " WHERE val LIKE '%s'"\
1185 : : " and pg_catalog.lower(name)=pg_catalog.lower('%s')"
1186 : :
1187 : : #define Query_for_list_of_channels \
1188 : : " SELECT channel "\
1189 : : " FROM pg_catalog.pg_listening_channels() AS channel "\
1190 : : " WHERE channel LIKE '%s'"
1191 : :
1192 : : #define Query_for_list_of_cursors \
1193 : : " SELECT name "\
1194 : : " FROM pg_catalog.pg_cursors "\
1195 : : " WHERE name LIKE '%s'"
1196 : :
1197 : : #define Query_for_list_of_timezone_names_unquoted \
1198 : : " SELECT name "\
1199 : : " FROM pg_catalog.pg_timezone_names() "\
1200 : : " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1201 : :
1202 : : #define Query_for_list_of_timezone_names_quoted_out \
1203 : : "SELECT pg_catalog.quote_literal(name) AS name "\
1204 : : " FROM pg_catalog.pg_timezone_names() "\
1205 : : " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1206 : :
1207 : : #define Query_for_list_of_timezone_names_quoted_in \
1208 : : "SELECT pg_catalog.quote_literal(name) AS name "\
1209 : : " FROM pg_catalog.pg_timezone_names() "\
1210 : : " WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.lower('%s')"
1211 : :
1212 : : /* Privilege options shared between GRANT and REVOKE */
1213 : : #define Privilege_options_of_grant_and_revoke \
1214 : : "SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", \
1215 : : "CREATE", "CONNECT", "TEMPORARY", "EXECUTE", "USAGE", "SET", "ALTER SYSTEM", \
1216 : : "MAINTAIN", "ALL"
1217 : :
1218 : : /* ALTER PROCEDURE options */
1219 : : #define Alter_procedure_options \
1220 : : "DEPENDS ON EXTENSION", "EXTERNAL SECURITY", "NO DEPENDS ON EXTENSION", \
1221 : : "OWNER TO", "RENAME TO", "RESET", "SECURITY", "SET"
1222 : :
1223 : : /* ALTER ROUTINE options */
1224 : : #define Alter_routine_options \
1225 : : Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
1226 : : "PARALLEL", "ROWS", "STABLE", "VOLATILE"
1227 : :
1228 : : /* ALTER FUNCTION options */
1229 : : #define Alter_function_options \
1230 : : Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
1231 : : "STRICT", "SUPPORT"
1232 : :
1233 : : /* COPY options shared between FROM and TO */
1234 : : #define Copy_common_options \
1235 : : "DELIMITER", "ENCODING", "ESCAPE", "FORMAT", "HEADER", "NULL", "QUOTE"
1236 : :
1237 : : /* COPY FROM options */
1238 : : #define Copy_from_options \
1239 : : Copy_common_options, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
1240 : : "LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"
1241 : :
1242 : : /* COPY TO options */
1243 : : #define Copy_to_options \
1244 : : Copy_common_options, "FORCE_QUOTE", "FORCE_ARRAY"
1245 : :
1246 : : /*
1247 : : * These object types were introduced later than our support cutoff of
1248 : : * server version 9.2. We use the VersionedQuery infrastructure so that
1249 : : * we don't send certain-to-fail queries to older servers.
1250 : : */
1251 : :
1252 : : static const VersionedQuery Query_for_list_of_publications[] = {
1253 : : {100000,
1254 : : " SELECT pubname "
1255 : : " FROM pg_catalog.pg_publication "
1256 : : " WHERE pubname LIKE '%s'"
1257 : : },
1258 : : {0, NULL}
1259 : : };
1260 : :
1261 : : static const VersionedQuery Query_for_list_of_subscriptions[] = {
1262 : : {100000,
1263 : : " SELECT s.subname "
1264 : : " FROM pg_catalog.pg_subscription s, pg_catalog.pg_database d "
1265 : : " WHERE s.subname LIKE '%s' "
1266 : : " AND d.datname = pg_catalog.current_database() "
1267 : : " AND s.subdbid = d.oid"
1268 : : },
1269 : : {0, NULL}
1270 : : };
1271 : :
1272 : : /* Known command-starting keywords. */
1273 : : static const char *const sql_commands[] = {
1274 : : "ABORT", "ALTER", "ANALYZE", "BEGIN", "CALL", "CHECKPOINT", "CLOSE", "CLUSTER",
1275 : : "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
1276 : : "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN",
1277 : : "FETCH", "GRANT", "IMPORT FOREIGN SCHEMA", "INSERT INTO", "LISTEN", "LOAD", "LOCK",
1278 : : "MERGE INTO", "MOVE", "NOTIFY", "PREPARE",
1279 : : "REASSIGN", "REFRESH MATERIALIZED VIEW", "REINDEX", "RELEASE", "REPACK",
1280 : : "RESET", "REVOKE", "ROLLBACK",
1281 : : "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START",
1282 : : "TABLE", "TRUNCATE", "UNLISTEN", "UPDATE", "VACUUM", "VALUES",
1283 : : "WAIT FOR", "WITH",
1284 : : NULL
1285 : : };
1286 : :
1287 : : /*
1288 : : * This is a list of all "things" in Pgsql, which can show up after CREATE or
1289 : : * DROP; and there is also a query to get a list of them.
1290 : : */
1291 : :
1292 : : typedef struct
1293 : : {
1294 : : const char *name;
1295 : : /* Provide at most one of these three types of query: */
1296 : : const char *query; /* simple query, or NULL */
1297 : : const VersionedQuery *vquery; /* versioned query, or NULL */
1298 : : const SchemaQuery *squery; /* schema query, or NULL */
1299 : : const char *const *keywords; /* keywords to be offered as well */
1300 : : const uint32 flags; /* visibility flags, see below */
1301 : : } pgsql_thing_t;
1302 : :
1303 : : #define THING_NO_CREATE (1 << 0) /* should not show up after CREATE */
1304 : : #define THING_NO_DROP (1 << 1) /* should not show up after DROP */
1305 : : #define THING_NO_ALTER (1 << 2) /* should not show up after ALTER */
1306 : : #define THING_NO_SHOW (THING_NO_CREATE | THING_NO_DROP | THING_NO_ALTER)
1307 : :
1308 : : /* When we have DROP USER etc, also offer MAPPING FOR */
1309 : : static const char *const Keywords_for_user_thing[] = {
1310 : : "MAPPING FOR",
1311 : : NULL
1312 : : };
1313 : :
1314 : : static const pgsql_thing_t words_after_create[] = {
1315 : : {"ACCESS METHOD", NULL, NULL, NULL, NULL, THING_NO_ALTER},
1316 : : {"AGGREGATE", NULL, NULL, Query_for_list_of_aggregates},
1317 : : {"CAST", NULL, NULL, NULL}, /* Casts have complex structures for names, so
1318 : : * skip it */
1319 : : {"COLLATION", NULL, NULL, &Query_for_list_of_collations},
1320 : :
1321 : : /*
1322 : : * CREATE CONSTRAINT TRIGGER is not supported here because it is designed
1323 : : * to be used only by pg_dump.
1324 : : */
1325 : : {"CONFIGURATION", NULL, NULL, &Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW},
1326 : : {"CONVERSION", "SELECT conname FROM pg_catalog.pg_conversion WHERE conname LIKE '%s'"},
1327 : : {"DATABASE", Query_for_list_of_databases},
1328 : : {"DEFAULT PRIVILEGES", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1329 : : {"DICTIONARY", NULL, NULL, &Query_for_list_of_ts_dictionaries, NULL, THING_NO_SHOW},
1330 : : {"DOMAIN", NULL, NULL, &Query_for_list_of_domains},
1331 : : {"EVENT TRIGGER", NULL, NULL, NULL},
1332 : : {"EXTENSION", Query_for_list_of_extensions},
1333 : : {"FOREIGN DATA WRAPPER", NULL, NULL, NULL},
1334 : : {"FOREIGN TABLE", NULL, NULL, NULL},
1335 : : {"FUNCTION", NULL, NULL, Query_for_list_of_functions},
1336 : : {"GROUP", Query_for_list_of_roles},
1337 : : {"INDEX", NULL, NULL, &Query_for_list_of_indexes},
1338 : : {"LANGUAGE", Query_for_list_of_languages},
1339 : : {"LARGE OBJECT", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1340 : : {"MATERIALIZED VIEW", NULL, NULL, &Query_for_list_of_matviews},
1341 : : {"OPERATOR", NULL, NULL, NULL}, /* Querying for this is probably not such
1342 : : * a good idea. */
1343 : : {"OR REPLACE", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER},
1344 : : {"OWNED", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_ALTER}, /* for DROP OWNED BY ... */
1345 : : {"PARSER", NULL, NULL, &Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW},
1346 : : {"POLICY", NULL, NULL, NULL},
1347 : : {"PROCEDURE", NULL, NULL, Query_for_list_of_procedures},
1348 : : {"PROPERTY GRAPH", NULL, NULL, &Query_for_list_of_propgraphs},
1349 : : {"PUBLICATION", NULL, Query_for_list_of_publications},
1350 : : {"ROLE", Query_for_list_of_roles},
1351 : : {"ROUTINE", NULL, NULL, &Query_for_list_of_routines, NULL, THING_NO_CREATE},
1352 : : {"RULE", "SELECT rulename FROM pg_catalog.pg_rules WHERE rulename LIKE '%s'"},
1353 : : {"SCHEMA", Query_for_list_of_schemas},
1354 : : {"SEQUENCE", NULL, NULL, &Query_for_list_of_sequences},
1355 : : {"SERVER", Query_for_list_of_servers},
1356 : : {"STATISTICS", NULL, NULL, &Query_for_list_of_statistics},
1357 : : {"SUBSCRIPTION", NULL, Query_for_list_of_subscriptions},
1358 : : {"SYSTEM", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1359 : : {"TABLE", NULL, NULL, &Query_for_list_of_tables},
1360 : : {"TABLESPACE", Query_for_list_of_tablespaces},
1361 : : {"TEMP", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMP TABLE
1362 : : * ... */
1363 : : {"TEMPLATE", NULL, NULL, &Query_for_list_of_ts_templates, NULL, THING_NO_SHOW},
1364 : : {"TEMPORARY", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMPORARY
1365 : : * TABLE ... */
1366 : : {"TEXT SEARCH", NULL, NULL, NULL},
1367 : : {"TRANSFORM", NULL, NULL, NULL, NULL, THING_NO_ALTER},
1368 : : {"TRIGGER", "SELECT tgname FROM pg_catalog.pg_trigger WHERE tgname LIKE '%s' AND NOT tgisinternal"},
1369 : : {"TYPE", NULL, NULL, &Query_for_list_of_datatypes},
1370 : : {"UNIQUE", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNIQUE
1371 : : * INDEX ... */
1372 : : {"UNLOGGED", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNLOGGED
1373 : : * TABLE ... */
1374 : : {"USER", Query_for_list_of_roles, NULL, NULL, Keywords_for_user_thing},
1375 : : {"USER MAPPING FOR", NULL, NULL, NULL},
1376 : : {"VIEW", NULL, NULL, &Query_for_list_of_views},
1377 : : {NULL} /* end of list */
1378 : : };
1379 : :
1380 : : /*
1381 : : * The tcpatterns[] table provides the initial pattern-match rule for each
1382 : : * switch case in match_previous_words(). The contents of the table
1383 : : * are constructed by gen_tabcomplete.pl.
1384 : : */
1385 : :
1386 : : /* Basic match rules appearing in tcpatterns[].kind */
1387 : : enum TCPatternKind
1388 : : {
1389 : : Match,
1390 : : MatchCS,
1391 : : HeadMatch,
1392 : : HeadMatchCS,
1393 : : TailMatch,
1394 : : TailMatchCS,
1395 : : };
1396 : :
1397 : : /* Things besides string literals that can appear in tcpatterns[].words */
1398 : : #define MatchAny NULL
1399 : : #define MatchAnyExcept(pattern) ("!" pattern)
1400 : : #define MatchAnyN ""
1401 : :
1402 : : /* One entry in tcpatterns[] */
1403 : : typedef struct
1404 : : {
1405 : : int id; /* case label used in match_previous_words */
1406 : : enum TCPatternKind kind; /* match kind, see above */
1407 : : int nwords; /* length of words[] array */
1408 : : const char *const *words; /* array of match words */
1409 : : } TCPattern;
1410 : :
1411 : : /* Macro emitted by gen_tabcomplete.pl to fill a tcpatterns[] entry */
1412 : : #define TCPAT(id, kind, ...) \
1413 : : { (id), (kind), VA_ARGS_NARGS(__VA_ARGS__), \
1414 : : (const char * const []) { __VA_ARGS__ } }
1415 : :
1416 : : #ifdef SWITCH_CONVERSION_APPLIED
1417 : :
1418 : : static const TCPattern tcpatterns[] =
1419 : : {
1420 : : /* Insert tab-completion pattern data here. */
1421 : : };
1422 : :
1423 : : #endif /* SWITCH_CONVERSION_APPLIED */
1424 : :
1425 : : /* Storage parameters for CREATE TABLE and ALTER TABLE */
1426 : : static const char *const table_storage_parameters[] = {
1427 : : "autovacuum_analyze_scale_factor",
1428 : : "autovacuum_analyze_threshold",
1429 : : "autovacuum_enabled",
1430 : : "autovacuum_freeze_max_age",
1431 : : "autovacuum_freeze_min_age",
1432 : : "autovacuum_freeze_table_age",
1433 : : "autovacuum_multixact_freeze_max_age",
1434 : : "autovacuum_multixact_freeze_min_age",
1435 : : "autovacuum_multixact_freeze_table_age",
1436 : : "autovacuum_parallel_workers",
1437 : : "autovacuum_vacuum_cost_delay",
1438 : : "autovacuum_vacuum_cost_limit",
1439 : : "autovacuum_vacuum_insert_scale_factor",
1440 : : "autovacuum_vacuum_insert_threshold",
1441 : : "autovacuum_vacuum_max_threshold",
1442 : : "autovacuum_vacuum_scale_factor",
1443 : : "autovacuum_vacuum_threshold",
1444 : : "fillfactor",
1445 : : "log_autovacuum_min_duration",
1446 : : "log_autoanalyze_min_duration",
1447 : : "parallel_workers",
1448 : : "toast.autovacuum_enabled",
1449 : : "toast.autovacuum_freeze_max_age",
1450 : : "toast.autovacuum_freeze_min_age",
1451 : : "toast.autovacuum_freeze_table_age",
1452 : : "toast.autovacuum_multixact_freeze_max_age",
1453 : : "toast.autovacuum_multixact_freeze_min_age",
1454 : : "toast.autovacuum_multixact_freeze_table_age",
1455 : : "toast.autovacuum_vacuum_cost_delay",
1456 : : "toast.autovacuum_vacuum_cost_limit",
1457 : : "toast.autovacuum_vacuum_insert_scale_factor",
1458 : : "toast.autovacuum_vacuum_insert_threshold",
1459 : : "toast.autovacuum_vacuum_max_threshold",
1460 : : "toast.autovacuum_vacuum_scale_factor",
1461 : : "toast.autovacuum_vacuum_threshold",
1462 : : "toast.log_autovacuum_min_duration",
1463 : : "toast.vacuum_index_cleanup",
1464 : : "toast.vacuum_max_eager_freeze_failure_rate",
1465 : : "toast.vacuum_truncate",
1466 : : "toast_tuple_target",
1467 : : "user_catalog_table",
1468 : : "vacuum_index_cleanup",
1469 : : "vacuum_max_eager_freeze_failure_rate",
1470 : : "vacuum_truncate",
1471 : : NULL
1472 : : };
1473 : :
1474 : : /* Optional parameters for CREATE VIEW and ALTER VIEW */
1475 : : static const char *const view_optional_parameters[] = {
1476 : : "check_option",
1477 : : "security_barrier",
1478 : : "security_invoker",
1479 : : NULL
1480 : : };
1481 : :
1482 : : /* Forward declaration of functions */
1483 : : static char **psql_completion(const char *text, int start, int end);
1484 : : static char **match_previous_words(int pattern_id,
1485 : : const char *text, int start, int end,
1486 : : char **previous_words,
1487 : : int previous_words_count);
1488 : : static char *create_command_generator(const char *text, int state);
1489 : : static char *drop_command_generator(const char *text, int state);
1490 : : static char *alter_command_generator(const char *text, int state);
1491 : : static char *complete_from_query(const char *text, int state);
1492 : : static char *complete_from_versioned_query(const char *text, int state);
1493 : : static char *complete_from_schema_query(const char *text, int state);
1494 : : static char *complete_from_versioned_schema_query(const char *text, int state);
1495 : : static char *_complete_from_query(const char *simple_query,
1496 : : const SchemaQuery *schema_query,
1497 : : const char *const *keywords,
1498 : : bool verbatim,
1499 : : const char *text, int state);
1500 : : static void set_completion_reference(const char *word);
1501 : : static void set_completion_reference_verbatim(const char *word);
1502 : : static char *complete_from_list(const char *text, int state);
1503 : : static char *complete_from_const(const char *text, int state);
1504 : : static void append_variable_names(char ***varnames, int *nvars,
1505 : : int *maxvars, const char *varname,
1506 : : const char *prefix, const char *suffix);
1507 : : static char **complete_from_variables(const char *text,
1508 : : const char *prefix, const char *suffix, bool need_value);
1509 : : static char *complete_from_files(const char *text, int state);
1510 : : static char *_complete_from_files(const char *text, int state);
1511 : :
1512 : : static char *pg_strdup_keyword_case(const char *s, const char *ref);
1513 : : static char *escape_string(const char *text);
1514 : : static char *make_like_pattern(const char *word);
1515 : : static void parse_identifier(const char *ident,
1516 : : char **schemaname, char **objectname,
1517 : : bool *schemaquoted, bool *objectquoted);
1518 : : static char *requote_identifier(const char *schemaname, const char *objectname,
1519 : : bool quote_schema, bool quote_object);
1520 : : static bool identifier_needs_quotes(const char *ident);
1521 : : static PGresult *exec_query(const char *query);
1522 : :
1523 : : static char **get_previous_words(int point, char **buffer, int *nwords);
1524 : :
1525 : : static char *get_guctype(const char *varname);
1526 : :
1527 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
1528 : : static char *quote_file_name(char *fname, int match_type, char *quote_pointer);
1529 : : static char *dequote_file_name(char *fname, int quote_char);
1530 : : #endif
1531 : :
1532 : :
1533 : : /*
1534 : : * Initialize the readline library for our purposes.
1535 : : */
1536 : : void
8248 tgl@sss.pgh.pa.us 1537 :CBC 3 : initialize_readline(void)
1538 : : {
7944 bruce@momjian.us 1539 : 3 : rl_readline_name = (char *) pset.progname;
4439 rhaas@postgresql.org 1540 : 3 : rl_attempted_completion_function = psql_completion;
1541 : :
1542 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
2319 tgl@sss.pgh.pa.us 1543 : 3 : rl_filename_quoting_function = quote_file_name;
1544 : 3 : rl_filename_dequoting_function = dequote_file_name;
1545 : : #endif
1546 : :
5946 itagaki.takahiro@gma 1547 : 3 : rl_basic_word_break_characters = WORD_BREAKS;
1548 : :
1549 : : /*
1550 : : * Ideally we'd include '"' in rl_completer_quote_characters too, which
1551 : : * should allow us to complete quoted identifiers that include spaces.
1552 : : * However, the library support for rl_completer_quote_characters is
1553 : : * presently too inconsistent to want to mess with that. (Note in
1554 : : * particular that libedit has this variable but completely ignores it.)
1555 : : */
2319 tgl@sss.pgh.pa.us 1556 : 3 : rl_completer_quote_characters = "'";
1557 : :
1558 : : /*
1559 : : * Set rl_filename_quote_characters to "all possible characters",
1560 : : * otherwise Readline will skip filename quoting if it thinks a filename
1561 : : * doesn't need quoting. Readline actually interprets this as bytes, so
1562 : : * there are no encoding considerations here.
1563 : : */
1564 : : #ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS
1565 : : {
1566 : 3 : unsigned char *fqc = (unsigned char *) pg_malloc(256);
1567 : :
1568 [ + + ]: 768 : for (int i = 0; i < 255; i++)
1569 : 765 : fqc[i] = (unsigned char) (i + 1);
1570 : 3 : fqc[255] = '\0';
1571 : 3 : rl_filename_quote_characters = (const char *) fqc;
1572 : : }
1573 : : #endif
1574 : :
8248 1575 : 3 : completion_max_records = 1000;
1576 : :
1577 : : /*
1578 : : * There is a variable rl_completion_query_items for this but apparently
1579 : : * it's not defined everywhere.
1580 : : */
1581 : 3 : }
1582 : :
1583 : : /*
1584 : : * Check if 'word' matches any of the '|'-separated strings in 'pattern',
1585 : : * using case-insensitive or case-sensitive comparisons.
1586 : : *
1587 : : * If pattern is NULL, it's a wild card that matches any word.
1588 : : * If pattern begins with '!', the result is negated, ie we check that 'word'
1589 : : * does *not* match any alternative appearing in the rest of 'pattern'.
1590 : : * Any alternative can contain '*' which is a wild card, i.e., it can match
1591 : : * any substring; however, we allow at most one '*' per alternative.
1592 : : *
1593 : : * For readability, callers should use the macros MatchAny and MatchAnyExcept
1594 : : * to invoke those two special cases for 'pattern'. (But '|' and '*' must
1595 : : * just be written directly in patterns.) There is also MatchAnyN, but that
1596 : : * is supported only in Matches/MatchesCS and is not handled here.
1597 : : */
1598 : : static bool
2808 1599 : 10399 : word_matches(const char *pattern,
1600 : : const char *word,
1601 : : bool case_sensitive)
1602 : : {
1603 : : size_t wordlen;
1604 : :
1605 : : #define cimatch(s1, s2, n) \
1606 : : (case_sensitive ? strncmp(s1, s2, n) == 0 : pg_strncasecmp(s1, s2, n) == 0)
1607 : :
1608 : : /* NULL pattern matches anything. */
3815 1609 [ + + ]: 10399 : if (pattern == NULL)
1610 : 195 : return true;
1611 : :
1612 : : /* Handle negated patterns from the MatchAnyExcept macro. */
1613 [ + + ]: 10204 : if (*pattern == '!')
2808 tgl@sss.pgh.pa.us 1614 :GBC 2 : return !word_matches(pattern + 1, word, case_sensitive);
1615 : :
1616 : : /* Else consider each alternative in the pattern. */
3815 tgl@sss.pgh.pa.us 1617 :CBC 10202 : wordlen = strlen(word);
1618 : : for (;;)
1619 : 966 : {
2808 1620 : 11168 : const char *star = NULL;
1621 : : const char *c;
1622 : :
1623 : : /* Find end of current alternative, and locate any wild card. */
3815 1624 : 11168 : c = pattern;
1625 [ + + + + ]: 75190 : while (*c != '\0' && *c != '|')
1626 : : {
2808 1627 [ + + ]: 64022 : if (*c == '*')
1628 : 409 : star = c;
3815 1629 : 64022 : c++;
1630 : : }
1631 : : /* Was there a wild card? */
2808 1632 [ + + ]: 11168 : if (star)
1633 : : {
1634 : : /* Yes, wildcard match? */
1635 : 409 : size_t beforelen = star - pattern,
1636 : 409 : afterlen = c - star - 1;
1637 : :
1638 [ + + + + : 809 : if (wordlen >= (beforelen + afterlen) &&
+ + ]
1639 [ + + + - ]: 414 : cimatch(word, pattern, beforelen) &&
1640 : 7 : cimatch(word + wordlen - afterlen, star + 1, afterlen))
3798 1641 : 7 : return true;
1642 : : }
1643 : : else
1644 : : {
1645 : : /* No, plain match? */
2808 1646 [ + + + + : 13693 : if (wordlen == (c - pattern) &&
+ + ]
1647 : 2934 : cimatch(word, pattern, wordlen))
3798 1648 : 1312 : return true;
1649 : : }
1650 : : /* Out of alternatives? */
3815 1651 [ + + ]: 9849 : if (*c == '\0')
1652 : 8883 : break;
1653 : : /* Nope, try next alternative. */
1654 : 966 : pattern = c + 1;
1655 : : }
1656 : :
1657 : 8883 : return false;
1658 : : }
1659 : :
1660 : : /*
1661 : : * Implementation of TailMatches and TailMatchesCS tests: do the last N words
1662 : : * in previous_words match the pattern arguments?
1663 : : *
1664 : : * The array indexing might look backwards, but remember that
1665 : : * previous_words[0] contains the *last* word on the line, not the first.
1666 : : */
1667 : : static bool
600 1668 : 9428 : TailMatchesArray(bool case_sensitive,
1669 : : int previous_words_count, char **previous_words,
1670 : : int narg, const char *const *args)
1671 : : {
2808 1672 [ + + ]: 9428 : if (previous_words_count < narg)
1673 : 6364 : return false;
1674 : :
1675 [ + + ]: 3220 : for (int argno = 0; argno < narg; argno++)
1676 : : {
600 1677 : 3173 : const char *arg = args[argno];
1678 : :
2808 1679 [ + + ]: 3173 : if (!word_matches(arg, previous_words[narg - argno - 1],
1680 : : case_sensitive))
1681 : 3017 : return false;
1682 : : }
1683 : :
1684 : 47 : return true;
1685 : : }
1686 : :
1687 : : /*
1688 : : * As above, but the pattern is passed as a variadic argument list.
1689 : : */
1690 : : static bool
600 1691 : 36 : TailMatchesImpl(bool case_sensitive,
1692 : : int previous_words_count, char **previous_words,
1693 : : int narg, ...)
1694 : : {
1695 : : const char *argarray[64];
1696 : : va_list args;
1697 : :
1698 [ - + ]: 36 : Assert(narg <= lengthof(argarray));
1699 : :
1700 [ + + ]: 36 : if (previous_words_count < narg)
2808 1701 : 10 : return false;
1702 : :
1703 : 26 : va_start(args, narg);
600 1704 [ + + ]: 64 : for (int argno = 0; argno < narg; argno++)
1705 : 38 : argarray[argno] = va_arg(args, const char *);
1706 : 26 : va_end(args);
1707 : :
1708 : 26 : return TailMatchesArray(case_sensitive,
1709 : : previous_words_count, previous_words,
1710 : : narg, argarray);
1711 : : }
1712 : :
1713 : : /*
1714 : : * Implementation of HeadMatches and HeadMatchesCS tests: do the first N
1715 : : * words in previous_words match the pattern arguments?
1716 : : */
1717 : : static bool
1718 : 6503 : HeadMatchesArray(bool case_sensitive,
1719 : : int previous_words_count, char **previous_words,
1720 : : int narg, const char *const *args)
1721 : : {
1722 [ + + ]: 6503 : if (previous_words_count < narg)
1723 : 604 : return false;
1724 : :
2808 1725 [ + + ]: 7259 : for (int argno = 0; argno < narg; argno++)
1726 : : {
600 1727 : 7224 : const char *arg = args[argno];
1728 : :
1729 [ + + ]: 7224 : if (!word_matches(arg, previous_words[previous_words_count - argno - 1],
1730 : : case_sensitive))
2808 1731 : 5864 : return false;
1732 : : }
1733 : :
1734 : 35 : return true;
1735 : : }
1736 : :
1737 : : /*
1738 : : * As above, but the pattern is passed as a variadic argument list.
1739 : : */
1740 : : static bool
1741 : 10 : HeadMatchesImpl(bool case_sensitive,
1742 : : int previous_words_count, char **previous_words,
1743 : : int narg, ...)
1744 : : {
1745 : : const char *argarray[64];
1746 : : va_list args;
1747 : :
600 1748 [ - + ]: 10 : Assert(narg <= lengthof(argarray));
1749 : :
2808 1750 [ + + ]: 10 : if (previous_words_count < narg)
1751 : 2 : return false;
1752 : :
1753 : 8 : va_start(args, narg);
600 1754 [ + + ]: 25 : for (int argno = 0; argno < narg; argno++)
1755 : 17 : argarray[argno] = va_arg(args, const char *);
1756 : 8 : va_end(args);
1757 : :
1758 : 8 : return HeadMatchesArray(case_sensitive,
1759 : : previous_words_count, previous_words,
1760 : : narg, argarray);
1761 : : }
1762 : :
1763 : : /*
1764 : : * Implementation of Matches and MatchesCS tests: do all of the words
1765 : : * in previous_words match the pattern arguments?
1766 : : *
1767 : : * This supports an additional kind of wildcard: MatchAnyN (represented as "")
1768 : : * can match any number of words, including zero, in the middle of the list.
1769 : : */
1770 : : static bool
1771 : 33759 : MatchesArray(bool case_sensitive,
1772 : : int previous_words_count, char **previous_words,
1773 : : int narg, const char *const *args)
1774 : : {
1775 : 33759 : int match_any_pos = -1;
1776 : :
1777 : : /* Even with MatchAnyN, there must be at least N-1 words */
1778 [ + + ]: 33759 : if (previous_words_count < narg - 1)
1779 : 17565 : return false;
1780 : :
1781 : : /* Check for MatchAnyN */
2808 1782 [ + + ]: 67712 : for (int argno = 0; argno < narg; argno++)
1783 : : {
600 1784 : 52524 : const char *arg = args[argno];
1785 : :
1786 [ + + + + ]: 52524 : if (arg != NULL && arg[0] == '\0')
1787 : : {
1788 : 1006 : match_any_pos = argno;
1789 : 1006 : break;
1790 : : }
1791 : : }
1792 : :
1793 [ + + ]: 16194 : if (match_any_pos < 0)
1794 : : {
1795 : : /* Standard case without MatchAnyN */
1796 [ + + ]: 15188 : if (previous_words_count != narg)
1797 : 10822 : return false;
1798 : :
1799 : : /* Either Head or Tail match will do for the rest */
1800 [ + + ]: 4366 : if (!HeadMatchesArray(case_sensitive,
1801 : : previous_words_count, previous_words,
1802 : : narg, args))
1803 : 4339 : return false;
1804 : : }
1805 : : else
1806 : : {
1807 : : /* Match against head */
1808 [ + - ]: 1006 : if (!HeadMatchesArray(case_sensitive,
1809 : : previous_words_count, previous_words,
1810 : : match_any_pos, args))
1811 : 1006 : return false;
1812 : :
1813 : : /* Match against tail */
600 tgl@sss.pgh.pa.us 1814 [ # # ]:UBC 0 : if (!TailMatchesArray(case_sensitive,
1815 : : previous_words_count, previous_words,
1816 : 0 : narg - match_any_pos - 1,
1817 : 0 : args + match_any_pos + 1))
1818 : 0 : return false;
1819 : : }
1820 : :
2808 tgl@sss.pgh.pa.us 1821 :CBC 27 : return true;
1822 : : }
1823 : :
1824 : : /*
1825 : : * As above, but the pattern is passed as a variadic argument list.
1826 : : */
1827 : : static bool
600 1828 : 14 : MatchesImpl(bool case_sensitive,
1829 : : int previous_words_count, char **previous_words,
1830 : : int narg, ...)
1831 : : {
1832 : : const char *argarray[64];
1833 : : va_list args;
1834 : :
1835 [ - + ]: 14 : Assert(narg <= lengthof(argarray));
1836 : :
1837 : : /* Even with MatchAnyN, there must be at least N-1 words */
1838 [ - + ]: 14 : if (previous_words_count < narg - 1)
600 tgl@sss.pgh.pa.us 1839 :UBC 0 : return false;
1840 : :
600 tgl@sss.pgh.pa.us 1841 :CBC 14 : va_start(args, narg);
1842 [ + + ]: 56 : for (int argno = 0; argno < narg; argno++)
1843 : 42 : argarray[argno] = va_arg(args, const char *);
1844 : 14 : va_end(args);
1845 : :
1846 : 14 : return MatchesArray(case_sensitive,
1847 : : previous_words_count, previous_words,
1848 : : narg, argarray);
1849 : : }
1850 : :
1851 : : /*
1852 : : * Check if the final character of 's' is 'c'.
1853 : : */
1854 : : static bool
3815 1855 : 3 : ends_with(const char *s, char c)
1856 : : {
1857 : 3 : size_t length = strlen(s);
1858 : :
1859 [ + - + - ]: 3 : return (length > 0 && s[length - 1] == c);
1860 : : }
1861 : :
1862 : : /*
1863 : : * The completion function.
1864 : : *
1865 : : * According to readline spec this gets passed the text entered so far and its
1866 : : * start and end positions in the readline buffer. The return value is some
1867 : : * partially obscure list format that can be generated by readline's
1868 : : * rl_completion_matches() function, so we don't have to worry about it.
1869 : : */
1870 : : static char **
4439 rhaas@postgresql.org 1871 : 77 : psql_completion(const char *text, int start, int end)
1872 : : {
1873 : : /* This is the variable we'll return. */
9544 bruce@momjian.us 1874 : 77 : char **matches = NULL;
1875 : :
1876 : : /* Workspace for parsed words. */
1877 : : char *words_buffer;
1878 : :
1879 : : /* This array will contain pointers to parsed words. */
1880 : : char **previous_words;
1881 : :
1882 : : /* The number of words found on the input line. */
1883 : : int previous_words_count;
1884 : :
1885 : : /*
1886 : : * For compactness, we use these macros to reference previous_words[].
1887 : : * Caution: do not access a previous_words[] entry without having checked
1888 : : * previous_words_count to be sure it's valid. In most cases below, that
1889 : : * check is implicit in a TailMatches() or similar macro, but in some
1890 : : * places we have to check it explicitly.
1891 : : */
1892 : : #define prev_wd (previous_words[0])
1893 : : #define prev2_wd (previous_words[1])
1894 : : #define prev3_wd (previous_words[2])
1895 : : #define prev4_wd (previous_words[3])
1896 : : #define prev5_wd (previous_words[4])
1897 : : #define prev6_wd (previous_words[5])
1898 : : #define prev7_wd (previous_words[6])
1899 : : #define prev8_wd (previous_words[7])
1900 : : #define prev9_wd (previous_words[8])
1901 : :
1902 : : /* Match the last N words before point, case-insensitively. */
1903 : : #define TailMatches(...) \
1904 : : TailMatchesImpl(false, previous_words_count, previous_words, \
1905 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1906 : :
1907 : : /* Match the last N words before point, case-sensitively. */
1908 : : #define TailMatchesCS(...) \
1909 : : TailMatchesImpl(true, previous_words_count, previous_words, \
1910 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1911 : :
1912 : : /* Match N words representing all of the line, case-insensitively. */
1913 : : #define Matches(...) \
1914 : : MatchesImpl(false, previous_words_count, previous_words, \
1915 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1916 : :
1917 : : /* Match N words representing all of the line, case-sensitively. */
1918 : : #define MatchesCS(...) \
1919 : : MatchesImpl(true, previous_words_count, previous_words, \
1920 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1921 : :
1922 : : /* Match the first N words on the line, case-insensitively. */
1923 : : #define HeadMatches(...) \
1924 : : HeadMatchesImpl(false, previous_words_count, previous_words, \
1925 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1926 : :
1927 : : /* Match the first N words on the line, case-sensitively. */
1928 : : #define HeadMatchesCS(...) \
1929 : : HeadMatchesImpl(true, previous_words_count, previous_words, \
1930 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1931 : :
1932 : : /* psql's backslash commands. */
1933 : : static const char *const backslash_commands[] = {
1934 : : "\\a",
1935 : : "\\bind", "\\bind_named",
1936 : : "\\connect", "\\conninfo", "\\C", "\\cd", "\\close_prepared", "\\copy",
1937 : : "\\copyright", "\\crosstabview",
1938 : : "\\d", "\\da", "\\dA", "\\dAc", "\\dAf", "\\dAo", "\\dAp",
1939 : : "\\db", "\\dc", "\\dconfig", "\\dC", "\\dd", "\\ddp", "\\dD",
1940 : : "\\des", "\\det", "\\deu", "\\dew", "\\dE", "\\df",
1941 : : "\\dF", "\\dFd", "\\dFp", "\\dFt", "\\dg", "\\di", "\\dl", "\\dL",
1942 : : "\\dm", "\\dn", "\\do", "\\dO", "\\dp", "\\dP", "\\dPi", "\\dPt",
1943 : : "\\drds", "\\drg", "\\dRs", "\\dRp", "\\ds",
1944 : : "\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dX", "\\dy",
1945 : : "\\echo", "\\edit", "\\ef", "\\elif", "\\else", "\\encoding",
1946 : : "\\endif", "\\endpipeline", "\\errverbose", "\\ev",
1947 : : "\\f", "\\flush", "\\flushrequest",
1948 : : "\\g", "\\gdesc", "\\getenv", "\\getresults", "\\gexec", "\\gset", "\\gx",
1949 : : "\\help", "\\html",
1950 : : "\\if", "\\include", "\\include_relative", "\\ir",
1951 : : "\\list", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
1952 : : "\\out",
1953 : : "\\parse", "\\password", "\\print", "\\prompt", "\\pset",
1954 : : "\\qecho", "\\quit",
1955 : : "\\reset", "\\restrict",
1956 : : "\\s", "\\sendpipeline", "\\set", "\\setenv", "\\sf",
1957 : : "\\startpipeline", "\\sv", "\\syncpipeline",
1958 : : "\\t", "\\T", "\\timing",
1959 : : "\\unrestrict", "\\unset",
1960 : : "\\x",
1961 : : "\\warn", "\\watch", "\\write",
1962 : : "\\z",
1963 : : "\\!", "\\?",
1964 : : NULL
1965 : : };
1966 : :
1967 : : /*
1968 : : * Temporary workaround for a bug in recent (2019) libedit: it incorrectly
1969 : : * de-escapes the input "text", causing us to fail to recognize backslash
1970 : : * commands. So get the string to look at from rl_line_buffer instead.
1971 : : */
2339 tgl@sss.pgh.pa.us 1972 : 77 : char *text_copy = pnstrdup(rl_line_buffer + start, end - start);
1973 : 77 : text = text_copy;
1974 : :
1975 : : /* Remember last char of the given input word. */
2319 1976 [ + + ]: 77 : completion_last_char = (end > start) ? text[end - start - 1] : '\0';
1977 : :
1978 : : /* We usually want the append character to be a space. */
9544 bruce@momjian.us 1979 : 77 : rl_completion_append_character = ' ';
1980 : :
1981 : : /* Clear a few things. */
1982 : 77 : completion_charp = NULL;
1983 : 77 : completion_charpp = NULL;
1581 tgl@sss.pgh.pa.us 1984 : 77 : completion_vquery = NULL;
1985 : 77 : completion_squery = NULL;
1986 : 77 : completion_ref_object = NULL;
1987 : 77 : completion_ref_schema = NULL;
1988 : :
1989 : : /*
1990 : : * Scan the input line to extract the words before our current position.
1991 : : * According to those we'll make some smart decisions on what the user is
1992 : : * probably intending to type.
1993 : : */
3814 1994 : 77 : previous_words = get_previous_words(start,
1995 : : &words_buffer,
1996 : : &previous_words_count);
1997 : :
1998 : : /* If current word is a backslash command, offer completions for that */
9544 bruce@momjian.us 1999 [ + + ]: 77 : if (text[0] == '\\')
5232 peter_e@gmx.net 2000 : 1 : COMPLETE_WITH_LIST_CS(backslash_commands);
2001 : :
2002 : : /* If current word is a variable interpolation, handle that case */
5587 rhaas@postgresql.org 2003 [ + + + - ]: 76 : else if (text[0] == ':' && text[1] != ':')
2004 : : {
2005 [ - + ]: 2 : if (text[1] == '\'')
4309 fujii@postgresql.org 2006 :UBC 0 : matches = complete_from_variables(text, ":'", "'", true);
5587 rhaas@postgresql.org 2007 [ - + ]:CBC 2 : else if (text[1] == '"')
4309 fujii@postgresql.org 2008 :UBC 0 : matches = complete_from_variables(text, ":\"", "\"", true);
805 akorotkov@postgresql 2009 [ + + + - ]:CBC 2 : else if (text[1] == '{' && text[2] == '?')
2010 : 1 : matches = complete_from_variables(text, ":{?", "}", true);
2011 : : else
4309 fujii@postgresql.org 2012 : 1 : matches = complete_from_variables(text, ":", "", true);
2013 : : }
2014 : :
2015 : : /* If no previous word, suggest one of the basic sql commands */
3815 tgl@sss.pgh.pa.us 2016 [ + + ]: 74 : else if (previous_words_count == 0)
9544 bruce@momjian.us 2017 : 2 : COMPLETE_WITH_LIST(sql_commands);
2018 : :
2019 : : /* Else try completions based on matching patterns of previous words */
2020 : : else
2021 : : {
2022 : : #ifdef SWITCH_CONVERSION_APPLIED
2023 : : /*
2024 : : * If we have transformed match_previous_words into a switch, iterate
2025 : : * through tcpatterns[] to see which pattern ids match.
2026 : : *
2027 : : * For now, we have to try the patterns in the order they are stored
2028 : : * (matching the order of switch cases in match_previous_words),
2029 : : * because some of the logic in match_previous_words assumes that
2030 : : * previous matches have been eliminated. This is fairly
2031 : : * unprincipled, and it is likely that there are undesirable as well
2032 : : * as desirable interactions hidden in the order of the pattern
2033 : : * checks. TODO: think about a better way to manage that.
2034 : : */
600 tgl@sss.pgh.pa.us 2035 [ + + ]: 44276 : for (int tindx = 0; tindx < lengthof(tcpatterns); tindx++)
2036 : : {
2037 : 44270 : const TCPattern *tcpat = tcpatterns + tindx;
2038 : 44270 : bool match = false;
2039 : :
2040 [ + - + + : 44270 : switch (tcpat->kind)
+ + - ]
2041 : : {
2042 : 33745 : case Match:
2043 : 33745 : match = MatchesArray(false,
2044 : : previous_words_count,
2045 : : previous_words,
2046 : 33745 : tcpat->nwords, tcpat->words);
2047 : 33745 : break;
600 tgl@sss.pgh.pa.us 2048 :UBC 0 : case MatchCS:
2049 : 0 : match = MatchesArray(true,
2050 : : previous_words_count,
2051 : : previous_words,
2052 : 0 : tcpat->nwords, tcpat->words);
2053 : 0 : break;
600 tgl@sss.pgh.pa.us 2054 :CBC 1103 : case HeadMatch:
2055 : 1103 : match = HeadMatchesArray(false,
2056 : : previous_words_count,
2057 : : previous_words,
2058 : 1103 : tcpat->nwords, tcpat->words);
2059 : 1103 : break;
2060 : 20 : case HeadMatchCS:
2061 : 20 : match = HeadMatchesArray(true,
2062 : : previous_words_count,
2063 : : previous_words,
2064 : 20 : tcpat->nwords, tcpat->words);
2065 : 20 : break;
2066 : 8803 : case TailMatch:
2067 : 8803 : match = TailMatchesArray(false,
2068 : : previous_words_count,
2069 : : previous_words,
2070 : 8803 : tcpat->nwords, tcpat->words);
2071 : 8803 : break;
2072 : 599 : case TailMatchCS:
2073 : 599 : match = TailMatchesArray(true,
2074 : : previous_words_count,
2075 : : previous_words,
2076 : 599 : tcpat->nwords, tcpat->words);
2077 : 599 : break;
2078 : : }
2079 [ + + ]: 44270 : if (match)
2080 : : {
2081 : 68 : matches = match_previous_words(tcpat->id, text, start, end,
2082 : : previous_words,
2083 : : previous_words_count);
2084 [ + + ]: 68 : if (matches != NULL)
2085 : 66 : break;
2086 : : }
2087 : : }
2088 : : #else /* !SWITCH_CONVERSION_APPLIED */
2089 : : /*
2090 : : * If gen_tabcomplete.pl hasn't been applied to this code, just let
2091 : : * match_previous_words scan through all its patterns.
2092 : : */
2093 : : matches = match_previous_words(0, text, start, end,
2094 : : previous_words,
2095 : : previous_words_count);
2096 : : #endif /* SWITCH_CONVERSION_APPLIED */
2097 : : }
2098 : :
2099 : : /*
2100 : : * Finally, we look through the list of "things", such as TABLE, INDEX and
2101 : : * check if that was the previous word. If so, execute the query to get a
2102 : : * list of them.
2103 : : */
597 2104 [ + + + - ]: 77 : if (matches == NULL && previous_words_count > 0)
2105 : : {
2106 : : const pgsql_thing_t *wac;
2107 : :
600 2108 [ + + ]: 254 : for (wac = words_after_create; wac->name != NULL; wac++)
2109 : : {
2110 [ + + ]: 252 : if (pg_strcasecmp(prev_wd, wac->name) == 0)
2111 : : {
2112 [ - + ]: 4 : if (wac->query)
600 tgl@sss.pgh.pa.us 2113 :UBC 0 : COMPLETE_WITH_QUERY_LIST(wac->query,
2114 : : wac->keywords);
600 tgl@sss.pgh.pa.us 2115 [ + + ]:CBC 4 : else if (wac->vquery)
2116 : 1 : COMPLETE_WITH_VERSIONED_QUERY_LIST(wac->vquery,
2117 : : wac->keywords);
2118 [ + - ]: 3 : else if (wac->squery)
2119 : 3 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(wac->squery,
2120 : : wac->keywords);
2121 : 4 : break;
2122 : : }
2123 : : }
2124 : : }
2125 : :
2126 : : /*
2127 : : * If we still don't have anything to match we have to fabricate some sort
2128 : : * of default list. If we were to just return NULL, readline automatically
2129 : : * attempts filename completion, and that's usually no good.
2130 : : */
2131 [ + + ]: 77 : if (matches == NULL)
2132 : : {
2133 : 2 : COMPLETE_WITH_CONST(true, "");
2134 : : /* Also, prevent Readline from appending stuff to the non-match */
2135 : 2 : rl_completion_append_character = '\0';
2136 : : #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
2137 : 2 : rl_completion_suppress_quote = 1;
2138 : : #endif
2139 : : }
2140 : :
2141 : : /* free storage */
2142 : 77 : free(previous_words);
2143 : 77 : free(words_buffer);
2144 : 77 : free(text_copy);
2145 : 77 : free(completion_ref_object);
2146 : 77 : completion_ref_object = NULL;
2147 : 77 : free(completion_ref_schema);
2148 : 77 : completion_ref_schema = NULL;
2149 : :
2150 : : /* Return our Grand List O' Matches */
2151 : 77 : return matches;
2152 : : }
2153 : :
2154 : : /*
2155 : : * Subroutine to try matches based on previous_words.
2156 : : *
2157 : : * This can operate in one of two modes. As presented, the body of the
2158 : : * function is a long if-else-if chain that sequentially tries each known
2159 : : * match rule. That works, but some C compilers have trouble with such a long
2160 : : * else-if chain, either taking extra time to compile or failing altogether.
2161 : : * Therefore, we prefer to transform the else-if chain into a switch, and then
2162 : : * each call of this function considers just one match rule (under control of
2163 : : * a loop in psql_completion()). Compilers tend to be more ready to deal
2164 : : * with many-arm switches than many-arm else-if chains.
2165 : : *
2166 : : * Each if-condition in this function must begin with a call of one of the
2167 : : * functions Matches, HeadMatches, TailMatches, MatchesCS, HeadMatchesCS, or
2168 : : * TailMatchesCS. The preprocessor gen_tabcomplete.pl strips out those
2169 : : * calls and converts them into entries in tcpatterns[], which are evaluated
2170 : : * by the calling loop in psql_completion(). Successful matches result in
2171 : : * calls to this function with the appropriate pattern_id, causing just the
2172 : : * corresponding switch case to be executed.
2173 : : *
2174 : : * If-conditions in this function can be more complex than a single *Matches
2175 : : * function call in one of two ways (but not both!). They can be OR's
2176 : : * of *Matches calls, such as
2177 : : * else if (Matches("ALTER", "VIEW", MatchAny, "ALTER", MatchAny) ||
2178 : : * Matches("ALTER", "VIEW", MatchAny, "ALTER", "COLUMN", MatchAny))
2179 : : * or they can be a *Matches call AND'ed with some other condition, e.g.
2180 : : * else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE", MatchAny) &&
2181 : : * !ends_with(prev_wd, ','))
2182 : : * The former case is transformed into multiple tcpatterns[] entries and
2183 : : * multiple case labels for the same bit of code. The latter case is
2184 : : * transformed into a case label and a contained if-statement.
2185 : : *
2186 : : * This is split out of psql_completion() primarily to separate code that
2187 : : * gen_tabcomplete.pl should process from code that it should not, although
2188 : : * doing so also helps to avoid extra indentation of this code.
2189 : : *
2190 : : * Returns a matches list, or NULL if no match.
2191 : : */
2192 : : static char **
2193 : 68 : match_previous_words(int pattern_id,
2194 : : const char *text, int start, int end,
2195 : : char **previous_words, int previous_words_count)
2196 : : {
2197 : : /* This is the variable we'll return. */
2198 : 68 : char **matches = NULL;
2199 : :
2200 : : /* Dummy statement, allowing all the match rules to look like "else if" */
2201 : : if (0)
2202 : : {
2203 : : /* skip */
2204 : : }
2205 : :
2206 : : /* gen_tabcomplete.pl begins special processing here */
2207 : 68 : /* BEGIN GEN_TABCOMPLETE */
[ + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- - - - -
- - - - -
- - - - -
- + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - - -
- + + - -
+ - ]
[ + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - - -
- - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + + +
+ + - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - - -
- - - - -
- - - - -
- - - + -
+ - - - -
- - - - -
- - + + +
+ + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - + +
- - + - ]
2208 : :
2209 : : /* CREATE */
2210 : : /* complete with something you can create */
2808 2211 : 1 : else if (TailMatches("CREATE"))
2212 : : {
2213 : : /* only some object types can be created as part of CREATE SCHEMA */
1065 michael@paquier.xyz 2214 [ - + ]: 1 : if (HeadMatches("CREATE", "SCHEMA"))
54 tgl@sss.pgh.pa.us 2215 :UNC 0 : COMPLETE_WITH("AGGREGATE", "COLLATION", "DOMAIN", "FUNCTION",
2216 : : "INDEX", "OPERATOR", "PROCEDURE", "SEQUENCE", "TABLE",
2217 : : "TEXT SEARCH CONFIGURATION", "TEXT SEARCH DICTIONARY",
2218 : : "TEXT SEARCH PARSER", "TEXT SEARCH TEMPLATE",
2219 : : "TRIGGER", "TYPE", "VIEW",
2220 : : /* for INDEX and TABLE/SEQUENCE, respectively */
2221 : : "UNIQUE", "UNLOGGED");
2222 : : else
247 msawada@postgresql.o 2223 :GNC 1 : COMPLETE_WITH_GENERATOR(create_command_generator);
2224 : : }
2225 : : /* complete with something you can create or replace */
2451 fujii@postgresql.org 2226 :CBC 1 : else if (TailMatches("CREATE", "OR", "REPLACE"))
2451 fujii@postgresql.org 2227 :UBC 0 : COMPLETE_WITH("FUNCTION", "PROCEDURE", "LANGUAGE", "RULE", "VIEW",
2228 : : "AGGREGATE", "TRANSFORM", "TRIGGER");
2229 : :
2230 : : /* DROP, but not DROP embedded in other commands */
2231 : : /* complete with something you can drop */
2808 tgl@sss.pgh.pa.us 2232 : 0 : else if (Matches("DROP"))
247 msawada@postgresql.o 2233 :GNC 1 : COMPLETE_WITH_GENERATOR(drop_command_generator);
2234 : :
2235 : : /* ALTER */
2236 : :
2237 : : /* ALTER TABLE */
2808 tgl@sss.pgh.pa.us 2238 :CBC 1 : else if (Matches("ALTER", "TABLE"))
1581 tgl@sss.pgh.pa.us 2239 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
2240 : : "ALL IN TABLESPACE");
2241 : :
2242 : : /* ALTER something */
2808 2243 : 0 : else if (Matches("ALTER"))
247 msawada@postgresql.o 2244 :UNC 0 : COMPLETE_WITH_GENERATOR(alter_command_generator);
2245 : : /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx */
2808 tgl@sss.pgh.pa.us 2246 :UBC 0 : else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny))
2247 : 0 : COMPLETE_WITH("SET TABLESPACE", "OWNED BY");
2248 : : /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY */
2249 : 0 : else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY"))
4283 sfrost@snowman.net 2250 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2251 : : /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY xxx */
2808 tgl@sss.pgh.pa.us 2252 : 0 : else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY", MatchAny))
2253 : 0 : COMPLETE_WITH("SET TABLESPACE");
2254 : : /* ALTER AGGREGATE,FUNCTION,PROCEDURE,ROUTINE <name> */
2255 : 0 : else if (Matches("ALTER", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny))
2256 : 0 : COMPLETE_WITH("(");
2257 : : /* ALTER AGGREGATE <name> (...) */
1928 michael@paquier.xyz 2258 : 0 : else if (Matches("ALTER", "AGGREGATE", MatchAny, MatchAny))
2259 : : {
3815 tgl@sss.pgh.pa.us 2260 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
2808 2261 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA");
2262 : : else
5077 magnus@hagander.net 2263 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2264 : : }
2265 : : /* ALTER FUNCTION <name> (...) */
1240 dean.a.rasheed@gmail 2266 : 0 : else if (Matches("ALTER", "FUNCTION", MatchAny, MatchAny))
2267 : : {
2268 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
2269 : 0 : COMPLETE_WITH(Alter_function_options);
2270 : : else
2271 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2272 : : }
2273 : : /* ALTER PROCEDURE <name> (...) */
2274 : 0 : else if (Matches("ALTER", "PROCEDURE", MatchAny, MatchAny))
2275 : : {
2276 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
2277 : 0 : COMPLETE_WITH(Alter_procedure_options);
2278 : : else
2279 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2280 : : }
2281 : : /* ALTER ROUTINE <name> (...) */
2282 : 0 : else if (Matches("ALTER", "ROUTINE", MatchAny, MatchAny))
2283 : : {
1928 michael@paquier.xyz 2284 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
1240 dean.a.rasheed@gmail 2285 : 0 : COMPLETE_WITH(Alter_routine_options);
2286 : : else
1928 michael@paquier.xyz 2287 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2288 : : }
2289 : : /* ALTER FUNCTION|ROUTINE <name> (...) PARALLEL */
1240 dean.a.rasheed@gmail 2290 : 0 : else if (Matches("ALTER", "FUNCTION|ROUTINE", MatchAny, MatchAny, "PARALLEL"))
2291 : 0 : COMPLETE_WITH("RESTRICTED", "SAFE", "UNSAFE");
2292 : : /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) [EXTERNAL] SECURITY */
2293 : 0 : else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "SECURITY") ||
2294 : : Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "EXTERNAL", "SECURITY"))
2295 : 0 : COMPLETE_WITH("DEFINER", "INVOKER");
2296 : : /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) RESET */
2297 : 0 : else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "RESET"))
2298 : 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
2299 : : "ALL");
2300 : : /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) SET */
2301 : 0 : else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "SET"))
2302 : 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
2303 : : "SCHEMA");
2304 : :
2305 : : /* ALTER PUBLICATION <name> */
2808 tgl@sss.pgh.pa.us 2306 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny))
1676 akapila@postgresql.o 2307 : 0 : COMPLETE_WITH("ADD", "DROP", "OWNER TO", "RENAME TO", "SET");
2308 : : /* ALTER PUBLICATION <name> ADD */
2309 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD"))
1346 alvherre@alvh.no-ip. 2310 : 0 : COMPLETE_WITH("TABLES IN SCHEMA", "TABLE");
600 tgl@sss.pgh.pa.us 2311 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE"))
2312 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2313 : 0 : else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE") &&
2314 [ # # ]: 0 : ends_with(prev_wd, ','))
1581 2315 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2316 : :
2317 : : /*
2318 : : * "ALTER PUBLICATION <name> SET TABLE <name> WHERE (" - complete with
2319 : : * table attributes
2320 : : *
2321 : : * "ALTER PUBLICATION <name> ADD TABLE <name> WHERE (" - complete with
2322 : : * table attributes
2323 : : */
600 2324 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "WHERE"))
1558 akapila@postgresql.o 2325 : 0 : COMPLETE_WITH("(");
600 tgl@sss.pgh.pa.us 2326 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "("))
1558 akapila@postgresql.o 2327 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2328 : 0 : else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE") &&
2329 [ # # ]: 0 : !TailMatches("WHERE", "(*)"))
2330 : 0 : COMPLETE_WITH(",", "WHERE (");
1583 alvherre@alvh.no-ip. 2331 : 0 : else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE"))
2332 : 0 : COMPLETE_WITH(",");
2333 : : /* ALTER PUBLICATION <name> DROP */
1676 akapila@postgresql.o 2334 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "DROP"))
1346 alvherre@alvh.no-ip. 2335 : 0 : COMPLETE_WITH("TABLES IN SCHEMA", "TABLE");
2336 : : /* ALTER PUBLICATION <name> SET */
2808 tgl@sss.pgh.pa.us 2337 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET"))
71 akapila@postgresql.o 2338 :UNC 0 : COMPLETE_WITH("(", "ALL SEQUENCES", "ALL TABLES", "TABLES IN SCHEMA", "TABLE");
2339 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL"))
2340 : 0 : COMPLETE_WITH("SEQUENCES", "TABLES");
2341 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES"))
60 2342 : 0 : COMPLETE_WITH("EXCEPT ( TABLE");
71 2343 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT"))
60 2344 : 0 : COMPLETE_WITH("( TABLE");
2345 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "("))
2346 : 0 : COMPLETE_WITH("TABLE");
2347 : : /* Complete "ALTER PUBLICATION <name> FOR TABLE" with "<table>, ..." */
2348 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "(", "TABLE"))
2349 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2350 [ # # ]: 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && ends_with(prev_wd, ','))
71 2351 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
60 2352 [ # # ]: 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && !ends_with(prev_wd, ','))
2353 : 0 : COMPLETE_WITH(")");
1338 alvherre@alvh.no-ip. 2354 :UBC 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD|DROP|SET", "TABLES", "IN", "SCHEMA"))
1581 tgl@sss.pgh.pa.us 2355 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
2356 : : " AND nspname NOT LIKE E'pg\\\\_%%'",
2357 : : "CURRENT_SCHEMA");
2358 : : /* ALTER PUBLICATION <name> SET ( */
600 2359 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "SET", "("))
569 akapila@postgresql.o 2360 : 0 : COMPLETE_WITH("publish", "publish_generated_columns", "publish_via_partition_root");
2361 : : /* ALTER SUBSCRIPTION <name> */
2808 tgl@sss.pgh.pa.us 2362 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny))
2363 : 0 : COMPLETE_WITH("CONNECTION", "ENABLE", "DISABLE", "OWNER TO",
2364 : : "RENAME TO", "REFRESH PUBLICATION", "REFRESH SEQUENCES",
2365 : : "SERVER", "SET", "SKIP (", "ADD PUBLICATION", "DROP PUBLICATION");
66 msawada@postgresql.o 2366 :UNC 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, "SERVER"))
2367 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
2368 : : /* ALTER SUBSCRIPTION <name> REFRESH */
219 akapila@postgresql.o 2369 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "REFRESH"))
2370 : 0 : COMPLETE_WITH("PUBLICATION", "SEQUENCES");
2371 : : /* ALTER SUBSCRIPTION <name> REFRESH PUBLICATION WITH ( */
600 tgl@sss.pgh.pa.us 2372 :UBC 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "REFRESH", "PUBLICATION", "WITH", "("))
2808 2373 : 0 : COMPLETE_WITH("copy_data");
2374 : : /* ALTER SUBSCRIPTION <name> SET */
2375 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, "SET"))
2376 : 0 : COMPLETE_WITH("(", "PUBLICATION");
2377 : : /* ALTER SUBSCRIPTION <name> SET ( */
600 2378 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SET", "("))
270 akapila@postgresql.o 2379 :UNC 0 : COMPLETE_WITH("binary", "disable_on_error", "failover",
2380 : : "max_retention_duration", "origin",
2381 : : "password_required", "retain_dead_tuples",
2382 : : "run_as_owner", "slot_name", "streaming",
2383 : : "synchronous_commit", "two_phase");
2384 : : /* ALTER SUBSCRIPTION <name> SKIP ( */
600 tgl@sss.pgh.pa.us 2385 :UBC 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SKIP", "("))
1530 akapila@postgresql.o 2386 : 0 : COMPLETE_WITH("lsn");
2387 : : /* ALTER SUBSCRIPTION <name> SET PUBLICATION */
600 tgl@sss.pgh.pa.us 2388 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SET", "PUBLICATION"))
2389 : : {
2390 : : /* complete with nothing here as this refers to remote publications */
2391 : : }
2392 : : /* ALTER SUBSCRIPTION <name> ADD|DROP|SET PUBLICATION <name> */
2393 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN,
2394 : : "ADD|DROP|SET", "PUBLICATION", MatchAny))
2808 2395 : 0 : COMPLETE_WITH("WITH (");
2396 : : /* ALTER SUBSCRIPTION <name> ADD|DROP|SET PUBLICATION <name> WITH ( */
600 2397 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN,
2398 : : "ADD|DROP|SET", "PUBLICATION", MatchAny, "WITH", "("))
2808 2399 : 0 : COMPLETE_WITH("copy_data", "refresh");
2400 : :
2401 : : /* ALTER SCHEMA <name> */
2402 : 0 : else if (Matches("ALTER", "SCHEMA", MatchAny))
2403 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO");
2404 : :
2405 : : /* ALTER COLLATION <name> */
2406 : 0 : else if (Matches("ALTER", "COLLATION", MatchAny))
1584 peter@eisentraut.org 2407 : 0 : COMPLETE_WITH("OWNER TO", "REFRESH VERSION", "RENAME TO", "SET SCHEMA");
2408 : :
2409 : : /* ALTER CONVERSION <name> */
2808 tgl@sss.pgh.pa.us 2410 : 0 : else if (Matches("ALTER", "CONVERSION", MatchAny))
2411 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA");
2412 : :
2413 : : /* ALTER DATABASE <name> */
2414 : 0 : else if (Matches("ALTER", "DATABASE", MatchAny))
1566 peter@eisentraut.org 2415 : 0 : COMPLETE_WITH("RESET", "SET", "OWNER TO", "REFRESH COLLATION VERSION", "RENAME TO",
2416 : : "IS_TEMPLATE", "ALLOW_CONNECTIONS",
2417 : : "CONNECTION LIMIT");
2418 : :
2419 : : /* ALTER DATABASE <name> RESET */
467 tomas.vondra@postgre 2420 : 0 : else if (Matches("ALTER", "DATABASE", MatchAny, "RESET"))
2421 : : {
2422 : 0 : set_completion_reference(prev2_wd);
2423 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_database_vars, "ALL");
2424 : : }
2425 : :
2426 : : /* ALTER DATABASE <name> SET TABLESPACE */
2808 tgl@sss.pgh.pa.us 2427 : 0 : else if (Matches("ALTER", "DATABASE", MatchAny, "SET", "TABLESPACE"))
2809 2428 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
2429 : :
2430 : : /* ALTER EVENT TRIGGER */
2808 2431 : 0 : else if (Matches("ALTER", "EVENT", "TRIGGER"))
4429 rhaas@postgresql.org 2432 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
2433 : :
2434 : : /* ALTER EVENT TRIGGER <name> */
2808 tgl@sss.pgh.pa.us 2435 : 0 : else if (Matches("ALTER", "EVENT", "TRIGGER", MatchAny))
2436 : 0 : COMPLETE_WITH("DISABLE", "ENABLE", "OWNER TO", "RENAME TO");
2437 : :
2438 : : /* ALTER EVENT TRIGGER <name> ENABLE */
2439 : 0 : else if (Matches("ALTER", "EVENT", "TRIGGER", MatchAny, "ENABLE"))
2440 : 0 : COMPLETE_WITH("REPLICA", "ALWAYS");
2441 : :
2442 : : /* ALTER EXTENSION <name> */
2443 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny))
1427 2444 : 0 : COMPLETE_WITH("ADD", "DROP", "UPDATE", "SET SCHEMA");
2445 : :
2446 : : /* ALTER EXTENSION <name> ADD|DROP */
1234 michael@paquier.xyz 2447 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP"))
2448 : 0 : COMPLETE_WITH("ACCESS METHOD", "AGGREGATE", "CAST", "COLLATION",
2449 : : "CONVERSION", "DOMAIN", "EVENT TRIGGER", "FOREIGN",
2450 : : "FUNCTION", "MATERIALIZED VIEW", "OPERATOR",
2451 : : "LANGUAGE", "PROCEDURE", "ROUTINE", "SCHEMA",
2452 : : "SEQUENCE", "SERVER", "TABLE", "TEXT SEARCH",
2453 : : "TRANSFORM FOR", "TYPE", "VIEW");
2454 : :
2455 : : /* ALTER EXTENSION <name> ADD|DROP FOREIGN */
2456 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "FOREIGN"))
2457 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
2458 : :
2459 : : /* ALTER EXTENSION <name> ADD|DROP OPERATOR */
2460 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "OPERATOR"))
2461 : 0 : COMPLETE_WITH("CLASS", "FAMILY");
2462 : :
2463 : : /* ALTER EXTENSION <name> ADD|DROP TEXT SEARCH */
2464 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "TEXT", "SEARCH"))
2465 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
2466 : :
2467 : : /* ALTER EXTENSION <name> UPDATE */
2808 tgl@sss.pgh.pa.us 2468 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "UPDATE"))
1439 2469 : 0 : COMPLETE_WITH("TO");
2470 : :
2471 : : /* ALTER EXTENSION <name> UPDATE TO */
2808 2472 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "UPDATE", "TO"))
2473 : : {
1581 2474 : 0 : set_completion_reference(prev3_wd);
1439 2475 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions);
2476 : : }
2477 : :
2478 : : /* ALTER FOREIGN */
2808 2479 : 0 : else if (Matches("ALTER", "FOREIGN"))
2480 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
2481 : :
2482 : : /* ALTER FOREIGN DATA WRAPPER <name> */
2483 : 0 : else if (Matches("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny))
66 msawada@postgresql.o 2484 :UNC 0 : COMPLETE_WITH("CONNECTION", "HANDLER", "NO",
2485 : : "OPTIONS", "OWNER TO", "RENAME TO", "VALIDATOR");
1643 michael@paquier.xyz 2486 :UBC 0 : else if (Matches("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny, "NO"))
66 msawada@postgresql.o 2487 :UNC 0 : COMPLETE_WITH("CONNECTION", "HANDLER", "VALIDATOR");
2488 : :
2489 : : /* ALTER FOREIGN TABLE <name> */
2808 tgl@sss.pgh.pa.us 2490 :UBC 0 : else if (Matches("ALTER", "FOREIGN", "TABLE", MatchAny))
2491 : 0 : COMPLETE_WITH("ADD", "ALTER", "DISABLE TRIGGER", "DROP", "ENABLE",
2492 : : "INHERIT", "NO INHERIT", "OPTIONS", "OWNER TO",
2493 : : "RENAME", "SET", "VALIDATE CONSTRAINT");
2494 : :
2495 : : /* ALTER INDEX */
2496 : 0 : else if (Matches("ALTER", "INDEX"))
1581 2497 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
2498 : : "ALL IN TABLESPACE");
2499 : : /* ALTER INDEX <name> */
2808 2500 : 0 : else if (Matches("ALTER", "INDEX", MatchAny))
2501 : 0 : COMPLETE_WITH("ALTER COLUMN", "OWNER TO", "RENAME TO", "SET",
2502 : : "RESET", "ATTACH PARTITION",
2503 : : "DEPENDS ON EXTENSION", "NO DEPENDS ON EXTENSION");
2504 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ATTACH"))
2505 : 0 : COMPLETE_WITH("PARTITION");
2506 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ATTACH", "PARTITION"))
1581 2507 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
2508 : : /* ALTER INDEX <name> ALTER */
2713 michael@paquier.xyz 2509 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER"))
1849 tmunro@postgresql.or 2510 : 0 : COMPLETE_WITH("COLUMN");
2511 : : /* ALTER INDEX <name> ALTER COLUMN */
2679 michael@paquier.xyz 2512 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN"))
2513 : : {
1581 tgl@sss.pgh.pa.us 2514 : 0 : set_completion_reference(prev3_wd);
2515 : 0 : COMPLETE_WITH_SCHEMA_QUERY_VERBATIM(Query_for_list_of_attribute_numbers);
2516 : : }
2517 : : /* ALTER INDEX <name> ALTER COLUMN <colnum> */
2808 2518 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny))
2519 : 0 : COMPLETE_WITH("SET STATISTICS");
2520 : : /* ALTER INDEX <name> ALTER COLUMN <colnum> SET */
2713 michael@paquier.xyz 2521 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny, "SET"))
2522 : 0 : COMPLETE_WITH("STATISTICS");
2523 : : /* ALTER INDEX <name> ALTER COLUMN <colnum> SET STATISTICS */
2524 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STATISTICS"))
2525 : : {
2526 : : /* Enforce no completion here, as an integer has to be specified */
2527 : : }
2528 : : /* ALTER INDEX <name> SET */
2808 tgl@sss.pgh.pa.us 2529 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "SET"))
2530 : 0 : COMPLETE_WITH("(", "TABLESPACE");
2531 : : /* ALTER INDEX <name> RESET */
2532 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "RESET"))
2533 : 0 : COMPLETE_WITH("(");
2534 : : /* ALTER INDEX <foo> SET|RESET ( */
2535 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "RESET", "("))
2692 2536 : 0 : COMPLETE_WITH("fillfactor",
2537 : : "deduplicate_items", /* BTREE */
2538 : : "fastupdate", "gin_pending_list_limit", /* GIN */
2539 : : "buffering", /* GiST */
2540 : : "pages_per_range", "autosummarize" /* BRIN */
2541 : : );
2808 2542 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "SET", "("))
2692 2543 : 0 : COMPLETE_WITH("fillfactor =",
2544 : : "deduplicate_items =", /* BTREE */
2545 : : "fastupdate =", "gin_pending_list_limit =", /* GIN */
2546 : : "buffering =", /* GiST */
2547 : : "pages_per_range =", "autosummarize =" /* BRIN */
2548 : : );
1849 tmunro@postgresql.or 2549 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "NO", "DEPENDS"))
2550 : 0 : COMPLETE_WITH("ON EXTENSION");
2551 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "DEPENDS"))
2552 : 0 : COMPLETE_WITH("ON EXTENSION");
2553 : :
2554 : : /* ALTER LANGUAGE <name> */
2808 tgl@sss.pgh.pa.us 2555 : 0 : else if (Matches("ALTER", "LANGUAGE", MatchAny))
2490 michael@paquier.xyz 2556 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO");
2557 : :
2558 : : /* ALTER LARGE OBJECT <oid> */
2808 tgl@sss.pgh.pa.us 2559 : 0 : else if (Matches("ALTER", "LARGE", "OBJECT", MatchAny))
2560 : 0 : COMPLETE_WITH("OWNER TO");
2561 : :
2562 : : /* ALTER MATERIALIZED VIEW */
2563 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW"))
1581 2564 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_matviews,
2565 : : "ALL IN TABLESPACE");
2566 : :
2567 : : /* ALTER USER,ROLE <name> */
2808 2568 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny) &&
2569 [ # # ]: 0 : !TailMatches("USER", "MAPPING"))
2570 : 0 : COMPLETE_WITH("BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE",
2571 : : "ENCRYPTED PASSWORD", "IN", "INHERIT", "LOGIN", "NOBYPASSRLS",
2572 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
2573 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
2574 : : "RENAME TO", "REPLICATION", "RESET", "SET", "SUPERUSER",
2575 : : "VALID UNTIL", "WITH");
2576 : : /* ALTER USER,ROLE <name> IN */
89 alvherre@kurilemu.de 2577 :UNC 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN"))
2578 : 0 : COMPLETE_WITH("DATABASE");
2579 : : /* ALTER USER,ROLE <name> IN DATABASE */
2580 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN", "DATABASE"))
2581 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2582 : : /* ALTER USER,ROLE <name> IN DATABASE <dbname> */
2583 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN", "DATABASE", MatchAny))
2584 : 0 : COMPLETE_WITH("SET", "RESET");
2585 : : /* ALTER USER,ROLE <name> IN DATABASE <dbname> SET */
2586 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN", "DATABASE", MatchAny, "SET"))
2587 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_set_vars);
2588 : : /* XXX missing support for ALTER ROLE <name> IN DATABASE <dbname> RESET */
2589 : : /* ALTER USER,ROLE <name> RESET */
467 tomas.vondra@postgre 2590 :UBC 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "RESET"))
2591 : : {
303 2592 : 0 : set_completion_reference(prev2_wd);
467 2593 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_user_vars, "ALL");
2594 : : }
2595 : :
2596 : : /* ALTER USER,ROLE <name> WITH */
2808 tgl@sss.pgh.pa.us 2597 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "WITH"))
2598 : : /* Similar to the above, but don't complete "WITH" again. */
2599 : 0 : COMPLETE_WITH("BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE",
2600 : : "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS",
2601 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
2602 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
2603 : : "RENAME TO", "REPLICATION", "RESET", "SET", "SUPERUSER",
2604 : : "VALID UNTIL");
2605 : :
2606 : : /* ALTER DEFAULT PRIVILEGES */
2607 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES"))
782 msawada@postgresql.o 2608 : 0 : COMPLETE_WITH("FOR", "GRANT", "IN SCHEMA", "REVOKE");
2609 : : /* ALTER DEFAULT PRIVILEGES FOR */
2808 tgl@sss.pgh.pa.us 2610 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR"))
2611 : 0 : COMPLETE_WITH("ROLE");
2612 : : /* ALTER DEFAULT PRIVILEGES IN */
2613 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN"))
2614 : 0 : COMPLETE_WITH("SCHEMA");
2615 : : /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... */
2616 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER",
2617 : : MatchAny))
2618 : 0 : COMPLETE_WITH("GRANT", "REVOKE", "IN SCHEMA");
2619 : : /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... */
2620 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2621 : : MatchAny))
2622 : 0 : COMPLETE_WITH("GRANT", "REVOKE", "FOR ROLE");
2623 : : /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR */
2624 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2625 : : MatchAny, "FOR"))
2626 : 0 : COMPLETE_WITH("ROLE");
2627 : : /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... IN SCHEMA ... */
2628 : : /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR ROLE|USER ... */
2629 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER",
2630 : : MatchAny, "IN", "SCHEMA", MatchAny) ||
2631 : : Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2632 : : MatchAny, "FOR", "ROLE|USER", MatchAny))
2633 : 0 : COMPLETE_WITH("GRANT", "REVOKE");
2634 : : /* ALTER DOMAIN <name> */
2635 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny))
2636 : 0 : COMPLETE_WITH("ADD", "DROP", "OWNER TO", "RENAME", "SET",
2637 : : "VALIDATE CONSTRAINT");
2638 : : /* ALTER DOMAIN <sth> ADD */
384 alvherre@kurilemu.de 2639 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "ADD"))
2640 : 0 : COMPLETE_WITH("CONSTRAINT", "NOT NULL", "CHECK (");
2641 : : /* ALTER DOMAIN <sth> ADD CONSTRAINT <sth> */
2642 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "ADD", "CONSTRAINT", MatchAny))
2643 : 0 : COMPLETE_WITH("NOT NULL", "CHECK (");
2644 : : /* ALTER DOMAIN <sth> DROP */
2808 tgl@sss.pgh.pa.us 2645 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "DROP"))
2646 : 0 : COMPLETE_WITH("CONSTRAINT", "DEFAULT", "NOT NULL");
2647 : : /* ALTER DOMAIN <sth> DROP|RENAME|VALIDATE CONSTRAINT */
2648 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "DROP|RENAME|VALIDATE", "CONSTRAINT"))
2649 : : {
1581 2650 : 0 : set_completion_reference(prev3_wd);
2651 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_type);
2652 : : }
2653 : : /* ALTER DOMAIN <sth> RENAME */
2808 2654 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "RENAME"))
2655 : 0 : COMPLETE_WITH("CONSTRAINT", "TO");
2656 : : /* ALTER DOMAIN <sth> RENAME CONSTRAINT <sth> */
2657 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "RENAME", "CONSTRAINT", MatchAny))
2658 : 0 : COMPLETE_WITH("TO");
2659 : :
2660 : : /* ALTER DOMAIN <sth> SET */
2661 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "SET"))
2662 : 0 : COMPLETE_WITH("DEFAULT", "NOT NULL", "SCHEMA");
2663 : : /* ALTER SEQUENCE <name> */
2664 : 0 : else if (Matches("ALTER", "SEQUENCE", MatchAny))
1643 michael@paquier.xyz 2665 : 0 : COMPLETE_WITH("AS", "INCREMENT", "MINVALUE", "MAXVALUE", "RESTART",
2666 : : "START", "NO", "CACHE", "CYCLE", "SET", "OWNED BY",
2667 : : "OWNER TO", "RENAME TO");
2668 : : /* ALTER SEQUENCE <name> AS */
2669 : 0 : else if (TailMatches("ALTER", "SEQUENCE", MatchAny, "AS"))
2670 : 0 : COMPLETE_WITH_CS("smallint", "integer", "bigint");
2671 : : /* ALTER SEQUENCE <name> NO */
2808 tgl@sss.pgh.pa.us 2672 : 0 : else if (Matches("ALTER", "SEQUENCE", MatchAny, "NO"))
2673 : 0 : COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
2674 : : /* ALTER SEQUENCE <name> SET */
1514 peter@eisentraut.org 2675 : 0 : else if (Matches("ALTER", "SEQUENCE", MatchAny, "SET"))
2676 : 0 : COMPLETE_WITH("SCHEMA", "LOGGED", "UNLOGGED");
2677 : : /* ALTER SERVER <name> */
2808 tgl@sss.pgh.pa.us 2678 : 0 : else if (Matches("ALTER", "SERVER", MatchAny))
2679 : 0 : COMPLETE_WITH("VERSION", "OPTIONS", "OWNER TO", "RENAME TO");
2680 : : /* ALTER SERVER <name> VERSION <version> */
2681 : 0 : else if (Matches("ALTER", "SERVER", MatchAny, "VERSION", MatchAny))
2682 : 0 : COMPLETE_WITH("OPTIONS");
2683 : : /* ALTER SYSTEM SET, RESET, RESET ALL */
2684 : 0 : else if (Matches("ALTER", "SYSTEM"))
2685 : 0 : COMPLETE_WITH("SET", "RESET");
2686 : 0 : else if (Matches("ALTER", "SYSTEM", "SET|RESET"))
1571 2687 : 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_alter_system_set_vars,
2688 : : "ALL");
2808 2689 : 0 : else if (Matches("ALTER", "SYSTEM", "SET", MatchAny))
2690 : 0 : COMPLETE_WITH("TO");
2691 : : /* ALTER VIEW <name> */
2692 : 0 : else if (Matches("ALTER", "VIEW", MatchAny))
914 dean.a.rasheed@gmail 2693 : 0 : COMPLETE_WITH("ALTER COLUMN", "OWNER TO", "RENAME", "RESET", "SET");
2694 : : /* ALTER VIEW xxx RENAME */
2382 fujii@postgresql.org 2695 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RENAME"))
1581 tgl@sss.pgh.pa.us 2696 : 0 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "TO");
2382 fujii@postgresql.org 2697 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "ALTER|RENAME", "COLUMN"))
1581 tgl@sss.pgh.pa.us 2698 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2699 : : /* ALTER VIEW xxx ALTER [ COLUMN ] yyy */
1643 michael@paquier.xyz 2700 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "ALTER", MatchAny) ||
2701 : : Matches("ALTER", "VIEW", MatchAny, "ALTER", "COLUMN", MatchAny))
2702 : 0 : COMPLETE_WITH("SET DEFAULT", "DROP DEFAULT");
2703 : : /* ALTER VIEW xxx RENAME yyy */
2382 fujii@postgresql.org 2704 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RENAME", MatchAnyExcept("TO")))
2705 : 0 : COMPLETE_WITH("TO");
2706 : : /* ALTER VIEW xxx RENAME COLUMN yyy */
2707 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RENAME", "COLUMN", MatchAnyExcept("TO")))
2708 : 0 : COMPLETE_WITH("TO");
2709 : : /* ALTER VIEW xxx RESET ( */
914 dean.a.rasheed@gmail 2710 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RESET"))
2711 : 0 : COMPLETE_WITH("(");
2712 : : /* Complete ALTER VIEW xxx SET with "(" or "SCHEMA" */
2713 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET"))
2714 : 0 : COMPLETE_WITH("(", "SCHEMA");
2715 : : /* ALTER VIEW xxx SET|RESET ( yyy [= zzz] ) */
2716 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET|RESET", "("))
2717 : 0 : COMPLETE_WITH_LIST(view_optional_parameters);
2718 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", MatchAny))
2719 : 0 : COMPLETE_WITH("=");
2720 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", "check_option", "="))
2721 : 0 : COMPLETE_WITH("local", "cascaded");
2722 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", "security_barrier|security_invoker", "="))
2723 : 0 : COMPLETE_WITH("true", "false");
2724 : :
2725 : : /* ALTER MATERIALIZED VIEW <name> */
2808 tgl@sss.pgh.pa.us 2726 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny))
2382 fujii@postgresql.org 2727 : 0 : COMPLETE_WITH("ALTER COLUMN", "CLUSTER ON", "DEPENDS ON EXTENSION",
2728 : : "NO DEPENDS ON EXTENSION", "OWNER TO", "RENAME",
2729 : : "RESET (", "SET");
2730 : : /* ALTER MATERIALIZED VIEW xxx RENAME */
2731 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME"))
1581 tgl@sss.pgh.pa.us 2732 : 0 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "TO");
2382 fujii@postgresql.org 2733 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "ALTER|RENAME", "COLUMN"))
1581 tgl@sss.pgh.pa.us 2734 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2735 : : /* ALTER MATERIALIZED VIEW xxx RENAME yyy */
2382 fujii@postgresql.org 2736 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME", MatchAnyExcept("TO")))
2737 : 0 : COMPLETE_WITH("TO");
2738 : : /* ALTER MATERIALIZED VIEW xxx RENAME COLUMN yyy */
2739 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME", "COLUMN", MatchAnyExcept("TO")))
2740 : 0 : COMPLETE_WITH("TO");
2741 : : /* ALTER MATERIALIZED VIEW xxx SET */
2742 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "SET"))
1533 michael@paquier.xyz 2743 : 0 : COMPLETE_WITH("(", "ACCESS METHOD", "SCHEMA", "TABLESPACE", "WITHOUT CLUSTER");
2744 : : /* ALTER MATERIALIZED VIEW xxx SET ACCESS METHOD */
2745 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "SET", "ACCESS", "METHOD"))
2746 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
2747 : :
2748 : : /* ALTER POLICY <name> */
2808 tgl@sss.pgh.pa.us 2749 : 0 : else if (Matches("ALTER", "POLICY"))
3824 rhaas@postgresql.org 2750 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_policies);
2751 : : /* ALTER POLICY <name> ON */
2808 tgl@sss.pgh.pa.us 2752 : 0 : else if (Matches("ALTER", "POLICY", MatchAny))
2753 : 0 : COMPLETE_WITH("ON");
2754 : : /* ALTER POLICY <name> ON <table> */
2755 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON"))
2756 : : {
1581 2757 : 0 : set_completion_reference(prev2_wd);
2758 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
2759 : : }
2760 : : /* ALTER POLICY <name> ON <table> - show options */
2808 2761 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny))
2762 : 0 : COMPLETE_WITH("RENAME TO", "TO", "USING (", "WITH CHECK (");
2763 : : /* ALTER POLICY <name> ON <table> TO <role> */
2764 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "TO"))
1581 2765 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
2766 : : Keywords_for_list_of_grant_roles);
2767 : : /* ALTER POLICY <name> ON <table> USING ( */
2808 2768 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "USING"))
2769 : 0 : COMPLETE_WITH("(");
2770 : : /* ALTER POLICY <name> ON <table> WITH CHECK ( */
2771 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "WITH", "CHECK"))
2772 : 0 : COMPLETE_WITH("(");
2773 : :
2774 : : /* ALTER PROPERTY GRAPH */
75 peter@eisentraut.org 2775 :UNC 0 : else if (Matches("ALTER", "PROPERTY", "GRAPH"))
2776 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_propgraphs);
2777 : 0 : else if (Matches("ALTER", "PROPERTY", "GRAPH", MatchAny))
2778 : 0 : COMPLETE_WITH("ADD", "ALTER", "DROP", "OWNER TO", "RENAME TO", "SET SCHEMA");
2779 : 0 : else if (Matches("ALTER", "PROPERTY", "GRAPH", MatchAny, "ADD|ALTER|DROP"))
2780 : 0 : COMPLETE_WITH("VERTEX", "EDGE");
2781 : 0 : else if (Matches("ALTER", "PROPERTY", "GRAPH", MatchAny, "ADD|DROP", "VERTEX|EDGE"))
2782 : 0 : COMPLETE_WITH("TABLES");
2783 [ # # ]: 0 : else if (HeadMatches("ALTER", "PROPERTY", "GRAPH", MatchAny, "ADD") && TailMatches("EDGE"))
2784 : 0 : COMPLETE_WITH("TABLES");
2785 : 0 : else if (Matches("ALTER", "PROPERTY", "GRAPH", MatchAny, "ALTER", "VERTEX|EDGE"))
2786 : 0 : COMPLETE_WITH("TABLE");
2787 : :
2788 : : /* ALTER RULE <name>, add ON */
2808 tgl@sss.pgh.pa.us 2789 :UBC 0 : else if (Matches("ALTER", "RULE", MatchAny))
2790 : 0 : COMPLETE_WITH("ON");
2791 : :
2792 : : /* If we have ALTER RULE <name> ON, then add the correct tablename */
2793 : 0 : else if (Matches("ALTER", "RULE", MatchAny, "ON"))
2794 : : {
1581 2795 : 0 : set_completion_reference(prev2_wd);
2796 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
2797 : : }
2798 : :
2799 : : /* ALTER RULE <name> ON <name> */
2808 2800 : 0 : else if (Matches("ALTER", "RULE", MatchAny, "ON", MatchAny))
2801 : 0 : COMPLETE_WITH("RENAME TO");
2802 : :
2803 : : /* ALTER STATISTICS <name> */
2804 : 0 : else if (Matches("ALTER", "STATISTICS", MatchAny))
2454 tomas.vondra@postgre 2805 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA", "SET STATISTICS");
2806 : : /* ALTER STATISTICS <name> SET */
1314 michael@paquier.xyz 2807 : 0 : else if (Matches("ALTER", "STATISTICS", MatchAny, "SET"))
2808 : 0 : COMPLETE_WITH("SCHEMA", "STATISTICS");
2809 : :
2810 : : /* ALTER TRIGGER <name>, add ON */
2808 tgl@sss.pgh.pa.us 2811 : 0 : else if (Matches("ALTER", "TRIGGER", MatchAny))
2812 : 0 : COMPLETE_WITH("ON");
2813 : :
1581 2814 : 0 : else if (Matches("ALTER", "TRIGGER", MatchAny, "ON"))
2815 : : {
2816 : 0 : set_completion_reference(prev2_wd);
2817 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
2818 : : }
2819 : :
2820 : : /* ALTER TRIGGER <name> ON <name> */
2808 2821 : 0 : else if (Matches("ALTER", "TRIGGER", MatchAny, "ON", MatchAny))
1928 michael@paquier.xyz 2822 : 0 : COMPLETE_WITH("RENAME TO", "DEPENDS ON EXTENSION",
2823 : : "NO DEPENDS ON EXTENSION");
2824 : :
2825 : : /*
2826 : : * If we detect ALTER TABLE <name>, suggest sub commands
2827 : : */
2808 tgl@sss.pgh.pa.us 2828 : 0 : else if (Matches("ALTER", "TABLE", MatchAny))
2829 : 0 : COMPLETE_WITH("ADD", "ALTER", "CLUSTER ON", "DISABLE", "DROP",
2830 : : "ENABLE", "INHERIT", "NO", "RENAME", "RESET",
2831 : : "OWNER TO", "SET", "VALIDATE CONSTRAINT",
2832 : : "REPLICA IDENTITY", "ATTACH PARTITION",
2833 : : "DETACH PARTITION", "FORCE ROW LEVEL SECURITY",
2834 : : "SPLIT PARTITION", "MERGE PARTITIONS (",
2835 : : "OF", "NOT OF");
2836 : : /* ALTER TABLE xxx ADD */
1734 michael@paquier.xyz 2837 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD"))
2838 : : {
2839 : : /*
2840 : : * make sure to keep this list and the MatchAnyExcept() below in sync
2841 : : */
331 alvherre@kurilemu.de 2842 :UNC 0 : COMPLETE_WITH("COLUMN", "CONSTRAINT", "CHECK (", "NOT NULL", "UNIQUE",
2843 : : "PRIMARY KEY", "EXCLUDE", "FOREIGN KEY");
2844 : : }
2845 : : /* ALTER TABLE xxx ADD [COLUMN] yyy */
1733 michael@paquier.xyz 2846 :UBC 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny) ||
2847 : : Matches("ALTER", "TABLE", MatchAny, "ADD", MatchAnyExcept("COLUMN|CONSTRAINT|CHECK|UNIQUE|PRIMARY|NOT|EXCLUDE|FOREIGN")))
1581 tgl@sss.pgh.pa.us 2848 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2849 : : /* ALTER TABLE xxx ADD CONSTRAINT yyy */
1734 michael@paquier.xyz 2850 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny))
331 alvherre@kurilemu.de 2851 :UNC 0 : COMPLETE_WITH("CHECK (", "NOT NULL", "UNIQUE", "PRIMARY KEY", "EXCLUDE", "FOREIGN KEY");
2852 : : /* ALTER TABLE xxx ADD NOT NULL */
2853 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "NOT", "NULL"))
2854 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
2855 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "NOT", "NULL"))
2856 : 0 : COMPLETE_WITH_ATTR(prev6_wd);
2857 : : /* ALTER TABLE xxx ADD [CONSTRAINT yyy] (PRIMARY KEY|UNIQUE) */
1734 michael@paquier.xyz 2858 :UBC 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "PRIMARY", "KEY") ||
2859 : : Matches("ALTER", "TABLE", MatchAny, "ADD", "UNIQUE") ||
2860 : : Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "PRIMARY", "KEY") ||
2861 : : Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "UNIQUE"))
2862 : 0 : COMPLETE_WITH("(", "USING INDEX");
2863 : : /* ALTER TABLE xxx ADD PRIMARY KEY USING INDEX */
2864 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "PRIMARY", "KEY", "USING", "INDEX"))
2865 : : {
1581 tgl@sss.pgh.pa.us 2866 : 0 : set_completion_reference(prev6_wd);
2867 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2868 : : }
2869 : : /* ALTER TABLE xxx ADD UNIQUE USING INDEX */
1734 michael@paquier.xyz 2870 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "UNIQUE", "USING", "INDEX"))
2871 : : {
1581 tgl@sss.pgh.pa.us 2872 : 0 : set_completion_reference(prev5_wd);
2873 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2874 : : }
2875 : : /* ALTER TABLE xxx ADD CONSTRAINT yyy PRIMARY KEY USING INDEX */
1734 michael@paquier.xyz 2876 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny,
2877 : : "PRIMARY", "KEY", "USING", "INDEX"))
2878 : : {
1581 tgl@sss.pgh.pa.us 2879 : 0 : set_completion_reference(prev8_wd);
2880 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2881 : : }
2882 : : /* ALTER TABLE xxx ADD CONSTRAINT yyy UNIQUE USING INDEX */
1734 michael@paquier.xyz 2883 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny,
2884 : : "UNIQUE", "USING", "INDEX"))
2885 : : {
1581 tgl@sss.pgh.pa.us 2886 : 0 : set_completion_reference(prev7_wd);
2887 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2888 : : }
2889 : : /* ALTER TABLE xxx ENABLE */
2808 2890 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE"))
2891 : 0 : COMPLETE_WITH("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE",
2892 : : "TRIGGER");
2893 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "REPLICA|ALWAYS"))
2894 : 0 : COMPLETE_WITH("RULE", "TRIGGER");
2895 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "RULE"))
2896 : : {
1581 2897 : 0 : set_completion_reference(prev3_wd);
2898 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2899 : : }
2808 2900 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "RULE"))
2901 : : {
1581 2902 : 0 : set_completion_reference(prev4_wd);
2903 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2904 : : }
2808 2905 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "TRIGGER"))
2906 : : {
1581 2907 : 0 : set_completion_reference(prev3_wd);
2908 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2909 : : }
2808 2910 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "TRIGGER"))
2911 : : {
1581 2912 : 0 : set_completion_reference(prev4_wd);
2913 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2914 : : }
2915 : : /* ALTER TABLE xxx INHERIT */
2808 2916 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "INHERIT"))
1581 2917 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2918 : : /* ALTER TABLE xxx NO */
2044 michael@paquier.xyz 2919 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "NO"))
2920 : 0 : COMPLETE_WITH("FORCE ROW LEVEL SECURITY", "INHERIT");
2921 : : /* ALTER TABLE xxx NO INHERIT */
2808 tgl@sss.pgh.pa.us 2922 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "NO", "INHERIT"))
1581 2923 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2924 : : /* ALTER TABLE xxx DISABLE */
2808 2925 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE"))
2926 : 0 : COMPLETE_WITH("ROW LEVEL SECURITY", "RULE", "TRIGGER");
2927 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE", "RULE"))
2928 : : {
1581 2929 : 0 : set_completion_reference(prev3_wd);
2930 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2931 : : }
2808 2932 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE", "TRIGGER"))
2933 : : {
1581 2934 : 0 : set_completion_reference(prev3_wd);
2935 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2936 : : }
2937 : :
2938 : : /* ALTER TABLE xxx ALTER */
2808 2939 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER"))
1581 2940 : 0 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "CONSTRAINT");
2941 : :
2942 : : /* ALTER TABLE xxx RENAME */
2808 2943 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RENAME"))
1581 tgl@sss.pgh.pa.us 2944 :CBC 12 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "CONSTRAINT", "TO");
2808 2945 : 12 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|RENAME", "COLUMN"))
1581 tgl@sss.pgh.pa.us 2946 :UBC 0 : COMPLETE_WITH_ATTR(prev3_wd);
2947 : :
2948 : : /* ALTER TABLE xxx RENAME yyy */
2808 2949 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RENAME", MatchAnyExcept("CONSTRAINT|TO")))
2950 : 0 : COMPLETE_WITH("TO");
2951 : :
2952 : : /* ALTER TABLE xxx RENAME COLUMN/CONSTRAINT yyy */
2953 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RENAME", "COLUMN|CONSTRAINT", MatchAnyExcept("TO")))
2954 : 0 : COMPLETE_WITH("TO");
2955 : :
2956 : : /* If we have ALTER TABLE <sth> DROP, provide COLUMN or CONSTRAINT */
2957 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DROP"))
2958 : 0 : COMPLETE_WITH("COLUMN", "CONSTRAINT");
2959 : : /* If we have ALTER TABLE <sth> DROP COLUMN, provide list of columns */
2960 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DROP", "COLUMN"))
1581 2961 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2962 : : /* ALTER TABLE <sth> ALTER|DROP|RENAME CONSTRAINT <constraint> */
1608 2963 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME", "CONSTRAINT"))
2964 : : {
1581 tgl@sss.pgh.pa.us 2965 :CBC 3 : set_completion_reference(prev3_wd);
2966 : 3 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_table);
2967 : : }
2968 : : /* ALTER TABLE <sth> VALIDATE CONSTRAINT <non-validated constraint> */
1608 2969 : 3 : else if (Matches("ALTER", "TABLE", MatchAny, "VALIDATE", "CONSTRAINT"))
2970 : : {
1581 tgl@sss.pgh.pa.us 2971 :UBC 0 : set_completion_reference(prev3_wd);
2972 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_table_not_validated);
2973 : : }
2974 : : /* ALTER TABLE ALTER [COLUMN] <foo> */
2808 2975 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny) ||
2976 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny))
2977 : 0 : COMPLETE_WITH("TYPE", "SET", "RESET", "RESTART", "ADD", "DROP");
2978 : : /* ALTER TABLE ALTER [COLUMN] <foo> ADD */
1306 peter@eisentraut.org 2979 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD") ||
2980 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD"))
2981 : 0 : COMPLETE_WITH("GENERATED");
2982 : : /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED */
2983 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED") ||
2984 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED"))
2985 : 0 : COMPLETE_WITH("ALWAYS", "BY DEFAULT");
2986 : : /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED */
2987 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED", "ALWAYS") ||
2988 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED", "ALWAYS") ||
2989 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED", "BY", "DEFAULT") ||
2990 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED", "BY", "DEFAULT"))
2991 : 0 : COMPLETE_WITH("AS IDENTITY");
2992 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET */
2808 tgl@sss.pgh.pa.us 2993 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET") ||
2994 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET"))
793 msawada@postgresql.o 2995 : 0 : COMPLETE_WITH("(", "COMPRESSION", "DATA TYPE", "DEFAULT", "EXPRESSION", "GENERATED", "NOT NULL",
2996 : : "STATISTICS", "STORAGE",
2997 : : /* a subset of ALTER SEQUENCE options */
2998 : : "INCREMENT", "MINVALUE", "MAXVALUE", "START", "NO", "CACHE", "CYCLE");
2999 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET ( */
2808 tgl@sss.pgh.pa.us 3000 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "(") ||
3001 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "("))
3002 : 0 : COMPLETE_WITH("n_distinct", "n_distinct_inherited");
3003 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET COMPRESSION */
1362 michael@paquier.xyz 3004 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "COMPRESSION") ||
3005 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "COMPRESSION"))
3006 : 0 : COMPLETE_WITH("DEFAULT", "PGLZ", "LZ4");
3007 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET EXPRESSION */
877 peter@eisentraut.org 3008 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "EXPRESSION") ||
3009 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "EXPRESSION"))
3010 : 0 : COMPLETE_WITH("AS");
3011 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET EXPRESSION AS */
3012 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "EXPRESSION", "AS") ||
3013 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "EXPRESSION", "AS"))
3014 : 0 : COMPLETE_WITH("(");
3015 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET GENERATED */
1306 3016 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "GENERATED") ||
3017 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "GENERATED"))
3018 : 0 : COMPLETE_WITH("ALWAYS", "BY DEFAULT");
3019 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET NO */
3020 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "NO") ||
3021 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "NO"))
3022 : 0 : COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
3023 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET STORAGE */
2808 tgl@sss.pgh.pa.us 3024 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STORAGE") ||
3025 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STORAGE"))
1297 3026 : 0 : COMPLETE_WITH("DEFAULT", "PLAIN", "EXTERNAL", "EXTENDED", "MAIN");
3027 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET STATISTICS */
2713 michael@paquier.xyz 3028 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STATISTICS") ||
3029 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STATISTICS"))
3030 : : {
3031 : : /* Enforce no completion here, as an integer has to be specified */
3032 : : }
3033 : : /* ALTER TABLE ALTER [COLUMN] <foo> DROP */
2808 tgl@sss.pgh.pa.us 3034 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "DROP") ||
3035 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "DROP"))
2328 peter@eisentraut.org 3036 : 0 : COMPLETE_WITH("DEFAULT", "EXPRESSION", "IDENTITY", "NOT NULL");
2808 tgl@sss.pgh.pa.us 3037 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "CLUSTER"))
3038 : 0 : COMPLETE_WITH("ON");
3039 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "CLUSTER", "ON"))
3040 : : {
1581 3041 : 0 : set_completion_reference(prev3_wd);
3042 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
3043 : : }
3044 : : /* If we have ALTER TABLE <sth> SET, provide list of attributes and '(' */
2808 3045 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET"))
1767 michael@paquier.xyz 3046 : 0 : COMPLETE_WITH("(", "ACCESS METHOD", "LOGGED", "SCHEMA",
3047 : : "TABLESPACE", "UNLOGGED", "WITH", "WITHOUT");
3048 : :
3049 : : /*
3050 : : * If we have ALTER TABLE <sth> SET ACCESS METHOD provide a list of table
3051 : : * AMs.
3052 : : */
3053 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET", "ACCESS", "METHOD"))
813 3054 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_table_access_methods,
3055 : : "DEFAULT");
3056 : :
3057 : : /*
3058 : : * If we have ALTER TABLE <sth> SET TABLESPACE provide a list of
3059 : : * tablespaces
3060 : : */
2808 tgl@sss.pgh.pa.us 3061 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET", "TABLESPACE"))
7953 bruce@momjian.us 3062 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
3063 : : /* If we have ALTER TABLE <sth> SET WITHOUT provide CLUSTER or OIDS */
2808 tgl@sss.pgh.pa.us 3064 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET", "WITHOUT"))
3065 : 0 : COMPLETE_WITH("CLUSTER", "OIDS");
3066 : : /* ALTER TABLE <foo> RESET */
3067 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RESET"))
3068 : 0 : COMPLETE_WITH("(");
3069 : : /* ALTER TABLE <foo> SET|RESET ( */
3070 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET|RESET", "("))
2715 michael@paquier.xyz 3071 : 0 : COMPLETE_WITH_LIST(table_storage_parameters);
2808 tgl@sss.pgh.pa.us 3072 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING", "INDEX"))
3073 : : {
1581 3074 : 0 : set_completion_reference(prev5_wd);
3075 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
3076 : : }
2808 3077 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING"))
3078 : 0 : COMPLETE_WITH("INDEX");
3079 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY"))
3080 : 0 : COMPLETE_WITH("FULL", "NOTHING", "DEFAULT", "USING");
3081 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA"))
3082 : 0 : COMPLETE_WITH("IDENTITY");
3083 : :
3084 : : /*
3085 : : * If we have ALTER TABLE <foo> ATTACH PARTITION, provide a list of
3086 : : * tables.
3087 : : */
3088 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ATTACH", "PARTITION"))
1581 3089 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3090 : : /* Limited completion support for partition bound specification */
2808 3091 : 0 : else if (TailMatches("ATTACH", "PARTITION", MatchAny))
3092 : 0 : COMPLETE_WITH("FOR VALUES", "DEFAULT");
3093 : 0 : else if (TailMatches("FOR", "VALUES"))
3094 : 0 : COMPLETE_WITH("FROM (", "IN (", "WITH (");
3095 : :
3096 : : /*
3097 : : * If we have ALTER TABLE <foo> DETACH|SPLIT PARTITION, provide a list of
3098 : : * partitions of <foo>.
3099 : : */
167 akorotkov@postgresql 3100 :UNC 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DETACH|SPLIT", "PARTITION"))
3101 : : {
1581 tgl@sss.pgh.pa.us 3102 :UBC 0 : set_completion_reference(prev3_wd);
3103 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_partition_of_table);
3104 : : }
1864 alvherre@alvh.no-ip. 3105 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DETACH", "PARTITION", MatchAny))
3106 : 0 : COMPLETE_WITH("CONCURRENTLY", "FINALIZE");
3107 : :
3108 : : /* ALTER TABLE <name> SPLIT PARTITION <name> */
167 akorotkov@postgresql 3109 :UNC 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SPLIT", "PARTITION", MatchAny))
3110 : 0 : COMPLETE_WITH("INTO ( PARTITION");
3111 : :
3112 : : /* ALTER TABLE <name> MERGE PARTITIONS ( */
3113 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "MERGE", "PARTITIONS", "("))
3114 : : {
3115 : 0 : set_completion_reference(prev4_wd);
3116 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_partition_of_table);
3117 : : }
3118 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "MERGE", "PARTITIONS", "(*)"))
3119 : 0 : COMPLETE_WITH("INTO");
3120 : :
3121 : : /* ALTER TABLE <name> OF */
1358 michael@paquier.xyz 3122 :UBC 0 : else if (Matches("ALTER", "TABLE", MatchAny, "OF"))
3123 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_composite_datatypes);
3124 : :
3125 : : /* ALTER TABLESPACE <foo> with RENAME TO, OWNER TO, SET, RESET */
2808 tgl@sss.pgh.pa.us 3126 : 0 : else if (Matches("ALTER", "TABLESPACE", MatchAny))
3127 : 0 : COMPLETE_WITH("RENAME TO", "OWNER TO", "SET", "RESET");
3128 : : /* ALTER TABLESPACE <foo> SET|RESET */
3129 : 0 : else if (Matches("ALTER", "TABLESPACE", MatchAny, "SET|RESET"))
3130 : 0 : COMPLETE_WITH("(");
3131 : : /* ALTER TABLESPACE <foo> SET|RESET ( */
3132 : 0 : else if (Matches("ALTER", "TABLESPACE", MatchAny, "SET|RESET", "("))
3133 : 0 : COMPLETE_WITH("seq_page_cost", "random_page_cost",
3134 : : "effective_io_concurrency", "maintenance_io_concurrency");
3135 : :
3136 : : /* ALTER TEXT SEARCH */
3137 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH"))
3138 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
3139 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH", "TEMPLATE|PARSER", MatchAny))
3140 : 0 : COMPLETE_WITH("RENAME TO", "SET SCHEMA");
3141 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH", "DICTIONARY", MatchAny))
2275 3142 : 0 : COMPLETE_WITH("(", "OWNER TO", "RENAME TO", "SET SCHEMA");
2808 3143 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH", "CONFIGURATION", MatchAny))
3144 : 0 : COMPLETE_WITH("ADD MAPPING FOR", "ALTER MAPPING",
3145 : : "DROP MAPPING FOR",
3146 : : "OWNER TO", "RENAME TO", "SET SCHEMA");
3147 : :
3148 : : /* complete ALTER TYPE <foo> with actions */
3149 : 0 : else if (Matches("ALTER", "TYPE", MatchAny))
3150 : 0 : COMPLETE_WITH("ADD ATTRIBUTE", "ADD VALUE", "ALTER ATTRIBUTE",
3151 : : "DROP ATTRIBUTE",
3152 : : "OWNER TO", "RENAME", "SET SCHEMA", "SET (");
3153 : : /* complete ALTER TYPE <foo> ADD with actions */
3154 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ADD"))
3155 : 0 : COMPLETE_WITH("ATTRIBUTE", "VALUE");
3156 : : /* ALTER TYPE <foo> RENAME */
3157 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME"))
3158 : 0 : COMPLETE_WITH("ATTRIBUTE", "TO", "VALUE");
3159 : : /* ALTER TYPE xxx RENAME (ATTRIBUTE|VALUE) yyy */
3160 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE|VALUE", MatchAny))
3161 : 0 : COMPLETE_WITH("TO");
3162 : : /* ALTER TYPE xxx RENAME ATTRIBUTE yyy TO zzz */
530 tomas.vondra@postgre 3163 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE", MatchAny, "TO", MatchAny))
3164 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3165 : :
3166 : : /*
3167 : : * If we have ALTER TYPE <sth> ALTER/DROP/RENAME ATTRIBUTE, provide list
3168 : : * of attributes
3169 : : */
2808 tgl@sss.pgh.pa.us 3170 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ALTER|DROP|RENAME", "ATTRIBUTE"))
1581 3171 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
3172 : : /* complete ALTER TYPE ADD ATTRIBUTE <foo> with list of types */
530 tomas.vondra@postgre 3173 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ADD", "ATTRIBUTE", MatchAny))
3174 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
3175 : : /* complete ALTER TYPE ADD ATTRIBUTE <foo> <footype> with CASCADE/RESTRICT */
3176 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ADD", "ATTRIBUTE", MatchAny, MatchAny))
3177 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3178 : : /* complete ALTER TYPE DROP ATTRIBUTE <foo> with CASCADE/RESTRICT */
3179 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "DROP", "ATTRIBUTE", MatchAny))
3180 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3181 : : /* ALTER TYPE ALTER ATTRIBUTE <foo> */
2808 tgl@sss.pgh.pa.us 3182 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny))
3183 : 0 : COMPLETE_WITH("TYPE");
3184 : : /* ALTER TYPE ALTER ATTRIBUTE <foo> TYPE <footype> */
530 tomas.vondra@postgre 3185 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny, "TYPE", MatchAny))
3186 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3187 : : /* complete ALTER TYPE <sth> RENAME VALUE with list of enum values */
1384 michael@paquier.xyz 3188 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "VALUE"))
1384 michael@paquier.xyz 3189 [ + - + - :CBC 3 : COMPLETE_WITH_ENUM_VALUE(prev3_wd);
- + ]
3190 : : /* ALTER TYPE <foo> SET */
3191 : 3 : else if (Matches("ALTER", "TYPE", MatchAny, "SET"))
1384 michael@paquier.xyz 3192 :UBC 0 : COMPLETE_WITH("(", "SCHEMA");
3193 : : /* complete ALTER TYPE <foo> SET ( with settable properties */
3194 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "SET", "("))
3195 : 0 : COMPLETE_WITH("ANALYZE", "RECEIVE", "SEND", "STORAGE", "SUBSCRIPT",
3196 : : "TYPMOD_IN", "TYPMOD_OUT");
3197 : :
3198 : : /* complete ALTER GROUP <foo> */
2808 tgl@sss.pgh.pa.us 3199 : 0 : else if (Matches("ALTER", "GROUP", MatchAny))
3200 : 0 : COMPLETE_WITH("ADD USER", "DROP USER", "RENAME TO");
3201 : : /* complete ALTER GROUP <foo> ADD|DROP with USER */
3202 : 0 : else if (Matches("ALTER", "GROUP", MatchAny, "ADD|DROP"))
3203 : 0 : COMPLETE_WITH("USER");
3204 : : /* complete ALTER GROUP <foo> ADD|DROP USER with a user name */
3205 : 0 : else if (Matches("ALTER", "GROUP", MatchAny, "ADD|DROP", "USER"))
7594 3206 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
3207 : :
3208 : : /*
3209 : : * ANALYZE [ ( option [, ...] ) ] [ [ ONLY ] table_and_columns [, ...] ]
3210 : : * ANALYZE [ VERBOSE ] [ [ ONLY ] table_and_columns [, ...] ]
3211 : : */
2808 3212 : 0 : else if (Matches("ANALYZE"))
1581 3213 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
3214 : : "(", "VERBOSE", "ONLY");
433 3215 : 0 : else if (Matches("ANALYZE", "VERBOSE"))
3216 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
3217 : : "ONLY");
2642 michael@paquier.xyz 3218 : 0 : else if (HeadMatches("ANALYZE", "(*") &&
2642 michael@paquier.xyz 3219 [ + - ]:CBC 2 : !HeadMatches("ANALYZE", "(*)"))
3220 : : {
3221 : : /*
3222 : : * This fires if we're in an unfinished parenthesized option list.
3223 : : * get_previous_words treats a completed parenthesized option list as
3224 : : * one word, so the above test is correct.
3225 : : */
3226 [ - + - - ]: 2 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
1149 drowley@postgresql.o 3227 : 2 : COMPLETE_WITH("VERBOSE", "SKIP_LOCKED", "BUFFER_USAGE_LIMIT");
2564 fujii@postgresql.org 3228 [ # # ]:UBC 0 : else if (TailMatches("VERBOSE|SKIP_LOCKED"))
3229 : 0 : COMPLETE_WITH("ON", "OFF");
3230 : : }
157 msawada@postgresql.o 3231 :CBC 2 : else if (Matches("ANALYZE", "(*)"))
157 msawada@postgresql.o 3232 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
3233 : : "ONLY");
600 tgl@sss.pgh.pa.us 3234 : 0 : else if (Matches("ANALYZE", MatchAnyN, "("))
3235 : : /* "ANALYZE (" should be caught above, so assume we want columns */
1581 3236 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
2808 3237 : 0 : else if (HeadMatches("ANALYZE"))
1581 3238 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_analyzables);
3239 : :
3240 : : /* BEGIN */
2808 3241 : 0 : else if (Matches("BEGIN"))
3242 : 0 : COMPLETE_WITH("WORK", "TRANSACTION", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
3243 : : /* END, ABORT */
3244 : 0 : else if (Matches("END|ABORT"))
2624 peter@eisentraut.org 3245 : 0 : COMPLETE_WITH("AND", "WORK", "TRANSACTION");
3246 : : /* COMMIT */
2808 tgl@sss.pgh.pa.us 3247 : 0 : else if (Matches("COMMIT"))
2624 peter@eisentraut.org 3248 : 0 : COMPLETE_WITH("AND", "WORK", "TRANSACTION", "PREPARED");
3249 : : /* RELEASE SAVEPOINT */
2808 tgl@sss.pgh.pa.us 3250 : 0 : else if (Matches("RELEASE"))
3251 : 0 : COMPLETE_WITH("SAVEPOINT");
3252 : : /* ROLLBACK */
3253 : 0 : else if (Matches("ROLLBACK"))
2624 peter@eisentraut.org 3254 : 0 : COMPLETE_WITH("AND", "WORK", "TRANSACTION", "TO SAVEPOINT", "PREPARED");
3255 : 0 : else if (Matches("ABORT|END|COMMIT|ROLLBACK", "AND"))
3256 : 0 : COMPLETE_WITH("CHAIN");
3257 : : /* CALL */
2808 tgl@sss.pgh.pa.us 3258 : 0 : else if (Matches("CALL"))
1581 3259 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_procedures);
2808 3260 : 0 : else if (Matches("CALL", MatchAny))
3261 : 0 : COMPLETE_WITH("(");
3262 : : /* CHECKPOINT */
323 nathan@postgresql.or 3263 :UNC 0 : else if (Matches("CHECKPOINT"))
3264 : 0 : COMPLETE_WITH("(");
3265 : 0 : else if (HeadMatches("CHECKPOINT", "(*") &&
3266 [ # # ]: 0 : !HeadMatches("CHECKPOINT", "(*)"))
3267 : : {
3268 : : /*
3269 : : * This fires if we're in an unfinished parenthesized option list.
3270 : : * get_previous_words treats a completed parenthesized option list as
3271 : : * one word, so the above test is correct.
3272 : : */
3273 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3274 : 0 : COMPLETE_WITH("MODE", "FLUSH_UNLOGGED");
3275 [ # # ]: 0 : else if (TailMatches("MODE"))
3276 : 0 : COMPLETE_WITH("FAST", "SPREAD");
5 dgustafsson@postgres 3277 [ # # ]: 0 : else if (TailMatches("FLUSH_UNLOGGED"))
3278 : 0 : COMPLETE_WITH("ON", "OFF");
3279 : : }
3280 : : /* CLOSE */
1962 fujii@postgresql.org 3281 :UBC 0 : else if (Matches("CLOSE"))
1581 tgl@sss.pgh.pa.us 3282 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
3283 : : "ALL");
3284 : : /* CLUSTER */
2808 3285 : 0 : else if (Matches("CLUSTER"))
1581 3286 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
3287 : : "VERBOSE");
2004 michael@paquier.xyz 3288 : 0 : else if (Matches("CLUSTER", "VERBOSE") ||
3289 : : Matches("CLUSTER", "(*)"))
1581 tgl@sss.pgh.pa.us 3290 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_clusterables);
3291 : : /* If we have CLUSTER <sth>, then add "USING" */
2004 michael@paquier.xyz 3292 : 0 : else if (Matches("CLUSTER", MatchAnyExcept("VERBOSE|ON|(|(*)")))
2808 tgl@sss.pgh.pa.us 3293 : 0 : COMPLETE_WITH("USING");
3294 : : /* If we have CLUSTER VERBOSE <sth>, then add "USING" */
2004 michael@paquier.xyz 3295 : 0 : else if (Matches("CLUSTER", "VERBOSE|(*)", MatchAny))
2808 tgl@sss.pgh.pa.us 3296 : 0 : COMPLETE_WITH("USING");
3297 : : /* If we have CLUSTER <sth> USING, then add the index as well */
3298 : 0 : else if (Matches("CLUSTER", MatchAny, "USING") ||
3299 : : Matches("CLUSTER", "VERBOSE|(*)", MatchAny, "USING"))
3300 : : {
1581 3301 : 0 : set_completion_reference(prev2_wd);
3302 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
3303 : : }
2004 michael@paquier.xyz 3304 : 0 : else if (HeadMatches("CLUSTER", "(*") &&
3305 [ # # ]: 0 : !HeadMatches("CLUSTER", "(*)"))
3306 : : {
3307 : : /*
3308 : : * This fires if we're in an unfinished parenthesized option list.
3309 : : * get_previous_words treats a completed parenthesized option list as
3310 : : * one word, so the above test is correct.
3311 : : */
3312 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3313 : 0 : COMPLETE_WITH("VERBOSE");
3314 : : }
3315 : :
3316 : : /* COMMENT */
2808 tgl@sss.pgh.pa.us 3317 : 0 : else if (Matches("COMMENT"))
3318 : 0 : COMPLETE_WITH("ON");
3319 : 0 : else if (Matches("COMMENT", "ON"))
1667 michael@paquier.xyz 3320 : 0 : COMPLETE_WITH("ACCESS METHOD", "AGGREGATE", "CAST", "COLLATION",
3321 : : "COLUMN", "CONSTRAINT", "CONVERSION", "DATABASE",
3322 : : "DOMAIN", "EXTENSION", "EVENT TRIGGER",
3323 : : "FOREIGN DATA WRAPPER", "FOREIGN TABLE",
3324 : : "FUNCTION", "INDEX", "LANGUAGE", "LARGE OBJECT",
3325 : : "MATERIALIZED VIEW", "OPERATOR", "POLICY",
3326 : : "PROCEDURE", "PROCEDURAL LANGUAGE", "PROPERTY GRAPH", "PUBLICATION", "ROLE",
3327 : : "ROUTINE", "RULE", "SCHEMA", "SEQUENCE", "SERVER",
3328 : : "STATISTICS", "SUBSCRIPTION", "TABLE",
3329 : : "TABLESPACE", "TEXT SEARCH", "TRANSFORM FOR",
3330 : : "TRIGGER", "TYPE", "VIEW");
2808 tgl@sss.pgh.pa.us 3331 : 0 : else if (Matches("COMMENT", "ON", "ACCESS", "METHOD"))
3644 alvherre@alvh.no-ip. 3332 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
2808 tgl@sss.pgh.pa.us 3333 : 0 : else if (Matches("COMMENT", "ON", "CONSTRAINT"))
5006 peter_e@gmx.net 3334 : 0 : COMPLETE_WITH_QUERY(Query_for_all_table_constraints);
2808 tgl@sss.pgh.pa.us 3335 : 0 : else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny))
3336 : 0 : COMPLETE_WITH("ON");
3337 : 0 : else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON"))
3338 : : {
1581 tgl@sss.pgh.pa.us 3339 :CBC 1 : set_completion_reference(prev2_wd);
3340 : 1 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables_for_constraint,
3341 : : "DOMAIN");
3342 : : }
1667 michael@paquier.xyz 3343 : 1 : else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON", "DOMAIN"))
1581 tgl@sss.pgh.pa.us 3344 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
2808 3345 : 0 : else if (Matches("COMMENT", "ON", "EVENT", "TRIGGER"))
4429 rhaas@postgresql.org 3346 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
1667 michael@paquier.xyz 3347 : 0 : else if (Matches("COMMENT", "ON", "FOREIGN"))
3348 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
3349 : 0 : else if (Matches("COMMENT", "ON", "FOREIGN", "TABLE"))
1581 tgl@sss.pgh.pa.us 3350 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
1667 michael@paquier.xyz 3351 : 0 : else if (Matches("COMMENT", "ON", "MATERIALIZED", "VIEW"))
1581 tgl@sss.pgh.pa.us 3352 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
1667 michael@paquier.xyz 3353 : 0 : else if (Matches("COMMENT", "ON", "POLICY"))
3354 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_policies);
3355 : 0 : else if (Matches("COMMENT", "ON", "POLICY", MatchAny))
3356 : 0 : COMPLETE_WITH("ON");
3357 : 0 : else if (Matches("COMMENT", "ON", "POLICY", MatchAny, "ON"))
3358 : : {
1581 tgl@sss.pgh.pa.us 3359 : 0 : set_completion_reference(prev2_wd);
3360 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
3361 : : }
1667 michael@paquier.xyz 3362 : 0 : else if (Matches("COMMENT", "ON", "PROCEDURAL", "LANGUAGE"))
3363 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
75 peter@eisentraut.org 3364 :UNC 0 : else if (Matches("COMMENT", "ON", "PROPERTY", "GRAPH"))
3365 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_propgraphs);
1667 michael@paquier.xyz 3366 :UBC 0 : else if (Matches("COMMENT", "ON", "RULE", MatchAny))
3367 : 0 : COMPLETE_WITH("ON");
3368 : 0 : else if (Matches("COMMENT", "ON", "RULE", MatchAny, "ON"))
3369 : : {
1581 tgl@sss.pgh.pa.us 3370 : 0 : set_completion_reference(prev2_wd);
3371 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
3372 : : }
1667 michael@paquier.xyz 3373 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH"))
3374 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
3375 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "CONFIGURATION"))
1581 tgl@sss.pgh.pa.us 3376 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_configurations);
1667 michael@paquier.xyz 3377 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "DICTIONARY"))
1581 tgl@sss.pgh.pa.us 3378 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_dictionaries);
1667 michael@paquier.xyz 3379 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "PARSER"))
1581 tgl@sss.pgh.pa.us 3380 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_parsers);
1667 michael@paquier.xyz 3381 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "TEMPLATE"))
1581 tgl@sss.pgh.pa.us 3382 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_templates);
1667 michael@paquier.xyz 3383 : 0 : else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR"))
1581 tgl@sss.pgh.pa.us 3384 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
1667 michael@paquier.xyz 3385 : 0 : else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR", MatchAny))
3386 : 0 : COMPLETE_WITH("LANGUAGE");
3387 : 0 : else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
3388 : : {
1581 tgl@sss.pgh.pa.us 3389 : 0 : set_completion_reference(prev2_wd);
1667 michael@paquier.xyz 3390 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3391 : : }
3392 : 0 : else if (Matches("COMMENT", "ON", "TRIGGER", MatchAny))
3393 : 0 : COMPLETE_WITH("ON");
3394 : 0 : else if (Matches("COMMENT", "ON", "TRIGGER", MatchAny, "ON"))
3395 : : {
1581 tgl@sss.pgh.pa.us 3396 : 0 : set_completion_reference(prev2_wd);
3397 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
3398 : : }
2808 3399 : 0 : else if (Matches("COMMENT", "ON", MatchAny, MatchAnyExcept("IS")) ||
3400 : : Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAnyExcept("IS")) ||
3401 : : Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS")) ||
3402 : : Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS")))
3403 : 0 : COMPLETE_WITH("IS");
3404 : :
3405 : : /* COPY */
3406 : :
3407 : : /*
3408 : : * If we have COPY, offer list of tables or "(" (Also cover the analogous
3409 : : * backslash command).
3410 : : */
3411 : 0 : else if (Matches("COPY|\\copy"))
334 fujii@postgresql.org 3412 :UNC 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables_for_copy, "(");
3413 : : /* Complete COPY ( with legal query commands */
2808 tgl@sss.pgh.pa.us 3414 :UBC 0 : else if (Matches("COPY|\\copy", "("))
548 peter@eisentraut.org 3415 : 0 : COMPLETE_WITH("SELECT", "TABLE", "VALUES", "INSERT INTO", "UPDATE", "DELETE FROM", "MERGE INTO", "WITH");
3416 : : /* Complete COPY <sth> */
2139 michael@paquier.xyz 3417 : 0 : else if (Matches("COPY|\\copy", MatchAny))
2808 tgl@sss.pgh.pa.us 3418 : 0 : COMPLETE_WITH("FROM", "TO");
3419 : : /* Complete COPY|\copy <sth> FROM|TO with filename or STDIN/STDOUT/PROGRAM */
207 msawada@postgresql.o 3420 :UNC 0 : else if (Matches("COPY|\\copy", MatchAny, "FROM|TO"))
3421 : : {
143 msawada@postgresql.o 3422 [ + - ]:GNC 4 : if (HeadMatches("COPY"))
3423 : : {
3424 : : /* COPY requires quoted filename */
3425 [ + - ]: 4 : if (TailMatches("FROM"))
3426 : 4 : COMPLETE_WITH_FILES_PLUS("", true, "STDIN", "PROGRAM");
3427 : : else
143 msawada@postgresql.o 3428 :UNC 0 : COMPLETE_WITH_FILES_PLUS("", true, "STDOUT", "PROGRAM");
3429 : : }
3430 : : else
3431 : : {
3432 : : /* \copy supports pstdin and pstdout */
3433 [ # # ]: 0 : if (TailMatches("FROM"))
3434 : 0 : COMPLETE_WITH_FILES_PLUS("", false, "STDIN", "PSTDIN", "PROGRAM");
3435 : : else
3436 : 0 : COMPLETE_WITH_FILES_PLUS("", false, "STDOUT", "PSTDOUT", "PROGRAM");
3437 : : }
3438 : : }
3439 : :
3440 : : /* Complete COPY|\copy <sth> FROM|TO PROGRAM */
207 msawada@postgresql.o 3441 :GNC 4 : else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", "PROGRAM"))
207 msawada@postgresql.o 3442 :UNC 0 : COMPLETE_WITH_FILES("", HeadMatches("COPY")); /* COPY requires quoted
3443 : : * filename */
3444 : :
3445 : : /* Complete COPY <sth> TO [PROGRAM] <sth> */
3446 : 0 : else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAnyExcept("PROGRAM")) ||
3447 : : Matches("COPY|\\copy", MatchAny, "TO", "PROGRAM", MatchAny))
2139 michael@paquier.xyz 3448 :UBC 0 : COMPLETE_WITH("WITH (");
3449 : :
3450 : : /* Complete COPY <sth> FROM [PROGRAM] <sth> */
207 msawada@postgresql.o 3451 :UNC 0 : else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAnyExcept("PROGRAM")) ||
3452 : : Matches("COPY|\\copy", MatchAny, "FROM", "PROGRAM", MatchAny))
2139 michael@paquier.xyz 3453 :UBC 0 : COMPLETE_WITH("WITH (", "WHERE");
3454 : :
3455 : : /* Complete COPY <sth> FROM|TO [PROGRAM] filename WITH ( */
159 msawada@postgresql.o 3456 :UNC 0 : else if (HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", MatchAnyExcept("PROGRAM"), "WITH", "(*") ||
3457 : : HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", "PROGRAM", MatchAny, "WITH", "(*"))
3458 : : {
159 msawada@postgresql.o 3459 [ + - ]:GNC 1 : if (!HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", MatchAnyExcept("PROGRAM"), "WITH", "(*)") &&
3460 [ + - ]: 1 : !HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", "PROGRAM", MatchAny, "WITH", "(*)"))
3461 : : {
3462 : : /*
3463 : : * This fires if we're in an unfinished parenthesized option list.
3464 : : * get_previous_words treats a completed parenthesized option list
3465 : : * as one word, so the above tests are correct.
3466 : : */
3467 : :
3468 [ - + - - ]: 1 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3469 : : {
3470 [ + - ]: 2 : if (HeadMatches("COPY|\\copy", MatchAny, "FROM"))
3471 : 1 : COMPLETE_WITH(Copy_from_options);
3472 : : else
159 msawada@postgresql.o 3473 :UNC 0 : COMPLETE_WITH(Copy_to_options);
3474 : : }
3475 : :
3476 : : /* Complete COPY <sth> FROM|TO filename WITH (FORMAT */
3477 [ # # ]: 0 : else if (TailMatches("FORMAT"))
75 andrew@dunslane.net 3478 : 0 : COMPLETE_WITH("binary", "csv", "text", "json");
3479 : :
3480 : : /* Complete COPY <sth> FROM|TO filename WITH (FREEZE */
159 msawada@postgresql.o 3481 [ # # ]: 0 : else if (TailMatches("FREEZE"))
3482 : 0 : COMPLETE_WITH("true", "false");
3483 : :
3484 : : /* Complete COPY <sth> FROM|TO filename WITH (HEADER */
3485 [ # # ]: 0 : else if (TailMatches("HEADER"))
3486 : 0 : COMPLETE_WITH("true", "false", "MATCH");
3487 : :
3488 : : /* Complete COPY <sth> FROM filename WITH (ON_ERROR */
3489 [ # # ]: 0 : else if (TailMatches("ON_ERROR"))
88 peter@eisentraut.org 3490 : 0 : COMPLETE_WITH("stop", "ignore", "set_null");
3491 : :
3492 : : /* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
159 msawada@postgresql.o 3493 [ # # ]: 0 : else if (TailMatches("LOG_VERBOSITY"))
3494 : 0 : COMPLETE_WITH("silent", "default", "verbose");
3495 : : }
3496 : :
3497 : : /* A completed parenthesized option list should be caught below */
3498 : : }
3499 : :
3500 : : /* Complete COPY <sth> FROM [PROGRAM] <sth> WITH (<options>) */
207 msawada@postgresql.o 3501 :GNC 1 : else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAnyExcept("PROGRAM"), "WITH", MatchAny) ||
3502 : : Matches("COPY|\\copy", MatchAny, "FROM", "PROGRAM", MatchAny, "WITH", MatchAny))
2139 michael@paquier.xyz 3503 :UBC 0 : COMPLETE_WITH("WHERE");
3504 : :
3505 : : /* CREATE ACCESS METHOD */
3506 : : /* Complete "CREATE ACCESS METHOD <name>" */
2808 tgl@sss.pgh.pa.us 3507 : 0 : else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny))
3508 : 0 : COMPLETE_WITH("TYPE");
3509 : : /* Complete "CREATE ACCESS METHOD <name> TYPE" */
3510 : 0 : else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE"))
2553 michael@paquier.xyz 3511 : 0 : COMPLETE_WITH("INDEX", "TABLE");
3512 : : /* Complete "CREATE ACCESS METHOD <name> TYPE <type>" */
2808 tgl@sss.pgh.pa.us 3513 : 0 : else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE", MatchAny))
3514 : 0 : COMPLETE_WITH("HANDLER");
3515 : :
3516 : : /* CREATE COLLATION */
1923 tmunro@postgresql.or 3517 : 0 : else if (Matches("CREATE", "COLLATION", MatchAny))
3518 : 0 : COMPLETE_WITH("(", "FROM");
3519 : 0 : else if (Matches("CREATE", "COLLATION", MatchAny, "FROM"))
1581 tgl@sss.pgh.pa.us 3520 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_collations);
1923 tmunro@postgresql.or 3521 : 0 : else if (HeadMatches("CREATE", "COLLATION", MatchAny, "(*"))
3522 : : {
3523 [ # # ]: 0 : if (TailMatches("(|*,"))
3524 : 0 : COMPLETE_WITH("LOCALE =", "LC_COLLATE =", "LC_CTYPE =",
3525 : : "PROVIDER =", "DETERMINISTIC =");
3526 [ # # ]: 0 : else if (TailMatches("PROVIDER", "="))
3527 : 0 : COMPLETE_WITH("libc", "icu");
3528 [ # # ]: 0 : else if (TailMatches("DETERMINISTIC", "="))
3529 : 0 : COMPLETE_WITH("true", "false");
3530 : : }
3531 : :
3532 : : /* CREATE DATABASE */
2808 tgl@sss.pgh.pa.us 3533 : 0 : else if (Matches("CREATE", "DATABASE", MatchAny))
3534 : 0 : COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
3535 : : "IS_TEMPLATE", "STRATEGY",
3536 : : "ALLOW_CONNECTIONS", "CONNECTION LIMIT",
3537 : : "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID",
3538 : : "LOCALE_PROVIDER", "ICU_LOCALE");
3539 : :
3540 : 0 : else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
6833 bruce@momjian.us 3541 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
1523 rhaas@postgresql.org 3542 : 0 : else if (Matches("CREATE", "DATABASE", MatchAny, "STRATEGY"))
3543 : 0 : COMPLETE_WITH("WAL_LOG", "FILE_COPY");
3544 : :
3545 : : /* CREATE DOMAIN --- is allowed inside CREATE SCHEMA, so use TailMatches */
54 tgl@sss.pgh.pa.us 3546 :UNC 0 : else if (TailMatches("CREATE", "DOMAIN", MatchAny))
1653 michael@paquier.xyz 3547 :UBC 0 : COMPLETE_WITH("AS");
54 tgl@sss.pgh.pa.us 3548 :UNC 0 : else if (TailMatches("CREATE", "DOMAIN", MatchAny, "AS"))
1581 tgl@sss.pgh.pa.us 3549 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
54 tgl@sss.pgh.pa.us 3550 :UNC 0 : else if (TailMatches("CREATE", "DOMAIN", MatchAny, "AS", MatchAny))
1653 michael@paquier.xyz 3551 :UBC 0 : COMPLETE_WITH("COLLATE", "DEFAULT", "CONSTRAINT",
3552 : : "NOT NULL", "NULL", "CHECK (");
54 tgl@sss.pgh.pa.us 3553 :UNC 0 : else if (TailMatches("CREATE", "DOMAIN", MatchAny, "COLLATE"))
1581 tgl@sss.pgh.pa.us 3554 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_collations);
3555 : :
3556 : : /* CREATE EXTENSION */
3557 : : /* Complete with available extensions rather than installed ones. */
2808 3558 : 0 : else if (Matches("CREATE", "EXTENSION"))
5590 3559 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions);
3560 : : /* CREATE EXTENSION <name> */
2808 3561 : 0 : else if (Matches("CREATE", "EXTENSION", MatchAny))
3562 : 0 : COMPLETE_WITH("WITH SCHEMA", "CASCADE", "VERSION");
3563 : : /* CREATE EXTENSION <name> VERSION */
3564 : 0 : else if (Matches("CREATE", "EXTENSION", MatchAny, "VERSION"))
3565 : : {
1581 3566 : 0 : set_completion_reference(prev2_wd);
1439 3567 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions);
3568 : : }
3569 : :
3570 : : /* CREATE FOREIGN */
2808 3571 : 0 : else if (Matches("CREATE", "FOREIGN"))
3572 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
3573 : :
3574 : : /* CREATE FOREIGN DATA WRAPPER */
3575 : 0 : else if (Matches("CREATE", "FOREIGN", "DATA", "WRAPPER", MatchAny))
66 msawada@postgresql.o 3576 :UNC 0 : COMPLETE_WITH("CONNECTION", "HANDLER", "OPTIONS", "VALIDATOR");
3577 : :
3578 : : /* CREATE FOREIGN TABLE */
1596 fujii@postgresql.org 3579 :UBC 0 : else if (Matches("CREATE", "FOREIGN", "TABLE", MatchAny))
3580 : 0 : COMPLETE_WITH("(", "PARTITION OF");
3581 : :
3582 : : /* CREATE INDEX --- is allowed inside CREATE SCHEMA, so use TailMatches */
3583 : : /* First off we complete CREATE UNIQUE with "INDEX" */
2808 tgl@sss.pgh.pa.us 3584 : 0 : else if (TailMatches("CREATE", "UNIQUE"))
3585 : 0 : COMPLETE_WITH("INDEX");
3586 : :
3587 : : /*
3588 : : * If we have CREATE|UNIQUE INDEX, then add "ON", "CONCURRENTLY", and
3589 : : * existing indexes
3590 : : */
3591 : 0 : else if (TailMatches("CREATE|UNIQUE", "INDEX"))
1581 3592 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
3593 : : "ON", "CONCURRENTLY");
3594 : :
3595 : : /*
3596 : : * Complete ... INDEX|CONCURRENTLY [<name>] ON with a list of relations
3597 : : * that indexes can be created on
3598 : : */
2808 3599 : 0 : else if (TailMatches("INDEX|CONCURRENTLY", MatchAny, "ON") ||
3600 : : TailMatches("INDEX|CONCURRENTLY", "ON"))
1581 3601 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables);
3602 : :
3603 : : /*
3604 : : * Complete CREATE|UNIQUE INDEX CONCURRENTLY with "ON" and existing
3605 : : * indexes
3606 : : */
2808 3607 : 0 : else if (TailMatches("CREATE|UNIQUE", "INDEX", "CONCURRENTLY"))
1581 3608 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
3609 : : "ON");
3610 : : /* Complete CREATE|UNIQUE INDEX [CONCURRENTLY] <sth> with "ON" */
2808 3611 : 0 : else if (TailMatches("CREATE|UNIQUE", "INDEX", MatchAny) ||
3612 : : TailMatches("CREATE|UNIQUE", "INDEX", "CONCURRENTLY", MatchAny))
3613 : 0 : COMPLETE_WITH("ON");
3614 : :
3615 : : /*
3616 : : * Complete INDEX <name> ON <table> with a list of table columns (which
3617 : : * should really be in parens)
3618 : : */
3619 : 0 : else if (TailMatches("INDEX", MatchAny, "ON", MatchAny) ||
3620 : : TailMatches("INDEX|CONCURRENTLY", "ON", MatchAny))
3621 : 0 : COMPLETE_WITH("(", "USING");
3622 : 0 : else if (TailMatches("INDEX", MatchAny, "ON", MatchAny, "(") ||
3623 : : TailMatches("INDEX|CONCURRENTLY", "ON", MatchAny, "("))
1581 3624 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
3625 : : /* same if you put in USING */
2808 3626 : 0 : else if (TailMatches("ON", MatchAny, "USING", MatchAny, "("))
1581 3627 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
3628 : : /* Complete USING with an index method */
2808 3629 : 0 : else if (TailMatches("INDEX", MatchAny, MatchAny, "ON", MatchAny, "USING") ||
3630 : : TailMatches("INDEX", MatchAny, "ON", MatchAny, "USING") ||
3631 : : TailMatches("INDEX", "ON", MatchAny, "USING"))
2553 michael@paquier.xyz 3632 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_index_access_methods);
2808 tgl@sss.pgh.pa.us 3633 : 0 : else if (TailMatches("ON", MatchAny, "USING", MatchAny) &&
3634 : : !TailMatches("POLICY", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny) &&
3635 [ # # # # ]: 0 : !TailMatches("FOR", MatchAny, MatchAny, MatchAny))
3636 : 0 : COMPLETE_WITH("(");
3637 : :
3638 : : /* CREATE OR REPLACE */
2451 fujii@postgresql.org 3639 : 0 : else if (Matches("CREATE", "OR"))
3640 : 0 : COMPLETE_WITH("REPLACE");
3641 : :
3642 : : /* CREATE POLICY */
3643 : : /* Complete "CREATE POLICY <name> ON" */
2808 tgl@sss.pgh.pa.us 3644 : 0 : else if (Matches("CREATE", "POLICY", MatchAny))
3645 : 0 : COMPLETE_WITH("ON");
3646 : : /* Complete "CREATE POLICY <name> ON <table>" */
3647 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON"))
1581 3648 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3649 : : /* Complete "CREATE POLICY <name> ON <table> AS|FOR|TO|USING|WITH CHECK" */
2808 3650 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny))
3651 : 0 : COMPLETE_WITH("AS", "FOR", "TO", "USING (", "WITH CHECK (");
3652 : : /* CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE */
3653 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS"))
3654 : 0 : COMPLETE_WITH("PERMISSIVE", "RESTRICTIVE");
3655 : :
3656 : : /*
3657 : : * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE
3658 : : * FOR|TO|USING|WITH CHECK
3659 : : */
3660 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny))
3661 : 0 : COMPLETE_WITH("FOR", "TO", "USING", "WITH CHECK");
3662 : : /* CREATE POLICY <name> ON <table> FOR ALL|SELECT|INSERT|UPDATE|DELETE */
3663 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR"))
3664 : 0 : COMPLETE_WITH("ALL", "SELECT", "INSERT", "UPDATE", "DELETE");
3665 : : /* Complete "CREATE POLICY <name> ON <table> FOR INSERT TO|WITH CHECK" */
3666 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "INSERT"))
3667 : 0 : COMPLETE_WITH("TO", "WITH CHECK (");
3668 : : /* Complete "CREATE POLICY <name> ON <table> FOR SELECT|DELETE TO|USING" */
3669 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "SELECT|DELETE"))
3670 : 0 : COMPLETE_WITH("TO", "USING (");
3671 : : /* CREATE POLICY <name> ON <table> FOR ALL|UPDATE TO|USING|WITH CHECK */
3672 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "ALL|UPDATE"))
3673 : 0 : COMPLETE_WITH("TO", "USING (", "WITH CHECK (");
3674 : : /* Complete "CREATE POLICY <name> ON <table> TO <role>" */
3675 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "TO"))
1581 3676 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3677 : : Keywords_for_list_of_grant_roles);
3678 : : /* Complete "CREATE POLICY <name> ON <table> USING (" */
2808 3679 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "USING"))
3680 : 0 : COMPLETE_WITH("(");
3681 : :
3682 : : /*
3683 : : * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3684 : : * ALL|SELECT|INSERT|UPDATE|DELETE
3685 : : */
3686 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR"))
3687 : 0 : COMPLETE_WITH("ALL", "SELECT", "INSERT", "UPDATE", "DELETE");
3688 : :
3689 : : /*
3690 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3691 : : * INSERT TO|WITH CHECK"
3692 : : */
3693 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "INSERT"))
3694 : 0 : COMPLETE_WITH("TO", "WITH CHECK (");
3695 : :
3696 : : /*
3697 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3698 : : * SELECT|DELETE TO|USING"
3699 : : */
3700 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "SELECT|DELETE"))
3701 : 0 : COMPLETE_WITH("TO", "USING (");
3702 : :
3703 : : /*
3704 : : * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3705 : : * ALL|UPDATE TO|USING|WITH CHECK
3706 : : */
3707 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "ALL|UPDATE"))
3708 : 0 : COMPLETE_WITH("TO", "USING (", "WITH CHECK (");
3709 : :
3710 : : /*
3711 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE TO
3712 : : * <role>"
3713 : : */
3714 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "TO"))
1581 3715 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3716 : : Keywords_for_list_of_grant_roles);
3717 : :
3718 : : /*
3719 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE
3720 : : * USING ("
3721 : : */
2808 3722 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "USING"))
3723 : 0 : COMPLETE_WITH("(");
3724 : :
3725 : : /* CREATE PROPERTY GRAPH */
75 peter@eisentraut.org 3726 :UNC 0 : else if (Matches("CREATE", "PROPERTY"))
3727 : 0 : COMPLETE_WITH("GRAPH");
3728 : 0 : else if (Matches("CREATE", "PROPERTY", "GRAPH", MatchAny))
3729 : 0 : COMPLETE_WITH("VERTEX");
3730 : 0 : else if (Matches("CREATE", "PROPERTY", "GRAPH", MatchAny, "VERTEX|NODE"))
3731 : 0 : COMPLETE_WITH("TABLES");
3732 : 0 : else if (Matches("CREATE", "PROPERTY", "GRAPH", MatchAny, "VERTEX|NODE", "TABLES"))
3733 : 0 : COMPLETE_WITH("(");
3734 : 0 : else if (Matches("CREATE", "PROPERTY", "GRAPH", MatchAny, "VERTEX|NODE", "TABLES", "("))
3735 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3736 : 0 : else if (Matches("CREATE", "PROPERTY", "GRAPH", MatchAny, "VERTEX|NODE", "TABLES", "(*)"))
3737 : 0 : COMPLETE_WITH("EDGE");
3738 [ # # ]: 0 : else if (HeadMatches("CREATE", "PROPERTY", "GRAPH") && TailMatches("EDGE|RELATIONSHIP"))
3739 : 0 : COMPLETE_WITH("TABLES");
3740 [ # # ]: 0 : else if (HeadMatches("CREATE", "PROPERTY", "GRAPH") && TailMatches("EDGE|RELATIONSHIP", "TABLES"))
3741 : 0 : COMPLETE_WITH("(");
3742 [ # # ]: 0 : else if (HeadMatches("CREATE", "PROPERTY", "GRAPH") && TailMatches("EDGE|RELATIONSHIP", "TABLES", "("))
3743 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3744 : :
3745 : : /* CREATE PUBLICATION */
2808 tgl@sss.pgh.pa.us 3746 :UBC 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny))
233 akapila@postgresql.o 3747 :UNC 0 : COMPLETE_WITH("FOR TABLE", "FOR TABLES IN SCHEMA", "FOR ALL TABLES", "FOR ALL SEQUENCES", "WITH (");
2808 tgl@sss.pgh.pa.us 3748 :UBC 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR"))
233 akapila@postgresql.o 3749 :UNC 0 : COMPLETE_WITH("TABLE", "TABLES IN SCHEMA", "ALL TABLES", "ALL SEQUENCES");
1732 fujii@postgresql.org 3750 :UBC 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL"))
233 akapila@postgresql.o 3751 :UNC 0 : COMPLETE_WITH("TABLES", "SEQUENCES");
1514 tomas.vondra@postgre 3752 :UBC 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES"))
60 akapila@postgresql.o 3753 :UNC 0 : COMPLETE_WITH("EXCEPT ( TABLE", "WITH (");
87 3754 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT"))
60 3755 : 0 : COMPLETE_WITH("( TABLE");
3756 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "("))
3757 : 0 : COMPLETE_WITH("TABLE");
3758 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "(", "TABLE"))
87 3759 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
60 3760 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && ends_with(prev_wd, ','))
87 3761 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
60 3762 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && !ends_with(prev_wd, ','))
87 3763 : 0 : COMPLETE_WITH(")");
1338 alvherre@alvh.no-ip. 3764 :UBC 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES"))
3765 : 0 : COMPLETE_WITH("IN SCHEMA");
1514 tomas.vondra@postgre 3766 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE", MatchAny) && !ends_with(prev_wd, ','))
1558 akapila@postgresql.o 3767 : 0 : COMPLETE_WITH("WHERE (", "WITH (");
3768 : : /* Complete "CREATE PUBLICATION <name> FOR TABLE" with "<table>, ..." */
1732 fujii@postgresql.org 3769 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE"))
1581 tgl@sss.pgh.pa.us 3770 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3771 : :
3772 : : /*
3773 : : * "CREATE PUBLICATION <name> FOR TABLE <name> WHERE (" - complete with
3774 : : * table attributes
3775 : : */
600 3776 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE"))
1558 akapila@postgresql.o 3777 : 0 : COMPLETE_WITH("(");
600 tgl@sss.pgh.pa.us 3778 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "("))
1558 akapila@postgresql.o 3779 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
600 tgl@sss.pgh.pa.us 3780 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "(*)"))
1558 akapila@postgresql.o 3781 : 0 : COMPLETE_WITH(" WITH (");
3782 : :
3783 : : /*
3784 : : * Complete "CREATE PUBLICATION <name> FOR TABLES IN SCHEMA <schema>, ..."
3785 : : */
1338 alvherre@alvh.no-ip. 3786 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES", "IN", "SCHEMA"))
1581 tgl@sss.pgh.pa.us 3787 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
3788 : : " AND nspname NOT LIKE E'pg\\\\_%%'",
3789 : : "CURRENT_SCHEMA");
1338 alvherre@alvh.no-ip. 3790 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES", "IN", "SCHEMA", MatchAny) && (!ends_with(prev_wd, ',')))
1676 akapila@postgresql.o 3791 : 0 : COMPLETE_WITH("WITH (");
3792 : : /* Complete "CREATE PUBLICATION <name> [...] WITH" */
600 tgl@sss.pgh.pa.us 3793 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAnyN, "WITH", "("))
569 akapila@postgresql.o 3794 : 0 : COMPLETE_WITH("publish", "publish_generated_columns", "publish_via_partition_root");
3795 : :
3796 : : /* CREATE RULE */
3797 : : /* Complete "CREATE [ OR REPLACE ] RULE <sth>" with "AS ON" */
2451 fujii@postgresql.org 3798 : 0 : else if (Matches("CREATE", "RULE", MatchAny) ||
3799 : : Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny))
2808 tgl@sss.pgh.pa.us 3800 : 0 : COMPLETE_WITH("AS ON");
3801 : : /* Complete "CREATE [ OR REPLACE ] RULE <sth> AS" with "ON" */
2451 fujii@postgresql.org 3802 : 0 : else if (Matches("CREATE", "RULE", MatchAny, "AS") ||
3803 : : Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny, "AS"))
2808 tgl@sss.pgh.pa.us 3804 : 0 : COMPLETE_WITH("ON");
3805 : :
3806 : : /*
3807 : : * Complete "CREATE [ OR REPLACE ] RULE <sth> AS ON" with
3808 : : * SELECT|UPDATE|INSERT|DELETE
3809 : : */
2451 fujii@postgresql.org 3810 : 0 : else if (Matches("CREATE", "RULE", MatchAny, "AS", "ON") ||
3811 : : Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny, "AS", "ON"))
2808 tgl@sss.pgh.pa.us 3812 : 0 : COMPLETE_WITH("SELECT", "UPDATE", "INSERT", "DELETE");
3813 : : /* Complete "AS ON SELECT|UPDATE|INSERT|DELETE" with a "TO" */
3814 : 0 : else if (TailMatches("AS", "ON", "SELECT|UPDATE|INSERT|DELETE"))
3815 : 0 : COMPLETE_WITH("TO");
3816 : : /* Complete "AS ON <sth> TO" with a table name */
3817 : 0 : else if (TailMatches("AS", "ON", "SELECT|UPDATE|INSERT|DELETE", "TO"))
1581 3818 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3819 : :
3820 : : /* CREATE SCHEMA [ <name> ] [ AUTHORIZATION ] */
1065 michael@paquier.xyz 3821 : 0 : else if (Matches("CREATE", "SCHEMA"))
3822 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas,
3823 : : "AUTHORIZATION");
3824 : 0 : else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION") ||
3825 : : Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION"))
3826 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3827 : : Keywords_for_list_of_owner_roles);
3828 : 0 : else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION", MatchAny) ||
3829 : : Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION", MatchAny))
3830 : 0 : COMPLETE_WITH("CREATE", "GRANT");
3831 : 0 : else if (Matches("CREATE", "SCHEMA", MatchAny))
3832 : 0 : COMPLETE_WITH("AUTHORIZATION", "CREATE", "GRANT");
3833 : :
3834 : : /* CREATE SEQUENCE --- is allowed inside CREATE SCHEMA, so use TailMatches */
2808 tgl@sss.pgh.pa.us 3835 : 0 : else if (TailMatches("CREATE", "SEQUENCE", MatchAny) ||
3836 : : TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny))
1653 michael@paquier.xyz 3837 : 0 : COMPLETE_WITH("AS", "INCREMENT BY", "MINVALUE", "MAXVALUE", "NO",
3838 : : "CACHE", "CYCLE", "OWNED BY", "START WITH");
3839 : 0 : else if (TailMatches("CREATE", "SEQUENCE", MatchAny, "AS") ||
3840 : : TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "AS"))
3841 : 0 : COMPLETE_WITH_CS("smallint", "integer", "bigint");
2808 tgl@sss.pgh.pa.us 3842 : 0 : else if (TailMatches("CREATE", "SEQUENCE", MatchAny, "NO") ||
3843 : : TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "NO"))
3844 : 0 : COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
3845 : :
3846 : : /* CREATE SERVER <name> */
3847 : 0 : else if (Matches("CREATE", "SERVER", MatchAny))
3848 : 0 : COMPLETE_WITH("TYPE", "VERSION", "FOREIGN DATA WRAPPER");
3849 : :
3850 : : /* CREATE STATISTICS <name> */
3851 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny))
3852 : 0 : COMPLETE_WITH("(", "ON");
3853 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny, "("))
2621 tomas.vondra@postgre 3854 : 0 : COMPLETE_WITH("ndistinct", "dependencies", "mcv");
2740 3855 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny, "(*)"))
2808 tgl@sss.pgh.pa.us 3856 : 0 : COMPLETE_WITH("ON");
600 3857 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny, MatchAnyN, "FROM"))
1581 3858 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3859 : :
3860 : : /* CREATE TABLE --- is allowed inside CREATE SCHEMA, so use TailMatches */
3861 : : /* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */
2808 3862 : 0 : else if (TailMatches("CREATE", "TEMP|TEMPORARY"))
3863 : 0 : COMPLETE_WITH("SEQUENCE", "TABLE", "VIEW");
3864 : : /* Complete "CREATE UNLOGGED" with TABLE or SEQUENCE */
3865 : 0 : else if (TailMatches("CREATE", "UNLOGGED"))
670 nathan@postgresql.or 3866 : 0 : COMPLETE_WITH("TABLE", "SEQUENCE");
3867 : : /* Complete PARTITION BY with RANGE ( or LIST ( or ... */
2808 tgl@sss.pgh.pa.us 3868 : 0 : else if (TailMatches("PARTITION", "BY"))
3869 : 0 : COMPLETE_WITH("RANGE (", "LIST (", "HASH (");
3870 : : /* If we have xxx PARTITION OF, provide a list of partitioned tables */
3871 : 0 : else if (TailMatches("PARTITION", "OF"))
1581 3872 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables);
3873 : : /* Limited completion support for partition bound specification */
2808 3874 : 0 : else if (TailMatches("PARTITION", "OF", MatchAny))
3875 : 0 : COMPLETE_WITH("FOR VALUES", "DEFAULT");
3876 : : /* Complete CREATE TABLE <name> with '(', AS, OF or PARTITION OF */
2718 michael@paquier.xyz 3877 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny) ||
3878 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny))
926 3879 : 0 : COMPLETE_WITH("(", "AS", "OF", "PARTITION OF");
3880 : : /* Complete CREATE TABLE <name> OF with list of composite types */
2718 3881 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "OF") ||
3882 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "OF"))
1581 tgl@sss.pgh.pa.us 3883 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_composite_datatypes);
3884 : : /* Complete CREATE TABLE <name> [ (...) ] AS with list of keywords */
926 michael@paquier.xyz 3885 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "AS") ||
3886 : : TailMatches("CREATE", "TABLE", MatchAny, "(*)", "AS") ||
3887 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "AS") ||
3888 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "AS"))
3889 : 0 : COMPLETE_WITH("EXECUTE", "SELECT", "TABLE", "VALUES", "WITH");
3890 : : /* Complete CREATE TABLE name (...) with supported options */
353 3891 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)"))
926 3892 : 0 : COMPLETE_WITH("AS", "INHERITS (", "PARTITION BY", "USING", "TABLESPACE", "WITH (");
353 3893 : 0 : else if (TailMatches("CREATE", "UNLOGGED", "TABLE", MatchAny, "(*)"))
3894 : 0 : COMPLETE_WITH("AS", "INHERITS (", "USING", "TABLESPACE", "WITH (");
2718 3895 : 0 : else if (TailMatches("CREATE", "TEMP|TEMPORARY", "TABLE", MatchAny, "(*)"))
530 tomas.vondra@postgre 3896 : 0 : COMPLETE_WITH("AS", "INHERITS (", "ON COMMIT", "PARTITION BY", "USING",
3897 : : "TABLESPACE", "WITH (");
3898 : : /* Complete CREATE TABLE (...) USING with table access methods */
2553 michael@paquier.xyz 3899 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)", "USING") ||
3900 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "USING"))
3901 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
3902 : : /* Complete CREATE TABLE (...) WITH with storage parameters */
2715 3903 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)", "WITH", "(") ||
3904 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "WITH", "("))
3905 : 0 : COMPLETE_WITH_LIST(table_storage_parameters);
3906 : : /* Complete CREATE TABLE ON COMMIT with actions */
2718 3907 : 0 : else if (TailMatches("CREATE", "TEMP|TEMPORARY", "TABLE", MatchAny, "(*)", "ON", "COMMIT"))
3908 : 0 : COMPLETE_WITH("DELETE ROWS", "DROP", "PRESERVE ROWS");
3909 : :
3910 : : /* CREATE TABLESPACE */
2808 tgl@sss.pgh.pa.us 3911 : 0 : else if (Matches("CREATE", "TABLESPACE", MatchAny))
3912 : 0 : COMPLETE_WITH("OWNER", "LOCATION");
3913 : : /* Complete CREATE TABLESPACE name OWNER name with "LOCATION" */
3914 : 0 : else if (Matches("CREATE", "TABLESPACE", MatchAny, "OWNER", MatchAny))
3915 : 0 : COMPLETE_WITH("LOCATION");
3916 : :
3917 : : /* CREATE TEXT SEARCH --- is allowed inside CREATE SCHEMA, so use TailMatches */
54 tgl@sss.pgh.pa.us 3918 :UNC 0 : else if (TailMatches("CREATE", "TEXT", "SEARCH"))
2808 tgl@sss.pgh.pa.us 3919 :UBC 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
54 tgl@sss.pgh.pa.us 3920 :UNC 0 : else if (TailMatches("CREATE", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny))
2808 tgl@sss.pgh.pa.us 3921 :UBC 0 : COMPLETE_WITH("(");
3922 : :
3923 : : /* CREATE TRANSFORM */
1653 michael@paquier.xyz 3924 : 0 : else if (Matches("CREATE", "TRANSFORM") ||
3925 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM"))
3926 : 0 : COMPLETE_WITH("FOR");
3927 : 0 : else if (Matches("CREATE", "TRANSFORM", "FOR") ||
3928 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR"))
1581 tgl@sss.pgh.pa.us 3929 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
1653 michael@paquier.xyz 3930 : 0 : else if (Matches("CREATE", "TRANSFORM", "FOR", MatchAny) ||
3931 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR", MatchAny))
3932 : 0 : COMPLETE_WITH("LANGUAGE");
3933 : 0 : else if (Matches("CREATE", "TRANSFORM", "FOR", MatchAny, "LANGUAGE") ||
3934 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
3935 : : {
1581 tgl@sss.pgh.pa.us 3936 : 0 : set_completion_reference(prev2_wd);
1653 michael@paquier.xyz 3937 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3938 : : }
3939 : :
3940 : : /* CREATE SUBSCRIPTION */
2808 tgl@sss.pgh.pa.us 3941 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny))
66 msawada@postgresql.o 3942 :UNC 0 : COMPLETE_WITH("CONNECTION", "SERVER");
3943 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "SERVER"))
3944 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
85 jdavis@postgresql.or 3945 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "SERVER", MatchAny))
3946 : 0 : COMPLETE_WITH("PUBLICATION");
2808 tgl@sss.pgh.pa.us 3947 :UBC 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION", MatchAny))
3948 : 0 : COMPLETE_WITH("PUBLICATION");
85 jdavis@postgresql.or 3949 :UNC 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "SERVER",
3950 : : MatchAny, "PUBLICATION"))
3951 : : {
3952 : : /* complete with nothing here as this refers to remote publications */
3953 : : }
2808 tgl@sss.pgh.pa.us 3954 :UBC 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION",
3955 : : MatchAny, "PUBLICATION"))
3956 : : {
3957 : : /* complete with nothing here as this refers to remote publications */
3958 : : }
600 3959 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAnyN, "PUBLICATION", MatchAny))
2808 3960 : 0 : COMPLETE_WITH("WITH (");
3961 : : /* Complete "CREATE SUBSCRIPTION <name> ... WITH ( <opt>" */
600 3962 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAnyN, "WITH", "("))
1814 michael@paquier.xyz 3963 : 0 : COMPLETE_WITH("binary", "connect", "copy_data", "create_slot",
3964 : : "disable_on_error", "enabled", "failover",
3965 : : "max_retention_duration", "origin",
3966 : : "password_required", "retain_dead_tuples",
3967 : : "run_as_owner", "slot_name", "streaming",
3968 : : "synchronous_commit", "two_phase");
3969 : :
3970 : : /* CREATE TRIGGER --- is allowed inside CREATE SCHEMA, so use TailMatches */
3971 : :
3972 : : /*
3973 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> with BEFORE|AFTER|INSTEAD
3974 : : * OF.
3975 : : */
2019 3976 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny) ||
3977 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny))
2808 tgl@sss.pgh.pa.us 3978 : 0 : COMPLETE_WITH("BEFORE", "AFTER", "INSTEAD OF");
3979 : :
3980 : : /*
3981 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER with an
3982 : : * event.
3983 : : */
2019 michael@paquier.xyz 3984 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER") ||
3985 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER"))
2808 tgl@sss.pgh.pa.us 3986 : 0 : COMPLETE_WITH("INSERT", "DELETE", "UPDATE", "TRUNCATE");
3987 : : /* Complete CREATE [ OR REPLACE ] TRIGGER <name> INSTEAD OF with an event */
2019 michael@paquier.xyz 3988 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF") ||
3989 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF"))
2808 tgl@sss.pgh.pa.us 3990 : 0 : COMPLETE_WITH("INSERT", "DELETE", "UPDATE");
3991 : :
3992 : : /*
3993 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER sth with
3994 : : * OR|ON.
3995 : : */
3996 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) ||
3997 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) ||
3998 : : TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny) ||
3999 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny))
4000 : 0 : COMPLETE_WITH("ON", "OR");
4001 : :
4002 : : /*
4003 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER event ON
4004 : : * with a list of tables. EXECUTE FUNCTION is the recommended grammar
4005 : : * instead of EXECUTE PROCEDURE in version 11 and upwards.
4006 : : */
2019 michael@paquier.xyz 4007 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON") ||
4008 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON"))
1581 tgl@sss.pgh.pa.us 4009 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
4010 : :
4011 : : /*
4012 : : * Complete CREATE [ OR REPLACE ] TRIGGER ... INSTEAD OF event ON with a
4013 : : * list of views.
4014 : : */
2019 michael@paquier.xyz 4015 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON") ||
4016 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON"))
1581 tgl@sss.pgh.pa.us 4017 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
600 4018 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4019 : : "ON", MatchAny) ||
4020 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4021 : : "ON", MatchAny))
4022 : : {
2773 michael@paquier.xyz 4023 [ # # ]: 0 : if (pset.sversion >= 110000)
4024 : 0 : COMPLETE_WITH("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY",
4025 : : "REFERENCING", "FOR", "WHEN (", "EXECUTE FUNCTION");
4026 : : else
4027 : 0 : COMPLETE_WITH("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY",
4028 : : "REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE");
4029 : : }
600 tgl@sss.pgh.pa.us 4030 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4031 : : "DEFERRABLE") ||
4032 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4033 : : "DEFERRABLE") ||
4034 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4035 : : "INITIALLY", "IMMEDIATE|DEFERRED") ||
4036 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4037 : : "INITIALLY", "IMMEDIATE|DEFERRED"))
4038 : : {
2773 michael@paquier.xyz 4039 [ # # ]: 0 : if (pset.sversion >= 110000)
4040 : 0 : COMPLETE_WITH("REFERENCING", "FOR", "WHEN (", "EXECUTE FUNCTION");
4041 : : else
4042 : 0 : COMPLETE_WITH("REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE");
4043 : : }
600 tgl@sss.pgh.pa.us 4044 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4045 : : "REFERENCING") ||
4046 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4047 : : "REFERENCING"))
2808 4048 : 0 : COMPLETE_WITH("OLD TABLE", "NEW TABLE");
600 4049 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4050 : : "OLD|NEW", "TABLE") ||
4051 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4052 : : "OLD|NEW", "TABLE"))
2808 4053 : 0 : COMPLETE_WITH("AS");
600 4054 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4055 : : "REFERENCING", "OLD", "TABLE", "AS", MatchAny) ||
4056 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4057 : : "REFERENCING", "OLD", "TABLE", "AS", MatchAny) ||
4058 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4059 : : "REFERENCING", "OLD", "TABLE", MatchAny) ||
4060 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4061 : : "REFERENCING", "OLD", "TABLE", MatchAny))
4062 : : {
2773 michael@paquier.xyz 4063 [ # # ]: 0 : if (pset.sversion >= 110000)
4064 : 0 : COMPLETE_WITH("NEW TABLE", "FOR", "WHEN (", "EXECUTE FUNCTION");
4065 : : else
4066 : 0 : COMPLETE_WITH("NEW TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE");
4067 : : }
600 tgl@sss.pgh.pa.us 4068 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4069 : : "REFERENCING", "NEW", "TABLE", "AS", MatchAny) ||
4070 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4071 : : "REFERENCING", "NEW", "TABLE", "AS", MatchAny) ||
4072 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4073 : : "REFERENCING", "NEW", "TABLE", MatchAny) ||
4074 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4075 : : "REFERENCING", "NEW", "TABLE", MatchAny))
4076 : : {
2773 michael@paquier.xyz 4077 [ # # ]: 0 : if (pset.sversion >= 110000)
4078 : 0 : COMPLETE_WITH("OLD TABLE", "FOR", "WHEN (", "EXECUTE FUNCTION");
4079 : : else
4080 : 0 : COMPLETE_WITH("OLD TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE");
4081 : : }
600 tgl@sss.pgh.pa.us 4082 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4083 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4084 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4085 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4086 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4087 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4088 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4089 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4090 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4091 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
4092 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4093 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
4094 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4095 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
4096 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4097 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny))
4098 : : {
2773 michael@paquier.xyz 4099 [ # # ]: 0 : if (pset.sversion >= 110000)
4100 : 0 : COMPLETE_WITH("FOR", "WHEN (", "EXECUTE FUNCTION");
4101 : : else
4102 : 0 : COMPLETE_WITH("FOR", "WHEN (", "EXECUTE PROCEDURE");
4103 : : }
600 tgl@sss.pgh.pa.us 4104 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4105 : : "FOR") ||
4106 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4107 : : "FOR"))
2808 4108 : 0 : COMPLETE_WITH("EACH", "ROW", "STATEMENT");
600 4109 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4110 : : "FOR", "EACH") ||
4111 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4112 : : "FOR", "EACH"))
2808 4113 : 0 : COMPLETE_WITH("ROW", "STATEMENT");
600 4114 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4115 : : "FOR", "EACH", "ROW|STATEMENT") ||
4116 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4117 : : "FOR", "EACH", "ROW|STATEMENT") ||
4118 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4119 : : "FOR", "ROW|STATEMENT") ||
4120 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4121 : : "FOR", "ROW|STATEMENT"))
4122 : : {
2773 michael@paquier.xyz 4123 [ # # ]: 0 : if (pset.sversion >= 110000)
4124 : 0 : COMPLETE_WITH("WHEN (", "EXECUTE FUNCTION");
4125 : : else
4126 : 0 : COMPLETE_WITH("WHEN (", "EXECUTE PROCEDURE");
4127 : : }
600 tgl@sss.pgh.pa.us 4128 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4129 : : "WHEN", "(*)") ||
4130 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4131 : : "WHEN", "(*)"))
4132 : : {
2773 michael@paquier.xyz 4133 [ # # ]: 0 : if (pset.sversion >= 110000)
4134 : 0 : COMPLETE_WITH("EXECUTE FUNCTION");
4135 : : else
4136 : 0 : COMPLETE_WITH("EXECUTE PROCEDURE");
4137 : : }
4138 : :
4139 : : /*
4140 : : * Complete CREATE [ OR REPLACE ] TRIGGER ... EXECUTE with
4141 : : * PROCEDURE|FUNCTION.
4142 : : */
600 tgl@sss.pgh.pa.us 4143 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4144 : : "EXECUTE") ||
4145 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4146 : : "EXECUTE"))
4147 : : {
2773 michael@paquier.xyz 4148 [ # # ]: 0 : if (pset.sversion >= 110000)
4149 : 0 : COMPLETE_WITH("FUNCTION");
4150 : : else
4151 : 0 : COMPLETE_WITH("PROCEDURE");
4152 : : }
600 tgl@sss.pgh.pa.us 4153 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4154 : : "EXECUTE", "FUNCTION|PROCEDURE") ||
4155 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4156 : : "EXECUTE", "FUNCTION|PROCEDURE"))
1581 4157 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
4158 : :
4159 : : /* CREATE ROLE,USER,GROUP <name> */
2808 4160 : 0 : else if (Matches("CREATE", "ROLE|GROUP|USER", MatchAny) &&
4161 [ # # ]: 0 : !TailMatches("USER", "MAPPING"))
4162 : 0 : COMPLETE_WITH("ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB",
4163 : : "CREATEROLE", "ENCRYPTED PASSWORD", "IN", "INHERIT",
4164 : : "LOGIN", "NOBYPASSRLS",
4165 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
4166 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
4167 : : "REPLICATION", "ROLE", "SUPERUSER", "SYSID",
4168 : : "VALID UNTIL", "WITH");
4169 : :
4170 : : /* CREATE ROLE,USER,GROUP <name> WITH */
4171 : 0 : else if (Matches("CREATE", "ROLE|GROUP|USER", MatchAny, "WITH"))
4172 : : /* Similar to the above, but don't complete "WITH" again. */
4173 : 0 : COMPLETE_WITH("ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB",
4174 : : "CREATEROLE", "ENCRYPTED PASSWORD", "IN", "INHERIT",
4175 : : "LOGIN", "NOBYPASSRLS",
4176 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
4177 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
4178 : : "REPLICATION", "ROLE", "SUPERUSER", "SYSID",
4179 : : "VALID UNTIL");
4180 : :
4181 : : /* complete CREATE ROLE,USER,GROUP <name> IN with ROLE,GROUP */
4182 : 0 : else if (Matches("CREATE", "ROLE|USER|GROUP", MatchAny, "IN"))
4183 : 0 : COMPLETE_WITH("GROUP", "ROLE");
4184 : :
4185 : : /* CREATE TYPE */
2513 tmunro@postgresql.or 4186 : 0 : else if (Matches("CREATE", "TYPE", MatchAny))
4187 : 0 : COMPLETE_WITH("(", "AS");
4188 : 0 : else if (Matches("CREATE", "TYPE", MatchAny, "AS"))
4189 : 0 : COMPLETE_WITH("ENUM", "RANGE", "(");
4190 : 0 : else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "("))
4191 : : {
4192 [ # # ]: 0 : if (TailMatches("(|*,", MatchAny))
1581 tgl@sss.pgh.pa.us 4193 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2513 tmunro@postgresql.or 4194 [ # # ]: 0 : else if (TailMatches("(|*,", MatchAny, MatchAnyExcept("*)")))
4195 : 0 : COMPLETE_WITH("COLLATE", ",", ")");
4196 : : }
4197 : 0 : else if (Matches("CREATE", "TYPE", MatchAny, "AS", "ENUM|RANGE"))
4198 : 0 : COMPLETE_WITH("(");
4199 : 0 : else if (HeadMatches("CREATE", "TYPE", MatchAny, "("))
4200 : : {
4201 [ # # ]: 0 : if (TailMatches("(|*,"))
4202 : 0 : COMPLETE_WITH("INPUT", "OUTPUT", "RECEIVE", "SEND",
4203 : : "TYPMOD_IN", "TYPMOD_OUT", "ANALYZE", "SUBSCRIPT",
4204 : : "INTERNALLENGTH", "PASSEDBYVALUE", "ALIGNMENT",
4205 : : "STORAGE", "LIKE", "CATEGORY", "PREFERRED",
4206 : : "DEFAULT", "ELEMENT", "DELIMITER",
4207 : : "COLLATABLE");
4208 [ # # ]: 0 : else if (TailMatches("(*|*,", MatchAnyExcept("*=")))
4209 : 0 : COMPLETE_WITH("=");
4210 [ # # ]: 0 : else if (TailMatches("=", MatchAnyExcept("*)")))
4211 : 0 : COMPLETE_WITH(",", ")");
4212 : : }
4213 : 0 : else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "RANGE", "("))
4214 : : {
4215 [ # # ]: 0 : if (TailMatches("(|*,"))
4216 : 0 : COMPLETE_WITH("SUBTYPE", "SUBTYPE_OPCLASS", "COLLATION",
4217 : : "CANONICAL", "SUBTYPE_DIFF",
4218 : : "MULTIRANGE_TYPE_NAME");
4219 [ # # ]: 0 : else if (TailMatches("(*|*,", MatchAnyExcept("*=")))
4220 : 0 : COMPLETE_WITH("=");
4221 [ # # ]: 0 : else if (TailMatches("=", MatchAnyExcept("*)")))
4222 : 0 : COMPLETE_WITH(",", ")");
4223 : : }
4224 : :
4225 : : /* CREATE VIEW --- is allowed inside CREATE SCHEMA, so use TailMatches */
4226 : : /* Complete CREATE [ OR REPLACE ] VIEW <name> with AS or WITH */
2451 fujii@postgresql.org 4227 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny) ||
4228 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny))
914 dean.a.rasheed@gmail 4229 : 0 : COMPLETE_WITH("AS", "WITH");
4230 : : /* Complete "CREATE [ OR REPLACE ] VIEW <sth> AS with "SELECT" */
2451 fujii@postgresql.org 4231 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "AS") ||
4232 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "AS"))
2808 tgl@sss.pgh.pa.us 4233 : 0 : COMPLETE_WITH("SELECT");
4234 : : /* CREATE [ OR REPLACE ] VIEW <name> WITH ( yyy [= zzz] ) */
914 dean.a.rasheed@gmail 4235 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH") ||
4236 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH"))
4237 : 0 : COMPLETE_WITH("(");
4238 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(") ||
4239 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "("))
4240 : 0 : COMPLETE_WITH_LIST(view_optional_parameters);
4241 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(", "check_option") ||
4242 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(", "check_option"))
4243 : 0 : COMPLETE_WITH("=");
4244 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(", "check_option", "=") ||
4245 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(", "check_option", "="))
4246 : 0 : COMPLETE_WITH("local", "cascaded");
4247 : : /* CREATE [ OR REPLACE ] VIEW <name> WITH ( ... ) AS */
4248 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(*)") ||
4249 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(*)"))
4250 : 0 : COMPLETE_WITH("AS");
4251 : : /* CREATE [ OR REPLACE ] VIEW <name> WITH ( ... ) AS SELECT */
4252 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(*)", "AS") ||
4253 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(*)", "AS"))
4254 : 0 : COMPLETE_WITH("SELECT");
4255 : :
4256 : : /* CREATE MATERIALIZED VIEW */
2808 tgl@sss.pgh.pa.us 4257 : 0 : else if (Matches("CREATE", "MATERIALIZED"))
4258 : 0 : COMPLETE_WITH("VIEW");
4259 : : /* Complete CREATE MATERIALIZED VIEW <name> with AS or USING */
4260 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny))
530 tomas.vondra@postgre 4261 : 0 : COMPLETE_WITH("AS", "USING");
4262 : :
4263 : : /*
4264 : : * Complete CREATE MATERIALIZED VIEW <name> USING with list of access
4265 : : * methods
4266 : : */
4267 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING"))
4268 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
4269 : : /* Complete CREATE MATERIALIZED VIEW <name> USING <access method> with AS */
4270 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny))
2808 tgl@sss.pgh.pa.us 4271 : 0 : COMPLETE_WITH("AS");
4272 : :
4273 : : /*
4274 : : * Complete CREATE MATERIALIZED VIEW <name> [USING <access method> ] AS
4275 : : * with "SELECT"
4276 : : */
530 tomas.vondra@postgre 4277 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "AS") ||
4278 : : Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny, "AS"))
2808 tgl@sss.pgh.pa.us 4279 : 0 : COMPLETE_WITH("SELECT");
4280 : :
4281 : : /* CREATE EVENT TRIGGER */
4282 : 0 : else if (Matches("CREATE", "EVENT"))
4283 : 0 : COMPLETE_WITH("TRIGGER");
4284 : : /* Complete CREATE EVENT TRIGGER <name> with ON */
4285 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny))
4286 : 0 : COMPLETE_WITH("ON");
4287 : : /* Complete CREATE EVENT TRIGGER <name> ON with event_type */
4288 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny, "ON"))
957 akorotkov@postgresql 4289 : 0 : COMPLETE_WITH("ddl_command_start", "ddl_command_end", "login",
4290 : : "sql_drop", "table_rewrite");
4291 : :
4292 : : /*
4293 : : * Complete CREATE EVENT TRIGGER <name> ON <event_type>. EXECUTE FUNCTION
4294 : : * is the recommended grammar instead of EXECUTE PROCEDURE in version 11
4295 : : * and upwards.
4296 : : */
2773 michael@paquier.xyz 4297 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny, "ON", MatchAny))
4298 : : {
4299 [ # # ]: 0 : if (pset.sversion >= 110000)
4300 : 0 : COMPLETE_WITH("WHEN TAG IN (", "EXECUTE FUNCTION");
4301 : : else
4302 : 0 : COMPLETE_WITH("WHEN TAG IN (", "EXECUTE PROCEDURE");
4303 : : }
600 tgl@sss.pgh.pa.us 4304 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAnyN, "WHEN|AND", MatchAny, "IN", "(*)"))
4305 : : {
2773 michael@paquier.xyz 4306 [ # # ]: 0 : if (pset.sversion >= 110000)
4307 : 0 : COMPLETE_WITH("EXECUTE FUNCTION");
4308 : : else
4309 : 0 : COMPLETE_WITH("EXECUTE PROCEDURE");
4310 : : }
600 tgl@sss.pgh.pa.us 4311 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAnyN, "EXECUTE", "FUNCTION|PROCEDURE"))
1581 4312 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
4313 : :
4314 : : /* DEALLOCATE */
2808 4315 : 0 : else if (Matches("DEALLOCATE"))
1581 4316 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_prepared_statements,
4317 : : "ALL");
4318 : :
4319 : : /* DECLARE */
4320 : :
4321 : : /*
4322 : : * Complete DECLARE <name> with one of BINARY, ASENSITIVE, INSENSITIVE,
4323 : : * SCROLL, NO SCROLL, and CURSOR.
4324 : : */
2808 4325 : 0 : else if (Matches("DECLARE", MatchAny))
1879 peter@eisentraut.org 4326 : 0 : COMPLETE_WITH("BINARY", "ASENSITIVE", "INSENSITIVE", "SCROLL", "NO SCROLL",
4327 : : "CURSOR");
4328 : :
4329 : : /*
4330 : : * Complete DECLARE ... <option> with other options. The PostgreSQL parser
4331 : : * allows DECLARE options to be specified in any order. But the
4332 : : * tab-completion follows the ordering of them that the SQL standard
4333 : : * provides, like the syntax of DECLARE command in the documentation
4334 : : * indicates.
4335 : : */
600 tgl@sss.pgh.pa.us 4336 : 0 : else if (Matches("DECLARE", MatchAnyN, "BINARY"))
1754 michael@paquier.xyz 4337 : 0 : COMPLETE_WITH("ASENSITIVE", "INSENSITIVE", "SCROLL", "NO SCROLL", "CURSOR");
600 tgl@sss.pgh.pa.us 4338 : 0 : else if (Matches("DECLARE", MatchAnyN, "ASENSITIVE|INSENSITIVE"))
1962 fujii@postgresql.org 4339 : 0 : COMPLETE_WITH("SCROLL", "NO SCROLL", "CURSOR");
600 tgl@sss.pgh.pa.us 4340 : 0 : else if (Matches("DECLARE", MatchAnyN, "SCROLL"))
1962 fujii@postgresql.org 4341 : 0 : COMPLETE_WITH("CURSOR");
4342 : : /* Complete DECLARE ... [options] NO with SCROLL */
600 tgl@sss.pgh.pa.us 4343 : 0 : else if (Matches("DECLARE", MatchAnyN, "NO"))
1962 fujii@postgresql.org 4344 : 0 : COMPLETE_WITH("SCROLL");
4345 : :
4346 : : /*
4347 : : * Complete DECLARE ... CURSOR with one of WITH HOLD, WITHOUT HOLD, and
4348 : : * FOR
4349 : : */
600 tgl@sss.pgh.pa.us 4350 : 0 : else if (Matches("DECLARE", MatchAnyN, "CURSOR"))
2808 4351 : 0 : COMPLETE_WITH("WITH HOLD", "WITHOUT HOLD", "FOR");
4352 : : /* Complete DECLARE ... CURSOR WITH|WITHOUT with HOLD */
600 4353 : 0 : else if (Matches("DECLARE", MatchAnyN, "CURSOR", "WITH|WITHOUT"))
1962 fujii@postgresql.org 4354 : 0 : COMPLETE_WITH("HOLD");
4355 : : /* Complete DECLARE ... CURSOR WITH|WITHOUT HOLD with FOR */
600 tgl@sss.pgh.pa.us 4356 : 0 : else if (Matches("DECLARE", MatchAnyN, "CURSOR", "WITH|WITHOUT", "HOLD"))
1962 fujii@postgresql.org 4357 : 0 : COMPLETE_WITH("FOR");
4358 : :
4359 : : /* DELETE --- can be inside EXPLAIN, RULE, etc */
4360 : : /* Complete DELETE with "FROM" */
2808 tgl@sss.pgh.pa.us 4361 : 0 : else if (Matches("DELETE"))
4362 : 0 : COMPLETE_WITH("FROM");
4363 : : /* Complete DELETE FROM with a list of tables */
4364 : 0 : else if (TailMatches("DELETE", "FROM"))
1581 4365 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
4366 : : /* Complete DELETE FROM <table> */
2808 4367 : 0 : else if (TailMatches("DELETE", "FROM", MatchAny))
18 peter@eisentraut.org 4368 :GNC 1 : COMPLETE_WITH("FOR", "USING", "WHERE");
4369 : : /* Complete DELETE FROM <table> FOR with PORTION */
4370 : 1 : else if (TailMatches("DELETE", "FROM", MatchAny, "FOR"))
4371 : 1 : COMPLETE_WITH("PORTION");
4372 : : /* Complete DELETE FROM <table> FOR PORTION with OF */
4373 : 1 : else if (TailMatches("DELETE", "FROM", MatchAny, "FOR", "PORTION"))
4374 : 1 : COMPLETE_WITH("OF");
4375 : : /* Complete DELETE FROM <table> FOR PORTION OF with column names */
4376 : 1 : else if (TailMatches("DELETE", "FROM", MatchAny, "FOR", "PORTION", "OF"))
4377 : 1 : COMPLETE_WITH_ATTR(prev4_wd);
4378 : : /* Complete DELETE FROM <table> FOR PORTION OF <period> with FROM */
4379 : 1 : else if (TailMatches("DELETE", "FROM", MatchAny, "FOR", "PORTION", "OF", MatchAny))
4380 : 1 : COMPLETE_WITH("FROM");
4381 : : /* Complete DELETE FROM <table> USING with relations supporting SELECT */
89 fujii@postgresql.org 4382 : 1 : else if (TailMatches("DELETE", "FROM", MatchAny, "USING"))
89 fujii@postgresql.org 4383 :UNC 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
4384 : :
4385 : : /* DISCARD */
2808 tgl@sss.pgh.pa.us 4386 :UBC 0 : else if (Matches("DISCARD"))
4387 : 0 : COMPLETE_WITH("ALL", "PLANS", "SEQUENCES", "TEMP");
4388 : :
4389 : : /* DO */
4390 : 0 : else if (Matches("DO"))
4391 : 0 : COMPLETE_WITH("LANGUAGE");
4392 : :
4393 : : /* DROP */
4394 : : /* Complete DROP object with CASCADE / RESTRICT */
4395 : 0 : else if (Matches("DROP",
4396 : : "COLLATION|CONVERSION|DOMAIN|EXTENSION|LANGUAGE|PUBLICATION|SCHEMA|SEQUENCE|SERVER|SUBSCRIPTION|STATISTICS|TABLE|TYPE|VIEW",
4397 : : MatchAny) ||
4398 : : Matches("DROP", "ACCESS", "METHOD", MatchAny) ||
4399 : : Matches("DROP", "EVENT", "TRIGGER", MatchAny) ||
4400 : : Matches("DROP", "FOREIGN", "DATA", "WRAPPER", MatchAny) ||
4401 : : Matches("DROP", "FOREIGN", "TABLE", MatchAny) ||
4402 : : Matches("DROP", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny))
2808 tgl@sss.pgh.pa.us 4403 :CBC 1 : COMPLETE_WITH("CASCADE", "RESTRICT");
600 4404 : 1 : else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny) &&
600 tgl@sss.pgh.pa.us 4405 [ # # ]:UBC 0 : ends_with(prev_wd, ')'))
4406 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4407 : :
4408 : : /* help completing some of the variants */
2808 4409 : 0 : else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny))
4410 : 0 : COMPLETE_WITH("(");
4411 : 0 : else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny, "("))
3815 4412 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2808 4413 : 0 : else if (Matches("DROP", "FOREIGN"))
4414 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
2391 akapila@postgresql.o 4415 : 0 : else if (Matches("DROP", "DATABASE", MatchAny))
4416 : 0 : COMPLETE_WITH("WITH (");
4417 [ # # ]: 0 : else if (HeadMatches("DROP", "DATABASE") && (ends_with(prev_wd, '(')))
4418 : 0 : COMPLETE_WITH("FORCE");
4419 : :
4420 : : /* DROP INDEX */
2808 tgl@sss.pgh.pa.us 4421 : 0 : else if (Matches("DROP", "INDEX"))
1581 4422 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
4423 : : "CONCURRENTLY");
2808 4424 : 0 : else if (Matches("DROP", "INDEX", "CONCURRENTLY"))
1581 4425 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
2808 4426 : 0 : else if (Matches("DROP", "INDEX", MatchAny))
4427 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4428 : 0 : else if (Matches("DROP", "INDEX", "CONCURRENTLY", MatchAny))
4429 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4430 : :
4431 : : /* DROP MATERIALIZED VIEW */
4432 : 0 : else if (Matches("DROP", "MATERIALIZED"))
4433 : 0 : COMPLETE_WITH("VIEW");
4434 : 0 : else if (Matches("DROP", "MATERIALIZED", "VIEW"))
1581 4435 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
1641 michael@paquier.xyz 4436 : 0 : else if (Matches("DROP", "MATERIALIZED", "VIEW", MatchAny))
4437 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4438 : :
4439 : : /* DROP OWNED BY */
2808 tgl@sss.pgh.pa.us 4440 : 0 : else if (Matches("DROP", "OWNED"))
4441 : 0 : COMPLETE_WITH("BY");
4442 : 0 : else if (Matches("DROP", "OWNED", "BY"))
7363 alvherre@alvh.no-ip. 4443 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
1641 michael@paquier.xyz 4444 : 0 : else if (Matches("DROP", "OWNED", "BY", MatchAny))
4445 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4446 : :
4447 : : /* DROP TEXT SEARCH */
2808 tgl@sss.pgh.pa.us 4448 : 0 : else if (Matches("DROP", "TEXT", "SEARCH"))
4449 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
4450 : :
4451 : : /* DROP TRIGGER */
4452 : 0 : else if (Matches("DROP", "TRIGGER", MatchAny))
4453 : 0 : COMPLETE_WITH("ON");
4454 : 0 : else if (Matches("DROP", "TRIGGER", MatchAny, "ON"))
4455 : : {
1581 4456 : 0 : set_completion_reference(prev2_wd);
4457 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
4458 : : }
2808 4459 : 0 : else if (Matches("DROP", "TRIGGER", MatchAny, "ON", MatchAny))
4460 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4461 : :
4462 : : /* DROP ACCESS METHOD */
4463 : 0 : else if (Matches("DROP", "ACCESS"))
4464 : 0 : COMPLETE_WITH("METHOD");
4465 : 0 : else if (Matches("DROP", "ACCESS", "METHOD"))
3644 alvherre@alvh.no-ip. 4466 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
4467 : :
4468 : : /* DROP EVENT TRIGGER */
2808 tgl@sss.pgh.pa.us 4469 : 0 : else if (Matches("DROP", "EVENT"))
4470 : 0 : COMPLETE_WITH("TRIGGER");
4471 : 0 : else if (Matches("DROP", "EVENT", "TRIGGER"))
4429 rhaas@postgresql.org 4472 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
4473 : :
4474 : : /* DROP POLICY <name> */
2808 tgl@sss.pgh.pa.us 4475 : 0 : else if (Matches("DROP", "POLICY"))
3967 alvherre@alvh.no-ip. 4476 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_policies);
4477 : : /* DROP POLICY <name> ON */
2808 tgl@sss.pgh.pa.us 4478 : 0 : else if (Matches("DROP", "POLICY", MatchAny))
4479 : 0 : COMPLETE_WITH("ON");
4480 : : /* DROP POLICY <name> ON <table> */
4481 : 0 : else if (Matches("DROP", "POLICY", MatchAny, "ON"))
4482 : : {
1581 4483 : 0 : set_completion_reference(prev2_wd);
4484 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
4485 : : }
1641 michael@paquier.xyz 4486 : 0 : else if (Matches("DROP", "POLICY", MatchAny, "ON", MatchAny))
4487 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4488 : :
4489 : : /* DROP PROPERTY GRAPH */
75 peter@eisentraut.org 4490 :UNC 0 : else if (Matches("DROP", "PROPERTY"))
4491 : 0 : COMPLETE_WITH("GRAPH");
4492 : 0 : else if (Matches("DROP", "PROPERTY", "GRAPH"))
4493 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_propgraphs);
4494 : :
4495 : : /* DROP RULE */
2808 tgl@sss.pgh.pa.us 4496 :UBC 0 : else if (Matches("DROP", "RULE", MatchAny))
4497 : 0 : COMPLETE_WITH("ON");
4498 : 0 : else if (Matches("DROP", "RULE", MatchAny, "ON"))
4499 : : {
1581 4500 : 0 : set_completion_reference(prev2_wd);
4501 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
4502 : : }
2808 4503 : 0 : else if (Matches("DROP", "RULE", MatchAny, "ON", MatchAny))
4504 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4505 : :
4506 : : /* DROP TRANSFORM */
1641 michael@paquier.xyz 4507 : 0 : else if (Matches("DROP", "TRANSFORM"))
4508 : 0 : COMPLETE_WITH("FOR");
4509 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR"))
1581 tgl@sss.pgh.pa.us 4510 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
1641 michael@paquier.xyz 4511 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny))
4512 : 0 : COMPLETE_WITH("LANGUAGE");
4513 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
4514 : : {
1581 tgl@sss.pgh.pa.us 4515 : 0 : set_completion_reference(prev2_wd);
1641 michael@paquier.xyz 4516 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
4517 : : }
4518 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny, "LANGUAGE", MatchAny))
4519 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4520 : :
4521 : : /* EXECUTE */
2808 tgl@sss.pgh.pa.us 4522 : 0 : else if (Matches("EXECUTE"))
5333 4523 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements);
4524 : :
4525 : : /*
4526 : : * EXPLAIN [ ( option [, ...] ) ] statement
4527 : : * EXPLAIN [ ANALYZE ] [ VERBOSE ] statement
4528 : : */
2808 4529 : 0 : else if (Matches("EXPLAIN"))
1843 michael@paquier.xyz 4530 : 0 : COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4531 : : "MERGE INTO", "EXECUTE", "ANALYZE", "VERBOSE");
2808 tgl@sss.pgh.pa.us 4532 : 0 : else if (HeadMatches("EXPLAIN", "(*") &&
4533 [ # # ]: 0 : !HeadMatches("EXPLAIN", "(*)"))
4534 : : {
4535 : : /*
4536 : : * This fires if we're in an unfinished parenthesized option list.
4537 : : * get_previous_words treats a completed parenthesized option list as
4538 : : * one word, so the above test is correct.
4539 : : */
4540 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
1163 4541 : 0 : COMPLETE_WITH("ANALYZE", "VERBOSE", "COSTS", "SETTINGS", "GENERIC_PLAN",
4542 : : "BUFFERS", "SERIALIZE", "WAL", "TIMING", "SUMMARY",
4543 : : "MEMORY", "IO", "FORMAT");
7 fujii@postgresql.org 4544 [ # # ]:UNC 0 : else if (TailMatches("ANALYZE|VERBOSE|COSTS|SETTINGS|GENERIC_PLAN|BUFFERS|WAL|TIMING|SUMMARY|MEMORY|IO"))
2808 tgl@sss.pgh.pa.us 4545 :UBC 0 : COMPLETE_WITH("ON", "OFF");
759 michael@paquier.xyz 4546 [ # # ]: 0 : else if (TailMatches("SERIALIZE"))
4547 : 0 : COMPLETE_WITH("TEXT", "NONE", "BINARY");
2808 tgl@sss.pgh.pa.us 4548 [ # # ]: 0 : else if (TailMatches("FORMAT"))
4549 : 0 : COMPLETE_WITH("TEXT", "XML", "JSON", "YAML");
4550 : : }
4551 : 0 : else if (Matches("EXPLAIN", "ANALYZE"))
1843 michael@paquier.xyz 4552 : 0 : COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4553 : : "MERGE INTO", "EXECUTE", "VERBOSE");
2808 tgl@sss.pgh.pa.us 4554 : 0 : else if (Matches("EXPLAIN", "(*)") ||
4555 : : Matches("EXPLAIN", "VERBOSE") ||
4556 : : Matches("EXPLAIN", "ANALYZE", "VERBOSE"))
1739 michael@paquier.xyz 4557 : 0 : COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4558 : : "MERGE INTO", "EXECUTE");
4559 : :
4560 : : /* FETCH && MOVE */
4561 : :
4562 : : /*
4563 : : * Complete FETCH with one of ABSOLUTE, BACKWARD, FORWARD, RELATIVE, ALL,
4564 : : * NEXT, PRIOR, FIRST, LAST, FROM, IN, and a list of cursors
4565 : : */
2808 tgl@sss.pgh.pa.us 4566 : 0 : else if (Matches("FETCH|MOVE"))
1581 4567 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4568 : : "ABSOLUTE",
4569 : : "BACKWARD",
4570 : : "FORWARD",
4571 : : "RELATIVE",
4572 : : "ALL",
4573 : : "NEXT",
4574 : : "PRIOR",
4575 : : "FIRST",
4576 : : "LAST",
4577 : : "FROM",
4578 : : "IN");
4579 : :
4580 : : /*
4581 : : * Complete FETCH BACKWARD or FORWARD with one of ALL, FROM, IN, and a
4582 : : * list of cursors
4583 : : */
2053 fujii@postgresql.org 4584 : 0 : else if (Matches("FETCH|MOVE", "BACKWARD|FORWARD"))
1581 tgl@sss.pgh.pa.us 4585 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4586 : : "ALL",
4587 : : "FROM",
4588 : : "IN");
4589 : :
4590 : : /*
4591 : : * Complete FETCH <direction> with "FROM" or "IN". These are equivalent,
4592 : : * but we may as well tab-complete both: perhaps some users prefer one
4593 : : * variant or the other.
4594 : : */
2053 fujii@postgresql.org 4595 : 0 : else if (Matches("FETCH|MOVE", "ABSOLUTE|BACKWARD|FORWARD|RELATIVE",
4596 : : MatchAnyExcept("FROM|IN")) ||
4597 : : Matches("FETCH|MOVE", "ALL|NEXT|PRIOR|FIRST|LAST"))
1581 tgl@sss.pgh.pa.us 4598 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4599 : : "FROM",
4600 : : "IN");
4601 : : /* Complete FETCH <direction> "FROM" or "IN" with a list of cursors */
600 4602 : 0 : else if (Matches("FETCH|MOVE", MatchAnyN, "FROM|IN"))
1962 fujii@postgresql.org 4603 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_cursors);
4604 : :
4605 : : /* FOREIGN DATA WRAPPER */
4606 : : /* applies in ALTER/DROP FDW and in CREATE SERVER */
2808 tgl@sss.pgh.pa.us 4607 : 0 : else if (TailMatches("FOREIGN", "DATA", "WRAPPER") &&
4608 [ # # ]: 0 : !TailMatches("CREATE", MatchAny, MatchAny, MatchAny))
6371 peter_e@gmx.net 4609 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
4610 : : /* applies in CREATE SERVER */
600 tgl@sss.pgh.pa.us 4611 : 0 : else if (Matches("CREATE", "SERVER", MatchAnyN, "FOREIGN", "DATA", "WRAPPER", MatchAny))
2808 4612 : 0 : COMPLETE_WITH("OPTIONS");
4613 : :
4614 : : /* FOREIGN TABLE */
4615 : 0 : else if (TailMatches("FOREIGN", "TABLE") &&
4616 [ # # ]: 0 : !TailMatches("CREATE", MatchAny, MatchAny))
1581 4617 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
4618 : :
4619 : : /* FOREIGN SERVER */
2808 4620 : 0 : else if (TailMatches("FOREIGN", "SERVER"))
3916 fujii@postgresql.org 4621 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
4622 : :
4623 : : /*
4624 : : * GRANT and REVOKE are allowed inside CREATE SCHEMA and
4625 : : * ALTER DEFAULT PRIVILEGES, so use TailMatches
4626 : : */
4627 : : /* Complete GRANT/REVOKE with a list of roles and privileges */
1515 tgl@sss.pgh.pa.us 4628 : 0 : else if (TailMatches("GRANT|REVOKE") ||
4629 : : TailMatches("REVOKE", "ADMIN|GRANT|INHERIT|SET", "OPTION", "FOR"))
4630 : : {
4631 : : /*
4632 : : * With ALTER DEFAULT PRIVILEGES, restrict completion to grantable
4633 : : * privileges (can't grant roles)
4634 : : */
2808 4635 [ # # ]: 0 : if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
4636 : : {
782 msawada@postgresql.o 4637 [ # # # # ]: 0 : if (TailMatches("GRANT") ||
4638 : 0 : TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
4639 : 0 : COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
4640 : : "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
4641 : : "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL");
4642 [ # # ]: 0 : else if (TailMatches("REVOKE"))
4643 : 0 : COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
4644 : : "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
4645 : : "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL",
4646 : : "GRANT OPTION FOR");
4647 : : }
1289 michael@paquier.xyz 4648 [ # # ]: 0 : else if (TailMatches("GRANT"))
1581 tgl@sss.pgh.pa.us 4649 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4650 : : Privilege_options_of_grant_and_revoke);
1289 michael@paquier.xyz 4651 [ # # ]: 0 : else if (TailMatches("REVOKE"))
4652 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4653 : : Privilege_options_of_grant_and_revoke,
4654 : : "GRANT OPTION FOR",
4655 : : "ADMIN OPTION FOR",
4656 : : "INHERIT OPTION FOR",
4657 : : "SET OPTION FOR");
4658 [ # # ]: 0 : else if (TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
4659 : 0 : COMPLETE_WITH(Privilege_options_of_grant_and_revoke);
1265 4660 [ # # ]: 0 : else if (TailMatches("REVOKE", "ADMIN|INHERIT|SET", "OPTION", "FOR"))
1289 4661 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
4662 : : }
4663 : :
1515 tgl@sss.pgh.pa.us 4664 : 0 : else if (TailMatches("GRANT|REVOKE", "ALTER") ||
4665 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER"))
4666 : 0 : COMPLETE_WITH("SYSTEM");
4667 : :
1265 michael@paquier.xyz 4668 : 0 : else if (TailMatches("REVOKE", "SET"))
4669 : 0 : COMPLETE_WITH("ON PARAMETER", "OPTION FOR");
4670 : 0 : else if (TailMatches("GRANT", "SET") ||
4671 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "SET") ||
4672 : : TailMatches("GRANT|REVOKE", "ALTER", "SYSTEM") ||
4673 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER", "SYSTEM"))
1515 tgl@sss.pgh.pa.us 4674 : 0 : COMPLETE_WITH("ON PARAMETER");
4675 : :
4676 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "PARAMETER") ||
4677 : : TailMatches("GRANT|REVOKE", MatchAny, MatchAny, "ON", "PARAMETER") ||
4678 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "PARAMETER") ||
4679 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, MatchAny, "ON", "PARAMETER"))
1514 4680 : 0 : COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_alter_system_set_vars);
4681 : :
1515 4682 : 0 : else if (TailMatches("GRANT", MatchAny, "ON", "PARAMETER", MatchAny) ||
4683 : : TailMatches("GRANT", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny))
4684 : 0 : COMPLETE_WITH("TO");
4685 : :
4686 : 0 : else if (TailMatches("REVOKE", MatchAny, "ON", "PARAMETER", MatchAny) ||
4687 : : TailMatches("REVOKE", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny) ||
4688 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "PARAMETER", MatchAny) ||
4689 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny))
4690 : 0 : COMPLETE_WITH("FROM");
4691 : :
4692 : : /*
4693 : : * Complete GRANT/REVOKE <privilege> with "ON", GRANT/REVOKE <role> with
4694 : : * TO/FROM
4695 : : */
4696 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny) ||
4697 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny))
4698 : : {
808 nathan@postgresql.or 4699 [ # # ]: 0 : if (TailMatches("SELECT|INSERT|UPDATE|DELETE|TRUNCATE|REFERENCES|TRIGGER|CREATE|CONNECT|TEMPORARY|TEMP|EXECUTE|USAGE|MAINTAIN|ALL"))
2808 tgl@sss.pgh.pa.us 4700 : 0 : COMPLETE_WITH("ON");
4701 [ # # ]: 0 : else if (TailMatches("GRANT", MatchAny))
4702 : 0 : COMPLETE_WITH("TO");
4703 : : else
4704 : 0 : COMPLETE_WITH("FROM");
4705 : : }
4706 : :
4707 : : /*
4708 : : * Complete GRANT/REVOKE <sth> ON with a list of appropriate relations.
4709 : : *
4710 : : * Note: GRANT/REVOKE can get quite complex; tab-completion as implemented
4711 : : * here will only work if the privilege list contains exactly one
4712 : : * privilege.
4713 : : */
1515 4714 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON") ||
4715 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON"))
4716 : : {
4717 : : /*
4718 : : * With ALTER DEFAULT PRIVILEGES, restrict completion to the kinds of
4719 : : * objects supported.
4720 : : */
2808 4721 [ # # ]: 0 : if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
421 fujii@postgresql.org 4722 : 0 : COMPLETE_WITH("TABLES", "SEQUENCES", "FUNCTIONS", "PROCEDURES", "ROUTINES", "TYPES", "SCHEMAS", "LARGE OBJECTS");
4723 : : else
1581 tgl@sss.pgh.pa.us 4724 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_grantables,
4725 : : "ALL FUNCTIONS IN SCHEMA",
4726 : : "ALL PROCEDURES IN SCHEMA",
4727 : : "ALL ROUTINES IN SCHEMA",
4728 : : "ALL SEQUENCES IN SCHEMA",
4729 : : "ALL TABLES IN SCHEMA",
4730 : : "DATABASE",
4731 : : "DOMAIN",
4732 : : "FOREIGN DATA WRAPPER",
4733 : : "FOREIGN SERVER",
4734 : : "FUNCTION",
4735 : : "LANGUAGE",
4736 : : "LARGE OBJECT",
4737 : : "PARAMETER",
4738 : : "PROCEDURE",
4739 : : "PROPERTY GRAPH",
4740 : : "ROUTINE",
4741 : : "SCHEMA",
4742 : : "SEQUENCE",
4743 : : "TABLE",
4744 : : "TABLESPACE",
4745 : : "TYPE");
4746 : : }
1515 4747 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "ALL") ||
4748 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "ALL"))
2808 4749 : 0 : COMPLETE_WITH("FUNCTIONS IN SCHEMA",
4750 : : "PROCEDURES IN SCHEMA",
4751 : : "ROUTINES IN SCHEMA",
4752 : : "SEQUENCES IN SCHEMA",
4753 : : "TABLES IN SCHEMA");
4754 : :
4755 : : /*
4756 : : * Complete "GRANT/REVOKE * ON DATABASE/DOMAIN/..." with a list of
4757 : : * appropriate objects or keywords.
4758 : : *
4759 : : * Complete "GRANT/REVOKE * ON *" with "TO/FROM".
4760 : : */
1515 4761 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", MatchAny) ||
4762 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", MatchAny))
4763 : : {
2808 4764 [ # # ]: 0 : if (TailMatches("DATABASE"))
8465 bruce@momjian.us 4765 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2808 tgl@sss.pgh.pa.us 4766 [ # # ]: 0 : else if (TailMatches("DOMAIN"))
1581 4767 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
2808 4768 [ # # ]: 0 : else if (TailMatches("FUNCTION"))
1581 4769 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
218 fujii@postgresql.org 4770 [ # # ]:UNC 0 : else if (TailMatches("FOREIGN"))
4771 : 0 : COMPLETE_WITH("DATA WRAPPER", "SERVER");
2808 tgl@sss.pgh.pa.us 4772 [ # # ]:UBC 0 : else if (TailMatches("LANGUAGE"))
8465 bruce@momjian.us 4773 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
218 fujii@postgresql.org 4774 [ # # ]:UNC 0 : else if (TailMatches("LARGE"))
4775 : : {
4776 [ # # ]: 0 : if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
4777 : 0 : COMPLETE_WITH("OBJECTS");
4778 : : else
4779 : 0 : COMPLETE_WITH("OBJECT");
4780 : : }
2808 tgl@sss.pgh.pa.us 4781 [ # # ]:UBC 0 : else if (TailMatches("PROCEDURE"))
1581 4782 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_procedures);
2808 4783 [ # # ]: 0 : else if (TailMatches("ROUTINE"))
1581 4784 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
2808 4785 [ # # ]: 0 : else if (TailMatches("SCHEMA"))
8465 bruce@momjian.us 4786 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
2808 tgl@sss.pgh.pa.us 4787 [ # # ]: 0 : else if (TailMatches("SEQUENCE"))
1581 4788 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences);
2808 4789 [ # # ]: 0 : else if (TailMatches("TABLE"))
1581 4790 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables);
2808 4791 [ # # ]: 0 : else if (TailMatches("TABLESPACE"))
7953 bruce@momjian.us 4792 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
2808 tgl@sss.pgh.pa.us 4793 [ # # ]: 0 : else if (TailMatches("TYPE"))
1581 4794 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2808 4795 [ # # ]: 0 : else if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny))
4796 : 0 : COMPLETE_WITH("TO");
4797 : : else
4798 : 0 : COMPLETE_WITH("FROM");
4799 : : }
4800 : :
4801 : : /*
4802 : : * Complete "GRANT/REVOKE ... TO/FROM" with username, PUBLIC,
4803 : : * CURRENT_ROLE, CURRENT_USER, or SESSION_USER.
4804 : : */
600 4805 : 0 : else if (Matches("GRANT", MatchAnyN, "TO") ||
4806 : : Matches("REVOKE", MatchAnyN, "FROM"))
1581 4807 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4808 : : Keywords_for_list_of_grant_roles);
4809 : :
4810 : : /*
4811 : : * Offer grant options after that.
4812 : : */
600 4813 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny))
1289 michael@paquier.xyz 4814 : 0 : COMPLETE_WITH("WITH ADMIN",
4815 : : "WITH INHERIT",
4816 : : "WITH SET",
4817 : : "WITH GRANT OPTION",
4818 : : "GRANTED BY");
600 tgl@sss.pgh.pa.us 4819 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH"))
1289 michael@paquier.xyz 4820 : 0 : COMPLETE_WITH("ADMIN",
4821 : : "INHERIT",
4822 : : "SET",
4823 : : "GRANT OPTION");
600 tgl@sss.pgh.pa.us 4824 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", "ADMIN|INHERIT|SET"))
1289 michael@paquier.xyz 4825 : 0 : COMPLETE_WITH("OPTION", "TRUE", "FALSE");
600 tgl@sss.pgh.pa.us 4826 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", MatchAny, "OPTION"))
1515 4827 : 0 : COMPLETE_WITH("GRANTED BY");
600 4828 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", MatchAny, "OPTION", "GRANTED", "BY"))
1515 4829 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4830 : : Keywords_for_list_of_grant_roles);
4831 : : /* Complete "ALTER DEFAULT PRIVILEGES ... GRANT/REVOKE ... TO/FROM */
600 4832 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", MatchAnyN, "TO|FROM"))
1581 4833 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4834 : : Keywords_for_list_of_grant_roles);
4835 : : /* Offer WITH GRANT OPTION after that */
600 4836 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", MatchAnyN, "TO", MatchAny))
782 msawada@postgresql.o 4837 : 0 : COMPLETE_WITH("WITH GRANT OPTION");
4838 : : /* Complete "GRANT/REVOKE ... ON * *" with TO/FROM */
319 fujii@postgresql.org 4839 :UNC 0 : else if (Matches("GRANT|REVOKE", MatchAnyN, "ON", MatchAny, MatchAny) &&
4840 [ # # # # ]: 0 : !TailMatches("FOREIGN", "SERVER") && !TailMatches("LARGE", "OBJECT"))
4841 : : {
4842 [ # # ]: 0 : if (Matches("GRANT", MatchAnyN, "ON", MatchAny, MatchAny))
4843 : 0 : COMPLETE_WITH("TO");
4844 : : else
4845 : 0 : COMPLETE_WITH("FROM");
4846 : : }
4847 : :
4848 : : /* Complete "GRANT/REVOKE * ON ALL * IN SCHEMA *" with TO/FROM */
1515 tgl@sss.pgh.pa.us 4849 :UBC 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny) ||
4850 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny))
4851 : : {
2808 4852 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4853 : 0 : COMPLETE_WITH("TO");
4854 : : else
4855 : 0 : COMPLETE_WITH("FROM");
4856 : : }
4857 : :
4858 : : /* Complete "GRANT/REVOKE * ON FOREIGN DATA WRAPPER *" with TO/FROM */
1515 4859 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny) ||
4860 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny))
4861 : : {
2808 4862 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4863 : 0 : COMPLETE_WITH("TO");
4864 : : else
4865 : 0 : COMPLETE_WITH("FROM");
4866 : : }
4867 : :
4868 : : /* Complete "GRANT/REVOKE * ON FOREIGN SERVER *" with TO/FROM */
1515 4869 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny) ||
4870 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny))
4871 : : {
2808 4872 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4873 : 0 : COMPLETE_WITH("TO");
4874 : : else
4875 : 0 : COMPLETE_WITH("FROM");
4876 : : }
4877 : :
4878 : : /* Complete "GRANT/REVOKE * ON LARGE OBJECT *" with TO/FROM */
325 fujii@postgresql.org 4879 :UNC 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "LARGE", "OBJECT", MatchAny) ||
4880 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "LARGE", "OBJECT", MatchAny))
4881 : : {
4882 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4883 : 0 : COMPLETE_WITH("TO");
4884 : : else
4885 : 0 : COMPLETE_WITH("FROM");
4886 : : }
4887 : :
4888 : : /* Complete "GRANT/REVOKE * ON LARGE OBJECTS" with TO/FROM */
4889 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "LARGE", "OBJECTS") ||
4890 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "LARGE", "OBJECTS"))
4891 : : {
4892 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny))
4893 : 0 : COMPLETE_WITH("TO");
4894 : : else
4895 : 0 : COMPLETE_WITH("FROM");
4896 : : }
4897 : :
4898 : : /* GRAPH_TABLE */
75 peter@eisentraut.org 4899 : 0 : else if (TailMatches("GRAPH_TABLE"))
4900 : 0 : COMPLETE_WITH("(");
4901 : 0 : else if (TailMatches("GRAPH_TABLE", "("))
4902 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_propgraphs);
4903 : 0 : else if (TailMatches("GRAPH_TABLE", "(", MatchAny))
4904 : 0 : COMPLETE_WITH("MATCH");
4905 : :
4906 : : /* GROUP BY */
2808 tgl@sss.pgh.pa.us 4907 :UBC 0 : else if (TailMatches("FROM", MatchAny, "GROUP"))
4908 : 0 : COMPLETE_WITH("BY");
4909 : :
4910 : : /* IMPORT FOREIGN SCHEMA */
4911 : 0 : else if (Matches("IMPORT"))
4912 : 0 : COMPLETE_WITH("FOREIGN SCHEMA");
4913 : 0 : else if (Matches("IMPORT", "FOREIGN"))
4914 : 0 : COMPLETE_WITH("SCHEMA");
2081 michael@paquier.xyz 4915 : 0 : else if (Matches("IMPORT", "FOREIGN", "SCHEMA", MatchAny))
4916 : 0 : COMPLETE_WITH("EXCEPT (", "FROM SERVER", "LIMIT TO (");
4917 : 0 : else if (TailMatches("LIMIT", "TO", "(*)") ||
4918 : : Matches("IMPORT", "FOREIGN", "SCHEMA", MatchAny, "EXCEPT", "(*)"))
4919 : 0 : COMPLETE_WITH("FROM SERVER");
4920 : 0 : else if (TailMatches("FROM", "SERVER", MatchAny))
4921 : 0 : COMPLETE_WITH("INTO");
4922 : 0 : else if (TailMatches("FROM", "SERVER", MatchAny, "INTO"))
4923 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
4924 : 0 : else if (TailMatches("FROM", "SERVER", MatchAny, "INTO", MatchAny))
4925 : 0 : COMPLETE_WITH("OPTIONS (");
4926 : :
4927 : : /* INSERT --- can be inside EXPLAIN, RULE, etc */
4928 : : /* Complete NOT MATCHED THEN INSERT */
1524 alvherre@alvh.no-ip. 4929 : 0 : else if (TailMatches("NOT", "MATCHED", "THEN", "INSERT"))
4930 : 0 : COMPLETE_WITH("VALUES", "(");
4931 : : /* Complete INSERT with "INTO" */
2808 tgl@sss.pgh.pa.us 4932 : 0 : else if (TailMatches("INSERT"))
4933 : 0 : COMPLETE_WITH("INTO");
4934 : : /* Complete INSERT INTO with table names */
4935 : 0 : else if (TailMatches("INSERT", "INTO"))
1581 4936 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
4937 : : /* Complete "INSERT INTO <table> (" with attribute names */
2808 4938 : 0 : else if (TailMatches("INSERT", "INTO", MatchAny, "("))
1581 4939 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
4940 : :
4941 : : /*
4942 : : * Complete INSERT INTO <table> with "(" or "VALUES" or "SELECT" or
4943 : : * "TABLE" or "DEFAULT VALUES" or "OVERRIDING"
4944 : : */
2808 4945 : 0 : else if (TailMatches("INSERT", "INTO", MatchAny))
4946 : 0 : COMPLETE_WITH("(", "DEFAULT VALUES", "SELECT", "TABLE", "VALUES", "OVERRIDING");
4947 : :
4948 : : /*
4949 : : * Complete INSERT INTO <table> (attribs) with "VALUES" or "SELECT" or
4950 : : * "TABLE" or "OVERRIDING"
4951 : : */
4952 : 0 : else if (TailMatches("INSERT", "INTO", MatchAny, MatchAny) &&
3815 4953 [ # # ]: 0 : ends_with(prev_wd, ')'))
2808 4954 : 0 : COMPLETE_WITH("SELECT", "TABLE", "VALUES", "OVERRIDING");
4955 : :
4956 : : /* Complete OVERRIDING */
4957 : 0 : else if (TailMatches("OVERRIDING"))
4958 : 0 : COMPLETE_WITH("SYSTEM VALUE", "USER VALUE");
4959 : :
4960 : : /* Complete after OVERRIDING clause */
4961 : 0 : else if (TailMatches("OVERRIDING", MatchAny, "VALUE"))
4962 : 0 : COMPLETE_WITH("SELECT", "TABLE", "VALUES");
4963 : :
4964 : : /* Insert an open parenthesis after "VALUES" */
4965 [ # # ]: 0 : else if (TailMatches("VALUES") && !TailMatches("DEFAULT", "VALUES"))
4966 : 0 : COMPLETE_WITH("(");
4967 : :
4968 : : /* LOCK */
4969 : : /* Complete LOCK [TABLE] [ONLY] with a list of tables */
4970 : 0 : else if (Matches("LOCK"))
1581 4971 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
4972 : : "TABLE", "ONLY");
2808 4973 : 0 : else if (Matches("LOCK", "TABLE"))
1581 4974 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
4975 : : "ONLY");
1698 fujii@postgresql.org 4976 : 0 : else if (Matches("LOCK", "TABLE", "ONLY") || Matches("LOCK", "ONLY"))
1581 tgl@sss.pgh.pa.us 4977 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
4978 : : /* For the following, handle the case of a single table only for now */
4979 : :
4980 : : /* Complete LOCK [TABLE] [ONLY] <table> with IN or NOWAIT */
1698 fujii@postgresql.org 4981 : 0 : else if (Matches("LOCK", MatchAnyExcept("TABLE|ONLY")) ||
4982 : : Matches("LOCK", "TABLE", MatchAnyExcept("ONLY")) ||
4983 : : Matches("LOCK", "ONLY", MatchAny) ||
4984 : : Matches("LOCK", "TABLE", "ONLY", MatchAny))
4985 : 0 : COMPLETE_WITH("IN", "NOWAIT");
4986 : :
4987 : : /* Complete LOCK [TABLE] [ONLY] <table> IN with a lock mode */
600 tgl@sss.pgh.pa.us 4988 : 0 : else if (Matches("LOCK", MatchAnyN, "IN"))
2808 4989 : 0 : COMPLETE_WITH("ACCESS SHARE MODE",
4990 : : "ROW SHARE MODE", "ROW EXCLUSIVE MODE",
4991 : : "SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE",
4992 : : "SHARE ROW EXCLUSIVE MODE",
4993 : : "EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE");
4994 : :
4995 : : /*
4996 : : * Complete LOCK [TABLE][ONLY] <table> IN ACCESS|ROW with rest of lock
4997 : : * mode
4998 : : */
600 4999 : 0 : else if (Matches("LOCK", MatchAnyN, "IN", "ACCESS|ROW"))
2808 5000 : 0 : COMPLETE_WITH("EXCLUSIVE MODE", "SHARE MODE");
5001 : :
5002 : : /* Complete LOCK [TABLE] [ONLY] <table> IN SHARE with rest of lock mode */
600 5003 : 0 : else if (Matches("LOCK", MatchAnyN, "IN", "SHARE"))
2808 5004 : 0 : COMPLETE_WITH("MODE", "ROW EXCLUSIVE MODE",
5005 : : "UPDATE EXCLUSIVE MODE");
5006 : :
5007 : : /* Complete LOCK [TABLE] [ONLY] <table> [IN lockmode MODE] with "NOWAIT" */
600 5008 : 0 : else if (Matches("LOCK", MatchAnyN, "MODE"))
1346 fujii@postgresql.org 5009 : 0 : COMPLETE_WITH("NOWAIT");
5010 : :
5011 : : /* MERGE --- can be inside EXPLAIN */
1524 alvherre@alvh.no-ip. 5012 : 0 : else if (TailMatches("MERGE"))
5013 : 0 : COMPLETE_WITH("INTO");
5014 : 0 : else if (TailMatches("MERGE", "INTO"))
5015 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_mergetargets);
5016 : :
5017 : : /* Complete MERGE INTO <table> [[AS] <alias>] with USING */
5018 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny))
5019 : 0 : COMPLETE_WITH("USING", "AS");
1346 fujii@postgresql.org 5020 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny) ||
5021 : : TailMatches("MERGE", "INTO", MatchAny, MatchAnyExcept("USING|AS")))
1524 alvherre@alvh.no-ip. 5022 : 0 : COMPLETE_WITH("USING");
5023 : :
5024 : : /*
5025 : : * Complete MERGE INTO ... USING with a list of relations supporting
5026 : : * SELECT
5027 : : */
1346 fujii@postgresql.org 5028 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "USING") ||
5029 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING") ||
5030 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING"))
5031 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
5032 : :
5033 : : /*
5034 : : * Complete MERGE INTO <table> [[AS] <alias>] USING <relations> [[AS]
5035 : : * alias] with ON
5036 : : */
5037 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny) ||
5038 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny) ||
5039 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny))
5040 : 0 : COMPLETE_WITH("AS", "ON");
5041 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny, "AS", MatchAny) ||
5042 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, "AS", MatchAny) ||
5043 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny, "AS", MatchAny) ||
5044 : : TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")) ||
5045 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")) ||
5046 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")))
1524 alvherre@alvh.no-ip. 5047 : 0 : COMPLETE_WITH("ON");
5048 : :
5049 : : /* Complete MERGE INTO ... ON with target table attributes */
5050 : 0 : else if (TailMatches("INTO", MatchAny, "USING", MatchAny, "ON"))
5051 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
5052 : 0 : else if (TailMatches("INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, "AS", MatchAny, "ON"))
5053 : 0 : COMPLETE_WITH_ATTR(prev8_wd);
5054 : 0 : else if (TailMatches("INTO", MatchAny, MatchAny, "USING", MatchAny, MatchAny, "ON"))
5055 : 0 : COMPLETE_WITH_ATTR(prev6_wd);
5056 : :
5057 : : /*
5058 : : * Complete ... USING <relation> [[AS] alias] ON join condition
5059 : : * (consisting of one or three words typically used) with WHEN [NOT]
5060 : : * MATCHED
5061 : : */
1346 fujii@postgresql.org 5062 : 0 : else if (TailMatches("USING", MatchAny, "ON", MatchAny) ||
5063 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny) ||
5064 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny) ||
5065 : : TailMatches("USING", MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")) ||
5066 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")) ||
5067 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")))
1524 alvherre@alvh.no-ip. 5068 : 0 : COMPLETE_WITH("WHEN MATCHED", "WHEN NOT MATCHED");
1346 fujii@postgresql.org 5069 : 0 : else if (TailMatches("USING", MatchAny, "ON", MatchAny, "WHEN") ||
5070 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, "WHEN") ||
5071 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, "WHEN") ||
5072 : : TailMatches("USING", MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN") ||
5073 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN") ||
5074 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN"))
5075 : 0 : COMPLETE_WITH("MATCHED", "NOT MATCHED");
5076 : :
5077 : : /*
5078 : : * Complete ... WHEN MATCHED and WHEN NOT MATCHED BY SOURCE|TARGET with
5079 : : * THEN/AND
5080 : : */
5081 : 0 : else if (TailMatches("WHEN", "MATCHED") ||
5082 : : TailMatches("WHEN", "NOT", "MATCHED", "BY", "SOURCE|TARGET"))
1524 alvherre@alvh.no-ip. 5083 : 0 : COMPLETE_WITH("THEN", "AND");
5084 : :
5085 : : /* Complete ... WHEN NOT MATCHED with BY/THEN/AND */
791 dean.a.rasheed@gmail 5086 : 0 : else if (TailMatches("WHEN", "NOT", "MATCHED"))
5087 : 0 : COMPLETE_WITH("BY", "THEN", "AND");
5088 : :
5089 : : /* Complete ... WHEN NOT MATCHED BY with SOURCE/TARGET */
5090 : 0 : else if (TailMatches("WHEN", "NOT", "MATCHED", "BY"))
5091 : 0 : COMPLETE_WITH("SOURCE", "TARGET");
5092 : :
5093 : : /*
5094 : : * Complete ... WHEN MATCHED THEN and WHEN NOT MATCHED BY SOURCE THEN with
5095 : : * UPDATE SET/DELETE/DO NOTHING
5096 : : */
5097 : 0 : else if (TailMatches("WHEN", "MATCHED", "THEN") ||
5098 : : TailMatches("WHEN", "NOT", "MATCHED", "BY", "SOURCE", "THEN"))
1346 fujii@postgresql.org 5099 : 0 : COMPLETE_WITH("UPDATE SET", "DELETE", "DO NOTHING");
5100 : :
5101 : : /*
5102 : : * Complete ... WHEN NOT MATCHED [BY TARGET] THEN with INSERT/DO NOTHING
5103 : : */
791 dean.a.rasheed@gmail 5104 : 0 : else if (TailMatches("WHEN", "NOT", "MATCHED", "THEN") ||
5105 : : TailMatches("WHEN", "NOT", "MATCHED", "BY", "TARGET", "THEN"))
1524 alvherre@alvh.no-ip. 5106 : 0 : COMPLETE_WITH("INSERT", "DO NOTHING");
5107 : :
5108 : : /* NOTIFY --- can be inside EXPLAIN, RULE, etc */
2808 tgl@sss.pgh.pa.us 5109 : 0 : else if (TailMatches("NOTIFY"))
1581 5110 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_channels);
5111 : :
5112 : : /* OPTIONS */
2808 5113 : 0 : else if (TailMatches("OPTIONS"))
5114 : 0 : COMPLETE_WITH("(");
5115 : :
5116 : : /* OWNER TO - complete with available roles */
5117 : 0 : else if (TailMatches("OWNER", "TO"))
1333 michael@paquier.xyz 5118 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
5119 : : Keywords_for_list_of_owner_roles);
5120 : :
5121 : : /* ORDER BY */
2808 tgl@sss.pgh.pa.us 5122 : 0 : else if (TailMatches("FROM", MatchAny, "ORDER"))
5123 : 0 : COMPLETE_WITH("BY");
5124 : 0 : else if (TailMatches("FROM", MatchAny, "ORDER", "BY"))
1581 5125 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
5126 : :
5127 : : /* PREPARE xx AS */
2808 5128 : 0 : else if (Matches("PREPARE", MatchAny, "AS"))
188 fujii@postgresql.org 5129 :UNC 0 : COMPLETE_WITH("SELECT", "UPDATE", "INSERT INTO", "DELETE FROM",
5130 : : "MERGE INTO", "VALUES", "WITH", "TABLE");
5131 : :
5132 : : /*
5133 : : * PREPARE TRANSACTION is missing on purpose. It's intended for transaction
5134 : : * managers, not for manual use in interactive sessions.
5135 : : */
5136 : :
5137 : : /* REASSIGN OWNED BY xxx TO yyy */
2808 tgl@sss.pgh.pa.us 5138 :UBC 0 : else if (Matches("REASSIGN"))
5139 : 0 : COMPLETE_WITH("OWNED BY");
5140 : 0 : else if (Matches("REASSIGN", "OWNED"))
5141 : 0 : COMPLETE_WITH("BY");
5142 : 0 : else if (Matches("REASSIGN", "OWNED", "BY"))
7363 alvherre@alvh.no-ip. 5143 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2808 tgl@sss.pgh.pa.us 5144 : 0 : else if (Matches("REASSIGN", "OWNED", "BY", MatchAny))
5145 : 0 : COMPLETE_WITH("TO");
5146 : 0 : else if (Matches("REASSIGN", "OWNED", "BY", MatchAny, "TO"))
7363 alvherre@alvh.no-ip. 5147 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5148 : :
5149 : : /* REFRESH MATERIALIZED VIEW */
2808 tgl@sss.pgh.pa.us 5150 : 0 : else if (Matches("REFRESH"))
5151 : 0 : COMPLETE_WITH("MATERIALIZED VIEW");
5152 : 0 : else if (Matches("REFRESH", "MATERIALIZED"))
5153 : 0 : COMPLETE_WITH("VIEW");
5154 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW"))
1581 5155 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_matviews,
5156 : : "CONCURRENTLY");
2808 5157 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY"))
1581 5158 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
2808 5159 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny))
5160 : 0 : COMPLETE_WITH("WITH");
5161 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny))
5162 : 0 : COMPLETE_WITH("WITH");
5163 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH"))
5164 : 0 : COMPLETE_WITH("NO DATA", "DATA");
5165 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH"))
5166 : 0 : COMPLETE_WITH("NO DATA", "DATA");
5167 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH", "NO"))
5168 : 0 : COMPLETE_WITH("DATA");
5169 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH", "NO"))
5170 : 0 : COMPLETE_WITH("DATA");
5171 : :
5172 : : /* REINDEX */
2118 michael@paquier.xyz 5173 : 0 : else if (Matches("REINDEX") ||
5174 : : Matches("REINDEX", "(*)"))
2808 tgl@sss.pgh.pa.us 5175 : 0 : COMPLETE_WITH("TABLE", "INDEX", "SYSTEM", "SCHEMA", "DATABASE");
2118 michael@paquier.xyz 5176 : 0 : else if (Matches("REINDEX", "TABLE") ||
5177 : : Matches("REINDEX", "(*)", "TABLE"))
1581 tgl@sss.pgh.pa.us 5178 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexables,
5179 : : "CONCURRENTLY");
2118 michael@paquier.xyz 5180 : 0 : else if (Matches("REINDEX", "INDEX") ||
5181 : : Matches("REINDEX", "(*)", "INDEX"))
1581 tgl@sss.pgh.pa.us 5182 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
5183 : : "CONCURRENTLY");
2118 michael@paquier.xyz 5184 : 0 : else if (Matches("REINDEX", "SCHEMA") ||
5185 : : Matches("REINDEX", "(*)", "SCHEMA"))
1581 tgl@sss.pgh.pa.us 5186 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas,
5187 : : "CONCURRENTLY");
2118 michael@paquier.xyz 5188 : 0 : else if (Matches("REINDEX", "SYSTEM|DATABASE") ||
5189 : : Matches("REINDEX", "(*)", "SYSTEM|DATABASE"))
1581 tgl@sss.pgh.pa.us 5190 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_databases,
5191 : : "CONCURRENTLY");
2118 michael@paquier.xyz 5192 : 0 : else if (Matches("REINDEX", "TABLE", "CONCURRENTLY") ||
5193 : : Matches("REINDEX", "(*)", "TABLE", "CONCURRENTLY"))
1581 tgl@sss.pgh.pa.us 5194 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables);
2118 michael@paquier.xyz 5195 : 0 : else if (Matches("REINDEX", "INDEX", "CONCURRENTLY") ||
5196 : : Matches("REINDEX", "(*)", "INDEX", "CONCURRENTLY"))
1581 tgl@sss.pgh.pa.us 5197 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
2118 michael@paquier.xyz 5198 : 0 : else if (Matches("REINDEX", "SCHEMA", "CONCURRENTLY") ||
5199 : : Matches("REINDEX", "(*)", "SCHEMA", "CONCURRENTLY"))
2619 peter@eisentraut.org 5200 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
2118 michael@paquier.xyz 5201 : 0 : else if (Matches("REINDEX", "SYSTEM|DATABASE", "CONCURRENTLY") ||
5202 : : Matches("REINDEX", "(*)", "SYSTEM|DATABASE", "CONCURRENTLY"))
3799 tgl@sss.pgh.pa.us 5203 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2118 michael@paquier.xyz 5204 : 0 : else if (HeadMatches("REINDEX", "(*") &&
5205 [ # # ]: 0 : !HeadMatches("REINDEX", "(*)"))
5206 : : {
5207 : : /*
5208 : : * This fires if we're in an unfinished parenthesized option list.
5209 : : * get_previous_words treats a completed parenthesized option list as
5210 : : * one word, so the above test is correct.
5211 : : */
5212 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
1941 5213 : 0 : COMPLETE_WITH("CONCURRENTLY", "TABLESPACE", "VERBOSE");
5214 [ # # ]: 0 : else if (TailMatches("TABLESPACE"))
5215 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
5216 : : }
5217 : :
5218 : : /* REPACK */
81 alvherre@kurilemu.de 5219 :UNC 0 : else if (Matches("REPACK"))
5220 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
5221 : : "(", "USING INDEX");
5222 : 0 : else if (Matches("REPACK", "(*)"))
5223 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
5224 : : "USING INDEX");
5225 : 0 : else if (Matches("REPACK", MatchAnyExcept("(")))
5226 : 0 : COMPLETE_WITH("USING INDEX");
5227 : 0 : else if (Matches("REPACK", "(*)", MatchAnyExcept("(")))
5228 : 0 : COMPLETE_WITH("USING INDEX");
5229 : 0 : else if (Matches("REPACK", MatchAny, "USING", "INDEX") ||
5230 : : Matches("REPACK", "(*)", MatchAny, "USING", "INDEX"))
5231 : : {
5232 : 0 : set_completion_reference(prev3_wd);
5233 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
5234 : : }
5235 : :
5236 : : /*
5237 : : * Complete ... [ (*) ] <sth> USING INDEX, with a list of indexes for
5238 : : * <sth>.
5239 : : */
5240 : 0 : else if (TailMatches(MatchAny, "USING", "INDEX"))
5241 : : {
5242 : 0 : set_completion_reference(prev3_wd);
5243 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
5244 : : }
5245 : 0 : else if (HeadMatches("REPACK", "(*") &&
5246 [ # # ]: 0 : !HeadMatches("REPACK", "(*)"))
5247 : : {
5248 : : /*
5249 : : * This fires if we're in an unfinished parenthesized option list.
5250 : : * get_previous_words treats a completed parenthesized option list as
5251 : : * one word, so the above test is correct.
5252 : : */
5253 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
54 5254 : 0 : COMPLETE_WITH("ANALYZE", "CONCURRENTLY", "VERBOSE");
15 fujii@postgresql.org 5255 [ # # ]: 0 : else if (TailMatches("ANALYZE|CONCURRENTLY|VERBOSE"))
81 alvherre@kurilemu.de 5256 : 0 : COMPLETE_WITH("ON", "OFF");
5257 : : }
5258 : :
5259 : : /* SECURITY LABEL */
2808 tgl@sss.pgh.pa.us 5260 :UBC 0 : else if (Matches("SECURITY"))
5261 : 0 : COMPLETE_WITH("LABEL");
5262 : 0 : else if (Matches("SECURITY", "LABEL"))
5263 : 0 : COMPLETE_WITH("ON", "FOR");
5264 : 0 : else if (Matches("SECURITY", "LABEL", "FOR", MatchAny))
5265 : 0 : COMPLETE_WITH("ON");
5266 : 0 : else if (Matches("SECURITY", "LABEL", "ON") ||
5267 : : Matches("SECURITY", "LABEL", "FOR", MatchAny, "ON"))
5268 : 0 : COMPLETE_WITH("TABLE", "COLUMN", "AGGREGATE", "DATABASE", "DOMAIN",
5269 : : "EVENT TRIGGER", "FOREIGN TABLE", "FUNCTION",
5270 : : "LARGE OBJECT", "MATERIALIZED VIEW", "LANGUAGE",
5271 : : "PROPERTY GRAPH", "PUBLICATION", "PROCEDURE", "ROLE", "ROUTINE", "SCHEMA",
5272 : : "SEQUENCE", "SUBSCRIPTION", "TABLESPACE", "TYPE", "VIEW");
75 peter@eisentraut.org 5273 :UNC 0 : else if (Matches("SECURITY", "LABEL", "ON", "PROPERTY", "GRAPH"))
5274 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_propgraphs);
2808 tgl@sss.pgh.pa.us 5275 :UBC 0 : else if (Matches("SECURITY", "LABEL", "ON", MatchAny, MatchAny))
5276 : 0 : COMPLETE_WITH("IS");
5277 : :
5278 : : /* SELECT */
5279 : : /* naah . . . */
5280 : :
5281 : : /* SET, RESET, SHOW */
5282 : : /* Complete with a variable name */
467 tomas.vondra@postgre 5283 : 0 : else if (TailMatches("SET|RESET") &&
5284 : : !TailMatches("UPDATE", MatchAny, "SET") &&
303 tomas.vondra@postgre 5285 [ + - + - ]:CBC 3 : !TailMatches("ALTER", "DATABASE|USER|ROLE", MatchAny, "RESET"))
1571 tgl@sss.pgh.pa.us 5286 : 3 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
5287 : : "CONSTRAINTS",
5288 : : "TRANSACTION",
5289 : : "SESSION",
5290 : : "ROLE",
5291 : : "TABLESPACE",
5292 : : "ALL");
2808 5293 : 3 : else if (Matches("SHOW"))
1571 tgl@sss.pgh.pa.us 5294 :UBC 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_show_vars,
5295 : : "SESSION AUTHORIZATION",
5296 : : "ALL");
1579 5297 : 0 : else if (Matches("SHOW", "SESSION"))
5298 : 0 : COMPLETE_WITH("AUTHORIZATION");
5299 : : /* Complete "SET TRANSACTION" */
2808 5300 : 0 : else if (Matches("SET", "TRANSACTION"))
5301 : 0 : COMPLETE_WITH("SNAPSHOT", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
5302 : 0 : else if (Matches("BEGIN|START", "TRANSACTION") ||
5303 : : Matches("BEGIN", "WORK") ||
5304 : : Matches("BEGIN") ||
5305 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION"))
5306 : 0 : COMPLETE_WITH("ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
5307 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "NOT") ||
5308 : : Matches("BEGIN", "NOT") ||
5309 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "NOT"))
5310 : 0 : COMPLETE_WITH("DEFERRABLE");
5311 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION") ||
5312 : : Matches("BEGIN", "ISOLATION") ||
5313 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION"))
5314 : 0 : COMPLETE_WITH("LEVEL");
5315 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL") ||
5316 : : Matches("BEGIN", "ISOLATION", "LEVEL") ||
5317 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL"))
5318 : 0 : COMPLETE_WITH("READ", "REPEATABLE READ", "SERIALIZABLE");
5319 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "READ") ||
5320 : : Matches("BEGIN", "ISOLATION", "LEVEL", "READ") ||
5321 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "READ"))
5322 : 0 : COMPLETE_WITH("UNCOMMITTED", "COMMITTED");
5323 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "REPEATABLE") ||
5324 : : Matches("BEGIN", "ISOLATION", "LEVEL", "REPEATABLE") ||
5325 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "REPEATABLE"))
5326 : 0 : COMPLETE_WITH("READ");
5327 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "READ") ||
5328 : : Matches("BEGIN", "READ") ||
5329 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "READ"))
5330 : 0 : COMPLETE_WITH("ONLY", "WRITE");
5331 : : /* SET CONSTRAINTS */
5332 : 0 : else if (Matches("SET", "CONSTRAINTS"))
1581 5333 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_constraints_with_schema,
5334 : : "ALL");
5335 : : /* Complete SET CONSTRAINTS <foo> with DEFERRED|IMMEDIATE */
2808 5336 : 0 : else if (Matches("SET", "CONSTRAINTS", MatchAny))
5337 : 0 : COMPLETE_WITH("DEFERRED", "IMMEDIATE");
5338 : : /* Complete SET ROLE */
5339 : 0 : else if (Matches("SET", "ROLE"))
7594 5340 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5341 : : /* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */
2808 5342 : 0 : else if (Matches("SET", "SESSION"))
5343 : 0 : COMPLETE_WITH("AUTHORIZATION", "CHARACTERISTICS AS TRANSACTION");
5344 : : /* Complete SET SESSION AUTHORIZATION with username */
5345 : 0 : else if (Matches("SET", "SESSION", "AUTHORIZATION"))
1581 5346 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
5347 : : "DEFAULT");
5348 : : /* Complete RESET SESSION with AUTHORIZATION */
2808 5349 : 0 : else if (Matches("RESET", "SESSION"))
5350 : 0 : COMPLETE_WITH("AUTHORIZATION");
5351 : : /* Complete SET <var> with "TO" */
5352 : 0 : else if (Matches("SET", MatchAny))
2808 tgl@sss.pgh.pa.us 5353 :CBC 2 : COMPLETE_WITH("TO");
5354 : :
5355 : : /*
5356 : : * Complete ALTER DATABASE|FUNCTION|PROCEDURE|ROLE|ROUTINE|USER ... SET
5357 : : * <name>
5358 : : */
600 5359 : 2 : else if (Matches("ALTER", "DATABASE|FUNCTION|PROCEDURE|ROLE|ROUTINE|USER", MatchAnyN, "SET", MatchAnyExcept("SCHEMA")))
2808 tgl@sss.pgh.pa.us 5360 :UBC 0 : COMPLETE_WITH("FROM CURRENT", "TO");
5361 : :
5362 : : /*
5363 : : * Suggest possible variable values in SET variable TO|=, along with the
5364 : : * preceding ALTER syntaxes.
5365 : : */
2524 5366 : 0 : else if (TailMatches("SET", MatchAny, "TO|=") &&
2524 tgl@sss.pgh.pa.us 5367 [ + - ]:CBC 4 : !TailMatches("UPDATE", MatchAny, "SET", MatchAny, "TO|="))
5368 : : {
5369 : : /* special cased code for individual GUCs */
2808 5370 [ - + ]: 4 : if (TailMatches("DateStyle", "TO|="))
2808 tgl@sss.pgh.pa.us 5371 :UBC 0 : COMPLETE_WITH("ISO", "SQL", "Postgres", "German",
5372 : : "YMD", "DMY", "MDY",
5373 : : "US", "European", "NonEuropean",
5374 : : "DEFAULT");
2808 tgl@sss.pgh.pa.us 5375 [ - + ]:CBC 4 : else if (TailMatches("search_path", "TO|="))
5376 : : {
5377 : : /* Here, we want to allow pg_catalog, so use narrower exclusion */
1581 tgl@sss.pgh.pa.us 5378 :UBC 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
5379 : : " AND nspname NOT LIKE E'pg\\\\_toast%%'"
5380 : : " AND nspname NOT LIKE E'pg\\\\_temp%%'",
5381 : : "DEFAULT");
5382 : : }
1532 tgl@sss.pgh.pa.us 5383 [ + + ]:CBC 4 : else if (TailMatches("TimeZone", "TO|="))
5384 [ - + + - : 2 : COMPLETE_WITH_TIMEZONE_NAME();
+ + ]
5385 : : else
5386 : : {
5387 : : /* generic, type based, GUC support */
3917 andres@anarazel.de 5388 : 2 : char *guctype = get_guctype(prev2_wd);
5389 : :
5390 : : /*
5391 : : * Note: if we don't recognize the GUC name, it's important to not
5392 : : * offer any completions, as most likely we've misinterpreted the
5393 : : * context and this isn't a GUC-setting command at all.
5394 : : */
2524 tgl@sss.pgh.pa.us 5395 [ + - ]: 2 : if (guctype)
5396 : : {
5397 [ + - ]: 2 : if (strcmp(guctype, "enum") == 0)
5398 : : {
1571 5399 : 2 : set_completion_reference_verbatim(prev2_wd);
1581 5400 : 2 : COMPLETE_WITH_QUERY_PLUS(Query_for_values_of_enum_GUC,
5401 : : "DEFAULT");
5402 : : }
2524 tgl@sss.pgh.pa.us 5403 [ # # ]:UBC 0 : else if (strcmp(guctype, "bool") == 0)
5404 : 0 : COMPLETE_WITH("on", "off", "true", "false", "yes", "no",
5405 : : "1", "0", "DEFAULT");
5406 : : else
5407 : 0 : COMPLETE_WITH("DEFAULT");
5408 : :
3917 andres@anarazel.de 5409 :CBC 2 : free(guctype);
5410 : : }
5411 : : }
5412 : : }
5413 : :
5414 : : /* START TRANSACTION */
2808 tgl@sss.pgh.pa.us 5415 : 4 : else if (Matches("START"))
2808 tgl@sss.pgh.pa.us 5416 :UBC 0 : COMPLETE_WITH("TRANSACTION");
5417 : :
5418 : : /* TABLE, but not TABLE embedded in other commands */
5419 : 0 : else if (Matches("TABLE"))
1581 5420 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
5421 : :
5422 : : /* TABLESAMPLE */
2808 5423 : 0 : else if (TailMatches("TABLESAMPLE"))
3995 rhaas@postgresql.org 5424 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablesample_methods);
2808 tgl@sss.pgh.pa.us 5425 : 0 : else if (TailMatches("TABLESAMPLE", MatchAny))
5426 : 0 : COMPLETE_WITH("(");
5427 : :
5428 : : /* TRUNCATE */
5429 : 0 : else if (Matches("TRUNCATE"))
1581 5430 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_truncatables,
5431 : : "TABLE", "ONLY");
1920 fujii@postgresql.org 5432 : 0 : else if (Matches("TRUNCATE", "TABLE"))
1581 tgl@sss.pgh.pa.us 5433 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_truncatables,
5434 : : "ONLY");
600 5435 : 0 : else if (Matches("TRUNCATE", MatchAnyN, "ONLY"))
1581 5436 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_truncatables);
1920 fujii@postgresql.org 5437 : 0 : else if (Matches("TRUNCATE", MatchAny) ||
5438 : : Matches("TRUNCATE", "TABLE|ONLY", MatchAny) ||
5439 : : Matches("TRUNCATE", "TABLE", "ONLY", MatchAny))
5440 : 0 : COMPLETE_WITH("RESTART IDENTITY", "CONTINUE IDENTITY", "CASCADE", "RESTRICT");
600 tgl@sss.pgh.pa.us 5441 : 0 : else if (Matches("TRUNCATE", MatchAnyN, "IDENTITY"))
1920 fujii@postgresql.org 5442 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
5443 : :
5444 : : /* UNLISTEN */
2808 tgl@sss.pgh.pa.us 5445 : 0 : else if (Matches("UNLISTEN"))
1581 5446 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_channels, "*");
5447 : :
5448 : : /* UPDATE --- can be inside EXPLAIN, RULE, etc */
5449 : : /* If prev. word is UPDATE suggest a list of tables */
2808 5450 : 0 : else if (TailMatches("UPDATE"))
1581 5451 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
5452 : : /* Complete UPDATE <table> with "SET" or "FOR" (for FOR PORTION OF) */
2808 5453 : 0 : else if (TailMatches("UPDATE", MatchAny))
18 peter@eisentraut.org 5454 :GNC 1 : COMPLETE_WITH("FOR", "SET");
5455 : : /* Complete UPDATE <table> FOR with PORTION */
5456 : 1 : else if (TailMatches("UPDATE", MatchAny, "FOR"))
5457 : 1 : COMPLETE_WITH("PORTION");
5458 : : /* Complete UPDATE <table> FOR PORTION with OF */
5459 : 1 : else if (TailMatches("UPDATE", MatchAny, "FOR", "PORTION"))
5460 : 1 : COMPLETE_WITH("OF");
5461 : : /* Complete UPDATE <table> FOR PORTION OF with column names */
5462 : 1 : else if (TailMatches("UPDATE", MatchAny, "FOR", "PORTION", "OF"))
5463 : 1 : COMPLETE_WITH_ATTR(prev4_wd);
5464 : : /* Complete UPDATE <table> FOR PORTION OF <period> with FROM */
5465 : 1 : else if (TailMatches("UPDATE", MatchAny, "FOR", "PORTION", "OF", MatchAny))
5466 : 1 : COMPLETE_WITH("FROM");
5467 : : /* Complete UPDATE <table> SET with list of attributes */
2808 tgl@sss.pgh.pa.us 5468 :GBC 1 : else if (TailMatches("UPDATE", MatchAny, "SET"))
1581 tgl@sss.pgh.pa.us 5469 :UBC 0 : COMPLETE_WITH_ATTR(prev2_wd);
5470 : : /* UPDATE <table> SET <attr> = */
2513 tmunro@postgresql.or 5471 : 0 : else if (TailMatches("UPDATE", MatchAny, "SET", MatchAnyExcept("*=")))
2808 tgl@sss.pgh.pa.us 5472 : 0 : COMPLETE_WITH("=");
5473 : :
5474 : : /* USER MAPPING */
5475 : 0 : else if (Matches("ALTER|CREATE|DROP", "USER", "MAPPING"))
5476 : 0 : COMPLETE_WITH("FOR");
5477 : 0 : else if (Matches("CREATE", "USER", "MAPPING", "FOR"))
1581 5478 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
5479 : : "CURRENT_ROLE",
5480 : : "CURRENT_USER",
5481 : : "PUBLIC",
5482 : : "USER");
2808 5483 : 0 : else if (Matches("ALTER|DROP", "USER", "MAPPING", "FOR"))
6371 peter_e@gmx.net 5484 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
2808 tgl@sss.pgh.pa.us 5485 : 0 : else if (Matches("CREATE|ALTER|DROP", "USER", "MAPPING", "FOR", MatchAny))
5486 : 0 : COMPLETE_WITH("SERVER");
5487 : 0 : else if (Matches("CREATE|ALTER", "USER", "MAPPING", "FOR", MatchAny, "SERVER", MatchAny))
5488 : 0 : COMPLETE_WITH("OPTIONS");
5489 : :
5490 : : /*
5491 : : * VACUUM [ ( option [, ...] ) ] [ [ ONLY ] table_and_columns [, ...] ]
5492 : : * VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ [ ONLY ] table_and_columns [, ...] ]
5493 : : */
5494 : 0 : else if (Matches("VACUUM"))
1581 5495 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5496 : : "(",
5497 : : "FULL",
5498 : : "FREEZE",
5499 : : "VERBOSE",
5500 : : "ANALYZE",
5501 : : "ONLY");
2808 5502 : 0 : else if (HeadMatches("VACUUM", "(*") &&
5503 [ # # ]: 0 : !HeadMatches("VACUUM", "(*)"))
5504 : : {
5505 : : /*
5506 : : * This fires if we're in an unfinished parenthesized option list.
5507 : : * get_previous_words treats a completed parenthesized option list as
5508 : : * one word, so the above test is correct.
5509 : : */
5510 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
5511 : 0 : COMPLETE_WITH("FULL", "FREEZE", "ANALYZE", "VERBOSE",
5512 : : "DISABLE_PAGE_SKIPPING", "SKIP_LOCKED",
5513 : : "INDEX_CLEANUP", "PROCESS_MAIN", "PROCESS_TOAST",
5514 : : "TRUNCATE", "PARALLEL", "SKIP_DATABASE_STATS",
5515 : : "ONLY_DATABASE_STATS", "BUFFER_USAGE_LIMIT");
1181 michael@paquier.xyz 5516 [ # # ]: 0 : else if (TailMatches("FULL|FREEZE|ANALYZE|VERBOSE|DISABLE_PAGE_SKIPPING|SKIP_LOCKED|PROCESS_MAIN|PROCESS_TOAST|TRUNCATE|SKIP_DATABASE_STATS|ONLY_DATABASE_STATS"))
2619 rhaas@postgresql.org 5517 : 0 : COMPLETE_WITH("ON", "OFF");
1807 pg@bowt.ie 5518 [ # # ]: 0 : else if (TailMatches("INDEX_CLEANUP"))
5519 : 0 : COMPLETE_WITH("AUTO", "ON", "OFF");
5520 : : }
157 msawada@postgresql.o 5521 : 0 : else if (Matches("VACUUM", "(*)"))
5522 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5523 : : "ONLY");
5524 : 0 : else if (Matches("VACUUM", "FULL"))
5525 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5526 : : "FREEZE",
5527 : : "VERBOSE",
5528 : : "ANALYZE",
5529 : : "ONLY");
5530 : 0 : else if (Matches("VACUUM", MatchAnyN, "FREEZE"))
5531 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5532 : : "VERBOSE",
5533 : : "ANALYZE",
5534 : : "ONLY");
5535 : 0 : else if (Matches("VACUUM", MatchAnyN, "VERBOSE"))
5536 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5537 : : "ANALYZE",
5538 : : "ONLY");
5539 : 0 : else if (Matches("VACUUM", MatchAnyN, "ANALYZE"))
5540 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5541 : : "ONLY");
600 tgl@sss.pgh.pa.us 5542 : 0 : else if (Matches("VACUUM", MatchAnyN, "("))
5543 : : /* "VACUUM (" should be caught above, so assume we want columns */
1581 5544 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
2808 5545 : 0 : else if (HeadMatches("VACUUM"))
1581 5546 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables);
5547 : :
5548 : : /*
5549 : : * WAIT FOR LSN '<lsn>' [ WITH ( option [, ...] ) ]
5550 : : * where option can be:
5551 : : * MODE '<mode>'
5552 : : * TIMEOUT '<timeout>'
5553 : : * NO_THROW
5554 : : * and mode can be:
5555 : : * standby_replay | standby_write | standby_flush | primary_flush
5556 : : */
199 akorotkov@postgresql 5557 :UNC 0 : else if (Matches("WAIT"))
5558 : 0 : COMPLETE_WITH("FOR");
5559 : 0 : else if (Matches("WAIT", "FOR"))
5560 : 0 : COMPLETE_WITH("LSN");
5561 : 0 : else if (Matches("WAIT", "FOR", "LSN"))
5562 : : /* No completion for LSN value - user must provide manually */
5563 : : ;
5564 : 0 : else if (Matches("WAIT", "FOR", "LSN", MatchAny))
5565 : 0 : COMPLETE_WITH("WITH");
5566 : 0 : else if (Matches("WAIT", "FOR", "LSN", MatchAny, "WITH"))
5567 : 0 : COMPLETE_WITH("(");
5568 : :
5569 : : /*
5570 : : * Handle parenthesized option list. This fires when we're in an
5571 : : * unfinished parenthesized option list. get_previous_words treats a
5572 : : * completed parenthesized option list as one word, so the above test is
5573 : : * correct.
5574 : : *
5575 : : * 'mode' takes a string value (one of the listed above), 'timeout' takes
5576 : : * a string value, and 'no_throw' takes no value. We do not offer
5577 : : * completions for the *values* of 'timeout' or 'no_throw'.
5578 : : */
5579 : 0 : else if (HeadMatches("WAIT", "FOR", "LSN", MatchAny, "WITH", "(*") &&
5580 [ # # ]: 0 : !HeadMatches("WAIT", "FOR", "LSN", MatchAny, "WITH", "(*)"))
5581 : : {
5582 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
145 5583 : 0 : COMPLETE_WITH("mode", "timeout", "no_throw");
5584 [ # # ]: 0 : else if (TailMatches("mode"))
5585 : 0 : COMPLETE_WITH("'standby_replay'", "'standby_write'", "'standby_flush'", "'primary_flush'");
5586 : : }
5587 : :
5588 : : /* WITH [RECURSIVE] */
5589 : :
5590 : : /*
5591 : : * Only match when WITH is the first word, as WITH may appear in many
5592 : : * other contexts.
5593 : : */
2808 tgl@sss.pgh.pa.us 5594 :UBC 0 : else if (Matches("WITH"))
5595 : 0 : COMPLETE_WITH("RECURSIVE");
5596 : :
5597 : : /* WHERE */
5598 : : /* Simple case of the word before the where being the table name */
5599 : 0 : else if (TailMatches(MatchAny, "WHERE"))
1581 5600 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
5601 : :
5602 : : /* ... FROM ... */
5603 : : /* TODO: also include SRF ? */
2808 tgl@sss.pgh.pa.us 5604 [ + - ]:CBC 14 : else if (TailMatches("FROM") && !Matches("COPY|\\copy", MatchAny, "FROM"))
1581 5605 : 14 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
5606 : :
5607 : : /* ... JOIN ... */
2808 5608 : 14 : else if (TailMatches("JOIN"))
530 tomas.vondra@postgre 5609 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_selectables, "LATERAL");
5610 [ # # ]: 0 : else if (TailMatches("JOIN", MatchAny) && !TailMatches("CROSS|NATURAL", "JOIN", MatchAny))
5611 : 0 : COMPLETE_WITH("ON", "USING (");
5612 : 0 : else if (TailMatches("JOIN", MatchAny, MatchAny) &&
5613 [ # # # # ]: 0 : !TailMatches("CROSS|NATURAL", "JOIN", MatchAny, MatchAny) && !TailMatches("ON|USING"))
5614 : 0 : COMPLETE_WITH("ON", "USING (");
5615 : 0 : else if (TailMatches("JOIN", "LATERAL", MatchAny, MatchAny) &&
5616 [ # # # # ]: 0 : !TailMatches("CROSS|NATURAL", "JOIN", "LATERAL", MatchAny, MatchAny) && !TailMatches("ON|USING"))
5617 : 0 : COMPLETE_WITH("ON", "USING (");
5618 : 0 : else if (TailMatches("JOIN", MatchAny, "USING") ||
5619 : : TailMatches("JOIN", MatchAny, MatchAny, "USING") ||
5620 : : TailMatches("JOIN", "LATERAL", MatchAny, MatchAny, "USING"))
5621 : 0 : COMPLETE_WITH("(");
5622 : 0 : else if (TailMatches("JOIN", MatchAny, "USING", "("))
5623 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
5624 : 0 : else if (TailMatches("JOIN", MatchAny, MatchAny, "USING", "("))
5625 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
5626 : :
5627 : : /* ... AT [ LOCAL | TIME ZONE ] ... */
960 michael@paquier.xyz 5628 : 0 : else if (TailMatches("AT"))
5629 : 0 : COMPLETE_WITH("LOCAL", "TIME ZONE");
5630 : 0 : else if (TailMatches("AT", "TIME", "ZONE"))
5631 [ # # # # : 0 : COMPLETE_WITH_TIMEZONE_NAME();
# # ]
5632 : :
5633 : : /* Backslash commands */
5634 : : /* TODO: \dc \dd \dl */
2808 tgl@sss.pgh.pa.us 5635 : 0 : else if (TailMatchesCS("\\?"))
5636 : 0 : COMPLETE_WITH_CS("commands", "options", "variables");
5637 : 0 : else if (TailMatchesCS("\\connect|\\c"))
5638 : : {
4076 alvherre@alvh.no-ip. 5639 [ # # ]: 0 : if (!recognized_connection_string(text))
5640 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
5641 : : }
2808 tgl@sss.pgh.pa.us 5642 : 0 : else if (TailMatchesCS("\\connect|\\c", MatchAny))
5643 : : {
4076 alvherre@alvh.no-ip. 5644 [ # # ]: 0 : if (!recognized_connection_string(prev_wd))
5645 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5646 : : }
2808 tgl@sss.pgh.pa.us 5647 : 0 : else if (TailMatchesCS("\\da*"))
1581 5648 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_aggregates);
2274 akorotkov@postgresql 5649 : 0 : else if (TailMatchesCS("\\dAc*", MatchAny) ||
5650 : : TailMatchesCS("\\dAf*", MatchAny))
1581 tgl@sss.pgh.pa.us 5651 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2274 akorotkov@postgresql 5652 : 0 : else if (TailMatchesCS("\\dAo*", MatchAny) ||
5653 : : TailMatchesCS("\\dAp*", MatchAny))
1581 tgl@sss.pgh.pa.us 5654 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_operator_families);
2808 5655 : 0 : else if (TailMatchesCS("\\dA*"))
3644 alvherre@alvh.no-ip. 5656 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
2808 tgl@sss.pgh.pa.us 5657 : 0 : else if (TailMatchesCS("\\db*"))
7953 bruce@momjian.us 5658 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
1514 tgl@sss.pgh.pa.us 5659 : 0 : else if (TailMatchesCS("\\dconfig*"))
5660 : 0 : COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_show_vars);
2808 5661 : 0 : else if (TailMatchesCS("\\dD*"))
1581 5662 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
2808 5663 : 0 : else if (TailMatchesCS("\\des*"))
6371 peter_e@gmx.net 5664 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
2808 tgl@sss.pgh.pa.us 5665 : 0 : else if (TailMatchesCS("\\deu*"))
6371 peter_e@gmx.net 5666 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
2808 tgl@sss.pgh.pa.us 5667 : 0 : else if (TailMatchesCS("\\dew*"))
6371 peter_e@gmx.net 5668 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
2808 tgl@sss.pgh.pa.us 5669 : 0 : else if (TailMatchesCS("\\df*"))
1581 5670 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
1878 5671 : 0 : else if (HeadMatchesCS("\\df*"))
1581 5672 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
5673 : :
2808 5674 : 0 : else if (TailMatchesCS("\\dFd*"))
1581 5675 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_dictionaries);
2808 5676 : 0 : else if (TailMatchesCS("\\dFp*"))
1581 5677 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_parsers);
2808 5678 : 0 : else if (TailMatchesCS("\\dFt*"))
1581 5679 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_templates);
5680 : : /* must be at end of \dF alternatives: */
2808 5681 : 0 : else if (TailMatchesCS("\\dF*"))
1581 5682 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_configurations);
5683 : :
2808 5684 : 0 : else if (TailMatchesCS("\\di*"))
1581 5685 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
2808 5686 : 0 : else if (TailMatchesCS("\\dL*"))
5609 rhaas@postgresql.org 5687 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
2808 tgl@sss.pgh.pa.us 5688 : 0 : else if (TailMatchesCS("\\dn*"))
8465 bruce@momjian.us 5689 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
5690 : : /* no support for completing operators, but we can complete types: */
1878 tgl@sss.pgh.pa.us 5691 : 0 : else if (HeadMatchesCS("\\do*", MatchAny))
1581 5692 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2808 5693 : 0 : else if (TailMatchesCS("\\dp") || TailMatchesCS("\\z"))
1581 5694 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables);
2610 alvherre@alvh.no-ip. 5695 : 0 : else if (TailMatchesCS("\\dPi*"))
1581 tgl@sss.pgh.pa.us 5696 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_indexes);
2610 alvherre@alvh.no-ip. 5697 : 0 : else if (TailMatchesCS("\\dPt*"))
1581 tgl@sss.pgh.pa.us 5698 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables);
2610 alvherre@alvh.no-ip. 5699 : 0 : else if (TailMatchesCS("\\dP*"))
1581 tgl@sss.pgh.pa.us 5700 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_relations);
1333 michael@paquier.xyz 5701 : 0 : else if (TailMatchesCS("\\dRp*"))
5702 : 0 : COMPLETE_WITH_VERSIONED_QUERY(Query_for_list_of_publications);
5703 : 0 : else if (TailMatchesCS("\\dRs*"))
5704 : 0 : COMPLETE_WITH_VERSIONED_QUERY(Query_for_list_of_subscriptions);
2808 tgl@sss.pgh.pa.us 5705 : 0 : else if (TailMatchesCS("\\ds*"))
1581 5706 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences);
2808 5707 : 0 : else if (TailMatchesCS("\\dt*"))
1581 5708 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2808 5709 : 0 : else if (TailMatchesCS("\\dT*"))
1581 5710 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
1046 5711 : 0 : else if (TailMatchesCS("\\du*") ||
5712 : : TailMatchesCS("\\dg*") ||
5713 : : TailMatchesCS("\\drg*"))
7594 5714 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2808 5715 : 0 : else if (TailMatchesCS("\\dv*"))
1581 5716 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
2808 5717 : 0 : else if (TailMatchesCS("\\dx*"))
4671 magnus@hagander.net 5718 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_extensions);
1956 tomas.vondra@postgre 5719 : 0 : else if (TailMatchesCS("\\dX*"))
1581 tgl@sss.pgh.pa.us 5720 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_statistics);
2808 5721 : 0 : else if (TailMatchesCS("\\dm*"))
1581 5722 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
2808 5723 : 0 : else if (TailMatchesCS("\\dE*"))
1581 5724 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
2808 5725 : 0 : else if (TailMatchesCS("\\dy*"))
3980 fujii@postgresql.org 5726 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
5727 : :
5728 : : /* must be at end of \d alternatives: */
2808 tgl@sss.pgh.pa.us 5729 : 0 : else if (TailMatchesCS("\\d*"))
1581 5730 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations);
5731 : :
2808 5732 : 0 : else if (TailMatchesCS("\\ef"))
1581 5733 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
2808 5734 : 0 : else if (TailMatchesCS("\\ev"))
1581 5735 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
5736 : :
2808 5737 : 0 : else if (TailMatchesCS("\\encoding"))
1581 5738 : 0 : COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_encodings);
2808 5739 : 0 : else if (TailMatchesCS("\\h|\\help"))
9544 bruce@momjian.us 5740 : 0 : COMPLETE_WITH_LIST(sql_commands);
2808 tgl@sss.pgh.pa.us 5741 : 0 : else if (TailMatchesCS("\\h|\\help", MatchAny))
5742 : : {
5743 [ # # ]: 0 : if (TailMatches("DROP"))
247 msawada@postgresql.o 5744 :UNC 0 : COMPLETE_WITH_GENERATOR(drop_command_generator);
2808 tgl@sss.pgh.pa.us 5745 [ # # ]:UBC 0 : else if (TailMatches("ALTER"))
247 msawada@postgresql.o 5746 :UNC 0 : COMPLETE_WITH_GENERATOR(alter_command_generator);
5747 : :
5748 : : /*
5749 : : * CREATE is recognized by tail match elsewhere, so doesn't need to be
5750 : : * repeated here
5751 : : */
5752 : : }
2808 tgl@sss.pgh.pa.us 5753 :UBC 0 : else if (TailMatchesCS("\\h|\\help", MatchAny, MatchAny))
5754 : : {
5755 [ # # ]: 0 : if (TailMatches("CREATE|DROP", "ACCESS"))
5756 : 0 : COMPLETE_WITH("METHOD");
5757 [ # # ]: 0 : else if (TailMatches("ALTER", "DEFAULT"))
5758 : 0 : COMPLETE_WITH("PRIVILEGES");
5759 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "EVENT"))
5760 : 0 : COMPLETE_WITH("TRIGGER");
5761 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "FOREIGN"))
5762 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
5763 [ # # ]: 0 : else if (TailMatches("ALTER", "LARGE"))
5764 : 0 : COMPLETE_WITH("OBJECT");
5765 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "MATERIALIZED"))
5766 : 0 : COMPLETE_WITH("VIEW");
75 peter@eisentraut.org 5767 [ # # ]:UNC 0 : else if (TailMatches("CREATE|ALTER|DROP", "PROPERTY"))
5768 : 0 : COMPLETE_WITH("GRAPH");
2808 tgl@sss.pgh.pa.us 5769 [ # # ]:UBC 0 : else if (TailMatches("CREATE|ALTER|DROP", "TEXT"))
5770 : 0 : COMPLETE_WITH("SEARCH");
5771 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "USER"))
5772 : 0 : COMPLETE_WITH("MAPPING FOR");
5773 : : }
5774 : 0 : else if (TailMatchesCS("\\h|\\help", MatchAny, MatchAny, MatchAny))
5775 : : {
5776 [ # # ]: 0 : if (TailMatches("CREATE|ALTER|DROP", "FOREIGN", "DATA"))
5777 : 0 : COMPLETE_WITH("WRAPPER");
5778 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "TEXT", "SEARCH"))
5779 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
5780 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "USER", "MAPPING"))
5781 : 0 : COMPLETE_WITH("FOR");
5782 : : }
2808 tgl@sss.pgh.pa.us 5783 [ - + ]:CBC 2 : else if (TailMatchesCS("\\l*") && !TailMatchesCS("\\lo*"))
3572 tgl@sss.pgh.pa.us 5784 :UBC 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2808 tgl@sss.pgh.pa.us 5785 :CBC 2 : else if (TailMatchesCS("\\password"))
7468 peter_e@gmx.net 5786 :UBC 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2808 tgl@sss.pgh.pa.us 5787 : 0 : else if (TailMatchesCS("\\pset"))
208 alvherre@kurilemu.de 5788 :UNC 0 : COMPLETE_WITH_CS("border", "columns", "csv_fieldsep",
5789 : : "display_false", "display_true", "expanded",
5790 : : "fieldsep", "fieldsep_zero", "footer", "format",
5791 : : "linestyle", "null", "numericlocale",
5792 : : "pager", "pager_min_lines",
5793 : : "recordsep", "recordsep_zero",
5794 : : "tableattr", "title", "tuples_only",
5795 : : "unicode_border_linestyle",
5796 : : "unicode_column_linestyle",
5797 : : "unicode_header_linestyle",
5798 : : "xheader_width");
2808 tgl@sss.pgh.pa.us 5799 :UBC 0 : else if (TailMatchesCS("\\pset", MatchAny))
5800 : : {
5801 [ # # ]: 0 : if (TailMatchesCS("format"))
2742 5802 : 0 : COMPLETE_WITH_CS("aligned", "asciidoc", "csv", "html", "latex",
5803 : : "latex-longtable", "troff-ms", "unaligned",
5804 : : "wrapped");
1405 andrew@dunslane.net 5805 [ # # ]: 0 : else if (TailMatchesCS("xheader_width"))
5806 : 0 : COMPLETE_WITH_CS("full", "column", "page");
2808 tgl@sss.pgh.pa.us 5807 [ # # ]: 0 : else if (TailMatchesCS("linestyle"))
5808 : 0 : COMPLETE_WITH_CS("ascii", "old-ascii", "unicode");
5809 [ # # ]: 0 : else if (TailMatchesCS("pager"))
5810 : 0 : COMPLETE_WITH_CS("on", "off", "always");
5811 [ # # ]: 0 : else if (TailMatchesCS("unicode_border_linestyle|"
5812 : : "unicode_column_linestyle|"
5813 : : "unicode_header_linestyle"))
5814 : 0 : COMPLETE_WITH_CS("single", "double");
5815 : : }
5816 : 0 : else if (TailMatchesCS("\\unset"))
4309 fujii@postgresql.org 5817 : 0 : matches = complete_from_variables(text, "", "", true);
2808 tgl@sss.pgh.pa.us 5818 : 0 : else if (TailMatchesCS("\\set"))
4309 fujii@postgresql.org 5819 :CBC 1 : matches = complete_from_variables(text, "", "", false);
2808 tgl@sss.pgh.pa.us 5820 : 1 : else if (TailMatchesCS("\\set", MatchAny))
5821 : : {
1517 peter@eisentraut.org 5822 [ - + ]: 1 : if (TailMatchesCS("AUTOCOMMIT|ON_ERROR_STOP|QUIET|SHOW_ALL_RESULTS|"
5823 : : "SINGLELINE|SINGLESTEP"))
2808 tgl@sss.pgh.pa.us 5824 :UBC 0 : COMPLETE_WITH_CS("on", "off");
2808 tgl@sss.pgh.pa.us 5825 [ - + ]:CBC 1 : else if (TailMatchesCS("COMP_KEYWORD_CASE"))
2808 tgl@sss.pgh.pa.us 5826 :UBC 0 : COMPLETE_WITH_CS("lower", "upper",
5827 : : "preserve-lower", "preserve-upper");
2808 tgl@sss.pgh.pa.us 5828 [ - + ]:CBC 1 : else if (TailMatchesCS("ECHO"))
2808 tgl@sss.pgh.pa.us 5829 :UBC 0 : COMPLETE_WITH_CS("errors", "queries", "all", "none");
2808 tgl@sss.pgh.pa.us 5830 [ - + ]:CBC 1 : else if (TailMatchesCS("ECHO_HIDDEN"))
2808 tgl@sss.pgh.pa.us 5831 :UBC 0 : COMPLETE_WITH_CS("noexec", "off", "on");
2808 tgl@sss.pgh.pa.us 5832 [ - + ]:CBC 1 : else if (TailMatchesCS("HISTCONTROL"))
2808 tgl@sss.pgh.pa.us 5833 :UBC 0 : COMPLETE_WITH_CS("ignorespace", "ignoredups",
5834 : : "ignoreboth", "none");
2808 tgl@sss.pgh.pa.us 5835 [ - + ]:CBC 1 : else if (TailMatchesCS("ON_ERROR_ROLLBACK"))
2808 tgl@sss.pgh.pa.us 5836 :UBC 0 : COMPLETE_WITH_CS("on", "off", "interactive");
2808 tgl@sss.pgh.pa.us 5837 [ - + ]:CBC 1 : else if (TailMatchesCS("SHOW_CONTEXT"))
2808 tgl@sss.pgh.pa.us 5838 :UBC 0 : COMPLETE_WITH_CS("never", "errors", "always");
2808 tgl@sss.pgh.pa.us 5839 [ + - ]:CBC 1 : else if (TailMatchesCS("VERBOSITY"))
2613 5840 : 1 : COMPLETE_WITH_CS("default", "verbose", "terse", "sqlstate");
5841 : : }
2808 5842 : 1 : else if (TailMatchesCS("\\sf*"))
1581 tgl@sss.pgh.pa.us 5843 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
2808 5844 : 0 : else if (TailMatchesCS("\\sv*"))
1581 5845 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
2251 bruce@momjian.us 5846 : 0 : else if (TailMatchesCS("\\cd|\\e|\\edit|\\g|\\gx|\\i|\\include|"
5847 : : "\\ir|\\include_relative|\\o|\\out|"
5848 : : "\\s|\\w|\\write|\\lo_import") ||
5849 : : TailMatchesCS("\\lo_export", MatchAny))
247 msawada@postgresql.o 5850 :GNC 2 : COMPLETE_WITH_FILES("\\", false);
5851 : :
5852 : : /* gen_tabcomplete.pl ends special processing here */
600 tgl@sss.pgh.pa.us 5853 :CBC 2 : /* END GEN_TABCOMPLETE */
7502 bruce@momjian.us 5854 :UBC 0 :
7502 bruce@momjian.us 5855 :CBC 68 : return matches;
5856 : : }
5857 : :
5858 : :
5859 : : /*
5860 : : * GENERATOR FUNCTIONS
5861 : : *
5862 : : * These functions do all the actual work of completing the input. They get
5863 : : * passed the text so far and the count how many times they have been called
5864 : : * so far with the same text.
5865 : : * If you read the above carefully, you'll see that these don't get called
5866 : : * directly but through the readline interface.
5867 : : * The return value is expected to be the full completion of the text, going
5868 : : * through a list each time, or NULL if there are no more matches. The string
5869 : : * will be free()'d by readline, so you must run it through strdup() or
5870 : : * something of that sort.
5871 : : */
5872 : :
5873 : : /*
5874 : : * Common routine for create_command_generator and drop_command_generator.
5875 : : * Entries that have 'excluded' flags are not returned.
5876 : : */
5877 : : static char *
61 nathan@postgresql.or 5878 :GNC 4 : create_or_drop_command_generator(const char *text, int state, uint32 excluded)
5879 : : {
5880 : : static int list_index,
5881 : : string_length;
5882 : : const char *name;
5883 : :
5884 : : /* If this is the first time for this completion, init some values */
7502 bruce@momjian.us 5885 [ + + ]:CBC 4 : if (state == 0)
5886 : : {
5887 : 2 : list_index = 0;
5888 : 2 : string_length = strlen(text);
5889 : : }
5890 : :
5891 : : /* find something that matches */
5892 [ + + ]: 104 : while ((name = words_after_create[list_index++].name))
5893 : : {
5711 tgl@sss.pgh.pa.us 5894 [ + + ]: 102 : if ((pg_strncasecmp(name, text, string_length) == 0) &&
5574 itagaki.takahiro@gma 5895 [ + - ]: 2 : !(words_after_create[list_index - 1].flags & excluded))
5135 peter_e@gmx.net 5896 : 2 : return pg_strdup_keyword_case(name, text);
5897 : : }
5898 : : /* if nothing matches, return NULL */
7502 bruce@momjian.us 5899 : 2 : return NULL;
5900 : : }
5901 : :
5902 : : /*
5903 : : * This one gives you one from a list of things you can put after CREATE
5904 : : * as defined above.
5905 : : */
5906 : : static char *
5574 itagaki.takahiro@gma 5907 : 2 : create_command_generator(const char *text, int state)
5908 : : {
5909 : 2 : return create_or_drop_command_generator(text, state, THING_NO_CREATE);
5910 : : }
5911 : :
5912 : : /*
5913 : : * This function gives you a list of things you can put after a DROP command.
5914 : : */
5915 : : static char *
7363 alvherre@alvh.no-ip. 5916 : 2 : drop_command_generator(const char *text, int state)
5917 : : {
5574 itagaki.takahiro@gma 5918 : 2 : return create_or_drop_command_generator(text, state, THING_NO_DROP);
5919 : : }
5920 : :
5921 : : /*
5922 : : * This function gives you a list of things you can put after an ALTER command.
5923 : : */
5924 : : static char *
3362 peter_e@gmx.net 5925 :UBC 0 : alter_command_generator(const char *text, int state)
5926 : : {
5927 : 0 : return create_or_drop_command_generator(text, state, THING_NO_ALTER);
5928 : : }
5929 : :
5930 : : /*
5931 : : * These functions generate lists using server queries.
5932 : : * They are all wrappers for _complete_from_query.
5933 : : */
5934 : :
5935 : : static char *
7502 bruce@momjian.us 5936 :CBC 186 : complete_from_query(const char *text, int state)
5937 : : {
5938 : : /* query is assumed to work for any server version */
1581 tgl@sss.pgh.pa.us 5939 : 186 : return _complete_from_query(completion_charp, NULL, completion_charpp,
5940 : : completion_verbatim, text, state);
5941 : : }
5942 : :
5943 : : static char *
3008 5944 : 2 : complete_from_versioned_query(const char *text, int state)
5945 : : {
5946 : 2 : const VersionedQuery *vquery = completion_vquery;
5947 : :
5948 : : /* Find appropriate array element */
5949 [ - + ]: 2 : while (pset.sversion < vquery->min_server_version)
3008 tgl@sss.pgh.pa.us 5950 :UBC 0 : vquery++;
5951 : : /* Fail completion if server is too old */
3008 tgl@sss.pgh.pa.us 5952 [ - + ]:CBC 2 : if (vquery->query == NULL)
3008 tgl@sss.pgh.pa.us 5953 :UBC 0 : return NULL;
5954 : :
1581 tgl@sss.pgh.pa.us 5955 :CBC 2 : return _complete_from_query(vquery->query, NULL, completion_charpp,
5956 : : completion_verbatim, text, state);
5957 : : }
5958 : :
5959 : : static char *
7502 bruce@momjian.us 5960 : 90 : complete_from_schema_query(const char *text, int state)
5961 : : {
5962 : : /* query is assumed to work for any server version */
1581 tgl@sss.pgh.pa.us 5963 : 90 : return _complete_from_query(NULL, completion_squery, completion_charpp,
5964 : : completion_verbatim, text, state);
5965 : : }
5966 : :
5967 : : static char *
3008 5968 : 8 : complete_from_versioned_schema_query(const char *text, int state)
5969 : : {
5970 : 8 : const SchemaQuery *squery = completion_squery;
5971 : :
5972 : : /* Find appropriate array element */
5973 [ - + ]: 8 : while (pset.sversion < squery->min_server_version)
3008 tgl@sss.pgh.pa.us 5974 :UBC 0 : squery++;
5975 : : /* Fail completion if server is too old */
3008 tgl@sss.pgh.pa.us 5976 [ - + ]:CBC 8 : if (squery->catname == NULL)
3008 tgl@sss.pgh.pa.us 5977 :UBC 0 : return NULL;
5978 : :
1581 tgl@sss.pgh.pa.us 5979 :CBC 8 : return _complete_from_query(NULL, squery, completion_charpp,
5980 : : completion_verbatim, text, state);
5981 : : }
5982 : :
5983 : :
5984 : : /*
5985 : : * This creates a list of matching things, according to a query described by
5986 : : * the initial arguments. The caller has already done any work needed to
5987 : : * select the appropriate query for the server's version.
5988 : : *
5989 : : * The query can be one of two kinds:
5990 : : *
5991 : : * 1. A simple query, which must contain a restriction clause of the form
5992 : : * output LIKE '%s'
5993 : : * where "output" is the same string that the query returns. The %s
5994 : : * will be replaced by a LIKE pattern to match the already-typed text.
5995 : : * There can be a second '%s', which will be replaced by a suitably-escaped
5996 : : * version of the string provided in completion_ref_object. If there is a
5997 : : * third '%s', it will be replaced by a suitably-escaped version of the string
5998 : : * provided in completion_ref_schema. Those strings should be set up
5999 : : * by calling set_completion_reference or set_completion_reference_verbatim.
6000 : : * Simple queries should return a single column of matches. If "verbatim"
6001 : : * is true, the matches are returned as-is; otherwise, they are taken to
6002 : : * be SQL identifiers and quoted if necessary.
6003 : : *
6004 : : * 2. A schema query used for completion of both schema and relation names.
6005 : : * This is represented by a SchemaQuery object; see that typedef for details.
6006 : : *
6007 : : * See top of file for examples of both kinds of query.
6008 : : *
6009 : : * In addition to the query itself, we accept a null-terminated array of
6010 : : * literal keywords, which will be returned if they match the input-so-far
6011 : : * (case insensitively). (These are in addition to keywords specified
6012 : : * within the schema_query, if any.)
6013 : : *
6014 : : * If "verbatim" is true, then we use the given text as-is to match the
6015 : : * query results; otherwise we parse it as a possibly-qualified identifier,
6016 : : * and reconstruct suitable quoting afterward.
6017 : : *
6018 : : * "text" and "state" are supplied by Readline. "text" is the word we are
6019 : : * trying to complete. "state" is zero on first call, nonzero later.
6020 : : *
6021 : : * readline will call this repeatedly with the same text and varying
6022 : : * state. On each call, we are supposed to return a malloc'd string
6023 : : * that is a candidate completion. Return NULL when done.
6024 : : */
6025 : : static char *
3008 6026 : 286 : _complete_from_query(const char *simple_query,
6027 : : const SchemaQuery *schema_query,
6028 : : const char *const *keywords,
6029 : : bool verbatim,
6030 : : const char *text, int state)
6031 : : {
6032 : : static int list_index,
6033 : : num_schema_only,
6034 : : num_query_other,
6035 : : num_keywords;
6036 : : static PGresult *result = NULL;
6037 : : static bool non_empty_object;
6038 : : static bool schemaquoted;
6039 : : static bool objectquoted;
6040 : :
6041 : : /*
6042 : : * If this is the first time for this completion, we fetch a list of our
6043 : : * "things" from the backend.
6044 : : */
7502 bruce@momjian.us 6045 [ + + ]: 286 : if (state == 0)
6046 : : {
6047 : : PQExpBufferData query_buffer;
6048 : : char *schemaname;
6049 : : char *objectname;
6050 : : char *e_object_like;
6051 : : char *e_schemaname;
6052 : : char *e_ref_object;
6053 : : char *e_ref_schema;
6054 : :
6055 : : /* Reset static state, ensuring no memory leaks */
6056 : 46 : list_index = 0;
1581 tgl@sss.pgh.pa.us 6057 : 46 : num_schema_only = 0;
1579 6058 : 46 : num_query_other = 0;
6059 : 46 : num_keywords = 0;
1581 6060 : 46 : PQclear(result);
6061 : 46 : result = NULL;
6062 : :
6063 : : /* Parse text, splitting into schema and object name if needed */
6064 [ + + ]: 46 : if (verbatim)
6065 : : {
6066 : 8 : objectname = pg_strdup(text);
6067 : 8 : schemaname = NULL;
6068 : : }
6069 : : else
6070 : : {
6071 : 38 : parse_identifier(text,
6072 : : &schemaname, &objectname,
6073 : : &schemaquoted, &objectquoted);
6074 : : }
6075 : :
6076 : : /* Remember whether the user has typed anything in the object part */
6077 : 46 : non_empty_object = (*objectname != '\0');
6078 : :
6079 : : /*
6080 : : * Convert objectname to a LIKE prefix pattern (e.g. 'foo%'), and set
6081 : : * up suitably-escaped copies of all the strings we need.
6082 : : */
6083 : 46 : e_object_like = make_like_pattern(objectname);
6084 : :
6085 [ + + ]: 46 : if (schemaname)
6086 : 3 : e_schemaname = escape_string(schemaname);
6087 : : else
6088 : 43 : e_schemaname = NULL;
6089 : :
6090 [ + + ]: 46 : if (completion_ref_object)
6091 : 23 : e_ref_object = escape_string(completion_ref_object);
6092 : : else
6093 : 23 : e_ref_object = NULL;
6094 : :
6095 [ + + ]: 46 : if (completion_ref_schema)
6096 : 1 : e_ref_schema = escape_string(completion_ref_schema);
6097 : : else
6098 : 45 : e_ref_schema = NULL;
6099 : :
7502 bruce@momjian.us 6100 : 46 : initPQExpBuffer(&query_buffer);
6101 : :
3008 tgl@sss.pgh.pa.us 6102 [ + + ]: 46 : if (schema_query)
6103 : : {
1581 6104 [ - + ]: 38 : Assert(simple_query == NULL);
6105 : :
6106 : : /*
6107 : : * We issue different queries depending on whether the input is
6108 : : * already qualified or not. schema_query gives us the pieces to
6109 : : * assemble.
6110 : : */
6111 [ + + - + ]: 38 : if (schemaname == NULL || schema_query->namespace == NULL)
6112 : : {
6113 : : /* Get unqualified names matching the input-so-far */
6114 : 35 : appendPQExpBufferStr(&query_buffer, "SELECT ");
6115 [ - + ]: 35 : if (schema_query->use_distinct)
1581 tgl@sss.pgh.pa.us 6116 :UBC 0 : appendPQExpBufferStr(&query_buffer, "DISTINCT ");
1581 tgl@sss.pgh.pa.us 6117 :CBC 35 : appendPQExpBuffer(&query_buffer,
6118 : : "%s, NULL::pg_catalog.text FROM %s",
6119 : 35 : schema_query->result,
6120 : 35 : schema_query->catname);
6121 [ + + + + ]: 35 : if (schema_query->refnamespace && completion_ref_schema)
6122 : 1 : appendPQExpBufferStr(&query_buffer,
6123 : : ", pg_catalog.pg_namespace nr");
6124 : 35 : appendPQExpBufferStr(&query_buffer, " WHERE ");
6125 [ + - ]: 35 : if (schema_query->selcondition)
6126 : 35 : appendPQExpBuffer(&query_buffer, "%s AND ",
6127 : 35 : schema_query->selcondition);
6128 : 35 : appendPQExpBuffer(&query_buffer, "(%s) LIKE '%s'",
6129 : 35 : schema_query->result,
6130 : : e_object_like);
6131 [ + + ]: 35 : if (schema_query->viscondition)
6132 : 15 : appendPQExpBuffer(&query_buffer, " AND %s",
6133 : 15 : schema_query->viscondition);
6134 [ + + ]: 35 : if (schema_query->refname)
6135 : : {
6136 [ - + ]: 20 : Assert(completion_ref_object);
6137 : 20 : appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
6138 : 20 : schema_query->refname, e_ref_object);
6139 [ + - + + ]: 20 : if (schema_query->refnamespace && completion_ref_schema)
6140 : 1 : appendPQExpBuffer(&query_buffer,
6141 : : " AND %s = nr.oid AND nr.nspname = '%s'",
6142 : 1 : schema_query->refnamespace,
6143 : : e_ref_schema);
6144 [ + - ]: 19 : else if (schema_query->refviscondition)
6145 : 19 : appendPQExpBuffer(&query_buffer,
6146 : : " AND %s",
6147 : 19 : schema_query->refviscondition);
6148 : : }
6149 : :
6150 : : /*
6151 : : * When fetching relation names, suppress system catalogs
6152 : : * unless the input-so-far begins with "pg_". This is a
6153 : : * compromise between not offering system catalogs for
6154 : : * completion at all, and having them swamp the result when
6155 : : * the input is just "p".
6156 : : */
6157 [ + + ]: 35 : if (strcmp(schema_query->catname,
6158 : 14 : "pg_catalog.pg_class c") == 0 &&
6159 [ + - ]: 14 : strncmp(objectname, "pg_", 3) != 0)
6160 : : {
6161 : 14 : appendPQExpBufferStr(&query_buffer,
6162 : : " AND c.relnamespace <> (SELECT oid FROM"
6163 : : " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')");
6164 : : }
6165 : :
6166 : : /*
6167 : : * If the target object type can be schema-qualified, add in
6168 : : * schema names matching the input-so-far.
6169 : : */
6170 [ + + ]: 35 : if (schema_query->namespace)
6171 : : {
6172 : 15 : appendPQExpBuffer(&query_buffer, "\nUNION ALL\n"
6173 : : "SELECT NULL::pg_catalog.text, n.nspname "
6174 : : "FROM pg_catalog.pg_namespace n "
6175 : : "WHERE n.nspname LIKE '%s'",
6176 : : e_object_like);
6177 : :
6178 : : /*
6179 : : * Likewise, suppress system schemas unless the
6180 : : * input-so-far begins with "pg_".
6181 : : */
6182 [ + - ]: 15 : if (strncmp(objectname, "pg_", 3) != 0)
6183 : 15 : appendPQExpBufferStr(&query_buffer,
6184 : : " AND n.nspname NOT LIKE E'pg\\\\_%'");
6185 : :
6186 : : /*
6187 : : * Since we're matching these schema names to the object
6188 : : * name, handle their quoting using the object name's
6189 : : * quoting state.
6190 : : */
6191 : 15 : schemaquoted = objectquoted;
6192 : : }
6193 : : }
6194 : : else
6195 : : {
6196 : : /* Input is qualified, so produce only qualified names */
6197 : 3 : appendPQExpBufferStr(&query_buffer, "SELECT ");
6198 [ + + ]: 3 : if (schema_query->use_distinct)
6199 : 1 : appendPQExpBufferStr(&query_buffer, "DISTINCT ");
6200 : 3 : appendPQExpBuffer(&query_buffer, "%s, n.nspname "
6201 : : "FROM %s, pg_catalog.pg_namespace n",
6202 : 3 : schema_query->result,
6203 : 3 : schema_query->catname);
6204 [ - + - - ]: 3 : if (schema_query->refnamespace && completion_ref_schema)
1581 tgl@sss.pgh.pa.us 6205 :UBC 0 : appendPQExpBufferStr(&query_buffer,
6206 : : ", pg_catalog.pg_namespace nr");
1581 tgl@sss.pgh.pa.us 6207 :CBC 3 : appendPQExpBuffer(&query_buffer, " WHERE %s = n.oid AND ",
6208 : 3 : schema_query->namespace);
6209 [ + - ]: 3 : if (schema_query->selcondition)
6210 : 3 : appendPQExpBuffer(&query_buffer, "%s AND ",
6211 : 3 : schema_query->selcondition);
6212 : 3 : appendPQExpBuffer(&query_buffer, "(%s) LIKE '%s' AND ",
6213 : 3 : schema_query->result,
6214 : : e_object_like);
6215 : 3 : appendPQExpBuffer(&query_buffer, "n.nspname = '%s'",
6216 : : e_schemaname);
6217 [ + + ]: 3 : if (schema_query->refname)
6218 : : {
6219 [ - + ]: 1 : Assert(completion_ref_object);
6220 : 1 : appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
6221 : 1 : schema_query->refname, e_ref_object);
6222 [ - + - - ]: 1 : if (schema_query->refnamespace && completion_ref_schema)
1581 tgl@sss.pgh.pa.us 6223 :UBC 0 : appendPQExpBuffer(&query_buffer,
6224 : : " AND %s = nr.oid AND nr.nspname = '%s'",
6225 : 0 : schema_query->refnamespace,
6226 : : e_ref_schema);
1581 tgl@sss.pgh.pa.us 6227 [ - + ]:CBC 1 : else if (schema_query->refviscondition)
1581 tgl@sss.pgh.pa.us 6228 :UBC 0 : appendPQExpBuffer(&query_buffer,
6229 : : " AND %s",
6230 : 0 : schema_query->refviscondition);
6231 : : }
6232 : : }
6233 : : }
6234 : : else
6235 : : {
3008 tgl@sss.pgh.pa.us 6236 [ - + ]:CBC 8 : Assert(simple_query);
6237 : : /* simple_query is an sprintf-style format string */
6238 : 8 : appendPQExpBuffer(&query_buffer, simple_query,
6239 : : e_object_like,
6240 : : e_ref_object, e_ref_schema);
6241 : : }
6242 : :
6243 : : /* Limit the number of records in the result */
7502 bruce@momjian.us 6244 : 46 : appendPQExpBuffer(&query_buffer, "\nLIMIT %d",
6245 : : completion_max_records);
6246 : :
6247 : : /* Finally, we can issue the query */
6248 : 46 : result = exec_query(query_buffer.data);
6249 : :
6250 : : /* Clean up */
6251 : 46 : termPQExpBuffer(&query_buffer);
1408 tgl@sss.pgh.pa.us 6252 : 46 : free(schemaname);
6253 : 46 : free(objectname);
1581 6254 : 46 : free(e_object_like);
1444 peter@eisentraut.org 6255 : 46 : free(e_schemaname);
6256 : 46 : free(e_ref_object);
6257 : 46 : free(e_ref_schema);
6258 : : }
6259 : :
6260 : : /* Return the next result, if any, but not if the query failed */
7502 bruce@momjian.us 6261 [ + - + - ]: 286 : if (result && PQresultStatus(result) == PGRES_TUPLES_OK)
6262 : : {
6263 : : int nskip;
6264 : :
1581 tgl@sss.pgh.pa.us 6265 [ + + ]: 286 : while (list_index < PQntuples(result))
6266 : : {
6267 : 215 : const char *item = NULL;
6268 : 215 : const char *nsp = NULL;
6269 : :
6270 [ + + ]: 215 : if (!PQgetisnull(result, list_index, 0))
6271 : 214 : item = PQgetvalue(result, list_index, 0);
6272 [ + + + + ]: 250 : if (PQnfields(result) > 1 &&
6273 : 35 : !PQgetisnull(result, list_index, 1))
6274 : 4 : nsp = PQgetvalue(result, list_index, 1);
6275 : 215 : list_index++;
6276 : :
6277 : : /* In verbatim mode, we return all the items as-is */
6278 [ + + ]: 215 : if (verbatim)
6279 : : {
1579 6280 : 182 : num_query_other++;
7502 bruce@momjian.us 6281 : 182 : return pg_strdup(item);
6282 : : }
6283 : :
6284 : : /*
6285 : : * In normal mode, a name requiring quoting will be returned only
6286 : : * if the input was empty or quoted. Otherwise the user might see
6287 : : * completion inserting a quote she didn't type, which is
6288 : : * surprising. This restriction also dodges some odd behaviors of
6289 : : * some versions of readline/libedit.
6290 : : */
1581 tgl@sss.pgh.pa.us 6291 [ + + ]: 33 : if (non_empty_object)
6292 : : {
6293 [ + + + + : 31 : if (item && !objectquoted && identifier_needs_quotes(item))
- + ]
1581 tgl@sss.pgh.pa.us 6294 :UBC 0 : continue;
1581 tgl@sss.pgh.pa.us 6295 [ + + + - :CBC 31 : if (nsp && !schemaquoted && identifier_needs_quotes(nsp))
- + ]
1581 tgl@sss.pgh.pa.us 6296 :UBC 0 : continue;
6297 : : }
6298 : :
6299 : : /* Count schema-only results for hack below */
1581 tgl@sss.pgh.pa.us 6300 [ + + + - ]:CBC 33 : if (item == NULL && nsp != NULL)
6301 : 1 : num_schema_only++;
6302 : : else
1579 6303 : 32 : num_query_other++;
6304 : :
1581 6305 : 33 : return requote_identifier(nsp, item, schemaquoted, objectquoted);
6306 : : }
6307 : :
6308 : : /*
6309 : : * When the query result is exhausted, check for hard-wired keywords.
6310 : : * These will only be returned if they match the input-so-far,
6311 : : * ignoring case.
6312 : : */
6313 : 71 : nskip = list_index - PQntuples(result);
6314 [ + + + + ]: 71 : if (schema_query && schema_query->keywords)
6315 : : {
6316 : 2 : const char *const *itemp = schema_query->keywords;
6317 : :
6318 [ + + ]: 9 : while (*itemp)
6319 : : {
6320 : 8 : const char *item = *itemp++;
6321 : :
6322 [ + + ]: 8 : if (nskip-- > 0)
6323 : 1 : continue;
6324 : 7 : list_index++;
6325 [ + + ]: 7 : if (pg_strncasecmp(text, item, strlen(text)) == 0)
6326 : : {
1579 6327 : 1 : num_keywords++;
1571 6328 : 1 : return pg_strdup_keyword_case(item, text);
6329 : : }
6330 : : }
6331 : : }
1581 6332 [ + + ]: 70 : if (keywords)
6333 : : {
6334 : 44 : const char *const *itemp = keywords;
6335 : :
6336 [ + + ]: 115 : while (*itemp)
6337 : : {
6338 : 95 : const char *item = *itemp++;
6339 : :
6340 [ + + ]: 95 : if (nskip-- > 0)
6341 : 36 : continue;
6342 : 59 : list_index++;
6343 [ + + ]: 59 : if (pg_strncasecmp(text, item, strlen(text)) == 0)
6344 : : {
1579 6345 : 24 : num_keywords++;
1571 6346 : 24 : return pg_strdup_keyword_case(item, text);
6347 : : }
6348 : : }
6349 : : }
6350 : : }
6351 : :
6352 : : /*
6353 : : * Hack: if we returned only bare schema names, don't let Readline add a
6354 : : * space afterwards. Otherwise the schema will stop being part of the
6355 : : * completion subject text, which is not what we want.
6356 : : */
1579 6357 [ + + + - : 46 : if (num_schema_only > 0 && num_query_other == 0 && num_keywords == 0)
+ - ]
1581 6358 : 1 : rl_completion_append_character = '\0';
6359 : :
6360 : : /* No more matches, so free the result structure and return null */
7502 bruce@momjian.us 6361 : 46 : PQclear(result);
6362 : 46 : result = NULL;
6363 : 46 : return NULL;
6364 : : }
6365 : :
6366 : :
6367 : : /*
6368 : : * Set up completion_ref_object and completion_ref_schema
6369 : : * by parsing the given word. These variables can then be
6370 : : * used in a query passed to _complete_from_query.
6371 : : */
6372 : : static void
1581 tgl@sss.pgh.pa.us 6373 : 21 : set_completion_reference(const char *word)
6374 : : {
6375 : : bool schemaquoted,
6376 : : objectquoted;
6377 : :
6378 : 21 : parse_identifier(word,
6379 : : &completion_ref_schema, &completion_ref_object,
6380 : : &schemaquoted, &objectquoted);
6381 : 21 : }
6382 : :
6383 : : /*
6384 : : * Set up completion_ref_object when it should just be
6385 : : * the given word verbatim.
6386 : : */
6387 : : static void
1571 6388 : 2 : set_completion_reference_verbatim(const char *word)
6389 : : {
6390 : 2 : completion_ref_schema = NULL;
6391 : 2 : completion_ref_object = pg_strdup(word);
6392 : 2 : }
6393 : :
6394 : :
6395 : : /*
6396 : : * This function returns in order one of a fixed, NULL pointer terminated list
6397 : : * of strings (if matching). This can be used if there are only a fixed number
6398 : : * SQL words that can appear at certain spot.
6399 : : */
6400 : : static char *
7502 bruce@momjian.us 6401 : 48 : complete_from_list(const char *text, int state)
6402 : : {
6403 : : static int string_length,
6404 : : list_index,
6405 : : matches;
6406 : : static bool casesensitive;
6407 : : const char *item;
6408 : :
6409 : : /* need to have a list */
4915 andrew@dunslane.net 6410 [ - + ]: 48 : Assert(completion_charpp != NULL);
6411 : :
6412 : : /* Initialization */
7502 bruce@momjian.us 6413 [ + + ]: 48 : if (state == 0)
6414 : : {
6415 : 21 : list_index = 0;
6416 : 21 : string_length = strlen(text);
5232 peter_e@gmx.net 6417 : 21 : casesensitive = completion_case_sensitive;
7502 bruce@momjian.us 6418 : 21 : matches = 0;
6419 : : }
6420 : :
6421 [ + + ]: 581 : while ((item = completion_charpp[list_index++]))
6422 : : {
6423 : : /* First pass is case sensitive */
6424 [ + + + + ]: 511 : if (casesensitive && strncmp(text, item, string_length) == 0)
6425 : : {
6426 : 4 : matches++;
6427 : 4 : return pg_strdup(item);
6428 : : }
6429 : :
6430 : : /* Second pass is case insensitive, don't bother counting matches */
6431 [ + + + + ]: 507 : if (!casesensitive && pg_strncasecmp(text, item, string_length) == 0)
6432 : : {
5232 peter_e@gmx.net 6433 [ + + ]: 22 : if (completion_case_sensitive)
6434 : 1 : return pg_strdup(item);
6435 : : else
6436 : :
6437 : : /*
6438 : : * If case insensitive matching was requested initially,
6439 : : * adjust the case according to setting.
6440 : : */
5135 6441 : 21 : return pg_strdup_keyword_case(item, text);
6442 : : }
6443 : : }
6444 : :
6445 : : /*
6446 : : * No matches found. If we're not case insensitive already, lets switch to
6447 : : * being case insensitive and try again
6448 : : */
7502 bruce@momjian.us 6449 [ + + + + ]: 22 : if (casesensitive && matches == 0)
6450 : : {
6451 : 1 : casesensitive = false;
6452 : 1 : list_index = 0;
6453 : 1 : state++;
7444 neilc@samurai.com 6454 : 1 : return complete_from_list(text, state);
6455 : : }
6456 : :
6457 : : /* If no more matches, return null. */
7502 bruce@momjian.us 6458 : 21 : return NULL;
6459 : : }
6460 : :
6461 : :
6462 : : /*
6463 : : * This function returns one fixed string the first time even if it doesn't
6464 : : * match what's there, and nothing the second time. The string
6465 : : * to be used must be in completion_charp.
6466 : : *
6467 : : * If the given string is "", this has the effect of preventing readline
6468 : : * from doing any completion. (Without this, readline tries to do filename
6469 : : * completion which is seldom the right thing.)
6470 : : *
6471 : : * If the given string is not empty, readline will replace whatever the
6472 : : * user typed with that string. This behavior might be useful if it's
6473 : : * completely certain that we know what must appear at a certain spot,
6474 : : * so that it's okay to overwrite misspellings. In practice, given the
6475 : : * relatively lame parsing technology used in this file, the level of
6476 : : * certainty is seldom that high, so that you probably don't want to
6477 : : * use this. Use complete_from_list with a one-element list instead;
6478 : : * that won't try to auto-correct "misspellings".
6479 : : */
6480 : : static char *
6481 : 4 : complete_from_const(const char *text, int state)
6482 : : {
4915 andrew@dunslane.net 6483 [ - + ]: 4 : Assert(completion_charp != NULL);
7502 bruce@momjian.us 6484 [ + + ]: 4 : if (state == 0)
6485 : : {
5232 peter_e@gmx.net 6486 [ + - ]: 2 : if (completion_case_sensitive)
6487 : 2 : return pg_strdup(completion_charp);
6488 : : else
6489 : :
6490 : : /*
6491 : : * If case insensitive matching was requested initially, adjust
6492 : : * the case according to setting.
6493 : : */
5135 peter_e@gmx.net 6494 :UBC 0 : return pg_strdup_keyword_case(completion_charp, text);
6495 : : }
6496 : : else
7502 bruce@momjian.us 6497 :CBC 2 : return NULL;
6498 : : }
6499 : :
6500 : :
6501 : : /*
6502 : : * This function appends the variable name with prefix and suffix to
6503 : : * the variable names array.
6504 : : */
6505 : : static void
4309 fujii@postgresql.org 6506 : 124 : append_variable_names(char ***varnames, int *nvars,
6507 : : int *maxvars, const char *varname,
6508 : : const char *prefix, const char *suffix)
6509 : : {
6510 [ - + ]: 124 : if (*nvars >= *maxvars)
6511 : : {
4309 fujii@postgresql.org 6512 :UBC 0 : *maxvars *= 2;
92 michael@paquier.xyz 6513 :UNC 0 : *varnames = pg_realloc_array(*varnames, char *, (*maxvars) + 1);
6514 : : }
6515 : :
4309 fujii@postgresql.org 6516 :CBC 124 : (*varnames)[(*nvars)++] = psprintf("%s%s%s", prefix, varname, suffix);
6517 : 124 : }
6518 : :
6519 : :
6520 : : /*
6521 : : * This function supports completion with the name of a psql variable.
6522 : : * The variable names can be prefixed and suffixed with additional text
6523 : : * to support quoting usages. If need_value is true, only variables
6524 : : * that are currently set are included; otherwise, special variables
6525 : : * (those that have hooks) are included even if currently unset.
6526 : : */
6527 : : static char **
6528 : 3 : complete_from_variables(const char *text, const char *prefix, const char *suffix,
6529 : : bool need_value)
6530 : : {
6531 : : char **matches;
6532 : : char **varnames;
5711 tgl@sss.pgh.pa.us 6533 : 3 : int nvars = 0;
6534 : 3 : int maxvars = 100;
6535 : : int i;
6536 : : struct _variable *ptr;
6537 : :
92 michael@paquier.xyz 6538 :GNC 3 : varnames = pg_malloc_array(char *, maxvars + 1);
6539 : :
5711 tgl@sss.pgh.pa.us 6540 [ + + ]:CBC 129 : for (ptr = pset.vars->next; ptr; ptr = ptr->next)
6541 : : {
4309 fujii@postgresql.org 6542 [ + + + + ]: 126 : if (need_value && !(ptr->value))
6543 : 2 : continue;
6544 : 124 : append_variable_names(&varnames, &nvars, &maxvars, ptr->name,
6545 : : prefix, suffix);
6546 : : }
6547 : :
5711 tgl@sss.pgh.pa.us 6548 : 3 : varnames[nvars] = NULL;
5102 bruce@momjian.us 6549 : 3 : COMPLETE_WITH_LIST_CS((const char *const *) varnames);
6550 : :
5711 tgl@sss.pgh.pa.us 6551 [ + + ]: 127 : for (i = 0; i < nvars; i++)
5233 peter_e@gmx.net 6552 : 124 : free(varnames[i]);
5711 tgl@sss.pgh.pa.us 6553 : 3 : free(varnames);
6554 : :
6555 : 3 : return matches;
6556 : : }
6557 : :
6558 : :
6559 : : /*
6560 : : * This function returns in order one of a fixed, NULL pointer terminated list
6561 : : * of string that matches file names or optionally specified list of keywords.
6562 : : *
6563 : : * If completion_charpp is set to a null-terminated array of literal keywords,
6564 : : * those keywords are added to the completion results alongside filenames if
6565 : : * they case-insensitively match the current input.
6566 : : */
6567 : : static char *
207 msawada@postgresql.o 6568 :GNC 16 : complete_from_files(const char *text, int state)
6569 : : {
6570 : : static int list_index;
6571 : : static bool files_done;
6572 : : const char *item;
6573 : :
6574 : : /* Initialization */
6575 [ + + ]: 16 : if (state == 0)
6576 : : {
6577 : 6 : list_index = 0;
6578 : 6 : files_done = false;
6579 : : }
6580 : :
6581 [ + - ]: 16 : if (!files_done)
6582 : : {
6583 : 16 : char *result = _complete_from_files(text, state);
6584 : :
6585 : : /* Return a filename that matches */
6586 [ + + ]: 16 : if (result)
6587 : 10 : return result;
6588 : :
6589 : : /* There are no more matching files */
6590 : 6 : files_done = true;
6591 : : }
6592 : :
6593 [ + + ]: 6 : if (!completion_charpp)
6594 : 2 : return NULL;
6595 : :
6596 : : /*
6597 : : * Check for hard-wired keywords. These will only be returned if they
6598 : : * match the input-so-far, ignoring case.
6599 : : */
6600 [ + + ]: 12 : while ((item = completion_charpp[list_index++]))
6601 : : {
6602 [ - + ]: 8 : if (pg_strncasecmp(text, item, strlen(text)) == 0)
6603 : : {
207 msawada@postgresql.o 6604 :UNC 0 : completion_force_quote = false;
6605 : 0 : return pg_strdup_keyword_case(item, text);
6606 : : }
6607 : : }
6608 : :
207 msawada@postgresql.o 6609 :GNC 4 : return NULL;
6610 : : }
6611 : :
6612 : : /*
6613 : : * This function wraps rl_filename_completion_function() to strip quotes from
6614 : : * the input before searching for matches and to quote any matches for which
6615 : : * the consuming command will require it.
6616 : : *
6617 : : * Caller must set completion_charp to a zero- or one-character string
6618 : : * containing the escape character. This is necessary since \copy has no
6619 : : * escape character, but every other backslash command recognizes "\" as an
6620 : : * escape character.
6621 : : *
6622 : : * Caller must also set completion_force_quote to indicate whether to force
6623 : : * quotes around the result. (The SQL COPY command requires that.)
6624 : : */
6625 : : static char *
6626 : 16 : _complete_from_files(const char *text, int state)
6627 : : {
6628 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
6629 : :
6630 : : /*
6631 : : * If we're using a version of Readline that supports filename quoting
6632 : : * hooks, rely on those, and invoke rl_filename_completion_function()
6633 : : * without messing with its arguments. Readline does stuff internally
6634 : : * that does not work well at all if we try to handle dequoting here.
6635 : : * Instead, Readline will call quote_file_name() and dequote_file_name()
6636 : : * (see below) at appropriate times.
6637 : : *
6638 : : * ... or at least, mostly it will. There are some paths involving
6639 : : * unmatched file names in which Readline never calls quote_file_name(),
6640 : : * and if left to its own devices it will incorrectly append a quote
6641 : : * anyway. Set rl_completion_suppress_quote to prevent that. If we do
6642 : : * get to quote_file_name(), we'll clear this again. (Yes, this seems
6643 : : * like it's working around Readline bugs.)
6644 : : */
6645 : : #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
2319 tgl@sss.pgh.pa.us 6646 :CBC 16 : rl_completion_suppress_quote = 1;
6647 : : #endif
6648 : :
6649 : : /* If user typed a quote, force quoting (never remove user's quote) */
6650 [ - + ]: 16 : if (*text == '\'')
2319 tgl@sss.pgh.pa.us 6651 :UBC 0 : completion_force_quote = true;
6652 : :
2319 tgl@sss.pgh.pa.us 6653 :CBC 16 : return rl_filename_completion_function(text, state);
6654 : : #else
6655 : :
6656 : : /*
6657 : : * Otherwise, we have to do the best we can.
6658 : : */
6659 : : static const char *unquoted_text;
6660 : : char *unquoted_match;
6661 : : char *ret = NULL;
6662 : :
6663 : : /* If user typed a quote, force quoting (never remove user's quote) */
6664 : : if (*text == '\'')
6665 : : completion_force_quote = true;
6666 : :
6667 : : if (state == 0)
6668 : : {
6669 : : /* Initialization: stash the unquoted input. */
6670 : : unquoted_text = strtokx(text, "", NULL, "'", *completion_charp,
6671 : : false, true, pset.encoding);
6672 : : /* expect a NULL return for the empty string only */
6673 : : if (!unquoted_text)
6674 : : {
6675 : : Assert(*text == '\0');
6676 : : unquoted_text = text;
6677 : : }
6678 : : }
6679 : :
6680 : : unquoted_match = rl_filename_completion_function(unquoted_text, state);
6681 : : if (unquoted_match)
6682 : : {
6683 : : struct stat statbuf;
6684 : : bool is_dir = (stat(unquoted_match, &statbuf) == 0 &&
6685 : : S_ISDIR(statbuf.st_mode) != 0);
6686 : :
6687 : : /* Re-quote the result, if needed. */
6688 : : ret = quote_if_needed(unquoted_match, " \t\r\n\"`",
6689 : : '\'', *completion_charp,
6690 : : completion_force_quote,
6691 : : pset.encoding);
6692 : : if (ret)
6693 : : free(unquoted_match);
6694 : : else
6695 : : ret = unquoted_match;
6696 : :
6697 : : /*
6698 : : * If it's a directory, replace trailing quote with a slash; this is
6699 : : * usually more convenient. (If we didn't quote, leave this to
6700 : : * libedit.)
6701 : : */
6702 : : if (*ret == '\'' && is_dir)
6703 : : {
6704 : : char *retend = ret + strlen(ret) - 1;
6705 : :
6706 : : Assert(*retend == '\'');
6707 : : *retend = '/';
6708 : : /* Prevent libedit from adding a space, too */
6709 : : rl_completion_append_character = '\0';
6710 : : }
6711 : : }
6712 : :
6713 : : return ret;
6714 : : #endif /* USE_FILENAME_QUOTING_FUNCTIONS */
6715 : : }
6716 : :
6717 : :
6718 : : /* HELPER FUNCTIONS */
6719 : :
6720 : :
6721 : : /*
6722 : : * Make a pg_strdup copy of s and convert the case according to
6723 : : * COMP_KEYWORD_CASE setting, using ref as the text that was already entered.
6724 : : */
6725 : : static char *
5135 peter_e@gmx.net 6726 : 48 : pg_strdup_keyword_case(const char *s, const char *ref)
6727 : : {
6728 : : char *ret,
6729 : : *p;
5232 6730 : 48 : unsigned char first = ref[0];
6731 : :
5135 6732 : 48 : ret = pg_strdup(s);
6733 : :
4168 tgl@sss.pgh.pa.us 6734 [ + + ]: 48 : if (pset.comp_case == PSQL_COMP_CASE_LOWER ||
6735 [ + + ]: 42 : ((pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER ||
3265 6736 [ + + + + ]: 42 : pset.comp_case == PSQL_COMP_CASE_PRESERVE_UPPER) && islower(first)) ||
4168 6737 [ - + - - ]: 34 : (pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER && !isalpha(first)))
6738 : : {
5135 peter_e@gmx.net 6739 [ + + ]: 122 : for (p = ret; *p; p++)
6740 : 108 : *p = pg_tolower((unsigned char) *p);
6741 : : }
6742 : : else
6743 : : {
6744 [ + + ]: 281 : for (p = ret; *p; p++)
6745 : 247 : *p = pg_toupper((unsigned char) *p);
6746 : : }
6747 : :
6748 : 48 : return ret;
6749 : : }
6750 : :
6751 : :
6752 : : /*
6753 : : * escape_string - Escape argument for use as string literal.
6754 : : *
6755 : : * The returned value has to be freed.
6756 : : */
6757 : : static char *
3917 andres@anarazel.de 6758 : 75 : escape_string(const char *text)
6759 : : {
6760 : : size_t text_length;
6761 : : char *result;
6762 : :
6763 : 75 : text_length = strlen(text);
6764 : :
6765 : 75 : result = pg_malloc(text_length * 2 + 1);
6766 : 75 : PQescapeStringConn(pset.db, result, text, text_length, NULL);
6767 : :
6768 : 75 : return result;
6769 : : }
6770 : :
6771 : :
6772 : : /*
6773 : : * make_like_pattern - Convert argument to a LIKE prefix pattern.
6774 : : *
6775 : : * We escape _ and % in the given text by backslashing, append a % to
6776 : : * represent "any subsequent characters", and then pass the string through
6777 : : * escape_string() so it's ready to insert in a query. The result needs
6778 : : * to be freed.
6779 : : */
6780 : : static char *
1581 tgl@sss.pgh.pa.us 6781 : 46 : make_like_pattern(const char *word)
6782 : : {
6783 : : char *result;
6784 : 46 : char *buffer = pg_malloc(strlen(word) * 2 + 2);
6785 : 46 : char *bptr = buffer;
6786 : :
6787 [ + + ]: 187 : while (*word)
6788 : : {
6789 [ + + - + ]: 141 : if (*word == '_' || *word == '%')
6790 : 2 : *bptr++ = '\\';
6791 [ - + ]: 141 : if (IS_HIGHBIT_SET(*word))
6792 : : {
6793 : : /*
6794 : : * Transfer multibyte characters without further processing, to
6795 : : * avoid getting confused in unsafe client encodings.
6796 : : */
1581 tgl@sss.pgh.pa.us 6797 :UBC 0 : int chlen = PQmblenBounded(word, pset.encoding);
6798 : :
6799 [ # # ]: 0 : while (chlen-- > 0)
6800 : 0 : *bptr++ = *word++;
6801 : : }
6802 : : else
1581 tgl@sss.pgh.pa.us 6803 :CBC 141 : *bptr++ = *word++;
6804 : : }
6805 : 46 : *bptr++ = '%';
6806 : 46 : *bptr = '\0';
6807 : :
6808 : 46 : result = escape_string(buffer);
6809 : 46 : free(buffer);
6810 : 46 : return result;
6811 : : }
6812 : :
6813 : :
6814 : : /*
6815 : : * parse_identifier - Parse a possibly-schema-qualified SQL identifier.
6816 : : *
6817 : : * This involves splitting off the schema name if present, de-quoting,
6818 : : * and downcasing any unquoted text. We are a bit laxer than the backend
6819 : : * in that we allow just portions of a name to be quoted --- that's because
6820 : : * psql metacommands have traditionally behaved that way.
6821 : : *
6822 : : * Outputs are a malloc'd schema name (NULL if none), malloc'd object name,
6823 : : * and booleans telling whether any part of the schema and object name was
6824 : : * double-quoted.
6825 : : */
6826 : : static void
6827 : 59 : parse_identifier(const char *ident,
6828 : : char **schemaname, char **objectname,
6829 : : bool *schemaquoted, bool *objectquoted)
6830 : : {
6831 : 59 : size_t buflen = strlen(ident) + 1;
6832 : 59 : bool enc_is_single_byte = (pg_encoding_max_length(pset.encoding) == 1);
6833 : : char *sname;
6834 : : char *oname;
6835 : : char *optr;
6836 : : bool inquotes;
6837 : :
6838 : : /* Initialize, making a certainly-large-enough output buffer */
6839 : 59 : sname = NULL;
6840 : 59 : oname = pg_malloc(buflen);
6841 : 59 : *schemaquoted = *objectquoted = false;
6842 : : /* Scan */
6843 : 59 : optr = oname;
6844 : 59 : inquotes = false;
6845 [ + + ]: 293 : while (*ident)
6846 : : {
6847 : 234 : unsigned char ch = (unsigned char) *ident++;
6848 : :
6849 [ + + ]: 234 : if (ch == '"')
6850 : : {
6851 [ + + - + ]: 7 : if (inquotes && *ident == '"')
6852 : : {
6853 : : /* two quote marks within a quoted identifier = emit quote */
1581 tgl@sss.pgh.pa.us 6854 :UBC 0 : *optr++ = '"';
6855 : 0 : ident++;
6856 : : }
6857 : : else
6858 : : {
1581 tgl@sss.pgh.pa.us 6859 :CBC 7 : inquotes = !inquotes;
6860 : 7 : *objectquoted = true;
6861 : : }
6862 : : }
6863 [ + + + - ]: 227 : else if (ch == '.' && !inquotes)
6864 : : {
6865 : : /* Found a schema name, transfer it to sname / *schemaquoted */
6866 : 4 : *optr = '\0';
6867 : 4 : free(sname); /* drop any catalog name */
6868 : 4 : sname = oname;
6869 : 4 : oname = pg_malloc(buflen);
6870 : 4 : optr = oname;
6871 : 4 : *schemaquoted = *objectquoted;
6872 : 4 : *objectquoted = false;
6873 : : }
6874 [ + - - + ]: 223 : else if (!enc_is_single_byte && IS_HIGHBIT_SET(ch))
1581 tgl@sss.pgh.pa.us 6875 :UBC 0 : {
6876 : : /*
6877 : : * Transfer multibyte characters without further processing. They
6878 : : * wouldn't be affected by our downcasing rule anyway, and this
6879 : : * avoids possibly doing the wrong thing in unsafe client
6880 : : * encodings.
6881 : : */
6882 : 0 : int chlen = PQmblenBounded(ident - 1, pset.encoding);
6883 : :
6884 : 0 : *optr++ = (char) ch;
6885 [ # # ]: 0 : while (--chlen > 0)
6886 : 0 : *optr++ = *ident++;
6887 : : }
6888 : : else
6889 : : {
1581 tgl@sss.pgh.pa.us 6890 [ + + ]:CBC 223 : if (!inquotes)
6891 : : {
6892 : : /*
6893 : : * This downcasing transformation should match the backend's
6894 : : * downcase_identifier() as best we can. We do not know the
6895 : : * backend's locale, though, so it's necessarily approximate.
6896 : : * We assume that psql is operating in the same locale and
6897 : : * encoding as the backend.
6898 : : */
6899 [ + + + + ]: 199 : if (ch >= 'A' && ch <= 'Z')
6900 : 28 : ch += 'a' - 'A';
6901 [ - + - - : 171 : else if (enc_is_single_byte && IS_HIGHBIT_SET(ch) && isupper(ch))
- - ]
1581 tgl@sss.pgh.pa.us 6902 :UBC 0 : ch = tolower(ch);
6903 : : }
1581 tgl@sss.pgh.pa.us 6904 :CBC 223 : *optr++ = (char) ch;
6905 : : }
6906 : : }
6907 : :
6908 : 59 : *optr = '\0';
6909 : 59 : *schemaname = sname;
6910 : 59 : *objectname = oname;
6911 : 59 : }
6912 : :
6913 : :
6914 : : /*
6915 : : * requote_identifier - Reconstruct a possibly-schema-qualified SQL identifier.
6916 : : *
6917 : : * Build a malloc'd string containing the identifier, with quoting applied
6918 : : * as necessary. This is more or less the inverse of parse_identifier;
6919 : : * in particular, if an input component was quoted, we'll quote the output
6920 : : * even when that isn't strictly required.
6921 : : *
6922 : : * Unlike parse_identifier, we handle the case where a schema and no
6923 : : * object name is provided, producing just "schema.".
6924 : : */
6925 : : static char *
6926 : 33 : requote_identifier(const char *schemaname, const char *objectname,
6927 : : bool quote_schema, bool quote_object)
6928 : : {
6929 : : char *result;
6930 : 33 : size_t buflen = 1; /* count the trailing \0 */
6931 : : char *ptr;
6932 : :
6933 : : /*
6934 : : * We could use PQescapeIdentifier for some of this, but not all, and it
6935 : : * adds more notational cruft than it seems worth.
6936 : : */
6937 [ + + ]: 33 : if (schemaname)
6938 : : {
6939 : 4 : buflen += strlen(schemaname) + 1; /* +1 for the dot */
6940 [ + - ]: 4 : if (!quote_schema)
6941 : 4 : quote_schema = identifier_needs_quotes(schemaname);
6942 [ - + ]: 4 : if (quote_schema)
6943 : : {
1581 tgl@sss.pgh.pa.us 6944 :UBC 0 : buflen += 2; /* account for quote marks */
6945 [ # # ]: 0 : for (const char *p = schemaname; *p; p++)
6946 : : {
6947 [ # # ]: 0 : if (*p == '"')
6948 : 0 : buflen++;
6949 : : }
6950 : : }
6951 : : }
1581 tgl@sss.pgh.pa.us 6952 [ + + ]:CBC 33 : if (objectname)
6953 : : {
6954 : 32 : buflen += strlen(objectname);
6955 [ + + ]: 32 : if (!quote_object)
6956 : 24 : quote_object = identifier_needs_quotes(objectname);
6957 [ + + ]: 32 : if (quote_object)
6958 : : {
6959 : 8 : buflen += 2; /* account for quote marks */
6960 [ + + ]: 73 : for (const char *p = objectname; *p; p++)
6961 : : {
6962 [ - + ]: 65 : if (*p == '"')
1581 tgl@sss.pgh.pa.us 6963 :UBC 0 : buflen++;
6964 : : }
6965 : : }
6966 : : }
1581 tgl@sss.pgh.pa.us 6967 :CBC 33 : result = pg_malloc(buflen);
6968 : 33 : ptr = result;
6969 [ + + ]: 33 : if (schemaname)
6970 : : {
6971 [ - + ]: 4 : if (quote_schema)
1581 tgl@sss.pgh.pa.us 6972 :UBC 0 : *ptr++ = '"';
1581 tgl@sss.pgh.pa.us 6973 [ + + ]:CBC 28 : for (const char *p = schemaname; *p; p++)
6974 : : {
6975 : 24 : *ptr++ = *p;
6976 [ - + ]: 24 : if (*p == '"')
1581 tgl@sss.pgh.pa.us 6977 :UBC 0 : *ptr++ = '"';
6978 : : }
1581 tgl@sss.pgh.pa.us 6979 [ - + ]:CBC 4 : if (quote_schema)
1581 tgl@sss.pgh.pa.us 6980 :UBC 0 : *ptr++ = '"';
1581 tgl@sss.pgh.pa.us 6981 :CBC 4 : *ptr++ = '.';
6982 : : }
6983 [ + + ]: 33 : if (objectname)
6984 : : {
6985 [ + + ]: 32 : if (quote_object)
6986 : 8 : *ptr++ = '"';
6987 [ + + ]: 282 : for (const char *p = objectname; *p; p++)
6988 : : {
6989 : 250 : *ptr++ = *p;
6990 [ - + ]: 250 : if (*p == '"')
1581 tgl@sss.pgh.pa.us 6991 :UBC 0 : *ptr++ = '"';
6992 : : }
1581 tgl@sss.pgh.pa.us 6993 [ + + ]:CBC 32 : if (quote_object)
6994 : 8 : *ptr++ = '"';
6995 : : }
6996 : 33 : *ptr = '\0';
6997 : 33 : return result;
6998 : : }
6999 : :
7000 : :
7001 : : /*
7002 : : * Detect whether an identifier must be double-quoted.
7003 : : *
7004 : : * Note we'll quote anything that's not ASCII; the backend's quote_ident()
7005 : : * does the same. Perhaps this could be relaxed in future.
7006 : : */
7007 : : static bool
7008 : 53 : identifier_needs_quotes(const char *ident)
7009 : : {
7010 : : int kwnum;
7011 : :
7012 : : /* Check syntax. */
7013 [ + - - + : 53 : if (!((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_'))
- - ]
1581 tgl@sss.pgh.pa.us 7014 :UBC 0 : return true;
984 heikki.linnakangas@i 7015 [ - + ]:CBC 53 : if (strspn(ident, "abcdefghijklmnopqrstuvwxyz0123456789_$") != strlen(ident))
1581 tgl@sss.pgh.pa.us 7016 :UBC 0 : return true;
7017 : :
7018 : : /*
7019 : : * Check for keyword. We quote keywords except for unreserved ones.
7020 : : *
7021 : : * It is possible that our keyword list doesn't quite agree with the
7022 : : * server's, but this should be close enough for tab-completion purposes.
7023 : : *
7024 : : * Note: ScanKeywordLookup() does case-insensitive comparison, but that's
7025 : : * fine, since we already know we have all-lower-case.
7026 : : */
1581 tgl@sss.pgh.pa.us 7027 :CBC 53 : kwnum = ScanKeywordLookup(ident, &ScanKeywords);
7028 : :
7029 [ - + - - ]: 53 : if (kwnum >= 0 && ScanKeywordCategories[kwnum] != UNRESERVED_KEYWORD)
1581 tgl@sss.pgh.pa.us 7030 :UBC 0 : return true;
7031 : :
1581 tgl@sss.pgh.pa.us 7032 :CBC 53 : return false;
7033 : : }
7034 : :
7035 : :
7036 : : /*
7037 : : * Execute a query, returning NULL if there was any error.
7038 : : * This should be the preferred way of talking to the database in this file.
7039 : : */
7040 : : static PGresult *
7502 bruce@momjian.us 7041 : 48 : exec_query(const char *query)
7042 : : {
7043 : : PGresult *result;
7044 : :
7045 [ + - + - : 48 : if (query == NULL || !pset.db || PQstatus(pset.db) != CONNECTION_OK)
- + ]
7502 bruce@momjian.us 7046 :UBC 0 : return NULL;
7047 : :
7502 bruce@momjian.us 7048 :CBC 48 : result = PQexec(pset.db, query);
7049 : :
6496 tgl@sss.pgh.pa.us 7050 [ - + ]: 48 : if (PQresultStatus(result) != PGRES_TUPLES_OK)
7051 : : {
7052 : : /*
7053 : : * Printing an error while the user is typing would be quite annoying,
7054 : : * so we don't. This does complicate debugging of this code; but you
7055 : : * can look in the server log instead.
7056 : : */
7057 : : #ifdef NOT_USED
7058 : : pg_log_error("tab completion query failed: %s\nQuery was:\n%s",
7059 : : PQerrorMessage(pset.db), query);
7060 : : #endif
7502 bruce@momjian.us 7061 :UBC 0 : PQclear(result);
7062 : 0 : result = NULL;
7063 : : }
7064 : :
7502 bruce@momjian.us 7065 :CBC 48 : return result;
7066 : : }
7067 : :
7068 : :
7069 : : /*
7070 : : * Parse all the word(s) before point.
7071 : : *
7072 : : * Returns a malloc'd array of character pointers that point into the malloc'd
7073 : : * data array returned to *buffer; caller must free() both of these when done.
7074 : : * *nwords receives the number of words found, ie, the valid length of the
7075 : : * return array.
7076 : : *
7077 : : * Words are returned right to left, that is, previous_words[0] gets the last
7078 : : * word before point, previous_words[1] the next-to-last, etc.
7079 : : */
7080 : : static char **
3814 tgl@sss.pgh.pa.us 7081 : 77 : get_previous_words(int point, char **buffer, int *nwords)
7082 : : {
7083 : : char **previous_words;
7084 : : char *buf;
7085 : : char *outptr;
3815 7086 : 77 : int words_found = 0;
7087 : : int i;
7088 : :
7089 : : /*
7090 : : * If we have anything in tab_completion_query_buf, paste it together with
7091 : : * rl_line_buffer to construct the full query. Otherwise we can just use
7092 : : * rl_line_buffer as the input string.
7093 : : */
3813 7094 [ + - + + ]: 77 : if (tab_completion_query_buf && tab_completion_query_buf->len > 0)
7095 : : {
7096 : 3 : i = tab_completion_query_buf->len;
7097 : 3 : buf = pg_malloc(point + i + 2);
7098 : 3 : memcpy(buf, tab_completion_query_buf->data, i);
3814 7099 : 3 : buf[i++] = '\n';
3813 7100 : 3 : memcpy(buf + i, rl_line_buffer, point);
7101 : 3 : i += point;
7102 : 3 : buf[i] = '\0';
7103 : : /* Readjust point to reference appropriate offset in buf */
7104 : 3 : point = i;
7105 : : }
7106 : : else
7107 : 74 : buf = rl_line_buffer;
7108 : :
7109 : : /*
7110 : : * Allocate an array of string pointers and a buffer to hold the strings
7111 : : * themselves. The worst case is that the line contains only
7112 : : * non-whitespace WORD_BREAKS characters, making each one a separate word.
7113 : : * This is usually much more space than we need, but it's cheaper than
7114 : : * doing a separate malloc() for each word.
7115 : : */
92 michael@paquier.xyz 7116 :GNC 77 : previous_words = pg_malloc_array(char *, point);
3813 tgl@sss.pgh.pa.us 7117 :CBC 77 : *buffer = outptr = (char *) pg_malloc(point * 2);
7118 : :
7119 : : /*
7120 : : * First we look for a non-word char before the current point. (This is
7121 : : * probably useless, if readline is on the same page as we are about what
7122 : : * is a word, but if so it's cheap.)
7123 : : */
5946 itagaki.takahiro@gma 7124 [ + + ]: 83 : for (i = point - 1; i >= 0; i--)
7125 : : {
7126 [ + + ]: 80 : if (strchr(WORD_BREAKS, buf[i]))
7127 : 74 : break;
7128 : : }
7129 : 77 : point = i;
7130 : :
7131 : : /*
7132 : : * Now parse words, working backwards, until we hit start of line. The
7133 : : * backwards scan has some interesting but intentional properties
7134 : : * concerning parenthesis handling.
7135 : : */
3814 tgl@sss.pgh.pa.us 7136 [ + + ]: 310 : while (point >= 0)
7137 : : {
7138 : : int start,
7139 : : end;
7140 : 233 : bool inquotes = false;
7141 : 233 : int parentheses = 0;
7142 : :
7143 : : /* now find the first non-space which then constitutes the end */
5336 7144 : 233 : end = -1;
5946 itagaki.takahiro@gma 7145 [ + - ]: 472 : for (i = point; i >= 0; i--)
7146 : : {
5336 tgl@sss.pgh.pa.us 7147 [ + + ]: 472 : if (!isspace((unsigned char) buf[i]))
7148 : : {
7502 bruce@momjian.us 7149 : 233 : end = i;
7150 : 233 : break;
7151 : : }
7152 : : }
7153 : : /* if no end found, we're done */
3814 tgl@sss.pgh.pa.us 7154 [ - + ]: 233 : if (end < 0)
3814 tgl@sss.pgh.pa.us 7155 :UBC 0 : break;
7156 : :
7157 : : /*
7158 : : * Otherwise we now look for the start. The start is either the last
7159 : : * character before any word-break character going backwards from the
7160 : : * end, or it's simply character 0. We also handle open quotes and
7161 : : * parentheses.
7162 : : */
3814 tgl@sss.pgh.pa.us 7163 [ + + ]:CBC 1193 : for (start = end; start > 0; start--)
7164 : : {
7165 [ + + ]: 1119 : if (buf[start] == '"')
7166 : 2 : inquotes = !inquotes;
7167 [ + + ]: 1119 : if (!inquotes)
7168 : : {
7169 [ - + ]: 1114 : if (buf[start] == ')')
3814 tgl@sss.pgh.pa.us 7170 :UBC 0 : parentheses++;
3814 tgl@sss.pgh.pa.us 7171 [ + + ]:CBC 1114 : else if (buf[start] == '(')
7172 : : {
7173 [ + - ]: 3 : if (--parentheses <= 0)
5946 itagaki.takahiro@gma 7174 : 3 : break;
7175 : : }
3814 tgl@sss.pgh.pa.us 7176 [ + - ]: 1111 : else if (parentheses == 0 &&
7177 [ + + ]: 1111 : strchr(WORD_BREAKS, buf[start - 1]))
7178 : 156 : break;
7179 : : }
7180 : : }
7181 : :
7182 : : /* Return the word located at start to end inclusive */
3813 7183 : 233 : previous_words[words_found++] = outptr;
7184 : 233 : i = end - start + 1;
7185 : 233 : memcpy(outptr, &buf[start], i);
7186 : 233 : outptr += i;
7187 : 233 : *outptr++ = '\0';
7188 : :
7189 : : /* Continue searching */
3814 7190 : 233 : point = start - 1;
7191 : : }
7192 : :
7193 : : /* Release parsing input workspace, if we made one above */
3813 7194 [ + + ]: 77 : if (buf != rl_line_buffer)
7195 : 3 : free(buf);
7196 : :
3814 7197 : 77 : *nwords = words_found;
7198 : 77 : return previous_words;
7199 : : }
7200 : :
7201 : : /*
7202 : : * Look up the type for the GUC variable with the passed name.
7203 : : *
7204 : : * Returns NULL if the variable is unknown. Otherwise the returned string,
7205 : : * containing the type, has to be freed.
7206 : : */
7207 : : static char *
3917 andres@anarazel.de 7208 : 2 : get_guctype(const char *varname)
7209 : : {
7210 : : PQExpBufferData query_buffer;
7211 : : char *e_varname;
7212 : : PGresult *result;
7213 : 2 : char *guctype = NULL;
7214 : :
7215 : 2 : e_varname = escape_string(varname);
7216 : :
7217 : 2 : initPQExpBuffer(&query_buffer);
7218 : 2 : appendPQExpBuffer(&query_buffer,
7219 : : "SELECT vartype FROM pg_catalog.pg_settings "
7220 : : "WHERE pg_catalog.lower(name) = pg_catalog.lower('%s')",
7221 : : e_varname);
7222 : :
7223 : 2 : result = exec_query(query_buffer.data);
7224 : 2 : termPQExpBuffer(&query_buffer);
7225 : 2 : free(e_varname);
7226 : :
7227 [ + - + - ]: 2 : if (PQresultStatus(result) == PGRES_TUPLES_OK && PQntuples(result) > 0)
7228 : 2 : guctype = pg_strdup(PQgetvalue(result, 0, 0));
7229 : :
7230 : 2 : PQclear(result);
7231 : :
7232 : 2 : return guctype;
7233 : : }
7234 : :
7235 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
7236 : :
7237 : : /*
7238 : : * Quote a filename according to SQL rules, returning a malloc'd string.
7239 : : * completion_charp must point to escape character or '\0', and
7240 : : * completion_force_quote must be set correctly, as per comments for
7241 : : * complete_from_files().
7242 : : */
7243 : : static char *
2319 tgl@sss.pgh.pa.us 7244 : 5 : quote_file_name(char *fname, int match_type, char *quote_pointer)
7245 : : {
7246 : : char *s;
7247 : : struct stat statbuf;
7248 : :
7249 : : /* Quote if needed. */
7250 : 5 : s = quote_if_needed(fname, " \t\r\n\"`",
7251 : 5 : '\'', *completion_charp,
7252 : : completion_force_quote,
7253 : : pset.encoding);
7254 [ + + ]: 5 : if (!s)
7255 : 2 : s = pg_strdup(fname);
7256 : :
7257 : : /*
7258 : : * However, some of the time we have to strip the trailing quote from what
7259 : : * we send back. Never strip the trailing quote if the user already typed
7260 : : * one; otherwise, suppress the trailing quote if we have multiple/no
7261 : : * matches (because we don't want to add a quote if the input is seemingly
7262 : : * unfinished), or if the input was already quoted (because Readline will
7263 : : * do arguably-buggy things otherwise), or if the file does not exist, or
7264 : : * if it's a directory.
7265 : : */
7266 [ + + ]: 5 : if (*s == '\'' &&
7267 [ + - + + ]: 3 : completion_last_char != '\'' &&
7268 [ + - ]: 1 : (match_type != SINGLE_MATCH ||
7269 [ + - + - ]: 2 : (quote_pointer && *quote_pointer == '\'') ||
7270 : 1 : stat(fname, &statbuf) != 0 ||
7271 [ - + ]: 1 : S_ISDIR(statbuf.st_mode)))
7272 : : {
7273 : 2 : char *send = s + strlen(s) - 1;
7274 : :
7275 [ - + ]: 2 : Assert(*send == '\'');
7276 : 2 : *send = '\0';
7277 : : }
7278 : :
7279 : : /*
7280 : : * And now we can let Readline do its thing with possibly adding a quote
7281 : : * on its own accord. (This covers some additional cases beyond those
7282 : : * dealt with above.)
7283 : : */
7284 : : #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
7285 : 5 : rl_completion_suppress_quote = 0;
7286 : : #endif
7287 : :
7288 : : /*
7289 : : * If user typed a leading quote character other than single quote (i.e.,
7290 : : * double quote), zap it, so that we replace it with the correct single
7291 : : * quote.
7292 : : */
7293 [ + - + + ]: 5 : if (quote_pointer && *quote_pointer != '\'')
7294 : 4 : *quote_pointer = '\0';
7295 : :
7502 bruce@momjian.us 7296 : 5 : return s;
7297 : : }
7298 : :
7299 : : /*
7300 : : * Dequote a filename, if it's quoted.
7301 : : * completion_charp must point to escape character or '\0', as per
7302 : : * comments for complete_from_files().
7303 : : */
7304 : : static char *
2319 tgl@sss.pgh.pa.us 7305 : 12 : dequote_file_name(char *fname, int quote_char)
7306 : : {
7307 : : char *unquoted_fname;
7308 : :
7309 : : /*
7310 : : * If quote_char is set, it's not included in "fname". We have to add it
7311 : : * or strtokx will not interpret the string correctly (notably, it won't
7312 : : * recognize escapes).
7313 : : */
7314 [ + + ]: 12 : if (quote_char == '\'')
7315 : : {
7316 : 6 : char *workspace = (char *) pg_malloc(strlen(fname) + 2);
7317 : :
7318 : 6 : workspace[0] = quote_char;
7319 : 6 : strcpy(workspace + 1, fname);
7320 : 6 : unquoted_fname = strtokx(workspace, "", NULL, "'", *completion_charp,
7321 : : false, true, pset.encoding);
7322 : 6 : free(workspace);
7323 : : }
7324 : : else
7325 : 6 : unquoted_fname = strtokx(fname, "", NULL, "'", *completion_charp,
7326 : : false, true, pset.encoding);
7327 : :
7328 : : /* expect a NULL return for the empty string only */
7329 [ - + ]: 12 : if (!unquoted_fname)
7330 : : {
2319 tgl@sss.pgh.pa.us 7331 [ # # ]:UBC 0 : Assert(*fname == '\0');
7332 : 0 : unquoted_fname = fname;
7333 : : }
7334 : :
7335 : : /* readline expects a malloc'd result that it is to free */
2319 tgl@sss.pgh.pa.us 7336 :CBC 12 : return pg_strdup(unquoted_fname);
7337 : : }
7338 : :
7339 : : #endif /* USE_FILENAME_QUOTING_FUNCTIONS */
7340 : :
7341 : : #endif /* USE_READLINE */
|