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