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