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