Age Owner Branch data TLA Line data Source code
1 : : /*
2 : : * psql - the PostgreSQL interactive terminal
3 : : *
4 : : * Copyright (c) 2000-2026, PostgreSQL Global Development Group
5 : : *
6 : : * src/bin/psql/tab-complete.in.c
7 : : *
8 : : * Note: this will compile and work as-is if SWITCH_CONVERSION_APPLIED
9 : : * is not defined. However, the expected usage is that it's first run
10 : : * through gen_tabcomplete.pl, which will #define that symbol, fill in the
11 : : * tcpatterns[] array, and convert the else-if chain in match_previous_words()
12 : : * into a switch. See comments for match_previous_words() and the header
13 : : * comment in gen_tabcomplete.pl for more detail.
14 : : */
15 : :
16 : : /*----------------------------------------------------------------------
17 : : * This file implements a somewhat more sophisticated readline "TAB
18 : : * completion" in psql. It is not intended to be AI, to replace
19 : : * learning SQL, or to relieve you from thinking about what you're
20 : : * doing. Also it does not always give you all the syntactically legal
21 : : * completions, only those that are the most common or the ones that
22 : : * the programmer felt most like implementing.
23 : : *
24 : : * CAVEAT: Tab completion causes queries to be sent to the backend.
25 : : * The number of tuples returned gets limited, in most default
26 : : * installations to 1000, but if you still don't like this prospect,
27 : : * you can turn off tab completion in your ~/.inputrc (or else
28 : : * ${INPUTRC}) file so:
29 : : *
30 : : * $if psql
31 : : * set disable-completion on
32 : : * $endif
33 : : *
34 : : * See `man 3 readline' or `info readline' for the full details.
35 : : *
36 : : * BUGS:
37 : : * - Quotes, parentheses, and other funny characters are not handled
38 : : * all that gracefully.
39 : : *----------------------------------------------------------------------
40 : : */
41 : :
42 : : #include "postgres_fe.h"
43 : :
44 : : #include "input.h"
45 : : #include "tab-complete.h"
46 : :
47 : : /* If we don't have this, we might as well forget about the whole thing: */
48 : : #ifdef USE_READLINE
49 : :
50 : : #include <ctype.h>
51 : : #include <sys/stat.h>
52 : :
53 : : #include "catalog/pg_am_d.h"
54 : : #include "catalog/pg_class_d.h"
55 : : #include "common.h"
56 : : #include "common/keywords.h"
57 : : #include "libpq-fe.h"
58 : : #include "mb/pg_wchar.h"
59 : : #include "pqexpbuffer.h"
60 : : #include "settings.h"
61 : : #include "stringutils.h"
62 : :
63 : : /*
64 : : * Ancient versions of libedit provide filename_completion_function()
65 : : * instead of rl_filename_completion_function(). Likewise for
66 : : * [rl_]completion_matches().
67 : : */
68 : : #ifndef HAVE_RL_FILENAME_COMPLETION_FUNCTION
69 : : #define rl_filename_completion_function filename_completion_function
70 : : #endif
71 : :
72 : : #ifndef HAVE_RL_COMPLETION_MATCHES
73 : : #define rl_completion_matches completion_matches
74 : : #endif
75 : :
76 : : /*
77 : : * Currently we assume that rl_filename_dequoting_function exists if
78 : : * rl_filename_quoting_function does. If that proves not to be the case,
79 : : * we'd need to test for the former, or possibly both, in configure.
80 : : */
81 : : #ifdef HAVE_RL_FILENAME_QUOTING_FUNCTION
82 : : #define USE_FILENAME_QUOTING_FUNCTIONS 1
83 : : #endif
84 : :
85 : : /* word break characters */
86 : : #define WORD_BREAKS "\t\n@><=;|&() "
87 : :
88 : : /*
89 : : * Since readline doesn't let us pass any state through to the tab completion
90 : : * callback, we have to use this global variable to let get_previous_words()
91 : : * get at the previous lines of the current command. Ick.
92 : : */
93 : : PQExpBuffer tab_completion_query_buf = NULL;
94 : :
95 : : /*
96 : : * In some situations, the query to find out what names are available to
97 : : * complete with must vary depending on server version. We handle this by
98 : : * storing a list of queries, each tagged with the minimum server version
99 : : * it will work for. Each list must be stored in descending server version
100 : : * order, so that the first satisfactory query is the one to use.
101 : : *
102 : : * When the query string is otherwise constant, an array of VersionedQuery
103 : : * suffices. Terminate the array with an entry having min_server_version = 0.
104 : : * That entry's query string can be a query that works in all supported older
105 : : * server versions, or NULL to give up and do no completion.
106 : : */
107 : : typedef struct VersionedQuery
108 : : {
109 : : int min_server_version;
110 : : const char *query;
111 : : } VersionedQuery;
112 : :
113 : : /*
114 : : * This struct is used to define "schema queries", which are custom-built
115 : : * to obtain possibly-schema-qualified names of database objects. There is
116 : : * enough similarity in the structure that we don't want to repeat it each
117 : : * time. So we put the components of each query into this struct and
118 : : * assemble them with the common boilerplate in _complete_from_query().
119 : : *
120 : : * We also use this struct to define queries that use completion_ref_object,
121 : : * which is some object related to the one(s) we want to get the names of
122 : : * (for example, the table we want the indexes of). In that usage the
123 : : * objects we're completing might not have a schema of their own, but the
124 : : * reference object almost always does (passed in completion_ref_schema).
125 : : *
126 : : * As with VersionedQuery, we can use an array of these if the query details
127 : : * must vary across versions.
128 : : */
129 : : typedef struct SchemaQuery
130 : : {
131 : : /*
132 : : * If not zero, minimum server version this struct applies to. If not
133 : : * zero, there should be a following struct with a smaller minimum server
134 : : * version; use catname == NULL in the last entry if we should do nothing.
135 : : */
136 : : int min_server_version;
137 : :
138 : : /*
139 : : * Name of catalog or catalogs to be queried, with alias(es), eg.
140 : : * "pg_catalog.pg_class c". Note that "pg_namespace n" and/or
141 : : * "pg_namespace nr" will be added automatically when needed.
142 : : */
143 : : const char *catname;
144 : :
145 : : /*
146 : : * Selection condition --- only rows meeting this condition are candidates
147 : : * to display. If catname mentions multiple tables, include the necessary
148 : : * join condition here. For example, this might look like "c.relkind = "
149 : : * CppAsString2(RELKIND_RELATION). Write NULL (not an empty string) if
150 : : * not needed.
151 : : */
152 : : const char *selcondition;
153 : :
154 : : /*
155 : : * Visibility condition --- which rows are visible without schema
156 : : * qualification? For example, "pg_catalog.pg_table_is_visible(c.oid)".
157 : : * NULL if not needed.
158 : : */
159 : : const char *viscondition;
160 : :
161 : : /*
162 : : * Namespace --- name of field to join to pg_namespace.oid when there is
163 : : * schema qualification. For example, "c.relnamespace". NULL if we don't
164 : : * want to join to pg_namespace (then any schema part in the input word
165 : : * will be ignored).
166 : : */
167 : : const char *namespace;
168 : :
169 : : /*
170 : : * Result --- the base object name to return. For example, "c.relname".
171 : : */
172 : : const char *result;
173 : :
174 : : /*
175 : : * In some cases, it's difficult to keep the query from returning the same
176 : : * object multiple times. Specify use_distinct to filter out duplicates.
177 : : */
178 : : bool use_distinct;
179 : :
180 : : /*
181 : : * Additional literal strings (usually keywords) to be offered along with
182 : : * the query results. Provide a NULL-terminated array of constant
183 : : * strings, or NULL if none.
184 : : */
185 : : const char *const *keywords;
186 : :
187 : : /*
188 : : * If this query uses completion_ref_object/completion_ref_schema,
189 : : * populate the remaining fields, else leave them NULL. When using this
190 : : * capability, catname must include the catalog that defines the
191 : : * completion_ref_object, and selcondition must include the join condition
192 : : * that connects it to the result's catalog.
193 : : *
194 : : * refname is the field that should be equated to completion_ref_object,
195 : : * for example "cr.relname".
196 : : */
197 : : const char *refname;
198 : :
199 : : /*
200 : : * Visibility condition to use when completion_ref_schema is not set. For
201 : : * example, "pg_catalog.pg_table_is_visible(cr.oid)". NULL if not needed.
202 : : */
203 : : const char *refviscondition;
204 : :
205 : : /*
206 : : * Name of field to join to pg_namespace.oid when completion_ref_schema is
207 : : * set. For example, "cr.relnamespace". NULL if we don't want to
208 : : * consider completion_ref_schema.
209 : : */
210 : : const char *refnamespace;
211 : : } SchemaQuery;
212 : :
213 : :
214 : : /*
215 : : * Store maximum number of records we want from database queries
216 : : * (implemented via SELECT ... LIMIT xx).
217 : : */
218 : : static int completion_max_records;
219 : :
220 : : /*
221 : : * Communication variables set by psql_completion (mostly in COMPLETE_WITH_FOO
222 : : * macros) and then used by the completion callback functions. Ugly but there
223 : : * is no better way.
224 : : */
225 : : static char completion_last_char; /* last char of input word */
226 : : static const char *completion_charp; /* to pass a string */
227 : : static const char *const *completion_charpp; /* to pass a list of strings */
228 : : static const VersionedQuery *completion_vquery; /* to pass a VersionedQuery */
229 : : static const SchemaQuery *completion_squery; /* to pass a SchemaQuery */
230 : : static char *completion_ref_object; /* name of reference object */
231 : : static char *completion_ref_schema; /* schema name of reference object */
232 : : static bool completion_case_sensitive; /* completion is case sensitive */
233 : : static bool completion_verbatim; /* completion is verbatim */
234 : : static bool completion_force_quote; /* true to force-quote filenames */
235 : :
236 : : /*
237 : : * A few macros to ease typing. You can use these to complete the given
238 : : * string with
239 : : * 1) The result from a query you pass it. (Perhaps one of those below?)
240 : : * We support both simple and versioned queries.
241 : : * 2) The result from a schema query you pass it.
242 : : * We support both simple and versioned schema queries.
243 : : * 3) The items from a null-pointer-terminated list (with or without
244 : : * case-sensitive comparison); if the list is constant you can build it
245 : : * with COMPLETE_WITH() or COMPLETE_WITH_CS(). The QUERY_LIST and
246 : : * QUERY_PLUS forms combine such literal lists with a query result.
247 : : * 4) The list of attributes of the given table (possibly schema-qualified).
248 : : * 5) The list of arguments to the given function (possibly schema-qualified).
249 : : *
250 : : * The query is generally expected to return raw SQL identifiers; matching
251 : : * to what the user typed is done in a quoting-aware fashion. If what is
252 : : * returned is not SQL identifiers, use one of the VERBATIM forms, in which
253 : : * case the query results are matched to the user's text without double-quote
254 : : * processing (so if quoting is needed, you must provide it in the query
255 : : * results).
256 : : */
257 : : #define COMPLETE_WITH_QUERY(query) \
258 : : COMPLETE_WITH_QUERY_LIST(query, NULL)
259 : :
260 : : #define COMPLETE_WITH_QUERY_LIST(query, list) \
261 : : do { \
262 : : completion_charp = query; \
263 : : completion_charpp = list; \
264 : : completion_verbatim = false; \
265 : : matches = rl_completion_matches(text, complete_from_query); \
266 : : } while (0)
267 : :
268 : : #define COMPLETE_WITH_QUERY_PLUS(query, ...) \
269 : : do { \
270 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
271 : : COMPLETE_WITH_QUERY_LIST(query, list); \
272 : : } while (0)
273 : :
274 : : #define COMPLETE_WITH_QUERY_VERBATIM(query) \
275 : : COMPLETE_WITH_QUERY_VERBATIM_LIST(query, NULL)
276 : :
277 : : #define COMPLETE_WITH_QUERY_VERBATIM_LIST(query, list) \
278 : : do { \
279 : : completion_charp = query; \
280 : : completion_charpp = list; \
281 : : completion_verbatim = true; \
282 : : matches = rl_completion_matches(text, complete_from_query); \
283 : : } while (0)
284 : :
285 : : #define COMPLETE_WITH_QUERY_VERBATIM_PLUS(query, ...) \
286 : : do { \
287 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
288 : : COMPLETE_WITH_QUERY_VERBATIM_LIST(query, list); \
289 : : } while (0)
290 : :
291 : : #define COMPLETE_WITH_VERSIONED_QUERY(query) \
292 : : COMPLETE_WITH_VERSIONED_QUERY_LIST(query, NULL)
293 : :
294 : : #define COMPLETE_WITH_VERSIONED_QUERY_LIST(query, list) \
295 : : do { \
296 : : completion_vquery = query; \
297 : : completion_charpp = list; \
298 : : completion_verbatim = false; \
299 : : matches = rl_completion_matches(text, complete_from_versioned_query); \
300 : : } while (0)
301 : :
302 : : #define COMPLETE_WITH_VERSIONED_QUERY_PLUS(query, ...) \
303 : : do { \
304 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
305 : : COMPLETE_WITH_VERSIONED_QUERY_LIST(query, list); \
306 : : } while (0)
307 : :
308 : : #define COMPLETE_WITH_SCHEMA_QUERY(query) \
309 : : COMPLETE_WITH_SCHEMA_QUERY_LIST(query, NULL)
310 : :
311 : : #define COMPLETE_WITH_SCHEMA_QUERY_LIST(query, list) \
312 : : do { \
313 : : completion_squery = &(query); \
314 : : completion_charpp = list; \
315 : : completion_verbatim = false; \
316 : : matches = rl_completion_matches(text, complete_from_schema_query); \
317 : : } while (0)
318 : :
319 : : #define COMPLETE_WITH_SCHEMA_QUERY_PLUS(query, ...) \
320 : : do { \
321 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
322 : : COMPLETE_WITH_SCHEMA_QUERY_LIST(query, list); \
323 : : } while (0)
324 : :
325 : : #define COMPLETE_WITH_SCHEMA_QUERY_VERBATIM(query) \
326 : : do { \
327 : : completion_squery = &(query); \
328 : : completion_charpp = NULL; \
329 : : completion_verbatim = true; \
330 : : matches = rl_completion_matches(text, complete_from_schema_query); \
331 : : } while (0)
332 : :
333 : : #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(query) \
334 : : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, NULL)
335 : :
336 : : #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, list) \
337 : : do { \
338 : : completion_squery = query; \
339 : : completion_charpp = list; \
340 : : completion_verbatim = false; \
341 : : matches = rl_completion_matches(text, complete_from_versioned_schema_query); \
342 : : } while (0)
343 : :
344 : : #define COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_PLUS(query, ...) \
345 : : do { \
346 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
347 : : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(query, list); \
348 : : } while (0)
349 : :
350 : : /*
351 : : * Caution: COMPLETE_WITH_CONST is not for general-purpose use; you probably
352 : : * want COMPLETE_WITH() with one element, instead.
353 : : */
354 : : #define COMPLETE_WITH_CONST(cs, con) \
355 : : do { \
356 : : completion_case_sensitive = (cs); \
357 : : completion_charp = (con); \
358 : : matches = rl_completion_matches(text, complete_from_const); \
359 : : } while (0)
360 : :
361 : : #define COMPLETE_WITH_LIST_INT(cs, list) \
362 : : do { \
363 : : completion_case_sensitive = (cs); \
364 : : completion_charpp = (list); \
365 : : matches = rl_completion_matches(text, complete_from_list); \
366 : : } while (0)
367 : :
368 : : #define COMPLETE_WITH_LIST(list) COMPLETE_WITH_LIST_INT(false, list)
369 : : #define COMPLETE_WITH_LIST_CS(list) COMPLETE_WITH_LIST_INT(true, list)
370 : :
371 : : #define COMPLETE_WITH(...) \
372 : : do { \
373 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
374 : : COMPLETE_WITH_LIST(list); \
375 : : } while (0)
376 : :
377 : : #define COMPLETE_WITH_CS(...) \
378 : : do { \
379 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
380 : : COMPLETE_WITH_LIST_CS(list); \
381 : : } while (0)
382 : :
383 : : #define COMPLETE_WITH_ATTR(relation) \
384 : : COMPLETE_WITH_ATTR_LIST(relation, NULL)
385 : :
386 : : #define COMPLETE_WITH_ATTR_LIST(relation, list) \
387 : : do { \
388 : : set_completion_reference(relation); \
389 : : completion_squery = &(Query_for_list_of_attributes); \
390 : : completion_charpp = list; \
391 : : completion_verbatim = false; \
392 : : matches = rl_completion_matches(text, complete_from_schema_query); \
393 : : } while (0)
394 : :
395 : : #define COMPLETE_WITH_ATTR_PLUS(relation, ...) \
396 : : do { \
397 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
398 : : COMPLETE_WITH_ATTR_LIST(relation, list); \
399 : : } while (0)
400 : :
401 : : /*
402 : : * libedit will typically include the literal's leading single quote in
403 : : * "text", while readline will not. Adapt our offered strings to fit.
404 : : * But include a quote if there's not one just before "text", to get the
405 : : * user off to the right start.
406 : : */
407 : : #define COMPLETE_WITH_ENUM_VALUE(type) \
408 : : do { \
409 : : set_completion_reference(type); \
410 : : if (text[0] == '\'' || \
411 : : start == 0 || rl_line_buffer[start - 1] != '\'') \
412 : : completion_squery = &(Query_for_list_of_enum_values_quoted); \
413 : : else \
414 : : completion_squery = &(Query_for_list_of_enum_values_unquoted); \
415 : : completion_charpp = NULL; \
416 : : completion_verbatim = true; \
417 : : matches = rl_completion_matches(text, complete_from_schema_query); \
418 : : } while (0)
419 : :
420 : : /*
421 : : * Timezone completion is mostly like enum label completion, but we work
422 : : * a little harder since this is a more common use-case.
423 : : */
424 : : #define COMPLETE_WITH_TIMEZONE_NAME() \
425 : : do { \
426 : : static const char *const list[] = { "DEFAULT", NULL }; \
427 : : if (text[0] == '\'') \
428 : : completion_charp = Query_for_list_of_timezone_names_quoted_in; \
429 : : else if (start == 0 || rl_line_buffer[start - 1] != '\'') \
430 : : completion_charp = Query_for_list_of_timezone_names_quoted_out; \
431 : : else \
432 : : completion_charp = Query_for_list_of_timezone_names_unquoted; \
433 : : completion_charpp = list; \
434 : : completion_verbatim = true; \
435 : : matches = rl_completion_matches(text, complete_from_query); \
436 : : } while (0)
437 : :
438 : : #define COMPLETE_WITH_FUNCTION_ARG(function) \
439 : : do { \
440 : : set_completion_reference(function); \
441 : : completion_squery = &(Query_for_list_of_arguments); \
442 : : completion_charpp = NULL; \
443 : : completion_verbatim = true; \
444 : : matches = rl_completion_matches(text, complete_from_schema_query); \
445 : : } while (0)
446 : :
447 : : #define COMPLETE_WITH_FILES_LIST(escape, force_quote, list) \
448 : : do { \
449 : : completion_charp = escape; \
450 : : completion_charpp = list; \
451 : : completion_force_quote = force_quote; \
452 : : matches = rl_completion_matches(text, complete_from_files); \
453 : : } while (0)
454 : :
455 : : #define COMPLETE_WITH_FILES(escape, force_quote) \
456 : : COMPLETE_WITH_FILES_LIST(escape, force_quote, NULL)
457 : :
458 : : #define COMPLETE_WITH_FILES_PLUS(escape, force_quote, ...) \
459 : : do { \
460 : : static const char *const list[] = { __VA_ARGS__, NULL }; \
461 : : COMPLETE_WITH_FILES_LIST(escape, force_quote, list); \
462 : : } while (0)
463 : :
464 : : #define COMPLETE_WITH_GENERATOR(generator) \
465 : : matches = rl_completion_matches(text, generator)
466 : :
467 : : /*
468 : : * Assembly instructions for schema queries
469 : : *
470 : : * Note that toast tables are not included in those queries to avoid
471 : : * unnecessary bloat in the completions generated.
472 : : */
473 : :
474 : : static const SchemaQuery Query_for_constraint_of_table = {
475 : : .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_class c1",
476 : : .selcondition = "con.conrelid=c1.oid",
477 : : .result = "con.conname",
478 : : .refname = "c1.relname",
479 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
480 : : .refnamespace = "c1.relnamespace",
481 : : };
482 : :
483 : : static const SchemaQuery Query_for_constraint_of_table_not_validated = {
484 : : .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_class c1",
485 : : .selcondition = "con.conrelid=c1.oid and not con.convalidated",
486 : : .result = "con.conname",
487 : : .refname = "c1.relname",
488 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
489 : : .refnamespace = "c1.relnamespace",
490 : : };
491 : :
492 : : static const SchemaQuery Query_for_constraint_of_type = {
493 : : .catname = "pg_catalog.pg_constraint con, pg_catalog.pg_type t",
494 : : .selcondition = "con.contypid=t.oid",
495 : : .result = "con.conname",
496 : : .refname = "t.typname",
497 : : .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
498 : : .refnamespace = "t.typnamespace",
499 : : };
500 : :
501 : : static const SchemaQuery Query_for_index_of_table = {
502 : : .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i",
503 : : .selcondition = "c1.oid=i.indrelid and i.indexrelid=c2.oid",
504 : : .result = "c2.relname",
505 : : .refname = "c1.relname",
506 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
507 : : .refnamespace = "c1.relnamespace",
508 : : };
509 : :
510 : : static const SchemaQuery Query_for_unique_index_of_table = {
511 : : .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i",
512 : : .selcondition = "c1.oid=i.indrelid and i.indexrelid=c2.oid and i.indisunique",
513 : : .result = "c2.relname",
514 : : .refname = "c1.relname",
515 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
516 : : .refnamespace = "c1.relnamespace",
517 : : };
518 : :
519 : : static const SchemaQuery Query_for_list_of_aggregates[] = {
520 : : {
521 : : .min_server_version = 110000,
522 : : .catname = "pg_catalog.pg_proc p",
523 : : .selcondition = "p.prokind = 'a'",
524 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
525 : : .namespace = "p.pronamespace",
526 : : .result = "p.proname",
527 : : },
528 : : {
529 : : .catname = "pg_catalog.pg_proc p",
530 : : .selcondition = "p.proisagg",
531 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
532 : : .namespace = "p.pronamespace",
533 : : .result = "p.proname",
534 : : }
535 : : };
536 : :
537 : : static const SchemaQuery Query_for_list_of_arguments = {
538 : : .catname = "pg_catalog.pg_proc p",
539 : : .result = "pg_catalog.oidvectortypes(p.proargtypes)||')'",
540 : : .refname = "p.proname",
541 : : .refviscondition = "pg_catalog.pg_function_is_visible(p.oid)",
542 : : .refnamespace = "p.pronamespace",
543 : : };
544 : :
545 : : static const SchemaQuery Query_for_list_of_attributes = {
546 : : .catname = "pg_catalog.pg_attribute a, pg_catalog.pg_class c",
547 : : .selcondition = "c.oid = a.attrelid and a.attnum > 0 and not a.attisdropped",
548 : : .result = "a.attname",
549 : : .refname = "c.relname",
550 : : .refviscondition = "pg_catalog.pg_table_is_visible(c.oid)",
551 : : .refnamespace = "c.relnamespace",
552 : : };
553 : :
554 : : static const SchemaQuery Query_for_list_of_attribute_numbers = {
555 : : .catname = "pg_catalog.pg_attribute a, pg_catalog.pg_class c",
556 : : .selcondition = "c.oid = a.attrelid and a.attnum > 0 and not a.attisdropped",
557 : : .result = "a.attnum::pg_catalog.text",
558 : : .refname = "c.relname",
559 : : .refviscondition = "pg_catalog.pg_table_is_visible(c.oid)",
560 : : .refnamespace = "c.relnamespace",
561 : : };
562 : :
563 : : static const char *const Keywords_for_list_of_datatypes[] = {
564 : : "bigint",
565 : : "boolean",
566 : : "character",
567 : : "double precision",
568 : : "integer",
569 : : "real",
570 : : "smallint",
571 : :
572 : : /*
573 : : * Note: currently there's no value in offering the following multiword
574 : : * type names, because tab completion cannot succeed for them: we can't
575 : : * disambiguate until somewhere in the second word, at which point we
576 : : * won't have the first word as context. ("double precision" does work,
577 : : * as long as no other type name begins with "double".) Leave them out to
578 : : * encourage users to use the PG-specific aliases, which we can complete.
579 : : */
580 : : #ifdef NOT_USED
581 : : "bit varying",
582 : : "character varying",
583 : : "time with time zone",
584 : : "time without time zone",
585 : : "timestamp with time zone",
586 : : "timestamp without time zone",
587 : : #endif
588 : : NULL
589 : : };
590 : :
591 : : static const SchemaQuery Query_for_list_of_datatypes = {
592 : : .catname = "pg_catalog.pg_type t",
593 : : /* selcondition --- ignore table rowtypes and array types */
594 : : .selcondition = "(t.typrelid = 0 "
595 : : " OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
596 : : " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) "
597 : : "AND t.typname !~ '^_'",
598 : : .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
599 : : .namespace = "t.typnamespace",
600 : : .result = "t.typname",
601 : : .keywords = Keywords_for_list_of_datatypes,
602 : : };
603 : :
604 : : static const SchemaQuery Query_for_list_of_composite_datatypes = {
605 : : .catname = "pg_catalog.pg_type t",
606 : : /* selcondition --- only get composite types */
607 : : .selcondition = "(SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
608 : : " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid) "
609 : : "AND t.typname !~ '^_'",
610 : : .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
611 : : .namespace = "t.typnamespace",
612 : : .result = "t.typname",
613 : : };
614 : :
615 : : static const SchemaQuery Query_for_list_of_domains = {
616 : : .catname = "pg_catalog.pg_type t",
617 : : .selcondition = "t.typtype = 'd'",
618 : : .viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
619 : : .namespace = "t.typnamespace",
620 : : .result = "t.typname",
621 : : };
622 : :
623 : : static const SchemaQuery Query_for_list_of_enum_values_quoted = {
624 : : .catname = "pg_catalog.pg_enum e, pg_catalog.pg_type t",
625 : : .selcondition = "t.oid = e.enumtypid",
626 : : .result = "pg_catalog.quote_literal(enumlabel)",
627 : : .refname = "t.typname",
628 : : .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
629 : : .refnamespace = "t.typnamespace",
630 : : };
631 : :
632 : : static const SchemaQuery Query_for_list_of_enum_values_unquoted = {
633 : : .catname = "pg_catalog.pg_enum e, pg_catalog.pg_type t",
634 : : .selcondition = "t.oid = e.enumtypid",
635 : : .result = "e.enumlabel",
636 : : .refname = "t.typname",
637 : : .refviscondition = "pg_catalog.pg_type_is_visible(t.oid)",
638 : : .refnamespace = "t.typnamespace",
639 : : };
640 : :
641 : : /* Note: this intentionally accepts aggregates as well as plain functions */
642 : : static const SchemaQuery Query_for_list_of_functions[] = {
643 : : {
644 : : .min_server_version = 110000,
645 : : .catname = "pg_catalog.pg_proc p",
646 : : .selcondition = "p.prokind != 'p'",
647 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
648 : : .namespace = "p.pronamespace",
649 : : .result = "p.proname",
650 : : },
651 : : {
652 : : .catname = "pg_catalog.pg_proc p",
653 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
654 : : .namespace = "p.pronamespace",
655 : : .result = "p.proname",
656 : : }
657 : : };
658 : :
659 : : static const SchemaQuery Query_for_list_of_procedures[] = {
660 : : {
661 : : .min_server_version = 110000,
662 : : .catname = "pg_catalog.pg_proc p",
663 : : .selcondition = "p.prokind = 'p'",
664 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
665 : : .namespace = "p.pronamespace",
666 : : .result = "p.proname",
667 : : },
668 : : {
669 : : /* not supported in older versions */
670 : : .catname = NULL,
671 : : }
672 : : };
673 : :
674 : : static const SchemaQuery Query_for_list_of_routines = {
675 : : .catname = "pg_catalog.pg_proc p",
676 : : .viscondition = "pg_catalog.pg_function_is_visible(p.oid)",
677 : : .namespace = "p.pronamespace",
678 : : .result = "p.proname",
679 : : };
680 : :
681 : : static const SchemaQuery Query_for_list_of_sequences = {
682 : : .catname = "pg_catalog.pg_class c",
683 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_SEQUENCE) ")",
684 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
685 : : .namespace = "c.relnamespace",
686 : : .result = "c.relname",
687 : : };
688 : :
689 : : static const SchemaQuery Query_for_list_of_foreign_tables = {
690 : : .catname = "pg_catalog.pg_class c",
691 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_FOREIGN_TABLE) ")",
692 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
693 : : .namespace = "c.relnamespace",
694 : : .result = "c.relname",
695 : : };
696 : :
697 : : static const SchemaQuery Query_for_list_of_tables = {
698 : : .catname = "pg_catalog.pg_class c",
699 : : .selcondition =
700 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
701 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
702 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
703 : : .namespace = "c.relnamespace",
704 : : .result = "c.relname",
705 : : };
706 : :
707 : : static const SchemaQuery Query_for_list_of_partitioned_tables = {
708 : : .catname = "pg_catalog.pg_class c",
709 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
710 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
711 : : .namespace = "c.relnamespace",
712 : : .result = "c.relname",
713 : : };
714 : :
715 : : static const SchemaQuery Query_for_list_of_tables_for_constraint = {
716 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_constraint con",
717 : : .selcondition = "c.oid=con.conrelid and c.relkind IN ("
718 : : CppAsString2(RELKIND_RELATION) ", "
719 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
720 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
721 : : .namespace = "c.relnamespace",
722 : : .result = "c.relname",
723 : : .use_distinct = true,
724 : : .refname = "con.conname",
725 : : };
726 : :
727 : : static const SchemaQuery Query_for_list_of_tables_for_policy = {
728 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_policy p",
729 : : .selcondition = "c.oid=p.polrelid",
730 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
731 : : .namespace = "c.relnamespace",
732 : : .result = "c.relname",
733 : : .use_distinct = true,
734 : : .refname = "p.polname",
735 : : };
736 : :
737 : : static const SchemaQuery Query_for_list_of_tables_for_rule = {
738 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_rewrite r",
739 : : .selcondition = "c.oid=r.ev_class",
740 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
741 : : .namespace = "c.relnamespace",
742 : : .result = "c.relname",
743 : : .use_distinct = true,
744 : : .refname = "r.rulename",
745 : : };
746 : :
747 : : static const SchemaQuery Query_for_list_of_tables_for_trigger = {
748 : : .catname = "pg_catalog.pg_class c, pg_catalog.pg_trigger t",
749 : : .selcondition = "c.oid=t.tgrelid",
750 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
751 : : .namespace = "c.relnamespace",
752 : : .result = "c.relname",
753 : : .use_distinct = true,
754 : : .refname = "t.tgname",
755 : : };
756 : :
757 : : static const SchemaQuery Query_for_list_of_ts_configurations = {
758 : : .catname = "pg_catalog.pg_ts_config c",
759 : : .viscondition = "pg_catalog.pg_ts_config_is_visible(c.oid)",
760 : : .namespace = "c.cfgnamespace",
761 : : .result = "c.cfgname",
762 : : };
763 : :
764 : : static const SchemaQuery Query_for_list_of_ts_dictionaries = {
765 : : .catname = "pg_catalog.pg_ts_dict d",
766 : : .viscondition = "pg_catalog.pg_ts_dict_is_visible(d.oid)",
767 : : .namespace = "d.dictnamespace",
768 : : .result = "d.dictname",
769 : : };
770 : :
771 : : static const SchemaQuery Query_for_list_of_ts_parsers = {
772 : : .catname = "pg_catalog.pg_ts_parser p",
773 : : .viscondition = "pg_catalog.pg_ts_parser_is_visible(p.oid)",
774 : : .namespace = "p.prsnamespace",
775 : : .result = "p.prsname",
776 : : };
777 : :
778 : : static const SchemaQuery Query_for_list_of_ts_templates = {
779 : : .catname = "pg_catalog.pg_ts_template t",
780 : : .viscondition = "pg_catalog.pg_ts_template_is_visible(t.oid)",
781 : : .namespace = "t.tmplnamespace",
782 : : .result = "t.tmplname",
783 : : };
784 : :
785 : : static const SchemaQuery Query_for_list_of_views = {
786 : : .catname = "pg_catalog.pg_class c",
787 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")",
788 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
789 : : .namespace = "c.relnamespace",
790 : : .result = "c.relname",
791 : : };
792 : :
793 : : static const SchemaQuery Query_for_list_of_matviews = {
794 : : .catname = "pg_catalog.pg_class c",
795 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")",
796 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
797 : : .namespace = "c.relnamespace",
798 : : .result = "c.relname",
799 : : };
800 : :
801 : : static const SchemaQuery Query_for_list_of_indexes = {
802 : : .catname = "pg_catalog.pg_class c",
803 : : .selcondition =
804 : : "c.relkind IN (" CppAsString2(RELKIND_INDEX) ", "
805 : : CppAsString2(RELKIND_PARTITIONED_INDEX) ")",
806 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
807 : : .namespace = "c.relnamespace",
808 : : .result = "c.relname",
809 : : };
810 : :
811 : : static const SchemaQuery Query_for_list_of_partitioned_indexes = {
812 : : .catname = "pg_catalog.pg_class c",
813 : : .selcondition = "c.relkind = " CppAsString2(RELKIND_PARTITIONED_INDEX),
814 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
815 : : .namespace = "c.relnamespace",
816 : : .result = "c.relname",
817 : : };
818 : :
819 : :
820 : : /* All relations */
821 : : static const SchemaQuery Query_for_list_of_relations = {
822 : : .catname = "pg_catalog.pg_class c",
823 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
824 : : .namespace = "c.relnamespace",
825 : : .result = "c.relname",
826 : : };
827 : :
828 : : /* partitioned relations */
829 : : static const SchemaQuery Query_for_list_of_partitioned_relations = {
830 : : .catname = "pg_catalog.pg_class c",
831 : : .selcondition = "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE)
832 : : ", " CppAsString2(RELKIND_PARTITIONED_INDEX) ")",
833 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
834 : : .namespace = "c.relnamespace",
835 : : .result = "c.relname",
836 : : };
837 : :
838 : : static const SchemaQuery Query_for_list_of_operator_families = {
839 : : .catname = "pg_catalog.pg_opfamily c",
840 : : .viscondition = "pg_catalog.pg_opfamily_is_visible(c.oid)",
841 : : .namespace = "c.opfnamespace",
842 : : .result = "c.opfname",
843 : : };
844 : :
845 : : /* Relations supporting INSERT, UPDATE or DELETE */
846 : : static const SchemaQuery Query_for_list_of_updatables = {
847 : : .catname = "pg_catalog.pg_class c",
848 : : .selcondition =
849 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
850 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
851 : : CppAsString2(RELKIND_VIEW) ", "
852 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
853 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
854 : : .namespace = "c.relnamespace",
855 : : .result = "c.relname",
856 : : };
857 : :
858 : : /* Relations supporting MERGE */
859 : : static const SchemaQuery Query_for_list_of_mergetargets = {
860 : : .catname = "pg_catalog.pg_class c",
861 : : .selcondition =
862 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
863 : : CppAsString2(RELKIND_VIEW) ", "
864 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ") ",
865 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
866 : : .namespace = "c.relnamespace",
867 : : .result = "c.relname",
868 : : };
869 : :
870 : : /* Relations supporting SELECT */
871 : : static const SchemaQuery Query_for_list_of_selectables = {
872 : : .catname = "pg_catalog.pg_class c",
873 : : .selcondition =
874 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
875 : : CppAsString2(RELKIND_SEQUENCE) ", "
876 : : CppAsString2(RELKIND_VIEW) ", "
877 : : CppAsString2(RELKIND_MATVIEW) ", "
878 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
879 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
880 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
881 : : .namespace = "c.relnamespace",
882 : : .result = "c.relname",
883 : : };
884 : :
885 : : /* Relations supporting TRUNCATE */
886 : : static const SchemaQuery Query_for_list_of_truncatables = {
887 : : .catname = "pg_catalog.pg_class c",
888 : : .selcondition =
889 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
890 : : CppAsString2(RELKIND_FOREIGN_TABLE) ", "
891 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
892 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
893 : : .namespace = "c.relnamespace",
894 : : .result = "c.relname",
895 : : };
896 : :
897 : : /* Relations supporting GRANT are currently same as those supporting SELECT */
898 : : #define Query_for_list_of_grantables Query_for_list_of_selectables
899 : :
900 : : /* Relations supporting ANALYZE */
901 : : static const SchemaQuery Query_for_list_of_analyzables = {
902 : : .catname = "pg_catalog.pg_class c",
903 : : .selcondition =
904 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
905 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
906 : : CppAsString2(RELKIND_MATVIEW) ", "
907 : : CppAsString2(RELKIND_FOREIGN_TABLE) ")",
908 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
909 : : .namespace = "c.relnamespace",
910 : : .result = "c.relname",
911 : : };
912 : :
913 : : /*
914 : : * Relations supporting COPY TO/FROM are currently almost the same as
915 : : * those supporting ANALYZE. Although views with INSTEAD OF INSERT triggers
916 : : * can be used with COPY FROM, they are rarely used for this purpose,
917 : : * so plain views are intentionally excluded from this tab completion.
918 : : */
919 : : #define Query_for_list_of_tables_for_copy Query_for_list_of_analyzables
920 : :
921 : : /* Relations supporting index creation */
922 : : static const SchemaQuery Query_for_list_of_indexables = {
923 : : .catname = "pg_catalog.pg_class c",
924 : : .selcondition =
925 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
926 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
927 : : CppAsString2(RELKIND_MATVIEW) ")",
928 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
929 : : .namespace = "c.relnamespace",
930 : : .result = "c.relname",
931 : : };
932 : :
933 : : /*
934 : : * Relations supporting VACUUM are currently same as those supporting
935 : : * indexing.
936 : : */
937 : : #define Query_for_list_of_vacuumables Query_for_list_of_indexables
938 : :
939 : : /* Relations supporting CLUSTER */
940 : : static const SchemaQuery Query_for_list_of_clusterables = {
941 : : .catname = "pg_catalog.pg_class c",
942 : : .selcondition =
943 : : "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
944 : : CppAsString2(RELKIND_PARTITIONED_TABLE) ", "
945 : : CppAsString2(RELKIND_MATVIEW) ")",
946 : : .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
947 : : .namespace = "c.relnamespace",
948 : : .result = "c.relname",
949 : : };
950 : :
951 : : static const SchemaQuery Query_for_list_of_constraints_with_schema = {
952 : : .catname = "pg_catalog.pg_constraint c",
953 : : .selcondition = "c.conrelid <> 0",
954 : : .namespace = "c.connamespace",
955 : : .result = "c.conname",
956 : : };
957 : :
958 : : static const SchemaQuery Query_for_list_of_statistics = {
959 : : .catname = "pg_catalog.pg_statistic_ext s",
960 : : .viscondition = "pg_catalog.pg_statistics_obj_is_visible(s.oid)",
961 : : .namespace = "s.stxnamespace",
962 : : .result = "s.stxname",
963 : : };
964 : :
965 : : static const SchemaQuery Query_for_list_of_collations = {
966 : : .catname = "pg_catalog.pg_collation c",
967 : : .selcondition = "c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))",
968 : : .viscondition = "pg_catalog.pg_collation_is_visible(c.oid)",
969 : : .namespace = "c.collnamespace",
970 : : .result = "c.collname",
971 : : };
972 : :
973 : : static const SchemaQuery Query_for_partition_of_table = {
974 : : .catname = "pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_inherits i",
975 : : .selcondition = "c1.oid=i.inhparent and i.inhrelid=c2.oid and c2.relispartition",
976 : : .viscondition = "pg_catalog.pg_table_is_visible(c2.oid)",
977 : : .namespace = "c2.relnamespace",
978 : : .result = "c2.relname",
979 : : .refname = "c1.relname",
980 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
981 : : .refnamespace = "c1.relnamespace",
982 : : };
983 : :
984 : : static const SchemaQuery Query_for_rule_of_table = {
985 : : .catname = "pg_catalog.pg_rewrite r, pg_catalog.pg_class c1",
986 : : .selcondition = "r.ev_class=c1.oid",
987 : : .result = "r.rulename",
988 : : .refname = "c1.relname",
989 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
990 : : .refnamespace = "c1.relnamespace",
991 : : };
992 : :
993 : : static const SchemaQuery Query_for_trigger_of_table = {
994 : : .catname = "pg_catalog.pg_trigger t, pg_catalog.pg_class c1",
995 : : .selcondition = "t.tgrelid=c1.oid and not t.tgisinternal",
996 : : .result = "t.tgname",
997 : : .refname = "c1.relname",
998 : : .refviscondition = "pg_catalog.pg_table_is_visible(c1.oid)",
999 : : .refnamespace = "c1.relnamespace",
1000 : : };
1001 : :
1002 : :
1003 : : /*
1004 : : * Queries to get lists of names of various kinds of things, possibly
1005 : : * restricted to names matching a partially entered name. Don't use
1006 : : * this method where the user might wish to enter a schema-qualified
1007 : : * name; make a SchemaQuery instead.
1008 : : *
1009 : : * In these queries, there must be a restriction clause of the form
1010 : : * output LIKE '%s'
1011 : : * where "output" is the same string that the query returns. The %s
1012 : : * will be replaced by a LIKE pattern to match the already-typed text.
1013 : : *
1014 : : * There can be a second '%s', which will be replaced by a suitably-escaped
1015 : : * version of the string provided in completion_ref_object. If there is a
1016 : : * third '%s', it will be replaced by a suitably-escaped version of the string
1017 : : * provided in completion_ref_schema. NOTE: using completion_ref_object
1018 : : * that way is usually the wrong thing, and using completion_ref_schema
1019 : : * that way is always the wrong thing. Make a SchemaQuery instead.
1020 : : */
1021 : :
1022 : : #define Query_for_list_of_template_databases \
1023 : : "SELECT d.datname "\
1024 : : " FROM pg_catalog.pg_database d "\
1025 : : " WHERE d.datname LIKE '%s' "\
1026 : : " AND (d.datistemplate OR pg_catalog.pg_has_role(d.datdba, 'USAGE'))"
1027 : :
1028 : : #define Query_for_list_of_databases \
1029 : : "SELECT datname FROM pg_catalog.pg_database "\
1030 : : " WHERE datname LIKE '%s'"
1031 : :
1032 : : #define Query_for_list_of_database_vars \
1033 : : "SELECT conf FROM ("\
1034 : : " SELECT setdatabase, pg_catalog.split_part(pg_catalog.unnest(setconfig),'=',1) conf"\
1035 : : " FROM pg_catalog.pg_db_role_setting "\
1036 : : " ) s, pg_catalog.pg_database d "\
1037 : : " WHERE s.setdatabase = d.oid "\
1038 : : " AND conf LIKE '%s'"\
1039 : : " AND d.datname LIKE '%s'"
1040 : :
1041 : : #define Query_for_list_of_tablespaces \
1042 : : "SELECT spcname FROM pg_catalog.pg_tablespace "\
1043 : : " WHERE spcname LIKE '%s'"
1044 : :
1045 : : #define Query_for_list_of_encodings \
1046 : : " SELECT DISTINCT pg_catalog.pg_encoding_to_char(conforencoding) "\
1047 : : " FROM pg_catalog.pg_conversion "\
1048 : : " WHERE pg_catalog.pg_encoding_to_char(conforencoding) LIKE pg_catalog.upper('%s')"
1049 : :
1050 : : #define Query_for_list_of_languages \
1051 : : "SELECT lanname "\
1052 : : " FROM pg_catalog.pg_language "\
1053 : : " WHERE lanname != 'internal' "\
1054 : : " AND lanname LIKE '%s'"
1055 : :
1056 : : #define Query_for_list_of_schemas \
1057 : : "SELECT nspname FROM pg_catalog.pg_namespace "\
1058 : : " WHERE nspname LIKE '%s'"
1059 : :
1060 : : /* Use COMPLETE_WITH_QUERY_VERBATIM with these queries for GUC names: */
1061 : : #define Query_for_list_of_alter_system_set_vars \
1062 : : "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1063 : : " WHERE context != 'internal' "\
1064 : : " AND pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1065 : :
1066 : : #define Query_for_list_of_set_vars \
1067 : : "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1068 : : " WHERE context IN ('user', 'superuser') "\
1069 : : " AND pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1070 : :
1071 : : #define Query_for_list_of_show_vars \
1072 : : "SELECT pg_catalog.lower(name) FROM pg_catalog.pg_settings "\
1073 : : " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1074 : :
1075 : : #define Query_for_list_of_roles \
1076 : : " SELECT rolname "\
1077 : : " FROM pg_catalog.pg_roles "\
1078 : : " WHERE rolname LIKE '%s'"
1079 : :
1080 : : /* add these to Query_for_list_of_roles in OWNER contexts */
1081 : : #define Keywords_for_list_of_owner_roles \
1082 : : "CURRENT_ROLE", "CURRENT_USER", "SESSION_USER"
1083 : :
1084 : : /* add these to Query_for_list_of_roles in GRANT contexts */
1085 : : #define Keywords_for_list_of_grant_roles \
1086 : : Keywords_for_list_of_owner_roles, "PUBLIC"
1087 : :
1088 : : #define Query_for_all_table_constraints \
1089 : : "SELECT conname "\
1090 : : " FROM pg_catalog.pg_constraint c "\
1091 : : " WHERE c.conrelid <> 0 "\
1092 : : " and conname LIKE '%s'"
1093 : :
1094 : : #define Query_for_list_of_fdws \
1095 : : " SELECT fdwname "\
1096 : : " FROM pg_catalog.pg_foreign_data_wrapper "\
1097 : : " WHERE fdwname LIKE '%s'"
1098 : :
1099 : : #define Query_for_list_of_servers \
1100 : : " SELECT srvname "\
1101 : : " FROM pg_catalog.pg_foreign_server "\
1102 : : " WHERE srvname LIKE '%s'"
1103 : :
1104 : : #define Query_for_list_of_user_mappings \
1105 : : " SELECT usename "\
1106 : : " FROM pg_catalog.pg_user_mappings "\
1107 : : " WHERE usename LIKE '%s'"
1108 : :
1109 : : #define Query_for_list_of_user_vars \
1110 : : "SELECT conf FROM ("\
1111 : : " SELECT rolname, pg_catalog.split_part(pg_catalog.unnest(rolconfig),'=',1) conf"\
1112 : : " FROM pg_catalog.pg_roles"\
1113 : : " ) s"\
1114 : : " WHERE s.conf like '%s' "\
1115 : : " AND s.rolname LIKE '%s'"
1116 : :
1117 : : #define Query_for_list_of_access_methods \
1118 : : " SELECT amname "\
1119 : : " FROM pg_catalog.pg_am "\
1120 : : " WHERE amname LIKE '%s'"
1121 : :
1122 : : #define Query_for_list_of_index_access_methods \
1123 : : " SELECT amname "\
1124 : : " FROM pg_catalog.pg_am "\
1125 : : " WHERE amname LIKE '%s' AND "\
1126 : : " amtype=" CppAsString2(AMTYPE_INDEX)
1127 : :
1128 : : #define Query_for_list_of_table_access_methods \
1129 : : " SELECT amname "\
1130 : : " FROM pg_catalog.pg_am "\
1131 : : " WHERE amname LIKE '%s' AND "\
1132 : : " amtype=" CppAsString2(AMTYPE_TABLE)
1133 : :
1134 : : #define Query_for_list_of_extensions \
1135 : : " SELECT extname "\
1136 : : " FROM pg_catalog.pg_extension "\
1137 : : " WHERE extname LIKE '%s'"
1138 : :
1139 : : #define Query_for_list_of_available_extensions \
1140 : : " SELECT name "\
1141 : : " FROM pg_catalog.pg_available_extensions "\
1142 : : " WHERE name LIKE '%s' AND installed_version IS NULL"
1143 : :
1144 : : #define Query_for_list_of_available_extension_versions \
1145 : : " SELECT version "\
1146 : : " FROM pg_catalog.pg_available_extension_versions "\
1147 : : " WHERE version LIKE '%s' AND name='%s'"
1148 : :
1149 : : #define Query_for_list_of_prepared_statements \
1150 : : " SELECT name "\
1151 : : " FROM pg_catalog.pg_prepared_statements "\
1152 : : " WHERE name LIKE '%s'"
1153 : :
1154 : : #define Query_for_list_of_event_triggers \
1155 : : " SELECT evtname "\
1156 : : " FROM pg_catalog.pg_event_trigger "\
1157 : : " WHERE evtname LIKE '%s'"
1158 : :
1159 : : #define Query_for_list_of_tablesample_methods \
1160 : : " SELECT proname "\
1161 : : " FROM pg_catalog.pg_proc "\
1162 : : " WHERE prorettype = 'pg_catalog.tsm_handler'::pg_catalog.regtype AND "\
1163 : : " proargtypes[0] = 'pg_catalog.internal'::pg_catalog.regtype AND "\
1164 : : " proname LIKE '%s'"
1165 : :
1166 : : #define Query_for_list_of_policies \
1167 : : " SELECT polname "\
1168 : : " FROM pg_catalog.pg_policy "\
1169 : : " WHERE polname LIKE '%s'"
1170 : :
1171 : : #define Query_for_values_of_enum_GUC \
1172 : : " SELECT val FROM ( "\
1173 : : " SELECT name, pg_catalog.unnest(enumvals) AS val "\
1174 : : " FROM pg_catalog.pg_settings "\
1175 : : " ) ss "\
1176 : : " WHERE val LIKE '%s'"\
1177 : : " and pg_catalog.lower(name)=pg_catalog.lower('%s')"
1178 : :
1179 : : #define Query_for_list_of_channels \
1180 : : " SELECT channel "\
1181 : : " FROM pg_catalog.pg_listening_channels() AS channel "\
1182 : : " WHERE channel LIKE '%s'"
1183 : :
1184 : : #define Query_for_list_of_cursors \
1185 : : " SELECT name "\
1186 : : " FROM pg_catalog.pg_cursors "\
1187 : : " WHERE name LIKE '%s'"
1188 : :
1189 : : #define Query_for_list_of_timezone_names_unquoted \
1190 : : " SELECT name "\
1191 : : " FROM pg_catalog.pg_timezone_names() "\
1192 : : " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1193 : :
1194 : : #define Query_for_list_of_timezone_names_quoted_out \
1195 : : "SELECT pg_catalog.quote_literal(name) AS name "\
1196 : : " FROM pg_catalog.pg_timezone_names() "\
1197 : : " WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1198 : :
1199 : : #define Query_for_list_of_timezone_names_quoted_in \
1200 : : "SELECT pg_catalog.quote_literal(name) AS name "\
1201 : : " FROM pg_catalog.pg_timezone_names() "\
1202 : : " WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.lower('%s')"
1203 : :
1204 : : #define Query_for_list_of_publications \
1205 : : "SELECT pubname "\
1206 : : " FROM pg_catalog.pg_publication "\
1207 : : " WHERE pubname LIKE '%s'"
1208 : :
1209 : : #define Query_for_list_of_subscriptions \
1210 : : "SELECT s.subname "\
1211 : : " FROM pg_catalog.pg_subscription s, pg_catalog.pg_database d"\
1212 : : " WHERE s.subname LIKE '%s' "\
1213 : : " AND d.datname = pg_catalog.current_database() "\
1214 : : " AND s.subdbid = d.oid"
1215 : :
1216 : : /* Privilege options shared between GRANT and REVOKE */
1217 : : #define Privilege_options_of_grant_and_revoke \
1218 : : "SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", \
1219 : : "CREATE", "CONNECT", "TEMPORARY", "EXECUTE", "USAGE", "SET", "ALTER SYSTEM", \
1220 : : "MAINTAIN", "ALL"
1221 : :
1222 : : /* ALTER PROCEDURE options */
1223 : : #define Alter_procedure_options \
1224 : : "DEPENDS ON EXTENSION", "EXTERNAL SECURITY", "NO DEPENDS ON EXTENSION", \
1225 : : "OWNER TO", "RENAME TO", "RESET", "SECURITY", "SET"
1226 : :
1227 : : /* ALTER ROUTINE options */
1228 : : #define Alter_routine_options \
1229 : : Alter_procedure_options, "COST", "IMMUTABLE", "LEAKPROOF", "NOT LEAKPROOF", \
1230 : : "PARALLEL", "ROWS", "STABLE", "VOLATILE"
1231 : :
1232 : : /* ALTER FUNCTION options */
1233 : : #define Alter_function_options \
1234 : : Alter_routine_options, "CALLED ON NULL INPUT", "RETURNS NULL ON NULL INPUT", \
1235 : : "STRICT", "SUPPORT"
1236 : :
1237 : : /* COPY options shared between FROM and TO */
1238 : : #define Copy_common_options \
1239 : : "DELIMITER", "ENCODING", "ESCAPE", "FORMAT", "HEADER", "NULL", "QUOTE"
1240 : :
1241 : : /* COPY FROM options */
1242 : : #define Copy_from_options \
1243 : : Copy_common_options, "DEFAULT", "FORCE_NOT_NULL", "FORCE_NULL", "FREEZE", \
1244 : : "LOG_VERBOSITY", "ON_ERROR", "REJECT_LIMIT"
1245 : :
1246 : : /* COPY TO options */
1247 : : #define Copy_to_options \
1248 : : Copy_common_options, "FORCE_QUOTE", "FORCE_ARRAY"
1249 : :
1250 : : /* Known command-starting keywords. */
1251 : : static const char *const sql_commands[] = {
1252 : : "ABORT", "ALTER", "ANALYZE", "BEGIN", "CALL", "CHECKPOINT", "CLOSE", "CLUSTER",
1253 : : "COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
1254 : : "DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN",
1255 : : "FETCH", "GRANT", "IMPORT FOREIGN SCHEMA", "INSERT INTO", "LISTEN", "LOAD", "LOCK",
1256 : : "MERGE INTO", "MOVE", "NOTIFY", "PREPARE",
1257 : : "REASSIGN", "REFRESH MATERIALIZED VIEW", "REINDEX", "RELEASE", "REPACK",
1258 : : "RESET", "REVOKE", "ROLLBACK",
1259 : : "SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START",
1260 : : "TABLE", "TRUNCATE", "UNLISTEN", "UPDATE", "VACUUM", "VALUES",
1261 : : "WAIT FOR", "WITH",
1262 : : NULL
1263 : : };
1264 : :
1265 : : /*
1266 : : * This is a list of all "things" in Pgsql, which can show up after CREATE or
1267 : : * DROP; and there is also a query to get a list of them.
1268 : : */
1269 : :
1270 : : typedef struct
1271 : : {
1272 : : const char *name;
1273 : : /* Provide at most one of these three types of query: */
1274 : : const char *query; /* simple query, or NULL */
1275 : : const VersionedQuery *vquery; /* versioned query, or NULL */
1276 : : const SchemaQuery *squery; /* schema query, or NULL */
1277 : : const char *const *keywords; /* keywords to be offered as well */
1278 : : const uint32 flags; /* visibility flags, see below */
1279 : : } pgsql_thing_t;
1280 : :
1281 : : #define THING_NO_CREATE (1 << 0) /* should not show up after CREATE */
1282 : : #define THING_NO_DROP (1 << 1) /* should not show up after DROP */
1283 : : #define THING_NO_ALTER (1 << 2) /* should not show up after ALTER */
1284 : : #define THING_NO_SHOW (THING_NO_CREATE | THING_NO_DROP | THING_NO_ALTER)
1285 : :
1286 : : /* When we have DROP USER etc, also offer MAPPING FOR */
1287 : : static const char *const Keywords_for_user_thing[] = {
1288 : : "MAPPING FOR",
1289 : : NULL
1290 : : };
1291 : :
1292 : : static const pgsql_thing_t words_after_create[] = {
1293 : : {"ACCESS METHOD", NULL, NULL, NULL, NULL, THING_NO_ALTER},
1294 : : {"AGGREGATE", NULL, NULL, Query_for_list_of_aggregates},
1295 : : {"CAST", NULL, NULL, NULL}, /* Casts have complex structures for names, so
1296 : : * skip it */
1297 : : {"COLLATION", NULL, NULL, &Query_for_list_of_collations},
1298 : :
1299 : : /*
1300 : : * CREATE CONSTRAINT TRIGGER is not supported here because it is designed
1301 : : * to be used only by pg_dump.
1302 : : */
1303 : : {"CONFIGURATION", NULL, NULL, &Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW},
1304 : : {"CONVERSION", "SELECT conname FROM pg_catalog.pg_conversion WHERE conname LIKE '%s'"},
1305 : : {"DATABASE", Query_for_list_of_databases},
1306 : : {"DEFAULT PRIVILEGES", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1307 : : {"DICTIONARY", NULL, NULL, &Query_for_list_of_ts_dictionaries, NULL, THING_NO_SHOW},
1308 : : {"DOMAIN", NULL, NULL, &Query_for_list_of_domains},
1309 : : {"EVENT TRIGGER", NULL, NULL, NULL},
1310 : : {"EXTENSION", Query_for_list_of_extensions},
1311 : : {"FOREIGN DATA WRAPPER", NULL, NULL, NULL},
1312 : : {"FOREIGN TABLE", NULL, NULL, NULL},
1313 : : {"FUNCTION", NULL, NULL, Query_for_list_of_functions},
1314 : : {"GROUP", Query_for_list_of_roles},
1315 : : {"INDEX", NULL, NULL, &Query_for_list_of_indexes},
1316 : : {"LANGUAGE", Query_for_list_of_languages},
1317 : : {"LARGE OBJECT", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1318 : : {"MATERIALIZED VIEW", NULL, NULL, &Query_for_list_of_matviews},
1319 : : {"OPERATOR", NULL, NULL, NULL}, /* Querying for this is probably not such
1320 : : * a good idea. */
1321 : : {"OR REPLACE", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER},
1322 : : {"OWNED", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_ALTER}, /* for DROP OWNED BY ... */
1323 : : {"PARSER", NULL, NULL, &Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW},
1324 : : {"POLICY", NULL, NULL, NULL},
1325 : : {"PROCEDURE", NULL, NULL, Query_for_list_of_procedures},
1326 : : {"PUBLICATION", Query_for_list_of_publications},
1327 : : {"ROLE", Query_for_list_of_roles},
1328 : : {"ROUTINE", NULL, NULL, &Query_for_list_of_routines, NULL, THING_NO_CREATE},
1329 : : {"RULE", "SELECT rulename FROM pg_catalog.pg_rules WHERE rulename LIKE '%s'"},
1330 : : {"SCHEMA", Query_for_list_of_schemas},
1331 : : {"SEQUENCE", NULL, NULL, &Query_for_list_of_sequences},
1332 : : {"SERVER", Query_for_list_of_servers},
1333 : : {"STATISTICS", NULL, NULL, &Query_for_list_of_statistics},
1334 : : {"SUBSCRIPTION", Query_for_list_of_subscriptions},
1335 : : {"SYSTEM", NULL, NULL, NULL, NULL, THING_NO_CREATE | THING_NO_DROP},
1336 : : {"TABLE", NULL, NULL, &Query_for_list_of_tables},
1337 : : {"TABLESPACE", Query_for_list_of_tablespaces},
1338 : : {"TEMP", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMP TABLE
1339 : : * ... */
1340 : : {"TEMPLATE", NULL, NULL, &Query_for_list_of_ts_templates, NULL, THING_NO_SHOW},
1341 : : {"TEMPORARY", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE TEMPORARY
1342 : : * TABLE ... */
1343 : : {"TEXT SEARCH", NULL, NULL, NULL},
1344 : : {"TRANSFORM", NULL, NULL, NULL, NULL, THING_NO_ALTER},
1345 : : {"TRIGGER", "SELECT tgname FROM pg_catalog.pg_trigger WHERE tgname LIKE '%s' AND NOT tgisinternal"},
1346 : : {"TYPE", NULL, NULL, &Query_for_list_of_datatypes},
1347 : : {"UNIQUE", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNIQUE
1348 : : * INDEX ... */
1349 : : {"UNLOGGED", NULL, NULL, NULL, NULL, THING_NO_DROP | THING_NO_ALTER}, /* for CREATE UNLOGGED
1350 : : * TABLE ... */
1351 : : {"USER", Query_for_list_of_roles, NULL, NULL, Keywords_for_user_thing},
1352 : : {"USER MAPPING FOR", NULL, NULL, NULL},
1353 : : {"VIEW", NULL, NULL, &Query_for_list_of_views},
1354 : : {NULL} /* end of list */
1355 : : };
1356 : :
1357 : : /*
1358 : : * The tcpatterns[] table provides the initial pattern-match rule for each
1359 : : * switch case in match_previous_words(). The contents of the table
1360 : : * are constructed by gen_tabcomplete.pl.
1361 : : */
1362 : :
1363 : : /* Basic match rules appearing in tcpatterns[].kind */
1364 : : enum TCPatternKind
1365 : : {
1366 : : Match,
1367 : : MatchCS,
1368 : : HeadMatch,
1369 : : HeadMatchCS,
1370 : : TailMatch,
1371 : : TailMatchCS,
1372 : : };
1373 : :
1374 : : /* Things besides string literals that can appear in tcpatterns[].words */
1375 : : #define MatchAny NULL
1376 : : #define MatchAnyExcept(pattern) ("!" pattern)
1377 : : #define MatchAnyN ""
1378 : :
1379 : : /* One entry in tcpatterns[] */
1380 : : typedef struct
1381 : : {
1382 : : int id; /* case label used in match_previous_words */
1383 : : enum TCPatternKind kind; /* match kind, see above */
1384 : : int nwords; /* length of words[] array */
1385 : : const char *const *words; /* array of match words */
1386 : : } TCPattern;
1387 : :
1388 : : /* Macro emitted by gen_tabcomplete.pl to fill a tcpatterns[] entry */
1389 : : #define TCPAT(id, kind, ...) \
1390 : : { (id), (kind), VA_ARGS_NARGS(__VA_ARGS__), \
1391 : : (const char * const []) { __VA_ARGS__ } }
1392 : :
1393 : : #ifdef SWITCH_CONVERSION_APPLIED
1394 : :
1395 : : static const TCPattern tcpatterns[] =
1396 : : {
1397 : : /* Insert tab-completion pattern data here. */
1398 : : };
1399 : :
1400 : : #endif /* SWITCH_CONVERSION_APPLIED */
1401 : :
1402 : : /* Storage parameters for CREATE TABLE and ALTER TABLE */
1403 : : static const char *const table_storage_parameters[] = {
1404 : : "autovacuum_analyze_scale_factor",
1405 : : "autovacuum_analyze_threshold",
1406 : : "autovacuum_enabled",
1407 : : "autovacuum_freeze_max_age",
1408 : : "autovacuum_freeze_min_age",
1409 : : "autovacuum_freeze_table_age",
1410 : : "autovacuum_multixact_freeze_max_age",
1411 : : "autovacuum_multixact_freeze_min_age",
1412 : : "autovacuum_multixact_freeze_table_age",
1413 : : "autovacuum_parallel_workers",
1414 : : "autovacuum_vacuum_cost_delay",
1415 : : "autovacuum_vacuum_cost_limit",
1416 : : "autovacuum_vacuum_insert_scale_factor",
1417 : : "autovacuum_vacuum_insert_threshold",
1418 : : "autovacuum_vacuum_max_threshold",
1419 : : "autovacuum_vacuum_scale_factor",
1420 : : "autovacuum_vacuum_threshold",
1421 : : "fillfactor",
1422 : : "log_autovacuum_min_duration",
1423 : : "log_autoanalyze_min_duration",
1424 : : "parallel_workers",
1425 : : "toast.autovacuum_enabled",
1426 : : "toast.autovacuum_freeze_max_age",
1427 : : "toast.autovacuum_freeze_min_age",
1428 : : "toast.autovacuum_freeze_table_age",
1429 : : "toast.autovacuum_multixact_freeze_max_age",
1430 : : "toast.autovacuum_multixact_freeze_min_age",
1431 : : "toast.autovacuum_multixact_freeze_table_age",
1432 : : "toast.autovacuum_vacuum_cost_delay",
1433 : : "toast.autovacuum_vacuum_cost_limit",
1434 : : "toast.autovacuum_vacuum_insert_scale_factor",
1435 : : "toast.autovacuum_vacuum_insert_threshold",
1436 : : "toast.autovacuum_vacuum_max_threshold",
1437 : : "toast.autovacuum_vacuum_scale_factor",
1438 : : "toast.autovacuum_vacuum_threshold",
1439 : : "toast.log_autovacuum_min_duration",
1440 : : "toast.vacuum_index_cleanup",
1441 : : "toast.vacuum_max_eager_freeze_failure_rate",
1442 : : "toast.vacuum_truncate",
1443 : : "toast_tuple_target",
1444 : : "toast_value_type",
1445 : : "user_catalog_table",
1446 : : "vacuum_index_cleanup",
1447 : : "vacuum_max_eager_freeze_failure_rate",
1448 : : "vacuum_truncate",
1449 : : NULL
1450 : : };
1451 : :
1452 : : /* Optional parameters for CREATE VIEW and ALTER VIEW */
1453 : : static const char *const view_optional_parameters[] = {
1454 : : "check_option",
1455 : : "security_barrier",
1456 : : "security_invoker",
1457 : : NULL
1458 : : };
1459 : :
1460 : : /* Forward declaration of functions */
1461 : : static char **psql_completion(const char *text, int start, int end);
1462 : : static char **match_previous_words(int pattern_id,
1463 : : const char *text, int start, int end,
1464 : : char **previous_words,
1465 : : int previous_words_count);
1466 : : static char *create_command_generator(const char *text, int state);
1467 : : static char *drop_command_generator(const char *text, int state);
1468 : : static char *alter_command_generator(const char *text, int state);
1469 : : static char *complete_from_query(const char *text, int state);
1470 : : static char *complete_from_versioned_query(const char *text, int state);
1471 : : static char *complete_from_schema_query(const char *text, int state);
1472 : : static char *complete_from_versioned_schema_query(const char *text, int state);
1473 : : static char *_complete_from_query(const char *simple_query,
1474 : : const SchemaQuery *schema_query,
1475 : : const char *const *keywords,
1476 : : bool verbatim,
1477 : : const char *text, int state);
1478 : : static void set_completion_reference(const char *word);
1479 : : static void set_completion_reference_verbatim(const char *word);
1480 : : static char *complete_from_list(const char *text, int state);
1481 : : static char *complete_from_const(const char *text, int state);
1482 : : static void append_variable_names(char ***varnames, int *nvars,
1483 : : int *maxvars, const char *varname,
1484 : : const char *prefix, const char *suffix);
1485 : : static char **complete_from_variables(const char *text,
1486 : : const char *prefix, const char *suffix, bool need_value);
1487 : : static char *complete_from_files(const char *text, int state);
1488 : : static char *_complete_from_files(const char *text, int state);
1489 : :
1490 : : static char *pg_strdup_keyword_case(const char *s, const char *ref);
1491 : : static char *escape_string(const char *text);
1492 : : static char *make_like_pattern(const char *word);
1493 : : static void parse_identifier(const char *ident,
1494 : : char **schemaname, char **objectname,
1495 : : bool *schemaquoted, bool *objectquoted);
1496 : : static char *requote_identifier(const char *schemaname, const char *objectname,
1497 : : bool quote_schema, bool quote_object);
1498 : : static bool identifier_needs_quotes(const char *ident);
1499 : : static PGresult *exec_query(const char *query);
1500 : :
1501 : : static char **get_previous_words(int point, char **buffer, int *nwords);
1502 : :
1503 : : static char *get_guctype(const char *varname);
1504 : :
1505 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
1506 : : static char *quote_file_name(char *fname, int match_type, char *quote_pointer);
1507 : : static char *dequote_file_name(char *fname, int quote_char);
1508 : : #endif
1509 : :
1510 : :
1511 : : /*
1512 : : * Initialize the readline library for our purposes.
1513 : : */
1514 : : void
8361 tgl@sss.pgh.pa.us 1515 :CBC 3 : initialize_readline(void)
1516 : : {
26 peter@eisentraut.org 1517 : 3 : rl_readline_name = (char *) pset.progname;
4552 rhaas@postgresql.org 1518 : 3 : rl_attempted_completion_function = psql_completion;
1519 : :
1520 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
2432 tgl@sss.pgh.pa.us 1521 : 3 : rl_filename_quoting_function = quote_file_name;
1522 : 3 : rl_filename_dequoting_function = dequote_file_name;
1523 : : #endif
1524 : :
6059 itagaki.takahiro@gma 1525 : 3 : rl_basic_word_break_characters = WORD_BREAKS;
1526 : :
1527 : : /*
1528 : : * Ideally we'd include '"' in rl_completer_quote_characters too, which
1529 : : * should allow us to complete quoted identifiers that include spaces.
1530 : : * However, the library support for rl_completer_quote_characters is
1531 : : * presently too inconsistent to want to mess with that. (Note in
1532 : : * particular that libedit has this variable but completely ignores it.)
1533 : : */
2432 tgl@sss.pgh.pa.us 1534 : 3 : rl_completer_quote_characters = "'";
1535 : :
1536 : : /*
1537 : : * Set rl_filename_quote_characters to "all possible characters",
1538 : : * otherwise Readline will skip filename quoting if it thinks a filename
1539 : : * doesn't need quoting. Readline actually interprets this as bytes, so
1540 : : * there are no encoding considerations here.
1541 : : */
1542 : : #ifdef HAVE_RL_FILENAME_QUOTE_CHARACTERS
1543 : : {
1544 : 3 : unsigned char *fqc = (unsigned char *) pg_malloc(256);
1545 : :
1546 [ + + ]: 768 : for (int i = 0; i < 255; i++)
1547 : 765 : fqc[i] = (unsigned char) (i + 1);
1548 : 3 : fqc[255] = '\0';
1549 : 3 : rl_filename_quote_characters = (const char *) fqc;
1550 : : }
1551 : : #endif
1552 : :
8361 1553 : 3 : completion_max_records = 1000;
1554 : :
1555 : : /*
1556 : : * There is a variable rl_completion_query_items for this but apparently
1557 : : * it's not defined everywhere.
1558 : : */
1559 : 3 : }
1560 : :
1561 : : /*
1562 : : * Check if 'word' matches any of the '|'-separated strings in 'pattern',
1563 : : * using case-insensitive or case-sensitive comparisons.
1564 : : *
1565 : : * If pattern is NULL, it's a wild card that matches any word.
1566 : : * If pattern begins with '!', the result is negated, ie we check that 'word'
1567 : : * does *not* match any alternative appearing in the rest of 'pattern'.
1568 : : * Any alternative can contain '*' which is a wild card, i.e., it can match
1569 : : * any substring; however, we allow at most one '*' per alternative.
1570 : : *
1571 : : * For readability, callers should use the macros MatchAny and MatchAnyExcept
1572 : : * to invoke those two special cases for 'pattern'. (But '|' and '*' must
1573 : : * just be written directly in patterns.) There is also MatchAnyN, but that
1574 : : * is supported only in Matches/MatchesCS and is not handled here.
1575 : : */
1576 : : static bool
2921 1577 : 8006 : word_matches(const char *pattern,
1578 : : const char *word,
1579 : : bool case_sensitive)
1580 : : {
1581 : : size_t wordlen;
1582 : :
1583 : : #define cimatch(s1, s2, n) \
1584 : : (case_sensitive ? strncmp(s1, s2, n) == 0 : pg_strncasecmp(s1, s2, n) == 0)
1585 : :
1586 : : /* NULL pattern matches anything. */
3928 1587 [ + + ]: 8006 : if (pattern == NULL)
1588 : 179 : return true;
1589 : :
1590 : : /* Handle negated patterns from the MatchAnyExcept macro. */
1591 [ + + ]: 7827 : if (*pattern == '!')
2921 1592 : 2 : return !word_matches(pattern + 1, word, case_sensitive);
1593 : :
1594 : : /* Else consider each alternative in the pattern. */
3928 1595 : 7825 : wordlen = strlen(word);
1596 : : for (;;)
1597 : 805 : {
2921 1598 : 8630 : const char *star = NULL;
1599 : : const char *c;
1600 : :
1601 : : /* Find end of current alternative, and locate any wild card. */
3928 1602 : 8630 : c = pattern;
1603 [ + + + + ]: 57665 : while (*c != '\0' && *c != '|')
1604 : : {
2921 1605 [ + + ]: 49035 : if (*c == '*')
1606 : 409 : star = c;
3928 1607 : 49035 : c++;
1608 : : }
1609 : : /* Was there a wild card? */
2921 1610 [ + + ]: 8630 : if (star)
1611 : : {
1612 : : /* Yes, wildcard match? */
1613 : 409 : size_t beforelen = star - pattern,
1614 : 409 : afterlen = c - star - 1;
1615 : :
1616 [ + + + + : 809 : if (wordlen >= (beforelen + afterlen) &&
+ + ]
1617 [ + + + - ]: 414 : cimatch(word, pattern, beforelen) &&
1618 : 7 : cimatch(word + wordlen - afterlen, star + 1, afterlen))
3911 1619 : 7 : return true;
1620 : : }
1621 : : else
1622 : : {
1623 : : /* No, plain match? */
2921 1624 [ + + + + : 10462 : if (wordlen == (c - pattern) &&
+ + ]
1625 : 2241 : cimatch(word, pattern, wordlen))
3911 1626 : 1255 : return true;
1627 : : }
1628 : : /* Out of alternatives? */
3928 1629 [ + + ]: 7368 : if (*c == '\0')
1630 : 6563 : break;
1631 : : /* Nope, try next alternative. */
1632 : 805 : pattern = c + 1;
1633 : : }
1634 : :
1635 : 6563 : return false;
1636 : : }
1637 : :
1638 : : /*
1639 : : * Implementation of TailMatches and TailMatchesCS tests: do the last N words
1640 : : * in previous_words match the pattern arguments?
1641 : : *
1642 : : * The array indexing might look backwards, but remember that
1643 : : * previous_words[0] contains the *last* word on the line, not the first.
1644 : : */
1645 : : static bool
713 1646 : 7393 : TailMatchesArray(bool case_sensitive,
1647 : : int previous_words_count, char **previous_words,
1648 : : int narg, const char *const *args)
1649 : : {
2921 1650 [ + + ]: 7393 : if (previous_words_count < narg)
1651 : 5267 : return false;
1652 : :
1653 [ + + ]: 2231 : for (int argno = 0; argno < narg; argno++)
1654 : : {
713 1655 : 2194 : const char *arg = args[argno];
1656 : :
2921 1657 [ + + ]: 2194 : if (!word_matches(arg, previous_words[narg - argno - 1],
1658 : : case_sensitive))
1659 : 2089 : return false;
1660 : : }
1661 : :
1662 : 37 : return true;
1663 : : }
1664 : :
1665 : : /*
1666 : : * As above, but the pattern is passed as a variadic argument list.
1667 : : */
1668 : : static bool
713 1669 : 36 : TailMatchesImpl(bool case_sensitive,
1670 : : int previous_words_count, char **previous_words,
1671 : : int narg, ...)
1672 : : {
1673 : : const char *argarray[64];
1674 : : va_list args;
1675 : :
1676 [ - + ]: 36 : Assert(narg <= lengthof(argarray));
1677 : :
1678 [ + + ]: 36 : if (previous_words_count < narg)
2921 1679 : 10 : return false;
1680 : :
1681 : 26 : va_start(args, narg);
713 1682 [ + + ]: 64 : for (int argno = 0; argno < narg; argno++)
1683 : 38 : argarray[argno] = va_arg(args, const char *);
1684 : 26 : va_end(args);
1685 : :
1686 : 26 : return TailMatchesArray(case_sensitive,
1687 : : previous_words_count, previous_words,
1688 : : narg, argarray);
1689 : : }
1690 : :
1691 : : /*
1692 : : * Implementation of HeadMatches and HeadMatchesCS tests: do the first N
1693 : : * words in previous_words match the pattern arguments?
1694 : : */
1695 : : static bool
1696 : 4957 : HeadMatchesArray(bool case_sensitive,
1697 : : int previous_words_count, char **previous_words,
1698 : : int narg, const char *const *args)
1699 : : {
1700 [ + + ]: 4957 : if (previous_words_count < narg)
1701 : 450 : return false;
1702 : :
2921 1703 [ + + ]: 5845 : for (int argno = 0; argno < narg; argno++)
1704 : : {
713 1705 : 5810 : const char *arg = args[argno];
1706 : :
1707 [ + + ]: 5810 : if (!word_matches(arg, previous_words[previous_words_count - argno - 1],
1708 : : case_sensitive))
2921 1709 : 4472 : return false;
1710 : : }
1711 : :
1712 : 35 : return true;
1713 : : }
1714 : :
1715 : : /*
1716 : : * As above, but the pattern is passed as a variadic argument list.
1717 : : */
1718 : : static bool
1719 : 10 : HeadMatchesImpl(bool case_sensitive,
1720 : : int previous_words_count, char **previous_words,
1721 : : int narg, ...)
1722 : : {
1723 : : const char *argarray[64];
1724 : : va_list args;
1725 : :
713 1726 [ - + ]: 10 : Assert(narg <= lengthof(argarray));
1727 : :
2921 1728 [ + + ]: 10 : if (previous_words_count < narg)
1729 : 2 : return false;
1730 : :
1731 : 8 : va_start(args, narg);
713 1732 [ + + ]: 25 : for (int argno = 0; argno < narg; argno++)
1733 : 17 : argarray[argno] = va_arg(args, const char *);
1734 : 8 : va_end(args);
1735 : :
1736 : 8 : return HeadMatchesArray(case_sensitive,
1737 : : previous_words_count, previous_words,
1738 : : narg, argarray);
1739 : : }
1740 : :
1741 : : /*
1742 : : * Implementation of Matches and MatchesCS tests: do all of the words
1743 : : * in previous_words match the pattern arguments?
1744 : : *
1745 : : * This supports an additional kind of wildcard: MatchAnyN (represented as "")
1746 : : * can match any number of words, including zero, in the middle of the list.
1747 : : */
1748 : : static bool
1749 : 27581 : MatchesArray(bool case_sensitive,
1750 : : int previous_words_count, char **previous_words,
1751 : : int narg, const char *const *args)
1752 : : {
1753 : 27581 : int match_any_pos = -1;
1754 : :
1755 : : /* Even with MatchAnyN, there must be at least N-1 words */
1756 [ + + ]: 27581 : if (previous_words_count < narg - 1)
1757 : 15455 : return false;
1758 : :
1759 : : /* Check for MatchAnyN */
2921 1760 [ + + ]: 48789 : for (int argno = 0; argno < narg; argno++)
1761 : : {
713 1762 : 37302 : const char *arg = args[argno];
1763 : :
1764 [ + + + + ]: 37302 : if (arg != NULL && arg[0] == '\0')
1765 : : {
1766 : 639 : match_any_pos = argno;
1767 : 639 : break;
1768 : : }
1769 : : }
1770 : :
1771 [ + + ]: 12126 : if (match_any_pos < 0)
1772 : : {
1773 : : /* Standard case without MatchAnyN */
1774 [ + + ]: 11487 : if (previous_words_count != narg)
1775 : 7948 : return false;
1776 : :
1777 : : /* Either Head or Tail match will do for the rest */
1778 [ + + ]: 3539 : if (!HeadMatchesArray(case_sensitive,
1779 : : previous_words_count, previous_words,
1780 : : narg, args))
1781 : 3512 : return false;
1782 : : }
1783 : : else
1784 : : {
1785 : : /* Match against head */
1786 [ + - ]: 639 : if (!HeadMatchesArray(case_sensitive,
1787 : : previous_words_count, previous_words,
1788 : : match_any_pos, args))
1789 : 639 : return false;
1790 : :
1791 : : /* Match against tail */
713 tgl@sss.pgh.pa.us 1792 [ # # ]:UBC 0 : if (!TailMatchesArray(case_sensitive,
1793 : : previous_words_count, previous_words,
1794 : 0 : narg - match_any_pos - 1,
1795 : 0 : args + match_any_pos + 1))
1796 : 0 : return false;
1797 : : }
1798 : :
2921 tgl@sss.pgh.pa.us 1799 :CBC 27 : return true;
1800 : : }
1801 : :
1802 : : /*
1803 : : * As above, but the pattern is passed as a variadic argument list.
1804 : : */
1805 : : static bool
713 1806 : 14 : MatchesImpl(bool case_sensitive,
1807 : : int previous_words_count, char **previous_words,
1808 : : int narg, ...)
1809 : : {
1810 : : const char *argarray[64];
1811 : : va_list args;
1812 : :
1813 [ - + ]: 14 : Assert(narg <= lengthof(argarray));
1814 : :
1815 : : /* Even with MatchAnyN, there must be at least N-1 words */
1816 [ - + ]: 14 : if (previous_words_count < narg - 1)
713 tgl@sss.pgh.pa.us 1817 :UBC 0 : return false;
1818 : :
713 tgl@sss.pgh.pa.us 1819 :CBC 14 : va_start(args, narg);
1820 [ + + ]: 56 : for (int argno = 0; argno < narg; argno++)
1821 : 42 : argarray[argno] = va_arg(args, const char *);
1822 : 14 : va_end(args);
1823 : :
1824 : 14 : return MatchesArray(case_sensitive,
1825 : : previous_words_count, previous_words,
1826 : : narg, argarray);
1827 : : }
1828 : :
1829 : : /*
1830 : : * Check if the final character of 's' is 'c'.
1831 : : */
1832 : : static bool
3928 1833 : 3 : ends_with(const char *s, char c)
1834 : : {
1835 : 3 : size_t length = strlen(s);
1836 : :
1837 [ + - + - ]: 3 : return (length > 0 && s[length - 1] == c);
1838 : : }
1839 : :
1840 : : /*
1841 : : * The completion function.
1842 : : *
1843 : : * According to readline spec this gets passed the text entered so far and its
1844 : : * start and end positions in the readline buffer. The return value is some
1845 : : * partially obscure list format that can be generated by readline's
1846 : : * rl_completion_matches() function, so we don't have to worry about it.
1847 : : */
1848 : : static char **
4552 rhaas@postgresql.org 1849 : 67 : psql_completion(const char *text, int start, int end)
1850 : : {
1851 : : /* This is the variable we'll return. */
9657 bruce@momjian.us 1852 : 67 : char **matches = NULL;
1853 : :
1854 : : /* Workspace for parsed words. */
1855 : : char *words_buffer;
1856 : :
1857 : : /* This array will contain pointers to parsed words. */
1858 : : char **previous_words;
1859 : :
1860 : : /* The number of words found on the input line. */
1861 : : int previous_words_count;
1862 : :
1863 : : /*
1864 : : * For compactness, we use these macros to reference previous_words[].
1865 : : * Caution: do not access a previous_words[] entry without having checked
1866 : : * previous_words_count to be sure it's valid. In most cases below, that
1867 : : * check is implicit in a TailMatches() or similar macro, but in some
1868 : : * places we have to check it explicitly.
1869 : : */
1870 : : #define prev_wd (previous_words[0])
1871 : : #define prev2_wd (previous_words[1])
1872 : : #define prev3_wd (previous_words[2])
1873 : : #define prev4_wd (previous_words[3])
1874 : : #define prev5_wd (previous_words[4])
1875 : : #define prev6_wd (previous_words[5])
1876 : : #define prev7_wd (previous_words[6])
1877 : : #define prev8_wd (previous_words[7])
1878 : : #define prev9_wd (previous_words[8])
1879 : :
1880 : : /* Match the last N words before point, case-insensitively. */
1881 : : #define TailMatches(...) \
1882 : : TailMatchesImpl(false, previous_words_count, previous_words, \
1883 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1884 : :
1885 : : /* Match the last N words before point, case-sensitively. */
1886 : : #define TailMatchesCS(...) \
1887 : : TailMatchesImpl(true, previous_words_count, previous_words, \
1888 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1889 : :
1890 : : /* Match N words representing all of the line, case-insensitively. */
1891 : : #define Matches(...) \
1892 : : MatchesImpl(false, previous_words_count, previous_words, \
1893 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1894 : :
1895 : : /* Match N words representing all of the line, case-sensitively. */
1896 : : #define MatchesCS(...) \
1897 : : MatchesImpl(true, previous_words_count, previous_words, \
1898 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1899 : :
1900 : : /* Match the first N words on the line, case-insensitively. */
1901 : : #define HeadMatches(...) \
1902 : : HeadMatchesImpl(false, previous_words_count, previous_words, \
1903 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1904 : :
1905 : : /* Match the first N words on the line, case-sensitively. */
1906 : : #define HeadMatchesCS(...) \
1907 : : HeadMatchesImpl(true, previous_words_count, previous_words, \
1908 : : VA_ARGS_NARGS(__VA_ARGS__), __VA_ARGS__)
1909 : :
1910 : : /* psql's backslash commands. */
1911 : : static const char *const backslash_commands[] = {
1912 : : "\\a",
1913 : : "\\bind", "\\bind_named",
1914 : : "\\connect", "\\conninfo", "\\C", "\\cd", "\\close_prepared", "\\copy",
1915 : : "\\copyright", "\\crosstabview",
1916 : : "\\d", "\\da", "\\dA", "\\dAc", "\\dAf", "\\dAo", "\\dAp",
1917 : : "\\db", "\\dc", "\\dconfig", "\\dC", "\\dd", "\\ddp", "\\dD",
1918 : : "\\des", "\\det", "\\deu", "\\dew", "\\dE", "\\df",
1919 : : "\\dF", "\\dFd", "\\dFp", "\\dFt", "\\dg", "\\di", "\\dl", "\\dL",
1920 : : "\\dm", "\\dn", "\\do", "\\dO", "\\dp", "\\dP", "\\dPi", "\\dPt",
1921 : : "\\drds", "\\drg", "\\dRs", "\\dRp", "\\ds",
1922 : : "\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dX", "\\dy",
1923 : : "\\echo", "\\edit", "\\ef", "\\elif", "\\else", "\\encoding",
1924 : : "\\endif", "\\endpipeline", "\\errverbose", "\\ev",
1925 : : "\\f", "\\flush", "\\flushrequest",
1926 : : "\\g", "\\gdesc", "\\getenv", "\\getresults", "\\gexec", "\\gset", "\\gx",
1927 : : "\\help", "\\html",
1928 : : "\\if", "\\include", "\\include_relative", "\\ir",
1929 : : "\\list", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
1930 : : "\\out",
1931 : : "\\parse", "\\password", "\\print", "\\prompt", "\\pset",
1932 : : "\\qecho", "\\quit",
1933 : : "\\reset", "\\restrict",
1934 : : "\\s", "\\sendpipeline", "\\set", "\\setenv", "\\sf",
1935 : : "\\startpipeline", "\\sv", "\\syncpipeline",
1936 : : "\\t", "\\T", "\\timing",
1937 : : "\\unrestrict", "\\unset",
1938 : : "\\x",
1939 : : "\\warn", "\\watch", "\\write",
1940 : : "\\z",
1941 : : "\\!", "\\?",
1942 : : NULL
1943 : : };
1944 : :
1945 : : /*
1946 : : * Temporary workaround for a bug in recent (2019) libedit: it incorrectly
1947 : : * de-escapes the input "text", causing us to fail to recognize backslash
1948 : : * commands. So get the string to look at from rl_line_buffer instead.
1949 : : */
2452 tgl@sss.pgh.pa.us 1950 : 67 : char *text_copy = pnstrdup(rl_line_buffer + start, end - start);
1951 : 67 : text = text_copy;
1952 : :
1953 : : /* Remember last char of the given input word. */
2432 1954 [ + + ]: 67 : completion_last_char = (end > start) ? text[end - start - 1] : '\0';
1955 : :
1956 : : /* We usually want the append character to be a space. */
9657 bruce@momjian.us 1957 : 67 : rl_completion_append_character = ' ';
1958 : :
1959 : : /* Clear a few things. */
1960 : 67 : completion_charp = NULL;
1961 : 67 : completion_charpp = NULL;
1694 tgl@sss.pgh.pa.us 1962 : 67 : completion_vquery = NULL;
1963 : 67 : completion_squery = NULL;
1964 : 67 : completion_ref_object = NULL;
1965 : 67 : completion_ref_schema = NULL;
1966 : :
1967 : : /*
1968 : : * Scan the input line to extract the words before our current position.
1969 : : * According to those we'll make some smart decisions on what the user is
1970 : : * probably intending to type.
1971 : : */
3927 1972 : 67 : previous_words = get_previous_words(start,
1973 : : &words_buffer,
1974 : : &previous_words_count);
1975 : :
1976 : : /* If current word is a backslash command, offer completions for that */
9657 bruce@momjian.us 1977 [ + + ]: 67 : if (text[0] == '\\')
5345 peter_e@gmx.net 1978 : 1 : COMPLETE_WITH_LIST_CS(backslash_commands);
1979 : :
1980 : : /* If current word is a variable interpolation, handle that case */
5700 rhaas@postgresql.org 1981 [ + + + - ]: 66 : else if (text[0] == ':' && text[1] != ':')
1982 : : {
1983 [ - + ]: 2 : if (text[1] == '\'')
4422 fujii@postgresql.org 1984 :UBC 0 : matches = complete_from_variables(text, ":'", "'", true);
5700 rhaas@postgresql.org 1985 [ - + ]:CBC 2 : else if (text[1] == '"')
4422 fujii@postgresql.org 1986 :UBC 0 : matches = complete_from_variables(text, ":\"", "\"", true);
918 akorotkov@postgresql 1987 [ + + + - ]:CBC 2 : else if (text[1] == '{' && text[2] == '?')
1988 : 1 : matches = complete_from_variables(text, ":{?", "}", true);
1989 : : else
4422 fujii@postgresql.org 1990 : 1 : matches = complete_from_variables(text, ":", "", true);
1991 : : }
1992 : :
1993 : : /* If no previous word, suggest one of the basic sql commands */
3928 tgl@sss.pgh.pa.us 1994 [ + + ]: 64 : else if (previous_words_count == 0)
9657 bruce@momjian.us 1995 : 2 : COMPLETE_WITH_LIST(sql_commands);
1996 : :
1997 : : /* Else try completions based on matching patterns of previous words */
1998 : : else
1999 : : {
2000 : : #ifdef SWITCH_CONVERSION_APPLIED
2001 : : /*
2002 : : * If we have transformed match_previous_words into a switch, iterate
2003 : : * through tcpatterns[] to see which pattern ids match.
2004 : : *
2005 : : * For now, we have to try the patterns in the order they are stored
2006 : : * (matching the order of switch cases in match_previous_words),
2007 : : * because some of the logic in match_previous_words assumes that
2008 : : * previous matches have been eliminated. This is fairly
2009 : : * unprincipled, and it is likely that there are undesirable as well
2010 : : * as desirable interactions hidden in the order of the pattern
2011 : : * checks. TODO: think about a better way to manage that.
2012 : : */
71 peter@eisentraut.org 2013 [ + + ]:GNC 35711 : for (size_t tindx = 0; tindx < lengthof(tcpatterns); tindx++)
2014 : : {
713 tgl@sss.pgh.pa.us 2015 :CBC 35705 : const TCPattern *tcpat = tcpatterns + tindx;
2016 : 35705 : bool match = false;
2017 : :
2018 [ + - + + : 35705 : switch (tcpat->kind)
+ + - ]
2019 : : {
2020 : 27567 : case Match:
2021 : 27567 : match = MatchesArray(false,
2022 : : previous_words_count,
2023 : : previous_words,
2024 : 27567 : tcpat->nwords, tcpat->words);
2025 : 27567 : break;
713 tgl@sss.pgh.pa.us 2026 :UBC 0 : case MatchCS:
2027 : 0 : match = MatchesArray(true,
2028 : : previous_words_count,
2029 : : previous_words,
2030 : 0 : tcpat->nwords, tcpat->words);
2031 : 0 : break;
713 tgl@sss.pgh.pa.us 2032 :CBC 751 : case HeadMatch:
2033 : 751 : match = HeadMatchesArray(false,
2034 : : previous_words_count,
2035 : : previous_words,
2036 : 751 : tcpat->nwords, tcpat->words);
2037 : 751 : break;
2038 : 20 : case HeadMatchCS:
2039 : 20 : match = HeadMatchesArray(true,
2040 : : previous_words_count,
2041 : : previous_words,
2042 : 20 : tcpat->nwords, tcpat->words);
2043 : 20 : break;
2044 : 6768 : case TailMatch:
2045 : 6768 : match = TailMatchesArray(false,
2046 : : previous_words_count,
2047 : : previous_words,
2048 : 6768 : tcpat->nwords, tcpat->words);
2049 : 6768 : break;
2050 : 599 : case TailMatchCS:
2051 : 599 : match = TailMatchesArray(true,
2052 : : previous_words_count,
2053 : : previous_words,
2054 : 599 : tcpat->nwords, tcpat->words);
2055 : 599 : break;
2056 : : }
2057 [ + + ]: 35705 : if (match)
2058 : : {
2059 : 58 : matches = match_previous_words(tcpat->id, text, start, end,
2060 : : previous_words,
2061 : : previous_words_count);
2062 [ + + ]: 58 : if (matches != NULL)
2063 : 56 : break;
2064 : : }
2065 : : }
2066 : : #else /* !SWITCH_CONVERSION_APPLIED */
2067 : : /*
2068 : : * If gen_tabcomplete.pl hasn't been applied to this code, just let
2069 : : * match_previous_words scan through all its patterns.
2070 : : */
2071 : : matches = match_previous_words(0, text, start, end,
2072 : : previous_words,
2073 : : previous_words_count);
2074 : : #endif /* SWITCH_CONVERSION_APPLIED */
2075 : : }
2076 : :
2077 : : /*
2078 : : * Finally, we look through the list of "things", such as TABLE, INDEX and
2079 : : * check if that was the previous word. If so, execute the query to get a
2080 : : * list of them.
2081 : : */
710 2082 [ + + + - ]: 67 : if (matches == NULL && previous_words_count > 0)
2083 : : {
2084 : : const pgsql_thing_t *wac;
2085 : :
713 2086 [ + + ]: 248 : for (wac = words_after_create; wac->name != NULL; wac++)
2087 : : {
2088 [ + + ]: 246 : if (pg_strcasecmp(prev_wd, wac->name) == 0)
2089 : : {
2090 [ + + ]: 4 : if (wac->query)
713 tgl@sss.pgh.pa.us 2091 :GBC 1 : COMPLETE_WITH_QUERY_LIST(wac->query,
2092 : : wac->keywords);
713 tgl@sss.pgh.pa.us 2093 [ - + ]:CBC 3 : else if (wac->vquery)
713 tgl@sss.pgh.pa.us 2094 :LBC (1) : COMPLETE_WITH_VERSIONED_QUERY_LIST(wac->vquery,
2095 : : wac->keywords);
713 tgl@sss.pgh.pa.us 2096 [ + - ]:CBC 3 : else if (wac->squery)
2097 : 3 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY_LIST(wac->squery,
2098 : : wac->keywords);
2099 : 4 : break;
2100 : : }
2101 : : }
2102 : : }
2103 : :
2104 : : /*
2105 : : * If we still don't have anything to match we have to fabricate some sort
2106 : : * of default list. If we were to just return NULL, readline automatically
2107 : : * attempts filename completion, and that's usually no good.
2108 : : */
2109 [ + + ]: 67 : if (matches == NULL)
2110 : : {
2111 : 2 : COMPLETE_WITH_CONST(true, "");
2112 : : /* Also, prevent Readline from appending stuff to the non-match */
2113 : 2 : rl_completion_append_character = '\0';
2114 : : #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
2115 : 2 : rl_completion_suppress_quote = 1;
2116 : : #endif
2117 : : }
2118 : :
2119 : : /* free storage */
81 peter@eisentraut.org 2120 :GNC 67 : pg_free(previous_words);
2121 : 67 : pg_free(words_buffer);
2122 : 67 : pfree(text_copy);
2123 : 67 : pg_free(completion_ref_object);
713 tgl@sss.pgh.pa.us 2124 :CBC 67 : completion_ref_object = NULL;
81 peter@eisentraut.org 2125 :GNC 67 : pg_free(completion_ref_schema);
713 tgl@sss.pgh.pa.us 2126 :CBC 67 : completion_ref_schema = NULL;
2127 : :
2128 : : /* Return our Grand List O' Matches */
2129 : 67 : return matches;
2130 : : }
2131 : :
2132 : : /*
2133 : : * Subroutine to try matches based on previous_words.
2134 : : *
2135 : : * This can operate in one of two modes. As presented, the body of the
2136 : : * function is a long if-else-if chain that sequentially tries each known
2137 : : * match rule. That works, but some C compilers have trouble with such a long
2138 : : * else-if chain, either taking extra time to compile or failing altogether.
2139 : : * Therefore, we prefer to transform the else-if chain into a switch, and then
2140 : : * each call of this function considers just one match rule (under control of
2141 : : * a loop in psql_completion()). Compilers tend to be more ready to deal
2142 : : * with many-arm switches than many-arm else-if chains.
2143 : : *
2144 : : * Each if-condition in this function must begin with a call of one of the
2145 : : * functions Matches, HeadMatches, TailMatches, MatchesCS, HeadMatchesCS, or
2146 : : * TailMatchesCS. The preprocessor gen_tabcomplete.pl strips out those
2147 : : * calls and converts them into entries in tcpatterns[], which are evaluated
2148 : : * by the calling loop in psql_completion(). Successful matches result in
2149 : : * calls to this function with the appropriate pattern_id, causing just the
2150 : : * corresponding switch case to be executed.
2151 : : *
2152 : : * If-conditions in this function can be more complex than a single *Matches
2153 : : * function call in one of two ways (but not both!). They can be OR's
2154 : : * of *Matches calls, such as
2155 : : * else if (Matches("ALTER", "VIEW", MatchAny, "ALTER", MatchAny) ||
2156 : : * Matches("ALTER", "VIEW", MatchAny, "ALTER", "COLUMN", MatchAny))
2157 : : * or they can be a *Matches call AND'ed with some other condition, e.g.
2158 : : * else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE", MatchAny) &&
2159 : : * !ends_with(prev_wd, ','))
2160 : : * The former case is transformed into multiple tcpatterns[] entries and
2161 : : * multiple case labels for the same bit of code. The latter case is
2162 : : * transformed into a case label and a contained if-statement.
2163 : : *
2164 : : * This is split out of psql_completion() primarily to separate code that
2165 : : * gen_tabcomplete.pl should process from code that it should not, although
2166 : : * doing so also helps to avoid extra indentation of this code.
2167 : : *
2168 : : * Returns a matches list, or NULL if no match.
2169 : : */
2170 : : static char **
2171 : 58 : match_previous_words(int pattern_id,
2172 : : const char *text, int start, int end,
2173 : : char **previous_words, int previous_words_count)
2174 : : {
2175 : : /* This is the variable we'll return. */
2176 : 58 : char **matches = NULL;
2177 : :
2178 : : /* Dummy statement, allowing all the match rules to look like "else if" */
2179 : : if (0)
2180 : : {
2181 : : /* skip */
2182 : : }
2183 : :
2184 : : /* gen_tabcomplete.pl begins special processing here */
2185 [ + - + - : 58 : /* BEGIN GEN_TABCOMPLETE */
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - -
+ - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - - -
- + + - -
+ - ]
2186 : :
2187 : : /* CREATE */
2188 : : /* complete with something you can create */
2921 2189 : 1 : else if (TailMatches("CREATE"))
2190 : : {
2191 : : /* only some object types can be created as part of CREATE SCHEMA */
1178 michael@paquier.xyz 2192 [ - + ]: 1 : if (HeadMatches("CREATE", "SCHEMA"))
9 tgl@sss.pgh.pa.us 2193 :UBC 0 : COMPLETE_WITH("TABLE", "VIEW", "INDEX", "SEQUENCE", "TRIGGER",
2194 : : /* for INDEX and TABLE/SEQUENCE, respectively */
2195 : : "UNIQUE", "UNLOGGED");
2196 : : else
360 msawada@postgresql.o 2197 :CBC 1 : COMPLETE_WITH_GENERATOR(create_command_generator);
2198 : : }
2199 : : /* complete with something you can create or replace */
2564 fujii@postgresql.org 2200 : 1 : else if (TailMatches("CREATE", "OR", "REPLACE"))
2564 fujii@postgresql.org 2201 :UBC 0 : COMPLETE_WITH("FUNCTION", "PROCEDURE", "LANGUAGE", "RULE", "VIEW",
2202 : : "AGGREGATE", "TRANSFORM", "TRIGGER");
2203 : :
2204 : : /* DROP, but not DROP embedded in other commands */
2205 : : /* complete with something you can drop */
2921 tgl@sss.pgh.pa.us 2206 : 0 : else if (Matches("DROP"))
360 msawada@postgresql.o 2207 :CBC 1 : COMPLETE_WITH_GENERATOR(drop_command_generator);
2208 : :
2209 : : /* ALTER */
2210 : :
2211 : : /* ALTER TABLE */
2921 tgl@sss.pgh.pa.us 2212 : 1 : else if (Matches("ALTER", "TABLE"))
1694 tgl@sss.pgh.pa.us 2213 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
2214 : : "ALL IN TABLESPACE");
2215 : :
2216 : : /* ALTER something */
2921 2217 : 0 : else if (Matches("ALTER"))
360 msawada@postgresql.o 2218 : 0 : COMPLETE_WITH_GENERATOR(alter_command_generator);
2219 : : /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx */
2921 tgl@sss.pgh.pa.us 2220 : 0 : else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny))
2221 : 0 : COMPLETE_WITH("SET TABLESPACE", "OWNED BY");
2222 : : /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY */
2223 : 0 : else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY"))
4396 sfrost@snowman.net 2224 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2225 : : /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx OWNED BY xxx */
2921 tgl@sss.pgh.pa.us 2226 : 0 : else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny, "OWNED", "BY", MatchAny))
2227 : 0 : COMPLETE_WITH("SET TABLESPACE");
2228 : : /* ALTER AGGREGATE,FUNCTION,PROCEDURE,ROUTINE <name> */
2229 : 0 : else if (Matches("ALTER", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny))
2230 : 0 : COMPLETE_WITH("(");
2231 : : /* ALTER AGGREGATE <name> (...) */
2041 michael@paquier.xyz 2232 : 0 : else if (Matches("ALTER", "AGGREGATE", MatchAny, MatchAny))
2233 : : {
3928 tgl@sss.pgh.pa.us 2234 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
2921 2235 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA");
2236 : : else
5190 magnus@hagander.net 2237 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2238 : : }
2239 : : /* ALTER FUNCTION <name> (...) */
1353 dean.a.rasheed@gmail 2240 : 0 : else if (Matches("ALTER", "FUNCTION", MatchAny, MatchAny))
2241 : : {
2242 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
2243 : 0 : COMPLETE_WITH(Alter_function_options);
2244 : : else
2245 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2246 : : }
2247 : : /* ALTER PROCEDURE <name> (...) */
2248 : 0 : else if (Matches("ALTER", "PROCEDURE", MatchAny, MatchAny))
2249 : : {
2250 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
2251 : 0 : COMPLETE_WITH(Alter_procedure_options);
2252 : : else
2253 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2254 : : }
2255 : : /* ALTER ROUTINE <name> (...) */
2256 : 0 : else if (Matches("ALTER", "ROUTINE", MatchAny, MatchAny))
2257 : : {
2041 michael@paquier.xyz 2258 [ # # ]: 0 : if (ends_with(prev_wd, ')'))
1353 dean.a.rasheed@gmail 2259 : 0 : COMPLETE_WITH(Alter_routine_options);
2260 : : else
2041 michael@paquier.xyz 2261 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2262 : : }
2263 : : /* ALTER FUNCTION|ROUTINE <name> (...) PARALLEL */
1353 dean.a.rasheed@gmail 2264 : 0 : else if (Matches("ALTER", "FUNCTION|ROUTINE", MatchAny, MatchAny, "PARALLEL"))
2265 : 0 : COMPLETE_WITH("RESTRICTED", "SAFE", "UNSAFE");
2266 : : /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) [EXTERNAL] SECURITY */
2267 : 0 : else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "SECURITY") ||
2268 : : Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "EXTERNAL", "SECURITY"))
2269 : 0 : COMPLETE_WITH("DEFINER", "INVOKER");
2270 : : /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) RESET */
2271 : 0 : else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "RESET"))
2272 : 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
2273 : : "ALL");
2274 : : /* ALTER FUNCTION|PROCEDURE|ROUTINE <name> (...) SET */
2275 : 0 : else if (Matches("ALTER", "FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny, "SET"))
2276 : 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
2277 : : "SCHEMA");
2278 : :
2279 : : /* ALTER PUBLICATION <name> */
2921 tgl@sss.pgh.pa.us 2280 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny))
1789 akapila@postgresql.o 2281 : 0 : COMPLETE_WITH("ADD", "DROP", "OWNER TO", "RENAME TO", "SET");
2282 : : /* ALTER PUBLICATION <name> ADD */
2283 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD"))
1459 alvherre@alvh.no-ip. 2284 : 0 : COMPLETE_WITH("TABLES IN SCHEMA", "TABLE");
713 tgl@sss.pgh.pa.us 2285 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE"))
2286 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2287 : 0 : else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE") &&
2288 [ # # ]: 0 : ends_with(prev_wd, ','))
1694 2289 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2290 : :
2291 : : /*
2292 : : * "ALTER PUBLICATION <name> SET TABLE <name> WHERE (" - complete with
2293 : : * table attributes
2294 : : *
2295 : : * "ALTER PUBLICATION <name> ADD TABLE <name> WHERE (" - complete with
2296 : : * table attributes
2297 : : */
713 2298 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "WHERE"))
1671 akapila@postgresql.o 2299 : 0 : COMPLETE_WITH("(");
713 tgl@sss.pgh.pa.us 2300 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "("))
1671 akapila@postgresql.o 2301 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2302 : 0 : else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE") &&
2303 [ # # ]: 0 : !TailMatches("WHERE", "(*)"))
2304 : 0 : COMPLETE_WITH(",", "WHERE (");
1696 alvherre@alvh.no-ip. 2305 : 0 : else if (HeadMatches("ALTER", "PUBLICATION", MatchAny, "ADD|SET", "TABLE"))
2306 : 0 : COMPLETE_WITH(",");
2307 : : /* ALTER PUBLICATION <name> DROP */
1789 akapila@postgresql.o 2308 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "DROP"))
1459 alvherre@alvh.no-ip. 2309 : 0 : COMPLETE_WITH("TABLES IN SCHEMA", "TABLE");
2310 : : /* ALTER PUBLICATION <name> SET */
2921 tgl@sss.pgh.pa.us 2311 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET"))
184 akapila@postgresql.o 2312 : 0 : COMPLETE_WITH("(", "ALL SEQUENCES", "ALL TABLES", "TABLES IN SCHEMA", "TABLE");
2313 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL"))
2314 : 0 : COMPLETE_WITH("SEQUENCES", "TABLES");
2315 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES"))
173 2316 : 0 : COMPLETE_WITH("EXCEPT ( TABLE");
184 2317 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT"))
173 2318 : 0 : COMPLETE_WITH("( TABLE");
2319 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "("))
2320 : 0 : COMPLETE_WITH("TABLE");
2321 : : /* Complete "ALTER PUBLICATION <name> FOR TABLE" with "<table>, ..." */
2322 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "(", "TABLE"))
2323 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2324 [ # # ]: 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && ends_with(prev_wd, ','))
184 2325 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
173 2326 [ # # ]: 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "SET", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && !ends_with(prev_wd, ','))
2327 : 0 : COMPLETE_WITH(")");
1451 alvherre@alvh.no-ip. 2328 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, "ADD|DROP|SET", "TABLES", "IN", "SCHEMA"))
1694 tgl@sss.pgh.pa.us 2329 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
2330 : : " AND nspname NOT LIKE E'pg\\\\_%%'",
2331 : : "CURRENT_SCHEMA");
2332 : : /* ALTER PUBLICATION <name> SET ( */
713 2333 : 0 : else if (Matches("ALTER", "PUBLICATION", MatchAny, MatchAnyN, "SET", "("))
682 akapila@postgresql.o 2334 : 0 : COMPLETE_WITH("publish", "publish_generated_columns", "publish_via_partition_root");
2335 : : /* ALTER SUBSCRIPTION <name> */
2921 tgl@sss.pgh.pa.us 2336 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny))
2337 : 0 : COMPLETE_WITH("CONNECTION", "ENABLE", "DISABLE", "OWNER TO",
2338 : : "RENAME TO", "REFRESH PUBLICATION", "REFRESH SEQUENCES",
2339 : : "SERVER", "SET", "SKIP (", "ADD PUBLICATION", "DROP PUBLICATION");
179 msawada@postgresql.o 2340 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, "SERVER"))
2341 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
2342 : : /* ALTER SUBSCRIPTION <name> REFRESH */
332 akapila@postgresql.o 2343 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "REFRESH"))
2344 : 0 : COMPLETE_WITH("PUBLICATION", "SEQUENCES");
2345 : : /* ALTER SUBSCRIPTION <name> REFRESH PUBLICATION */
55 2346 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "REFRESH", "PUBLICATION"))
2347 : 0 : COMPLETE_WITH("WITH (");
2348 : : /* ALTER SUBSCRIPTION <name> REFRESH PUBLICATION WITH ( */
713 tgl@sss.pgh.pa.us 2349 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "REFRESH", "PUBLICATION", "WITH", "("))
2921 2350 : 0 : COMPLETE_WITH("copy_data");
2351 : : /* ALTER SUBSCRIPTION <name> SET */
2352 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, "SET"))
2353 : 0 : COMPLETE_WITH("(", "PUBLICATION");
2354 : : /* ALTER SUBSCRIPTION <name> SET ( */
713 2355 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SET", "("))
80 akapila@postgresql.o 2356 :UNC 0 : COMPLETE_WITH("binary", "conflict_log_destination", "disable_on_error",
2357 : : "failover", "max_retention_duration", "origin",
2358 : : "password_required", "retain_dead_tuples",
2359 : : "run_as_owner", "slot_name", "streaming",
2360 : : "synchronous_commit", "two_phase",
2361 : : "wal_receiver_timeout");
2362 : : /* ALTER SUBSCRIPTION <name> SKIP ( */
713 tgl@sss.pgh.pa.us 2363 :UBC 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SKIP", "("))
1643 akapila@postgresql.o 2364 : 0 : COMPLETE_WITH("lsn");
2365 : : /* ALTER SUBSCRIPTION <name> SET PUBLICATION */
713 tgl@sss.pgh.pa.us 2366 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN, "SET", "PUBLICATION"))
2367 : : {
2368 : : /* complete with nothing here as this refers to remote publications */
2369 : : }
2370 : : /* ALTER SUBSCRIPTION <name> ADD|DROP|SET PUBLICATION <name> */
2371 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN,
2372 : : "ADD|DROP|SET", "PUBLICATION", MatchAny))
2921 2373 : 0 : COMPLETE_WITH("WITH (");
2374 : : /* ALTER SUBSCRIPTION <name> ADD|DROP|SET PUBLICATION <name> WITH ( */
713 2375 : 0 : else if (Matches("ALTER", "SUBSCRIPTION", MatchAny, MatchAnyN,
2376 : : "ADD|DROP|SET", "PUBLICATION", MatchAny, "WITH", "("))
2921 2377 : 0 : COMPLETE_WITH("copy_data", "refresh");
2378 : :
2379 : : /* ALTER SCHEMA <name> */
2380 : 0 : else if (Matches("ALTER", "SCHEMA", MatchAny))
2381 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO");
2382 : :
2383 : : /* ALTER COLLATION <name> */
2384 : 0 : else if (Matches("ALTER", "COLLATION", MatchAny))
1697 peter@eisentraut.org 2385 : 0 : COMPLETE_WITH("OWNER TO", "REFRESH VERSION", "RENAME TO", "SET SCHEMA");
2386 : :
2387 : : /* ALTER CONVERSION <name> */
2921 tgl@sss.pgh.pa.us 2388 : 0 : else if (Matches("ALTER", "CONVERSION", MatchAny))
2389 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA");
2390 : :
2391 : : /* ALTER DATABASE <name> */
2392 : 0 : else if (Matches("ALTER", "DATABASE", MatchAny))
1679 peter@eisentraut.org 2393 : 0 : COMPLETE_WITH("RESET", "SET", "OWNER TO", "REFRESH COLLATION VERSION", "RENAME TO",
2394 : : "IS_TEMPLATE", "ALLOW_CONNECTIONS",
2395 : : "CONNECTION LIMIT");
2396 : :
2397 : : /* ALTER DATABASE <name> RESET */
580 tomas.vondra@postgre 2398 : 0 : else if (Matches("ALTER", "DATABASE", MatchAny, "RESET"))
2399 : : {
2400 : 0 : set_completion_reference(prev2_wd);
2401 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_database_vars, "ALL");
2402 : : }
2403 : :
2404 : : /* ALTER DATABASE <name> SET TABLESPACE */
2921 tgl@sss.pgh.pa.us 2405 : 0 : else if (Matches("ALTER", "DATABASE", MatchAny, "SET", "TABLESPACE"))
2922 2406 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
2407 : :
2408 : : /* ALTER EVENT TRIGGER */
2921 2409 : 0 : else if (Matches("ALTER", "EVENT", "TRIGGER"))
4542 rhaas@postgresql.org 2410 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
2411 : :
2412 : : /* ALTER EVENT TRIGGER <name> */
2921 tgl@sss.pgh.pa.us 2413 : 0 : else if (Matches("ALTER", "EVENT", "TRIGGER", MatchAny))
2414 : 0 : COMPLETE_WITH("DISABLE", "ENABLE", "OWNER TO", "RENAME TO");
2415 : :
2416 : : /* ALTER EVENT TRIGGER <name> ENABLE */
2417 : 0 : else if (Matches("ALTER", "EVENT", "TRIGGER", MatchAny, "ENABLE"))
2418 : 0 : COMPLETE_WITH("REPLICA", "ALWAYS");
2419 : :
2420 : : /* ALTER EXTENSION <name> */
2421 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny))
1540 2422 : 0 : COMPLETE_WITH("ADD", "DROP", "UPDATE", "SET SCHEMA");
2423 : :
2424 : : /* ALTER EXTENSION <name> ADD|DROP */
1347 michael@paquier.xyz 2425 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP"))
2426 : 0 : COMPLETE_WITH("ACCESS METHOD", "AGGREGATE", "CAST", "COLLATION",
2427 : : "CONVERSION", "DOMAIN", "EVENT TRIGGER", "FOREIGN",
2428 : : "FUNCTION", "MATERIALIZED VIEW", "OPERATOR",
2429 : : "LANGUAGE", "PROCEDURE", "ROUTINE", "SCHEMA",
2430 : : "SEQUENCE", "SERVER", "TABLE", "TEXT SEARCH",
2431 : : "TRANSFORM FOR", "TYPE", "VIEW");
2432 : :
2433 : : /* ALTER EXTENSION <name> ADD|DROP FOREIGN */
2434 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "FOREIGN"))
2435 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
2436 : :
2437 : : /* ALTER EXTENSION <name> ADD|DROP OPERATOR */
2438 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "OPERATOR"))
2439 : 0 : COMPLETE_WITH("CLASS", "FAMILY");
2440 : :
2441 : : /* ALTER EXTENSION <name> ADD|DROP TEXT SEARCH */
2442 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "ADD|DROP", "TEXT", "SEARCH"))
2443 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
2444 : :
2445 : : /* ALTER EXTENSION <name> UPDATE */
2921 tgl@sss.pgh.pa.us 2446 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "UPDATE"))
1552 2447 : 0 : COMPLETE_WITH("TO");
2448 : :
2449 : : /* ALTER EXTENSION <name> UPDATE TO */
2921 2450 : 0 : else if (Matches("ALTER", "EXTENSION", MatchAny, "UPDATE", "TO"))
2451 : : {
1694 2452 : 0 : set_completion_reference(prev3_wd);
1552 2453 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions);
2454 : : }
2455 : :
2456 : : /* ALTER FOREIGN */
2921 2457 : 0 : else if (Matches("ALTER", "FOREIGN"))
2458 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
2459 : :
2460 : : /* ALTER FOREIGN DATA WRAPPER <name> */
2461 : 0 : else if (Matches("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny))
179 msawada@postgresql.o 2462 : 0 : COMPLETE_WITH("CONNECTION", "HANDLER", "NO",
2463 : : "OPTIONS", "OWNER TO", "RENAME TO", "VALIDATOR");
1756 michael@paquier.xyz 2464 : 0 : else if (Matches("ALTER", "FOREIGN", "DATA", "WRAPPER", MatchAny, "NO"))
179 msawada@postgresql.o 2465 : 0 : COMPLETE_WITH("CONNECTION", "HANDLER", "VALIDATOR");
2466 : :
2467 : : /* ALTER FOREIGN TABLE <name> */
2921 tgl@sss.pgh.pa.us 2468 : 0 : else if (Matches("ALTER", "FOREIGN", "TABLE", MatchAny))
2469 : 0 : COMPLETE_WITH("ADD", "ALTER", "DISABLE TRIGGER", "DROP", "ENABLE",
2470 : : "INHERIT", "NO INHERIT", "OPTIONS", "OWNER TO",
2471 : : "RENAME", "SET", "VALIDATE CONSTRAINT");
2472 : :
2473 : : /* ALTER INDEX */
2474 : 0 : else if (Matches("ALTER", "INDEX"))
1694 2475 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
2476 : : "ALL IN TABLESPACE");
2477 : : /* ALTER INDEX <name> */
2921 2478 : 0 : else if (Matches("ALTER", "INDEX", MatchAny))
2479 : 0 : COMPLETE_WITH("ALTER COLUMN", "OWNER TO", "RENAME TO", "SET",
2480 : : "RESET", "ATTACH PARTITION",
2481 : : "DEPENDS ON EXTENSION", "NO DEPENDS ON EXTENSION");
2482 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ATTACH"))
2483 : 0 : COMPLETE_WITH("PARTITION");
2484 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ATTACH", "PARTITION"))
1694 2485 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
2486 : : /* ALTER INDEX <name> ALTER */
2826 michael@paquier.xyz 2487 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER"))
1962 tmunro@postgresql.or 2488 : 0 : COMPLETE_WITH("COLUMN");
2489 : : /* ALTER INDEX <name> ALTER COLUMN */
2792 michael@paquier.xyz 2490 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN"))
2491 : : {
1694 tgl@sss.pgh.pa.us 2492 : 0 : set_completion_reference(prev3_wd);
2493 : 0 : COMPLETE_WITH_SCHEMA_QUERY_VERBATIM(Query_for_list_of_attribute_numbers);
2494 : : }
2495 : : /* ALTER INDEX <name> ALTER COLUMN <colnum> */
2921 2496 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny))
2497 : 0 : COMPLETE_WITH("SET STATISTICS");
2498 : : /* ALTER INDEX <name> ALTER COLUMN <colnum> SET */
2826 michael@paquier.xyz 2499 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny, "SET"))
2500 : 0 : COMPLETE_WITH("STATISTICS");
2501 : : /* ALTER INDEX <name> ALTER COLUMN <colnum> SET STATISTICS */
2502 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STATISTICS"))
2503 : : {
2504 : : /* Enforce no completion here, as an integer has to be specified */
2505 : : }
2506 : : /* ALTER INDEX <name> SET */
2921 tgl@sss.pgh.pa.us 2507 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "SET"))
2508 : 0 : COMPLETE_WITH("(", "TABLESPACE");
2509 : : /* ALTER INDEX <name> RESET */
2510 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "RESET"))
2511 : 0 : COMPLETE_WITH("(");
2512 : : /* ALTER INDEX <foo> SET|RESET ( */
2513 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "RESET", "("))
2805 2514 : 0 : COMPLETE_WITH("fillfactor",
2515 : : "deduplicate_items", /* BTREE */
2516 : : "fastupdate", "gin_pending_list_limit", /* GIN */
2517 : : "buffering", /* GiST */
2518 : : "pages_per_range", "autosummarize" /* BRIN */
2519 : : );
2921 2520 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "SET", "("))
2805 2521 : 0 : COMPLETE_WITH("fillfactor =",
2522 : : "deduplicate_items =", /* BTREE */
2523 : : "fastupdate =", "gin_pending_list_limit =", /* GIN */
2524 : : "buffering =", /* GiST */
2525 : : "pages_per_range =", "autosummarize =" /* BRIN */
2526 : : );
1962 tmunro@postgresql.or 2527 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "NO", "DEPENDS"))
2528 : 0 : COMPLETE_WITH("ON EXTENSION");
2529 : 0 : else if (Matches("ALTER", "INDEX", MatchAny, "DEPENDS"))
2530 : 0 : COMPLETE_WITH("ON EXTENSION");
2531 : :
2532 : : /* ALTER LANGUAGE <name> */
2921 tgl@sss.pgh.pa.us 2533 : 0 : else if (Matches("ALTER", "LANGUAGE", MatchAny))
2603 michael@paquier.xyz 2534 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO");
2535 : :
2536 : : /* ALTER LARGE OBJECT <oid> */
2921 tgl@sss.pgh.pa.us 2537 : 0 : else if (Matches("ALTER", "LARGE", "OBJECT", MatchAny))
2538 : 0 : COMPLETE_WITH("OWNER TO");
2539 : :
2540 : : /* ALTER MATERIALIZED VIEW */
2541 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW"))
1694 2542 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_matviews,
2543 : : "ALL IN TABLESPACE");
2544 : :
2545 : : /* ALTER USER,ROLE <name> */
2921 2546 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny) &&
2547 [ # # ]: 0 : !TailMatches("USER", "MAPPING"))
2548 : 0 : COMPLETE_WITH("BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE",
2549 : : "ENCRYPTED PASSWORD", "IN", "INHERIT", "LOGIN", "NOBYPASSRLS",
2550 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
2551 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
2552 : : "RENAME TO", "REPLICATION", "RESET", "SET", "SUPERUSER",
2553 : : "VALID UNTIL", "WITH");
2554 : : /* ALTER USER,ROLE <name> IN */
202 alvherre@kurilemu.de 2555 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN"))
2556 : 0 : COMPLETE_WITH("DATABASE");
2557 : : /* ALTER USER,ROLE <name> IN DATABASE */
2558 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN", "DATABASE"))
2559 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2560 : : /* ALTER USER,ROLE <name> IN DATABASE <dbname> */
2561 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN", "DATABASE", MatchAny))
2562 : 0 : COMPLETE_WITH("SET", "RESET");
2563 : : /* ALTER USER,ROLE <name> IN DATABASE <dbname> SET */
2564 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "IN", "DATABASE", MatchAny, "SET"))
2565 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_set_vars);
2566 : : /* XXX missing support for ALTER ROLE <name> IN DATABASE <dbname> RESET */
2567 : : /* ALTER USER,ROLE <name> RESET */
580 tomas.vondra@postgre 2568 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "RESET"))
2569 : : {
416 2570 : 0 : set_completion_reference(prev2_wd);
580 2571 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_user_vars, "ALL");
2572 : : }
2573 : :
2574 : : /* ALTER USER,ROLE <name> WITH */
2921 tgl@sss.pgh.pa.us 2575 : 0 : else if (Matches("ALTER", "USER|ROLE", MatchAny, "WITH"))
2576 : : /* Similar to the above, but don't complete "WITH" again. */
2577 : 0 : COMPLETE_WITH("BYPASSRLS", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE",
2578 : : "ENCRYPTED PASSWORD", "INHERIT", "LOGIN", "NOBYPASSRLS",
2579 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
2580 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
2581 : : "RENAME TO", "REPLICATION", "RESET", "SET", "SUPERUSER",
2582 : : "VALID UNTIL");
2583 : :
2584 : : /* ALTER DEFAULT PRIVILEGES */
2585 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES"))
895 msawada@postgresql.o 2586 : 0 : COMPLETE_WITH("FOR", "GRANT", "IN SCHEMA", "REVOKE");
2587 : : /* ALTER DEFAULT PRIVILEGES FOR */
2921 tgl@sss.pgh.pa.us 2588 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR"))
2589 : 0 : COMPLETE_WITH("ROLE");
2590 : : /* ALTER DEFAULT PRIVILEGES IN */
2591 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN"))
2592 : 0 : COMPLETE_WITH("SCHEMA");
2593 : : /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... */
2594 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER",
2595 : : MatchAny))
2596 : 0 : COMPLETE_WITH("GRANT", "REVOKE", "IN SCHEMA");
2597 : : /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... */
2598 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2599 : : MatchAny))
2600 : 0 : COMPLETE_WITH("GRANT", "REVOKE", "FOR ROLE");
2601 : : /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR */
2602 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2603 : : MatchAny, "FOR"))
2604 : 0 : COMPLETE_WITH("ROLE");
2605 : : /* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... IN SCHEMA ... */
2606 : : /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR ROLE|USER ... */
2607 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER",
2608 : : MatchAny, "IN", "SCHEMA", MatchAny) ||
2609 : : Matches("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA",
2610 : : MatchAny, "FOR", "ROLE|USER", MatchAny))
2611 : 0 : COMPLETE_WITH("GRANT", "REVOKE");
2612 : : /* ALTER DOMAIN <name> */
2613 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny))
2614 : 0 : COMPLETE_WITH("ADD", "DROP", "OWNER TO", "RENAME", "SET",
2615 : : "VALIDATE CONSTRAINT");
2616 : : /* ALTER DOMAIN <sth> ADD */
497 alvherre@kurilemu.de 2617 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "ADD"))
2618 : 0 : COMPLETE_WITH("CONSTRAINT", "NOT NULL", "CHECK (");
2619 : : /* ALTER DOMAIN <sth> ADD CONSTRAINT <sth> */
2620 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "ADD", "CONSTRAINT", MatchAny))
2621 : 0 : COMPLETE_WITH("NOT NULL", "CHECK (");
2622 : : /* ALTER DOMAIN <sth> DROP */
2921 tgl@sss.pgh.pa.us 2623 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "DROP"))
2624 : 0 : COMPLETE_WITH("CONSTRAINT", "DEFAULT", "NOT NULL");
2625 : : /* ALTER DOMAIN <sth> DROP|RENAME|VALIDATE CONSTRAINT */
2626 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "DROP|RENAME|VALIDATE", "CONSTRAINT"))
2627 : : {
1694 2628 : 0 : set_completion_reference(prev3_wd);
2629 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_type);
2630 : : }
2631 : : /* ALTER DOMAIN <sth> RENAME */
2921 2632 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "RENAME"))
2633 : 0 : COMPLETE_WITH("CONSTRAINT", "TO");
2634 : : /* ALTER DOMAIN <sth> RENAME CONSTRAINT <sth> */
2635 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "RENAME", "CONSTRAINT", MatchAny))
2636 : 0 : COMPLETE_WITH("TO");
2637 : :
2638 : : /* ALTER DOMAIN <sth> SET */
2639 : 0 : else if (Matches("ALTER", "DOMAIN", MatchAny, "SET"))
2640 : 0 : COMPLETE_WITH("DEFAULT", "NOT NULL", "SCHEMA");
2641 : : /* ALTER SEQUENCE <name> */
2642 : 0 : else if (Matches("ALTER", "SEQUENCE", MatchAny))
1756 michael@paquier.xyz 2643 : 0 : COMPLETE_WITH("AS", "INCREMENT", "MINVALUE", "MAXVALUE", "RESTART",
2644 : : "START", "NO", "CACHE", "CYCLE", "SET", "OWNED BY",
2645 : : "OWNER TO", "RENAME TO");
2646 : : /* ALTER SEQUENCE <name> AS */
2647 : 0 : else if (TailMatches("ALTER", "SEQUENCE", MatchAny, "AS"))
2648 : 0 : COMPLETE_WITH_CS("smallint", "integer", "bigint");
2649 : : /* ALTER SEQUENCE <name> NO */
2921 tgl@sss.pgh.pa.us 2650 : 0 : else if (Matches("ALTER", "SEQUENCE", MatchAny, "NO"))
2651 : 0 : COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
2652 : : /* ALTER SEQUENCE <name> SET */
1627 peter@eisentraut.org 2653 : 0 : else if (Matches("ALTER", "SEQUENCE", MatchAny, "SET"))
2654 : 0 : COMPLETE_WITH("SCHEMA", "LOGGED", "UNLOGGED");
2655 : : /* ALTER SERVER <name> */
2921 tgl@sss.pgh.pa.us 2656 : 0 : else if (Matches("ALTER", "SERVER", MatchAny))
2657 : 0 : COMPLETE_WITH("VERSION", "OPTIONS", "OWNER TO", "RENAME TO");
2658 : : /* ALTER SERVER <name> VERSION <version> */
2659 : 0 : else if (Matches("ALTER", "SERVER", MatchAny, "VERSION", MatchAny))
2660 : 0 : COMPLETE_WITH("OPTIONS");
2661 : : /* ALTER SYSTEM SET, RESET, RESET ALL */
2662 : 0 : else if (Matches("ALTER", "SYSTEM"))
2663 : 0 : COMPLETE_WITH("SET", "RESET");
2664 : 0 : else if (Matches("ALTER", "SYSTEM", "SET|RESET"))
1684 2665 : 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_alter_system_set_vars,
2666 : : "ALL");
2921 2667 : 0 : else if (Matches("ALTER", "SYSTEM", "SET", MatchAny))
2668 : 0 : COMPLETE_WITH("TO");
2669 : : /* ALTER VIEW <name> */
2670 : 0 : else if (Matches("ALTER", "VIEW", MatchAny))
1027 dean.a.rasheed@gmail 2671 : 0 : COMPLETE_WITH("ALTER COLUMN", "OWNER TO", "RENAME", "RESET", "SET");
2672 : : /* ALTER VIEW xxx RENAME */
2495 fujii@postgresql.org 2673 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RENAME"))
1694 tgl@sss.pgh.pa.us 2674 : 0 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "TO");
2495 fujii@postgresql.org 2675 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "ALTER|RENAME", "COLUMN"))
1694 tgl@sss.pgh.pa.us 2676 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2677 : : /* ALTER VIEW xxx ALTER [ COLUMN ] yyy */
1756 michael@paquier.xyz 2678 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "ALTER", MatchAny) ||
2679 : : Matches("ALTER", "VIEW", MatchAny, "ALTER", "COLUMN", MatchAny))
2680 : 0 : COMPLETE_WITH("SET DEFAULT", "DROP DEFAULT");
2681 : : /* ALTER VIEW xxx RENAME yyy */
2495 fujii@postgresql.org 2682 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RENAME", MatchAnyExcept("TO")))
2683 : 0 : COMPLETE_WITH("TO");
2684 : : /* ALTER VIEW xxx RENAME COLUMN yyy */
2685 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RENAME", "COLUMN", MatchAnyExcept("TO")))
2686 : 0 : COMPLETE_WITH("TO");
2687 : : /* ALTER VIEW xxx RESET ( */
1027 dean.a.rasheed@gmail 2688 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "RESET"))
2689 : 0 : COMPLETE_WITH("(");
2690 : : /* Complete ALTER VIEW xxx SET with "(" or "SCHEMA" */
2691 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET"))
2692 : 0 : COMPLETE_WITH("(", "SCHEMA");
2693 : : /* ALTER VIEW xxx SET|RESET ( yyy [= zzz] ) */
2694 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET|RESET", "("))
2695 : 0 : COMPLETE_WITH_LIST(view_optional_parameters);
2696 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", MatchAny))
2697 : 0 : COMPLETE_WITH("=");
2698 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", "check_option", "="))
2699 : 0 : COMPLETE_WITH("local", "cascaded");
2700 : 0 : else if (Matches("ALTER", "VIEW", MatchAny, "SET", "(", "security_barrier|security_invoker", "="))
2701 : 0 : COMPLETE_WITH("true", "false");
2702 : :
2703 : : /* ALTER MATERIALIZED VIEW <name> */
2921 tgl@sss.pgh.pa.us 2704 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny))
2495 fujii@postgresql.org 2705 : 0 : COMPLETE_WITH("ALTER COLUMN", "CLUSTER ON", "DEPENDS ON EXTENSION",
2706 : : "NO DEPENDS ON EXTENSION", "OWNER TO", "RENAME",
2707 : : "RESET (", "SET");
2708 : : /* ALTER MATERIALIZED VIEW xxx RENAME */
2709 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME"))
1694 tgl@sss.pgh.pa.us 2710 : 0 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "TO");
2495 fujii@postgresql.org 2711 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "ALTER|RENAME", "COLUMN"))
1694 tgl@sss.pgh.pa.us 2712 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2713 : : /* ALTER MATERIALIZED VIEW xxx RENAME yyy */
2495 fujii@postgresql.org 2714 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME", MatchAnyExcept("TO")))
2715 : 0 : COMPLETE_WITH("TO");
2716 : : /* ALTER MATERIALIZED VIEW xxx RENAME COLUMN yyy */
2717 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "RENAME", "COLUMN", MatchAnyExcept("TO")))
2718 : 0 : COMPLETE_WITH("TO");
2719 : : /* ALTER MATERIALIZED VIEW xxx SET */
2720 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "SET"))
1646 michael@paquier.xyz 2721 : 0 : COMPLETE_WITH("(", "ACCESS METHOD", "SCHEMA", "TABLESPACE", "WITHOUT CLUSTER");
2722 : : /* ALTER MATERIALIZED VIEW xxx SET ACCESS METHOD */
2723 : 0 : else if (Matches("ALTER", "MATERIALIZED", "VIEW", MatchAny, "SET", "ACCESS", "METHOD"))
2724 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
2725 : :
2726 : : /* ALTER POLICY <name> */
2921 tgl@sss.pgh.pa.us 2727 : 0 : else if (Matches("ALTER", "POLICY"))
3937 rhaas@postgresql.org 2728 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_policies);
2729 : : /* ALTER POLICY <name> ON */
2921 tgl@sss.pgh.pa.us 2730 : 0 : else if (Matches("ALTER", "POLICY", MatchAny))
2731 : 0 : COMPLETE_WITH("ON");
2732 : : /* ALTER POLICY <name> ON <table> */
2733 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON"))
2734 : : {
1694 2735 : 0 : set_completion_reference(prev2_wd);
2736 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
2737 : : }
2738 : : /* ALTER POLICY <name> ON <table> - show options */
2921 2739 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny))
2740 : 0 : COMPLETE_WITH("RENAME TO", "TO", "USING (", "WITH CHECK (");
2741 : : /* ALTER POLICY <name> ON <table> TO <role> */
2742 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "TO"))
1694 2743 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
2744 : : Keywords_for_list_of_grant_roles);
2745 : : /* ALTER POLICY <name> ON <table> USING ( */
2921 2746 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "USING"))
2747 : 0 : COMPLETE_WITH("(");
2748 : : /* ALTER POLICY <name> ON <table> WITH CHECK ( */
2749 : 0 : else if (Matches("ALTER", "POLICY", MatchAny, "ON", MatchAny, "WITH", "CHECK"))
2750 : 0 : COMPLETE_WITH("(");
2751 : :
2752 : : /* ALTER RULE <name>, add ON */
2753 : 0 : else if (Matches("ALTER", "RULE", MatchAny))
2754 : 0 : COMPLETE_WITH("ON");
2755 : :
2756 : : /* If we have ALTER RULE <name> ON, then add the correct tablename */
2757 : 0 : else if (Matches("ALTER", "RULE", MatchAny, "ON"))
2758 : : {
1694 2759 : 0 : set_completion_reference(prev2_wd);
2760 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
2761 : : }
2762 : :
2763 : : /* ALTER RULE <name> ON <name> */
2921 2764 : 0 : else if (Matches("ALTER", "RULE", MatchAny, "ON", MatchAny))
2765 : 0 : COMPLETE_WITH("RENAME TO");
2766 : :
2767 : : /* ALTER STATISTICS <name> */
2768 : 0 : else if (Matches("ALTER", "STATISTICS", MatchAny))
2567 tomas.vondra@postgre 2769 : 0 : COMPLETE_WITH("OWNER TO", "RENAME TO", "SET SCHEMA", "SET STATISTICS");
2770 : : /* ALTER STATISTICS <name> SET */
1427 michael@paquier.xyz 2771 : 0 : else if (Matches("ALTER", "STATISTICS", MatchAny, "SET"))
2772 : 0 : COMPLETE_WITH("SCHEMA", "STATISTICS");
2773 : :
2774 : : /* ALTER TRIGGER <name>, add ON */
2921 tgl@sss.pgh.pa.us 2775 : 0 : else if (Matches("ALTER", "TRIGGER", MatchAny))
2776 : 0 : COMPLETE_WITH("ON");
2777 : :
1694 2778 : 0 : else if (Matches("ALTER", "TRIGGER", MatchAny, "ON"))
2779 : : {
2780 : 0 : set_completion_reference(prev2_wd);
2781 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
2782 : : }
2783 : :
2784 : : /* ALTER TRIGGER <name> ON <name> */
2921 2785 : 0 : else if (Matches("ALTER", "TRIGGER", MatchAny, "ON", MatchAny))
2041 michael@paquier.xyz 2786 : 0 : COMPLETE_WITH("RENAME TO", "DEPENDS ON EXTENSION",
2787 : : "NO DEPENDS ON EXTENSION");
2788 : :
2789 : : /*
2790 : : * If we detect ALTER TABLE <name>, suggest sub commands
2791 : : */
2921 tgl@sss.pgh.pa.us 2792 : 0 : else if (Matches("ALTER", "TABLE", MatchAny))
2793 : 0 : COMPLETE_WITH("ADD", "ALTER", "CLUSTER ON", "DISABLE", "DROP",
2794 : : "ENABLE", "INHERIT", "NO", "RENAME", "RESET",
2795 : : "OWNER TO", "SET", "VALIDATE CONSTRAINT",
2796 : : "REPLICA IDENTITY", "ATTACH PARTITION",
2797 : : "DETACH PARTITION", "FORCE ROW LEVEL SECURITY",
2798 : : "OF", "NOT OF");
2799 : : /* ALTER TABLE xxx ADD */
1847 michael@paquier.xyz 2800 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD"))
2801 : : {
2802 : : /*
2803 : : * make sure to keep this list and the MatchAnyExcept() below in sync
2804 : : */
444 alvherre@kurilemu.de 2805 : 0 : COMPLETE_WITH("COLUMN", "CONSTRAINT", "CHECK (", "NOT NULL", "UNIQUE",
2806 : : "PRIMARY KEY", "EXCLUDE", "FOREIGN KEY");
2807 : : }
2808 : : /* ALTER TABLE xxx ADD [COLUMN] yyy */
1846 michael@paquier.xyz 2809 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny) ||
2810 : : Matches("ALTER", "TABLE", MatchAny, "ADD", MatchAnyExcept("COLUMN|CONSTRAINT|CHECK|UNIQUE|PRIMARY|NOT|EXCLUDE|FOREIGN")))
1694 tgl@sss.pgh.pa.us 2811 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2812 : : /* ALTER TABLE xxx ADD CONSTRAINT yyy */
1847 michael@paquier.xyz 2813 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny))
444 alvherre@kurilemu.de 2814 : 0 : COMPLETE_WITH("CHECK (", "NOT NULL", "UNIQUE", "PRIMARY KEY", "EXCLUDE", "FOREIGN KEY");
2815 : : /* ALTER TABLE xxx ADD NOT NULL */
2816 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "NOT", "NULL"))
2817 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
2818 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "NOT", "NULL"))
2819 : 0 : COMPLETE_WITH_ATTR(prev6_wd);
2820 : : /* ALTER TABLE xxx ADD [CONSTRAINT yyy] (PRIMARY KEY|UNIQUE) */
1847 michael@paquier.xyz 2821 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "PRIMARY", "KEY") ||
2822 : : Matches("ALTER", "TABLE", MatchAny, "ADD", "UNIQUE") ||
2823 : : Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "PRIMARY", "KEY") ||
2824 : : Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "UNIQUE"))
2825 : 0 : COMPLETE_WITH("(", "USING INDEX");
2826 : : /* ALTER TABLE xxx ADD PRIMARY KEY USING INDEX */
2827 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "PRIMARY", "KEY", "USING", "INDEX"))
2828 : : {
1694 tgl@sss.pgh.pa.us 2829 : 0 : set_completion_reference(prev6_wd);
2830 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2831 : : }
2832 : : /* ALTER TABLE xxx ADD UNIQUE USING INDEX */
1847 michael@paquier.xyz 2833 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "UNIQUE", "USING", "INDEX"))
2834 : : {
1694 tgl@sss.pgh.pa.us 2835 : 0 : set_completion_reference(prev5_wd);
2836 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2837 : : }
2838 : : /* ALTER TABLE xxx ADD CONSTRAINT yyy PRIMARY KEY USING INDEX */
1847 michael@paquier.xyz 2839 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny,
2840 : : "PRIMARY", "KEY", "USING", "INDEX"))
2841 : : {
1694 tgl@sss.pgh.pa.us 2842 : 0 : set_completion_reference(prev8_wd);
2843 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2844 : : }
2845 : : /* ALTER TABLE xxx ADD CONSTRAINT yyy UNIQUE USING INDEX */
1847 michael@paquier.xyz 2846 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny,
2847 : : "UNIQUE", "USING", "INDEX"))
2848 : : {
1694 tgl@sss.pgh.pa.us 2849 : 0 : set_completion_reference(prev7_wd);
2850 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_unique_index_of_table);
2851 : : }
2852 : : /* ALTER TABLE xxx ENABLE */
2921 2853 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE"))
2854 : 0 : COMPLETE_WITH("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE",
2855 : : "TRIGGER");
2856 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "REPLICA|ALWAYS"))
2857 : 0 : COMPLETE_WITH("RULE", "TRIGGER");
2858 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "RULE"))
2859 : : {
1694 2860 : 0 : set_completion_reference(prev3_wd);
2861 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2862 : : }
2921 2863 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "RULE"))
2864 : : {
1694 2865 : 0 : set_completion_reference(prev4_wd);
2866 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2867 : : }
2921 2868 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", "TRIGGER"))
2869 : : {
1694 2870 : 0 : set_completion_reference(prev3_wd);
2871 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2872 : : }
2921 2873 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE", MatchAny, "TRIGGER"))
2874 : : {
1694 2875 : 0 : set_completion_reference(prev4_wd);
2876 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2877 : : }
2878 : : /* ALTER TABLE xxx INHERIT */
2921 2879 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "INHERIT"))
1694 2880 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2881 : : /* ALTER TABLE xxx NO */
2157 michael@paquier.xyz 2882 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "NO"))
2883 : 0 : COMPLETE_WITH("FORCE ROW LEVEL SECURITY", "INHERIT");
2884 : : /* ALTER TABLE xxx NO INHERIT */
2921 tgl@sss.pgh.pa.us 2885 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "NO", "INHERIT"))
1694 2886 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2887 : : /* ALTER TABLE xxx DISABLE */
2921 2888 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE"))
2889 : 0 : COMPLETE_WITH("ROW LEVEL SECURITY", "RULE", "TRIGGER");
2890 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE", "RULE"))
2891 : : {
1694 2892 : 0 : set_completion_reference(prev3_wd);
2893 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_rule_of_table);
2894 : : }
2921 2895 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DISABLE", "TRIGGER"))
2896 : : {
1694 2897 : 0 : set_completion_reference(prev3_wd);
2898 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_trigger_of_table);
2899 : : }
2900 : :
2901 : : /* ALTER TABLE xxx ALTER */
2921 2902 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER"))
1694 2903 : 0 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "CONSTRAINT");
2904 : :
2905 : : /* ALTER TABLE xxx RENAME */
2921 2906 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RENAME"))
1694 tgl@sss.pgh.pa.us 2907 :CBC 12 : COMPLETE_WITH_ATTR_PLUS(prev2_wd, "COLUMN", "CONSTRAINT", "TO");
2921 2908 : 12 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|RENAME", "COLUMN"))
1694 tgl@sss.pgh.pa.us 2909 :UBC 0 : COMPLETE_WITH_ATTR(prev3_wd);
2910 : :
2911 : : /* ALTER TABLE xxx RENAME yyy */
2921 2912 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RENAME", MatchAnyExcept("CONSTRAINT|TO")))
2913 : 0 : COMPLETE_WITH("TO");
2914 : :
2915 : : /* ALTER TABLE xxx RENAME COLUMN/CONSTRAINT yyy */
2916 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RENAME", "COLUMN|CONSTRAINT", MatchAnyExcept("TO")))
2917 : 0 : COMPLETE_WITH("TO");
2918 : :
2919 : : /* If we have ALTER TABLE <sth> DROP, provide COLUMN or CONSTRAINT */
2920 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DROP"))
2921 : 0 : COMPLETE_WITH("COLUMN", "CONSTRAINT");
2922 : : /* If we have ALTER TABLE <sth> DROP COLUMN, provide list of columns */
2923 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DROP", "COLUMN"))
1694 2924 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
2925 : : /* ALTER TABLE <sth> ALTER|DROP|RENAME CONSTRAINT <constraint> */
1721 2926 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER|DROP|RENAME", "CONSTRAINT"))
2927 : : {
1694 tgl@sss.pgh.pa.us 2928 :CBC 3 : set_completion_reference(prev3_wd);
2929 : 3 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_table);
2930 : : }
2931 : : /* ALTER TABLE <sth> VALIDATE CONSTRAINT <non-validated constraint> */
1721 2932 : 3 : else if (Matches("ALTER", "TABLE", MatchAny, "VALIDATE", "CONSTRAINT"))
2933 : : {
1694 tgl@sss.pgh.pa.us 2934 :UBC 0 : set_completion_reference(prev3_wd);
2935 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_constraint_of_table_not_validated);
2936 : : }
2937 : : /* ALTER TABLE ALTER [COLUMN] <foo> */
2921 2938 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny) ||
2939 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny))
2940 : 0 : COMPLETE_WITH("TYPE", "SET", "RESET", "RESTART", "ADD", "DROP");
2941 : : /* ALTER TABLE ALTER [COLUMN] <foo> ADD */
1419 peter@eisentraut.org 2942 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD") ||
2943 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD"))
2944 : 0 : COMPLETE_WITH("GENERATED");
2945 : : /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED */
2946 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED") ||
2947 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED"))
2948 : 0 : COMPLETE_WITH("ALWAYS", "BY DEFAULT");
2949 : : /* ALTER TABLE ALTER [COLUMN] <foo> ADD GENERATED */
2950 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED", "ALWAYS") ||
2951 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED", "ALWAYS") ||
2952 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "ADD", "GENERATED", "BY", "DEFAULT") ||
2953 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "ADD", "GENERATED", "BY", "DEFAULT"))
2954 : 0 : COMPLETE_WITH("AS IDENTITY");
2955 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET */
2921 tgl@sss.pgh.pa.us 2956 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET") ||
2957 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET"))
906 msawada@postgresql.o 2958 : 0 : COMPLETE_WITH("(", "COMPRESSION", "DATA TYPE", "DEFAULT", "EXPRESSION", "GENERATED", "NOT NULL",
2959 : : "STATISTICS", "STORAGE",
2960 : : /* a subset of ALTER SEQUENCE options */
2961 : : "INCREMENT", "MINVALUE", "MAXVALUE", "START", "NO", "CACHE", "CYCLE");
2962 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET ( */
2921 tgl@sss.pgh.pa.us 2963 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "(") ||
2964 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "("))
2965 : 0 : COMPLETE_WITH("n_distinct", "n_distinct_inherited");
2966 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET COMPRESSION */
1475 michael@paquier.xyz 2967 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "COMPRESSION") ||
2968 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "COMPRESSION"))
2969 : 0 : COMPLETE_WITH("DEFAULT", "PGLZ", "LZ4");
2970 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET EXPRESSION */
990 peter@eisentraut.org 2971 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "EXPRESSION") ||
2972 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "EXPRESSION"))
2973 : 0 : COMPLETE_WITH("AS");
2974 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET EXPRESSION AS */
2975 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "EXPRESSION", "AS") ||
2976 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "EXPRESSION", "AS"))
2977 : 0 : COMPLETE_WITH("(");
2978 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET GENERATED */
1419 2979 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "GENERATED") ||
2980 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "GENERATED"))
2981 : 0 : COMPLETE_WITH("ALWAYS", "BY DEFAULT");
2982 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET NO */
2983 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "NO") ||
2984 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "NO"))
2985 : 0 : COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
2986 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET STORAGE */
2921 tgl@sss.pgh.pa.us 2987 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STORAGE") ||
2988 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STORAGE"))
1410 2989 : 0 : COMPLETE_WITH("DEFAULT", "PLAIN", "EXTERNAL", "EXTENDED", "MAIN");
2990 : : /* ALTER TABLE ALTER [COLUMN] <foo> SET STATISTICS */
2826 michael@paquier.xyz 2991 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STATISTICS") ||
2992 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STATISTICS"))
2993 : : {
2994 : : /* Enforce no completion here, as an integer has to be specified */
2995 : : }
2996 : : /* ALTER TABLE ALTER [COLUMN] <foo> DROP */
2921 tgl@sss.pgh.pa.us 2997 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "DROP") ||
2998 : : Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "DROP"))
2441 peter@eisentraut.org 2999 : 0 : COMPLETE_WITH("DEFAULT", "EXPRESSION", "IDENTITY", "NOT NULL");
2921 tgl@sss.pgh.pa.us 3000 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "CLUSTER"))
3001 : 0 : COMPLETE_WITH("ON");
3002 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "CLUSTER", "ON"))
3003 : : {
1694 3004 : 0 : set_completion_reference(prev3_wd);
3005 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
3006 : : }
3007 : : /* If we have ALTER TABLE <sth> SET, provide list of attributes and '(' */
2921 3008 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET"))
1880 michael@paquier.xyz 3009 : 0 : COMPLETE_WITH("(", "ACCESS METHOD", "LOGGED", "SCHEMA",
3010 : : "TABLESPACE", "UNLOGGED", "WITH", "WITHOUT");
3011 : :
3012 : : /*
3013 : : * If we have ALTER TABLE <sth> SET ACCESS METHOD provide a list of table
3014 : : * AMs.
3015 : : */
3016 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET", "ACCESS", "METHOD"))
926 3017 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_table_access_methods,
3018 : : "DEFAULT");
3019 : :
3020 : : /*
3021 : : * If we have ALTER TABLE <sth> SET TABLESPACE provide a list of
3022 : : * tablespaces
3023 : : */
2921 tgl@sss.pgh.pa.us 3024 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET", "TABLESPACE"))
8066 bruce@momjian.us 3025 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
3026 : : /* If we have ALTER TABLE <sth> SET WITHOUT provide CLUSTER or OIDS */
2921 tgl@sss.pgh.pa.us 3027 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET", "WITHOUT"))
3028 : 0 : COMPLETE_WITH("CLUSTER", "OIDS");
3029 : : /* ALTER TABLE <foo> RESET */
3030 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "RESET"))
3031 : 0 : COMPLETE_WITH("(");
3032 : : /* ALTER TABLE <foo> SET|RESET ( */
3033 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "SET|RESET", "("))
2828 michael@paquier.xyz 3034 : 0 : COMPLETE_WITH_LIST(table_storage_parameters);
2921 tgl@sss.pgh.pa.us 3035 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING", "INDEX"))
3036 : : {
1694 3037 : 0 : set_completion_reference(prev5_wd);
3038 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
3039 : : }
2921 3040 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY", "USING"))
3041 : 0 : COMPLETE_WITH("INDEX");
3042 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA", "IDENTITY"))
3043 : 0 : COMPLETE_WITH("FULL", "NOTHING", "DEFAULT", "USING");
3044 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "REPLICA"))
3045 : 0 : COMPLETE_WITH("IDENTITY");
3046 : :
3047 : : /*
3048 : : * If we have ALTER TABLE <foo> ATTACH PARTITION, provide a list of
3049 : : * tables.
3050 : : */
3051 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "ATTACH", "PARTITION"))
1694 3052 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3053 : : /* Limited completion support for partition bound specification */
2921 3054 : 0 : else if (TailMatches("ATTACH", "PARTITION", MatchAny))
3055 : 0 : COMPLETE_WITH("FOR VALUES", "DEFAULT");
3056 : 0 : else if (TailMatches("FOR", "VALUES"))
3057 : 0 : COMPLETE_WITH("FROM (", "IN (", "WITH (");
3058 : :
3059 : : /*
3060 : : * If we have ALTER TABLE <foo> DETACH PARTITION, provide a list of
3061 : : * partitions of <foo>.
3062 : : */
24 akorotkov@postgresql 3063 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DETACH", "PARTITION"))
3064 : : {
1694 tgl@sss.pgh.pa.us 3065 : 0 : set_completion_reference(prev3_wd);
3066 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_partition_of_table);
3067 : : }
1977 alvherre@alvh.no-ip. 3068 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "DETACH", "PARTITION", MatchAny))
3069 : 0 : COMPLETE_WITH("CONCURRENTLY", "FINALIZE");
3070 : :
3071 : : /* ALTER TABLE <name> OF */
1471 michael@paquier.xyz 3072 : 0 : else if (Matches("ALTER", "TABLE", MatchAny, "OF"))
3073 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_composite_datatypes);
3074 : :
3075 : : /* ALTER TABLESPACE <foo> with RENAME TO, OWNER TO, SET, RESET */
2921 tgl@sss.pgh.pa.us 3076 : 0 : else if (Matches("ALTER", "TABLESPACE", MatchAny))
3077 : 0 : COMPLETE_WITH("RENAME TO", "OWNER TO", "SET", "RESET");
3078 : : /* ALTER TABLESPACE <foo> SET|RESET */
3079 : 0 : else if (Matches("ALTER", "TABLESPACE", MatchAny, "SET|RESET"))
3080 : 0 : COMPLETE_WITH("(");
3081 : : /* ALTER TABLESPACE <foo> SET|RESET ( */
3082 : 0 : else if (Matches("ALTER", "TABLESPACE", MatchAny, "SET|RESET", "("))
3083 : 0 : COMPLETE_WITH("seq_page_cost", "random_page_cost",
3084 : : "effective_io_concurrency", "maintenance_io_concurrency");
3085 : :
3086 : : /* ALTER TEXT SEARCH */
3087 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH"))
3088 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
3089 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH", "TEMPLATE|PARSER", MatchAny))
3090 : 0 : COMPLETE_WITH("RENAME TO", "SET SCHEMA");
3091 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH", "DICTIONARY", MatchAny))
2388 3092 : 0 : COMPLETE_WITH("(", "OWNER TO", "RENAME TO", "SET SCHEMA");
2921 3093 : 0 : else if (Matches("ALTER", "TEXT", "SEARCH", "CONFIGURATION", MatchAny))
3094 : 0 : COMPLETE_WITH("ADD MAPPING FOR", "ALTER MAPPING",
3095 : : "DROP MAPPING FOR",
3096 : : "OWNER TO", "RENAME TO", "SET SCHEMA");
3097 : :
3098 : : /* complete ALTER TYPE <foo> with actions */
3099 : 0 : else if (Matches("ALTER", "TYPE", MatchAny))
3100 : 0 : COMPLETE_WITH("ADD ATTRIBUTE", "ADD VALUE", "ALTER ATTRIBUTE",
3101 : : "DROP ATTRIBUTE",
3102 : : "OWNER TO", "RENAME", "SET SCHEMA", "SET (");
3103 : : /* complete ALTER TYPE <foo> ADD with actions */
3104 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ADD"))
3105 : 0 : COMPLETE_WITH("ATTRIBUTE", "VALUE");
3106 : : /* ALTER TYPE <foo> RENAME */
3107 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME"))
3108 : 0 : COMPLETE_WITH("ATTRIBUTE", "TO", "VALUE");
3109 : : /* ALTER TYPE xxx RENAME (ATTRIBUTE|VALUE) yyy */
3110 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE|VALUE", MatchAny))
3111 : 0 : COMPLETE_WITH("TO");
3112 : : /* ALTER TYPE xxx RENAME ATTRIBUTE yyy TO zzz */
643 tomas.vondra@postgre 3113 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "ATTRIBUTE", MatchAny, "TO", MatchAny))
3114 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3115 : :
3116 : : /*
3117 : : * If we have ALTER TYPE <sth> ALTER/DROP/RENAME ATTRIBUTE, provide list
3118 : : * of attributes
3119 : : */
2921 tgl@sss.pgh.pa.us 3120 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ALTER|DROP|RENAME", "ATTRIBUTE"))
1694 3121 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
3122 : : /* complete ALTER TYPE ADD ATTRIBUTE <foo> with list of types */
643 tomas.vondra@postgre 3123 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ADD", "ATTRIBUTE", MatchAny))
3124 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
3125 : : /* complete ALTER TYPE ADD ATTRIBUTE <foo> <footype> with CASCADE/RESTRICT */
3126 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ADD", "ATTRIBUTE", MatchAny, MatchAny))
3127 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3128 : : /* complete ALTER TYPE DROP ATTRIBUTE <foo> with CASCADE/RESTRICT */
3129 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "DROP", "ATTRIBUTE", MatchAny))
3130 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3131 : : /* ALTER TYPE ALTER ATTRIBUTE <foo> */
2921 tgl@sss.pgh.pa.us 3132 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny))
3133 : 0 : COMPLETE_WITH("TYPE");
3134 : : /* ALTER TYPE ALTER ATTRIBUTE <foo> TYPE <footype> */
643 tomas.vondra@postgre 3135 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "ALTER", "ATTRIBUTE", MatchAny, "TYPE", MatchAny))
3136 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
3137 : : /* complete ALTER TYPE <sth> RENAME VALUE with list of enum values */
1497 michael@paquier.xyz 3138 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "RENAME", "VALUE"))
1497 michael@paquier.xyz 3139 [ + - + - :CBC 3 : COMPLETE_WITH_ENUM_VALUE(prev3_wd);
- + ]
3140 : : /* ALTER TYPE <foo> SET */
3141 : 3 : else if (Matches("ALTER", "TYPE", MatchAny, "SET"))
1497 michael@paquier.xyz 3142 :UBC 0 : COMPLETE_WITH("(", "SCHEMA");
3143 : : /* complete ALTER TYPE <foo> SET ( with settable properties */
3144 : 0 : else if (Matches("ALTER", "TYPE", MatchAny, "SET", "("))
3145 : 0 : COMPLETE_WITH("ANALYZE", "RECEIVE", "SEND", "STORAGE", "SUBSCRIPT",
3146 : : "TYPMOD_IN", "TYPMOD_OUT");
3147 : :
3148 : : /* complete ALTER GROUP <foo> */
2921 tgl@sss.pgh.pa.us 3149 : 0 : else if (Matches("ALTER", "GROUP", MatchAny))
3150 : 0 : COMPLETE_WITH("ADD USER", "DROP USER", "RENAME TO");
3151 : : /* complete ALTER GROUP <foo> ADD|DROP with USER */
3152 : 0 : else if (Matches("ALTER", "GROUP", MatchAny, "ADD|DROP"))
3153 : 0 : COMPLETE_WITH("USER");
3154 : : /* complete ALTER GROUP <foo> ADD|DROP USER with a user name */
3155 : 0 : else if (Matches("ALTER", "GROUP", MatchAny, "ADD|DROP", "USER"))
7707 3156 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
3157 : :
3158 : : /*
3159 : : * ANALYZE [ ( option [, ...] ) ] [ [ ONLY ] table_and_columns [, ...] ]
3160 : : * ANALYZE [ VERBOSE ] [ [ ONLY ] table_and_columns [, ...] ]
3161 : : */
2921 3162 : 0 : else if (Matches("ANALYZE"))
1694 3163 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
3164 : : "(", "VERBOSE", "ONLY");
546 3165 : 0 : else if (Matches("ANALYZE", "VERBOSE"))
3166 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
3167 : : "ONLY");
2755 michael@paquier.xyz 3168 : 0 : else if (HeadMatches("ANALYZE", "(*") &&
2755 michael@paquier.xyz 3169 [ + - ]:CBC 2 : !HeadMatches("ANALYZE", "(*)"))
3170 : : {
3171 : : /*
3172 : : * This fires if we're in an unfinished parenthesized option list.
3173 : : * get_previous_words treats a completed parenthesized option list as
3174 : : * one word, so the above test is correct.
3175 : : */
3176 [ - + - - ]: 2 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
1262 drowley@postgresql.o 3177 : 2 : COMPLETE_WITH("VERBOSE", "SKIP_LOCKED", "BUFFER_USAGE_LIMIT");
2677 fujii@postgresql.org 3178 [ # # ]:UBC 0 : else if (TailMatches("VERBOSE|SKIP_LOCKED"))
3179 : 0 : COMPLETE_WITH("ON", "OFF");
3180 : : }
270 msawada@postgresql.o 3181 :CBC 2 : else if (Matches("ANALYZE", "(*)"))
270 msawada@postgresql.o 3182 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_analyzables,
3183 : : "ONLY");
713 tgl@sss.pgh.pa.us 3184 : 0 : else if (Matches("ANALYZE", MatchAnyN, "("))
3185 : : /* "ANALYZE (" should be caught above, so assume we want columns */
1694 3186 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
2921 3187 : 0 : else if (HeadMatches("ANALYZE"))
1694 3188 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_analyzables);
3189 : :
3190 : : /* BEGIN */
2921 3191 : 0 : else if (Matches("BEGIN"))
3192 : 0 : COMPLETE_WITH("WORK", "TRANSACTION", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
3193 : : /* END, ABORT */
3194 : 0 : else if (Matches("END|ABORT"))
2737 peter@eisentraut.org 3195 : 0 : COMPLETE_WITH("AND", "WORK", "TRANSACTION");
3196 : : /* COMMIT */
2921 tgl@sss.pgh.pa.us 3197 : 0 : else if (Matches("COMMIT"))
2737 peter@eisentraut.org 3198 : 0 : COMPLETE_WITH("AND", "WORK", "TRANSACTION", "PREPARED");
3199 : : /* RELEASE SAVEPOINT */
2921 tgl@sss.pgh.pa.us 3200 : 0 : else if (Matches("RELEASE"))
3201 : 0 : COMPLETE_WITH("SAVEPOINT");
3202 : : /* ROLLBACK */
3203 : 0 : else if (Matches("ROLLBACK"))
2737 peter@eisentraut.org 3204 : 0 : COMPLETE_WITH("AND", "WORK", "TRANSACTION", "TO SAVEPOINT", "PREPARED");
3205 : 0 : else if (Matches("ABORT|END|COMMIT|ROLLBACK", "AND"))
3206 : 0 : COMPLETE_WITH("CHAIN");
3207 : : /* CALL */
2921 tgl@sss.pgh.pa.us 3208 : 0 : else if (Matches("CALL"))
1694 3209 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_procedures);
2921 3210 : 0 : else if (Matches("CALL", MatchAny))
3211 : 0 : COMPLETE_WITH("(");
3212 : : /* CHECKPOINT */
436 nathan@postgresql.or 3213 : 0 : else if (Matches("CHECKPOINT"))
3214 : 0 : COMPLETE_WITH("(");
3215 : 0 : else if (HeadMatches("CHECKPOINT", "(*") &&
3216 [ # # ]: 0 : !HeadMatches("CHECKPOINT", "(*)"))
3217 : : {
3218 : : /*
3219 : : * This fires if we're in an unfinished parenthesized option list.
3220 : : * get_previous_words treats a completed parenthesized option list as
3221 : : * one word, so the above test is correct.
3222 : : */
3223 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3224 : 0 : COMPLETE_WITH("MODE", "FLUSH_UNLOGGED");
3225 [ # # ]: 0 : else if (TailMatches("MODE"))
3226 : 0 : COMPLETE_WITH("FAST", "SPREAD");
118 dgustafsson@postgres 3227 [ # # ]: 0 : else if (TailMatches("FLUSH_UNLOGGED"))
3228 : 0 : COMPLETE_WITH("ON", "OFF");
3229 : : }
3230 : : /* CLOSE */
2075 fujii@postgresql.org 3231 : 0 : else if (Matches("CLOSE"))
1694 tgl@sss.pgh.pa.us 3232 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
3233 : : "ALL");
3234 : : /* CLUSTER */
2921 3235 : 0 : else if (Matches("CLUSTER"))
1694 3236 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
3237 : : "VERBOSE");
2117 michael@paquier.xyz 3238 : 0 : else if (Matches("CLUSTER", "VERBOSE") ||
3239 : : Matches("CLUSTER", "(*)"))
1694 tgl@sss.pgh.pa.us 3240 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_clusterables);
3241 : : /* If we have CLUSTER <sth>, then add "USING" */
2117 michael@paquier.xyz 3242 : 0 : else if (Matches("CLUSTER", MatchAnyExcept("VERBOSE|ON|(|(*)")))
2921 tgl@sss.pgh.pa.us 3243 : 0 : COMPLETE_WITH("USING");
3244 : : /* If we have CLUSTER VERBOSE <sth>, then add "USING" */
2117 michael@paquier.xyz 3245 : 0 : else if (Matches("CLUSTER", "VERBOSE|(*)", MatchAny))
2921 tgl@sss.pgh.pa.us 3246 : 0 : COMPLETE_WITH("USING");
3247 : : /* If we have CLUSTER <sth> USING, then add the index as well */
3248 : 0 : else if (Matches("CLUSTER", MatchAny, "USING") ||
3249 : : Matches("CLUSTER", "VERBOSE|(*)", MatchAny, "USING"))
3250 : : {
1694 3251 : 0 : set_completion_reference(prev2_wd);
3252 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
3253 : : }
2117 michael@paquier.xyz 3254 : 0 : else if (HeadMatches("CLUSTER", "(*") &&
3255 [ # # ]: 0 : !HeadMatches("CLUSTER", "(*)"))
3256 : : {
3257 : : /*
3258 : : * This fires if we're in an unfinished parenthesized option list.
3259 : : * get_previous_words treats a completed parenthesized option list as
3260 : : * one word, so the above test is correct.
3261 : : */
3262 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3263 : 0 : COMPLETE_WITH("VERBOSE");
3264 : : }
3265 : :
3266 : : /* COMMENT */
2921 tgl@sss.pgh.pa.us 3267 : 0 : else if (Matches("COMMENT"))
3268 : 0 : COMPLETE_WITH("ON");
3269 : 0 : else if (Matches("COMMENT", "ON"))
1780 michael@paquier.xyz 3270 : 0 : COMPLETE_WITH("ACCESS METHOD", "AGGREGATE", "CAST", "COLLATION",
3271 : : "COLUMN", "CONSTRAINT", "CONVERSION", "DATABASE",
3272 : : "DOMAIN", "EXTENSION", "EVENT TRIGGER",
3273 : : "FOREIGN DATA WRAPPER", "FOREIGN TABLE",
3274 : : "FUNCTION", "INDEX", "LANGUAGE", "LARGE OBJECT",
3275 : : "MATERIALIZED VIEW", "OPERATOR", "POLICY",
3276 : : "PROCEDURE", "PROCEDURAL LANGUAGE", "PUBLICATION", "ROLE",
3277 : : "ROUTINE", "RULE", "SCHEMA", "SEQUENCE", "SERVER",
3278 : : "STATISTICS", "SUBSCRIPTION", "TABLE",
3279 : : "TABLESPACE", "TEXT SEARCH", "TRANSFORM FOR",
3280 : : "TRIGGER", "TYPE", "VIEW");
2921 tgl@sss.pgh.pa.us 3281 : 0 : else if (Matches("COMMENT", "ON", "ACCESS", "METHOD"))
3757 alvherre@alvh.no-ip. 3282 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
2921 tgl@sss.pgh.pa.us 3283 : 0 : else if (Matches("COMMENT", "ON", "CONSTRAINT"))
5119 peter_e@gmx.net 3284 : 0 : COMPLETE_WITH_QUERY(Query_for_all_table_constraints);
2921 tgl@sss.pgh.pa.us 3285 : 0 : else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny))
3286 : 0 : COMPLETE_WITH("ON");
3287 : 0 : else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON"))
3288 : : {
1694 tgl@sss.pgh.pa.us 3289 :CBC 1 : set_completion_reference(prev2_wd);
3290 : 1 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables_for_constraint,
3291 : : "DOMAIN");
3292 : : }
1780 michael@paquier.xyz 3293 : 1 : else if (Matches("COMMENT", "ON", "CONSTRAINT", MatchAny, "ON", "DOMAIN"))
1694 tgl@sss.pgh.pa.us 3294 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
2921 3295 : 0 : else if (Matches("COMMENT", "ON", "EVENT", "TRIGGER"))
4542 rhaas@postgresql.org 3296 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
1780 michael@paquier.xyz 3297 : 0 : else if (Matches("COMMENT", "ON", "FOREIGN"))
3298 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
3299 : 0 : else if (Matches("COMMENT", "ON", "FOREIGN", "TABLE"))
1694 tgl@sss.pgh.pa.us 3300 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
1780 michael@paquier.xyz 3301 : 0 : else if (Matches("COMMENT", "ON", "MATERIALIZED", "VIEW"))
1694 tgl@sss.pgh.pa.us 3302 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
1780 michael@paquier.xyz 3303 : 0 : else if (Matches("COMMENT", "ON", "POLICY"))
3304 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_policies);
3305 : 0 : else if (Matches("COMMENT", "ON", "POLICY", MatchAny))
3306 : 0 : COMPLETE_WITH("ON");
3307 : 0 : else if (Matches("COMMENT", "ON", "POLICY", MatchAny, "ON"))
3308 : : {
1694 tgl@sss.pgh.pa.us 3309 : 0 : set_completion_reference(prev2_wd);
3310 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
3311 : : }
1780 michael@paquier.xyz 3312 : 0 : else if (Matches("COMMENT", "ON", "PROCEDURAL", "LANGUAGE"))
3313 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3314 : 0 : else if (Matches("COMMENT", "ON", "RULE", MatchAny))
3315 : 0 : COMPLETE_WITH("ON");
3316 : 0 : else if (Matches("COMMENT", "ON", "RULE", MatchAny, "ON"))
3317 : : {
1694 tgl@sss.pgh.pa.us 3318 : 0 : set_completion_reference(prev2_wd);
3319 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
3320 : : }
1780 michael@paquier.xyz 3321 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH"))
3322 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
3323 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "CONFIGURATION"))
1694 tgl@sss.pgh.pa.us 3324 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_configurations);
1780 michael@paquier.xyz 3325 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "DICTIONARY"))
1694 tgl@sss.pgh.pa.us 3326 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_dictionaries);
1780 michael@paquier.xyz 3327 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "PARSER"))
1694 tgl@sss.pgh.pa.us 3328 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_parsers);
1780 michael@paquier.xyz 3329 : 0 : else if (Matches("COMMENT", "ON", "TEXT", "SEARCH", "TEMPLATE"))
1694 tgl@sss.pgh.pa.us 3330 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_templates);
1780 michael@paquier.xyz 3331 : 0 : else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR"))
1694 tgl@sss.pgh.pa.us 3332 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
1780 michael@paquier.xyz 3333 : 0 : else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR", MatchAny))
3334 : 0 : COMPLETE_WITH("LANGUAGE");
3335 : 0 : else if (Matches("COMMENT", "ON", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
3336 : : {
1694 tgl@sss.pgh.pa.us 3337 : 0 : set_completion_reference(prev2_wd);
1780 michael@paquier.xyz 3338 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3339 : : }
3340 : 0 : else if (Matches("COMMENT", "ON", "TRIGGER", MatchAny))
3341 : 0 : COMPLETE_WITH("ON");
3342 : 0 : else if (Matches("COMMENT", "ON", "TRIGGER", MatchAny, "ON"))
3343 : : {
1694 tgl@sss.pgh.pa.us 3344 : 0 : set_completion_reference(prev2_wd);
3345 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
3346 : : }
2921 3347 : 0 : else if (Matches("COMMENT", "ON", MatchAny, MatchAnyExcept("IS")) ||
3348 : : Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAnyExcept("IS")) ||
3349 : : Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS")) ||
3350 : : Matches("COMMENT", "ON", MatchAny, MatchAny, MatchAny, MatchAny, MatchAnyExcept("IS")))
3351 : 0 : COMPLETE_WITH("IS");
3352 : :
3353 : : /* COPY */
3354 : :
3355 : : /*
3356 : : * If we have COPY, offer list of tables or "(" (Also cover the analogous
3357 : : * backslash command).
3358 : : */
3359 : 0 : else if (Matches("COPY|\\copy"))
447 fujii@postgresql.org 3360 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables_for_copy, "(");
3361 : : /* Complete COPY ( with legal query commands */
2921 tgl@sss.pgh.pa.us 3362 : 0 : else if (Matches("COPY|\\copy", "("))
661 peter@eisentraut.org 3363 : 0 : COMPLETE_WITH("SELECT", "TABLE", "VALUES", "INSERT INTO", "UPDATE", "DELETE FROM", "MERGE INTO", "WITH");
3364 : : /* Complete COPY <sth> */
2252 michael@paquier.xyz 3365 : 0 : else if (Matches("COPY|\\copy", MatchAny))
2921 tgl@sss.pgh.pa.us 3366 : 0 : COMPLETE_WITH("FROM", "TO");
3367 : : /* Complete COPY|\copy <sth> FROM|TO with filename or STDIN/STDOUT/PROGRAM */
320 msawada@postgresql.o 3368 : 0 : else if (Matches("COPY|\\copy", MatchAny, "FROM|TO"))
3369 : : {
256 msawada@postgresql.o 3370 [ + - ]:CBC 4 : if (HeadMatches("COPY"))
3371 : : {
3372 : : /* COPY requires quoted filename */
3373 [ + - ]: 4 : if (TailMatches("FROM"))
3374 : 4 : COMPLETE_WITH_FILES_PLUS("", true, "STDIN", "PROGRAM");
3375 : : else
256 msawada@postgresql.o 3376 :UBC 0 : COMPLETE_WITH_FILES_PLUS("", true, "STDOUT", "PROGRAM");
3377 : : }
3378 : : else
3379 : : {
3380 : : /* \copy supports pstdin and pstdout */
3381 [ # # ]: 0 : if (TailMatches("FROM"))
3382 : 0 : COMPLETE_WITH_FILES_PLUS("", false, "STDIN", "PSTDIN", "PROGRAM");
3383 : : else
3384 : 0 : COMPLETE_WITH_FILES_PLUS("", false, "STDOUT", "PSTDOUT", "PROGRAM");
3385 : : }
3386 : : }
3387 : :
3388 : : /* Complete COPY|\copy <sth> FROM|TO PROGRAM */
320 msawada@postgresql.o 3389 :CBC 4 : else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", "PROGRAM"))
320 msawada@postgresql.o 3390 :UBC 0 : COMPLETE_WITH_FILES("", HeadMatches("COPY")); /* COPY requires quoted
3391 : : * filename */
3392 : :
3393 : : /* Complete COPY <sth> TO [PROGRAM] <sth> */
3394 : 0 : else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAnyExcept("PROGRAM")) ||
3395 : : Matches("COPY|\\copy", MatchAny, "TO", "PROGRAM", MatchAny))
2252 michael@paquier.xyz 3396 : 0 : COMPLETE_WITH("WITH (");
3397 : :
3398 : : /* Complete COPY <sth> FROM [PROGRAM] <sth> */
320 msawada@postgresql.o 3399 : 0 : else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAnyExcept("PROGRAM")) ||
3400 : : Matches("COPY|\\copy", MatchAny, "FROM", "PROGRAM", MatchAny))
2252 michael@paquier.xyz 3401 : 0 : COMPLETE_WITH("WITH (", "WHERE");
3402 : :
3403 : : /* Complete COPY <sth> FROM|TO [PROGRAM] filename WITH ( */
272 msawada@postgresql.o 3404 : 0 : else if (HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", MatchAnyExcept("PROGRAM"), "WITH", "(*") ||
3405 : : HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", "PROGRAM", MatchAny, "WITH", "(*"))
3406 : : {
272 msawada@postgresql.o 3407 [ + - ]:CBC 1 : if (!HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", MatchAnyExcept("PROGRAM"), "WITH", "(*)") &&
3408 [ + - ]: 1 : !HeadMatches("COPY|\\copy", MatchAny, "FROM|TO", "PROGRAM", MatchAny, "WITH", "(*)"))
3409 : : {
3410 : : /*
3411 : : * This fires if we're in an unfinished parenthesized option list.
3412 : : * get_previous_words treats a completed parenthesized option list
3413 : : * as one word, so the above tests are correct.
3414 : : */
3415 : :
3416 [ - + - - ]: 1 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
3417 : : {
3418 [ + - ]: 2 : if (HeadMatches("COPY|\\copy", MatchAny, "FROM"))
3419 : 1 : COMPLETE_WITH(Copy_from_options);
3420 : : else
272 msawada@postgresql.o 3421 :UBC 0 : COMPLETE_WITH(Copy_to_options);
3422 : : }
3423 : :
3424 : : /* Complete COPY <sth> FROM|TO filename WITH (FORMAT */
3425 [ # # ]: 0 : else if (TailMatches("FORMAT"))
188 andrew@dunslane.net 3426 : 0 : COMPLETE_WITH("binary", "csv", "text", "json");
3427 : :
3428 : : /* Complete COPY <sth> FROM|TO filename WITH (FREEZE */
272 msawada@postgresql.o 3429 [ # # ]: 0 : else if (TailMatches("FREEZE"))
3430 : 0 : COMPLETE_WITH("true", "false");
3431 : :
3432 : : /* Complete COPY <sth> FROM|TO filename WITH (HEADER */
3433 [ # # ]: 0 : else if (TailMatches("HEADER"))
3434 : 0 : COMPLETE_WITH("true", "false", "MATCH");
3435 : :
3436 : : /* Complete COPY <sth> FROM filename WITH (ON_ERROR */
3437 [ # # ]: 0 : else if (TailMatches("ON_ERROR"))
201 peter@eisentraut.org 3438 : 0 : COMPLETE_WITH("stop", "ignore", "set_null");
3439 : :
3440 : : /* Complete COPY <sth> FROM filename WITH (LOG_VERBOSITY */
272 msawada@postgresql.o 3441 [ # # ]: 0 : else if (TailMatches("LOG_VERBOSITY"))
3442 : 0 : COMPLETE_WITH("silent", "default", "verbose");
3443 : : }
3444 : :
3445 : : /* A completed parenthesized option list should be caught below */
3446 : : }
3447 : :
3448 : : /* Complete COPY <sth> FROM [PROGRAM] <sth> WITH (<options>) */
320 msawada@postgresql.o 3449 :CBC 1 : else if (Matches("COPY|\\copy", MatchAny, "FROM", MatchAnyExcept("PROGRAM"), "WITH", MatchAny) ||
3450 : : Matches("COPY|\\copy", MatchAny, "FROM", "PROGRAM", MatchAny, "WITH", MatchAny))
2252 michael@paquier.xyz 3451 :UBC 0 : COMPLETE_WITH("WHERE");
3452 : :
3453 : : /* CREATE ACCESS METHOD */
3454 : : /* Complete "CREATE ACCESS METHOD <name>" */
2921 tgl@sss.pgh.pa.us 3455 : 0 : else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny))
3456 : 0 : COMPLETE_WITH("TYPE");
3457 : : /* Complete "CREATE ACCESS METHOD <name> TYPE" */
3458 : 0 : else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE"))
2666 michael@paquier.xyz 3459 : 0 : COMPLETE_WITH("INDEX", "TABLE");
3460 : : /* Complete "CREATE ACCESS METHOD <name> TYPE <type>" */
2921 tgl@sss.pgh.pa.us 3461 : 0 : else if (Matches("CREATE", "ACCESS", "METHOD", MatchAny, "TYPE", MatchAny))
3462 : 0 : COMPLETE_WITH("HANDLER");
3463 : :
3464 : : /* CREATE COLLATION */
2036 tmunro@postgresql.or 3465 : 0 : else if (Matches("CREATE", "COLLATION", MatchAny))
3466 : 0 : COMPLETE_WITH("(", "FROM");
3467 : 0 : else if (Matches("CREATE", "COLLATION", MatchAny, "FROM"))
1694 tgl@sss.pgh.pa.us 3468 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_collations);
2036 tmunro@postgresql.or 3469 : 0 : else if (HeadMatches("CREATE", "COLLATION", MatchAny, "(*"))
3470 : : {
3471 [ # # ]: 0 : if (TailMatches("(|*,"))
3472 : 0 : COMPLETE_WITH("LOCALE =", "LC_COLLATE =", "LC_CTYPE =",
3473 : : "PROVIDER =", "DETERMINISTIC =");
3474 [ # # ]: 0 : else if (TailMatches("PROVIDER", "="))
3475 : 0 : COMPLETE_WITH("libc", "icu");
3476 [ # # ]: 0 : else if (TailMatches("DETERMINISTIC", "="))
3477 : 0 : COMPLETE_WITH("true", "false");
3478 : : }
3479 : :
3480 : : /* CREATE DATABASE */
2921 tgl@sss.pgh.pa.us 3481 : 0 : else if (Matches("CREATE", "DATABASE", MatchAny))
3482 : 0 : COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
3483 : : "IS_TEMPLATE", "STRATEGY",
3484 : : "ALLOW_CONNECTIONS", "CONNECTION LIMIT",
3485 : : "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID",
3486 : : "LOCALE_PROVIDER", "ICU_LOCALE");
3487 : :
3488 : 0 : else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
6946 bruce@momjian.us 3489 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
1636 rhaas@postgresql.org 3490 : 0 : else if (Matches("CREATE", "DATABASE", MatchAny, "STRATEGY"))
3491 : 0 : COMPLETE_WITH("WAL_LOG", "FILE_COPY");
3492 : :
3493 : : /* CREATE DOMAIN */
9 tgl@sss.pgh.pa.us 3494 : 0 : else if (Matches("CREATE", "DOMAIN", MatchAny))
1766 michael@paquier.xyz 3495 : 0 : COMPLETE_WITH("AS");
9 tgl@sss.pgh.pa.us 3496 : 0 : else if (Matches("CREATE", "DOMAIN", MatchAny, "AS"))
1694 3497 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
9 3498 : 0 : else if (Matches("CREATE", "DOMAIN", MatchAny, "AS", MatchAny))
1766 michael@paquier.xyz 3499 : 0 : COMPLETE_WITH("COLLATE", "DEFAULT", "CONSTRAINT",
3500 : : "NOT NULL", "NULL", "CHECK (");
9 tgl@sss.pgh.pa.us 3501 : 0 : else if (Matches("CREATE", "DOMAIN", MatchAny, "COLLATE"))
1694 3502 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_collations);
3503 : :
3504 : : /* CREATE EXTENSION */
3505 : : /* Complete with available extensions rather than installed ones. */
2921 3506 : 0 : else if (Matches("CREATE", "EXTENSION"))
5703 3507 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions);
3508 : : /* CREATE EXTENSION <name> */
2921 3509 : 0 : else if (Matches("CREATE", "EXTENSION", MatchAny))
3510 : 0 : COMPLETE_WITH("WITH SCHEMA", "CASCADE", "VERSION");
3511 : : /* CREATE EXTENSION <name> VERSION */
3512 : 0 : else if (Matches("CREATE", "EXTENSION", MatchAny, "VERSION"))
3513 : : {
1694 3514 : 0 : set_completion_reference(prev2_wd);
1552 3515 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_available_extension_versions);
3516 : : }
3517 : :
3518 : : /* CREATE FOREIGN */
2921 3519 : 0 : else if (Matches("CREATE", "FOREIGN"))
3520 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
3521 : :
3522 : : /* CREATE FOREIGN DATA WRAPPER */
3523 : 0 : else if (Matches("CREATE", "FOREIGN", "DATA", "WRAPPER", MatchAny))
179 msawada@postgresql.o 3524 : 0 : COMPLETE_WITH("CONNECTION", "HANDLER", "OPTIONS", "VALIDATOR");
3525 : :
3526 : : /* CREATE FOREIGN TABLE */
1709 fujii@postgresql.org 3527 : 0 : else if (Matches("CREATE", "FOREIGN", "TABLE", MatchAny))
3528 : 0 : COMPLETE_WITH("(", "PARTITION OF");
3529 : :
3530 : : /* CREATE INDEX --- is allowed inside CREATE SCHEMA, so use TailMatches */
3531 : : /* First off we complete CREATE UNIQUE with "INDEX" */
2921 tgl@sss.pgh.pa.us 3532 : 0 : else if (TailMatches("CREATE", "UNIQUE"))
3533 : 0 : COMPLETE_WITH("INDEX");
3534 : :
3535 : : /*
3536 : : * If we have CREATE|UNIQUE INDEX, then add "ON", "CONCURRENTLY", and
3537 : : * existing indexes
3538 : : */
3539 : 0 : else if (TailMatches("CREATE|UNIQUE", "INDEX"))
1694 3540 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
3541 : : "ON", "CONCURRENTLY");
3542 : :
3543 : : /*
3544 : : * Complete ... INDEX|CONCURRENTLY [<name>] ON with a list of relations
3545 : : * that indexes can be created on
3546 : : */
2921 3547 : 0 : else if (TailMatches("INDEX|CONCURRENTLY", MatchAny, "ON") ||
3548 : : TailMatches("INDEX|CONCURRENTLY", "ON"))
1694 3549 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables);
3550 : :
3551 : : /*
3552 : : * Complete CREATE|UNIQUE INDEX CONCURRENTLY with "ON" and existing
3553 : : * indexes
3554 : : */
2921 3555 : 0 : else if (TailMatches("CREATE|UNIQUE", "INDEX", "CONCURRENTLY"))
1694 3556 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
3557 : : "ON");
3558 : : /* Complete CREATE|UNIQUE INDEX [CONCURRENTLY] <sth> with "ON" */
2921 3559 : 0 : else if (TailMatches("CREATE|UNIQUE", "INDEX", MatchAny) ||
3560 : : TailMatches("CREATE|UNIQUE", "INDEX", "CONCURRENTLY", MatchAny))
3561 : 0 : COMPLETE_WITH("ON");
3562 : :
3563 : : /*
3564 : : * Complete INDEX <name> ON <table> with a list of table columns (which
3565 : : * should really be in parens)
3566 : : */
3567 : 0 : else if (TailMatches("INDEX", MatchAny, "ON", MatchAny) ||
3568 : : TailMatches("INDEX|CONCURRENTLY", "ON", MatchAny))
3569 : 0 : COMPLETE_WITH("(", "USING");
3570 : 0 : else if (TailMatches("INDEX", MatchAny, "ON", MatchAny, "(") ||
3571 : : TailMatches("INDEX|CONCURRENTLY", "ON", MatchAny, "("))
1694 3572 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
3573 : : /* same if you put in USING */
2921 3574 : 0 : else if (TailMatches("ON", MatchAny, "USING", MatchAny, "("))
1694 3575 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
3576 : : /* Complete USING with an index method */
2921 3577 : 0 : else if (TailMatches("INDEX", MatchAny, MatchAny, "ON", MatchAny, "USING") ||
3578 : : TailMatches("INDEX", MatchAny, "ON", MatchAny, "USING") ||
3579 : : TailMatches("INDEX", "ON", MatchAny, "USING"))
2666 michael@paquier.xyz 3580 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_index_access_methods);
2921 tgl@sss.pgh.pa.us 3581 : 0 : else if (TailMatches("ON", MatchAny, "USING", MatchAny) &&
3582 : : !TailMatches("POLICY", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny) &&
3583 [ # # # # ]: 0 : !TailMatches("FOR", MatchAny, MatchAny, MatchAny))
3584 : 0 : COMPLETE_WITH("(");
3585 : :
3586 : : /* CREATE OR REPLACE */
2564 fujii@postgresql.org 3587 : 0 : else if (Matches("CREATE", "OR"))
3588 : 0 : COMPLETE_WITH("REPLACE");
3589 : :
3590 : : /* CREATE POLICY */
3591 : : /* Complete "CREATE POLICY <name> ON" */
2921 tgl@sss.pgh.pa.us 3592 : 0 : else if (Matches("CREATE", "POLICY", MatchAny))
3593 : 0 : COMPLETE_WITH("ON");
3594 : : /* Complete "CREATE POLICY <name> ON <table>" */
3595 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON"))
1694 3596 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3597 : : /* Complete "CREATE POLICY <name> ON <table> AS|FOR|TO|USING|WITH CHECK" */
2921 3598 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny))
3599 : 0 : COMPLETE_WITH("AS", "FOR", "TO", "USING (", "WITH CHECK (");
3600 : : /* CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE */
3601 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS"))
3602 : 0 : COMPLETE_WITH("PERMISSIVE", "RESTRICTIVE");
3603 : :
3604 : : /*
3605 : : * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE
3606 : : * FOR|TO|USING|WITH CHECK
3607 : : */
3608 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny))
3609 : 0 : COMPLETE_WITH("FOR", "TO", "USING", "WITH CHECK");
3610 : : /* CREATE POLICY <name> ON <table> FOR ALL|SELECT|INSERT|UPDATE|DELETE */
3611 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR"))
3612 : 0 : COMPLETE_WITH("ALL", "SELECT", "INSERT", "UPDATE", "DELETE");
3613 : : /* Complete "CREATE POLICY <name> ON <table> FOR INSERT TO|WITH CHECK" */
3614 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "INSERT"))
3615 : 0 : COMPLETE_WITH("TO", "WITH CHECK (");
3616 : : /* Complete "CREATE POLICY <name> ON <table> FOR SELECT|DELETE TO|USING" */
3617 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "SELECT|DELETE"))
3618 : 0 : COMPLETE_WITH("TO", "USING (");
3619 : : /* CREATE POLICY <name> ON <table> FOR ALL|UPDATE TO|USING|WITH CHECK */
3620 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "FOR", "ALL|UPDATE"))
3621 : 0 : COMPLETE_WITH("TO", "USING (", "WITH CHECK (");
3622 : : /* Complete "CREATE POLICY <name> ON <table> TO <role>" */
3623 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "TO"))
1694 3624 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3625 : : Keywords_for_list_of_grant_roles);
3626 : : /* Complete "CREATE POLICY <name> ON <table> USING (" */
2921 3627 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "USING"))
3628 : 0 : COMPLETE_WITH("(");
3629 : :
3630 : : /*
3631 : : * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3632 : : * ALL|SELECT|INSERT|UPDATE|DELETE
3633 : : */
3634 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR"))
3635 : 0 : COMPLETE_WITH("ALL", "SELECT", "INSERT", "UPDATE", "DELETE");
3636 : :
3637 : : /*
3638 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3639 : : * INSERT TO|WITH CHECK"
3640 : : */
3641 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "INSERT"))
3642 : 0 : COMPLETE_WITH("TO", "WITH CHECK (");
3643 : :
3644 : : /*
3645 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3646 : : * SELECT|DELETE TO|USING"
3647 : : */
3648 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "SELECT|DELETE"))
3649 : 0 : COMPLETE_WITH("TO", "USING (");
3650 : :
3651 : : /*
3652 : : * CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE FOR
3653 : : * ALL|UPDATE TO|USING|WITH CHECK
3654 : : */
3655 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "FOR", "ALL|UPDATE"))
3656 : 0 : COMPLETE_WITH("TO", "USING (", "WITH CHECK (");
3657 : :
3658 : : /*
3659 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE TO
3660 : : * <role>"
3661 : : */
3662 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "TO"))
1694 3663 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3664 : : Keywords_for_list_of_grant_roles);
3665 : :
3666 : : /*
3667 : : * Complete "CREATE POLICY <name> ON <table> AS PERMISSIVE|RESTRICTIVE
3668 : : * USING ("
3669 : : */
2921 3670 : 0 : else if (Matches("CREATE", "POLICY", MatchAny, "ON", MatchAny, "AS", MatchAny, "USING"))
3671 : 0 : COMPLETE_WITH("(");
3672 : :
3673 : :
3674 : : /* CREATE PUBLICATION */
3675 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny))
346 akapila@postgresql.o 3676 : 0 : COMPLETE_WITH("FOR TABLE", "FOR TABLES IN SCHEMA", "FOR ALL TABLES", "FOR ALL SEQUENCES", "WITH (");
2921 tgl@sss.pgh.pa.us 3677 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR"))
346 akapila@postgresql.o 3678 : 0 : COMPLETE_WITH("TABLE", "TABLES IN SCHEMA", "ALL TABLES", "ALL SEQUENCES");
1845 fujii@postgresql.org 3679 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL"))
346 akapila@postgresql.o 3680 : 0 : COMPLETE_WITH("TABLES", "SEQUENCES");
1627 tomas.vondra@postgre 3681 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES"))
173 akapila@postgresql.o 3682 : 0 : COMPLETE_WITH("EXCEPT ( TABLE", "WITH (");
200 3683 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT"))
173 3684 : 0 : COMPLETE_WITH("( TABLE");
3685 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "("))
3686 : 0 : COMPLETE_WITH("TABLE");
3687 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "(", "TABLE"))
200 3688 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
173 3689 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && ends_with(prev_wd, ','))
200 3690 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
173 3691 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "ALL", "TABLES", "EXCEPT", "(", "TABLE", MatchAnyN) && !ends_with(prev_wd, ','))
200 3692 : 0 : COMPLETE_WITH(")");
1451 alvherre@alvh.no-ip. 3693 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES"))
3694 : 0 : COMPLETE_WITH("IN SCHEMA");
1627 tomas.vondra@postgre 3695 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE", MatchAny) && !ends_with(prev_wd, ','))
1671 akapila@postgresql.o 3696 : 0 : COMPLETE_WITH("WHERE (", "WITH (");
3697 : : /* Complete "CREATE PUBLICATION <name> FOR TABLE" with "<table>, ..." */
1845 fujii@postgresql.org 3698 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLE"))
1694 tgl@sss.pgh.pa.us 3699 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3700 : :
3701 : : /*
3702 : : * "CREATE PUBLICATION <name> FOR TABLE <name> WHERE (" - complete with
3703 : : * table attributes
3704 : : */
713 3705 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE"))
1671 akapila@postgresql.o 3706 : 0 : COMPLETE_WITH("(");
713 tgl@sss.pgh.pa.us 3707 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "("))
1671 akapila@postgresql.o 3708 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
713 tgl@sss.pgh.pa.us 3709 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, MatchAnyN, "WHERE", "(*)"))
1671 akapila@postgresql.o 3710 : 0 : COMPLETE_WITH(" WITH (");
3711 : :
3712 : : /*
3713 : : * Complete "CREATE PUBLICATION <name> FOR TABLES IN SCHEMA <schema>, ..."
3714 : : */
1451 alvherre@alvh.no-ip. 3715 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES", "IN", "SCHEMA"))
1694 tgl@sss.pgh.pa.us 3716 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
3717 : : " AND nspname NOT LIKE E'pg\\\\_%%'",
3718 : : "CURRENT_SCHEMA");
1451 alvherre@alvh.no-ip. 3719 [ # # ]: 0 : else if (Matches("CREATE", "PUBLICATION", MatchAny, "FOR", "TABLES", "IN", "SCHEMA", MatchAny) && (!ends_with(prev_wd, ',')))
1789 akapila@postgresql.o 3720 : 0 : COMPLETE_WITH("WITH (");
3721 : : /* Complete "CREATE PUBLICATION <name> [...] WITH" */
713 tgl@sss.pgh.pa.us 3722 : 0 : else if (Matches("CREATE", "PUBLICATION", MatchAnyN, "WITH", "("))
682 akapila@postgresql.o 3723 : 0 : COMPLETE_WITH("publish", "publish_generated_columns", "publish_via_partition_root");
3724 : :
3725 : : /* CREATE RULE */
3726 : : /* Complete "CREATE [ OR REPLACE ] RULE <sth>" with "AS ON" */
2564 fujii@postgresql.org 3727 : 0 : else if (Matches("CREATE", "RULE", MatchAny) ||
3728 : : Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny))
2921 tgl@sss.pgh.pa.us 3729 : 0 : COMPLETE_WITH("AS ON");
3730 : : /* Complete "CREATE [ OR REPLACE ] RULE <sth> AS" with "ON" */
2564 fujii@postgresql.org 3731 : 0 : else if (Matches("CREATE", "RULE", MatchAny, "AS") ||
3732 : : Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny, "AS"))
2921 tgl@sss.pgh.pa.us 3733 : 0 : COMPLETE_WITH("ON");
3734 : :
3735 : : /*
3736 : : * Complete "CREATE [ OR REPLACE ] RULE <sth> AS ON" with
3737 : : * SELECT|UPDATE|INSERT|DELETE
3738 : : */
2564 fujii@postgresql.org 3739 : 0 : else if (Matches("CREATE", "RULE", MatchAny, "AS", "ON") ||
3740 : : Matches("CREATE", "OR", "REPLACE", "RULE", MatchAny, "AS", "ON"))
2921 tgl@sss.pgh.pa.us 3741 : 0 : COMPLETE_WITH("SELECT", "UPDATE", "INSERT", "DELETE");
3742 : : /* Complete "AS ON SELECT|UPDATE|INSERT|DELETE" with a "TO" */
3743 : 0 : else if (TailMatches("AS", "ON", "SELECT|UPDATE|INSERT|DELETE"))
3744 : 0 : COMPLETE_WITH("TO");
3745 : : /* Complete "AS ON <sth> TO" with a table name */
3746 : 0 : else if (TailMatches("AS", "ON", "SELECT|UPDATE|INSERT|DELETE", "TO"))
1694 3747 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3748 : :
3749 : : /* CREATE SCHEMA [ <name> ] [ AUTHORIZATION ] */
1178 michael@paquier.xyz 3750 : 0 : else if (Matches("CREATE", "SCHEMA"))
3751 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas,
3752 : : "AUTHORIZATION");
3753 : 0 : else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION") ||
3754 : : Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION"))
3755 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3756 : : Keywords_for_list_of_owner_roles);
3757 : 0 : else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION", MatchAny) ||
3758 : : Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION", MatchAny))
3759 : 0 : COMPLETE_WITH("CREATE", "GRANT");
3760 : 0 : else if (Matches("CREATE", "SCHEMA", MatchAny))
3761 : 0 : COMPLETE_WITH("AUTHORIZATION", "CREATE", "GRANT");
3762 : :
3763 : : /* CREATE SEQUENCE --- is allowed inside CREATE SCHEMA, so use TailMatches */
2921 tgl@sss.pgh.pa.us 3764 : 0 : else if (TailMatches("CREATE", "SEQUENCE", MatchAny) ||
3765 : : TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny))
1766 michael@paquier.xyz 3766 : 0 : COMPLETE_WITH("AS", "INCREMENT BY", "MINVALUE", "MAXVALUE", "NO",
3767 : : "CACHE", "CYCLE", "OWNED BY", "START WITH");
3768 : 0 : else if (TailMatches("CREATE", "SEQUENCE", MatchAny, "AS") ||
3769 : : TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "AS"))
3770 : 0 : COMPLETE_WITH_CS("smallint", "integer", "bigint");
2921 tgl@sss.pgh.pa.us 3771 : 0 : else if (TailMatches("CREATE", "SEQUENCE", MatchAny, "NO") ||
3772 : : TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny, "NO"))
3773 : 0 : COMPLETE_WITH("MINVALUE", "MAXVALUE", "CYCLE");
3774 : :
3775 : : /* CREATE SERVER <name> */
3776 : 0 : else if (Matches("CREATE", "SERVER", MatchAny))
3777 : 0 : COMPLETE_WITH("TYPE", "VERSION", "FOREIGN DATA WRAPPER");
3778 : :
3779 : : /* CREATE STATISTICS <name> */
3780 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny))
3781 : 0 : COMPLETE_WITH("(", "ON");
3782 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny, "("))
2734 tomas.vondra@postgre 3783 : 0 : COMPLETE_WITH("ndistinct", "dependencies", "mcv");
2853 3784 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny, "(*)"))
2921 tgl@sss.pgh.pa.us 3785 : 0 : COMPLETE_WITH("ON");
713 3786 : 0 : else if (Matches("CREATE", "STATISTICS", MatchAny, MatchAnyN, "FROM"))
1694 3787 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3788 : :
3789 : : /* CREATE TABLE --- is allowed inside CREATE SCHEMA, so use TailMatches */
3790 : : /* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */
2921 3791 : 0 : else if (TailMatches("CREATE", "TEMP|TEMPORARY"))
3792 : 0 : COMPLETE_WITH("SEQUENCE", "TABLE", "VIEW");
3793 : : /* Complete "CREATE UNLOGGED" with TABLE or SEQUENCE */
3794 : 0 : else if (TailMatches("CREATE", "UNLOGGED"))
783 nathan@postgresql.or 3795 : 0 : COMPLETE_WITH("TABLE", "SEQUENCE");
3796 : : /* Complete PARTITION BY with RANGE ( or LIST ( or ... */
2921 tgl@sss.pgh.pa.us 3797 : 0 : else if (TailMatches("PARTITION", "BY"))
3798 : 0 : COMPLETE_WITH("RANGE (", "LIST (", "HASH (");
3799 : : /* If we have xxx PARTITION OF, provide a list of partitioned tables */
3800 : 0 : else if (TailMatches("PARTITION", "OF"))
1694 3801 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables);
3802 : : /* Limited completion support for partition bound specification */
2921 3803 : 0 : else if (TailMatches("PARTITION", "OF", MatchAny))
3804 : 0 : COMPLETE_WITH("FOR VALUES", "DEFAULT");
3805 : : /* Complete CREATE TABLE <name> with '(', AS, OF or PARTITION OF */
2831 michael@paquier.xyz 3806 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny) ||
3807 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny))
1039 3808 : 0 : COMPLETE_WITH("(", "AS", "OF", "PARTITION OF");
3809 : : /* Complete CREATE TABLE <name> OF with list of composite types */
2831 3810 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "OF") ||
3811 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "OF"))
1694 tgl@sss.pgh.pa.us 3812 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_composite_datatypes);
3813 : : /* Complete CREATE TABLE <name> [ (...) ] AS with list of keywords */
1039 michael@paquier.xyz 3814 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "AS") ||
3815 : : TailMatches("CREATE", "TABLE", MatchAny, "(*)", "AS") ||
3816 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "AS") ||
3817 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "AS"))
3818 : 0 : COMPLETE_WITH("EXECUTE", "SELECT", "TABLE", "VALUES", "WITH");
3819 : : /* Complete CREATE TABLE name (...) with supported options */
466 3820 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)"))
1039 3821 : 0 : COMPLETE_WITH("AS", "INHERITS (", "PARTITION BY", "USING", "TABLESPACE", "WITH (");
466 3822 : 0 : else if (TailMatches("CREATE", "UNLOGGED", "TABLE", MatchAny, "(*)"))
3823 : 0 : COMPLETE_WITH("AS", "INHERITS (", "USING", "TABLESPACE", "WITH (");
2831 3824 : 0 : else if (TailMatches("CREATE", "TEMP|TEMPORARY", "TABLE", MatchAny, "(*)"))
643 tomas.vondra@postgre 3825 : 0 : COMPLETE_WITH("AS", "INHERITS (", "ON COMMIT", "PARTITION BY", "USING",
3826 : : "TABLESPACE", "WITH (");
3827 : : /* Complete CREATE TABLE (...) USING with table access methods */
2666 michael@paquier.xyz 3828 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)", "USING") ||
3829 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "USING"))
3830 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
3831 : : /* Complete CREATE TABLE (...) WITH with storage parameters */
2828 3832 : 0 : else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)", "WITH", "(") ||
3833 : : TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "(*)", "WITH", "("))
3834 : 0 : COMPLETE_WITH_LIST(table_storage_parameters);
3835 : : /* Complete CREATE TABLE ON COMMIT with actions */
2831 3836 : 0 : else if (TailMatches("CREATE", "TEMP|TEMPORARY", "TABLE", MatchAny, "(*)", "ON", "COMMIT"))
3837 : 0 : COMPLETE_WITH("DELETE ROWS", "DROP", "PRESERVE ROWS");
3838 : :
3839 : : /* CREATE TABLESPACE */
2921 tgl@sss.pgh.pa.us 3840 : 0 : else if (Matches("CREATE", "TABLESPACE", MatchAny))
3841 : 0 : COMPLETE_WITH("OWNER", "LOCATION");
3842 : : /* Complete CREATE TABLESPACE name OWNER name with "LOCATION" */
3843 : 0 : else if (Matches("CREATE", "TABLESPACE", MatchAny, "OWNER", MatchAny))
3844 : 0 : COMPLETE_WITH("LOCATION");
3845 : :
3846 : : /* CREATE TEXT SEARCH */
9 3847 : 0 : else if (Matches("CREATE", "TEXT", "SEARCH"))
2921 3848 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
9 3849 : 0 : else if (Matches("CREATE", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny))
2921 3850 : 0 : COMPLETE_WITH("(");
3851 : :
3852 : : /* CREATE TRANSFORM */
1766 michael@paquier.xyz 3853 : 0 : else if (Matches("CREATE", "TRANSFORM") ||
3854 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM"))
3855 : 0 : COMPLETE_WITH("FOR");
3856 : 0 : else if (Matches("CREATE", "TRANSFORM", "FOR") ||
3857 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR"))
1694 tgl@sss.pgh.pa.us 3858 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
1766 michael@paquier.xyz 3859 : 0 : else if (Matches("CREATE", "TRANSFORM", "FOR", MatchAny) ||
3860 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR", MatchAny))
3861 : 0 : COMPLETE_WITH("LANGUAGE");
3862 : 0 : else if (Matches("CREATE", "TRANSFORM", "FOR", MatchAny, "LANGUAGE") ||
3863 : : Matches("CREATE", "OR", "REPLACE", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
3864 : : {
1694 tgl@sss.pgh.pa.us 3865 : 0 : set_completion_reference(prev2_wd);
1766 michael@paquier.xyz 3866 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
3867 : : }
3868 : :
3869 : : /* CREATE SUBSCRIPTION */
2921 tgl@sss.pgh.pa.us 3870 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny))
179 msawada@postgresql.o 3871 : 0 : COMPLETE_WITH("CONNECTION", "SERVER");
3872 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "SERVER"))
3873 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
198 jdavis@postgresql.or 3874 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "SERVER", MatchAny))
3875 : 0 : COMPLETE_WITH("PUBLICATION");
2921 tgl@sss.pgh.pa.us 3876 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION", MatchAny))
3877 : 0 : COMPLETE_WITH("PUBLICATION");
198 jdavis@postgresql.or 3878 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "SERVER",
3879 : : MatchAny, "PUBLICATION"))
3880 : : {
3881 : : /* complete with nothing here as this refers to remote publications */
3882 : : }
2921 tgl@sss.pgh.pa.us 3883 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAny, "CONNECTION",
3884 : : MatchAny, "PUBLICATION"))
3885 : : {
3886 : : /* complete with nothing here as this refers to remote publications */
3887 : : }
713 3888 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAnyN, "PUBLICATION", MatchAny))
2921 3889 : 0 : COMPLETE_WITH("WITH (");
3890 : : /* Complete "CREATE SUBSCRIPTION <name> ... WITH ( <opt>" */
713 3891 : 0 : else if (Matches("CREATE", "SUBSCRIPTION", MatchAnyN, "WITH", "("))
80 akapila@postgresql.o 3892 :UNC 0 : COMPLETE_WITH("binary", "conflict_log_destination", "connect", "copy_data",
3893 : : "create_slot", "disable_on_error", "enabled", "failover",
3894 : : "max_retention_duration", "origin",
3895 : : "password_required", "retain_dead_tuples",
3896 : : "run_as_owner", "slot_name", "streaming",
3897 : : "synchronous_commit", "two_phase",
3898 : : "wal_receiver_timeout");
3899 : :
3900 : : /* CREATE TRIGGER --- is allowed inside CREATE SCHEMA, so use TailMatches */
3901 : :
3902 : : /*
3903 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> with BEFORE|AFTER|INSTEAD
3904 : : * OF.
3905 : : */
2132 michael@paquier.xyz 3906 :UBC 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny) ||
3907 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny))
2921 tgl@sss.pgh.pa.us 3908 : 0 : COMPLETE_WITH("BEFORE", "AFTER", "INSTEAD OF");
3909 : :
3910 : : /*
3911 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER with an
3912 : : * event.
3913 : : */
2132 michael@paquier.xyz 3914 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER") ||
3915 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER"))
2921 tgl@sss.pgh.pa.us 3916 : 0 : COMPLETE_WITH("INSERT", "DELETE", "UPDATE", "TRUNCATE");
3917 : : /* Complete CREATE [ OR REPLACE ] TRIGGER <name> INSTEAD OF with an event */
2132 michael@paquier.xyz 3918 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF") ||
3919 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF"))
2921 tgl@sss.pgh.pa.us 3920 : 0 : COMPLETE_WITH("INSERT", "DELETE", "UPDATE");
3921 : :
3922 : : /*
3923 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER sth with
3924 : : * OR|ON.
3925 : : */
3926 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) ||
3927 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny) ||
3928 : : TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny) ||
3929 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny))
3930 : 0 : COMPLETE_WITH("ON", "OR");
3931 : :
3932 : : /*
3933 : : * Complete CREATE [ OR REPLACE ] TRIGGER <name> BEFORE,AFTER event ON
3934 : : * with a list of tables. EXECUTE FUNCTION is the recommended grammar
3935 : : * instead of EXECUTE PROCEDURE in version 11 and upwards.
3936 : : */
2132 michael@paquier.xyz 3937 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON") ||
3938 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "BEFORE|AFTER", MatchAny, "ON"))
1694 tgl@sss.pgh.pa.us 3939 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
3940 : :
3941 : : /*
3942 : : * Complete CREATE [ OR REPLACE ] TRIGGER ... INSTEAD OF event ON with a
3943 : : * list of views.
3944 : : */
2132 michael@paquier.xyz 3945 : 0 : else if (TailMatches("CREATE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON") ||
3946 : : TailMatches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAny, "INSTEAD", "OF", MatchAny, "ON"))
1694 tgl@sss.pgh.pa.us 3947 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
713 3948 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3949 : : "ON", MatchAny) ||
3950 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3951 : : "ON", MatchAny))
3952 : : {
2886 michael@paquier.xyz 3953 [ # # ]: 0 : if (pset.sversion >= 110000)
3954 : 0 : COMPLETE_WITH("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY",
3955 : : "REFERENCING", "FOR", "WHEN (", "EXECUTE FUNCTION");
3956 : : else
3957 : 0 : COMPLETE_WITH("NOT DEFERRABLE", "DEFERRABLE", "INITIALLY",
3958 : : "REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE");
3959 : : }
713 tgl@sss.pgh.pa.us 3960 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3961 : : "DEFERRABLE") ||
3962 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3963 : : "DEFERRABLE") ||
3964 : : Matches("CREATE", "TRIGGER", MatchAnyN,
3965 : : "INITIALLY", "IMMEDIATE|DEFERRED") ||
3966 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3967 : : "INITIALLY", "IMMEDIATE|DEFERRED"))
3968 : : {
2886 michael@paquier.xyz 3969 [ # # ]: 0 : if (pset.sversion >= 110000)
3970 : 0 : COMPLETE_WITH("REFERENCING", "FOR", "WHEN (", "EXECUTE FUNCTION");
3971 : : else
3972 : 0 : COMPLETE_WITH("REFERENCING", "FOR", "WHEN (", "EXECUTE PROCEDURE");
3973 : : }
713 tgl@sss.pgh.pa.us 3974 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3975 : : "REFERENCING") ||
3976 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3977 : : "REFERENCING"))
2921 3978 : 0 : COMPLETE_WITH("OLD TABLE", "NEW TABLE");
713 3979 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3980 : : "OLD|NEW", "TABLE") ||
3981 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3982 : : "OLD|NEW", "TABLE"))
2921 3983 : 0 : COMPLETE_WITH("AS");
713 3984 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3985 : : "REFERENCING", "OLD", "TABLE", "AS", MatchAny) ||
3986 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3987 : : "REFERENCING", "OLD", "TABLE", "AS", MatchAny) ||
3988 : : Matches("CREATE", "TRIGGER", MatchAnyN,
3989 : : "REFERENCING", "OLD", "TABLE", MatchAny) ||
3990 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
3991 : : "REFERENCING", "OLD", "TABLE", MatchAny))
3992 : : {
2886 michael@paquier.xyz 3993 [ # # ]: 0 : if (pset.sversion >= 110000)
3994 : 0 : COMPLETE_WITH("NEW TABLE", "FOR", "WHEN (", "EXECUTE FUNCTION");
3995 : : else
3996 : 0 : COMPLETE_WITH("NEW TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE");
3997 : : }
713 tgl@sss.pgh.pa.us 3998 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
3999 : : "REFERENCING", "NEW", "TABLE", "AS", MatchAny) ||
4000 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4001 : : "REFERENCING", "NEW", "TABLE", "AS", MatchAny) ||
4002 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4003 : : "REFERENCING", "NEW", "TABLE", MatchAny) ||
4004 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4005 : : "REFERENCING", "NEW", "TABLE", MatchAny))
4006 : : {
2886 michael@paquier.xyz 4007 [ # # ]: 0 : if (pset.sversion >= 110000)
4008 : 0 : COMPLETE_WITH("OLD TABLE", "FOR", "WHEN (", "EXECUTE FUNCTION");
4009 : : else
4010 : 0 : COMPLETE_WITH("OLD TABLE", "FOR", "WHEN (", "EXECUTE PROCEDURE");
4011 : : }
713 tgl@sss.pgh.pa.us 4012 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4013 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4014 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4015 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4016 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4017 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4018 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4019 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", "AS", MatchAny) ||
4020 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4021 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
4022 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4023 : : "REFERENCING", "OLD|NEW", "TABLE", "AS", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
4024 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4025 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny) ||
4026 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4027 : : "REFERENCING", "OLD|NEW", "TABLE", MatchAny, "OLD|NEW", "TABLE", MatchAny))
4028 : : {
2886 michael@paquier.xyz 4029 [ # # ]: 0 : if (pset.sversion >= 110000)
4030 : 0 : COMPLETE_WITH("FOR", "WHEN (", "EXECUTE FUNCTION");
4031 : : else
4032 : 0 : COMPLETE_WITH("FOR", "WHEN (", "EXECUTE PROCEDURE");
4033 : : }
713 tgl@sss.pgh.pa.us 4034 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4035 : : "FOR") ||
4036 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4037 : : "FOR"))
2921 4038 : 0 : COMPLETE_WITH("EACH", "ROW", "STATEMENT");
713 4039 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4040 : : "FOR", "EACH") ||
4041 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4042 : : "FOR", "EACH"))
2921 4043 : 0 : COMPLETE_WITH("ROW", "STATEMENT");
713 4044 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4045 : : "FOR", "EACH", "ROW|STATEMENT") ||
4046 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4047 : : "FOR", "EACH", "ROW|STATEMENT") ||
4048 : : Matches("CREATE", "TRIGGER", MatchAnyN,
4049 : : "FOR", "ROW|STATEMENT") ||
4050 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4051 : : "FOR", "ROW|STATEMENT"))
4052 : : {
2886 michael@paquier.xyz 4053 [ # # ]: 0 : if (pset.sversion >= 110000)
4054 : 0 : COMPLETE_WITH("WHEN (", "EXECUTE FUNCTION");
4055 : : else
4056 : 0 : COMPLETE_WITH("WHEN (", "EXECUTE PROCEDURE");
4057 : : }
713 tgl@sss.pgh.pa.us 4058 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4059 : : "WHEN", "(*)") ||
4060 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4061 : : "WHEN", "(*)"))
4062 : : {
2886 michael@paquier.xyz 4063 [ # # ]: 0 : if (pset.sversion >= 110000)
4064 : 0 : COMPLETE_WITH("EXECUTE FUNCTION");
4065 : : else
4066 : 0 : COMPLETE_WITH("EXECUTE PROCEDURE");
4067 : : }
4068 : :
4069 : : /*
4070 : : * Complete CREATE [ OR REPLACE ] TRIGGER ... EXECUTE with
4071 : : * PROCEDURE|FUNCTION.
4072 : : */
713 tgl@sss.pgh.pa.us 4073 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4074 : : "EXECUTE") ||
4075 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4076 : : "EXECUTE"))
4077 : : {
2886 michael@paquier.xyz 4078 [ # # ]: 0 : if (pset.sversion >= 110000)
4079 : 0 : COMPLETE_WITH("FUNCTION");
4080 : : else
4081 : 0 : COMPLETE_WITH("PROCEDURE");
4082 : : }
713 tgl@sss.pgh.pa.us 4083 : 0 : else if (Matches("CREATE", "TRIGGER", MatchAnyN,
4084 : : "EXECUTE", "FUNCTION|PROCEDURE") ||
4085 : : Matches("CREATE", "OR", "REPLACE", "TRIGGER", MatchAnyN,
4086 : : "EXECUTE", "FUNCTION|PROCEDURE"))
1694 4087 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
4088 : :
4089 : : /* CREATE ROLE,USER,GROUP <name> */
2921 4090 : 0 : else if (Matches("CREATE", "ROLE|GROUP|USER", MatchAny) &&
4091 [ # # ]: 0 : !TailMatches("USER", "MAPPING"))
4092 : 0 : COMPLETE_WITH("ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB",
4093 : : "CREATEROLE", "ENCRYPTED PASSWORD", "IN", "INHERIT",
4094 : : "LOGIN", "NOBYPASSRLS",
4095 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
4096 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
4097 : : "REPLICATION", "ROLE", "SUPERUSER", "SYSID",
4098 : : "VALID UNTIL", "WITH");
4099 : :
4100 : : /* CREATE ROLE,USER,GROUP <name> WITH */
4101 : 0 : else if (Matches("CREATE", "ROLE|GROUP|USER", MatchAny, "WITH"))
4102 : : /* Similar to the above, but don't complete "WITH" again. */
4103 : 0 : COMPLETE_WITH("ADMIN", "BYPASSRLS", "CONNECTION LIMIT", "CREATEDB",
4104 : : "CREATEROLE", "ENCRYPTED PASSWORD", "IN", "INHERIT",
4105 : : "LOGIN", "NOBYPASSRLS",
4106 : : "NOCREATEDB", "NOCREATEROLE", "NOINHERIT",
4107 : : "NOLOGIN", "NOREPLICATION", "NOSUPERUSER", "PASSWORD",
4108 : : "REPLICATION", "ROLE", "SUPERUSER", "SYSID",
4109 : : "VALID UNTIL");
4110 : :
4111 : : /* complete CREATE ROLE,USER,GROUP <name> IN with ROLE,GROUP */
4112 : 0 : else if (Matches("CREATE", "ROLE|USER|GROUP", MatchAny, "IN"))
4113 : 0 : COMPLETE_WITH("GROUP", "ROLE");
4114 : :
4115 : : /* CREATE TYPE */
2626 tmunro@postgresql.or 4116 : 0 : else if (Matches("CREATE", "TYPE", MatchAny))
4117 : 0 : COMPLETE_WITH("(", "AS");
4118 : 0 : else if (Matches("CREATE", "TYPE", MatchAny, "AS"))
4119 : 0 : COMPLETE_WITH("ENUM", "RANGE", "(");
4120 : 0 : else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "("))
4121 : : {
4122 [ # # ]: 0 : if (TailMatches("(|*,", MatchAny))
1694 tgl@sss.pgh.pa.us 4123 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2626 tmunro@postgresql.or 4124 [ # # ]: 0 : else if (TailMatches("(|*,", MatchAny, MatchAnyExcept("*)")))
4125 : 0 : COMPLETE_WITH("COLLATE", ",", ")");
4126 : : }
4127 : 0 : else if (Matches("CREATE", "TYPE", MatchAny, "AS", "ENUM|RANGE"))
4128 : 0 : COMPLETE_WITH("(");
4129 : 0 : else if (HeadMatches("CREATE", "TYPE", MatchAny, "("))
4130 : : {
4131 [ # # ]: 0 : if (TailMatches("(|*,"))
4132 : 0 : COMPLETE_WITH("INPUT", "OUTPUT", "RECEIVE", "SEND",
4133 : : "TYPMOD_IN", "TYPMOD_OUT", "ANALYZE", "SUBSCRIPT",
4134 : : "INTERNALLENGTH", "PASSEDBYVALUE", "ALIGNMENT",
4135 : : "STORAGE", "LIKE", "CATEGORY", "PREFERRED",
4136 : : "DEFAULT", "ELEMENT", "DELIMITER",
4137 : : "COLLATABLE");
4138 [ # # ]: 0 : else if (TailMatches("(*|*,", MatchAnyExcept("*=")))
4139 : 0 : COMPLETE_WITH("=");
4140 [ # # ]: 0 : else if (TailMatches("=", MatchAnyExcept("*)")))
4141 : 0 : COMPLETE_WITH(",", ")");
4142 : : }
4143 : 0 : else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "RANGE", "("))
4144 : : {
4145 [ # # ]: 0 : if (TailMatches("(|*,"))
4146 : 0 : COMPLETE_WITH("SUBTYPE", "SUBTYPE_OPCLASS", "COLLATION",
4147 : : "CANONICAL", "SUBTYPE_DIFF",
4148 : : "MULTIRANGE_TYPE_NAME");
4149 [ # # ]: 0 : else if (TailMatches("(*|*,", MatchAnyExcept("*=")))
4150 : 0 : COMPLETE_WITH("=");
4151 [ # # ]: 0 : else if (TailMatches("=", MatchAnyExcept("*)")))
4152 : 0 : COMPLETE_WITH(",", ")");
4153 : : }
4154 : :
4155 : : /* CREATE VIEW --- is allowed inside CREATE SCHEMA, so use TailMatches */
4156 : : /* Complete CREATE [ OR REPLACE ] VIEW <name> with AS or WITH */
2564 fujii@postgresql.org 4157 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny) ||
4158 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny))
1027 dean.a.rasheed@gmail 4159 : 0 : COMPLETE_WITH("AS", "WITH");
4160 : : /* Complete "CREATE [ OR REPLACE ] VIEW <sth> AS with "SELECT" */
2564 fujii@postgresql.org 4161 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "AS") ||
4162 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "AS"))
2921 tgl@sss.pgh.pa.us 4163 : 0 : COMPLETE_WITH("SELECT");
4164 : : /* CREATE [ OR REPLACE ] VIEW <name> WITH ( yyy [= zzz] ) */
1027 dean.a.rasheed@gmail 4165 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH") ||
4166 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH"))
4167 : 0 : COMPLETE_WITH("(");
4168 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(") ||
4169 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "("))
4170 : 0 : COMPLETE_WITH_LIST(view_optional_parameters);
4171 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(", "check_option") ||
4172 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(", "check_option"))
4173 : 0 : COMPLETE_WITH("=");
4174 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(", "check_option", "=") ||
4175 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(", "check_option", "="))
4176 : 0 : COMPLETE_WITH("local", "cascaded");
4177 : : /* CREATE [ OR REPLACE ] VIEW <name> WITH ( ... ) AS */
4178 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(*)") ||
4179 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(*)"))
4180 : 0 : COMPLETE_WITH("AS");
4181 : : /* CREATE [ OR REPLACE ] VIEW <name> WITH ( ... ) AS SELECT */
4182 : 0 : else if (TailMatches("CREATE", "VIEW", MatchAny, "WITH", "(*)", "AS") ||
4183 : : TailMatches("CREATE", "OR", "REPLACE", "VIEW", MatchAny, "WITH", "(*)", "AS"))
4184 : 0 : COMPLETE_WITH("SELECT");
4185 : :
4186 : : /* CREATE MATERIALIZED VIEW */
2921 tgl@sss.pgh.pa.us 4187 : 0 : else if (Matches("CREATE", "MATERIALIZED"))
4188 : 0 : COMPLETE_WITH("VIEW");
4189 : : /* Complete CREATE MATERIALIZED VIEW <name> with AS or USING */
4190 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny))
643 tomas.vondra@postgre 4191 : 0 : COMPLETE_WITH("AS", "USING");
4192 : :
4193 : : /*
4194 : : * Complete CREATE MATERIALIZED VIEW <name> USING with list of access
4195 : : * methods
4196 : : */
4197 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING"))
4198 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_table_access_methods);
4199 : : /* Complete CREATE MATERIALIZED VIEW <name> USING <access method> with AS */
4200 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny))
2921 tgl@sss.pgh.pa.us 4201 : 0 : COMPLETE_WITH("AS");
4202 : :
4203 : : /*
4204 : : * Complete CREATE MATERIALIZED VIEW <name> [USING <access method> ] AS
4205 : : * with "SELECT"
4206 : : */
643 tomas.vondra@postgre 4207 : 0 : else if (Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "AS") ||
4208 : : Matches("CREATE", "MATERIALIZED", "VIEW", MatchAny, "USING", MatchAny, "AS"))
2921 tgl@sss.pgh.pa.us 4209 : 0 : COMPLETE_WITH("SELECT");
4210 : :
4211 : : /* CREATE EVENT TRIGGER */
4212 : 0 : else if (Matches("CREATE", "EVENT"))
4213 : 0 : COMPLETE_WITH("TRIGGER");
4214 : : /* Complete CREATE EVENT TRIGGER <name> with ON */
4215 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny))
4216 : 0 : COMPLETE_WITH("ON");
4217 : : /* Complete CREATE EVENT TRIGGER <name> ON with event_type */
4218 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny, "ON"))
1070 akorotkov@postgresql 4219 : 0 : COMPLETE_WITH("ddl_command_start", "ddl_command_end", "login",
4220 : : "sql_drop", "table_rewrite");
4221 : :
4222 : : /*
4223 : : * Complete CREATE EVENT TRIGGER <name> ON <event_type>. EXECUTE FUNCTION
4224 : : * is the recommended grammar instead of EXECUTE PROCEDURE in version 11
4225 : : * and upwards.
4226 : : */
2886 michael@paquier.xyz 4227 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAny, "ON", MatchAny))
4228 : : {
4229 [ # # ]: 0 : if (pset.sversion >= 110000)
4230 : 0 : COMPLETE_WITH("WHEN TAG IN (", "EXECUTE FUNCTION");
4231 : : else
4232 : 0 : COMPLETE_WITH("WHEN TAG IN (", "EXECUTE PROCEDURE");
4233 : : }
713 tgl@sss.pgh.pa.us 4234 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAnyN, "WHEN|AND", MatchAny, "IN", "(*)"))
4235 : : {
2886 michael@paquier.xyz 4236 [ # # ]: 0 : if (pset.sversion >= 110000)
4237 : 0 : COMPLETE_WITH("EXECUTE FUNCTION");
4238 : : else
4239 : 0 : COMPLETE_WITH("EXECUTE PROCEDURE");
4240 : : }
713 tgl@sss.pgh.pa.us 4241 : 0 : else if (Matches("CREATE", "EVENT", "TRIGGER", MatchAnyN, "EXECUTE", "FUNCTION|PROCEDURE"))
1694 4242 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
4243 : :
4244 : : /* DEALLOCATE */
2921 4245 : 0 : else if (Matches("DEALLOCATE"))
1694 4246 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_prepared_statements,
4247 : : "ALL");
4248 : :
4249 : : /* DECLARE */
4250 : :
4251 : : /*
4252 : : * Complete DECLARE <name> with one of BINARY, ASENSITIVE, INSENSITIVE,
4253 : : * SCROLL, NO SCROLL, and CURSOR.
4254 : : */
2921 4255 : 0 : else if (Matches("DECLARE", MatchAny))
1992 peter@eisentraut.org 4256 : 0 : COMPLETE_WITH("BINARY", "ASENSITIVE", "INSENSITIVE", "SCROLL", "NO SCROLL",
4257 : : "CURSOR");
4258 : :
4259 : : /*
4260 : : * Complete DECLARE ... <option> with other options. The PostgreSQL parser
4261 : : * allows DECLARE options to be specified in any order. But the
4262 : : * tab-completion follows the ordering of them that the SQL standard
4263 : : * provides, like the syntax of DECLARE command in the documentation
4264 : : * indicates.
4265 : : */
713 tgl@sss.pgh.pa.us 4266 : 0 : else if (Matches("DECLARE", MatchAnyN, "BINARY"))
1867 michael@paquier.xyz 4267 : 0 : COMPLETE_WITH("ASENSITIVE", "INSENSITIVE", "SCROLL", "NO SCROLL", "CURSOR");
713 tgl@sss.pgh.pa.us 4268 : 0 : else if (Matches("DECLARE", MatchAnyN, "ASENSITIVE|INSENSITIVE"))
2075 fujii@postgresql.org 4269 : 0 : COMPLETE_WITH("SCROLL", "NO SCROLL", "CURSOR");
713 tgl@sss.pgh.pa.us 4270 : 0 : else if (Matches("DECLARE", MatchAnyN, "SCROLL"))
2075 fujii@postgresql.org 4271 : 0 : COMPLETE_WITH("CURSOR");
4272 : : /* Complete DECLARE ... [options] NO with SCROLL */
713 tgl@sss.pgh.pa.us 4273 : 0 : else if (Matches("DECLARE", MatchAnyN, "NO"))
2075 fujii@postgresql.org 4274 : 0 : COMPLETE_WITH("SCROLL");
4275 : :
4276 : : /*
4277 : : * Complete DECLARE ... CURSOR with one of WITH HOLD, WITHOUT HOLD, and
4278 : : * FOR
4279 : : */
713 tgl@sss.pgh.pa.us 4280 : 0 : else if (Matches("DECLARE", MatchAnyN, "CURSOR"))
2921 4281 : 0 : COMPLETE_WITH("WITH HOLD", "WITHOUT HOLD", "FOR");
4282 : : /* Complete DECLARE ... CURSOR WITH|WITHOUT with HOLD */
713 4283 : 0 : else if (Matches("DECLARE", MatchAnyN, "CURSOR", "WITH|WITHOUT"))
2075 fujii@postgresql.org 4284 : 0 : COMPLETE_WITH("HOLD");
4285 : : /* Complete DECLARE ... CURSOR WITH|WITHOUT HOLD with FOR */
713 tgl@sss.pgh.pa.us 4286 : 0 : else if (Matches("DECLARE", MatchAnyN, "CURSOR", "WITH|WITHOUT", "HOLD"))
2075 fujii@postgresql.org 4287 : 0 : COMPLETE_WITH("FOR");
4288 : :
4289 : : /* DELETE --- can be inside EXPLAIN, RULE, etc */
4290 : : /* Complete DELETE with "FROM" */
2921 tgl@sss.pgh.pa.us 4291 : 0 : else if (Matches("DELETE"))
4292 : 0 : COMPLETE_WITH("FROM");
4293 : : /* Complete DELETE FROM with a list of tables */
4294 : 0 : else if (TailMatches("DELETE", "FROM"))
1694 4295 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
4296 : : /* Complete DELETE FROM <table> */
2921 4297 : 0 : else if (TailMatches("DELETE", "FROM", MatchAny))
5 peter@eisentraut.org 4298 : 0 : COMPLETE_WITH("USING", "WHERE");
4299 : : /* Complete DELETE FROM <table> USING with relations supporting SELECT */
202 fujii@postgresql.org 4300 : 0 : else if (TailMatches("DELETE", "FROM", MatchAny, "USING"))
4301 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
4302 : :
4303 : : /* DISCARD */
2921 tgl@sss.pgh.pa.us 4304 : 0 : else if (Matches("DISCARD"))
4305 : 0 : COMPLETE_WITH("ALL", "PLANS", "SEQUENCES", "TEMP");
4306 : :
4307 : : /* DO */
4308 : 0 : else if (Matches("DO"))
4309 : 0 : COMPLETE_WITH("LANGUAGE");
4310 : :
4311 : : /* DROP */
4312 : : /* Complete DROP object with CASCADE / RESTRICT */
4313 : 0 : else if (Matches("DROP",
4314 : : "COLLATION|CONVERSION|DOMAIN|EXTENSION|LANGUAGE|PUBLICATION|SCHEMA|SEQUENCE|SERVER|SUBSCRIPTION|STATISTICS|TABLE|TYPE|VIEW",
4315 : : MatchAny) ||
4316 : : Matches("DROP", "ACCESS", "METHOD", MatchAny) ||
4317 : : Matches("DROP", "EVENT", "TRIGGER", MatchAny) ||
4318 : : Matches("DROP", "FOREIGN", "DATA", "WRAPPER", MatchAny) ||
4319 : : Matches("DROP", "FOREIGN", "TABLE", MatchAny) ||
4320 : : Matches("DROP", "TEXT", "SEARCH", "CONFIGURATION|DICTIONARY|PARSER|TEMPLATE", MatchAny))
2921 tgl@sss.pgh.pa.us 4321 :CBC 1 : COMPLETE_WITH("CASCADE", "RESTRICT");
713 4322 : 1 : else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny, MatchAny) &&
713 tgl@sss.pgh.pa.us 4323 [ # # ]:UBC 0 : ends_with(prev_wd, ')'))
4324 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4325 : :
4326 : : /* help completing some of the variants */
2921 4327 : 0 : else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny))
4328 : 0 : COMPLETE_WITH("(");
4329 : 0 : else if (Matches("DROP", "AGGREGATE|FUNCTION|PROCEDURE|ROUTINE", MatchAny, "("))
3928 4330 : 0 : COMPLETE_WITH_FUNCTION_ARG(prev2_wd);
2921 4331 : 0 : else if (Matches("DROP", "FOREIGN"))
4332 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
2504 akapila@postgresql.o 4333 : 0 : else if (Matches("DROP", "DATABASE", MatchAny))
4334 : 0 : COMPLETE_WITH("WITH (");
4335 [ # # ]: 0 : else if (HeadMatches("DROP", "DATABASE") && (ends_with(prev_wd, '(')))
4336 : 0 : COMPLETE_WITH("FORCE");
4337 : :
4338 : : /* DROP INDEX */
2921 tgl@sss.pgh.pa.us 4339 : 0 : else if (Matches("DROP", "INDEX"))
1694 4340 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
4341 : : "CONCURRENTLY");
2921 4342 : 0 : else if (Matches("DROP", "INDEX", "CONCURRENTLY"))
1694 4343 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
2921 4344 : 0 : else if (Matches("DROP", "INDEX", MatchAny))
4345 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4346 : 0 : else if (Matches("DROP", "INDEX", "CONCURRENTLY", MatchAny))
4347 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4348 : :
4349 : : /* DROP MATERIALIZED VIEW */
4350 : 0 : else if (Matches("DROP", "MATERIALIZED"))
4351 : 0 : COMPLETE_WITH("VIEW");
4352 : 0 : else if (Matches("DROP", "MATERIALIZED", "VIEW"))
1694 4353 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
1754 michael@paquier.xyz 4354 : 0 : else if (Matches("DROP", "MATERIALIZED", "VIEW", MatchAny))
4355 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4356 : :
4357 : : /* DROP OWNED BY */
2921 tgl@sss.pgh.pa.us 4358 : 0 : else if (Matches("DROP", "OWNED"))
4359 : 0 : COMPLETE_WITH("BY");
4360 : 0 : else if (Matches("DROP", "OWNED", "BY"))
7476 alvherre@alvh.no-ip. 4361 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
1754 michael@paquier.xyz 4362 : 0 : else if (Matches("DROP", "OWNED", "BY", MatchAny))
4363 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4364 : :
4365 : : /* DROP TEXT SEARCH */
2921 tgl@sss.pgh.pa.us 4366 : 0 : else if (Matches("DROP", "TEXT", "SEARCH"))
4367 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
4368 : :
4369 : : /* DROP TRIGGER */
4370 : 0 : else if (Matches("DROP", "TRIGGER", MatchAny))
4371 : 0 : COMPLETE_WITH("ON");
4372 : 0 : else if (Matches("DROP", "TRIGGER", MatchAny, "ON"))
4373 : : {
1694 4374 : 0 : set_completion_reference(prev2_wd);
4375 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_trigger);
4376 : : }
2921 4377 : 0 : else if (Matches("DROP", "TRIGGER", MatchAny, "ON", MatchAny))
4378 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4379 : :
4380 : : /* DROP ACCESS METHOD */
4381 : 0 : else if (Matches("DROP", "ACCESS"))
4382 : 0 : COMPLETE_WITH("METHOD");
4383 : 0 : else if (Matches("DROP", "ACCESS", "METHOD"))
3757 alvherre@alvh.no-ip. 4384 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
4385 : :
4386 : : /* DROP EVENT TRIGGER */
2921 tgl@sss.pgh.pa.us 4387 : 0 : else if (Matches("DROP", "EVENT"))
4388 : 0 : COMPLETE_WITH("TRIGGER");
4389 : 0 : else if (Matches("DROP", "EVENT", "TRIGGER"))
4542 rhaas@postgresql.org 4390 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
4391 : :
4392 : : /* DROP POLICY <name> */
2921 tgl@sss.pgh.pa.us 4393 : 0 : else if (Matches("DROP", "POLICY"))
4080 alvherre@alvh.no-ip. 4394 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_policies);
4395 : : /* DROP POLICY <name> ON */
2921 tgl@sss.pgh.pa.us 4396 : 0 : else if (Matches("DROP", "POLICY", MatchAny))
4397 : 0 : COMPLETE_WITH("ON");
4398 : : /* DROP POLICY <name> ON <table> */
4399 : 0 : else if (Matches("DROP", "POLICY", MatchAny, "ON"))
4400 : : {
1694 4401 : 0 : set_completion_reference(prev2_wd);
4402 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_policy);
4403 : : }
1754 michael@paquier.xyz 4404 : 0 : else if (Matches("DROP", "POLICY", MatchAny, "ON", MatchAny))
4405 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4406 : :
4407 : : /* DROP RULE */
2921 tgl@sss.pgh.pa.us 4408 : 0 : else if (Matches("DROP", "RULE", MatchAny))
4409 : 0 : COMPLETE_WITH("ON");
4410 : 0 : else if (Matches("DROP", "RULE", MatchAny, "ON"))
4411 : : {
1694 4412 : 0 : set_completion_reference(prev2_wd);
4413 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables_for_rule);
4414 : : }
2921 4415 : 0 : else if (Matches("DROP", "RULE", MatchAny, "ON", MatchAny))
4416 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4417 : :
4418 : : /* DROP TRANSFORM */
1754 michael@paquier.xyz 4419 : 0 : else if (Matches("DROP", "TRANSFORM"))
4420 : 0 : COMPLETE_WITH("FOR");
4421 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR"))
1694 tgl@sss.pgh.pa.us 4422 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
1754 michael@paquier.xyz 4423 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny))
4424 : 0 : COMPLETE_WITH("LANGUAGE");
4425 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny, "LANGUAGE"))
4426 : : {
1694 tgl@sss.pgh.pa.us 4427 : 0 : set_completion_reference(prev2_wd);
1754 michael@paquier.xyz 4428 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
4429 : : }
4430 : 0 : else if (Matches("DROP", "TRANSFORM", "FOR", MatchAny, "LANGUAGE", MatchAny))
4431 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
4432 : :
4433 : : /* EXECUTE */
2921 tgl@sss.pgh.pa.us 4434 : 0 : else if (Matches("EXECUTE"))
5446 4435 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements);
4436 : :
4437 : : /*
4438 : : * EXPLAIN [ ( option [, ...] ) ] statement
4439 : : * EXPLAIN [ ANALYZE ] [ VERBOSE ] statement
4440 : : */
2921 4441 : 0 : else if (Matches("EXPLAIN"))
1956 michael@paquier.xyz 4442 : 0 : COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4443 : : "MERGE INTO", "EXECUTE", "ANALYZE", "VERBOSE");
2921 tgl@sss.pgh.pa.us 4444 : 0 : else if (HeadMatches("EXPLAIN", "(*") &&
4445 [ # # ]: 0 : !HeadMatches("EXPLAIN", "(*)"))
4446 : : {
4447 : : /*
4448 : : * This fires if we're in an unfinished parenthesized option list.
4449 : : * get_previous_words treats a completed parenthesized option list as
4450 : : * one word, so the above test is correct.
4451 : : */
4452 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
1276 4453 : 0 : COMPLETE_WITH("ANALYZE", "VERBOSE", "COSTS", "SETTINGS", "GENERIC_PLAN",
4454 : : "BUFFERS", "SERIALIZE", "WAL", "TIMING", "SUMMARY",
4455 : : "MEMORY", "IO", "FORMAT");
120 fujii@postgresql.org 4456 [ # # ]: 0 : else if (TailMatches("ANALYZE|VERBOSE|COSTS|SETTINGS|GENERIC_PLAN|BUFFERS|WAL|TIMING|SUMMARY|MEMORY|IO"))
2921 tgl@sss.pgh.pa.us 4457 : 0 : COMPLETE_WITH("ON", "OFF");
872 michael@paquier.xyz 4458 [ # # ]: 0 : else if (TailMatches("SERIALIZE"))
4459 : 0 : COMPLETE_WITH("TEXT", "NONE", "BINARY");
2921 tgl@sss.pgh.pa.us 4460 [ # # ]: 0 : else if (TailMatches("FORMAT"))
4461 : 0 : COMPLETE_WITH("TEXT", "XML", "JSON", "YAML");
4462 : : }
4463 : 0 : else if (Matches("EXPLAIN", "ANALYZE"))
1956 michael@paquier.xyz 4464 : 0 : COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4465 : : "MERGE INTO", "EXECUTE", "VERBOSE");
2921 tgl@sss.pgh.pa.us 4466 : 0 : else if (Matches("EXPLAIN", "(*)") ||
4467 : : Matches("EXPLAIN", "VERBOSE") ||
4468 : : Matches("EXPLAIN", "ANALYZE", "VERBOSE"))
1852 michael@paquier.xyz 4469 : 0 : COMPLETE_WITH("SELECT", "INSERT INTO", "DELETE FROM", "UPDATE", "DECLARE",
4470 : : "MERGE INTO", "EXECUTE");
4471 : :
4472 : : /* FETCH && MOVE */
4473 : :
4474 : : /*
4475 : : * Complete FETCH with one of ABSOLUTE, BACKWARD, FORWARD, RELATIVE, ALL,
4476 : : * NEXT, PRIOR, FIRST, LAST, FROM, IN, and a list of cursors
4477 : : */
2921 tgl@sss.pgh.pa.us 4478 : 0 : else if (Matches("FETCH|MOVE"))
1694 4479 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4480 : : "ABSOLUTE",
4481 : : "BACKWARD",
4482 : : "FORWARD",
4483 : : "RELATIVE",
4484 : : "ALL",
4485 : : "NEXT",
4486 : : "PRIOR",
4487 : : "FIRST",
4488 : : "LAST",
4489 : : "FROM",
4490 : : "IN");
4491 : :
4492 : : /*
4493 : : * Complete FETCH BACKWARD or FORWARD with one of ALL, FROM, IN, and a
4494 : : * list of cursors
4495 : : */
2166 fujii@postgresql.org 4496 : 0 : else if (Matches("FETCH|MOVE", "BACKWARD|FORWARD"))
1694 tgl@sss.pgh.pa.us 4497 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4498 : : "ALL",
4499 : : "FROM",
4500 : : "IN");
4501 : :
4502 : : /*
4503 : : * Complete FETCH <direction> with "FROM" or "IN". These are equivalent,
4504 : : * but we may as well tab-complete both: perhaps some users prefer one
4505 : : * variant or the other.
4506 : : */
2166 fujii@postgresql.org 4507 : 0 : else if (Matches("FETCH|MOVE", "ABSOLUTE|BACKWARD|FORWARD|RELATIVE",
4508 : : MatchAnyExcept("FROM|IN")) ||
4509 : : Matches("FETCH|MOVE", "ALL|NEXT|PRIOR|FIRST|LAST"))
1694 tgl@sss.pgh.pa.us 4510 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_cursors,
4511 : : "FROM",
4512 : : "IN");
4513 : : /* Complete FETCH <direction> "FROM" or "IN" with a list of cursors */
713 4514 : 0 : else if (Matches("FETCH|MOVE", MatchAnyN, "FROM|IN"))
2075 fujii@postgresql.org 4515 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_cursors);
4516 : :
4517 : : /* FOREIGN DATA WRAPPER */
4518 : : /* applies in ALTER/DROP FDW and in CREATE SERVER */
2921 tgl@sss.pgh.pa.us 4519 : 0 : else if (TailMatches("FOREIGN", "DATA", "WRAPPER") &&
4520 [ # # ]: 0 : !TailMatches("CREATE", MatchAny, MatchAny, MatchAny))
6484 peter_e@gmx.net 4521 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
4522 : : /* applies in CREATE SERVER */
713 tgl@sss.pgh.pa.us 4523 : 0 : else if (Matches("CREATE", "SERVER", MatchAnyN, "FOREIGN", "DATA", "WRAPPER", MatchAny))
2921 4524 : 0 : COMPLETE_WITH("OPTIONS");
4525 : :
4526 : : /* FOREIGN TABLE */
4527 : 0 : else if (TailMatches("FOREIGN", "TABLE") &&
4528 [ # # ]: 0 : !TailMatches("CREATE", MatchAny, MatchAny))
1694 4529 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
4530 : :
4531 : : /* FOREIGN SERVER */
2921 4532 : 0 : else if (TailMatches("FOREIGN", "SERVER"))
4029 fujii@postgresql.org 4533 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
4534 : :
4535 : : /*
4536 : : * GRANT and REVOKE are allowed inside CREATE SCHEMA and
4537 : : * ALTER DEFAULT PRIVILEGES, so use TailMatches
4538 : : */
4539 : : /* Complete GRANT/REVOKE with a list of roles and privileges */
1628 tgl@sss.pgh.pa.us 4540 : 0 : else if (TailMatches("GRANT|REVOKE") ||
4541 : : TailMatches("REVOKE", "ADMIN|GRANT|INHERIT|SET", "OPTION", "FOR"))
4542 : : {
4543 : : /*
4544 : : * With ALTER DEFAULT PRIVILEGES, restrict completion to grantable
4545 : : * privileges (can't grant roles)
4546 : : */
2921 4547 [ # # ]: 0 : if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
4548 : : {
895 msawada@postgresql.o 4549 [ # # # # ]: 0 : if (TailMatches("GRANT") ||
4550 : 0 : TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
4551 : 0 : COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
4552 : : "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
4553 : : "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL");
4554 [ # # ]: 0 : else if (TailMatches("REVOKE"))
4555 : 0 : COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
4556 : : "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
4557 : : "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL",
4558 : : "GRANT OPTION FOR");
4559 : : }
1402 michael@paquier.xyz 4560 [ # # ]: 0 : else if (TailMatches("GRANT"))
1694 tgl@sss.pgh.pa.us 4561 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4562 : : Privilege_options_of_grant_and_revoke);
1402 michael@paquier.xyz 4563 [ # # ]: 0 : else if (TailMatches("REVOKE"))
4564 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4565 : : Privilege_options_of_grant_and_revoke,
4566 : : "GRANT OPTION FOR",
4567 : : "ADMIN OPTION FOR",
4568 : : "INHERIT OPTION FOR",
4569 : : "SET OPTION FOR");
4570 [ # # ]: 0 : else if (TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
4571 : 0 : COMPLETE_WITH(Privilege_options_of_grant_and_revoke);
1378 4572 [ # # ]: 0 : else if (TailMatches("REVOKE", "ADMIN|INHERIT|SET", "OPTION", "FOR"))
1402 4573 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
4574 : : }
4575 : :
1628 tgl@sss.pgh.pa.us 4576 : 0 : else if (TailMatches("GRANT|REVOKE", "ALTER") ||
4577 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER"))
4578 : 0 : COMPLETE_WITH("SYSTEM");
4579 : :
1378 michael@paquier.xyz 4580 : 0 : else if (TailMatches("REVOKE", "SET"))
4581 : 0 : COMPLETE_WITH("ON PARAMETER", "OPTION FOR");
4582 : 0 : else if (TailMatches("GRANT", "SET") ||
4583 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "SET") ||
4584 : : TailMatches("GRANT|REVOKE", "ALTER", "SYSTEM") ||
4585 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER", "SYSTEM"))
1628 tgl@sss.pgh.pa.us 4586 : 0 : COMPLETE_WITH("ON PARAMETER");
4587 : :
4588 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "PARAMETER") ||
4589 : : TailMatches("GRANT|REVOKE", MatchAny, MatchAny, "ON", "PARAMETER") ||
4590 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "PARAMETER") ||
4591 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, MatchAny, "ON", "PARAMETER"))
1627 4592 : 0 : COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_alter_system_set_vars);
4593 : :
1628 4594 : 0 : else if (TailMatches("GRANT", MatchAny, "ON", "PARAMETER", MatchAny) ||
4595 : : TailMatches("GRANT", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny))
4596 : 0 : COMPLETE_WITH("TO");
4597 : :
4598 : 0 : else if (TailMatches("REVOKE", MatchAny, "ON", "PARAMETER", MatchAny) ||
4599 : : TailMatches("REVOKE", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny) ||
4600 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "PARAMETER", MatchAny) ||
4601 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, MatchAny, "ON", "PARAMETER", MatchAny))
4602 : 0 : COMPLETE_WITH("FROM");
4603 : :
4604 : : /*
4605 : : * Complete GRANT/REVOKE <privilege> with "ON", GRANT/REVOKE <role> with
4606 : : * TO/FROM
4607 : : */
4608 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny) ||
4609 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny))
4610 : : {
921 nathan@postgresql.or 4611 [ # # ]: 0 : if (TailMatches("SELECT|INSERT|UPDATE|DELETE|TRUNCATE|REFERENCES|TRIGGER|CREATE|CONNECT|TEMPORARY|TEMP|EXECUTE|USAGE|MAINTAIN|ALL"))
2921 tgl@sss.pgh.pa.us 4612 : 0 : COMPLETE_WITH("ON");
4613 [ # # ]: 0 : else if (TailMatches("GRANT", MatchAny))
4614 : 0 : COMPLETE_WITH("TO");
4615 : : else
4616 : 0 : COMPLETE_WITH("FROM");
4617 : : }
4618 : :
4619 : : /*
4620 : : * Complete GRANT/REVOKE <sth> ON with a list of appropriate relations.
4621 : : *
4622 : : * Note: GRANT/REVOKE can get quite complex; tab-completion as implemented
4623 : : * here will only work if the privilege list contains exactly one
4624 : : * privilege.
4625 : : */
1628 4626 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON") ||
4627 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON"))
4628 : : {
4629 : : /*
4630 : : * With ALTER DEFAULT PRIVILEGES, restrict completion to the kinds of
4631 : : * objects supported.
4632 : : */
2921 4633 [ # # ]: 0 : if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
534 fujii@postgresql.org 4634 : 0 : COMPLETE_WITH("TABLES", "SEQUENCES", "FUNCTIONS", "PROCEDURES", "ROUTINES", "TYPES", "SCHEMAS", "LARGE OBJECTS");
4635 : : else
1694 tgl@sss.pgh.pa.us 4636 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_grantables,
4637 : : "ALL FUNCTIONS IN SCHEMA",
4638 : : "ALL PROCEDURES IN SCHEMA",
4639 : : "ALL ROUTINES IN SCHEMA",
4640 : : "ALL SEQUENCES IN SCHEMA",
4641 : : "ALL TABLES IN SCHEMA",
4642 : : "DATABASE",
4643 : : "DOMAIN",
4644 : : "FOREIGN DATA WRAPPER",
4645 : : "FOREIGN SERVER",
4646 : : "FUNCTION",
4647 : : "LANGUAGE",
4648 : : "LARGE OBJECT",
4649 : : "PARAMETER",
4650 : : "PROCEDURE",
4651 : : "ROUTINE",
4652 : : "SCHEMA",
4653 : : "SEQUENCE",
4654 : : "TABLE",
4655 : : "TABLESPACE",
4656 : : "TYPE");
4657 : : }
1628 4658 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "ALL") ||
4659 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "ALL"))
2921 4660 : 0 : COMPLETE_WITH("FUNCTIONS IN SCHEMA",
4661 : : "PROCEDURES IN SCHEMA",
4662 : : "ROUTINES IN SCHEMA",
4663 : : "SEQUENCES IN SCHEMA",
4664 : : "TABLES IN SCHEMA");
4665 : :
4666 : : /*
4667 : : * Complete "GRANT/REVOKE * ON DATABASE/DOMAIN/..." with a list of
4668 : : * appropriate objects or keywords.
4669 : : *
4670 : : * Complete "GRANT/REVOKE * ON *" with "TO/FROM".
4671 : : */
1628 4672 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", MatchAny) ||
4673 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", MatchAny))
4674 : : {
2921 4675 [ # # ]: 0 : if (TailMatches("DATABASE"))
8578 bruce@momjian.us 4676 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2921 tgl@sss.pgh.pa.us 4677 [ # # ]: 0 : else if (TailMatches("DOMAIN"))
1694 4678 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
2921 4679 [ # # ]: 0 : else if (TailMatches("FUNCTION"))
1694 4680 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_functions);
331 fujii@postgresql.org 4681 [ # # ]: 0 : else if (TailMatches("FOREIGN"))
4682 : 0 : COMPLETE_WITH("DATA WRAPPER", "SERVER");
2921 tgl@sss.pgh.pa.us 4683 [ # # ]: 0 : else if (TailMatches("LANGUAGE"))
8578 bruce@momjian.us 4684 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
331 fujii@postgresql.org 4685 [ # # ]: 0 : else if (TailMatches("LARGE"))
4686 : : {
4687 [ # # ]: 0 : if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
4688 : 0 : COMPLETE_WITH("OBJECTS");
4689 : : else
4690 : 0 : COMPLETE_WITH("OBJECT");
4691 : : }
2921 tgl@sss.pgh.pa.us 4692 [ # # ]: 0 : else if (TailMatches("PROCEDURE"))
1694 4693 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_procedures);
2921 4694 [ # # ]: 0 : else if (TailMatches("ROUTINE"))
1694 4695 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
2921 4696 [ # # ]: 0 : else if (TailMatches("SCHEMA"))
8578 bruce@momjian.us 4697 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
2921 tgl@sss.pgh.pa.us 4698 [ # # ]: 0 : else if (TailMatches("SEQUENCE"))
1694 4699 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences);
2921 4700 [ # # ]: 0 : else if (TailMatches("TABLE"))
1694 4701 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables);
2921 4702 [ # # ]: 0 : else if (TailMatches("TABLESPACE"))
8066 bruce@momjian.us 4703 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
2921 tgl@sss.pgh.pa.us 4704 [ # # ]: 0 : else if (TailMatches("TYPE"))
1694 4705 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2921 4706 [ # # ]: 0 : else if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny))
4707 : 0 : COMPLETE_WITH("TO");
4708 : : else
4709 : 0 : COMPLETE_WITH("FROM");
4710 : : }
4711 : :
4712 : : /*
4713 : : * Complete "GRANT/REVOKE ... TO/FROM" with username, PUBLIC,
4714 : : * CURRENT_ROLE, CURRENT_USER, or SESSION_USER.
4715 : : */
713 4716 : 0 : else if (Matches("GRANT", MatchAnyN, "TO") ||
4717 : : Matches("REVOKE", MatchAnyN, "FROM"))
1694 4718 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4719 : : Keywords_for_list_of_grant_roles);
4720 : :
4721 : : /*
4722 : : * Offer grant options after that.
4723 : : */
713 4724 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny))
1402 michael@paquier.xyz 4725 : 0 : COMPLETE_WITH("WITH ADMIN",
4726 : : "WITH INHERIT",
4727 : : "WITH SET",
4728 : : "WITH GRANT OPTION",
4729 : : "GRANTED BY");
713 tgl@sss.pgh.pa.us 4730 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH"))
1402 michael@paquier.xyz 4731 : 0 : COMPLETE_WITH("ADMIN",
4732 : : "INHERIT",
4733 : : "SET",
4734 : : "GRANT OPTION");
713 tgl@sss.pgh.pa.us 4735 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", "ADMIN|INHERIT|SET"))
1402 michael@paquier.xyz 4736 : 0 : COMPLETE_WITH("OPTION", "TRUE", "FALSE");
713 tgl@sss.pgh.pa.us 4737 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", MatchAny, "OPTION"))
1628 4738 : 0 : COMPLETE_WITH("GRANTED BY");
713 4739 : 0 : else if (Matches("GRANT", MatchAnyN, "TO", MatchAny, "WITH", MatchAny, "OPTION", "GRANTED", "BY"))
1628 4740 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4741 : : Keywords_for_list_of_grant_roles);
4742 : : /* Complete "ALTER DEFAULT PRIVILEGES ... GRANT/REVOKE ... TO/FROM */
713 4743 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", MatchAnyN, "TO|FROM"))
1694 4744 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4745 : : Keywords_for_list_of_grant_roles);
4746 : : /* Offer WITH GRANT OPTION after that */
713 4747 : 0 : else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", MatchAnyN, "TO", MatchAny))
895 msawada@postgresql.o 4748 : 0 : COMPLETE_WITH("WITH GRANT OPTION");
4749 : : /* Complete "GRANT/REVOKE ... ON * *" with TO/FROM */
432 fujii@postgresql.org 4750 : 0 : else if (Matches("GRANT|REVOKE", MatchAnyN, "ON", MatchAny, MatchAny) &&
4751 [ # # # # ]: 0 : !TailMatches("FOREIGN", "SERVER") && !TailMatches("LARGE", "OBJECT"))
4752 : : {
4753 [ # # ]: 0 : if (Matches("GRANT", MatchAnyN, "ON", MatchAny, MatchAny))
4754 : 0 : COMPLETE_WITH("TO");
4755 : : else
4756 : 0 : COMPLETE_WITH("FROM");
4757 : : }
4758 : :
4759 : : /* Complete "GRANT/REVOKE * ON ALL * IN SCHEMA *" with TO/FROM */
1628 tgl@sss.pgh.pa.us 4760 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny) ||
4761 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "ALL", MatchAny, "IN", "SCHEMA", MatchAny))
4762 : : {
2921 4763 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4764 : 0 : COMPLETE_WITH("TO");
4765 : : else
4766 : 0 : COMPLETE_WITH("FROM");
4767 : : }
4768 : :
4769 : : /* Complete "GRANT/REVOKE * ON FOREIGN DATA WRAPPER *" with TO/FROM */
1628 4770 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny) ||
4771 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "FOREIGN", "DATA", "WRAPPER", MatchAny))
4772 : : {
2921 4773 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4774 : 0 : COMPLETE_WITH("TO");
4775 : : else
4776 : 0 : COMPLETE_WITH("FROM");
4777 : : }
4778 : :
4779 : : /* Complete "GRANT/REVOKE * ON FOREIGN SERVER *" with TO/FROM */
1628 4780 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny) ||
4781 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "FOREIGN", "SERVER", MatchAny))
4782 : : {
2921 4783 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4784 : 0 : COMPLETE_WITH("TO");
4785 : : else
4786 : 0 : COMPLETE_WITH("FROM");
4787 : : }
4788 : :
4789 : : /* Complete "GRANT/REVOKE * ON LARGE OBJECT *" with TO/FROM */
438 fujii@postgresql.org 4790 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "LARGE", "OBJECT", MatchAny) ||
4791 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "LARGE", "OBJECT", MatchAny))
4792 : : {
4793 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny, MatchAny))
4794 : 0 : COMPLETE_WITH("TO");
4795 : : else
4796 : 0 : COMPLETE_WITH("FROM");
4797 : : }
4798 : :
4799 : : /* Complete "GRANT/REVOKE * ON LARGE OBJECTS" with TO/FROM */
4800 : 0 : else if (TailMatches("GRANT|REVOKE", MatchAny, "ON", "LARGE", "OBJECTS") ||
4801 : : TailMatches("REVOKE", "GRANT", "OPTION", "FOR", MatchAny, "ON", "LARGE", "OBJECTS"))
4802 : : {
4803 [ # # ]: 0 : if (TailMatches("GRANT", MatchAny, MatchAny, MatchAny, MatchAny))
4804 : 0 : COMPLETE_WITH("TO");
4805 : : else
4806 : 0 : COMPLETE_WITH("FROM");
4807 : : }
4808 : :
4809 : : /* GROUP BY */
2921 tgl@sss.pgh.pa.us 4810 : 0 : else if (TailMatches("FROM", MatchAny, "GROUP"))
4811 : 0 : COMPLETE_WITH("BY");
4812 : :
4813 : : /* IMPORT FOREIGN SCHEMA */
4814 : 0 : else if (Matches("IMPORT"))
4815 : 0 : COMPLETE_WITH("FOREIGN SCHEMA");
4816 : 0 : else if (Matches("IMPORT", "FOREIGN"))
4817 : 0 : COMPLETE_WITH("SCHEMA");
2194 michael@paquier.xyz 4818 : 0 : else if (Matches("IMPORT", "FOREIGN", "SCHEMA", MatchAny))
4819 : 0 : COMPLETE_WITH("EXCEPT (", "FROM SERVER", "LIMIT TO (");
4820 : 0 : else if (TailMatches("LIMIT", "TO", "(*)") ||
4821 : : Matches("IMPORT", "FOREIGN", "SCHEMA", MatchAny, "EXCEPT", "(*)"))
4822 : 0 : COMPLETE_WITH("FROM SERVER");
4823 : 0 : else if (TailMatches("FROM", "SERVER", MatchAny))
4824 : 0 : COMPLETE_WITH("INTO");
4825 : 0 : else if (TailMatches("FROM", "SERVER", MatchAny, "INTO"))
4826 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
4827 : 0 : else if (TailMatches("FROM", "SERVER", MatchAny, "INTO", MatchAny))
4828 : 0 : COMPLETE_WITH("OPTIONS (");
4829 : :
4830 : : /* INSERT --- can be inside EXPLAIN, RULE, etc */
4831 : : /* Complete NOT MATCHED THEN INSERT */
1637 alvherre@alvh.no-ip. 4832 : 0 : else if (TailMatches("NOT", "MATCHED", "THEN", "INSERT"))
4833 : 0 : COMPLETE_WITH("VALUES", "(");
4834 : : /* Complete INSERT with "INTO" */
2921 tgl@sss.pgh.pa.us 4835 : 0 : else if (TailMatches("INSERT"))
4836 : 0 : COMPLETE_WITH("INTO");
4837 : : /* Complete INSERT INTO with table names */
4838 : 0 : else if (TailMatches("INSERT", "INTO"))
1694 4839 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
4840 : : /* Complete "INSERT INTO <table> (" with attribute names */
2921 4841 : 0 : else if (TailMatches("INSERT", "INTO", MatchAny, "("))
1694 4842 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
4843 : :
4844 : : /*
4845 : : * Complete INSERT INTO <table> with "(" or "VALUES" or "SELECT" or
4846 : : * "TABLE" or "DEFAULT VALUES" or "OVERRIDING"
4847 : : */
2921 4848 : 0 : else if (TailMatches("INSERT", "INTO", MatchAny))
4849 : 0 : COMPLETE_WITH("(", "DEFAULT VALUES", "SELECT", "TABLE", "VALUES", "OVERRIDING");
4850 : :
4851 : : /*
4852 : : * Complete INSERT INTO <table> (attribs) with "VALUES" or "SELECT" or
4853 : : * "TABLE" or "OVERRIDING"
4854 : : */
4855 : 0 : else if (TailMatches("INSERT", "INTO", MatchAny, MatchAny) &&
3928 4856 [ # # ]: 0 : ends_with(prev_wd, ')'))
2921 4857 : 0 : COMPLETE_WITH("SELECT", "TABLE", "VALUES", "OVERRIDING");
4858 : :
4859 : : /* Complete OVERRIDING */
4860 : 0 : else if (TailMatches("OVERRIDING"))
4861 : 0 : COMPLETE_WITH("SYSTEM VALUE", "USER VALUE");
4862 : :
4863 : : /* Complete after OVERRIDING clause */
4864 : 0 : else if (TailMatches("OVERRIDING", MatchAny, "VALUE"))
4865 : 0 : COMPLETE_WITH("SELECT", "TABLE", "VALUES");
4866 : :
4867 : : /* Insert an open parenthesis after "VALUES" */
4868 [ # # ]: 0 : else if (TailMatches("VALUES") && !TailMatches("DEFAULT", "VALUES"))
4869 : 0 : COMPLETE_WITH("(");
4870 : :
4871 : : /* LOCK */
4872 : : /* Complete LOCK [TABLE] [ONLY] with a list of tables */
4873 : 0 : else if (Matches("LOCK"))
1694 4874 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
4875 : : "TABLE", "ONLY");
2921 4876 : 0 : else if (Matches("LOCK", "TABLE"))
1694 4877 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_tables,
4878 : : "ONLY");
1811 fujii@postgresql.org 4879 : 0 : else if (Matches("LOCK", "TABLE", "ONLY") || Matches("LOCK", "ONLY"))
1694 tgl@sss.pgh.pa.us 4880 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
4881 : : /* For the following, handle the case of a single table only for now */
4882 : :
4883 : : /* Complete LOCK [TABLE] [ONLY] <table> with IN or NOWAIT */
1811 fujii@postgresql.org 4884 : 0 : else if (Matches("LOCK", MatchAnyExcept("TABLE|ONLY")) ||
4885 : : Matches("LOCK", "TABLE", MatchAnyExcept("ONLY")) ||
4886 : : Matches("LOCK", "ONLY", MatchAny) ||
4887 : : Matches("LOCK", "TABLE", "ONLY", MatchAny))
4888 : 0 : COMPLETE_WITH("IN", "NOWAIT");
4889 : :
4890 : : /* Complete LOCK [TABLE] [ONLY] <table> IN with a lock mode */
713 tgl@sss.pgh.pa.us 4891 : 0 : else if (Matches("LOCK", MatchAnyN, "IN"))
2921 4892 : 0 : COMPLETE_WITH("ACCESS SHARE MODE",
4893 : : "ROW SHARE MODE", "ROW EXCLUSIVE MODE",
4894 : : "SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE",
4895 : : "SHARE ROW EXCLUSIVE MODE",
4896 : : "EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE");
4897 : :
4898 : : /*
4899 : : * Complete LOCK [TABLE][ONLY] <table> IN ACCESS|ROW with rest of lock
4900 : : * mode
4901 : : */
713 4902 : 0 : else if (Matches("LOCK", MatchAnyN, "IN", "ACCESS|ROW"))
2921 4903 : 0 : COMPLETE_WITH("EXCLUSIVE MODE", "SHARE MODE");
4904 : :
4905 : : /* Complete LOCK [TABLE] [ONLY] <table> IN SHARE with rest of lock mode */
713 4906 : 0 : else if (Matches("LOCK", MatchAnyN, "IN", "SHARE"))
2921 4907 : 0 : COMPLETE_WITH("MODE", "ROW EXCLUSIVE MODE",
4908 : : "UPDATE EXCLUSIVE MODE");
4909 : :
4910 : : /* Complete LOCK [TABLE] [ONLY] <table> [IN lockmode MODE] with "NOWAIT" */
713 4911 : 0 : else if (Matches("LOCK", MatchAnyN, "MODE"))
1459 fujii@postgresql.org 4912 : 0 : COMPLETE_WITH("NOWAIT");
4913 : :
4914 : : /* MERGE --- can be inside EXPLAIN */
1637 alvherre@alvh.no-ip. 4915 : 0 : else if (TailMatches("MERGE"))
4916 : 0 : COMPLETE_WITH("INTO");
4917 : 0 : else if (TailMatches("MERGE", "INTO"))
4918 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_mergetargets);
4919 : :
4920 : : /* Complete MERGE INTO <table> [[AS] <alias>] with USING */
4921 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny))
4922 : 0 : COMPLETE_WITH("USING", "AS");
1459 fujii@postgresql.org 4923 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny) ||
4924 : : TailMatches("MERGE", "INTO", MatchAny, MatchAnyExcept("USING|AS")))
1637 alvherre@alvh.no-ip. 4925 : 0 : COMPLETE_WITH("USING");
4926 : :
4927 : : /*
4928 : : * Complete MERGE INTO ... USING with a list of relations supporting
4929 : : * SELECT
4930 : : */
1459 fujii@postgresql.org 4931 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "USING") ||
4932 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING") ||
4933 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING"))
4934 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
4935 : :
4936 : : /*
4937 : : * Complete MERGE INTO <table> [[AS] <alias>] USING <relations> [[AS]
4938 : : * alias] with ON
4939 : : */
4940 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny) ||
4941 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny) ||
4942 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny))
4943 : 0 : COMPLETE_WITH("AS", "ON");
4944 : 0 : else if (TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny, "AS", MatchAny) ||
4945 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, "AS", MatchAny) ||
4946 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny, "AS", MatchAny) ||
4947 : : TailMatches("MERGE", "INTO", MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")) ||
4948 : : TailMatches("MERGE", "INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")) ||
4949 : : TailMatches("MERGE", "INTO", MatchAny, MatchAny, "USING", MatchAny, MatchAnyExcept("ON|AS")))
1637 alvherre@alvh.no-ip. 4950 : 0 : COMPLETE_WITH("ON");
4951 : :
4952 : : /* Complete MERGE INTO ... ON with target table attributes */
4953 : 0 : else if (TailMatches("INTO", MatchAny, "USING", MatchAny, "ON"))
4954 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
4955 : 0 : else if (TailMatches("INTO", MatchAny, "AS", MatchAny, "USING", MatchAny, "AS", MatchAny, "ON"))
4956 : 0 : COMPLETE_WITH_ATTR(prev8_wd);
4957 : 0 : else if (TailMatches("INTO", MatchAny, MatchAny, "USING", MatchAny, MatchAny, "ON"))
4958 : 0 : COMPLETE_WITH_ATTR(prev6_wd);
4959 : :
4960 : : /*
4961 : : * Complete ... USING <relation> [[AS] alias] ON join condition
4962 : : * (consisting of one or three words typically used) with WHEN [NOT]
4963 : : * MATCHED
4964 : : */
1459 fujii@postgresql.org 4965 : 0 : else if (TailMatches("USING", MatchAny, "ON", MatchAny) ||
4966 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny) ||
4967 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny) ||
4968 : : TailMatches("USING", MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")) ||
4969 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")) ||
4970 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, MatchAnyExcept("WHEN"), MatchAnyExcept("WHEN")))
1637 alvherre@alvh.no-ip. 4971 : 0 : COMPLETE_WITH("WHEN MATCHED", "WHEN NOT MATCHED");
1459 fujii@postgresql.org 4972 : 0 : else if (TailMatches("USING", MatchAny, "ON", MatchAny, "WHEN") ||
4973 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, "WHEN") ||
4974 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, "WHEN") ||
4975 : : TailMatches("USING", MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN") ||
4976 : : TailMatches("USING", MatchAny, "AS", MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN") ||
4977 : : TailMatches("USING", MatchAny, MatchAny, "ON", MatchAny, MatchAny, MatchAny, "WHEN"))
4978 : 0 : COMPLETE_WITH("MATCHED", "NOT MATCHED");
4979 : :
4980 : : /*
4981 : : * Complete ... WHEN MATCHED and WHEN NOT MATCHED BY SOURCE|TARGET with
4982 : : * THEN/AND
4983 : : */
4984 : 0 : else if (TailMatches("WHEN", "MATCHED") ||
4985 : : TailMatches("WHEN", "NOT", "MATCHED", "BY", "SOURCE|TARGET"))
1637 alvherre@alvh.no-ip. 4986 : 0 : COMPLETE_WITH("THEN", "AND");
4987 : :
4988 : : /* Complete ... WHEN NOT MATCHED with BY/THEN/AND */
904 dean.a.rasheed@gmail 4989 : 0 : else if (TailMatches("WHEN", "NOT", "MATCHED"))
4990 : 0 : COMPLETE_WITH("BY", "THEN", "AND");
4991 : :
4992 : : /* Complete ... WHEN NOT MATCHED BY with SOURCE/TARGET */
4993 : 0 : else if (TailMatches("WHEN", "NOT", "MATCHED", "BY"))
4994 : 0 : COMPLETE_WITH("SOURCE", "TARGET");
4995 : :
4996 : : /*
4997 : : * Complete ... WHEN MATCHED THEN and WHEN NOT MATCHED BY SOURCE THEN with
4998 : : * UPDATE SET/DELETE/DO NOTHING
4999 : : */
5000 : 0 : else if (TailMatches("WHEN", "MATCHED", "THEN") ||
5001 : : TailMatches("WHEN", "NOT", "MATCHED", "BY", "SOURCE", "THEN"))
1459 fujii@postgresql.org 5002 : 0 : COMPLETE_WITH("UPDATE SET", "DELETE", "DO NOTHING");
5003 : :
5004 : : /*
5005 : : * Complete ... WHEN NOT MATCHED [BY TARGET] THEN with INSERT/DO NOTHING
5006 : : */
904 dean.a.rasheed@gmail 5007 : 0 : else if (TailMatches("WHEN", "NOT", "MATCHED", "THEN") ||
5008 : : TailMatches("WHEN", "NOT", "MATCHED", "BY", "TARGET", "THEN"))
1637 alvherre@alvh.no-ip. 5009 : 0 : COMPLETE_WITH("INSERT", "DO NOTHING");
5010 : :
5011 : : /* NOTIFY --- can be inside EXPLAIN, RULE, etc */
2921 tgl@sss.pgh.pa.us 5012 : 0 : else if (TailMatches("NOTIFY"))
1694 5013 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_channels);
5014 : :
5015 : : /* OPTIONS */
2921 5016 : 0 : else if (TailMatches("OPTIONS"))
5017 : 0 : COMPLETE_WITH("(");
5018 : :
5019 : : /* OWNER TO - complete with available roles */
5020 : 0 : else if (TailMatches("OWNER", "TO"))
1446 michael@paquier.xyz 5021 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
5022 : : Keywords_for_list_of_owner_roles);
5023 : :
5024 : : /* ORDER BY */
2921 tgl@sss.pgh.pa.us 5025 : 0 : else if (TailMatches("FROM", MatchAny, "ORDER"))
5026 : 0 : COMPLETE_WITH("BY");
5027 : 0 : else if (TailMatches("FROM", MatchAny, "ORDER", "BY"))
1694 5028 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
5029 : :
5030 : : /* PREPARE xx AS */
2921 5031 : 0 : else if (Matches("PREPARE", MatchAny, "AS"))
301 fujii@postgresql.org 5032 : 0 : COMPLETE_WITH("SELECT", "UPDATE", "INSERT INTO", "DELETE FROM",
5033 : : "MERGE INTO", "VALUES", "WITH", "TABLE");
5034 : :
5035 : : /*
5036 : : * PREPARE TRANSACTION is missing on purpose. It's intended for transaction
5037 : : * managers, not for manual use in interactive sessions.
5038 : : */
5039 : :
5040 : : /* REASSIGN OWNED BY xxx TO yyy */
2921 tgl@sss.pgh.pa.us 5041 : 0 : else if (Matches("REASSIGN"))
5042 : 0 : COMPLETE_WITH("OWNED BY");
5043 : 0 : else if (Matches("REASSIGN", "OWNED"))
5044 : 0 : COMPLETE_WITH("BY");
5045 : 0 : else if (Matches("REASSIGN", "OWNED", "BY"))
7476 alvherre@alvh.no-ip. 5046 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2921 tgl@sss.pgh.pa.us 5047 : 0 : else if (Matches("REASSIGN", "OWNED", "BY", MatchAny))
5048 : 0 : COMPLETE_WITH("TO");
5049 : 0 : else if (Matches("REASSIGN", "OWNED", "BY", MatchAny, "TO"))
7476 alvherre@alvh.no-ip. 5050 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5051 : :
5052 : : /* REFRESH MATERIALIZED VIEW */
2921 tgl@sss.pgh.pa.us 5053 : 0 : else if (Matches("REFRESH"))
5054 : 0 : COMPLETE_WITH("MATERIALIZED VIEW");
5055 : 0 : else if (Matches("REFRESH", "MATERIALIZED"))
5056 : 0 : COMPLETE_WITH("VIEW");
5057 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW"))
1694 5058 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_matviews,
5059 : : "CONCURRENTLY");
2921 5060 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY"))
1694 5061 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
2921 5062 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny))
5063 : 0 : COMPLETE_WITH("WITH");
5064 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny))
5065 : 0 : COMPLETE_WITH("WITH");
5066 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH"))
5067 : 0 : COMPLETE_WITH("NO DATA", "DATA");
5068 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH"))
5069 : 0 : COMPLETE_WITH("NO DATA", "DATA");
5070 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", MatchAny, "WITH", "NO"))
5071 : 0 : COMPLETE_WITH("DATA");
5072 : 0 : else if (Matches("REFRESH", "MATERIALIZED", "VIEW", "CONCURRENTLY", MatchAny, "WITH", "NO"))
5073 : 0 : COMPLETE_WITH("DATA");
5074 : :
5075 : : /* REINDEX */
2231 michael@paquier.xyz 5076 : 0 : else if (Matches("REINDEX") ||
5077 : : Matches("REINDEX", "(*)"))
2921 tgl@sss.pgh.pa.us 5078 : 0 : COMPLETE_WITH("TABLE", "INDEX", "SYSTEM", "SCHEMA", "DATABASE");
2231 michael@paquier.xyz 5079 : 0 : else if (Matches("REINDEX", "TABLE") ||
5080 : : Matches("REINDEX", "(*)", "TABLE"))
1694 tgl@sss.pgh.pa.us 5081 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexables,
5082 : : "CONCURRENTLY");
2231 michael@paquier.xyz 5083 : 0 : else if (Matches("REINDEX", "INDEX") ||
5084 : : Matches("REINDEX", "(*)", "INDEX"))
1694 tgl@sss.pgh.pa.us 5085 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_indexes,
5086 : : "CONCURRENTLY");
2231 michael@paquier.xyz 5087 : 0 : else if (Matches("REINDEX", "SCHEMA") ||
5088 : : Matches("REINDEX", "(*)", "SCHEMA"))
1694 tgl@sss.pgh.pa.us 5089 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas,
5090 : : "CONCURRENTLY");
2231 michael@paquier.xyz 5091 : 0 : else if (Matches("REINDEX", "SYSTEM|DATABASE") ||
5092 : : Matches("REINDEX", "(*)", "SYSTEM|DATABASE"))
1694 tgl@sss.pgh.pa.us 5093 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_databases,
5094 : : "CONCURRENTLY");
2231 michael@paquier.xyz 5095 : 0 : else if (Matches("REINDEX", "TABLE", "CONCURRENTLY") ||
5096 : : Matches("REINDEX", "(*)", "TABLE", "CONCURRENTLY"))
1694 tgl@sss.pgh.pa.us 5097 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables);
2231 michael@paquier.xyz 5098 : 0 : else if (Matches("REINDEX", "INDEX", "CONCURRENTLY") ||
5099 : : Matches("REINDEX", "(*)", "INDEX", "CONCURRENTLY"))
1694 tgl@sss.pgh.pa.us 5100 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
2231 michael@paquier.xyz 5101 : 0 : else if (Matches("REINDEX", "SCHEMA", "CONCURRENTLY") ||
5102 : : Matches("REINDEX", "(*)", "SCHEMA", "CONCURRENTLY"))
2732 peter@eisentraut.org 5103 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
2231 michael@paquier.xyz 5104 : 0 : else if (Matches("REINDEX", "SYSTEM|DATABASE", "CONCURRENTLY") ||
5105 : : Matches("REINDEX", "(*)", "SYSTEM|DATABASE", "CONCURRENTLY"))
3912 tgl@sss.pgh.pa.us 5106 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2231 michael@paquier.xyz 5107 : 0 : else if (HeadMatches("REINDEX", "(*") &&
5108 [ # # ]: 0 : !HeadMatches("REINDEX", "(*)"))
5109 : : {
5110 : : /*
5111 : : * This fires if we're in an unfinished parenthesized option list.
5112 : : * get_previous_words treats a completed parenthesized option list as
5113 : : * one word, so the above test is correct.
5114 : : */
5115 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
2054 5116 : 0 : COMPLETE_WITH("CONCURRENTLY", "TABLESPACE", "VERBOSE");
5117 [ # # ]: 0 : else if (TailMatches("TABLESPACE"))
5118 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
5119 : : }
5120 : :
5121 : : /* REPACK */
194 alvherre@kurilemu.de 5122 : 0 : else if (Matches("REPACK"))
5123 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
5124 : : "(", "USING INDEX");
5125 : 0 : else if (Matches("REPACK", "(*)"))
5126 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_clusterables,
5127 : : "USING INDEX");
5128 : 0 : else if (Matches("REPACK", MatchAnyExcept("(")))
5129 : 0 : COMPLETE_WITH("USING INDEX");
5130 : 0 : else if (Matches("REPACK", "(*)", MatchAnyExcept("(")))
5131 : 0 : COMPLETE_WITH("USING INDEX");
5132 : 0 : else if (Matches("REPACK", MatchAny, "USING", "INDEX") ||
5133 : : Matches("REPACK", "(*)", MatchAny, "USING", "INDEX"))
5134 : : {
5135 : 0 : set_completion_reference(prev3_wd);
5136 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
5137 : : }
5138 : :
5139 : : /*
5140 : : * Complete ... [ (*) ] <sth> USING INDEX, with a list of indexes for
5141 : : * <sth>.
5142 : : */
5143 : 0 : else if (TailMatches(MatchAny, "USING", "INDEX"))
5144 : : {
5145 : 0 : set_completion_reference(prev3_wd);
5146 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_index_of_table);
5147 : : }
5148 : 0 : else if (HeadMatches("REPACK", "(*") &&
5149 [ # # ]: 0 : !HeadMatches("REPACK", "(*)"))
5150 : : {
5151 : : /*
5152 : : * This fires if we're in an unfinished parenthesized option list.
5153 : : * get_previous_words treats a completed parenthesized option list as
5154 : : * one word, so the above test is correct.
5155 : : */
5156 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
167 5157 : 0 : COMPLETE_WITH("ANALYZE", "CONCURRENTLY", "VERBOSE");
128 fujii@postgresql.org 5158 [ # # ]: 0 : else if (TailMatches("ANALYZE|CONCURRENTLY|VERBOSE"))
194 alvherre@kurilemu.de 5159 : 0 : COMPLETE_WITH("ON", "OFF");
5160 : : }
5161 : :
5162 : : /* SECURITY LABEL */
2921 tgl@sss.pgh.pa.us 5163 : 0 : else if (Matches("SECURITY"))
5164 : 0 : COMPLETE_WITH("LABEL");
5165 : 0 : else if (Matches("SECURITY", "LABEL"))
5166 : 0 : COMPLETE_WITH("ON", "FOR");
5167 : 0 : else if (Matches("SECURITY", "LABEL", "FOR", MatchAny))
5168 : 0 : COMPLETE_WITH("ON");
5169 : 0 : else if (Matches("SECURITY", "LABEL", "ON") ||
5170 : : Matches("SECURITY", "LABEL", "FOR", MatchAny, "ON"))
5171 : 0 : COMPLETE_WITH("TABLE", "COLUMN", "AGGREGATE", "DATABASE", "DOMAIN",
5172 : : "EVENT TRIGGER", "FOREIGN TABLE", "FUNCTION",
5173 : : "LARGE OBJECT", "MATERIALIZED VIEW", "LANGUAGE",
5174 : : "PUBLICATION", "PROCEDURE", "ROLE", "ROUTINE", "SCHEMA",
5175 : : "SEQUENCE", "SUBSCRIPTION", "TABLESPACE", "TYPE", "VIEW");
5176 : 0 : else if (Matches("SECURITY", "LABEL", "ON", MatchAny, MatchAny))
5177 : 0 : COMPLETE_WITH("IS");
5178 : :
5179 : : /* SELECT */
5180 : : /* naah . . . */
5181 : :
5182 : : /* SET, RESET, SHOW */
5183 : : /* Complete with a variable name */
580 tomas.vondra@postgre 5184 : 0 : else if (TailMatches("SET|RESET") &&
5185 : : !TailMatches("UPDATE", MatchAny, "SET") &&
416 tomas.vondra@postgre 5186 [ + - + - ]:CBC 3 : !TailMatches("ALTER", "DATABASE|USER|ROLE", MatchAny, "RESET"))
1684 tgl@sss.pgh.pa.us 5187 : 3 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_set_vars,
5188 : : "CONSTRAINTS",
5189 : : "TRANSACTION",
5190 : : "SESSION",
5191 : : "ROLE",
5192 : : "TABLESPACE",
5193 : : "ALL");
2921 5194 : 3 : else if (Matches("SHOW"))
1684 tgl@sss.pgh.pa.us 5195 :UBC 0 : COMPLETE_WITH_QUERY_VERBATIM_PLUS(Query_for_list_of_show_vars,
5196 : : "SESSION AUTHORIZATION",
5197 : : "ALL");
1692 5198 : 0 : else if (Matches("SHOW", "SESSION"))
5199 : 0 : COMPLETE_WITH("AUTHORIZATION");
5200 : : /* Complete "SET TRANSACTION" */
2921 5201 : 0 : else if (Matches("SET", "TRANSACTION"))
5202 : 0 : COMPLETE_WITH("SNAPSHOT", "ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
5203 : 0 : else if (Matches("BEGIN|START", "TRANSACTION") ||
5204 : : Matches("BEGIN", "WORK") ||
5205 : : Matches("BEGIN") ||
5206 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION"))
5207 : 0 : COMPLETE_WITH("ISOLATION LEVEL", "READ", "DEFERRABLE", "NOT DEFERRABLE");
5208 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "NOT") ||
5209 : : Matches("BEGIN", "NOT") ||
5210 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "NOT"))
5211 : 0 : COMPLETE_WITH("DEFERRABLE");
5212 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION") ||
5213 : : Matches("BEGIN", "ISOLATION") ||
5214 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION"))
5215 : 0 : COMPLETE_WITH("LEVEL");
5216 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL") ||
5217 : : Matches("BEGIN", "ISOLATION", "LEVEL") ||
5218 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL"))
5219 : 0 : COMPLETE_WITH("READ", "REPEATABLE READ", "SERIALIZABLE");
5220 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "READ") ||
5221 : : Matches("BEGIN", "ISOLATION", "LEVEL", "READ") ||
5222 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "READ"))
5223 : 0 : COMPLETE_WITH("UNCOMMITTED", "COMMITTED");
5224 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "ISOLATION", "LEVEL", "REPEATABLE") ||
5225 : : Matches("BEGIN", "ISOLATION", "LEVEL", "REPEATABLE") ||
5226 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "ISOLATION", "LEVEL", "REPEATABLE"))
5227 : 0 : COMPLETE_WITH("READ");
5228 : 0 : else if (Matches("SET|BEGIN|START", "TRANSACTION|WORK", "READ") ||
5229 : : Matches("BEGIN", "READ") ||
5230 : : Matches("SET", "SESSION", "CHARACTERISTICS", "AS", "TRANSACTION", "READ"))
5231 : 0 : COMPLETE_WITH("ONLY", "WRITE");
5232 : : /* SET CONSTRAINTS */
5233 : 0 : else if (Matches("SET", "CONSTRAINTS"))
1694 5234 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_constraints_with_schema,
5235 : : "ALL");
5236 : : /* Complete SET CONSTRAINTS <foo> with DEFERRED|IMMEDIATE */
2921 5237 : 0 : else if (Matches("SET", "CONSTRAINTS", MatchAny))
5238 : 0 : COMPLETE_WITH("DEFERRED", "IMMEDIATE");
5239 : : /* Complete SET ROLE */
5240 : 0 : else if (Matches("SET", "ROLE"))
7707 5241 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5242 : : /* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */
2921 5243 : 0 : else if (Matches("SET", "SESSION"))
5244 : 0 : COMPLETE_WITH("AUTHORIZATION", "CHARACTERISTICS AS TRANSACTION");
5245 : : /* Complete SET SESSION AUTHORIZATION with username */
5246 : 0 : else if (Matches("SET", "SESSION", "AUTHORIZATION"))
1694 5247 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
5248 : : "DEFAULT");
5249 : : /* Complete RESET SESSION with AUTHORIZATION */
2921 5250 : 0 : else if (Matches("RESET", "SESSION"))
5251 : 0 : COMPLETE_WITH("AUTHORIZATION");
5252 : : /* Complete SET <var> with "TO" */
5253 : 0 : else if (Matches("SET", MatchAny))
2921 tgl@sss.pgh.pa.us 5254 :CBC 2 : COMPLETE_WITH("TO");
5255 : :
5256 : : /*
5257 : : * Complete ALTER DATABASE|FUNCTION|PROCEDURE|ROLE|ROUTINE|USER ... SET
5258 : : * <name>
5259 : : */
713 5260 : 2 : else if (Matches("ALTER", "DATABASE|FUNCTION|PROCEDURE|ROLE|ROUTINE|USER", MatchAnyN, "SET", MatchAnyExcept("SCHEMA")))
2921 tgl@sss.pgh.pa.us 5261 :UBC 0 : COMPLETE_WITH("FROM CURRENT", "TO");
5262 : :
5263 : : /*
5264 : : * Suggest possible variable values in SET variable TO|=, along with the
5265 : : * preceding ALTER syntaxes.
5266 : : */
2637 5267 : 0 : else if (TailMatches("SET", MatchAny, "TO|=") &&
2637 tgl@sss.pgh.pa.us 5268 [ + - ]:CBC 4 : !TailMatches("UPDATE", MatchAny, "SET", MatchAny, "TO|="))
5269 : : {
5270 : : /* special cased code for individual GUCs */
2921 5271 [ - + ]: 4 : if (TailMatches("DateStyle", "TO|="))
2921 tgl@sss.pgh.pa.us 5272 :UBC 0 : COMPLETE_WITH("ISO", "SQL", "Postgres", "German",
5273 : : "YMD", "DMY", "MDY",
5274 : : "US", "European", "NonEuropean",
5275 : : "DEFAULT");
2921 tgl@sss.pgh.pa.us 5276 [ - + ]:CBC 4 : else if (TailMatches("search_path", "TO|="))
5277 : : {
5278 : : /* Here, we want to allow pg_catalog, so use narrower exclusion */
1694 tgl@sss.pgh.pa.us 5279 :UBC 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas
5280 : : " AND nspname NOT LIKE E'pg\\\\_toast%%'"
5281 : : " AND nspname NOT LIKE E'pg\\\\_temp%%'",
5282 : : "DEFAULT");
5283 : : }
1645 tgl@sss.pgh.pa.us 5284 [ + + ]:CBC 4 : else if (TailMatches("TimeZone", "TO|="))
5285 [ - + + - : 2 : COMPLETE_WITH_TIMEZONE_NAME();
+ + ]
5286 : : else
5287 : : {
5288 : : /* generic, type based, GUC support */
4030 andres@anarazel.de 5289 : 2 : char *guctype = get_guctype(prev2_wd);
5290 : :
5291 : : /*
5292 : : * Note: if we don't recognize the GUC name, it's important to not
5293 : : * offer any completions, as most likely we've misinterpreted the
5294 : : * context and this isn't a GUC-setting command at all.
5295 : : */
2637 tgl@sss.pgh.pa.us 5296 [ + - ]: 2 : if (guctype)
5297 : : {
5298 [ + - ]: 2 : if (strcmp(guctype, "enum") == 0)
5299 : : {
1684 5300 : 2 : set_completion_reference_verbatim(prev2_wd);
1694 5301 : 2 : COMPLETE_WITH_QUERY_PLUS(Query_for_values_of_enum_GUC,
5302 : : "DEFAULT");
5303 : : }
2637 tgl@sss.pgh.pa.us 5304 [ # # ]:UBC 0 : else if (strcmp(guctype, "bool") == 0)
5305 : 0 : COMPLETE_WITH("on", "off", "true", "false", "yes", "no",
5306 : : "1", "0", "DEFAULT");
5307 : : else
5308 : 0 : COMPLETE_WITH("DEFAULT");
5309 : :
4030 andres@anarazel.de 5310 :CBC 2 : free(guctype);
5311 : : }
5312 : : }
5313 : : }
5314 : :
5315 : : /* START TRANSACTION */
2921 tgl@sss.pgh.pa.us 5316 : 4 : else if (Matches("START"))
2921 tgl@sss.pgh.pa.us 5317 :UBC 0 : COMPLETE_WITH("TRANSACTION");
5318 : :
5319 : : /* TABLE, but not TABLE embedded in other commands */
5320 : 0 : else if (Matches("TABLE"))
1694 5321 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
5322 : :
5323 : : /* TABLESAMPLE */
2921 5324 : 0 : else if (TailMatches("TABLESAMPLE"))
4108 rhaas@postgresql.org 5325 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablesample_methods);
2921 tgl@sss.pgh.pa.us 5326 : 0 : else if (TailMatches("TABLESAMPLE", MatchAny))
5327 : 0 : COMPLETE_WITH("(");
5328 : :
5329 : : /* TRUNCATE */
5330 : 0 : else if (Matches("TRUNCATE"))
1694 5331 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_truncatables,
5332 : : "TABLE", "ONLY");
2033 fujii@postgresql.org 5333 : 0 : else if (Matches("TRUNCATE", "TABLE"))
1694 tgl@sss.pgh.pa.us 5334 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_truncatables,
5335 : : "ONLY");
713 5336 : 0 : else if (Matches("TRUNCATE", MatchAnyN, "ONLY"))
1694 5337 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_truncatables);
2033 fujii@postgresql.org 5338 : 0 : else if (Matches("TRUNCATE", MatchAny) ||
5339 : : Matches("TRUNCATE", "TABLE|ONLY", MatchAny) ||
5340 : : Matches("TRUNCATE", "TABLE", "ONLY", MatchAny))
5341 : 0 : COMPLETE_WITH("RESTART IDENTITY", "CONTINUE IDENTITY", "CASCADE", "RESTRICT");
713 tgl@sss.pgh.pa.us 5342 : 0 : else if (Matches("TRUNCATE", MatchAnyN, "IDENTITY"))
2033 fujii@postgresql.org 5343 : 0 : COMPLETE_WITH("CASCADE", "RESTRICT");
5344 : :
5345 : : /* UNLISTEN */
2921 tgl@sss.pgh.pa.us 5346 : 0 : else if (Matches("UNLISTEN"))
1694 5347 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_channels, "*");
5348 : :
5349 : : /* UPDATE --- can be inside EXPLAIN, RULE, etc */
5350 : : /* If prev. word is UPDATE suggest a list of tables */
2921 5351 : 0 : else if (TailMatches("UPDATE"))
1694 5352 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables);
5353 : : /* Complete UPDATE <table> with "SET" */
2921 5354 : 0 : else if (TailMatches("UPDATE", MatchAny))
5 peter@eisentraut.org 5355 : 0 : COMPLETE_WITH("SET");
5356 : : /* Complete UPDATE <table> SET with list of attributes */
2921 tgl@sss.pgh.pa.us 5357 : 0 : else if (TailMatches("UPDATE", MatchAny, "SET"))
1694 5358 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
5359 : : /* UPDATE <table> SET <attr> = */
2626 tmunro@postgresql.or 5360 : 0 : else if (TailMatches("UPDATE", MatchAny, "SET", MatchAnyExcept("*=")))
2921 tgl@sss.pgh.pa.us 5361 : 0 : COMPLETE_WITH("=");
5362 : :
5363 : : /* USER MAPPING */
5364 : 0 : else if (Matches("ALTER|CREATE|DROP", "USER", "MAPPING"))
5365 : 0 : COMPLETE_WITH("FOR");
5366 : 0 : else if (Matches("CREATE", "USER", "MAPPING", "FOR"))
1694 5367 : 0 : COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
5368 : : "CURRENT_ROLE",
5369 : : "CURRENT_USER",
5370 : : "PUBLIC",
5371 : : "USER");
2921 5372 : 0 : else if (Matches("ALTER|DROP", "USER", "MAPPING", "FOR"))
6484 peter_e@gmx.net 5373 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
2921 tgl@sss.pgh.pa.us 5374 : 0 : else if (Matches("CREATE|ALTER|DROP", "USER", "MAPPING", "FOR", MatchAny))
5375 : 0 : COMPLETE_WITH("SERVER");
5376 : 0 : else if (Matches("CREATE|ALTER", "USER", "MAPPING", "FOR", MatchAny, "SERVER", MatchAny))
5377 : 0 : COMPLETE_WITH("OPTIONS");
5378 : :
5379 : : /*
5380 : : * VACUUM [ ( option [, ...] ) ] [ [ ONLY ] table_and_columns [, ...] ]
5381 : : * VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ [ ONLY ] table_and_columns [, ...] ]
5382 : : */
5383 : 0 : else if (Matches("VACUUM"))
1694 5384 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5385 : : "(",
5386 : : "FULL",
5387 : : "FREEZE",
5388 : : "VERBOSE",
5389 : : "ANALYZE",
5390 : : "ONLY");
2921 5391 : 0 : else if (HeadMatches("VACUUM", "(*") &&
5392 [ # # ]: 0 : !HeadMatches("VACUUM", "(*)"))
5393 : : {
5394 : : /*
5395 : : * This fires if we're in an unfinished parenthesized option list.
5396 : : * get_previous_words treats a completed parenthesized option list as
5397 : : * one word, so the above test is correct.
5398 : : */
5399 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
5400 : 0 : COMPLETE_WITH("FULL", "FREEZE", "ANALYZE", "VERBOSE",
5401 : : "DISABLE_PAGE_SKIPPING", "SKIP_LOCKED",
5402 : : "INDEX_CLEANUP", "PROCESS_MAIN", "PROCESS_TOAST",
5403 : : "TRUNCATE", "PARALLEL", "SKIP_DATABASE_STATS",
5404 : : "ONLY_DATABASE_STATS", "BUFFER_USAGE_LIMIT");
1294 michael@paquier.xyz 5405 [ # # ]: 0 : else if (TailMatches("FULL|FREEZE|ANALYZE|VERBOSE|DISABLE_PAGE_SKIPPING|SKIP_LOCKED|PROCESS_MAIN|PROCESS_TOAST|TRUNCATE|SKIP_DATABASE_STATS|ONLY_DATABASE_STATS"))
2732 rhaas@postgresql.org 5406 : 0 : COMPLETE_WITH("ON", "OFF");
1920 pg@bowt.ie 5407 [ # # ]: 0 : else if (TailMatches("INDEX_CLEANUP"))
5408 : 0 : COMPLETE_WITH("AUTO", "ON", "OFF");
5409 : : }
270 msawada@postgresql.o 5410 : 0 : else if (Matches("VACUUM", "(*)"))
5411 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5412 : : "ONLY");
5413 : 0 : else if (Matches("VACUUM", "FULL"))
5414 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5415 : : "FREEZE",
5416 : : "VERBOSE",
5417 : : "ANALYZE",
5418 : : "ONLY");
5419 : 0 : else if (Matches("VACUUM", MatchAnyN, "FREEZE"))
5420 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5421 : : "VERBOSE",
5422 : : "ANALYZE",
5423 : : "ONLY");
5424 : 0 : else if (Matches("VACUUM", MatchAnyN, "VERBOSE"))
5425 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5426 : : "ANALYZE",
5427 : : "ONLY");
5428 : 0 : else if (Matches("VACUUM", MatchAnyN, "ANALYZE"))
5429 : 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_vacuumables,
5430 : : "ONLY");
713 tgl@sss.pgh.pa.us 5431 : 0 : else if (Matches("VACUUM", MatchAnyN, "("))
5432 : : /* "VACUUM (" should be caught above, so assume we want columns */
1694 5433 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
2921 5434 : 0 : else if (HeadMatches("VACUUM"))
1694 5435 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables);
5436 : :
5437 : : /*
5438 : : * WAIT FOR LSN '<lsn>' [ WITH ( option [, ...] ) ]
5439 : : * where option can be:
5440 : : * MODE '<mode>'
5441 : : * TIMEOUT '<timeout>'
5442 : : * NO_THROW
5443 : : * and mode can be:
5444 : : * standby_replay | standby_write | standby_flush | primary_flush
5445 : : */
312 akorotkov@postgresql 5446 : 0 : else if (Matches("WAIT"))
5447 : 0 : COMPLETE_WITH("FOR");
5448 : 0 : else if (Matches("WAIT", "FOR"))
5449 : 0 : COMPLETE_WITH("LSN");
5450 : 0 : else if (Matches("WAIT", "FOR", "LSN"))
5451 : : /* No completion for LSN value - user must provide manually */
5452 : : ;
5453 : 0 : else if (Matches("WAIT", "FOR", "LSN", MatchAny))
5454 : 0 : COMPLETE_WITH("WITH");
5455 : 0 : else if (Matches("WAIT", "FOR", "LSN", MatchAny, "WITH"))
5456 : 0 : COMPLETE_WITH("(");
5457 : :
5458 : : /*
5459 : : * Handle parenthesized option list. This fires when we're in an
5460 : : * unfinished parenthesized option list. get_previous_words treats a
5461 : : * completed parenthesized option list as one word, so the above test is
5462 : : * correct.
5463 : : *
5464 : : * 'mode' takes a string value (one of the listed above), 'timeout' takes
5465 : : * a string value, and 'no_throw' takes no value. We do not offer
5466 : : * completions for the *values* of 'timeout' or 'no_throw'.
5467 : : */
5468 : 0 : else if (HeadMatches("WAIT", "FOR", "LSN", MatchAny, "WITH", "(*") &&
5469 [ # # ]: 0 : !HeadMatches("WAIT", "FOR", "LSN", MatchAny, "WITH", "(*)"))
5470 : : {
5471 [ # # # # ]: 0 : if (ends_with(prev_wd, '(') || ends_with(prev_wd, ','))
258 5472 : 0 : COMPLETE_WITH("mode", "timeout", "no_throw");
5473 [ # # ]: 0 : else if (TailMatches("mode"))
5474 : 0 : COMPLETE_WITH("'standby_replay'", "'standby_write'", "'standby_flush'", "'primary_flush'");
5475 : : }
5476 : :
5477 : : /* WITH [RECURSIVE] */
5478 : :
5479 : : /*
5480 : : * Only match when WITH is the first word, as WITH may appear in many
5481 : : * other contexts.
5482 : : */
2921 tgl@sss.pgh.pa.us 5483 : 0 : else if (Matches("WITH"))
5484 : 0 : COMPLETE_WITH("RECURSIVE");
5485 : :
5486 : : /* WHERE */
5487 : : /* Simple case of the word before the where being the table name */
5488 : 0 : else if (TailMatches(MatchAny, "WHERE"))
1694 5489 : 0 : COMPLETE_WITH_ATTR(prev2_wd);
5490 : :
5491 : : /* ... FROM ... */
5492 : : /* TODO: also include SRF ? */
2921 tgl@sss.pgh.pa.us 5493 [ + - ]:CBC 14 : else if (TailMatches("FROM") && !Matches("COPY|\\copy", MatchAny, "FROM"))
1694 5494 : 14 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables);
5495 : :
5496 : : /* ... JOIN ... */
2921 5497 : 14 : else if (TailMatches("JOIN"))
643 tomas.vondra@postgre 5498 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_selectables, "LATERAL");
5499 [ # # ]: 0 : else if (TailMatches("JOIN", MatchAny) && !TailMatches("CROSS|NATURAL", "JOIN", MatchAny))
5500 : 0 : COMPLETE_WITH("ON", "USING (");
5501 : 0 : else if (TailMatches("JOIN", MatchAny, MatchAny) &&
5502 [ # # # # ]: 0 : !TailMatches("CROSS|NATURAL", "JOIN", MatchAny, MatchAny) && !TailMatches("ON|USING"))
5503 : 0 : COMPLETE_WITH("ON", "USING (");
5504 : 0 : else if (TailMatches("JOIN", "LATERAL", MatchAny, MatchAny) &&
5505 [ # # # # ]: 0 : !TailMatches("CROSS|NATURAL", "JOIN", "LATERAL", MatchAny, MatchAny) && !TailMatches("ON|USING"))
5506 : 0 : COMPLETE_WITH("ON", "USING (");
5507 : 0 : else if (TailMatches("JOIN", MatchAny, "USING") ||
5508 : : TailMatches("JOIN", MatchAny, MatchAny, "USING") ||
5509 : : TailMatches("JOIN", "LATERAL", MatchAny, MatchAny, "USING"))
5510 : 0 : COMPLETE_WITH("(");
5511 : 0 : else if (TailMatches("JOIN", MatchAny, "USING", "("))
5512 : 0 : COMPLETE_WITH_ATTR(prev3_wd);
5513 : 0 : else if (TailMatches("JOIN", MatchAny, MatchAny, "USING", "("))
5514 : 0 : COMPLETE_WITH_ATTR(prev4_wd);
5515 : :
5516 : : /* ... AT [ LOCAL | TIME ZONE ] ... */
1073 michael@paquier.xyz 5517 : 0 : else if (TailMatches("AT"))
5518 : 0 : COMPLETE_WITH("LOCAL", "TIME ZONE");
5519 : 0 : else if (TailMatches("AT", "TIME", "ZONE"))
5520 [ # # # # : 0 : COMPLETE_WITH_TIMEZONE_NAME();
# # ]
5521 : :
5522 : : /* Backslash commands */
5523 : : /* TODO: \dc \dd \dl */
2921 tgl@sss.pgh.pa.us 5524 : 0 : else if (TailMatchesCS("\\?"))
5525 : 0 : COMPLETE_WITH_CS("commands", "options", "variables");
5526 : 0 : else if (TailMatchesCS("\\connect|\\c"))
5527 : : {
4189 alvherre@alvh.no-ip. 5528 [ # # ]: 0 : if (!recognized_connection_string(text))
5529 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
5530 : : }
2921 tgl@sss.pgh.pa.us 5531 : 0 : else if (TailMatchesCS("\\connect|\\c", MatchAny))
5532 : : {
4189 alvherre@alvh.no-ip. 5533 [ # # ]: 0 : if (!recognized_connection_string(prev_wd))
5534 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
5535 : : }
2921 tgl@sss.pgh.pa.us 5536 : 0 : else if (TailMatchesCS("\\da*"))
1694 5537 : 0 : COMPLETE_WITH_VERSIONED_SCHEMA_QUERY(Query_for_list_of_aggregates);
2387 akorotkov@postgresql 5538 : 0 : else if (TailMatchesCS("\\dAc*", MatchAny) ||
5539 : : TailMatchesCS("\\dAf*", MatchAny))
1694 tgl@sss.pgh.pa.us 5540 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2387 akorotkov@postgresql 5541 : 0 : else if (TailMatchesCS("\\dAo*", MatchAny) ||
5542 : : TailMatchesCS("\\dAp*", MatchAny))
1694 tgl@sss.pgh.pa.us 5543 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_operator_families);
2921 5544 : 0 : else if (TailMatchesCS("\\dA*"))
3757 alvherre@alvh.no-ip. 5545 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
2921 tgl@sss.pgh.pa.us 5546 : 0 : else if (TailMatchesCS("\\db*"))
8066 bruce@momjian.us 5547 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
1627 tgl@sss.pgh.pa.us 5548 : 0 : else if (TailMatchesCS("\\dconfig*"))
5549 : 0 : COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_show_vars);
2921 5550 : 0 : else if (TailMatchesCS("\\dD*"))
1694 5551 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains);
2921 5552 : 0 : else if (TailMatchesCS("\\des*"))
6484 peter_e@gmx.net 5553 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_servers);
2921 tgl@sss.pgh.pa.us 5554 : 0 : else if (TailMatchesCS("\\deu*"))
6484 peter_e@gmx.net 5555 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
2921 tgl@sss.pgh.pa.us 5556 : 0 : else if (TailMatchesCS("\\dew*"))
6484 peter_e@gmx.net 5557 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
2921 tgl@sss.pgh.pa.us 5558 : 0 : else if (TailMatchesCS("\\df*"))
79 fujii@postgresql.org 5559 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
1991 tgl@sss.pgh.pa.us 5560 : 0 : else if (HeadMatchesCS("\\df*"))
1694 5561 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
5562 : :
2921 5563 : 0 : else if (TailMatchesCS("\\dFd*"))
1694 5564 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_dictionaries);
2921 5565 : 0 : else if (TailMatchesCS("\\dFp*"))
1694 5566 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_parsers);
2921 5567 : 0 : else if (TailMatchesCS("\\dFt*"))
1694 5568 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_templates);
5569 : : /* must be at end of \dF alternatives: */
2921 5570 : 0 : else if (TailMatchesCS("\\dF*"))
1694 5571 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_ts_configurations);
5572 : :
2921 5573 : 0 : else if (TailMatchesCS("\\di*"))
1694 5574 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes);
2921 5575 : 0 : else if (TailMatchesCS("\\dL*"))
5722 rhaas@postgresql.org 5576 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_languages);
2921 tgl@sss.pgh.pa.us 5577 : 0 : else if (TailMatchesCS("\\dn*"))
8578 bruce@momjian.us 5578 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
5579 : : /* no support for completing operators, but we can complete types: */
1991 tgl@sss.pgh.pa.us 5580 : 0 : else if (HeadMatchesCS("\\do*", MatchAny))
1694 5581 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
2921 5582 : 0 : else if (TailMatchesCS("\\dp") || TailMatchesCS("\\z"))
1694 5583 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables);
2723 alvherre@alvh.no-ip. 5584 : 0 : else if (TailMatchesCS("\\dPi*"))
1694 tgl@sss.pgh.pa.us 5585 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_indexes);
2723 alvherre@alvh.no-ip. 5586 : 0 : else if (TailMatchesCS("\\dPt*"))
1694 tgl@sss.pgh.pa.us 5587 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_tables);
2723 alvherre@alvh.no-ip. 5588 : 0 : else if (TailMatchesCS("\\dP*"))
1694 tgl@sss.pgh.pa.us 5589 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_partitioned_relations);
1446 michael@paquier.xyz 5590 : 0 : else if (TailMatchesCS("\\dRp*"))
80 nathan@postgresql.or 5591 :UNC 0 : COMPLETE_WITH_QUERY(Query_for_list_of_publications);
1446 michael@paquier.xyz 5592 :UBC 0 : else if (TailMatchesCS("\\dRs*"))
80 nathan@postgresql.or 5593 :UNC 0 : COMPLETE_WITH_QUERY(Query_for_list_of_subscriptions);
2921 tgl@sss.pgh.pa.us 5594 :UBC 0 : else if (TailMatchesCS("\\ds*"))
1694 5595 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences);
2921 5596 : 0 : else if (TailMatchesCS("\\dt*"))
1694 5597 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
2921 5598 : 0 : else if (TailMatchesCS("\\dT*"))
1694 5599 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes);
1159 5600 : 0 : else if (TailMatchesCS("\\du*") ||
5601 : : TailMatchesCS("\\dg*") ||
5602 : : TailMatchesCS("\\drg*"))
7707 5603 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2921 5604 : 0 : else if (TailMatchesCS("\\dv*"))
1694 5605 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
2921 5606 : 0 : else if (TailMatchesCS("\\dx*"))
4784 magnus@hagander.net 5607 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_extensions);
2069 tomas.vondra@postgre 5608 : 0 : else if (TailMatchesCS("\\dX*"))
1694 tgl@sss.pgh.pa.us 5609 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_statistics);
2921 5610 : 0 : else if (TailMatchesCS("\\dm*"))
1694 5611 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_matviews);
2921 5612 : 0 : else if (TailMatchesCS("\\dE*"))
1694 5613 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables);
2921 5614 : 0 : else if (TailMatchesCS("\\dy*"))
4093 fujii@postgresql.org 5615 : 0 : COMPLETE_WITH_QUERY(Query_for_list_of_event_triggers);
5616 : :
5617 : : /* must be at end of \d alternatives: */
2921 tgl@sss.pgh.pa.us 5618 : 0 : else if (TailMatchesCS("\\d*"))
1694 5619 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations);
5620 : :
2921 5621 : 0 : else if (TailMatchesCS("\\ef"))
1694 5622 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
2921 5623 : 0 : else if (TailMatchesCS("\\ev"))
1694 5624 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
5625 : :
2921 5626 : 0 : else if (TailMatchesCS("\\encoding"))
1694 5627 : 0 : COMPLETE_WITH_QUERY_VERBATIM(Query_for_list_of_encodings);
2921 5628 : 0 : else if (TailMatchesCS("\\h|\\help"))
9657 bruce@momjian.us 5629 : 0 : COMPLETE_WITH_LIST(sql_commands);
2921 tgl@sss.pgh.pa.us 5630 : 0 : else if (TailMatchesCS("\\h|\\help", MatchAny))
5631 : : {
5632 [ # # ]: 0 : if (TailMatches("DROP"))
360 msawada@postgresql.o 5633 : 0 : COMPLETE_WITH_GENERATOR(drop_command_generator);
2921 tgl@sss.pgh.pa.us 5634 [ # # ]: 0 : else if (TailMatches("ALTER"))
360 msawada@postgresql.o 5635 : 0 : COMPLETE_WITH_GENERATOR(alter_command_generator);
5636 : :
5637 : : /*
5638 : : * CREATE is recognized by tail match elsewhere, so doesn't need to be
5639 : : * repeated here
5640 : : */
5641 : : }
2921 tgl@sss.pgh.pa.us 5642 : 0 : else if (TailMatchesCS("\\h|\\help", MatchAny, MatchAny))
5643 : : {
5644 [ # # ]: 0 : if (TailMatches("CREATE|DROP", "ACCESS"))
5645 : 0 : COMPLETE_WITH("METHOD");
5646 [ # # ]: 0 : else if (TailMatches("ALTER", "DEFAULT"))
5647 : 0 : COMPLETE_WITH("PRIVILEGES");
5648 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "EVENT"))
5649 : 0 : COMPLETE_WITH("TRIGGER");
5650 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "FOREIGN"))
5651 : 0 : COMPLETE_WITH("DATA WRAPPER", "TABLE");
5652 [ # # ]: 0 : else if (TailMatches("ALTER", "LARGE"))
5653 : 0 : COMPLETE_WITH("OBJECT");
5654 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "MATERIALIZED"))
5655 : 0 : COMPLETE_WITH("VIEW");
5656 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "TEXT"))
5657 : 0 : COMPLETE_WITH("SEARCH");
5658 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "USER"))
5659 : 0 : COMPLETE_WITH("MAPPING FOR");
5660 : : }
5661 : 0 : else if (TailMatchesCS("\\h|\\help", MatchAny, MatchAny, MatchAny))
5662 : : {
5663 [ # # ]: 0 : if (TailMatches("CREATE|ALTER|DROP", "FOREIGN", "DATA"))
5664 : 0 : COMPLETE_WITH("WRAPPER");
5665 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "TEXT", "SEARCH"))
5666 : 0 : COMPLETE_WITH("CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE");
5667 [ # # ]: 0 : else if (TailMatches("CREATE|ALTER|DROP", "USER", "MAPPING"))
5668 : 0 : COMPLETE_WITH("FOR");
5669 : : }
2921 tgl@sss.pgh.pa.us 5670 [ - + ]:CBC 2 : else if (TailMatchesCS("\\l*") && !TailMatchesCS("\\lo*"))
3685 tgl@sss.pgh.pa.us 5671 :UBC 0 : COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2921 tgl@sss.pgh.pa.us 5672 :CBC 2 : else if (TailMatchesCS("\\password"))
7581 peter_e@gmx.net 5673 :UBC 0 : COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2921 tgl@sss.pgh.pa.us 5674 : 0 : else if (TailMatchesCS("\\pset"))
321 alvherre@kurilemu.de 5675 : 0 : COMPLETE_WITH_CS("border", "columns", "csv_fieldsep",
5676 : : "display_false", "display_true", "expanded",
5677 : : "fieldsep", "fieldsep_zero", "footer", "format",
5678 : : "linestyle", "null", "numericlocale",
5679 : : "pager", "pager_min_lines",
5680 : : "recordsep", "recordsep_zero",
5681 : : "tableattr", "title", "tuples_only",
5682 : : "unicode_border_linestyle",
5683 : : "unicode_column_linestyle",
5684 : : "unicode_header_linestyle",
5685 : : "xheader_width");
2921 tgl@sss.pgh.pa.us 5686 : 0 : else if (TailMatchesCS("\\pset", MatchAny))
5687 : : {
5688 [ # # ]: 0 : if (TailMatchesCS("format"))
2855 5689 : 0 : COMPLETE_WITH_CS("aligned", "asciidoc", "csv", "html", "latex",
5690 : : "latex-longtable", "troff-ms", "unaligned",
5691 : : "wrapped");
1518 andrew@dunslane.net 5692 [ # # ]: 0 : else if (TailMatchesCS("xheader_width"))
5693 : 0 : COMPLETE_WITH_CS("full", "column", "page");
2921 tgl@sss.pgh.pa.us 5694 [ # # ]: 0 : else if (TailMatchesCS("linestyle"))
5695 : 0 : COMPLETE_WITH_CS("ascii", "old-ascii", "unicode");
5696 [ # # ]: 0 : else if (TailMatchesCS("pager"))
5697 : 0 : COMPLETE_WITH_CS("on", "off", "always");
5698 [ # # ]: 0 : else if (TailMatchesCS("unicode_border_linestyle|"
5699 : : "unicode_column_linestyle|"
5700 : : "unicode_header_linestyle"))
5701 : 0 : COMPLETE_WITH_CS("single", "double");
5702 : : }
5703 : 0 : else if (TailMatchesCS("\\unset"))
4422 fujii@postgresql.org 5704 : 0 : matches = complete_from_variables(text, "", "", true);
2921 tgl@sss.pgh.pa.us 5705 : 0 : else if (TailMatchesCS("\\set"))
4422 fujii@postgresql.org 5706 :CBC 1 : matches = complete_from_variables(text, "", "", false);
2921 tgl@sss.pgh.pa.us 5707 : 1 : else if (TailMatchesCS("\\set", MatchAny))
5708 : : {
1630 peter@eisentraut.org 5709 [ - + ]: 1 : if (TailMatchesCS("AUTOCOMMIT|ON_ERROR_STOP|QUIET|SHOW_ALL_RESULTS|"
5710 : : "SINGLELINE|SINGLESTEP"))
2921 tgl@sss.pgh.pa.us 5711 :UBC 0 : COMPLETE_WITH_CS("on", "off");
2921 tgl@sss.pgh.pa.us 5712 [ - + ]:CBC 1 : else if (TailMatchesCS("COMP_KEYWORD_CASE"))
2921 tgl@sss.pgh.pa.us 5713 :UBC 0 : COMPLETE_WITH_CS("lower", "upper",
5714 : : "preserve-lower", "preserve-upper");
2921 tgl@sss.pgh.pa.us 5715 [ - + ]:CBC 1 : else if (TailMatchesCS("ECHO"))
2921 tgl@sss.pgh.pa.us 5716 :UBC 0 : COMPLETE_WITH_CS("errors", "queries", "all", "none");
2921 tgl@sss.pgh.pa.us 5717 [ - + ]:CBC 1 : else if (TailMatchesCS("ECHO_HIDDEN"))
2921 tgl@sss.pgh.pa.us 5718 :UBC 0 : COMPLETE_WITH_CS("noexec", "off", "on");
2921 tgl@sss.pgh.pa.us 5719 [ - + ]:CBC 1 : else if (TailMatchesCS("HISTCONTROL"))
2921 tgl@sss.pgh.pa.us 5720 :UBC 0 : COMPLETE_WITH_CS("ignorespace", "ignoredups",
5721 : : "ignoreboth", "none");
2921 tgl@sss.pgh.pa.us 5722 [ - + ]:CBC 1 : else if (TailMatchesCS("ON_ERROR_ROLLBACK"))
2921 tgl@sss.pgh.pa.us 5723 :UBC 0 : COMPLETE_WITH_CS("on", "off", "interactive");
2921 tgl@sss.pgh.pa.us 5724 [ - + ]:CBC 1 : else if (TailMatchesCS("SHOW_CONTEXT"))
2921 tgl@sss.pgh.pa.us 5725 :UBC 0 : COMPLETE_WITH_CS("never", "errors", "always");
2921 tgl@sss.pgh.pa.us 5726 [ + - ]:CBC 1 : else if (TailMatchesCS("VERBOSITY"))
2726 5727 : 1 : COMPLETE_WITH_CS("default", "verbose", "terse", "sqlstate");
5728 : : }
2921 5729 : 1 : else if (TailMatchesCS("\\sf*"))
1694 tgl@sss.pgh.pa.us 5730 :UBC 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines);
2921 5731 : 0 : else if (TailMatchesCS("\\sv*"))
1694 5732 : 0 : COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views);
2364 bruce@momjian.us 5733 : 0 : else if (TailMatchesCS("\\cd|\\e|\\edit|\\g|\\gx|\\i|\\include|"
5734 : : "\\ir|\\include_relative|\\o|\\out|"
5735 : : "\\s|\\w|\\write|\\lo_import") ||
5736 : : TailMatchesCS("\\lo_export", MatchAny))
360 msawada@postgresql.o 5737 :CBC 2 : COMPLETE_WITH_FILES("\\", false);
5738 : :
5739 : : /* gen_tabcomplete.pl ends special processing here */
713 tgl@sss.pgh.pa.us 5740 : 2 : /* END GEN_TABCOMPLETE */
7615 bruce@momjian.us 5741 :UBC 0 :
7615 bruce@momjian.us 5742 :CBC 58 : return matches;
5743 : : }
5744 : :
5745 : :
5746 : : /*
5747 : : * GENERATOR FUNCTIONS
5748 : : *
5749 : : * These functions do all the actual work of completing the input. They get
5750 : : * passed the text so far and the count how many times they have been called
5751 : : * so far with the same text.
5752 : : * If you read the above carefully, you'll see that these don't get called
5753 : : * directly but through the readline interface.
5754 : : * The return value is expected to be the full completion of the text, going
5755 : : * through a list each time, or NULL if there are no more matches. The string
5756 : : * will be free()'d by readline, so you must run it through strdup() or
5757 : : * something of that sort.
5758 : : */
5759 : :
5760 : : /*
5761 : : * Common routine for create_command_generator and drop_command_generator.
5762 : : * Entries that have 'excluded' flags are not returned.
5763 : : */
5764 : : static char *
174 nathan@postgresql.or 5765 : 4 : create_or_drop_command_generator(const char *text, int state, uint32 excluded)
5766 : : {
5767 : : static int list_index,
5768 : : string_length;
5769 : : const char *name;
5770 : :
5771 : : /* If this is the first time for this completion, init some values */
7615 bruce@momjian.us 5772 [ + + ]: 4 : if (state == 0)
5773 : : {
5774 : 2 : list_index = 0;
5775 : 2 : string_length = strlen(text);
5776 : : }
5777 : :
5778 : : /* find something that matches */
5779 [ + + ]: 102 : while ((name = words_after_create[list_index++].name))
5780 : : {
5824 tgl@sss.pgh.pa.us 5781 [ + + ]: 100 : if ((pg_strncasecmp(name, text, string_length) == 0) &&
5687 itagaki.takahiro@gma 5782 [ + - ]: 2 : !(words_after_create[list_index - 1].flags & excluded))
5248 peter_e@gmx.net 5783 : 2 : return pg_strdup_keyword_case(name, text);
5784 : : }
5785 : : /* if nothing matches, return NULL */
7615 bruce@momjian.us 5786 : 2 : return NULL;
5787 : : }
5788 : :
5789 : : /*
5790 : : * This one gives you one from a list of things you can put after CREATE
5791 : : * as defined above.
5792 : : */
5793 : : static char *
5687 itagaki.takahiro@gma 5794 : 2 : create_command_generator(const char *text, int state)
5795 : : {
5796 : 2 : return create_or_drop_command_generator(text, state, THING_NO_CREATE);
5797 : : }
5798 : :
5799 : : /*
5800 : : * This function gives you a list of things you can put after a DROP command.
5801 : : */
5802 : : static char *
7476 alvherre@alvh.no-ip. 5803 : 2 : drop_command_generator(const char *text, int state)
5804 : : {
5687 itagaki.takahiro@gma 5805 : 2 : return create_or_drop_command_generator(text, state, THING_NO_DROP);
5806 : : }
5807 : :
5808 : : /*
5809 : : * This function gives you a list of things you can put after an ALTER command.
5810 : : */
5811 : : static char *
3475 peter_e@gmx.net 5812 :UBC 0 : alter_command_generator(const char *text, int state)
5813 : : {
5814 : 0 : return create_or_drop_command_generator(text, state, THING_NO_ALTER);
5815 : : }
5816 : :
5817 : : /*
5818 : : * These functions generate lists using server queries.
5819 : : * They are all wrappers for _complete_from_query.
5820 : : */
5821 : :
5822 : : static char *
7615 bruce@momjian.us 5823 :CBC 188 : complete_from_query(const char *text, int state)
5824 : : {
5825 : : /* query is assumed to work for any server version */
1694 tgl@sss.pgh.pa.us 5826 : 188 : return _complete_from_query(completion_charp, NULL, completion_charpp,
5827 : : completion_verbatim, text, state);
5828 : : }
5829 : :
5830 : : static char *
3121 tgl@sss.pgh.pa.us 5831 :LBC (2) : complete_from_versioned_query(const char *text, int state)
5832 : : {
5833 : (2) : const VersionedQuery *vquery = completion_vquery;
5834 : :
5835 : : /* Find appropriate array element */
5836 [ # # ]: (2) : while (pset.sversion < vquery->min_server_version)
3121 tgl@sss.pgh.pa.us 5837 :UBC 0 : vquery++;
5838 : : /* Fail completion if server is too old */
3121 tgl@sss.pgh.pa.us 5839 [ # # ]:LBC (2) : if (vquery->query == NULL)
3121 tgl@sss.pgh.pa.us 5840 :UBC 0 : return NULL;
5841 : :
1694 tgl@sss.pgh.pa.us 5842 :LBC (2) : return _complete_from_query(vquery->query, NULL, completion_charpp,
5843 : : completion_verbatim, text, state);
5844 : : }
5845 : :
5846 : : static char *
7615 bruce@momjian.us 5847 :CBC 86 : complete_from_schema_query(const char *text, int state)
5848 : : {
5849 : : /* query is assumed to work for any server version */
1694 tgl@sss.pgh.pa.us 5850 : 86 : return _complete_from_query(NULL, completion_squery, completion_charpp,
5851 : : completion_verbatim, text, state);
5852 : : }
5853 : :
5854 : : static char *
3121 5855 : 8 : complete_from_versioned_schema_query(const char *text, int state)
5856 : : {
5857 : 8 : const SchemaQuery *squery = completion_squery;
5858 : :
5859 : : /* Find appropriate array element */
5860 [ - + ]: 8 : while (pset.sversion < squery->min_server_version)
3121 tgl@sss.pgh.pa.us 5861 :UBC 0 : squery++;
5862 : : /* Fail completion if server is too old */
3121 tgl@sss.pgh.pa.us 5863 [ - + ]:CBC 8 : if (squery->catname == NULL)
3121 tgl@sss.pgh.pa.us 5864 :UBC 0 : return NULL;
5865 : :
1694 tgl@sss.pgh.pa.us 5866 :CBC 8 : return _complete_from_query(NULL, squery, completion_charpp,
5867 : : completion_verbatim, text, state);
5868 : : }
5869 : :
5870 : :
5871 : : /*
5872 : : * This creates a list of matching things, according to a query described by
5873 : : * the initial arguments. The caller has already done any work needed to
5874 : : * select the appropriate query for the server's version.
5875 : : *
5876 : : * The query can be one of two kinds:
5877 : : *
5878 : : * 1. A simple query, which must contain a restriction clause of the form
5879 : : * output LIKE '%s'
5880 : : * where "output" is the same string that the query returns. The %s
5881 : : * will be replaced by a LIKE pattern to match the already-typed text.
5882 : : * There can be a second '%s', which will be replaced by a suitably-escaped
5883 : : * version of the string provided in completion_ref_object. If there is a
5884 : : * third '%s', it will be replaced by a suitably-escaped version of the string
5885 : : * provided in completion_ref_schema. Those strings should be set up
5886 : : * by calling set_completion_reference or set_completion_reference_verbatim.
5887 : : * Simple queries should return a single column of matches. If "verbatim"
5888 : : * is true, the matches are returned as-is; otherwise, they are taken to
5889 : : * be SQL identifiers and quoted if necessary.
5890 : : *
5891 : : * 2. A schema query used for completion of both schema and relation names.
5892 : : * This is represented by a SchemaQuery object; see that typedef for details.
5893 : : *
5894 : : * See top of file for examples of both kinds of query.
5895 : : *
5896 : : * In addition to the query itself, we accept a null-terminated array of
5897 : : * literal keywords, which will be returned if they match the input-so-far
5898 : : * (case insensitively). (These are in addition to keywords specified
5899 : : * within the schema_query, if any.)
5900 : : *
5901 : : * If "verbatim" is true, then we use the given text as-is to match the
5902 : : * query results; otherwise we parse it as a possibly-qualified identifier,
5903 : : * and reconstruct suitable quoting afterward.
5904 : : *
5905 : : * "text" and "state" are supplied by Readline. "text" is the word we are
5906 : : * trying to complete. "state" is zero on first call, nonzero later.
5907 : : *
5908 : : * readline will call this repeatedly with the same text and varying
5909 : : * state. On each call, we are supposed to return a malloc'd string
5910 : : * that is a candidate completion. Return NULL when done.
5911 : : */
5912 : : static char *
3121 5913 : 282 : _complete_from_query(const char *simple_query,
5914 : : const SchemaQuery *schema_query,
5915 : : const char *const *keywords,
5916 : : bool verbatim,
5917 : : const char *text, int state)
5918 : : {
5919 : : static int list_index,
5920 : : num_schema_only,
5921 : : num_query_other,
5922 : : num_keywords;
5923 : : static PGresult *result = NULL;
5924 : : static bool non_empty_object;
5925 : : static bool schemaquoted;
5926 : : static bool objectquoted;
5927 : :
5928 : : /*
5929 : : * If this is the first time for this completion, we fetch a list of our
5930 : : * "things" from the backend.
5931 : : */
7615 bruce@momjian.us 5932 [ + + ]: 282 : if (state == 0)
5933 : : {
5934 : : PQExpBufferData query_buffer;
5935 : : char *schemaname;
5936 : : char *objectname;
5937 : : char *e_object_like;
5938 : : char *e_schemaname;
5939 : : char *e_ref_object;
5940 : : char *e_ref_schema;
5941 : :
5942 : : /* Reset static state, ensuring no memory leaks */
5943 : 44 : list_index = 0;
1694 tgl@sss.pgh.pa.us 5944 : 44 : num_schema_only = 0;
1692 5945 : 44 : num_query_other = 0;
5946 : 44 : num_keywords = 0;
1694 5947 : 44 : PQclear(result);
5948 : 44 : result = NULL;
5949 : :
5950 : : /* Parse text, splitting into schema and object name if needed */
5951 [ + + ]: 44 : if (verbatim)
5952 : : {
5953 : 8 : objectname = pg_strdup(text);
5954 : 8 : schemaname = NULL;
5955 : : }
5956 : : else
5957 : : {
5958 : 36 : parse_identifier(text,
5959 : : &schemaname, &objectname,
5960 : : &schemaquoted, &objectquoted);
5961 : : }
5962 : :
5963 : : /* Remember whether the user has typed anything in the object part */
5964 : 44 : non_empty_object = (*objectname != '\0');
5965 : :
5966 : : /*
5967 : : * Convert objectname to a LIKE prefix pattern (e.g. 'foo%'), and set
5968 : : * up suitably-escaped copies of all the strings we need.
5969 : : */
5970 : 44 : e_object_like = make_like_pattern(objectname);
5971 : :
5972 [ + + ]: 44 : if (schemaname)
5973 : 3 : e_schemaname = escape_string(schemaname);
5974 : : else
5975 : 41 : e_schemaname = NULL;
5976 : :
5977 [ + + ]: 44 : if (completion_ref_object)
5978 : 21 : e_ref_object = escape_string(completion_ref_object);
5979 : : else
5980 : 23 : e_ref_object = NULL;
5981 : :
5982 [ + + ]: 44 : if (completion_ref_schema)
5983 : 1 : e_ref_schema = escape_string(completion_ref_schema);
5984 : : else
5985 : 43 : e_ref_schema = NULL;
5986 : :
7615 bruce@momjian.us 5987 : 44 : initPQExpBuffer(&query_buffer);
5988 : :
3121 tgl@sss.pgh.pa.us 5989 [ + + ]: 44 : if (schema_query)
5990 : : {
1694 5991 [ - + ]: 36 : Assert(simple_query == NULL);
5992 : :
5993 : : /*
5994 : : * We issue different queries depending on whether the input is
5995 : : * already qualified or not. schema_query gives us the pieces to
5996 : : * assemble.
5997 : : */
5998 [ + + - + ]: 36 : if (schemaname == NULL || schema_query->namespace == NULL)
5999 : : {
6000 : : /* Get unqualified names matching the input-so-far */
6001 : 33 : appendPQExpBufferStr(&query_buffer, "SELECT ");
6002 [ - + ]: 33 : if (schema_query->use_distinct)
1694 tgl@sss.pgh.pa.us 6003 :UBC 0 : appendPQExpBufferStr(&query_buffer, "DISTINCT ");
1694 tgl@sss.pgh.pa.us 6004 :CBC 33 : appendPQExpBuffer(&query_buffer,
6005 : : "%s, NULL::pg_catalog.text FROM %s",
6006 : 33 : schema_query->result,
6007 : 33 : schema_query->catname);
6008 [ + + + + ]: 33 : if (schema_query->refnamespace && completion_ref_schema)
6009 : 1 : appendPQExpBufferStr(&query_buffer,
6010 : : ", pg_catalog.pg_namespace nr");
6011 : 33 : appendPQExpBufferStr(&query_buffer, " WHERE ");
6012 [ + - ]: 33 : if (schema_query->selcondition)
6013 : 33 : appendPQExpBuffer(&query_buffer, "%s AND ",
6014 : 33 : schema_query->selcondition);
6015 : 33 : appendPQExpBuffer(&query_buffer, "(%s) LIKE '%s'",
6016 : 33 : schema_query->result,
6017 : : e_object_like);
6018 [ + + ]: 33 : if (schema_query->viscondition)
6019 : 15 : appendPQExpBuffer(&query_buffer, " AND %s",
6020 : 15 : schema_query->viscondition);
6021 [ + + ]: 33 : if (schema_query->refname)
6022 : : {
6023 [ - + ]: 18 : Assert(completion_ref_object);
6024 : 18 : appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
6025 : 18 : schema_query->refname, e_ref_object);
6026 [ + - + + ]: 18 : if (schema_query->refnamespace && completion_ref_schema)
6027 : 1 : appendPQExpBuffer(&query_buffer,
6028 : : " AND %s = nr.oid AND nr.nspname = '%s'",
6029 : 1 : schema_query->refnamespace,
6030 : : e_ref_schema);
6031 [ + - ]: 17 : else if (schema_query->refviscondition)
6032 : 17 : appendPQExpBuffer(&query_buffer,
6033 : : " AND %s",
6034 : 17 : schema_query->refviscondition);
6035 : : }
6036 : :
6037 : : /*
6038 : : * When fetching relation names, suppress system catalogs
6039 : : * unless the input-so-far begins with "pg_". This is a
6040 : : * compromise between not offering system catalogs for
6041 : : * completion at all, and having them swamp the result when
6042 : : * the input is just "p".
6043 : : */
6044 [ + + ]: 33 : if (strcmp(schema_query->catname,
6045 : 14 : "pg_catalog.pg_class c") == 0 &&
6046 [ + - ]: 14 : strncmp(objectname, "pg_", 3) != 0)
6047 : : {
6048 : 14 : appendPQExpBufferStr(&query_buffer,
6049 : : " AND c.relnamespace <> (SELECT oid FROM"
6050 : : " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')");
6051 : : }
6052 : :
6053 : : /*
6054 : : * If the target object type can be schema-qualified, add in
6055 : : * schema names matching the input-so-far.
6056 : : */
6057 [ + + ]: 33 : if (schema_query->namespace)
6058 : : {
6059 : 15 : appendPQExpBuffer(&query_buffer, "\nUNION ALL\n"
6060 : : "SELECT NULL::pg_catalog.text, n.nspname "
6061 : : "FROM pg_catalog.pg_namespace n "
6062 : : "WHERE n.nspname LIKE '%s'",
6063 : : e_object_like);
6064 : :
6065 : : /*
6066 : : * Likewise, suppress system schemas unless the
6067 : : * input-so-far begins with "pg_".
6068 : : */
6069 [ + - ]: 15 : if (strncmp(objectname, "pg_", 3) != 0)
6070 : 15 : appendPQExpBufferStr(&query_buffer,
6071 : : " AND n.nspname NOT LIKE E'pg\\\\_%'");
6072 : :
6073 : : /*
6074 : : * Since we're matching these schema names to the object
6075 : : * name, handle their quoting using the object name's
6076 : : * quoting state.
6077 : : */
6078 : 15 : schemaquoted = objectquoted;
6079 : : }
6080 : : }
6081 : : else
6082 : : {
6083 : : /* Input is qualified, so produce only qualified names */
6084 : 3 : appendPQExpBufferStr(&query_buffer, "SELECT ");
6085 [ + + ]: 3 : if (schema_query->use_distinct)
6086 : 1 : appendPQExpBufferStr(&query_buffer, "DISTINCT ");
6087 : 3 : appendPQExpBuffer(&query_buffer, "%s, n.nspname "
6088 : : "FROM %s, pg_catalog.pg_namespace n",
6089 : 3 : schema_query->result,
6090 : 3 : schema_query->catname);
6091 [ - + - - ]: 3 : if (schema_query->refnamespace && completion_ref_schema)
1694 tgl@sss.pgh.pa.us 6092 :UBC 0 : appendPQExpBufferStr(&query_buffer,
6093 : : ", pg_catalog.pg_namespace nr");
1694 tgl@sss.pgh.pa.us 6094 :CBC 3 : appendPQExpBuffer(&query_buffer, " WHERE %s = n.oid AND ",
6095 : 3 : schema_query->namespace);
6096 [ + - ]: 3 : if (schema_query->selcondition)
6097 : 3 : appendPQExpBuffer(&query_buffer, "%s AND ",
6098 : 3 : schema_query->selcondition);
6099 : 3 : appendPQExpBuffer(&query_buffer, "(%s) LIKE '%s' AND ",
6100 : 3 : schema_query->result,
6101 : : e_object_like);
6102 : 3 : appendPQExpBuffer(&query_buffer, "n.nspname = '%s'",
6103 : : e_schemaname);
6104 [ + + ]: 3 : if (schema_query->refname)
6105 : : {
6106 [ - + ]: 1 : Assert(completion_ref_object);
6107 : 1 : appendPQExpBuffer(&query_buffer, " AND %s = '%s'",
6108 : 1 : schema_query->refname, e_ref_object);
6109 [ - + - - ]: 1 : if (schema_query->refnamespace && completion_ref_schema)
1694 tgl@sss.pgh.pa.us 6110 :UBC 0 : appendPQExpBuffer(&query_buffer,
6111 : : " AND %s = nr.oid AND nr.nspname = '%s'",
6112 : 0 : schema_query->refnamespace,
6113 : : e_ref_schema);
1694 tgl@sss.pgh.pa.us 6114 [ - + ]:CBC 1 : else if (schema_query->refviscondition)
1694 tgl@sss.pgh.pa.us 6115 :UBC 0 : appendPQExpBuffer(&query_buffer,
6116 : : " AND %s",
6117 : 0 : schema_query->refviscondition);
6118 : : }
6119 : : }
6120 : : }
6121 : : else
6122 : : {
3121 tgl@sss.pgh.pa.us 6123 [ - + ]:CBC 8 : Assert(simple_query);
6124 : : /* simple_query is an sprintf-style format string */
6125 : 8 : appendPQExpBuffer(&query_buffer, simple_query,
6126 : : e_object_like,
6127 : : e_ref_object, e_ref_schema);
6128 : : }
6129 : :
6130 : : /* Limit the number of records in the result */
7615 bruce@momjian.us 6131 : 44 : appendPQExpBuffer(&query_buffer, "\nLIMIT %d",
6132 : : completion_max_records);
6133 : :
6134 : : /* Finally, we can issue the query */
6135 : 44 : result = exec_query(query_buffer.data);
6136 : :
6137 : : /* Clean up */
6138 : 44 : termPQExpBuffer(&query_buffer);
81 peter@eisentraut.org 6139 :GNC 44 : pg_free(schemaname);
6140 : 44 : pg_free(objectname);
6141 : 44 : pg_free(e_object_like);
6142 : 44 : pg_free(e_schemaname);
6143 : 44 : pg_free(e_ref_object);
6144 : 44 : pg_free(e_ref_schema);
6145 : : }
6146 : :
6147 : : /* Return the next result, if any, but not if the query failed */
7615 bruce@momjian.us 6148 [ + - + - ]:CBC 282 : if (result && PQresultStatus(result) == PGRES_TUPLES_OK)
6149 : : {
6150 : : int nskip;
6151 : :
1694 tgl@sss.pgh.pa.us 6152 [ + + ]: 282 : while (list_index < PQntuples(result))
6153 : : {
6154 : 213 : const char *item = NULL;
6155 : 213 : const char *nsp = NULL;
6156 : :
6157 [ + + ]: 213 : if (!PQgetisnull(result, list_index, 0))
6158 : 212 : item = PQgetvalue(result, list_index, 0);
6159 [ + + + + ]: 246 : if (PQnfields(result) > 1 &&
6160 : 33 : !PQgetisnull(result, list_index, 1))
6161 : 4 : nsp = PQgetvalue(result, list_index, 1);
6162 : 213 : list_index++;
6163 : :
6164 : : /* In verbatim mode, we return all the items as-is */
6165 [ + + ]: 213 : if (verbatim)
6166 : : {
1692 6167 : 182 : num_query_other++;
7615 bruce@momjian.us 6168 : 182 : return pg_strdup(item);
6169 : : }
6170 : :
6171 : : /*
6172 : : * In normal mode, a name requiring quoting will be returned only
6173 : : * if the input was empty or quoted. Otherwise the user might see
6174 : : * completion inserting a quote she didn't type, which is
6175 : : * surprising. This restriction also dodges some odd behaviors of
6176 : : * some versions of readline/libedit.
6177 : : */
1694 tgl@sss.pgh.pa.us 6178 [ + + ]: 31 : if (non_empty_object)
6179 : : {
6180 [ + + + + : 29 : if (item && !objectquoted && identifier_needs_quotes(item))
- + ]
1694 tgl@sss.pgh.pa.us 6181 :UBC 0 : continue;
1694 tgl@sss.pgh.pa.us 6182 [ + + + - :CBC 29 : if (nsp && !schemaquoted && identifier_needs_quotes(nsp))
- + ]
1694 tgl@sss.pgh.pa.us 6183 :UBC 0 : continue;
6184 : : }
6185 : :
6186 : : /* Count schema-only results for hack below */
1694 tgl@sss.pgh.pa.us 6187 [ + + + - ]:CBC 31 : if (item == NULL && nsp != NULL)
6188 : 1 : num_schema_only++;
6189 : : else
1692 6190 : 30 : num_query_other++;
6191 : :
1694 6192 : 31 : return requote_identifier(nsp, item, schemaquoted, objectquoted);
6193 : : }
6194 : :
6195 : : /*
6196 : : * When the query result is exhausted, check for hard-wired keywords.
6197 : : * These will only be returned if they match the input-so-far,
6198 : : * ignoring case.
6199 : : */
6200 : 69 : nskip = list_index - PQntuples(result);
6201 [ + + + + ]: 69 : if (schema_query && schema_query->keywords)
6202 : : {
6203 : 2 : const char *const *itemp = schema_query->keywords;
6204 : :
6205 [ + + ]: 9 : while (*itemp)
6206 : : {
6207 : 8 : const char *item = *itemp++;
6208 : :
6209 [ + + ]: 8 : if (nskip-- > 0)
6210 : 1 : continue;
6211 : 7 : list_index++;
6212 [ + + ]: 7 : if (pg_strncasecmp(text, item, strlen(text)) == 0)
6213 : : {
1692 6214 : 1 : num_keywords++;
1684 6215 : 1 : return pg_strdup_keyword_case(item, text);
6216 : : }
6217 : : }
6218 : : }
1694 6219 [ + + ]: 68 : if (keywords)
6220 : : {
6221 : 44 : const char *const *itemp = keywords;
6222 : :
6223 [ + + ]: 115 : while (*itemp)
6224 : : {
6225 : 95 : const char *item = *itemp++;
6226 : :
6227 [ + + ]: 95 : if (nskip-- > 0)
6228 : 36 : continue;
6229 : 59 : list_index++;
6230 [ + + ]: 59 : if (pg_strncasecmp(text, item, strlen(text)) == 0)
6231 : : {
1692 6232 : 24 : num_keywords++;
1684 6233 : 24 : return pg_strdup_keyword_case(item, text);
6234 : : }
6235 : : }
6236 : : }
6237 : : }
6238 : :
6239 : : /*
6240 : : * Hack: if we returned only bare schema names, don't let Readline add a
6241 : : * space afterwards. Otherwise the schema will stop being part of the
6242 : : * completion subject text, which is not what we want.
6243 : : */
1692 6244 [ + + + - : 44 : if (num_schema_only > 0 && num_query_other == 0 && num_keywords == 0)
+ - ]
1694 6245 : 1 : rl_completion_append_character = '\0';
6246 : :
6247 : : /* No more matches, so free the result structure and return null */
7615 bruce@momjian.us 6248 : 44 : PQclear(result);
6249 : 44 : result = NULL;
6250 : 44 : return NULL;
6251 : : }
6252 : :
6253 : :
6254 : : /*
6255 : : * Set up completion_ref_object and completion_ref_schema
6256 : : * by parsing the given word. These variables can then be
6257 : : * used in a query passed to _complete_from_query.
6258 : : */
6259 : : static void
1694 tgl@sss.pgh.pa.us 6260 : 19 : set_completion_reference(const char *word)
6261 : : {
6262 : : bool schemaquoted,
6263 : : objectquoted;
6264 : :
6265 : 19 : parse_identifier(word,
6266 : : &completion_ref_schema, &completion_ref_object,
6267 : : &schemaquoted, &objectquoted);
6268 : 19 : }
6269 : :
6270 : : /*
6271 : : * Set up completion_ref_object when it should just be
6272 : : * the given word verbatim.
6273 : : */
6274 : : static void
1684 6275 : 2 : set_completion_reference_verbatim(const char *word)
6276 : : {
6277 : 2 : completion_ref_schema = NULL;
6278 : 2 : completion_ref_object = pg_strdup(word);
6279 : 2 : }
6280 : :
6281 : :
6282 : : /*
6283 : : * This function returns in order one of a fixed, NULL pointer terminated list
6284 : : * of strings (if matching). This can be used if there are only a fixed number
6285 : : * SQL words that can appear at certain spot.
6286 : : */
6287 : : static char *
7615 bruce@momjian.us 6288 : 32 : complete_from_list(const char *text, int state)
6289 : : {
6290 : : static int string_length,
6291 : : list_index,
6292 : : matches;
6293 : : static bool casesensitive;
6294 : : const char *item;
6295 : :
6296 : : /* need to have a list */
5028 andrew@dunslane.net 6297 [ - + ]: 32 : Assert(completion_charpp != NULL);
6298 : :
6299 : : /* Initialization */
7615 bruce@momjian.us 6300 [ + + ]: 32 : if (state == 0)
6301 : : {
6302 : 13 : list_index = 0;
6303 : 13 : string_length = strlen(text);
5345 peter_e@gmx.net 6304 : 13 : casesensitive = completion_case_sensitive;
7615 bruce@momjian.us 6305 : 13 : matches = 0;
6306 : : }
6307 : :
6308 [ + + ]: 546 : while ((item = completion_charpp[list_index++]))
6309 : : {
6310 : : /* First pass is case sensitive */
6311 [ + + + + ]: 500 : if (casesensitive && strncmp(text, item, string_length) == 0)
6312 : : {
6313 : 4 : matches++;
6314 : 4 : return pg_strdup(item);
6315 : : }
6316 : :
6317 : : /* Second pass is case insensitive, don't bother counting matches */
6318 [ + + + + ]: 496 : if (!casesensitive && pg_strncasecmp(text, item, string_length) == 0)
6319 : : {
5345 peter_e@gmx.net 6320 [ + + ]: 14 : if (completion_case_sensitive)
6321 : 1 : return pg_strdup(item);
6322 : : else
6323 : :
6324 : : /*
6325 : : * If case insensitive matching was requested initially,
6326 : : * adjust the case according to setting.
6327 : : */
5248 6328 : 13 : return pg_strdup_keyword_case(item, text);
6329 : : }
6330 : : }
6331 : :
6332 : : /*
6333 : : * No matches found. If we're not case insensitive already, lets switch to
6334 : : * being case insensitive and try again
6335 : : */
7615 bruce@momjian.us 6336 [ + + + + ]: 14 : if (casesensitive && matches == 0)
6337 : : {
6338 : 1 : casesensitive = false;
6339 : 1 : list_index = 0;
6340 : 1 : state++;
7557 neilc@samurai.com 6341 : 1 : return complete_from_list(text, state);
6342 : : }
6343 : :
6344 : : /* If no more matches, return null. */
7615 bruce@momjian.us 6345 : 13 : return NULL;
6346 : : }
6347 : :
6348 : :
6349 : : /*
6350 : : * This function returns one fixed string the first time even if it doesn't
6351 : : * match what's there, and nothing the second time. The string
6352 : : * to be used must be in completion_charp.
6353 : : *
6354 : : * If the given string is "", this has the effect of preventing readline
6355 : : * from doing any completion. (Without this, readline tries to do filename
6356 : : * completion which is seldom the right thing.)
6357 : : *
6358 : : * If the given string is not empty, readline will replace whatever the
6359 : : * user typed with that string. This behavior might be useful if it's
6360 : : * completely certain that we know what must appear at a certain spot,
6361 : : * so that it's okay to overwrite misspellings. In practice, given the
6362 : : * relatively lame parsing technology used in this file, the level of
6363 : : * certainty is seldom that high, so that you probably don't want to
6364 : : * use this. Use complete_from_list with a one-element list instead;
6365 : : * that won't try to auto-correct "misspellings".
6366 : : */
6367 : : static char *
6368 : 4 : complete_from_const(const char *text, int state)
6369 : : {
5028 andrew@dunslane.net 6370 [ - + ]: 4 : Assert(completion_charp != NULL);
7615 bruce@momjian.us 6371 [ + + ]: 4 : if (state == 0)
6372 : : {
5345 peter_e@gmx.net 6373 [ + - ]: 2 : if (completion_case_sensitive)
6374 : 2 : return pg_strdup(completion_charp);
6375 : : else
6376 : :
6377 : : /*
6378 : : * If case insensitive matching was requested initially, adjust
6379 : : * the case according to setting.
6380 : : */
5248 peter_e@gmx.net 6381 :UBC 0 : return pg_strdup_keyword_case(completion_charp, text);
6382 : : }
6383 : : else
7615 bruce@momjian.us 6384 :CBC 2 : return NULL;
6385 : : }
6386 : :
6387 : :
6388 : : /*
6389 : : * This function appends the variable name with prefix and suffix to
6390 : : * the variable names array.
6391 : : */
6392 : : static void
4422 fujii@postgresql.org 6393 : 124 : append_variable_names(char ***varnames, int *nvars,
6394 : : int *maxvars, const char *varname,
6395 : : const char *prefix, const char *suffix)
6396 : : {
6397 [ - + ]: 124 : if (*nvars >= *maxvars)
6398 : : {
4422 fujii@postgresql.org 6399 :UBC 0 : *maxvars *= 2;
205 michael@paquier.xyz 6400 : 0 : *varnames = pg_realloc_array(*varnames, char *, (*maxvars) + 1);
6401 : : }
6402 : :
4422 fujii@postgresql.org 6403 :CBC 124 : (*varnames)[(*nvars)++] = psprintf("%s%s%s", prefix, varname, suffix);
6404 : 124 : }
6405 : :
6406 : :
6407 : : /*
6408 : : * This function supports completion with the name of a psql variable.
6409 : : * The variable names can be prefixed and suffixed with additional text
6410 : : * to support quoting usages. If need_value is true, only variables
6411 : : * that are currently set are included; otherwise, special variables
6412 : : * (those that have hooks) are included even if currently unset.
6413 : : */
6414 : : static char **
6415 : 3 : complete_from_variables(const char *text, const char *prefix, const char *suffix,
6416 : : bool need_value)
6417 : : {
6418 : : char **matches;
6419 : : char **varnames;
5824 tgl@sss.pgh.pa.us 6420 : 3 : int nvars = 0;
6421 : 3 : int maxvars = 100;
6422 : : int i;
6423 : : struct _variable *ptr;
6424 : :
205 michael@paquier.xyz 6425 : 3 : varnames = pg_malloc_array(char *, maxvars + 1);
6426 : :
5824 tgl@sss.pgh.pa.us 6427 [ + + ]: 129 : for (ptr = pset.vars->next; ptr; ptr = ptr->next)
6428 : : {
4422 fujii@postgresql.org 6429 [ + + + + ]: 126 : if (need_value && !(ptr->value))
6430 : 2 : continue;
6431 : 124 : append_variable_names(&varnames, &nvars, &maxvars, ptr->name,
6432 : : prefix, suffix);
6433 : : }
6434 : :
5824 tgl@sss.pgh.pa.us 6435 : 3 : varnames[nvars] = NULL;
5215 bruce@momjian.us 6436 : 3 : COMPLETE_WITH_LIST_CS((const char *const *) varnames);
6437 : :
5824 tgl@sss.pgh.pa.us 6438 [ + + ]: 127 : for (i = 0; i < nvars; i++)
81 peter@eisentraut.org 6439 :GNC 124 : pg_free(varnames[i]);
6440 : 3 : pg_free(varnames);
6441 : :
5824 tgl@sss.pgh.pa.us 6442 :CBC 3 : return matches;
6443 : : }
6444 : :
6445 : :
6446 : : /*
6447 : : * This function returns in order one of a fixed, NULL pointer terminated list
6448 : : * of string that matches file names or optionally specified list of keywords.
6449 : : *
6450 : : * If completion_charpp is set to a null-terminated array of literal keywords,
6451 : : * those keywords are added to the completion results alongside filenames if
6452 : : * they case-insensitively match the current input.
6453 : : */
6454 : : static char *
320 msawada@postgresql.o 6455 : 16 : complete_from_files(const char *text, int state)
6456 : : {
6457 : : static int list_index;
6458 : : static bool files_done;
6459 : : const char *item;
6460 : :
6461 : : /* Initialization */
6462 [ + + ]: 16 : if (state == 0)
6463 : : {
6464 : 6 : list_index = 0;
6465 : 6 : files_done = false;
6466 : : }
6467 : :
6468 [ + - ]: 16 : if (!files_done)
6469 : : {
6470 : 16 : char *result = _complete_from_files(text, state);
6471 : :
6472 : : /* Return a filename that matches */
6473 [ + + ]: 16 : if (result)
6474 : 10 : return result;
6475 : :
6476 : : /* There are no more matching files */
6477 : 6 : files_done = true;
6478 : : }
6479 : :
6480 [ + + ]: 6 : if (!completion_charpp)
6481 : 2 : return NULL;
6482 : :
6483 : : /*
6484 : : * Check for hard-wired keywords. These will only be returned if they
6485 : : * match the input-so-far, ignoring case.
6486 : : */
6487 [ + + ]: 12 : while ((item = completion_charpp[list_index++]))
6488 : : {
6489 [ - + ]: 8 : if (pg_strncasecmp(text, item, strlen(text)) == 0)
6490 : : {
320 msawada@postgresql.o 6491 :UBC 0 : completion_force_quote = false;
6492 : 0 : return pg_strdup_keyword_case(item, text);
6493 : : }
6494 : : }
6495 : :
320 msawada@postgresql.o 6496 :CBC 4 : return NULL;
6497 : : }
6498 : :
6499 : : /*
6500 : : * This function wraps rl_filename_completion_function() to strip quotes from
6501 : : * the input before searching for matches and to quote any matches for which
6502 : : * the consuming command will require it.
6503 : : *
6504 : : * Caller must set completion_charp to a zero- or one-character string
6505 : : * containing the escape character. This is necessary since \copy has no
6506 : : * escape character, but every other backslash command recognizes "\" as an
6507 : : * escape character.
6508 : : *
6509 : : * Caller must also set completion_force_quote to indicate whether to force
6510 : : * quotes around the result. (The SQL COPY command requires that.)
6511 : : */
6512 : : static char *
6513 : 16 : _complete_from_files(const char *text, int state)
6514 : : {
6515 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
6516 : :
6517 : : /*
6518 : : * If we're using a version of Readline that supports filename quoting
6519 : : * hooks, rely on those, and invoke rl_filename_completion_function()
6520 : : * without messing with its arguments. Readline does stuff internally
6521 : : * that does not work well at all if we try to handle dequoting here.
6522 : : * Instead, Readline will call quote_file_name() and dequote_file_name()
6523 : : * (see below) at appropriate times.
6524 : : *
6525 : : * ... or at least, mostly it will. There are some paths involving
6526 : : * unmatched file names in which Readline never calls quote_file_name(),
6527 : : * and if left to its own devices it will incorrectly append a quote
6528 : : * anyway. Set rl_completion_suppress_quote to prevent that. If we do
6529 : : * get to quote_file_name(), we'll clear this again. (Yes, this seems
6530 : : * like it's working around Readline bugs.)
6531 : : */
6532 : : #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
2432 tgl@sss.pgh.pa.us 6533 : 16 : rl_completion_suppress_quote = 1;
6534 : : #endif
6535 : :
6536 : : /* If user typed a quote, force quoting (never remove user's quote) */
6537 [ - + ]: 16 : if (*text == '\'')
2432 tgl@sss.pgh.pa.us 6538 :UBC 0 : completion_force_quote = true;
6539 : :
2432 tgl@sss.pgh.pa.us 6540 :CBC 16 : return rl_filename_completion_function(text, state);
6541 : : #else
6542 : :
6543 : : /*
6544 : : * Otherwise, we have to do the best we can.
6545 : : */
6546 : : static const char *unquoted_text;
6547 : : char *unquoted_match;
6548 : : char *ret = NULL;
6549 : :
6550 : : /* If user typed a quote, force quoting (never remove user's quote) */
6551 : : if (*text == '\'')
6552 : : completion_force_quote = true;
6553 : :
6554 : : if (state == 0)
6555 : : {
6556 : : /* Initialization: stash the unquoted input. */
6557 : : unquoted_text = strtokx(text, "", NULL, "'", *completion_charp,
6558 : : false, true, pset.encoding);
6559 : : /* expect a NULL return for the empty string only */
6560 : : if (!unquoted_text)
6561 : : {
6562 : : Assert(*text == '\0');
6563 : : unquoted_text = text;
6564 : : }
6565 : : }
6566 : :
6567 : : unquoted_match = rl_filename_completion_function(unquoted_text, state);
6568 : : if (unquoted_match)
6569 : : {
6570 : : struct stat statbuf;
6571 : : bool is_dir = (stat(unquoted_match, &statbuf) == 0 &&
6572 : : S_ISDIR(statbuf.st_mode) != 0);
6573 : :
6574 : : /* Re-quote the result, if needed. */
6575 : : ret = quote_if_needed(unquoted_match, " \t\r\n\"`",
6576 : : '\'', *completion_charp,
6577 : : completion_force_quote,
6578 : : pset.encoding);
6579 : : if (ret)
6580 : : free(unquoted_match);
6581 : : else
6582 : : ret = unquoted_match;
6583 : :
6584 : : /*
6585 : : * If it's a directory, replace trailing quote with a slash; this is
6586 : : * usually more convenient. (If we didn't quote, leave this to
6587 : : * libedit.)
6588 : : */
6589 : : if (*ret == '\'' && is_dir)
6590 : : {
6591 : : char *retend = ret + strlen(ret) - 1;
6592 : :
6593 : : Assert(*retend == '\'');
6594 : : *retend = '/';
6595 : : /* Prevent libedit from adding a space, too */
6596 : : rl_completion_append_character = '\0';
6597 : : }
6598 : : }
6599 : :
6600 : : return ret;
6601 : : #endif /* USE_FILENAME_QUOTING_FUNCTIONS */
6602 : : }
6603 : :
6604 : :
6605 : : /* HELPER FUNCTIONS */
6606 : :
6607 : :
6608 : : /*
6609 : : * Make a pg_strdup copy of s and convert the case according to
6610 : : * COMP_KEYWORD_CASE setting, using ref as the text that was already entered.
6611 : : */
6612 : : static char *
5248 peter_e@gmx.net 6613 : 40 : pg_strdup_keyword_case(const char *s, const char *ref)
6614 : : {
6615 : : char *ret,
6616 : : *p;
5345 6617 : 40 : unsigned char first = ref[0];
6618 : :
5248 6619 : 40 : ret = pg_strdup(s);
6620 : :
4281 tgl@sss.pgh.pa.us 6621 [ + + ]: 40 : if (pset.comp_case == PSQL_COMP_CASE_LOWER ||
6622 [ + + ]: 34 : ((pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER ||
3378 6623 [ + + + + ]: 34 : pset.comp_case == PSQL_COMP_CASE_PRESERVE_UPPER) && islower(first)) ||
4281 6624 [ - + - - ]: 26 : (pset.comp_case == PSQL_COMP_CASE_PRESERVE_LOWER && !isalpha(first)))
6625 : : {
5248 peter_e@gmx.net 6626 [ + + ]: 122 : for (p = ret; *p; p++)
6627 : 108 : *p = pg_tolower((unsigned char) *p);
6628 : : }
6629 : : else
6630 : : {
6631 [ + + ]: 241 : for (p = ret; *p; p++)
6632 : 215 : *p = pg_toupper((unsigned char) *p);
6633 : : }
6634 : :
6635 : 40 : return ret;
6636 : : }
6637 : :
6638 : :
6639 : : /*
6640 : : * escape_string - Escape argument for use as string literal.
6641 : : *
6642 : : * The returned value has to be freed.
6643 : : */
6644 : : static char *
4030 andres@anarazel.de 6645 : 71 : escape_string(const char *text)
6646 : : {
6647 : : size_t text_length;
6648 : : char *result;
6649 : :
6650 : 71 : text_length = strlen(text);
6651 : :
6652 : 71 : result = pg_malloc(text_length * 2 + 1);
6653 : 71 : PQescapeStringConn(pset.db, result, text, text_length, NULL);
6654 : :
6655 : 71 : return result;
6656 : : }
6657 : :
6658 : :
6659 : : /*
6660 : : * make_like_pattern - Convert argument to a LIKE prefix pattern.
6661 : : *
6662 : : * We escape _ and % in the given text by backslashing, append a % to
6663 : : * represent "any subsequent characters", and then pass the string through
6664 : : * escape_string() so it's ready to insert in a query. The result needs
6665 : : * to be freed.
6666 : : */
6667 : : static char *
1694 tgl@sss.pgh.pa.us 6668 : 44 : make_like_pattern(const char *word)
6669 : : {
6670 : : char *result;
6671 : 44 : char *buffer = pg_malloc(strlen(word) * 2 + 2);
6672 : 44 : char *bptr = buffer;
6673 : :
6674 [ + + ]: 183 : while (*word)
6675 : : {
6676 [ + + - + ]: 139 : if (*word == '_' || *word == '%')
6677 : 2 : *bptr++ = '\\';
6678 [ - + ]: 139 : if (IS_HIGHBIT_SET(*word))
6679 : : {
6680 : : /*
6681 : : * Transfer multibyte characters without further processing, to
6682 : : * avoid getting confused in unsafe client encodings.
6683 : : */
1694 tgl@sss.pgh.pa.us 6684 :UBC 0 : int chlen = PQmblenBounded(word, pset.encoding);
6685 : :
6686 [ # # ]: 0 : while (chlen-- > 0)
6687 : 0 : *bptr++ = *word++;
6688 : : }
6689 : : else
1694 tgl@sss.pgh.pa.us 6690 :CBC 139 : *bptr++ = *word++;
6691 : : }
6692 : 44 : *bptr++ = '%';
6693 : 44 : *bptr = '\0';
6694 : :
6695 : 44 : result = escape_string(buffer);
81 peter@eisentraut.org 6696 :GNC 44 : pg_free(buffer);
1694 tgl@sss.pgh.pa.us 6697 :CBC 44 : return result;
6698 : : }
6699 : :
6700 : :
6701 : : /*
6702 : : * parse_identifier - Parse a possibly-schema-qualified SQL identifier.
6703 : : *
6704 : : * This involves splitting off the schema name if present, de-quoting,
6705 : : * and downcasing any unquoted text. We are a bit laxer than the backend
6706 : : * in that we allow just portions of a name to be quoted --- that's because
6707 : : * psql metacommands have traditionally behaved that way.
6708 : : *
6709 : : * Outputs are a malloc'd schema name (NULL if none), malloc'd object name,
6710 : : * and booleans telling whether any part of the schema and object name was
6711 : : * double-quoted.
6712 : : */
6713 : : static void
6714 : 55 : parse_identifier(const char *ident,
6715 : : char **schemaname, char **objectname,
6716 : : bool *schemaquoted, bool *objectquoted)
6717 : : {
6718 : 55 : size_t buflen = strlen(ident) + 1;
6719 : 55 : bool enc_is_single_byte = (pg_encoding_max_length(pset.encoding) == 1);
6720 : : char *sname;
6721 : : char *oname;
6722 : : char *optr;
6723 : : bool inquotes;
6724 : :
6725 : : /* Initialize, making a certainly-large-enough output buffer */
6726 : 55 : sname = NULL;
6727 : 55 : oname = pg_malloc(buflen);
6728 : 55 : *schemaquoted = *objectquoted = false;
6729 : : /* Scan */
6730 : 55 : optr = oname;
6731 : 55 : inquotes = false;
6732 [ + + ]: 271 : while (*ident)
6733 : : {
6734 : 216 : unsigned char ch = (unsigned char) *ident++;
6735 : :
6736 [ + + ]: 216 : if (ch == '"')
6737 : : {
6738 [ + + - + ]: 7 : if (inquotes && *ident == '"')
6739 : : {
6740 : : /* two quote marks within a quoted identifier = emit quote */
1694 tgl@sss.pgh.pa.us 6741 :UBC 0 : *optr++ = '"';
6742 : 0 : ident++;
6743 : : }
6744 : : else
6745 : : {
1694 tgl@sss.pgh.pa.us 6746 :CBC 7 : inquotes = !inquotes;
6747 : 7 : *objectquoted = true;
6748 : : }
6749 : : }
6750 [ + + + - ]: 209 : else if (ch == '.' && !inquotes)
6751 : : {
6752 : : /* Found a schema name, transfer it to sname / *schemaquoted */
6753 : 4 : *optr = '\0';
6754 : 4 : free(sname); /* drop any catalog name */
6755 : 4 : sname = oname;
6756 : 4 : oname = pg_malloc(buflen);
6757 : 4 : optr = oname;
6758 : 4 : *schemaquoted = *objectquoted;
6759 : 4 : *objectquoted = false;
6760 : : }
6761 [ + - - + ]: 205 : else if (!enc_is_single_byte && IS_HIGHBIT_SET(ch))
1694 tgl@sss.pgh.pa.us 6762 :UBC 0 : {
6763 : : /*
6764 : : * Transfer multibyte characters without further processing. They
6765 : : * wouldn't be affected by our downcasing rule anyway, and this
6766 : : * avoids possibly doing the wrong thing in unsafe client
6767 : : * encodings.
6768 : : */
6769 : 0 : int chlen = PQmblenBounded(ident - 1, pset.encoding);
6770 : :
6771 : 0 : *optr++ = (char) ch;
6772 [ # # ]: 0 : while (--chlen > 0)
6773 : 0 : *optr++ = *ident++;
6774 : : }
6775 : : else
6776 : : {
1694 tgl@sss.pgh.pa.us 6777 [ + + ]:CBC 205 : if (!inquotes)
6778 : : {
6779 : : /*
6780 : : * This downcasing transformation should match the backend's
6781 : : * downcase_identifier() as best we can. We do not know the
6782 : : * backend's locale, though, so it's necessarily approximate.
6783 : : * We assume that psql is operating in the same locale and
6784 : : * encoding as the backend.
6785 : : */
6786 [ + + + + ]: 181 : if (ch >= 'A' && ch <= 'Z')
6787 : 28 : ch += 'a' - 'A';
6788 [ - + - - : 153 : else if (enc_is_single_byte && IS_HIGHBIT_SET(ch) && isupper(ch))
- - ]
1694 tgl@sss.pgh.pa.us 6789 :UBC 0 : ch = tolower(ch);
6790 : : }
1694 tgl@sss.pgh.pa.us 6791 :CBC 205 : *optr++ = (char) ch;
6792 : : }
6793 : : }
6794 : :
6795 : 55 : *optr = '\0';
6796 : 55 : *schemaname = sname;
6797 : 55 : *objectname = oname;
6798 : 55 : }
6799 : :
6800 : :
6801 : : /*
6802 : : * requote_identifier - Reconstruct a possibly-schema-qualified SQL identifier.
6803 : : *
6804 : : * Build a malloc'd string containing the identifier, with quoting applied
6805 : : * as necessary. This is more or less the inverse of parse_identifier;
6806 : : * in particular, if an input component was quoted, we'll quote the output
6807 : : * even when that isn't strictly required.
6808 : : *
6809 : : * Unlike parse_identifier, we handle the case where a schema and no
6810 : : * object name is provided, producing just "schema.".
6811 : : */
6812 : : static char *
6813 : 31 : requote_identifier(const char *schemaname, const char *objectname,
6814 : : bool quote_schema, bool quote_object)
6815 : : {
6816 : : char *result;
6817 : 31 : size_t buflen = 1; /* count the trailing \0 */
6818 : : char *ptr;
6819 : :
6820 : : /*
6821 : : * We could use PQescapeIdentifier for some of this, but not all, and it
6822 : : * adds more notational cruft than it seems worth.
6823 : : */
6824 [ + + ]: 31 : if (schemaname)
6825 : : {
6826 : 4 : buflen += strlen(schemaname) + 1; /* +1 for the dot */
6827 [ + - ]: 4 : if (!quote_schema)
6828 : 4 : quote_schema = identifier_needs_quotes(schemaname);
6829 [ - + ]: 4 : if (quote_schema)
6830 : : {
1694 tgl@sss.pgh.pa.us 6831 :UBC 0 : buflen += 2; /* account for quote marks */
6832 [ # # ]: 0 : for (const char *p = schemaname; *p; p++)
6833 : : {
6834 [ # # ]: 0 : if (*p == '"')
6835 : 0 : buflen++;
6836 : : }
6837 : : }
6838 : : }
1694 tgl@sss.pgh.pa.us 6839 [ + + ]:CBC 31 : if (objectname)
6840 : : {
6841 : 30 : buflen += strlen(objectname);
6842 [ + + ]: 30 : if (!quote_object)
6843 : 22 : quote_object = identifier_needs_quotes(objectname);
6844 [ + + ]: 30 : if (quote_object)
6845 : : {
6846 : 8 : buflen += 2; /* account for quote marks */
6847 [ + + ]: 73 : for (const char *p = objectname; *p; p++)
6848 : : {
6849 [ - + ]: 65 : if (*p == '"')
1694 tgl@sss.pgh.pa.us 6850 :UBC 0 : buflen++;
6851 : : }
6852 : : }
6853 : : }
1694 tgl@sss.pgh.pa.us 6854 :CBC 31 : result = pg_malloc(buflen);
6855 : 31 : ptr = result;
6856 [ + + ]: 31 : if (schemaname)
6857 : : {
6858 [ - + ]: 4 : if (quote_schema)
1694 tgl@sss.pgh.pa.us 6859 :UBC 0 : *ptr++ = '"';
1694 tgl@sss.pgh.pa.us 6860 [ + + ]:CBC 28 : for (const char *p = schemaname; *p; p++)
6861 : : {
6862 : 24 : *ptr++ = *p;
6863 [ - + ]: 24 : if (*p == '"')
1694 tgl@sss.pgh.pa.us 6864 :UBC 0 : *ptr++ = '"';
6865 : : }
1694 tgl@sss.pgh.pa.us 6866 [ - + ]:CBC 4 : if (quote_schema)
1694 tgl@sss.pgh.pa.us 6867 :UBC 0 : *ptr++ = '"';
1694 tgl@sss.pgh.pa.us 6868 :CBC 4 : *ptr++ = '.';
6869 : : }
6870 [ + + ]: 31 : if (objectname)
6871 : : {
6872 [ + + ]: 30 : if (quote_object)
6873 : 8 : *ptr++ = '"';
6874 [ + + ]: 264 : for (const char *p = objectname; *p; p++)
6875 : : {
6876 : 234 : *ptr++ = *p;
6877 [ - + ]: 234 : if (*p == '"')
1694 tgl@sss.pgh.pa.us 6878 :UBC 0 : *ptr++ = '"';
6879 : : }
1694 tgl@sss.pgh.pa.us 6880 [ + + ]:CBC 30 : if (quote_object)
6881 : 8 : *ptr++ = '"';
6882 : : }
6883 : 31 : *ptr = '\0';
6884 : 31 : return result;
6885 : : }
6886 : :
6887 : :
6888 : : /*
6889 : : * Detect whether an identifier must be double-quoted.
6890 : : *
6891 : : * Note we'll quote anything that's not ASCII; the backend's quote_ident()
6892 : : * does the same. Perhaps this could be relaxed in future.
6893 : : */
6894 : : static bool
6895 : 49 : identifier_needs_quotes(const char *ident)
6896 : : {
6897 : : int kwnum;
6898 : :
6899 : : /* Check syntax. */
6900 [ + - - + : 49 : if (!((ident[0] >= 'a' && ident[0] <= 'z') || ident[0] == '_'))
- - ]
1694 tgl@sss.pgh.pa.us 6901 :UBC 0 : return true;
1097 heikki.linnakangas@i 6902 [ - + ]:CBC 49 : if (strspn(ident, "abcdefghijklmnopqrstuvwxyz0123456789_$") != strlen(ident))
1694 tgl@sss.pgh.pa.us 6903 :UBC 0 : return true;
6904 : :
6905 : : /*
6906 : : * Check for keyword. We quote keywords except for unreserved ones.
6907 : : *
6908 : : * It is possible that our keyword list doesn't quite agree with the
6909 : : * server's, but this should be close enough for tab-completion purposes.
6910 : : *
6911 : : * Note: ScanKeywordLookup() does case-insensitive comparison, but that's
6912 : : * fine, since we already know we have all-lower-case.
6913 : : */
1694 tgl@sss.pgh.pa.us 6914 :CBC 49 : kwnum = ScanKeywordLookup(ident, &ScanKeywords);
6915 : :
6916 [ - + - - ]: 49 : if (kwnum >= 0 && ScanKeywordCategories[kwnum] != UNRESERVED_KEYWORD)
1694 tgl@sss.pgh.pa.us 6917 :UBC 0 : return true;
6918 : :
1694 tgl@sss.pgh.pa.us 6919 :CBC 49 : return false;
6920 : : }
6921 : :
6922 : :
6923 : : /*
6924 : : * Execute a query, returning NULL if there was any error.
6925 : : * This should be the preferred way of talking to the database in this file.
6926 : : */
6927 : : static PGresult *
7615 bruce@momjian.us 6928 : 46 : exec_query(const char *query)
6929 : : {
6930 : : PGresult *result;
6931 : :
6932 [ + - + - : 46 : if (query == NULL || !pset.db || PQstatus(pset.db) != CONNECTION_OK)
- + ]
7615 bruce@momjian.us 6933 :UBC 0 : return NULL;
6934 : :
7615 bruce@momjian.us 6935 :CBC 46 : result = PQexec(pset.db, query);
6936 : :
6609 tgl@sss.pgh.pa.us 6937 [ - + ]: 46 : if (PQresultStatus(result) != PGRES_TUPLES_OK)
6938 : : {
6939 : : /*
6940 : : * Printing an error while the user is typing would be quite annoying,
6941 : : * so we don't. This does complicate debugging of this code; but you
6942 : : * can look in the server log instead.
6943 : : */
6944 : : #ifdef NOT_USED
6945 : : pg_log_error("tab completion query failed: %s\nQuery was:\n%s",
6946 : : PQerrorMessage(pset.db), query);
6947 : : #endif
7615 bruce@momjian.us 6948 :UBC 0 : PQclear(result);
6949 : 0 : result = NULL;
6950 : : }
6951 : :
7615 bruce@momjian.us 6952 :CBC 46 : return result;
6953 : : }
6954 : :
6955 : :
6956 : : /*
6957 : : * Parse all the word(s) before point.
6958 : : *
6959 : : * Returns a malloc'd array of character pointers that point into the malloc'd
6960 : : * data array returned to *buffer; caller must free() both of these when done.
6961 : : * *nwords receives the number of words found, ie, the valid length of the
6962 : : * return array.
6963 : : *
6964 : : * Words are returned right to left, that is, previous_words[0] gets the last
6965 : : * word before point, previous_words[1] the next-to-last, etc.
6966 : : */
6967 : : static char **
3927 tgl@sss.pgh.pa.us 6968 : 67 : get_previous_words(int point, char **buffer, int *nwords)
6969 : : {
6970 : : char **previous_words;
6971 : : char *buf;
6972 : : char *outptr;
3928 6973 : 67 : int words_found = 0;
6974 : : int i;
6975 : :
6976 : : /*
6977 : : * If we have anything in tab_completion_query_buf, paste it together with
6978 : : * rl_line_buffer to construct the full query. Otherwise we can just use
6979 : : * rl_line_buffer as the input string.
6980 : : */
3926 6981 [ + - + + ]: 67 : if (tab_completion_query_buf && tab_completion_query_buf->len > 0)
6982 : : {
6983 : 3 : i = tab_completion_query_buf->len;
6984 : 3 : buf = pg_malloc(point + i + 2);
6985 : 3 : memcpy(buf, tab_completion_query_buf->data, i);
3927 6986 : 3 : buf[i++] = '\n';
3926 6987 : 3 : memcpy(buf + i, rl_line_buffer, point);
6988 : 3 : i += point;
6989 : 3 : buf[i] = '\0';
6990 : : /* Readjust point to reference appropriate offset in buf */
6991 : 3 : point = i;
6992 : : }
6993 : : else
6994 : 64 : buf = rl_line_buffer;
6995 : :
6996 : : /*
6997 : : * Allocate an array of string pointers and a buffer to hold the strings
6998 : : * themselves. The worst case is that the line contains only
6999 : : * non-whitespace WORD_BREAKS characters, making each one a separate word.
7000 : : * This is usually much more space than we need, but it's cheaper than
7001 : : * doing a separate malloc() for each word.
7002 : : */
205 michael@paquier.xyz 7003 : 67 : previous_words = pg_malloc_array(char *, point);
3926 tgl@sss.pgh.pa.us 7004 : 67 : *buffer = outptr = (char *) pg_malloc(point * 2);
7005 : :
7006 : : /*
7007 : : * First we look for a non-word char before the current point. (This is
7008 : : * probably useless, if readline is on the same page as we are about what
7009 : : * is a word, but if so it's cheap.)
7010 : : */
6059 itagaki.takahiro@gma 7011 [ + + ]: 73 : for (i = point - 1; i >= 0; i--)
7012 : : {
7013 [ + + ]: 70 : if (strchr(WORD_BREAKS, buf[i]))
7014 : 64 : break;
7015 : : }
7016 : 67 : point = i;
7017 : :
7018 : : /*
7019 : : * Now parse words, working backwards, until we hit start of line. The
7020 : : * backwards scan has some interesting but intentional properties
7021 : : * concerning parenthesis handling.
7022 : : */
3927 tgl@sss.pgh.pa.us 7023 [ + + ]: 255 : while (point >= 0)
7024 : : {
7025 : : int start,
7026 : : end;
7027 : 188 : bool inquotes = false;
7028 : 188 : int parentheses = 0;
7029 : :
7030 : : /* now find the first non-space which then constitutes the end */
5449 7031 : 188 : end = -1;
6059 itagaki.takahiro@gma 7032 [ + - ]: 382 : for (i = point; i >= 0; i--)
7033 : : {
5449 tgl@sss.pgh.pa.us 7034 [ + + ]: 382 : if (!isspace((unsigned char) buf[i]))
7035 : : {
7615 bruce@momjian.us 7036 : 188 : end = i;
7037 : 188 : break;
7038 : : }
7039 : : }
7040 : : /* if no end found, we're done */
3927 tgl@sss.pgh.pa.us 7041 [ - + ]: 188 : if (end < 0)
3927 tgl@sss.pgh.pa.us 7042 :UBC 0 : break;
7043 : :
7044 : : /*
7045 : : * Otherwise we now look for the start. The start is either the last
7046 : : * character before any word-break character going backwards from the
7047 : : * end, or it's simply character 0. We also handle open quotes and
7048 : : * parentheses.
7049 : : */
3927 tgl@sss.pgh.pa.us 7050 [ + + ]:CBC 943 : for (start = end; start > 0; start--)
7051 : : {
7052 [ + + ]: 879 : if (buf[start] == '"')
7053 : 2 : inquotes = !inquotes;
7054 [ + + ]: 879 : if (!inquotes)
7055 : : {
7056 [ - + ]: 874 : if (buf[start] == ')')
3927 tgl@sss.pgh.pa.us 7057 :UBC 0 : parentheses++;
3927 tgl@sss.pgh.pa.us 7058 [ + + ]:CBC 874 : else if (buf[start] == '(')
7059 : : {
7060 [ + - ]: 3 : if (--parentheses <= 0)
6059 itagaki.takahiro@gma 7061 : 3 : break;
7062 : : }
3927 tgl@sss.pgh.pa.us 7063 [ + - ]: 871 : else if (parentheses == 0 &&
7064 [ + + ]: 871 : strchr(WORD_BREAKS, buf[start - 1]))
7065 : 121 : break;
7066 : : }
7067 : : }
7068 : :
7069 : : /* Return the word located at start to end inclusive */
3926 7070 : 188 : previous_words[words_found++] = outptr;
7071 : 188 : i = end - start + 1;
7072 : 188 : memcpy(outptr, &buf[start], i);
7073 : 188 : outptr += i;
7074 : 188 : *outptr++ = '\0';
7075 : :
7076 : : /* Continue searching */
3927 7077 : 188 : point = start - 1;
7078 : : }
7079 : :
7080 : : /* Release parsing input workspace, if we made one above */
3926 7081 [ + + ]: 67 : if (buf != rl_line_buffer)
81 peter@eisentraut.org 7082 :GNC 3 : pg_free(buf);
7083 : :
3927 tgl@sss.pgh.pa.us 7084 :CBC 67 : *nwords = words_found;
7085 : 67 : return previous_words;
7086 : : }
7087 : :
7088 : : /*
7089 : : * Look up the type for the GUC variable with the passed name.
7090 : : *
7091 : : * Returns NULL if the variable is unknown. Otherwise the returned string,
7092 : : * containing the type, has to be freed.
7093 : : */
7094 : : static char *
4030 andres@anarazel.de 7095 : 2 : get_guctype(const char *varname)
7096 : : {
7097 : : PQExpBufferData query_buffer;
7098 : : char *e_varname;
7099 : : PGresult *result;
7100 : 2 : char *guctype = NULL;
7101 : :
7102 : 2 : e_varname = escape_string(varname);
7103 : :
7104 : 2 : initPQExpBuffer(&query_buffer);
7105 : 2 : appendPQExpBuffer(&query_buffer,
7106 : : "SELECT vartype FROM pg_catalog.pg_settings "
7107 : : "WHERE pg_catalog.lower(name) = pg_catalog.lower('%s')",
7108 : : e_varname);
7109 : :
7110 : 2 : result = exec_query(query_buffer.data);
7111 : 2 : termPQExpBuffer(&query_buffer);
7112 : 2 : free(e_varname);
7113 : :
7114 [ + - + - ]: 2 : if (PQresultStatus(result) == PGRES_TUPLES_OK && PQntuples(result) > 0)
7115 : 2 : guctype = pg_strdup(PQgetvalue(result, 0, 0));
7116 : :
7117 : 2 : PQclear(result);
7118 : :
7119 : 2 : return guctype;
7120 : : }
7121 : :
7122 : : #ifdef USE_FILENAME_QUOTING_FUNCTIONS
7123 : :
7124 : : /*
7125 : : * Quote a filename according to SQL rules, returning a malloc'd string.
7126 : : * completion_charp must point to escape character or '\0', and
7127 : : * completion_force_quote must be set correctly, as per comments for
7128 : : * complete_from_files().
7129 : : */
7130 : : static char *
2432 tgl@sss.pgh.pa.us 7131 : 5 : quote_file_name(char *fname, int match_type, char *quote_pointer)
7132 : : {
7133 : : char *s;
7134 : : struct stat statbuf;
7135 : :
7136 : : /* Quote if needed. */
7137 : 5 : s = quote_if_needed(fname, " \t\r\n\"`",
7138 : 5 : '\'', *completion_charp,
7139 : : completion_force_quote,
7140 : : pset.encoding);
7141 [ + + ]: 5 : if (!s)
7142 : 2 : s = pg_strdup(fname);
7143 : :
7144 : : /*
7145 : : * However, some of the time we have to strip the trailing quote from what
7146 : : * we send back. Never strip the trailing quote if the user already typed
7147 : : * one; otherwise, suppress the trailing quote if we have multiple/no
7148 : : * matches (because we don't want to add a quote if the input is seemingly
7149 : : * unfinished), or if the input was already quoted (because Readline will
7150 : : * do arguably-buggy things otherwise), or if the file does not exist, or
7151 : : * if it's a directory.
7152 : : */
7153 [ + + ]: 5 : if (*s == '\'' &&
7154 [ + - + + ]: 3 : completion_last_char != '\'' &&
7155 [ + - ]: 1 : (match_type != SINGLE_MATCH ||
7156 [ + - + - ]: 2 : (quote_pointer && *quote_pointer == '\'') ||
7157 : 1 : stat(fname, &statbuf) != 0 ||
7158 [ - + ]: 1 : S_ISDIR(statbuf.st_mode)))
7159 : : {
7160 : 2 : char *send = s + strlen(s) - 1;
7161 : :
7162 [ - + ]: 2 : Assert(*send == '\'');
7163 : 2 : *send = '\0';
7164 : : }
7165 : :
7166 : : /*
7167 : : * And now we can let Readline do its thing with possibly adding a quote
7168 : : * on its own accord. (This covers some additional cases beyond those
7169 : : * dealt with above.)
7170 : : */
7171 : : #ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
7172 : 5 : rl_completion_suppress_quote = 0;
7173 : : #endif
7174 : :
7175 : : /*
7176 : : * If user typed a leading quote character other than single quote (i.e.,
7177 : : * double quote), zap it, so that we replace it with the correct single
7178 : : * quote.
7179 : : */
7180 [ + - + + ]: 5 : if (quote_pointer && *quote_pointer != '\'')
7181 : 4 : *quote_pointer = '\0';
7182 : :
7615 bruce@momjian.us 7183 : 5 : return s;
7184 : : }
7185 : :
7186 : : /*
7187 : : * Dequote a filename, if it's quoted.
7188 : : * completion_charp must point to escape character or '\0', as per
7189 : : * comments for complete_from_files().
7190 : : */
7191 : : static char *
2432 tgl@sss.pgh.pa.us 7192 : 12 : dequote_file_name(char *fname, int quote_char)
7193 : : {
7194 : : char *unquoted_fname;
7195 : :
7196 : : /*
7197 : : * If quote_char is set, it's not included in "fname". We have to add it
7198 : : * or strtokx will not interpret the string correctly (notably, it won't
7199 : : * recognize escapes).
7200 : : */
7201 [ + + ]: 12 : if (quote_char == '\'')
7202 : : {
7203 : 6 : char *workspace = (char *) pg_malloc(strlen(fname) + 2);
7204 : :
7205 : 6 : workspace[0] = quote_char;
7206 : 6 : strcpy(workspace + 1, fname);
7207 : 6 : unquoted_fname = strtokx(workspace, "", NULL, "'", *completion_charp,
7208 : : false, true, pset.encoding);
81 peter@eisentraut.org 7209 :GNC 6 : pg_free(workspace);
7210 : : }
7211 : : else
2432 tgl@sss.pgh.pa.us 7212 :CBC 6 : unquoted_fname = strtokx(fname, "", NULL, "'", *completion_charp,
7213 : : false, true, pset.encoding);
7214 : :
7215 : : /* expect a NULL return for the empty string only */
7216 [ - + ]: 12 : if (!unquoted_fname)
7217 : : {
2432 tgl@sss.pgh.pa.us 7218 [ # # ]:UBC 0 : Assert(*fname == '\0');
7219 : 0 : unquoted_fname = fname;
7220 : : }
7221 : :
7222 : : /* readline expects a malloc'd result that it is to free */
2432 tgl@sss.pgh.pa.us 7223 :CBC 12 : return pg_strdup(unquoted_fname);
7224 : : }
7225 : :
7226 : : #endif /* USE_FILENAME_QUOTING_FUNCTIONS */
7227 : :
7228 : : #endif /* USE_READLINE */
|