tab-complete.c 117.4 KB
Newer Older
P
Peter Eisentraut 已提交
1 2 3
/*
 * psql - the PostgreSQL interactive terminal
 *
4
 * Copyright (c) 2000-2012, PostgreSQL Global Development Group
P
Peter Eisentraut 已提交
5
 *
6
 * src/bin/psql/tab-complete.c
P
Peter Eisentraut 已提交
7 8
 */

9 10 11 12 13 14 15 16 17
/*----------------------------------------------------------------------
 * This file implements a somewhat more sophisticated readline "TAB
 * completion" in psql. It is not intended to be AI, to replace
 * learning SQL, or to relieve you from thinking about what you're
 * doing. Also it does not always give you all the syntactically legal
 * completions, only those that are the most common or the ones that
 * the programmer felt most like implementing.
 *
 * CAVEAT: Tab completion causes queries to be sent to the backend.
18 19
 * The number of tuples returned gets limited, in most default
 * installations to 1000, but if you still don't like this prospect,
20 21 22
 * you can turn off tab completion in your ~/.inputrc (or else
 * ${INPUTRC}) file so:
 *
B
Bruce Momjian 已提交
23 24 25
 *	 $if psql
 *	 set disable-completion on
 *	 $endif
26 27 28 29 30 31
 *
 * See `man 3 readline' or `info readline' for the full details. Also,
 * hence the
 *
 * BUGS:
 *
32
 * - If you split your queries across lines, this whole thing gets
B
Bruce Momjian 已提交
33 34 35
 *	 confused. (To fix this, one would have to read psql's query
 *	 buffer rather than readline's line buffer, which would require
 *	 some major revisions of things.)
36
 *
37
 * - Table or attribute names with spaces in it may confuse it.
38 39
 *
 * - Quotes, parenthesis, and other funny characters are not handled
B
Bruce Momjian 已提交
40
 *	 all that gracefully.
41 42
 *----------------------------------------------------------------------
 */
43

44
#include "postgres_fe.h"
45 46 47 48 49 50
#include "tab-complete.h"
#include "input.h"

/* If we don't have this, we might as well forget about the whole thing: */
#ifdef USE_READLINE

51
#include <ctype.h>
52
#include "libpq-fe.h"
53
#include "pqexpbuffer.h"
54
#include "common.h"
P
Peter Eisentraut 已提交
55
#include "settings.h"
56
#include "stringutils.h"
57

58 59
#ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
#define filename_completion_function rl_filename_completion_function
60 61 62
#else
/* missing in some header files */
extern char *filename_completion_function();
63
#endif
64

P
Peter Eisentraut 已提交
65
#ifdef HAVE_RL_COMPLETION_MATCHES
66
#define completion_matches rl_completion_matches
67 68
#endif

69 70
/* word break characters */
#define WORD_BREAKS		"\t\n@$><=;|&{() "
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*
 * This struct is used to define "schema queries", which are custom-built
 * to obtain possibly-schema-qualified names of database objects.  There is
 * enough similarity in the structure that we don't want to repeat it each
 * time.  So we put the components of each query into this struct and
 * assemble them with the common boilerplate in _complete_from_query().
 */
typedef struct SchemaQuery
{
	/*
	 * Name of catalog or catalogs to be queried, with alias, eg.
	 * "pg_catalog.pg_class c".  Note that "pg_namespace n" will be added.
	 */
	const char *catname;
B
Bruce Momjian 已提交
86

87
	/*
B
Bruce Momjian 已提交
88 89 90 91
	 * Selection condition --- only rows meeting this condition are candidates
	 * to display.	If catname mentions multiple tables, include the necessary
	 * join condition here.  For example, "c.relkind = 'r'". Write NULL (not
	 * an empty string) if not needed.
92 93
	 */
	const char *selcondition;
B
Bruce Momjian 已提交
94

95 96
	/*
	 * Visibility condition --- which rows are visible without schema
B
Bruce Momjian 已提交
97
	 * qualification?  For example, "pg_catalog.pg_table_is_visible(c.oid)".
98 99
	 */
	const char *viscondition;
B
Bruce Momjian 已提交
100

101
	/*
B
Bruce Momjian 已提交
102 103
	 * Namespace --- name of field to join to pg_namespace.oid. For example,
	 * "c.relnamespace".
104 105
	 */
	const char *namespace;
B
Bruce Momjian 已提交
106

107
	/*
B
Bruce Momjian 已提交
108 109
	 * Result --- the appropriately-quoted name to return, in the case of an
	 * unqualified name.  For example, "pg_catalog.quote_ident(c.relname)".
110 111
	 */
	const char *result;
B
Bruce Momjian 已提交
112

113 114 115 116 117 118
	/*
	 * In some cases a different result must be used for qualified names.
	 * Enter that here, or write NULL if result can be used.
	 */
	const char *qualresult;
} SchemaQuery;
119

120

121 122 123 124
/* Store maximum number of records we want from database queries
 * (implemented via SELECT ... LIMIT xx).
 */
static int	completion_max_records;
125

126 127 128 129
/*
 * Communication variables set by COMPLETE_WITH_FOO macros and then used by
 * the completion callback functions.  Ugly but there is no better way.
 */
130
static const char *completion_charp;	/* to pass a string */
B
Bruce Momjian 已提交
131
static const char *const * completion_charpp;	/* to pass a list of strings */
B
Bruce Momjian 已提交
132
static const char *completion_info_charp;		/* to pass a second string */
133
static const char *completion_info_charp2;		/* to pass a third string */
B
Bruce Momjian 已提交
134
static const SchemaQuery *completion_squery;	/* to pass a SchemaQuery */
135
static bool completion_case_sensitive;			/* completion is case sensitive */
136

137 138 139 140 141 142 143 144 145
/*
 * A few macros to ease typing. You can use these to complete the given
 * string with
 * 1) The results from a query you pass it. (Perhaps one of those below?)
 * 2) The results from a schema query you pass it.
 * 3) The items from a null-pointer-terminated list.
 * 4) A string constant.
 * 5) The list of attributes of the given table (possibly schema-qualified).
 */
146
#define COMPLETE_WITH_QUERY(query) \
147 148 149 150 151
do { \
	completion_charp = query; \
	matches = completion_matches(text, complete_from_query); \
} while (0)

152
#define COMPLETE_WITH_SCHEMA_QUERY(query, addon) \
153 154 155 156 157 158
do { \
	completion_squery = &(query); \
	completion_charp = addon; \
	matches = completion_matches(text, complete_from_schema_query); \
} while (0)

159 160 161 162 163 164 165
#define COMPLETE_WITH_LIST_CS(list) \
do { \
	completion_charpp = list; \
	completion_case_sensitive = true; \
	matches = completion_matches(text, complete_from_list); \
} while (0)

166
#define COMPLETE_WITH_LIST(list) \
167 168
do { \
	completion_charpp = list; \
169
	completion_case_sensitive = false; \
170 171 172
	matches = completion_matches(text, complete_from_list); \
} while (0)

173
#define COMPLETE_WITH_CONST(string) \
174 175
do { \
	completion_charp = string; \
176
	completion_case_sensitive = false; \
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	matches = completion_matches(text, complete_from_const); \
} while (0)

#define COMPLETE_WITH_ATTR(relation, addon) \
do { \
	char   *_completion_schema; \
	char   *_completion_table; \
\
	_completion_schema = strtokx(relation, " \t\n\r", ".", "\"", 0, \
								 false, false, pset.encoding); \
	(void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \
				   false, false, pset.encoding); \
	_completion_table = strtokx(NULL, " \t\n\r", ".", "\"", 0, \
								false, false, pset.encoding); \
	if (_completion_table == NULL) \
	{ \
		completion_charp = Query_for_list_of_attributes  addon; \
		completion_info_charp = relation; \
	} \
	else \
	{ \
		completion_charp = Query_for_list_of_attributes_with_schema  addon; \
		completion_info_charp = _completion_table; \
		completion_info_charp2 = _completion_schema; \
	} \
	matches = completion_matches(text, complete_from_query); \
} while (0)
204

205 206 207
/*
 * Assembly instructions for schema queries
 */
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222
static const SchemaQuery Query_for_list_of_aggregates = {
	/* catname */
	"pg_catalog.pg_proc p",
	/* selcondition */
	"p.proisagg",
	/* viscondition */
	"pg_catalog.pg_function_is_visible(p.oid)",
	/* namespace */
	"p.pronamespace",
	/* result */
	"pg_catalog.quote_ident(p.proname)",
	/* qualresult */
	NULL
};
223

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static const SchemaQuery Query_for_list_of_datatypes = {
	/* catname */
	"pg_catalog.pg_type t",
	/* selcondition --- ignore table rowtypes and array types */
	"(t.typrelid = 0 "
	" OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) "
	"AND t.typname !~ '^_'",
	/* viscondition */
	"pg_catalog.pg_type_is_visible(t.oid)",
	/* namespace */
	"t.typnamespace",
	/* result */
	"pg_catalog.format_type(t.oid, NULL)",
	/* qualresult */
	"pg_catalog.quote_ident(t.typname)"
};
240

241 242 243 244 245 246 247 248 249 250 251 252 253 254
static const SchemaQuery Query_for_list_of_domains = {
	/* catname */
	"pg_catalog.pg_type t",
	/* selcondition */
	"t.typtype = 'd'",
	/* viscondition */
	"pg_catalog.pg_type_is_visible(t.oid)",
	/* namespace */
	"t.typnamespace",
	/* result */
	"pg_catalog.quote_ident(t.typname)",
	/* qualresult */
	NULL
};
255

256 257 258 259 260 261 262 263 264 265 266 267 268 269
static const SchemaQuery Query_for_list_of_functions = {
	/* catname */
	"pg_catalog.pg_proc p",
	/* selcondition */
	NULL,
	/* viscondition */
	"pg_catalog.pg_function_is_visible(p.oid)",
	/* namespace */
	"p.pronamespace",
	/* result */
	"pg_catalog.quote_ident(p.proname)",
	/* qualresult */
	NULL
};
270

271 272 273 274 275 276 277 278 279 280 281 282 283 284
static const SchemaQuery Query_for_list_of_indexes = {
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
	"c.relkind IN ('i')",
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
static const SchemaQuery Query_for_list_of_sequences = {
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
	"c.relkind IN ('S')",
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};

R
Robert Haas 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
static const SchemaQuery Query_for_list_of_foreign_tables = {
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
	"c.relkind IN ('f')",
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
static const SchemaQuery Query_for_list_of_tables = {
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
	"c.relkind IN ('r')",
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/* The bit masks for the following three functions come from
 * src/include/catalog/pg_trigger.h.
 */
static const SchemaQuery Query_for_list_of_insertables = {
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
	"(c.relkind = 'r' OR (c.relkind = 'v' AND c.relhastriggers AND EXISTS "
	"(SELECT 1 FROM pg_catalog.pg_trigger t WHERE t.tgrelid = c.oid AND t.tgtype & (1 << 2) <> 0)))",
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};

static const SchemaQuery Query_for_list_of_deletables = {
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
	"(c.relkind = 'r' OR (c.relkind = 'v' AND c.relhastriggers AND EXISTS "
	"(SELECT 1 FROM pg_catalog.pg_trigger t WHERE t.tgrelid = c.oid AND t.tgtype & (1 << 3) <> 0)))",
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};

static const SchemaQuery Query_for_list_of_updatables = {
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
	"(c.relkind = 'r' OR (c.relkind = 'v' AND c.relhastriggers AND EXISTS "
	"(SELECT 1 FROM pg_catalog.pg_trigger t WHERE t.tgrelid = c.oid AND t.tgtype & (1 << 4) <> 0)))",
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};

382
static const SchemaQuery Query_for_list_of_relations = {
383 384 385
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
386
	NULL,
387 388 389 390 391 392 393 394 395 396
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};

R
Robert Haas 已提交
397
static const SchemaQuery Query_for_list_of_tsvf = {
398 399 400
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
R
Robert Haas 已提交
401
	"c.relkind IN ('r', 'S', 'v', 'f')",
402 403 404 405 406 407 408 409 410 411
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
static const SchemaQuery Query_for_list_of_tf = {
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
	"c.relkind IN ('r', 'f')",
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};

427 428 429 430 431 432 433 434 435 436 437 438 439 440
static const SchemaQuery Query_for_list_of_views = {
	/* catname */
	"pg_catalog.pg_class c",
	/* selcondition */
	"c.relkind IN ('v')",
	/* viscondition */
	"pg_catalog.pg_table_is_visible(c.oid)",
	/* namespace */
	"c.relnamespace",
	/* result */
	"pg_catalog.quote_ident(c.relname)",
	/* qualresult */
	NULL
};
441 442


443 444 445
/*
 * Queries to get lists of names of various kinds of things, possibly
 * restricted to names matching a partially entered name.  In these queries,
446 447 448 449 450 451
 * the first %s will be replaced by the text entered so far (suitably escaped
 * to become a SQL literal string).  %d will be replaced by the length of the
 * string (in unescaped form).	A second and third %s, if present, will be
 * replaced by a suitably-escaped version of the string provided in
 * completion_info_charp.  A fourth and fifth %s are similarly replaced by
 * completion_info_charp2.
452 453 454
 *
 * Beware that the allowed sequences of %s and %d are determined by
 * _complete_from_query().
455 456
 */

457
#define Query_for_list_of_attributes \
458 459
"SELECT pg_catalog.quote_ident(attname) "\
"  FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c "\
460 461 462
" WHERE c.oid = a.attrelid "\
"   AND a.attnum > 0 "\
"   AND NOT a.attisdropped "\
463
"   AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\
464 465
"   AND (pg_catalog.quote_ident(relname)='%s' "\
"        OR '\"' || relname || '\"'='%s') "\
466 467
"   AND pg_catalog.pg_table_is_visible(c.oid)"

468 469 470 471 472 473 474 475 476 477 478 479 480
#define Query_for_list_of_attributes_with_schema \
"SELECT pg_catalog.quote_ident(attname) "\
"  FROM pg_catalog.pg_attribute a, pg_catalog.pg_class c, pg_catalog.pg_namespace n "\
" WHERE c.oid = a.attrelid "\
"   AND n.oid = c.relnamespace "\
"   AND a.attnum > 0 "\
"   AND NOT a.attisdropped "\
"   AND substring(pg_catalog.quote_ident(attname),1,%d)='%s' "\
"   AND (pg_catalog.quote_ident(relname)='%s' "\
"        OR '\"' || relname || '\"' ='%s') "\
"   AND (pg_catalog.quote_ident(nspname)='%s' "\
"        OR '\"' || nspname || '\"' ='%s') "

481 482
#define Query_for_list_of_template_databases \
"SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database "\
483
" WHERE substring(pg_catalog.quote_ident(datname),1,%d)='%s' AND datistemplate"
484

485
#define Query_for_list_of_databases \
486 487
"SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database "\
" WHERE substring(pg_catalog.quote_ident(datname),1,%d)='%s'"
488

489 490 491 492
#define Query_for_list_of_tablespaces \
"SELECT pg_catalog.quote_ident(spcname) FROM pg_catalog.pg_tablespace "\
" WHERE substring(pg_catalog.quote_ident(spcname),1,%d)='%s'"

493 494 495
#define Query_for_list_of_encodings \
" SELECT DISTINCT pg_catalog.pg_encoding_to_char(conforencoding) "\
"   FROM pg_catalog.pg_conversion "\
496
"  WHERE substring(pg_catalog.pg_encoding_to_char(conforencoding),1,%d)=UPPER('%s')"
497

498
#define Query_for_list_of_languages \
499
"SELECT pg_catalog.quote_ident(lanname) "\
500
"  FROM pg_catalog.pg_language "\
501
" WHERE lanname != 'internal' "\
502
"   AND substring(pg_catalog.quote_ident(lanname),1,%d)='%s'"
503 504

#define Query_for_list_of_schemas \
505 506
"SELECT pg_catalog.quote_ident(nspname) FROM pg_catalog.pg_namespace "\
" WHERE substring(pg_catalog.quote_ident(nspname),1,%d)='%s'"
507

508 509 510 511 512 513 514 515
#define Query_for_list_of_set_vars \
"SELECT name FROM "\
" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\
"  WHERE context IN ('user', 'superuser') "\
"  UNION ALL SELECT 'constraints' "\
"  UNION ALL SELECT 'transaction' "\
"  UNION ALL SELECT 'session' "\
"  UNION ALL SELECT 'role' "\
516
"  UNION ALL SELECT 'tablespace' "\
517 518 519 520 521 522 523 524 525 526 527 528 529 530
"  UNION ALL SELECT 'all') ss "\
" WHERE substring(name,1,%d)='%s'"

#define Query_for_list_of_show_vars \
"SELECT name FROM "\
" (SELECT pg_catalog.lower(name) AS name FROM pg_catalog.pg_settings "\
"  UNION ALL SELECT 'session authorization' "\
"  UNION ALL SELECT 'all') ss "\
" WHERE substring(name,1,%d)='%s'"

#define Query_for_list_of_roles \
" SELECT pg_catalog.quote_ident(rolname) "\
"   FROM pg_catalog.pg_roles "\
"  WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"
531

532 533 534 535 536
#define Query_for_list_of_grant_roles \
" SELECT pg_catalog.quote_ident(rolname) "\
"   FROM pg_catalog.pg_roles "\
"  WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"\
" UNION ALL SELECT 'PUBLIC'"
537

538 539
/* the silly-looking length condition is just to eat up the current word */
#define Query_for_table_owning_index \
540
"SELECT pg_catalog.quote_ident(c1.relname) "\
541 542
"  FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\
" WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\
543
"       and (%d = pg_catalog.length('%s'))"\
544
"       and pg_catalog.quote_ident(c2.relname)='%s'"\
545 546
"       and pg_catalog.pg_table_is_visible(c2.oid)"

547 548 549 550 551
/* the silly-looking length condition is just to eat up the current word */
#define Query_for_index_of_table \
"SELECT pg_catalog.quote_ident(c2.relname) "\
"  FROM pg_catalog.pg_class c1, pg_catalog.pg_class c2, pg_catalog.pg_index i"\
" WHERE c1.oid=i.indrelid and i.indexrelid=c2.oid"\
552
"       and (%d = pg_catalog.length('%s'))"\
553 554 555
"       and pg_catalog.quote_ident(c1.relname)='%s'"\
"       and pg_catalog.pg_table_is_visible(c2.oid)"

556 557 558 559
/* the silly-looking length condition is just to eat up the current word */
#define Query_for_list_of_tables_for_trigger \
"SELECT pg_catalog.quote_ident(relname) "\
"  FROM pg_catalog.pg_class"\
560
" WHERE (%d = pg_catalog.length('%s'))"\
561 562 563 564
"   AND oid IN "\
"       (SELECT tgrelid FROM pg_catalog.pg_trigger "\
"         WHERE pg_catalog.quote_ident(tgname)='%s')"

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
#define Query_for_list_of_ts_configurations \
"SELECT pg_catalog.quote_ident(cfgname) FROM pg_catalog.pg_ts_config "\
" WHERE substring(pg_catalog.quote_ident(cfgname),1,%d)='%s'"

#define Query_for_list_of_ts_dictionaries \
"SELECT pg_catalog.quote_ident(dictname) FROM pg_catalog.pg_ts_dict "\
" WHERE substring(pg_catalog.quote_ident(dictname),1,%d)='%s'"

#define Query_for_list_of_ts_parsers \
"SELECT pg_catalog.quote_ident(prsname) FROM pg_catalog.pg_ts_parser "\
" WHERE substring(pg_catalog.quote_ident(prsname),1,%d)='%s'"

#define Query_for_list_of_ts_templates \
"SELECT pg_catalog.quote_ident(tmplname) FROM pg_catalog.pg_ts_template "\
" WHERE substring(pg_catalog.quote_ident(tmplname),1,%d)='%s'"

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
#define Query_for_list_of_fdws \
" SELECT pg_catalog.quote_ident(fdwname) "\
"   FROM pg_catalog.pg_foreign_data_wrapper "\
"  WHERE substring(pg_catalog.quote_ident(fdwname),1,%d)='%s'"

#define Query_for_list_of_servers \
" SELECT pg_catalog.quote_ident(srvname) "\
"   FROM pg_catalog.pg_foreign_server "\
"  WHERE substring(pg_catalog.quote_ident(srvname),1,%d)='%s'"

#define Query_for_list_of_user_mappings \
" SELECT pg_catalog.quote_ident(usename) "\
"   FROM pg_catalog.pg_user_mappings "\
"  WHERE substring(pg_catalog.quote_ident(usename),1,%d)='%s'"

596 597 598 599 600
#define Query_for_list_of_access_methods \
" SELECT pg_catalog.quote_ident(amname) "\
"   FROM pg_catalog.pg_am "\
"  WHERE substring(pg_catalog.quote_ident(amname),1,%d)='%s'"

601 602 603 604 605
#define Query_for_list_of_arguments \
" SELECT pg_catalog.oidvectortypes(proargtypes)||')' "\
"   FROM pg_catalog.pg_proc "\
"  WHERE proname='%s'"

606 607 608 609 610 611 612 613
#define Query_for_list_of_extensions \
" SELECT pg_catalog.quote_ident(extname) "\
"   FROM pg_catalog.pg_extension "\
"  WHERE substring(pg_catalog.quote_ident(extname),1,%d)='%s'"

#define Query_for_list_of_available_extensions \
" SELECT pg_catalog.quote_ident(name) "\
"   FROM pg_catalog.pg_available_extensions "\
614
"  WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s' AND installed_version IS NULL"
615

616 617 618 619 620
#define Query_for_list_of_prepared_statements \
" SELECT pg_catalog.quote_ident(name) "\
"   FROM pg_catalog.pg_prepared_statements "\
"  WHERE substring(pg_catalog.quote_ident(name),1,%d)='%s'"

621 622 623 624
/*
 * This is a list of all "things" in Pgsql, which can show up after CREATE or
 * DROP; and there is also a query to get a list of them.
 */
625

626 627
typedef struct
{
628 629 630
	const char *name;
	const char *query;			/* simple query, or NULL */
	const SchemaQuery *squery;	/* schema query, or NULL */
631
	const bits32 flags;			/* visibility flags, see below */
632 633
} pgsql_thing_t;

634 635 636 637
#define THING_NO_CREATE		(1 << 0)	/* should not show up after CREATE */
#define THING_NO_DROP		(1 << 1)	/* should not show up after DROP */
#define THING_NO_SHOW		(THING_NO_CREATE | THING_NO_DROP)

638 639
static const pgsql_thing_t words_after_create[] = {
	{"AGGREGATE", NULL, &Query_for_list_of_aggregates},
B
Bruce Momjian 已提交
640 641
	{"CAST", NULL, NULL},		/* Casts have complex structures for names, so
								 * skip it */
642
	{"COLLATION", "SELECT pg_catalog.quote_ident(collname) FROM pg_catalog.pg_collation WHERE collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding())) AND substring(pg_catalog.quote_ident(collname),1,%d)='%s'"},
B
Bruce Momjian 已提交
643 644 645 646 647

	/*
	 * CREATE CONSTRAINT TRIGGER is not supported here because it is designed
	 * to be used only by pg_dump.
	 */
648
	{"CONFIGURATION", Query_for_list_of_ts_configurations, NULL, THING_NO_SHOW},
649 650
	{"CONVERSION", "SELECT pg_catalog.quote_ident(conname) FROM pg_catalog.pg_conversion WHERE substring(pg_catalog.quote_ident(conname),1,%d)='%s'"},
	{"DATABASE", Query_for_list_of_databases},
651
	{"DICTIONARY", Query_for_list_of_ts_dictionaries, NULL, THING_NO_SHOW},
652
	{"DOMAIN", NULL, &Query_for_list_of_domains},
653
	{"EXTENSION", Query_for_list_of_extensions},
654
	{"FOREIGN DATA WRAPPER", NULL, NULL},
R
Robert Haas 已提交
655
	{"FOREIGN TABLE", NULL, NULL},
656
	{"FUNCTION", NULL, &Query_for_list_of_functions},
657
	{"GROUP", Query_for_list_of_roles},
658 659
	{"LANGUAGE", Query_for_list_of_languages},
	{"INDEX", NULL, &Query_for_list_of_indexes},
B
Bruce Momjian 已提交
660 661
	{"OPERATOR", NULL, NULL},	/* Querying for this is probably not such a
								 * good idea. */
662
	{"OWNED", NULL, NULL, THING_NO_CREATE},		/* for DROP OWNED BY ... */
663
	{"PARSER", Query_for_list_of_ts_parsers, NULL, THING_NO_SHOW},
664
	{"ROLE", Query_for_list_of_roles},
665 666 667
	{"RULE", "SELECT pg_catalog.quote_ident(rulename) FROM pg_catalog.pg_rules WHERE substring(pg_catalog.quote_ident(rulename),1,%d)='%s'"},
	{"SCHEMA", Query_for_list_of_schemas},
	{"SEQUENCE", NULL, &Query_for_list_of_sequences},
668
	{"SERVER", Query_for_list_of_servers},
669
	{"TABLE", NULL, &Query_for_list_of_tables},
670
	{"TABLESPACE", Query_for_list_of_tablespaces},
671
	{"TEMP", NULL, NULL, THING_NO_DROP},		/* for CREATE TEMP TABLE ... */
672
	{"TEMPLATE", Query_for_list_of_ts_templates, NULL, THING_NO_SHOW},
673
	{"TEXT SEARCH", NULL, NULL},
674 675
	{"TRIGGER", "SELECT pg_catalog.quote_ident(tgname) FROM pg_catalog.pg_trigger WHERE substring(pg_catalog.quote_ident(tgname),1,%d)='%s'"},
	{"TYPE", NULL, &Query_for_list_of_datatypes},
676 677 678
	{"UNIQUE", NULL, NULL, THING_NO_DROP},		/* for CREATE UNIQUE INDEX ... */
	{"UNLOGGED", NULL, NULL, THING_NO_DROP},	/* for CREATE UNLOGGED TABLE
												 * ... */
679
	{"USER", Query_for_list_of_roles},
680
	{"USER MAPPING FOR", NULL, NULL},
681
	{"VIEW", NULL, &Query_for_list_of_views},
682
	{NULL}						/* end of list */
683 684 685
};


686 687 688
/* Forward declaration of functions */
static char **psql_completion(char *text, int start, int end);
static char *create_command_generator(const char *text, int state);
689
static char *drop_command_generator(const char *text, int state);
690 691 692 693 694
static char *complete_from_query(const char *text, int state);
static char *complete_from_schema_query(const char *text, int state);
static char *_complete_from_query(int is_schema_query,
					 const char *text, int state);
static char *complete_from_list(const char *text, int state);
695 696
static char *complete_from_const(const char *text, int state);
static char **complete_from_variables(char *text,
697
						const char *prefix, const char *suffix);
698
static char *complete_from_files(const char *text, int state);
699

700
static char *pg_strdup_same_case(const char *s, const char *ref);
701 702
static PGresult *exec_query(const char *query);

703
static void get_previous_words(int point, char **previous_words, int nwords);
704

705
#ifdef NOT_USED
706 707 708 709 710
static char *quote_file_name(char *text, int match_type, char *quote_pointer);
static char *dequote_file_name(char *text, char quote_char);
#endif


711 712 713
/*
 * Initialize the readline library for our purposes.
 */
714 715 716
void
initialize_readline(void)
{
B
Bruce Momjian 已提交
717
	rl_readline_name = (char *) pset.progname;
718 719
	rl_attempted_completion_function = (void *) psql_completion;

720
	rl_basic_word_break_characters = WORD_BREAKS;
721 722 723 724

	completion_max_records = 1000;

	/*
B
Bruce Momjian 已提交
725 726
	 * There is a variable rl_completion_query_items for this but apparently
	 * it's not defined everywhere.
727 728
	 */
}
729 730


731 732 733 734 735 736 737 738
/*
 * The completion function.
 *
 * According to readline spec this gets passed the text entered so far and its
 * start and end positions in the readline buffer. The return value is some
 * partially obscure list format that can be generated by readline's
 * completion_matches() function, so we don't have to worry about it.
 */
739
static char **
740
psql_completion(char *text, int start, int end)
741
{
742 743 744
	/* This is the variable we'll return. */
	char	  **matches = NULL;

745 746 747 748 749 750 751 752 753 754
	/* This array will contain some scannage of the input line. */
	char	   *previous_words[6];

	/* For compactness, we use these macros to reference previous_words[]. */
#define prev_wd   (previous_words[0])
#define prev2_wd  (previous_words[1])
#define prev3_wd  (previous_words[2])
#define prev4_wd  (previous_words[3])
#define prev5_wd  (previous_words[4])
#define prev6_wd  (previous_words[5])
755

B
Bruce Momjian 已提交
756
	static const char *const sql_commands[] = {
757 758
		"ABORT", "ALTER", "ANALYZE", "BEGIN", "CHECKPOINT", "CLOSE", "CLUSTER",
		"COMMENT", "COMMIT", "COPY", "CREATE", "DEALLOCATE", "DECLARE",
759
		"DELETE FROM", "DISCARD", "DO", "DROP", "END", "EXECUTE", "EXPLAIN", "FETCH",
760
		"GRANT", "INSERT", "LISTEN", "LOAD", "LOCK", "MOVE", "NOTIFY", "PREPARE",
761
		"REASSIGN", "REINDEX", "RELEASE", "RESET", "REVOKE", "ROLLBACK",
R
Robert Haas 已提交
762 763 764
		"SAVEPOINT", "SECURITY LABEL", "SELECT", "SET", "SHOW", "START",
		"TABLE", "TRUNCATE", "UNLISTEN", "UPDATE", "VACUUM", "VALUES", "WITH",
		NULL
765 766
	};

B
Bruce Momjian 已提交
767
	static const char *const backslash_commands[] = {
768
		"\\a", "\\connect", "\\conninfo", "\\C", "\\cd", "\\copy", "\\copyright",
R
Robert Haas 已提交
769
		"\\d", "\\da", "\\db", "\\dc", "\\dC", "\\dd", "\\dD", "\\des", "\\det", "\\deu", "\\dew", "\\df",
770
		"\\dF", "\\dFd", "\\dFp", "\\dFt", "\\dg", "\\di", "\\dl", "\\dL",
771
		"\\dn", "\\do", "\\dp", "\\drds", "\\ds", "\\dS", "\\dt", "\\dT", "\\dv", "\\du",
B
Bruce Momjian 已提交
772
		"\\e", "\\echo", "\\ef", "\\encoding",
R
Robert Haas 已提交
773
		"\\f", "\\g", "\\h", "\\help", "\\H", "\\i", "\\ir", "\\l",
774
		"\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
B
Bruce Momjian 已提交
775
		"\\o", "\\p", "\\password", "\\prompt", "\\pset", "\\q", "\\qecho", "\\r",
776
		"\\set", "\\sf", "\\t", "\\T",
777
		"\\timing", "\\unset", "\\x", "\\w", "\\z", "\\!", NULL
778
	};
779

780
	(void) end;					/* not used */
781

782
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
783
	rl_completion_append_character = ' ';
784 785
#endif

786 787 788 789
	/* Clear a few things. */
	completion_charp = NULL;
	completion_charpp = NULL;
	completion_info_charp = NULL;
790
	completion_info_charp2 = NULL;
791 792

	/*
793
	 * Scan the input line before our current position for the last few
B
Bruce Momjian 已提交
794
	 * words. According to those we'll make some smart decisions on what the
795
	 * user is probably intending to type.
796
	 */
797
	get_previous_words(start, previous_words, lengthof(previous_words));
798 799 800

	/* If a backslash command was started, continue */
	if (text[0] == '\\')
801
		COMPLETE_WITH_LIST_CS(backslash_commands);
802

803 804 805 806 807 808 809 810 811 812 813
	/* Variable interpolation */
	else if (text[0] == ':' && text[1] != ':')
	{
		if (text[1] == '\'')
			matches = complete_from_variables(text, ":'", "'");
		else if (text[1] == '"')
			matches = complete_from_variables(text, ":\"", "\"");
		else
			matches = complete_from_variables(text, ":", "");
	}

814
	/* If no previous word, suggest one of the basic sql commands */
815
	else if (prev_wd[0] == '\0')
816
		COMPLETE_WITH_LIST(sql_commands);
817

818 819 820
/* CREATE */
	/* complete with something you can create */
	else if (pg_strcasecmp(prev_wd, "CREATE") == 0)
B
Bruce Momjian 已提交
821
		matches = completion_matches(text, create_command_generator);
822

823
/* DROP, but not DROP embedded in other commands */
824 825
	/* complete with something you can drop */
	else if (pg_strcasecmp(prev_wd, "DROP") == 0 &&
826
			 prev2_wd[0] == '\0')
827 828
		matches = completion_matches(text, drop_command_generator);

829
/* ALTER */
B
Bruce Momjian 已提交
830 831

	/*
B
Bruce Momjian 已提交
832 833
	 * complete with what you can alter (TABLE, GROUP, USER, ...) unless we're
	 * in ALTER TABLE sth ALTER
B
Bruce Momjian 已提交
834
	 */
835 836
	else if (pg_strcasecmp(prev_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "TABLE") != 0)
837
	{
838
		static const char *const list_ALTER[] =
P
Peter Eisentraut 已提交
839
		{"AGGREGATE", "COLLATION", "CONVERSION", "DATABASE", "DEFAULT PRIVILEGES", "DOMAIN",
840 841 842 843 844
			"EXTENSION", "FOREIGN DATA WRAPPER", "FOREIGN TABLE", "FUNCTION",
			"GROUP", "INDEX", "LANGUAGE", "LARGE OBJECT", "OPERATOR",
			"ROLE", "SCHEMA", "SERVER", "SEQUENCE", "TABLE",
			"TABLESPACE", "TEXT SEARCH", "TRIGGER", "TYPE",
		"USER", "USER MAPPING FOR", "VIEW", NULL};
845 846 847

		COMPLETE_WITH_LIST(list_ALTER);
	}
848 849
	/* ALTER AGGREGATE,FUNCTION <name> */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
B
Bruce Momjian 已提交
850
			 (pg_strcasecmp(prev2_wd, "AGGREGATE") == 0 ||
851
			  pg_strcasecmp(prev2_wd, "FUNCTION") == 0))
B
Bruce Momjian 已提交
852
		COMPLETE_WITH_CONST("(");
853 854 855 856
	/* ALTER AGGREGATE,FUNCTION <name> (...) */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 (pg_strcasecmp(prev3_wd, "AGGREGATE") == 0 ||
			  pg_strcasecmp(prev3_wd, "FUNCTION") == 0))
857
	{
858 859 860 861
		if (prev_wd[strlen(prev_wd) - 1] == ')')
		{
			static const char *const list_ALTERAGG[] =
			{"OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
862

863 864 865 866 867 868 869 870 871 872
			COMPLETE_WITH_LIST(list_ALTERAGG);
		}
		else
		{
			char	   *tmp_buf = malloc(strlen(Query_for_list_of_arguments) + strlen(prev2_wd));

			sprintf(tmp_buf, Query_for_list_of_arguments, prev2_wd);
			COMPLETE_WITH_QUERY(tmp_buf);
			free(tmp_buf);
		}
873
	}
B
Bruce Momjian 已提交
874

875
	/* ALTER SCHEMA <name> */
876
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
877
			 pg_strcasecmp(prev2_wd, "SCHEMA") == 0)
878 879 880 881 882 883
	{
		static const char *const list_ALTERGEN[] =
		{"OWNER TO", "RENAME TO", NULL};

		COMPLETE_WITH_LIST(list_ALTERGEN);
	}
B
Bruce Momjian 已提交
884

P
Peter Eisentraut 已提交
885 886 887 888 889 890 891 892 893 894
	/* ALTER COLLATION <name> */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "COLLATION") == 0)
	{
		static const char *const list_ALTERGEN[] =
		{"OWNER TO", "RENAME TO", "SET SCHEMA", NULL};

		COMPLETE_WITH_LIST(list_ALTERGEN);
	}

895 896 897 898 899 900 901 902 903 904
	/* ALTER CONVERSION <name> */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "CONVERSION") == 0)
	{
		static const char *const list_ALTERGEN[] =
		{"OWNER TO", "RENAME TO", "SET SCHEMA", NULL};

		COMPLETE_WITH_LIST(list_ALTERGEN);
	}

B
Bruce Momjian 已提交
905
	/* ALTER DATABASE <name> */
906 907
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "DATABASE") == 0)
B
Bruce Momjian 已提交
908
	{
909
		static const char *const list_ALTERDATABASE[] =
910
		{"RESET", "SET", "OWNER TO", "RENAME TO", "CONNECTION LIMIT", NULL};
B
Bruce Momjian 已提交
911 912 913

		COMPLETE_WITH_LIST(list_ALTERDATABASE);
	}
914

915 916 917
	/* ALTER EXTENSION <name> */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "EXTENSION") == 0)
918 919
	{
		static const char *const list_ALTEREXTENSION[] =
920
		{"ADD", "DROP", "UPDATE", "SET SCHEMA", NULL};
921 922 923

		COMPLETE_WITH_LIST(list_ALTEREXTENSION);
	}
924

R
Robert Haas 已提交
925 926 927 928 929 930 931 932 933 934
	/* ALTER FOREIGN */
	else if (pg_strcasecmp(prev2_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev_wd, "FOREIGN") == 0)
	{
		static const char *const list_ALTER_FOREIGN[] =
		{"DATA WRAPPER", "TABLE", NULL};

		COMPLETE_WITH_LIST(list_ALTER_FOREIGN);
	}

935 936 937 938 939 940 941
	/* ALTER FOREIGN DATA WRAPPER <name> */
	else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev4_wd, "FOREIGN") == 0 &&
			 pg_strcasecmp(prev3_wd, "DATA") == 0 &&
			 pg_strcasecmp(prev2_wd, "WRAPPER") == 0)
	{
		static const char *const list_ALTER_FDW[] =
942
		{"HANDLER", "VALIDATOR", "OPTIONS", "OWNER TO", NULL};
943 944 945 946

		COMPLETE_WITH_LIST(list_ALTER_FDW);
	}

R
Robert Haas 已提交
947 948 949 950 951 952 953 954 955 956 957
	/* ALTER FOREIGN TABLE <name> */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "FOREIGN") == 0 &&
			 pg_strcasecmp(prev2_wd, "TABLE") == 0)
	{
		static const char *const list_ALTER_FOREIGN_TABLE[] =
		{"ALTER", "DROP", "RENAME", "OWNER TO", "SET SCHEMA", NULL};

		COMPLETE_WITH_LIST(list_ALTER_FOREIGN_TABLE);
	}

958 959
	/* ALTER INDEX <name> */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
960 961 962
			 pg_strcasecmp(prev2_wd, "INDEX") == 0)
	{
		static const char *const list_ALTERINDEX[] =
963
		{"OWNER TO", "RENAME TO", "SET", "RESET", NULL};
B
Bruce Momjian 已提交
964

965 966
		COMPLETE_WITH_LIST(list_ALTERINDEX);
	}
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	/* ALTER INDEX <name> SET */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "INDEX") == 0 &&
			 pg_strcasecmp(prev_wd, "SET") == 0)
	{
		static const char *const list_ALTERINDEXSET[] =
		{"(", "TABLESPACE", NULL};

		COMPLETE_WITH_LIST(list_ALTERINDEXSET);
	}
	/* ALTER INDEX <name> RESET */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "INDEX") == 0 &&
			 pg_strcasecmp(prev_wd, "RESET") == 0)
		COMPLETE_WITH_CONST("(");
	/* ALTER INDEX <foo> SET|RESET ( */
	else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev4_wd, "INDEX") == 0 &&
			 (pg_strcasecmp(prev2_wd, "SET") == 0 ||
			  pg_strcasecmp(prev2_wd, "RESET") == 0) &&
			 pg_strcasecmp(prev_wd, "(") == 0)
	{
		static const char *const list_INDEXOPTIONS[] =
		{"fillfactor", "fastupdate", NULL};

		COMPLETE_WITH_LIST(list_INDEXOPTIONS);
	}
994

995 996 997
	/* ALTER LANGUAGE <name> */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "LANGUAGE") == 0)
998 999 1000 1001 1002 1003
	{
		static const char *const list_ALTERLANGUAGE[] =
		{"OWNER TO", "RENAME TO", NULL};

		COMPLETE_WITH_LIST(list_ALTERLANGUAGE);
	}
1004

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	/* ALTER LARGE OBJECT <oid> */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "LARGE") == 0 &&
			 pg_strcasecmp(prev2_wd, "OBJECT") == 0)
	{
		static const char *const list_ALTERLARGEOBJECT[] =
		{"OWNER TO", NULL};

		COMPLETE_WITH_LIST(list_ALTERLARGEOBJECT);
	}

1016
	/* ALTER USER,ROLE <name> */
1017
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
1018
			 !(pg_strcasecmp(prev2_wd, "USER") == 0 && pg_strcasecmp(prev_wd, "MAPPING") == 0) &&
1019
			 (pg_strcasecmp(prev2_wd, "USER") == 0 ||
B
Bruce Momjian 已提交
1020
			  pg_strcasecmp(prev2_wd, "ROLE") == 0))
1021 1022
	{
		static const char *const list_ALTERUSER[] =
1023 1024 1025 1026 1027
		{"CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER",
			"ENCRYPTED", "INHERIT", "LOGIN", "NOCREATEDB", "NOCREATEROLE",
			"NOCREATEUSER", "NOINHERIT", "NOLOGIN", "NOREPLICATION",
			"NOSUPERUSER", "RENAME TO", "REPLICATION", "RESET", "SET",
		"SUPERUSER", "UNENCRYPTED", "VALID UNTIL", NULL};
1028 1029 1030 1031

		COMPLETE_WITH_LIST(list_ALTERUSER);
	}

1032 1033
	/* complete ALTER USER,ROLE <name> ENCRYPTED,UNENCRYPTED with PASSWORD */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
B
Bruce Momjian 已提交
1034 1035
			 (pg_strcasecmp(prev3_wd, "ROLE") == 0 || pg_strcasecmp(prev3_wd, "USER") == 0) &&
			 (pg_strcasecmp(prev_wd, "ENCRYPTED") == 0 || pg_strcasecmp(prev_wd, "UNENCRYPTED") == 0))
1036
	{
B
Bruce Momjian 已提交
1037
		COMPLETE_WITH_CONST("PASSWORD");
1038
	}
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	/* ALTER DEFAULT PRIVILEGES */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "DEFAULT") == 0 &&
			 pg_strcasecmp(prev_wd, "PRIVILEGES") == 0)
	{
		static const char *const list_ALTER_DEFAULT_PRIVILEGES[] =
		{"FOR ROLE", "FOR USER", "IN SCHEMA", NULL};

		COMPLETE_WITH_LIST(list_ALTER_DEFAULT_PRIVILEGES);
	}
	/* ALTER DEFAULT PRIVILEGES FOR */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "DEFAULT") == 0 &&
			 pg_strcasecmp(prev2_wd, "PRIVILEGES") == 0 &&
			 pg_strcasecmp(prev_wd, "FOR") == 0)
	{
		static const char *const list_ALTER_DEFAULT_PRIVILEGES_FOR[] =
		{"ROLE", "USER", NULL};

		COMPLETE_WITH_LIST(list_ALTER_DEFAULT_PRIVILEGES_FOR);
	}
	/* ALTER DEFAULT PRIVILEGES { FOR ROLE ... | IN SCHEMA ... } */
	else if (pg_strcasecmp(prev5_wd, "DEFAULT") == 0 &&
			 pg_strcasecmp(prev4_wd, "PRIVILEGES") == 0 &&
			 (pg_strcasecmp(prev3_wd, "FOR") == 0 ||
			  pg_strcasecmp(prev3_wd, "IN") == 0))
	{
		static const char *const list_ALTER_DEFAULT_PRIVILEGES_REST[] =
		{"GRANT", "REVOKE", NULL};

		COMPLETE_WITH_LIST(list_ALTER_DEFAULT_PRIVILEGES_REST);
	}
1071 1072 1073 1074 1075 1076
	/* ALTER DOMAIN <name> */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "DOMAIN") == 0)
	{
		static const char *const list_ALTERDOMAIN[] =
		{"ADD", "DROP", "OWNER TO", "SET", NULL};
1077

1078 1079 1080 1081 1082 1083 1084 1085
		COMPLETE_WITH_LIST(list_ALTERDOMAIN);
	}
	/* ALTER DOMAIN <sth> DROP */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "DOMAIN") == 0 &&
			 pg_strcasecmp(prev_wd, "DROP") == 0)
	{
		static const char *const list_ALTERDOMAIN2[] =
1086
		{"CONSTRAINT", "DEFAULT", "NOT NULL", NULL};
1087 1088 1089 1090 1091 1092 1093 1094 1095

		COMPLETE_WITH_LIST(list_ALTERDOMAIN2);
	}
	/* ALTER DOMAIN <sth> SET */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "DOMAIN") == 0 &&
			 pg_strcasecmp(prev_wd, "SET") == 0)
	{
		static const char *const list_ALTERDOMAIN3[] =
1096
		{"DEFAULT", "NOT NULL", "SCHEMA", NULL};
1097 1098 1099

		COMPLETE_WITH_LIST(list_ALTERDOMAIN3);
	}
1100 1101 1102 1103
	/* ALTER SEQUENCE <name> */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "SEQUENCE") == 0)
	{
B
Bruce Momjian 已提交
1104 1105
		static const char *const list_ALTERSEQUENCE[] =
		{"INCREMENT", "MINVALUE", "MAXVALUE", "RESTART", "NO", "CACHE", "CYCLE",
1106
		"SET SCHEMA", "OWNED BY", "OWNER TO", "RENAME TO", NULL};
1107

B
Bruce Momjian 已提交
1108
		COMPLETE_WITH_LIST(list_ALTERSEQUENCE);
1109 1110 1111 1112 1113 1114
	}
	/* ALTER SEQUENCE <name> NO */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "SEQUENCE") == 0 &&
			 pg_strcasecmp(prev_wd, "NO") == 0)
	{
B
Bruce Momjian 已提交
1115 1116
		static const char *const list_ALTERSEQUENCE2[] =
		{"MINVALUE", "MAXVALUE", "CYCLE", NULL};
1117

B
Bruce Momjian 已提交
1118
		COMPLETE_WITH_LIST(list_ALTERSEQUENCE2);
1119
	}
1120 1121 1122 1123 1124 1125 1126 1127 1128
	/* ALTER SERVER <name> */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "SERVER") == 0)
	{
		static const char *const list_ALTER_SERVER[] =
		{"VERSION", "OPTIONS", "OWNER TO", NULL};

		COMPLETE_WITH_LIST(list_ALTER_SERVER);
	}
1129 1130 1131 1132
	/* ALTER VIEW <name> */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "VIEW") == 0)
	{
1133 1134
		static const char *const list_ALTERVIEW[] =
		{"ALTER COLUMN", "OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
1135 1136 1137

		COMPLETE_WITH_LIST(list_ALTERVIEW);
	}
B
Bruce Momjian 已提交
1138
	/* ALTER TRIGGER <name>, add ON */
1139
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
1140
			 pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
B
Bruce Momjian 已提交
1141
		COMPLETE_WITH_CONST("ON");
1142

1143 1144 1145 1146 1147 1148
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "TRIGGER") == 0)
	{
		completion_info_charp = prev2_wd;
		COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_trigger);
	}
B
Bruce Momjian 已提交
1149 1150 1151 1152

	/*
	 * If we have ALTER TRIGGER <sth> ON, then add the correct tablename
	 */
1153 1154 1155
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
			 pg_strcasecmp(prev_wd, "ON") == 0)
1156
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
B
Bruce Momjian 已提交
1157

1158 1159 1160 1161 1162
	/* ALTER TRIGGER <name> ON <name> */
	else if (pg_strcasecmp(prev4_wd, "TRIGGER") == 0 &&
			 pg_strcasecmp(prev2_wd, "ON") == 0)
		COMPLETE_WITH_CONST("RENAME TO");

B
Bruce Momjian 已提交
1163
	/*
1164
	 * If we detect ALTER TABLE <name>, suggest sub commands
B
Bruce Momjian 已提交
1165
	 */
1166 1167
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "TABLE") == 0)
1168
	{
1169
		static const char *const list_ALTER2[] =
1170
		{"ADD", "ALTER", "CLUSTER ON", "DISABLE", "DROP", "ENABLE", "INHERIT",
1171
			"NO INHERIT", "RENAME", "RESET", "OWNER TO", "SET",
1172
		"VALIDATE CONSTRAINT", NULL};
1173 1174 1175

		COMPLETE_WITH_LIST(list_ALTER2);
	}
1176 1177 1178 1179 1180 1181
	/* ALTER TABLE xxx ENABLE */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev_wd, "ENABLE") == 0)
	{
		static const char *const list_ALTERENABLE[] =
B
Bruce Momjian 已提交
1182 1183
		{"ALWAYS", "REPLICA", "RULE", "TRIGGER", NULL};

1184 1185 1186
		COMPLETE_WITH_LIST(list_ALTERENABLE);
	}
	else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
B
Bruce Momjian 已提交
1187 1188 1189
			 pg_strcasecmp(prev2_wd, "ENABLE") == 0 &&
			 (pg_strcasecmp(prev_wd, "REPLICA") == 0 ||
			  pg_strcasecmp(prev_wd, "ALWAYS") == 0))
1190 1191 1192
	{
		static const char *const list_ALTERENABLE2[] =
		{"RULE", "TRIGGER", NULL};
B
Bruce Momjian 已提交
1193

1194 1195
		COMPLETE_WITH_LIST(list_ALTERENABLE2);
	}
B
Bruce Momjian 已提交
1196
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
1197 1198 1199 1200 1201
			 pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev_wd, "DISABLE") == 0)
	{
		static const char *const list_ALTERDISABLE[] =
		{"RULE", "TRIGGER", NULL};
B
Bruce Momjian 已提交
1202

1203 1204
		COMPLETE_WITH_LIST(list_ALTERDISABLE);
	}
B
Bruce Momjian 已提交
1205

1206
	/* If we have TABLE <sth> ALTER|RENAME, provide list of columns */
1207 1208 1209
	else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
			 (pg_strcasecmp(prev_wd, "ALTER") == 0 ||
			  pg_strcasecmp(prev_wd, "RENAME") == 0))
1210
		COMPLETE_WITH_ATTR(prev2_wd, " UNION SELECT 'COLUMN'");
1211

B
Bruce Momjian 已提交
1212 1213 1214 1215
	/*
	 * If we have TABLE <sth> ALTER COLUMN|RENAME COLUMN, provide list of
	 * columns
	 */
1216 1217 1218 1219 1220
	else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
			 (pg_strcasecmp(prev2_wd, "ALTER") == 0 ||
			  pg_strcasecmp(prev2_wd, "RENAME") == 0) &&
			 pg_strcasecmp(prev_wd, "COLUMN") == 0)
		COMPLETE_WITH_ATTR(prev3_wd, "");
B
Bruce Momjian 已提交
1221

1222 1223
	/* ALTER TABLE xxx RENAME yyy */
	else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
1224 1225
			 pg_strcasecmp(prev2_wd, "RENAME") == 0 &&
			 pg_strcasecmp(prev_wd, "TO") != 0)
1226 1227
		COMPLETE_WITH_CONST("TO");

1228 1229 1230 1231 1232 1233 1234
	/* ALTER TABLE xxx RENAME COLUMN yyy */
	else if (pg_strcasecmp(prev5_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev3_wd, "RENAME") == 0 &&
			 pg_strcasecmp(prev2_wd, "COLUMN") == 0 &&
			 pg_strcasecmp(prev_wd, "TO") != 0)
		COMPLETE_WITH_CONST("TO");

1235
	/* If we have TABLE <sth> DROP, provide COLUMN or CONSTRAINT */
1236 1237
	else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev_wd, "DROP") == 0)
1238
	{
1239 1240
		static const char *const list_TABLEDROP[] =
		{"COLUMN", "CONSTRAINT", NULL};
B
Bruce Momjian 已提交
1241

1242 1243 1244
		COMPLETE_WITH_LIST(list_TABLEDROP);
	}
	/* If we have TABLE <sth> DROP COLUMN, provide list of columns */
1245 1246 1247
	else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev2_wd, "DROP") == 0 &&
			 pg_strcasecmp(prev_wd, "COLUMN") == 0)
1248
		COMPLETE_WITH_ATTR(prev3_wd, "");
1249 1250 1251 1252 1253 1254
	/* ALTER TABLE ALTER [COLUMN] <foo> */
	else if ((pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			  pg_strcasecmp(prev2_wd, "COLUMN") == 0) ||
			 (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
			  pg_strcasecmp(prev2_wd, "ALTER") == 0))
	{
B
Bruce Momjian 已提交
1255
		static const char *const list_COLUMNALTER[] =
1256
		{"TYPE", "SET", "RESET", "DROP", NULL};
1257 1258 1259

		COMPLETE_WITH_LIST(list_COLUMNALTER);
	}
1260
	/* ALTER TABLE ALTER [COLUMN] <foo> SET */
1261 1262 1263 1264 1265 1266 1267
	else if (((pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			   pg_strcasecmp(prev3_wd, "COLUMN") == 0) ||
			  (pg_strcasecmp(prev5_wd, "TABLE") == 0 &&
			   pg_strcasecmp(prev3_wd, "ALTER") == 0)) &&
			 pg_strcasecmp(prev_wd, "SET") == 0)
	{
		static const char *const list_COLUMNSET[] =
1268
		{"(", "DEFAULT", "NOT NULL", "STATISTICS", "STORAGE", NULL};
1269 1270 1271

		COMPLETE_WITH_LIST(list_COLUMNSET);
	}
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
	/* ALTER TABLE ALTER [COLUMN] <foo> SET ( */
	else if (((pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
			   pg_strcasecmp(prev4_wd, "COLUMN") == 0) ||
			  pg_strcasecmp(prev4_wd, "ALTER") == 0) &&
			 pg_strcasecmp(prev2_wd, "SET") == 0 &&
			 pg_strcasecmp(prev_wd, "(") == 0)
	{
		static const char *const list_COLUMNOPTIONS[] =
		{"n_distinct", "n_distinct_inherited", NULL};

		COMPLETE_WITH_LIST(list_COLUMNOPTIONS);
	}
	/* ALTER TABLE ALTER [COLUMN] <foo> SET STORAGE */
	else if (((pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
			   pg_strcasecmp(prev4_wd, "COLUMN") == 0) ||
			  pg_strcasecmp(prev4_wd, "ALTER") == 0) &&
			 pg_strcasecmp(prev2_wd, "SET") == 0 &&
			 pg_strcasecmp(prev_wd, "STORAGE") == 0)
	{
		static const char *const list_COLUMNSTORAGE[] =
		{"PLAIN", "EXTERNAL", "EXTENDED", "MAIN", NULL};

		COMPLETE_WITH_LIST(list_COLUMNSTORAGE);
	}
	/* ALTER TABLE ALTER [COLUMN] <foo> DROP */
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	else if (((pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			   pg_strcasecmp(prev3_wd, "COLUMN") == 0) ||
			  (pg_strcasecmp(prev5_wd, "TABLE") == 0 &&
			   pg_strcasecmp(prev3_wd, "ALTER") == 0)) &&
			 pg_strcasecmp(prev_wd, "DROP") == 0)
	{
		static const char *const list_COLUMNDROP[] =
		{"DEFAULT", "NOT NULL", NULL};

		COMPLETE_WITH_LIST(list_COLUMNDROP);
	}
1308 1309
	else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev_wd, "CLUSTER") == 0)
1310 1311
		COMPLETE_WITH_CONST("ON");
	else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
1312
			 pg_strcasecmp(prev2_wd, "CLUSTER") == 0 &&
B
Bruce Momjian 已提交
1313
			 pg_strcasecmp(prev_wd, "ON") == 0)
1314 1315 1316 1317
	{
		completion_info_charp = prev3_wd;
		COMPLETE_WITH_QUERY(Query_for_index_of_table);
	}
1318
	/* If we have TABLE <sth> SET, provide WITHOUT,TABLESPACE and SCHEMA */
1319 1320 1321 1322
	else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev_wd, "SET") == 0)
	{
		static const char *const list_TABLESET[] =
1323
		{"(", "WITHOUT", "TABLESPACE", "SCHEMA", NULL};
1324 1325 1326

		COMPLETE_WITH_LIST(list_TABLESET);
	}
B
Bruce Momjian 已提交
1327
	/* If we have TABLE <sth> SET TABLESPACE provide a list of tablespaces */
1328 1329 1330 1331
	else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev2_wd, "SET") == 0 &&
			 pg_strcasecmp(prev_wd, "TABLESPACE") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
B
Bruce Momjian 已提交
1332
	/* If we have TABLE <sth> SET WITHOUT provide CLUSTER or OIDS */
1333 1334 1335
	else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev2_wd, "SET") == 0 &&
			 pg_strcasecmp(prev_wd, "WITHOUT") == 0)
B
Bruce Momjian 已提交
1336
	{
1337 1338
		static const char *const list_TABLESET2[] =
		{"CLUSTER", "OIDS", NULL};
1339

1340 1341
		COMPLETE_WITH_LIST(list_TABLESET2);
	}
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
	/* ALTER TABLE <foo> RESET */
	else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev_wd, "RESET") == 0)
		COMPLETE_WITH_CONST("(");
	/* ALTER TABLE <foo> SET|RESET ( */
	else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
			 (pg_strcasecmp(prev2_wd, "SET") == 0 ||
			  pg_strcasecmp(prev2_wd, "RESET") == 0) &&
			 pg_strcasecmp(prev_wd, "(") == 0)
	{
		static const char *const list_TABLEOPTIONS[] =
		{
			"autovacuum_analyze_scale_factor",
			"autovacuum_analyze_threshold",
			"autovacuum_enabled",
			"autovacuum_freeze_max_age",
			"autovacuum_freeze_min_age",
			"autovacuum_freeze_table_age",
			"autovacuum_vacuum_cost_delay",
			"autovacuum_vacuum_cost_limit",
			"autovacuum_vacuum_scale_factor",
			"autovacuum_vacuum_threshold",
			"fillfactor",
			"toast.autovacuum_enabled",
			"toast.autovacuum_freeze_max_age",
			"toast.autovacuum_freeze_min_age",
			"toast.autovacuum_freeze_table_age",
			"toast.autovacuum_vacuum_cost_delay",
			"toast.autovacuum_vacuum_cost_limit",
			"toast.autovacuum_vacuum_scale_factor",
			"toast.autovacuum_vacuum_threshold",
			NULL
		};

		COMPLETE_WITH_LIST(list_TABLEOPTIONS);
	}

	/* ALTER TABLESPACE <foo> with RENAME TO, OWNER TO, SET, RESET */
1380 1381 1382 1383
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "TABLESPACE") == 0)
	{
		static const char *const list_ALTERTSPC[] =
1384
		{"RENAME TO", "OWNER TO", "SET", "RESET", NULL};
1385 1386 1387

		COMPLETE_WITH_LIST(list_ALTERTSPC);
	}
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
	/* ALTER TABLESPACE <foo> SET|RESET */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "TABLESPACE") == 0 &&
			 (pg_strcasecmp(prev_wd, "SET") == 0 ||
			  pg_strcasecmp(prev_wd, "RESET") == 0))
		COMPLETE_WITH_CONST("(");
	/* ALTER TABLESPACE <foo> SET|RESET ( */
	else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev4_wd, "TABLESPACE") == 0 &&
			 (pg_strcasecmp(prev2_wd, "SET") == 0 ||
			  pg_strcasecmp(prev2_wd, "RESET") == 0) &&
			 pg_strcasecmp(prev_wd, "(") == 0)
	{
		static const char *const list_TABLESPACEOPTIONS[] =
		{"seq_page_cost", "random_page_cost", NULL};

		COMPLETE_WITH_LIST(list_TABLESPACEOPTIONS);
	}

1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
	/* ALTER TEXT SEARCH */
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "TEXT") == 0 &&
			 pg_strcasecmp(prev_wd, "SEARCH") == 0)
	{
		static const char *const list_ALTERTEXTSEARCH[] =
		{"CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE", NULL};

		COMPLETE_WITH_LIST(list_ALTERTEXTSEARCH);
	}
	else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev4_wd, "TEXT") == 0 &&
			 pg_strcasecmp(prev3_wd, "SEARCH") == 0 &&
			 (pg_strcasecmp(prev2_wd, "TEMPLATE") == 0 ||
B
Bruce Momjian 已提交
1421
			  pg_strcasecmp(prev2_wd, "PARSER") == 0))
1422 1423 1424 1425 1426 1427
	{
		static const char *const list_ALTERTEXTSEARCH2[] =
		{"RENAME TO", "SET SCHEMA", NULL};

		COMPLETE_WITH_LIST(list_ALTERTEXTSEARCH2);
	}
1428 1429 1430 1431

	else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev4_wd, "TEXT") == 0 &&
			 pg_strcasecmp(prev3_wd, "SEARCH") == 0 &&
B
Bruce Momjian 已提交
1432
			 pg_strcasecmp(prev2_wd, "DICTIONARY") == 0)
1433
	{
1434 1435
		static const char *const list_ALTERTEXTSEARCH3[] =
		{"OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
1436

1437
		COMPLETE_WITH_LIST(list_ALTERTEXTSEARCH3);
1438 1439 1440 1441 1442 1443 1444
	}

	else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev4_wd, "TEXT") == 0 &&
			 pg_strcasecmp(prev3_wd, "SEARCH") == 0 &&
			 pg_strcasecmp(prev2_wd, "CONFIGURATION") == 0)
	{
1445 1446
		static const char *const list_ALTERTEXTSEARCH4[] =
		{"ADD MAPPING FOR", "ALTER MAPPING", "DROP MAPPING FOR", "OWNER TO", "RENAME TO", "SET SCHEMA", NULL};
1447

1448
		COMPLETE_WITH_LIST(list_ALTERTEXTSEARCH4);
1449 1450
	}

1451
	/* complete ALTER TYPE <foo> with actions */
1452 1453
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "TYPE") == 0)
1454 1455
	{
		static const char *const list_ALTERTYPE[] =
1456
		{"ADD ATTRIBUTE", "ADD VALUE", "ALTER ATTRIBUTE", "DROP ATTRIBUTE",
1457
		"OWNER TO", "RENAME", "SET SCHEMA", NULL};
B
Bruce Momjian 已提交
1458

1459 1460
		COMPLETE_WITH_LIST(list_ALTERTYPE);
	}
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
	/* complete ALTER TYPE <foo> ADD with actions */
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "TYPE") == 0 &&
			 pg_strcasecmp(prev_wd, "ADD") == 0)
	{
		static const char *const list_ALTERTYPE[] =
		{"ATTRIBUTE", "VALUE", NULL};

		COMPLETE_WITH_LIST(list_ALTERTYPE);
	}
1471
	/* ALTER TYPE <foo> RENAME	*/
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "TYPE") == 0 &&
			 pg_strcasecmp(prev_wd, "RENAME") == 0)
	{
		static const char *const list_ALTERTYPE[] =
		{"ATTRIBUTE", "TO", NULL};

		COMPLETE_WITH_LIST(list_ALTERTYPE);
	}
	/* ALTER TYPE xxx RENAME ATTRIBUTE yyy */
	else if (pg_strcasecmp(prev5_wd, "TYPE") == 0 &&
			 pg_strcasecmp(prev3_wd, "RENAME") == 0 &&
			 pg_strcasecmp(prev2_wd, "ATTRIBUTE") == 0)
		COMPLETE_WITH_CONST("TO");

1487 1488 1489 1490
	/*
	 * If we have TYPE <sth> ALTER/DROP/RENAME ATTRIBUTE, provide list of
	 * attributes
	 */
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
	else if (pg_strcasecmp(prev4_wd, "TYPE") == 0 &&
			 (pg_strcasecmp(prev2_wd, "ALTER") == 0 ||
			  pg_strcasecmp(prev2_wd, "DROP") == 0 ||
			  pg_strcasecmp(prev2_wd, "RENAME") == 0) &&
			 pg_strcasecmp(prev_wd, "ATTRIBUTE") == 0)
		COMPLETE_WITH_ATTR(prev3_wd, "");
	/* ALTER TYPE ALTER ATTRIBUTE <foo> */
	else if ((pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			  pg_strcasecmp(prev2_wd, "ATTRIBUTE") == 0))
	{
		COMPLETE_WITH_CONST("TYPE");
	}
1503
	/* complete ALTER GROUP <foo> */
1504 1505
	else if (pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev2_wd, "GROUP") == 0)
1506
	{
1507
		static const char *const list_ALTERGROUP[] =
1508
		{"ADD USER", "DROP USER", "RENAME TO", NULL};
B
Bruce Momjian 已提交
1509

1510 1511 1512
		COMPLETE_WITH_LIST(list_ALTERGROUP);
	}
	/* complete ALTER GROUP <foo> ADD|DROP with USER */
1513 1514 1515 1516
	else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
			 pg_strcasecmp(prev3_wd, "GROUP") == 0 &&
			 (pg_strcasecmp(prev_wd, "ADD") == 0 ||
			  pg_strcasecmp(prev_wd, "DROP") == 0))
1517 1518
		COMPLETE_WITH_CONST("USER");
	/* complete {ALTER} GROUP <foo> ADD|DROP USER with a user name */
1519 1520 1521 1522
	else if (pg_strcasecmp(prev4_wd, "GROUP") == 0 &&
			 (pg_strcasecmp(prev2_wd, "ADD") == 0 ||
			  pg_strcasecmp(prev2_wd, "DROP") == 0) &&
			 pg_strcasecmp(prev_wd, "USER") == 0)
1523
		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
1524

1525
/* BEGIN, END, ABORT */
1526
	else if (pg_strcasecmp(prev_wd, "BEGIN") == 0 ||
B
Bruce Momjian 已提交
1527 1528
			 pg_strcasecmp(prev_wd, "END") == 0 ||
			 pg_strcasecmp(prev_wd, "ABORT") == 0)
1529
	{
B
Bruce Momjian 已提交
1530
		static const char *const list_TRANS[] =
1531 1532
		{"WORK", "TRANSACTION", NULL};

1533
		COMPLETE_WITH_LIST(list_TRANS);
B
Bruce Momjian 已提交
1534
	}
1535
/* COMMIT */
B
Bruce Momjian 已提交
1536
	else if (pg_strcasecmp(prev_wd, "COMMIT") == 0)
1537 1538 1539 1540 1541
	{
		static const char *const list_COMMIT[] =
		{"WORK", "TRANSACTION", "PREPARED", NULL};

		COMPLETE_WITH_LIST(list_COMMIT);
1542
	}
1543
/* RELEASE SAVEPOINT */
B
Bruce Momjian 已提交
1544
	else if (pg_strcasecmp(prev_wd, "RELEASE") == 0)
1545
		COMPLETE_WITH_CONST("SAVEPOINT");
1546
/* ROLLBACK*/
B
Bruce Momjian 已提交
1547
	else if (pg_strcasecmp(prev_wd, "ROLLBACK") == 0)
1548
	{
B
Bruce Momjian 已提交
1549
		static const char *const list_TRANS[] =
1550
		{"WORK", "TRANSACTION", "TO SAVEPOINT", "PREPARED", NULL};
1551

1552 1553
		COMPLETE_WITH_LIST(list_TRANS);
	}
1554
/* CLUSTER */
B
Bruce Momjian 已提交
1555 1556

	/*
B
Bruce Momjian 已提交
1557
	 * If the previous word is CLUSTER and not without produce list of tables
B
Bruce Momjian 已提交
1558
	 */
1559
	else if (pg_strcasecmp(prev_wd, "CLUSTER") == 0 &&
B
Bruce Momjian 已提交
1560
			 pg_strcasecmp(prev2_wd, "WITHOUT") != 0)
1561 1562
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
	/* If we have CLUSTER <sth>, then add "USING" */
B
Bruce Momjian 已提交
1563
	else if (pg_strcasecmp(prev2_wd, "CLUSTER") == 0 &&
B
Bruce Momjian 已提交
1564 1565
			 pg_strcasecmp(prev_wd, "ON") != 0)
	{
1566 1567
		COMPLETE_WITH_CONST("USING");
	}
1568 1569

	/*
1570
	 * If we have CLUSTER <sth> USING, then add the index as well.
1571
	 */
1572
	else if (pg_strcasecmp(prev3_wd, "CLUSTER") == 0 &&
1573
			 pg_strcasecmp(prev_wd, "USING") == 0)
1574
	{
1575
		completion_info_charp = prev2_wd;
1576
		COMPLETE_WITH_QUERY(Query_for_index_of_table);
1577
	}
1578

1579
/* COMMENT */
1580
	else if (pg_strcasecmp(prev_wd, "COMMENT") == 0)
1581
		COMPLETE_WITH_CONST("ON");
1582 1583
	else if (pg_strcasecmp(prev2_wd, "COMMENT") == 0 &&
			 pg_strcasecmp(prev_wd, "ON") == 0)
1584
	{
1585
		static const char *const list_COMMENT[] =
1586 1587 1588 1589
		{"CAST", "COLLATION", "CONVERSION", "DATABASE", "EXTENSION",
			"FOREIGN DATA WRAPPER", "FOREIGN TABLE",
			"SERVER", "INDEX", "LANGUAGE", "RULE", "SCHEMA", "SEQUENCE",
			"TABLE", "TYPE", "VIEW", "COLUMN", "AGGREGATE", "FUNCTION",
1590
			"OPERATOR", "TRIGGER", "CONSTRAINT", "DOMAIN", "LARGE OBJECT",
1591
		"TABLESPACE", "TEXT SEARCH", "ROLE", NULL};
B
Bruce Momjian 已提交
1592

1593 1594
		COMPLETE_WITH_LIST(list_COMMENT);
	}
1595 1596 1597 1598 1599 1600 1601 1602 1603
	else if (pg_strcasecmp(prev3_wd, "COMMENT") == 0 &&
			 pg_strcasecmp(prev2_wd, "ON") == 0 &&
			 pg_strcasecmp(prev_wd, "FOREIGN") == 0)
	{
		static const char *const list_TRANS2[] =
		{"DATA WRAPPER", "TABLE", NULL};

		COMPLETE_WITH_LIST(list_TRANS2);
	}
1604
	else if (pg_strcasecmp(prev4_wd, "COMMENT") == 0 &&
B
Bruce Momjian 已提交
1605 1606
			 pg_strcasecmp(prev3_wd, "ON") == 0 &&
			 pg_strcasecmp(prev2_wd, "TEXT") == 0 &&
1607 1608 1609 1610 1611 1612 1613 1614
			 pg_strcasecmp(prev_wd, "SEARCH") == 0)
	{
		static const char *const list_TRANS2[] =
		{"CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE", NULL};

		COMPLETE_WITH_LIST(list_TRANS2);
	}
	else if ((pg_strcasecmp(prev4_wd, "COMMENT") == 0 &&
B
Bruce Momjian 已提交
1615
			  pg_strcasecmp(prev3_wd, "ON") == 0) ||
1616 1617
			 (pg_strcasecmp(prev5_wd, "COMMENT") == 0 &&
			  pg_strcasecmp(prev4_wd, "ON") == 0) ||
1618
			 (pg_strcasecmp(prev6_wd, "COMMENT") == 0 &&
1619
			  pg_strcasecmp(prev5_wd, "ON") == 0))
1620 1621
		COMPLETE_WITH_CONST("IS");

1622
/* COPY */
1623 1624 1625 1626 1627

	/*
	 * If we have COPY [BINARY] (which you'd have to type yourself), offer
	 * list of tables (Also cover the analogous backslash command)
	 */
1628 1629 1630 1631
	else if (pg_strcasecmp(prev_wd, "COPY") == 0 ||
			 pg_strcasecmp(prev_wd, "\\copy") == 0 ||
			 (pg_strcasecmp(prev2_wd, "COPY") == 0 &&
			  pg_strcasecmp(prev_wd, "BINARY") == 0))
1632
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
1633
	/* If we have COPY|BINARY <sth>, complete it with "TO" or "FROM" */
1634 1635 1636
	else if (pg_strcasecmp(prev2_wd, "COPY") == 0 ||
			 pg_strcasecmp(prev2_wd, "\\copy") == 0 ||
			 pg_strcasecmp(prev2_wd, "BINARY") == 0)
B
Bruce Momjian 已提交
1637 1638 1639
	{
		static const char *const list_FROMTO[] =
		{"FROM", "TO", NULL};
1640

B
Bruce Momjian 已提交
1641 1642
		COMPLETE_WITH_LIST(list_FROMTO);
	}
1643 1644 1645 1646 1647 1648
	/* If we have COPY|BINARY <sth> FROM|TO, complete with filename */
	else if ((pg_strcasecmp(prev3_wd, "COPY") == 0 ||
			  pg_strcasecmp(prev3_wd, "\\copy") == 0 ||
			  pg_strcasecmp(prev3_wd, "BINARY") == 0) &&
			 (pg_strcasecmp(prev_wd, "FROM") == 0 ||
			  pg_strcasecmp(prev_wd, "TO") == 0))
1649 1650 1651 1652
	{
		completion_charp = "";
		matches = completion_matches(text, complete_from_files);
	}
1653 1654 1655 1656 1657 1658 1659

	/* Handle COPY|BINARY <sth> FROM|TO filename */
	else if ((pg_strcasecmp(prev4_wd, "COPY") == 0 ||
			  pg_strcasecmp(prev4_wd, "\\copy") == 0 ||
			  pg_strcasecmp(prev4_wd, "BINARY") == 0) &&
			 (pg_strcasecmp(prev2_wd, "FROM") == 0 ||
			  pg_strcasecmp(prev2_wd, "TO") == 0))
B
Bruce Momjian 已提交
1660 1661
	{
		static const char *const list_COPY[] =
1662
		{"BINARY", "OIDS", "DELIMITER", "NULL", "CSV", "ENCODING", NULL};
1663

B
Bruce Momjian 已提交
1664 1665
		COMPLETE_WITH_LIST(list_COPY);
	}
1666 1667

	/* Handle COPY|BINARY <sth> FROM|TO filename CSV */
B
Bruce Momjian 已提交
1668
	else if (pg_strcasecmp(prev_wd, "CSV") == 0 &&
1669 1670
			 (pg_strcasecmp(prev3_wd, "FROM") == 0 ||
			  pg_strcasecmp(prev3_wd, "TO") == 0))
1671
	{
B
Bruce Momjian 已提交
1672
		static const char *const list_CSV[] =
1673
		{"HEADER", "QUOTE", "ESCAPE", "FORCE QUOTE", "FORCE NOT NULL", NULL};
1674

B
Bruce Momjian 已提交
1675
		COMPLETE_WITH_LIST(list_CSV);
1676
	}
1677

1678 1679 1680 1681 1682 1683
	/* CREATE DATABASE */
	else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev2_wd, "DATABASE") == 0)
	{
		static const char *const list_DATABASE[] =
		{"OWNER", "TEMPLATE", "ENCODING", "TABLESPACE", "CONNECTION LIMIT",
B
Bruce Momjian 已提交
1684
		NULL};
1685 1686 1687 1688

		COMPLETE_WITH_LIST(list_DATABASE);
	}

1689 1690 1691 1692 1693
	else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev3_wd, "DATABASE") == 0 &&
			 pg_strcasecmp(prev_wd, "TEMPLATE") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);

1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
	/* CREATE EXTENSION */
	/* Complete with available extensions rather than installed ones. */
	else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev_wd, "EXTENSION") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_available_extensions);
	/* CREATE EXTENSION <name> */
	else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev2_wd, "EXTENSION") == 0)
		COMPLETE_WITH_CONST("WITH SCHEMA");

R
Robert Haas 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
	/* CREATE FOREIGN */
	else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev_wd, "FOREIGN") == 0)
	{
		static const char *const list_CREATE_FOREIGN[] =
		{"DATA WRAPPER", "TABLE", NULL};

		COMPLETE_WITH_LIST(list_CREATE_FOREIGN);
	}

1714 1715 1716 1717 1718
	/* CREATE FOREIGN DATA WRAPPER */
	else if (pg_strcasecmp(prev5_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev4_wd, "FOREIGN") == 0 &&
			 pg_strcasecmp(prev3_wd, "DATA") == 0 &&
			 pg_strcasecmp(prev2_wd, "WRAPPER") == 0)
1719 1720 1721 1722 1723 1724
	{
		static const char *const list_CREATE_FOREIGN_DATA_WRAPPER[] =
		{"HANDLER", "VALIDATOR", NULL};

		COMPLETE_WITH_LIST(list_CREATE_FOREIGN_DATA_WRAPPER);
	}
1725

1726
	/* CREATE INDEX */
1727
	/* First off we complete CREATE UNIQUE with "INDEX" */
1728 1729
	else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev_wd, "UNIQUE") == 0)
1730
		COMPLETE_WITH_CONST("INDEX");
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
	/* If we have CREATE|UNIQUE INDEX, then add "ON" and existing indexes */
	else if (pg_strcasecmp(prev_wd, "INDEX") == 0 &&
			 (pg_strcasecmp(prev2_wd, "CREATE") == 0 ||
			  pg_strcasecmp(prev2_wd, "UNIQUE") == 0))
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes,
								   " UNION SELECT 'ON'"
								   " UNION SELECT 'CONCURRENTLY'");
	/* Complete ... INDEX [<name>] ON with a list of tables  */
	else if ((pg_strcasecmp(prev3_wd, "INDEX") == 0 ||
			  pg_strcasecmp(prev2_wd, "INDEX") == 0 ||
			  pg_strcasecmp(prev2_wd, "CONCURRENTLY") == 0) &&
1742
			 pg_strcasecmp(prev_wd, "ON") == 0)
1743
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
	/* If we have CREATE|UNIQUE INDEX <sth> CONCURRENTLY, then add "ON" */
	else if ((pg_strcasecmp(prev3_wd, "INDEX") == 0 ||
			  pg_strcasecmp(prev2_wd, "INDEX") == 0) &&
			 pg_strcasecmp(prev_wd, "CONCURRENTLY") == 0)
		COMPLETE_WITH_CONST("ON");
	/* If we have CREATE|UNIQUE INDEX <sth>, then add "ON" or "CONCURRENTLY" */
	else if ((pg_strcasecmp(prev3_wd, "CREATE") == 0 ||
			  pg_strcasecmp(prev3_wd, "UNIQUE") == 0) &&
			 pg_strcasecmp(prev2_wd, "INDEX") == 0)
	{
		static const char *const list_CREATE_INDEX[] =
		{"CONCURRENTLY", "ON", NULL};

		COMPLETE_WITH_LIST(list_CREATE_INDEX);
	}
1759 1760

	/*
B
Bruce Momjian 已提交
1761 1762
	 * Complete INDEX <name> ON <table> with a list of table columns (which
	 * should really be in parens)
1763
	 */
1764 1765 1766
	else if ((pg_strcasecmp(prev4_wd, "INDEX") == 0 ||
			  pg_strcasecmp(prev3_wd, "INDEX") == 0 ||
			  pg_strcasecmp(prev3_wd, "CONCURRENTLY") == 0) &&
1767
			 pg_strcasecmp(prev2_wd, "ON") == 0)
1768
	{
1769
		static const char *const list_CREATE_INDEX2[] =
B
Bruce Momjian 已提交
1770
		{"(", "USING", NULL};
1771 1772

		COMPLETE_WITH_LIST(list_CREATE_INDEX2);
1773
	}
1774 1775 1776
	else if ((pg_strcasecmp(prev5_wd, "INDEX") == 0 ||
			  pg_strcasecmp(prev4_wd, "INDEX") == 0 ||
			  pg_strcasecmp(prev4_wd, "CONCURRENTLY") == 0) &&
B
Bruce Momjian 已提交
1777 1778
			 pg_strcasecmp(prev3_wd, "ON") == 0 &&
			 pg_strcasecmp(prev_wd, "(") == 0)
1779
		COMPLETE_WITH_ATTR(prev2_wd, "");
1780
	/* same if you put in USING */
1781 1782 1783 1784
	else if (pg_strcasecmp(prev5_wd, "ON") == 0 &&
			 pg_strcasecmp(prev3_wd, "USING") == 0 &&
			 pg_strcasecmp(prev_wd, "(") == 0)
		COMPLETE_WITH_ATTR(prev4_wd, "");
1785
	/* Complete USING with an index method */
1786
	else if (pg_strcasecmp(prev_wd, "USING") == 0)
1787 1788 1789 1790
		COMPLETE_WITH_QUERY(Query_for_list_of_access_methods);
	else if (pg_strcasecmp(prev4_wd, "ON") == 0 &&
			 pg_strcasecmp(prev2_wd, "USING") == 0)
		COMPLETE_WITH_CONST("(");
1791

1792
/* CREATE RULE */
1793
	/* Complete "CREATE RULE <sth>" with "AS" */
1794 1795
	else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev2_wd, "RULE") == 0)
1796 1797
		COMPLETE_WITH_CONST("AS");
	/* Complete "CREATE RULE <sth> AS with "ON" */
1798 1799 1800
	else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev3_wd, "RULE") == 0 &&
			 pg_strcasecmp(prev_wd, "AS") == 0)
1801 1802
		COMPLETE_WITH_CONST("ON");
	/* Complete "RULE * AS ON" with SELECT|UPDATE|DELETE|INSERT */
1803 1804 1805
	else if (pg_strcasecmp(prev4_wd, "RULE") == 0 &&
			 pg_strcasecmp(prev2_wd, "AS") == 0 &&
			 pg_strcasecmp(prev_wd, "ON") == 0)
1806
	{
1807 1808
		static const char *const rule_events[] =
		{"SELECT", "UPDATE", "INSERT", "DELETE", NULL};
1809 1810 1811 1812

		COMPLETE_WITH_LIST(rule_events);
	}
	/* Complete "AS ON <sth with a 'T' :)>" with a "TO" */
1813 1814
	else if (pg_strcasecmp(prev3_wd, "AS") == 0 &&
			 pg_strcasecmp(prev2_wd, "ON") == 0 &&
1815 1816
			 (pg_toupper((unsigned char) prev_wd[4]) == 'T' ||
			  pg_toupper((unsigned char) prev_wd[5]) == 'T'))
1817 1818
		COMPLETE_WITH_CONST("TO");
	/* Complete "AS ON <sth> TO" with a table name */
1819 1820 1821
	else if (pg_strcasecmp(prev4_wd, "AS") == 0 &&
			 pg_strcasecmp(prev3_wd, "ON") == 0 &&
			 pg_strcasecmp(prev_wd, "TO") == 0)
1822
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
1823

1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
/* CREATE SERVER <name> */
	else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev2_wd, "SERVER") == 0)
	{
		static const char *const list_CREATE_SERVER[] =
		{"TYPE", "VERSION", "FOREIGN DATA WRAPPER", NULL};

		COMPLETE_WITH_LIST(list_CREATE_SERVER);
	}

1834
/* CREATE TABLE */
1835
	/* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */
1836
	else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
1837 1838
			 (pg_strcasecmp(prev_wd, "TEMP") == 0 ||
			  pg_strcasecmp(prev_wd, "TEMPORARY") == 0))
1839 1840
	{
		static const char *const list_TEMP[] =
B
Bruce Momjian 已提交
1841
		{"SEQUENCE", "TABLE", "VIEW", NULL};
1842 1843 1844

		COMPLETE_WITH_LIST(list_TEMP);
	}
1845 1846 1847 1848 1849 1850
	/* Complete "CREATE UNLOGGED" with TABLE */
	else if (pg_strcasecmp(prev2_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev_wd, "UNLOGGED") == 0)
	{
		COMPLETE_WITH_CONST("TABLE");
	}
1851

1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
/* CREATE TABLESPACE */
	else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev2_wd, "TABLESPACE") == 0)
	{
		static const char *const list_CREATETABLESPACE[] =
		{"OWNER", "LOCATION", NULL};

		COMPLETE_WITH_LIST(list_CREATETABLESPACE);
	}
	/* Complete CREATE TABLESPACE name OWNER name with "LOCATION" */
	else if (pg_strcasecmp(prev5_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev4_wd, "TABLESPACE") == 0 &&
			 pg_strcasecmp(prev2_wd, "OWNER") == 0)
	{
		COMPLETE_WITH_CONST("LOCATION");
	}

1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
/* CREATE TEXT SEARCH */
	else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev2_wd, "TEXT") == 0 &&
			 pg_strcasecmp(prev_wd, "SEARCH") == 0)
	{
		static const char *const list_CREATETEXTSEARCH[] =
		{"CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE", NULL};

		COMPLETE_WITH_LIST(list_CREATETEXTSEARCH);
	}
	else if (pg_strcasecmp(prev4_wd, "TEXT") == 0 &&
B
Bruce Momjian 已提交
1880 1881 1882
			 pg_strcasecmp(prev3_wd, "SEARCH") == 0 &&
			 pg_strcasecmp(prev2_wd, "CONFIGURATION") == 0)
		COMPLETE_WITH_CONST("(");
1883

1884
/* CREATE TRIGGER */
1885 1886
	/* complete CREATE TRIGGER <name> with BEFORE,AFTER */
	else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
B
Bruce Momjian 已提交
1887
			 pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
1888 1889
	{
		static const char *const list_CREATETRIGGER[] =
1890
		{"BEFORE", "AFTER", "INSTEAD OF", NULL};
B
Bruce Momjian 已提交
1891 1892

		COMPLETE_WITH_LIST(list_CREATETRIGGER);
1893
	}
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
	/* complete CREATE TRIGGER <name> BEFORE,AFTER with an event */
	else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev3_wd, "TRIGGER") == 0 &&
			 (pg_strcasecmp(prev_wd, "BEFORE") == 0 ||
			  pg_strcasecmp(prev_wd, "AFTER") == 0))
	{
		static const char *const list_CREATETRIGGER_EVENTS[] =
		{"INSERT", "DELETE", "UPDATE", "TRUNCATE", NULL};

		COMPLETE_WITH_LIST(list_CREATETRIGGER_EVENTS);
	}
1905
	/* complete CREATE TRIGGER <name> INSTEAD OF with an event */
1906
	else if (pg_strcasecmp(prev5_wd, "CREATE") == 0 &&
B
Bruce Momjian 已提交
1907
			 pg_strcasecmp(prev4_wd, "TRIGGER") == 0 &&
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
			 pg_strcasecmp(prev2_wd, "INSTEAD") == 0 &&
			 pg_strcasecmp(prev_wd, "OF") == 0)
	{
		static const char *const list_CREATETRIGGER_EVENTS[] =
		{"INSERT", "DELETE", "UPDATE", NULL};

		COMPLETE_WITH_LIST(list_CREATETRIGGER_EVENTS);
	}
	/* complete CREATE TRIGGER <name> BEFORE,AFTER sth with OR,ON */
	else if ((pg_strcasecmp(prev5_wd, "CREATE") == 0 &&
			  pg_strcasecmp(prev4_wd, "TRIGGER") == 0 &&
			  (pg_strcasecmp(prev2_wd, "BEFORE") == 0 ||
			   pg_strcasecmp(prev2_wd, "AFTER") == 0)) ||
			 (pg_strcasecmp(prev5_wd, "TRIGGER") == 0 &&
			  pg_strcasecmp(prev3_wd, "INSTEAD") == 0 &&
			  pg_strcasecmp(prev2_wd, "OF") == 0))
1924 1925
	{
		static const char *const list_CREATETRIGGER2[] =
B
Bruce Momjian 已提交
1926 1927
		{"ON", "OR", NULL};

1928 1929
		COMPLETE_WITH_LIST(list_CREATETRIGGER2);
	}
B
Bruce Momjian 已提交
1930 1931 1932 1933 1934

	/*
	 * complete CREATE TRIGGER <name> BEFORE,AFTER event ON with a list of
	 * tables
	 */
1935 1936 1937 1938 1939
	else if (pg_strcasecmp(prev5_wd, "TRIGGER") == 0 &&
			 (pg_strcasecmp(prev3_wd, "BEFORE") == 0 ||
			  pg_strcasecmp(prev3_wd, "AFTER") == 0) &&
			 pg_strcasecmp(prev_wd, "ON") == 0)
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
1940 1941 1942 1943 1944
	/* complete CREATE TRIGGER ... INSTEAD OF event ON with a list of views */
	else if (pg_strcasecmp(prev4_wd, "INSTEAD") == 0 &&
			 pg_strcasecmp(prev3_wd, "OF") == 0 &&
			 pg_strcasecmp(prev_wd, "ON") == 0)
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL);
1945
	/* complete CREATE TRIGGER ... EXECUTE with PROCEDURE */
1946 1947
	else if (pg_strcasecmp(prev_wd, "EXECUTE") == 0 &&
			 prev2_wd[0] != '\0')
1948
		COMPLETE_WITH_CONST("PROCEDURE");
1949 1950 1951

/* CREATE ROLE,USER,GROUP */
	else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
1952
			 !(pg_strcasecmp(prev2_wd, "USER") == 0 && pg_strcasecmp(prev_wd, "MAPPING") == 0) &&
1953 1954 1955 1956
			 (pg_strcasecmp(prev2_wd, "ROLE") == 0 ||
			  pg_strcasecmp(prev2_wd, "GROUP") == 0 || pg_strcasecmp(prev2_wd, "USER") == 0))
	{
		static const char *const list_CREATEROLE[] =
B
Bruce Momjian 已提交
1957
		{"ADMIN", "CONNECTION LIMIT", "CREATEDB", "CREATEROLE", "CREATEUSER",
1958 1959 1960 1961
			"ENCRYPTED", "IN", "INHERIT", "LOGIN", "NOCREATEDB",
			"NOCREATEROLE", "NOCREATEUSER", "NOINHERIT", "NOLOGIN",
			"NOREPLICATION", "NOSUPERUSER", "REPLICATION", "ROLE",
		"SUPERUSER", "SYSID", "UNENCRYPTED", "VALID UNTIL", NULL};
B
Bruce Momjian 已提交
1962 1963

		COMPLETE_WITH_LIST(list_CREATEROLE);
1964
	}
B
Bruce Momjian 已提交
1965 1966 1967 1968 1969

	/*
	 * complete CREATE ROLE,USER,GROUP <name> ENCRYPTED,UNENCRYPTED with
	 * PASSWORD
	 */
1970
	else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
B
Bruce Momjian 已提交
1971 1972 1973
			 (pg_strcasecmp(prev3_wd, "ROLE") == 0 ||
			  pg_strcasecmp(prev3_wd, "GROUP") == 0 || pg_strcasecmp(prev3_wd, "USER") == 0) &&
			 (pg_strcasecmp(prev_wd, "ENCRYPTED") == 0 || pg_strcasecmp(prev_wd, "UNENCRYPTED") == 0))
1974
	{
B
Bruce Momjian 已提交
1975
		COMPLETE_WITH_CONST("PASSWORD");
1976 1977 1978
	}
	/* complete CREATE ROLE,USER,GROUP <name> IN with ROLE,GROUP */
	else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
B
Bruce Momjian 已提交
1979 1980 1981
			 (pg_strcasecmp(prev3_wd, "ROLE") == 0 ||
			  pg_strcasecmp(prev3_wd, "GROUP") == 0 || pg_strcasecmp(prev3_wd, "USER") == 0) &&
			 pg_strcasecmp(prev_wd, "IN") == 0)
1982 1983
	{
		static const char *const list_CREATEROLE3[] =
B
Bruce Momjian 已提交
1984 1985 1986
		{"GROUP", "ROLE", NULL};

		COMPLETE_WITH_LIST(list_CREATEROLE3);
1987
	}
1988 1989

/* CREATE VIEW */
1990
	/* Complete CREATE VIEW <name> with AS */
1991 1992
	else if (pg_strcasecmp(prev3_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev2_wd, "VIEW") == 0)
1993 1994
		COMPLETE_WITH_CONST("AS");
	/* Complete "CREATE VIEW <sth> AS with "SELECT" */
1995 1996 1997
	else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev3_wd, "VIEW") == 0 &&
			 pg_strcasecmp(prev_wd, "AS") == 0)
1998
		COMPLETE_WITH_CONST("SELECT");
1999

2000 2001 2002
/* DECLARE */
	else if (pg_strcasecmp(prev2_wd, "DECLARE") == 0)
	{
B
Bruce Momjian 已提交
2003 2004 2005 2006
		static const char *const list_DECLARE[] =
		{"BINARY", "INSENSITIVE", "SCROLL", "NO SCROLL", "CURSOR", NULL};

		COMPLETE_WITH_LIST(list_DECLARE);
2007 2008
	}

2009
/* CURSOR */
2010 2011
	else if (pg_strcasecmp(prev_wd, "CURSOR") == 0)
	{
B
Bruce Momjian 已提交
2012 2013 2014 2015
		static const char *const list_DECLARECURSOR[] =
		{"WITH HOLD", "WITHOUT HOLD", "FOR", NULL};

		COMPLETE_WITH_LIST(list_DECLARECURSOR);
2016
	}
2017

2018 2019

/* DELETE */
B
Bruce Momjian 已提交
2020

2021 2022
	/*
	 * Complete DELETE with FROM (only if the word before that is not "ON"
2023
	 * (cf. rules) or "BEFORE" or "AFTER" (cf. triggers) or GRANT)
2024
	 */
2025 2026 2027 2028 2029
	else if (pg_strcasecmp(prev_wd, "DELETE") == 0 &&
			 !(pg_strcasecmp(prev2_wd, "ON") == 0 ||
			   pg_strcasecmp(prev2_wd, "GRANT") == 0 ||
			   pg_strcasecmp(prev2_wd, "BEFORE") == 0 ||
			   pg_strcasecmp(prev2_wd, "AFTER") == 0))
2030 2031
		COMPLETE_WITH_CONST("FROM");
	/* Complete DELETE FROM with a list of tables */
2032 2033
	else if (pg_strcasecmp(prev2_wd, "DELETE") == 0 &&
			 pg_strcasecmp(prev_wd, "FROM") == 0)
2034
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_deletables, NULL);
2035
	/* Complete DELETE FROM <table> */
2036 2037
	else if (pg_strcasecmp(prev3_wd, "DELETE") == 0 &&
			 pg_strcasecmp(prev2_wd, "FROM") == 0)
2038 2039 2040 2041 2042 2043 2044
	{
		static const char *const list_DELETE[] =
		{"USING", "WHERE", "SET", NULL};

		COMPLETE_WITH_LIST(list_DELETE);
	}
	/* XXX: implement tab completion for DELETE ... USING */
2045

2046 2047 2048 2049 2050 2051 2052 2053 2054
/* DISCARD */
	else if (pg_strcasecmp(prev_wd, "DISCARD") == 0)
	{
		static const char *const list_DISCARD[] =
		{"ALL", "PLANS", "TEMP", NULL};

		COMPLETE_WITH_LIST(list_DISCARD);
	}

2055
/* DO */
B
Bruce Momjian 已提交
2056

2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
	/*
	 * Complete DO with LANGUAGE.
	 */
	else if (pg_strcasecmp(prev_wd, "DO") == 0)
	{
		static const char *const list_DO[] =
		{"LANGUAGE", NULL};

		COMPLETE_WITH_LIST(list_DO);
	}

2068 2069 2070 2071 2072 2073 2074 2075
/* DROP (when not the previous word) */
	/* DROP AGGREGATE */
	else if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
			 pg_strcasecmp(prev2_wd, "AGGREGATE") == 0)
		COMPLETE_WITH_CONST("(");

	/* DROP object with CASCADE / RESTRICT */
	else if ((pg_strcasecmp(prev3_wd, "DROP") == 0 &&
P
Peter Eisentraut 已提交
2076 2077
			  (pg_strcasecmp(prev2_wd, "COLLATION") == 0 ||
			   pg_strcasecmp(prev2_wd, "CONVERSION") == 0 ||
2078
			   pg_strcasecmp(prev2_wd, "DOMAIN") == 0 ||
2079
			   pg_strcasecmp(prev2_wd, "EXTENSION") == 0 ||
2080 2081 2082 2083 2084
			   pg_strcasecmp(prev2_wd, "FUNCTION") == 0 ||
			   pg_strcasecmp(prev2_wd, "INDEX") == 0 ||
			   pg_strcasecmp(prev2_wd, "LANGUAGE") == 0 ||
			   pg_strcasecmp(prev2_wd, "SCHEMA") == 0 ||
			   pg_strcasecmp(prev2_wd, "SEQUENCE") == 0 ||
2085
			   pg_strcasecmp(prev2_wd, "SERVER") == 0 ||
2086 2087 2088 2089 2090
			   pg_strcasecmp(prev2_wd, "TABLE") == 0 ||
			   pg_strcasecmp(prev2_wd, "TYPE") == 0 ||
			   pg_strcasecmp(prev2_wd, "VIEW") == 0)) ||
			 (pg_strcasecmp(prev4_wd, "DROP") == 0 &&
			  pg_strcasecmp(prev3_wd, "AGGREGATE") == 0 &&
2091
			  prev_wd[strlen(prev_wd) - 1] == ')') ||
2092 2093 2094 2095
			 (pg_strcasecmp(prev5_wd, "DROP") == 0 &&
			  pg_strcasecmp(prev4_wd, "FOREIGN") == 0 &&
			  pg_strcasecmp(prev3_wd, "DATA") == 0 &&
			  pg_strcasecmp(prev2_wd, "WRAPPER") == 0) ||
B
Bruce Momjian 已提交
2096
			 (pg_strcasecmp(prev5_wd, "DROP") == 0 &&
2097 2098
			  pg_strcasecmp(prev4_wd, "TEXT") == 0 &&
			  pg_strcasecmp(prev3_wd, "SEARCH") == 0 &&
B
Bruce Momjian 已提交
2099 2100 2101 2102 2103
			  (pg_strcasecmp(prev2_wd, "CONFIGURATION") == 0 ||
			   pg_strcasecmp(prev2_wd, "DICTIONARY") == 0 ||
			   pg_strcasecmp(prev2_wd, "PARSER") == 0 ||
			   pg_strcasecmp(prev2_wd, "TEMPLATE") == 0))
		)
2104
	{
2105 2106
		if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
			pg_strcasecmp(prev2_wd, "FUNCTION") == 0)
2107
		{
2108
			COMPLETE_WITH_CONST("(");
2109 2110
		}
		else
2111 2112 2113
		{
			static const char *const list_DROPCR[] =
			{"CASCADE", "RESTRICT", NULL};
B
Bruce Momjian 已提交
2114

2115 2116
			COMPLETE_WITH_LIST(list_DROPCR);
		}
2117
	}
R
Robert Haas 已提交
2118 2119 2120 2121 2122 2123 2124 2125
	else if (pg_strcasecmp(prev2_wd, "DROP") == 0 &&
			 pg_strcasecmp(prev_wd, "FOREIGN") == 0)
	{
		static const char *const drop_CREATE_FOREIGN[] =
		{"DATA WRAPPER", "TABLE", NULL};

		COMPLETE_WITH_LIST(drop_CREATE_FOREIGN);
	}
2126
	else if (pg_strcasecmp(prev4_wd, "DROP") == 0 &&
2127 2128
			 (pg_strcasecmp(prev3_wd, "AGGREGATE") == 0 ||
			  pg_strcasecmp(prev3_wd, "FUNCTION") == 0) &&
B
Bruce Momjian 已提交
2129
			 pg_strcasecmp(prev_wd, "(") == 0)
2130
	{
2131
		char	   *tmp_buf = malloc(strlen(Query_for_list_of_arguments) + strlen(prev2_wd));
B
Bruce Momjian 已提交
2132

2133
		sprintf(tmp_buf, Query_for_list_of_arguments, prev2_wd);
2134 2135 2136
		COMPLETE_WITH_QUERY(tmp_buf);
		free(tmp_buf);
	}
2137 2138 2139 2140 2141 2142 2143 2144
	/* DROP OWNED BY */
	else if (pg_strcasecmp(prev2_wd, "DROP") == 0 &&
			 pg_strcasecmp(prev_wd, "OWNED") == 0)
		COMPLETE_WITH_CONST("BY");
	else if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
			 pg_strcasecmp(prev2_wd, "OWNED") == 0 &&
			 pg_strcasecmp(prev_wd, "BY") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2145 2146
	else if (pg_strcasecmp(prev3_wd, "DROP") == 0 &&
			 pg_strcasecmp(prev2_wd, "TEXT") == 0 &&
B
Bruce Momjian 已提交
2147
			 pg_strcasecmp(prev_wd, "SEARCH") == 0)
2148
	{
2149

2150 2151
		static const char *const list_ALTERTEXTSEARCH[] =
		{"CONFIGURATION", "DICTIONARY", "PARSER", "TEMPLATE", NULL};
2152

2153 2154
		COMPLETE_WITH_LIST(list_ALTERTEXTSEARCH);
	}
B
Bruce Momjian 已提交
2155

2156 2157 2158 2159 2160
/* EXECUTE, but not EXECUTE embedded in other commands */
	else if (pg_strcasecmp(prev_wd, "EXECUTE") == 0 &&
			 prev2_wd[0] == '\0')
		COMPLETE_WITH_QUERY(Query_for_list_of_prepared_statements);

2161
/* EXPLAIN */
B
Bruce Momjian 已提交
2162

2163
	/*
2164
	 * Complete EXPLAIN [ANALYZE] [VERBOSE] with list of EXPLAIN-able commands
2165
	 */
2166 2167
	else if (pg_strcasecmp(prev_wd, "EXPLAIN") == 0)
	{
B
Bruce Momjian 已提交
2168 2169 2170 2171
		static const char *const list_EXPLAIN[] =
		{"SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", "ANALYZE", "VERBOSE", NULL};

		COMPLETE_WITH_LIST(list_EXPLAIN);
2172 2173
	}
	else if (pg_strcasecmp(prev2_wd, "EXPLAIN") == 0 &&
B
Bruce Momjian 已提交
2174
			 pg_strcasecmp(prev_wd, "ANALYZE") == 0)
2175
	{
B
Bruce Momjian 已提交
2176 2177 2178 2179
		static const char *const list_EXPLAIN[] =
		{"SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", "VERBOSE", NULL};

		COMPLETE_WITH_LIST(list_EXPLAIN);
2180
	}
2181 2182 2183 2184 2185
	else if ((pg_strcasecmp(prev2_wd, "EXPLAIN") == 0 &&
			  pg_strcasecmp(prev_wd, "VERBOSE") == 0) ||
			 (pg_strcasecmp(prev3_wd, "EXPLAIN") == 0 &&
			  pg_strcasecmp(prev2_wd, "ANALYZE") == 0 &&
			  pg_strcasecmp(prev_wd, "VERBOSE") == 0))
2186
	{
B
Bruce Momjian 已提交
2187 2188 2189 2190
		static const char *const list_EXPLAIN[] =
		{"SELECT", "INSERT", "DELETE", "UPDATE", "DECLARE", NULL};

		COMPLETE_WITH_LIST(list_EXPLAIN);
2191
	}
2192 2193

/* FETCH && MOVE */
2194
	/* Complete FETCH with one of FORWARD, BACKWARD, RELATIVE */
2195 2196
	else if (pg_strcasecmp(prev_wd, "FETCH") == 0 ||
			 pg_strcasecmp(prev_wd, "MOVE") == 0)
2197
	{
B
Bruce Momjian 已提交
2198
		static const char *const list_FETCH1[] =
2199
		{"ABSOLUTE", "BACKWARD", "FORWARD", "RELATIVE", NULL};
2200 2201 2202 2203

		COMPLETE_WITH_LIST(list_FETCH1);
	}
	/* Complete FETCH <sth> with one of ALL, NEXT, PRIOR */
2204 2205
	else if (pg_strcasecmp(prev2_wd, "FETCH") == 0 ||
			 pg_strcasecmp(prev2_wd, "MOVE") == 0)
2206
	{
B
Bruce Momjian 已提交
2207
		static const char *const list_FETCH2[] =
2208
		{"ALL", "NEXT", "PRIOR", NULL};
2209 2210 2211 2212 2213

		COMPLETE_WITH_LIST(list_FETCH2);
	}

	/*
B
Bruce Momjian 已提交
2214 2215 2216
	 * Complete FETCH <sth1> <sth2> with "FROM" or "IN". These are equivalent,
	 * but we may as well tab-complete both: perhaps some users prefer one
	 * variant or the other.
2217
	 */
2218 2219
	else if (pg_strcasecmp(prev3_wd, "FETCH") == 0 ||
			 pg_strcasecmp(prev3_wd, "MOVE") == 0)
2220
	{
2221 2222
		static const char *const list_FROMIN[] =
		{"FROM", "IN", NULL};
2223

2224
		COMPLETE_WITH_LIST(list_FROMIN);
2225
	}
2226

2227 2228 2229 2230 2231 2232 2233 2234
/* FOREIGN DATA WRAPPER */
	/* applies in ALTER/DROP FDW and in CREATE SERVER */
	else if (pg_strcasecmp(prev4_wd, "CREATE") != 0 &&
			 pg_strcasecmp(prev3_wd, "FOREIGN") == 0 &&
			 pg_strcasecmp(prev2_wd, "DATA") == 0 &&
			 pg_strcasecmp(prev_wd, "WRAPPER") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_fdws);

R
Robert Haas 已提交
2235 2236 2237 2238
/* FOREIGN TABLE */
	else if (pg_strcasecmp(prev3_wd, "CREATE") != 0 &&
			 pg_strcasecmp(prev2_wd, "FOREIGN") == 0 &&
			 pg_strcasecmp(prev_wd, "TABLE") == 0)
2239
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables, NULL);
R
Robert Haas 已提交
2240

2241
/* GRANT && REVOKE */
2242
	/* Complete GRANT/REVOKE with a list of roles and privileges */
2243 2244
	else if (pg_strcasecmp(prev_wd, "GRANT") == 0 ||
			 pg_strcasecmp(prev_wd, "REVOKE") == 0)
2245
	{
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261
		COMPLETE_WITH_QUERY(Query_for_list_of_roles
							" UNION SELECT 'SELECT'"
							" UNION SELECT 'INSERT'"
							" UNION SELECT 'UPDATE'"
							" UNION SELECT 'DELETE'"
							" UNION SELECT 'TRUNCATE'"
							" UNION SELECT 'REFERENCES'"
							" UNION SELECT 'TRIGGER'"
							" UNION SELECT 'CREATE'"
							" UNION SELECT 'CONNECT'"
							" UNION SELECT 'TEMPORARY'"
							" UNION SELECT 'EXECUTE'"
							" UNION SELECT 'USAGE'"
							" UNION SELECT 'ALL'");
	}
	/* Complete GRANT/REVOKE <privilege> with "ON", GRANT/REVOKE <role> with TO/FROM */
2262 2263
	else if (pg_strcasecmp(prev2_wd, "GRANT") == 0 ||
			 pg_strcasecmp(prev2_wd, "REVOKE") == 0)
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
	{
		if (pg_strcasecmp(prev_wd, "SELECT") == 0
			|| pg_strcasecmp(prev_wd, "INSERT") == 0
			|| pg_strcasecmp(prev_wd, "UPDATE") == 0
			|| pg_strcasecmp(prev_wd, "DELETE") == 0
			|| pg_strcasecmp(prev_wd, "TRUNCATE") == 0
			|| pg_strcasecmp(prev_wd, "REFERENCES") == 0
			|| pg_strcasecmp(prev_wd, "TRIGGER") == 0
			|| pg_strcasecmp(prev_wd, "CREATE") == 0
			|| pg_strcasecmp(prev_wd, "CONNECT") == 0
			|| pg_strcasecmp(prev_wd, "TEMPORARY") == 0
			|| pg_strcasecmp(prev_wd, "TEMP") == 0
			|| pg_strcasecmp(prev_wd, "EXECUTE") == 0
			|| pg_strcasecmp(prev_wd, "USAGE") == 0
			|| pg_strcasecmp(prev_wd, "ALL") == 0)
			COMPLETE_WITH_CONST("ON");
		else
		{
			if (pg_strcasecmp(prev2_wd, "GRANT") == 0)
				COMPLETE_WITH_CONST("TO");
			else
				COMPLETE_WITH_CONST("FROM");
		}
	}
2288 2289

	/*
B
Bruce Momjian 已提交
2290 2291
	 * Complete GRANT/REVOKE <sth> ON with a list of tables, views, sequences,
	 * and indexes
2292
	 *
B
Bruce Momjian 已提交
2293 2294
	 * keywords DATABASE, FUNCTION, LANGUAGE, SCHEMA added to query result via
	 * UNION; seems to work intuitively
B
Bruce Momjian 已提交
2295
	 *
B
Bruce Momjian 已提交
2296 2297 2298
	 * Note: GRANT/REVOKE can get quite complex; tab-completion as implemented
	 * here will only work if the privilege list contains exactly one
	 * privilege
2299
	 */
2300 2301 2302
	else if ((pg_strcasecmp(prev3_wd, "GRANT") == 0 ||
			  pg_strcasecmp(prev3_wd, "REVOKE") == 0) &&
			 pg_strcasecmp(prev_wd, "ON") == 0)
R
Robert Haas 已提交
2303
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvf,
2304
								   " UNION SELECT 'DATABASE'"
2305
								   " UNION SELECT 'DOMAIN'"
2306 2307
								   " UNION SELECT 'FOREIGN DATA WRAPPER'"
								   " UNION SELECT 'FOREIGN SERVER'"
2308 2309
								   " UNION SELECT 'FUNCTION'"
								   " UNION SELECT 'LANGUAGE'"
2310
								   " UNION SELECT 'LARGE OBJECT'"
2311
								   " UNION SELECT 'SCHEMA'"
2312 2313
								   " UNION SELECT 'TABLESPACE'"
								   " UNION SELECT 'TYPE'");
2314 2315 2316 2317 2318 2319
	else if ((pg_strcasecmp(prev4_wd, "GRANT") == 0 ||
			  pg_strcasecmp(prev4_wd, "REVOKE") == 0) &&
			 pg_strcasecmp(prev2_wd, "ON") == 0 &&
			 pg_strcasecmp(prev_wd, "FOREIGN") == 0)
	{
		static const char *const list_privilege_foreign[] =
2320
		{"DATA WRAPPER", "SERVER", NULL};
2321 2322 2323

		COMPLETE_WITH_LIST(list_privilege_foreign);
	}
2324

2325
	/* Complete "GRANT/REVOKE * ON * " with "TO/FROM" */
2326 2327 2328
	else if ((pg_strcasecmp(prev4_wd, "GRANT") == 0 ||
			  pg_strcasecmp(prev4_wd, "REVOKE") == 0) &&
			 pg_strcasecmp(prev2_wd, "ON") == 0)
2329
	{
2330
		if (pg_strcasecmp(prev_wd, "DATABASE") == 0)
2331
			COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2332 2333
		else if (pg_strcasecmp(prev_wd, "DOMAIN") == 0)
			COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL);
2334
		else if (pg_strcasecmp(prev_wd, "FUNCTION") == 0)
2335
			COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
2336
		else if (pg_strcasecmp(prev_wd, "LANGUAGE") == 0)
2337
			COMPLETE_WITH_QUERY(Query_for_list_of_languages);
2338
		else if (pg_strcasecmp(prev_wd, "SCHEMA") == 0)
2339
			COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
2340 2341
		else if (pg_strcasecmp(prev_wd, "TABLESPACE") == 0)
			COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
2342 2343
		else if (pg_strcasecmp(prev_wd, "TYPE") == 0)
			COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL);
2344
		else if (pg_strcasecmp(prev4_wd, "GRANT") == 0)
2345
			COMPLETE_WITH_CONST("TO");
2346 2347
		else
			COMPLETE_WITH_CONST("FROM");
2348
	}
2349

2350
	/* Complete "GRANT/REVOKE * ON * TO/FROM" with username, GROUP, or PUBLIC */
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
	else if (pg_strcasecmp(prev5_wd, "GRANT") == 0 &&
			 pg_strcasecmp(prev3_wd, "ON") == 0)
	{
		if (pg_strcasecmp(prev_wd, "TO") == 0)
			COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
		else
			COMPLETE_WITH_CONST("TO");
	}
	else if (pg_strcasecmp(prev5_wd, "REVOKE") == 0 &&
			 pg_strcasecmp(prev3_wd, "ON") == 0)
	{
		if (pg_strcasecmp(prev_wd, "FROM") == 0)
			COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
		else
			COMPLETE_WITH_CONST("FROM");
	}
2367

2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
	/* Complete "GRANT/REVOKE * TO/FROM" with username, GROUP, or PUBLIC */
	else if (pg_strcasecmp(prev3_wd, "GRANT") == 0 &&
			 pg_strcasecmp(prev_wd, "TO") == 0)
	{
		COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
	}
	else if (pg_strcasecmp(prev3_wd, "REVOKE") == 0 &&
			 pg_strcasecmp(prev_wd, "FROM") == 0)
	{
		COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
	}

2380 2381 2382 2383
/* GROUP BY */
	else if (pg_strcasecmp(prev3_wd, "FROM") == 0 &&
			 pg_strcasecmp(prev_wd, "GROUP") == 0)
		COMPLETE_WITH_CONST("BY");
2384 2385

/* INSERT */
2386
	/* Complete INSERT with "INTO" */
2387
	else if (pg_strcasecmp(prev_wd, "INSERT") == 0)
2388 2389
		COMPLETE_WITH_CONST("INTO");
	/* Complete INSERT INTO with table names */
2390 2391
	else if (pg_strcasecmp(prev2_wd, "INSERT") == 0 &&
			 pg_strcasecmp(prev_wd, "INTO") == 0)
2392
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_insertables, NULL);
2393
	/* Complete "INSERT INTO <table> (" with attribute names */
2394 2395 2396 2397
	else if (pg_strcasecmp(prev4_wd, "INSERT") == 0 &&
			 pg_strcasecmp(prev3_wd, "INTO") == 0 &&
			 pg_strcasecmp(prev_wd, "(") == 0)
		COMPLETE_WITH_ATTR(prev2_wd, "");
2398 2399

	/*
B
Bruce Momjian 已提交
2400 2401
	 * Complete INSERT INTO <table> with "(" or "VALUES" or "SELECT" or
	 * "TABLE" or "DEFAULT VALUES"
2402
	 */
2403 2404
	else if (pg_strcasecmp(prev3_wd, "INSERT") == 0 &&
			 pg_strcasecmp(prev2_wd, "INTO") == 0)
2405
	{
B
Bruce Momjian 已提交
2406
		static const char *const list_INSERT[] =
2407
		{"(", "DEFAULT VALUES", "SELECT", "TABLE", "VALUES", NULL};
B
Bruce Momjian 已提交
2408

2409 2410
		COMPLETE_WITH_LIST(list_INSERT);
	}
2411 2412 2413 2414 2415

	/*
	 * Complete INSERT INTO <table> (attribs) with "VALUES" or "SELECT" or
	 * "TABLE"
	 */
2416 2417
	else if (pg_strcasecmp(prev4_wd, "INSERT") == 0 &&
			 pg_strcasecmp(prev3_wd, "INTO") == 0 &&
B
Bruce Momjian 已提交
2418
			 prev_wd[strlen(prev_wd) - 1] == ')')
2419
	{
B
Bruce Momjian 已提交
2420
		static const char *const list_INSERT[] =
P
Peter Eisentraut 已提交
2421
		{"SELECT", "TABLE", "VALUES", NULL};
B
Bruce Momjian 已提交
2422

2423 2424 2425
		COMPLETE_WITH_LIST(list_INSERT);
	}

2426
	/* Insert an open parenthesis after "VALUES" */
2427 2428
	else if (pg_strcasecmp(prev_wd, "VALUES") == 0 &&
			 pg_strcasecmp(prev2_wd, "DEFAULT") != 0)
2429
		COMPLETE_WITH_CONST("(");
2430 2431

/* LOCK */
2432
	/* Complete LOCK [TABLE] with a list of tables */
2433 2434 2435 2436 2437 2438
	else if (pg_strcasecmp(prev_wd, "LOCK") == 0)
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
								   " UNION SELECT 'TABLE'");
	else if (pg_strcasecmp(prev_wd, "TABLE") == 0 &&
			 pg_strcasecmp(prev2_wd, "LOCK") == 0)
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, "");
2439 2440 2441 2442

	/* For the following, handle the case of a single table only for now */

	/* Complete LOCK [TABLE] <table> with "IN" */
2443
	else if ((pg_strcasecmp(prev2_wd, "LOCK") == 0 &&
2444
			  pg_strcasecmp(prev_wd, "TABLE") != 0) ||
2445 2446
			 (pg_strcasecmp(prev2_wd, "TABLE") == 0 &&
			  pg_strcasecmp(prev3_wd, "LOCK") == 0))
B
Bruce Momjian 已提交
2447
		COMPLETE_WITH_CONST("IN");
2448 2449

	/* Complete LOCK [TABLE] <table> IN with a lock mode */
2450 2451 2452 2453
	else if (pg_strcasecmp(prev_wd, "IN") == 0 &&
			 (pg_strcasecmp(prev3_wd, "LOCK") == 0 ||
			  (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
			   pg_strcasecmp(prev4_wd, "LOCK") == 0)))
2454
	{
B
Bruce Momjian 已提交
2455
		static const char *const lock_modes[] =
2456
		{"ACCESS SHARE MODE",
B
Bruce Momjian 已提交
2457 2458 2459 2460
			"ROW SHARE MODE", "ROW EXCLUSIVE MODE",
			"SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE",
			"SHARE ROW EXCLUSIVE MODE",
		"EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE", NULL};
B
Bruce Momjian 已提交
2461 2462

		COMPLETE_WITH_LIST(lock_modes);
2463
	}
2464

2465
/* NOTIFY */
2466
	else if (pg_strcasecmp(prev_wd, "NOTIFY") == 0)
2467
		COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s'");
2468

2469 2470 2471 2472
/* OPTIONS */
	else if (pg_strcasecmp(prev_wd, "OPTIONS") == 0)
		COMPLETE_WITH_CONST("(");

2473
/* OWNER TO  - complete with available roles */
2474
	else if (pg_strcasecmp(prev2_wd, "OWNER") == 0 &&
B
Bruce Momjian 已提交
2475
			 pg_strcasecmp(prev_wd, "TO") == 0)
2476
		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2477 2478 2479 2480 2481 2482 2483 2484

/* ORDER BY */
	else if (pg_strcasecmp(prev3_wd, "FROM") == 0 &&
			 pg_strcasecmp(prev_wd, "ORDER") == 0)
		COMPLETE_WITH_CONST("BY");
	else if (pg_strcasecmp(prev4_wd, "FROM") == 0 &&
			 pg_strcasecmp(prev2_wd, "ORDER") == 0 &&
			 pg_strcasecmp(prev_wd, "BY") == 0)
2485
		COMPLETE_WITH_ATTR(prev3_wd, "");
2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496

/* PREPARE xx AS */
	else if (pg_strcasecmp(prev_wd, "AS") == 0 &&
			 pg_strcasecmp(prev3_wd, "PREPARE") == 0)
	{
		static const char *const list_PREPARE[] =
		{"SELECT", "UPDATE", "INSERT", "DELETE", NULL};

		COMPLETE_WITH_LIST(list_PREPARE);
	}

2497 2498 2499 2500 2501
/*
 * PREPARE TRANSACTION is missing on purpose. It's intended for transaction
 * managers, not for manual use in interactive sessions.
 */

2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520
/* REASSIGN OWNED BY xxx TO yyy */
	else if (pg_strcasecmp(prev_wd, "REASSIGN") == 0)
		COMPLETE_WITH_CONST("OWNED");
	else if (pg_strcasecmp(prev_wd, "OWNED") == 0 &&
			 pg_strcasecmp(prev2_wd, "REASSIGN") == 0)
		COMPLETE_WITH_CONST("BY");
	else if (pg_strcasecmp(prev_wd, "BY") == 0 &&
			 pg_strcasecmp(prev2_wd, "OWNED") == 0 &&
			 pg_strcasecmp(prev3_wd, "REASSIGN") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
	else if (pg_strcasecmp(prev2_wd, "BY") == 0 &&
			 pg_strcasecmp(prev3_wd, "OWNED") == 0 &&
			 pg_strcasecmp(prev4_wd, "REASSIGN") == 0)
		COMPLETE_WITH_CONST("TO");
	else if (pg_strcasecmp(prev_wd, "TO") == 0 &&
			 pg_strcasecmp(prev3_wd, "BY") == 0 &&
			 pg_strcasecmp(prev4_wd, "OWNED") == 0 &&
			 pg_strcasecmp(prev5_wd, "REASSIGN") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2521

2522
/* REINDEX */
2523
	else if (pg_strcasecmp(prev_wd, "REINDEX") == 0)
2524
	{
B
Bruce Momjian 已提交
2525
		static const char *const list_REINDEX[] =
2526
		{"TABLE", "INDEX", "SYSTEM", "DATABASE", NULL};
B
Bruce Momjian 已提交
2527

2528 2529
		COMPLETE_WITH_LIST(list_REINDEX);
	}
2530
	else if (pg_strcasecmp(prev2_wd, "REINDEX") == 0)
2531
	{
2532
		if (pg_strcasecmp(prev_wd, "TABLE") == 0)
2533
			COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
2534
		else if (pg_strcasecmp(prev_wd, "INDEX") == 0)
2535
			COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL);
2536 2537 2538
		else if (pg_strcasecmp(prev_wd, "SYSTEM") == 0 ||
				 pg_strcasecmp(prev_wd, "DATABASE") == 0)
			COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2539 2540
	}

R
Robert Haas 已提交
2541 2542 2543 2544 2545 2546 2547
/* SECURITY LABEL */
	else if (pg_strcasecmp(prev_wd, "SECURITY") == 0)
		COMPLETE_WITH_CONST("LABEL");
	else if (pg_strcasecmp(prev2_wd, "SECURITY") == 0 &&
			 pg_strcasecmp(prev_wd, "LABEL") == 0)
	{
		static const char *const list_SECURITY_LABEL_preposition[] =
2548 2549
		{"ON", "FOR"};

R
Robert Haas 已提交
2550 2551 2552 2553 2554 2555 2556 2557 2558
		COMPLETE_WITH_LIST(list_SECURITY_LABEL_preposition);
	}
	else if (pg_strcasecmp(prev4_wd, "SECURITY") == 0 &&
			 pg_strcasecmp(prev3_wd, "LABEL") == 0 &&
			 pg_strcasecmp(prev2_wd, "FOR") == 0)
		COMPLETE_WITH_CONST("ON");
	else if ((pg_strcasecmp(prev3_wd, "SECURITY") == 0 &&
			  pg_strcasecmp(prev2_wd, "LABEL") == 0 &&
			  pg_strcasecmp(prev_wd, "ON") == 0) ||
2559
			 (pg_strcasecmp(prev5_wd, "SECURITY") == 0 &&
R
Robert Haas 已提交
2560 2561 2562 2563 2564 2565
			  pg_strcasecmp(prev4_wd, "LABEL") == 0 &&
			  pg_strcasecmp(prev3_wd, "FOR") == 0 &&
			  pg_strcasecmp(prev_wd, "ON") == 0))
	{
		static const char *const list_SECURITY_LABEL[] =
		{"LANGUAGE", "SCHEMA", "SEQUENCE", "TABLE", "TYPE", "VIEW", "COLUMN",
2566
			"AGGREGATE", "FUNCTION", "DOMAIN", "LARGE OBJECT",
R
Robert Haas 已提交
2567 2568 2569 2570 2571 2572 2573 2574 2575
		NULL};

		COMPLETE_WITH_LIST(list_SECURITY_LABEL);
	}
	else if (pg_strcasecmp(prev5_wd, "SECURITY") == 0 &&
			 pg_strcasecmp(prev4_wd, "LABEL") == 0 &&
			 pg_strcasecmp(prev3_wd, "ON") == 0)
		COMPLETE_WITH_CONST("IS");

2576
/* SELECT */
2577
	/* naah . . . */
2578 2579

/* SET, RESET, SHOW */
2580
	/* Complete with a variable name */
2581 2582
	else if ((pg_strcasecmp(prev_wd, "SET") == 0 &&
			  pg_strcasecmp(prev3_wd, "UPDATE") != 0) ||
2583 2584 2585 2586
			 pg_strcasecmp(prev_wd, "RESET") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_set_vars);
	else if (pg_strcasecmp(prev_wd, "SHOW") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_show_vars);
2587
	/* Complete "SET TRANSACTION" */
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599
	else if ((pg_strcasecmp(prev2_wd, "SET") == 0 &&
			  pg_strcasecmp(prev_wd, "TRANSACTION") == 0)
			 || (pg_strcasecmp(prev2_wd, "START") == 0
				 && pg_strcasecmp(prev_wd, "TRANSACTION") == 0)
			 || (pg_strcasecmp(prev2_wd, "BEGIN") == 0
				 && pg_strcasecmp(prev_wd, "WORK") == 0)
			 || (pg_strcasecmp(prev2_wd, "BEGIN") == 0
				 && pg_strcasecmp(prev_wd, "TRANSACTION") == 0)
			 || (pg_strcasecmp(prev4_wd, "SESSION") == 0
				 && pg_strcasecmp(prev3_wd, "CHARACTERISTICS") == 0
				 && pg_strcasecmp(prev2_wd, "AS") == 0
				 && pg_strcasecmp(prev_wd, "TRANSACTION") == 0))
2600
	{
B
Bruce Momjian 已提交
2601
		static const char *const my_list[] =
2602
		{"ISOLATION LEVEL", "READ", NULL};
2603 2604 2605

		COMPLETE_WITH_LIST(my_list);
	}
2606
	else if ((pg_strcasecmp(prev3_wd, "SET") == 0
2607
			  || pg_strcasecmp(prev3_wd, "BEGIN") == 0
2608 2609 2610
			  || pg_strcasecmp(prev3_wd, "START") == 0
			  || (pg_strcasecmp(prev4_wd, "CHARACTERISTICS") == 0
				  && pg_strcasecmp(prev3_wd, "AS") == 0))
2611
			 && (pg_strcasecmp(prev2_wd, "TRANSACTION") == 0
B
Bruce Momjian 已提交
2612
				 || pg_strcasecmp(prev2_wd, "WORK") == 0)
2613
			 && pg_strcasecmp(prev_wd, "ISOLATION") == 0)
2614
		COMPLETE_WITH_CONST("LEVEL");
2615
	else if ((pg_strcasecmp(prev4_wd, "SET") == 0
2616
			  || pg_strcasecmp(prev4_wd, "BEGIN") == 0
2617 2618
			  || pg_strcasecmp(prev4_wd, "START") == 0
			  || pg_strcasecmp(prev4_wd, "AS") == 0)
2619
			 && (pg_strcasecmp(prev3_wd, "TRANSACTION") == 0
B
Bruce Momjian 已提交
2620
				 || pg_strcasecmp(prev3_wd, "WORK") == 0)
2621 2622
			 && pg_strcasecmp(prev2_wd, "ISOLATION") == 0
			 && pg_strcasecmp(prev_wd, "LEVEL") == 0)
2623
	{
B
Bruce Momjian 已提交
2624
		static const char *const my_list[] =
2625
		{"READ", "REPEATABLE", "SERIALIZABLE", NULL};
2626 2627 2628

		COMPLETE_WITH_LIST(my_list);
	}
2629
	else if ((pg_strcasecmp(prev4_wd, "TRANSACTION") == 0 ||
B
Bruce Momjian 已提交
2630
			  pg_strcasecmp(prev4_wd, "WORK") == 0) &&
2631 2632 2633
			 pg_strcasecmp(prev3_wd, "ISOLATION") == 0 &&
			 pg_strcasecmp(prev2_wd, "LEVEL") == 0 &&
			 pg_strcasecmp(prev_wd, "READ") == 0)
2634
	{
B
Bruce Momjian 已提交
2635
		static const char *const my_list[] =
2636 2637 2638 2639
		{"UNCOMMITTED", "COMMITTED", NULL};

		COMPLETE_WITH_LIST(my_list);
	}
B
Bruce Momjian 已提交
2640 2641
	else if ((pg_strcasecmp(prev4_wd, "TRANSACTION") == 0 ||
			  pg_strcasecmp(prev4_wd, "WORK") == 0) &&
2642 2643 2644
			 pg_strcasecmp(prev3_wd, "ISOLATION") == 0 &&
			 pg_strcasecmp(prev2_wd, "LEVEL") == 0 &&
			 pg_strcasecmp(prev_wd, "REPEATABLE") == 0)
2645
		COMPLETE_WITH_CONST("READ");
2646
	else if ((pg_strcasecmp(prev3_wd, "SET") == 0 ||
B
Bruce Momjian 已提交
2647 2648 2649
			  pg_strcasecmp(prev3_wd, "BEGIN") == 0 ||
			  pg_strcasecmp(prev3_wd, "START") == 0 ||
			  pg_strcasecmp(prev3_wd, "AS") == 0) &&
2650
			 (pg_strcasecmp(prev2_wd, "TRANSACTION") == 0 ||
B
Bruce Momjian 已提交
2651
			  pg_strcasecmp(prev2_wd, "WORK") == 0) &&
2652
			 pg_strcasecmp(prev_wd, "READ") == 0)
2653
	{
B
Bruce Momjian 已提交
2654
		static const char *const my_list[] =
2655
		{"ONLY", "WRITE", NULL};
2656 2657 2658

		COMPLETE_WITH_LIST(my_list);
	}
2659
	/* Complete SET CONSTRAINTS <foo> with DEFERRED|IMMEDIATE */
2660 2661
	else if (pg_strcasecmp(prev3_wd, "SET") == 0 &&
			 pg_strcasecmp(prev2_wd, "CONSTRAINTS") == 0)
2662
	{
B
Bruce Momjian 已提交
2663
		static const char *const constraint_list[] =
2664
		{"DEFERRED", "IMMEDIATE", NULL};
B
Bruce Momjian 已提交
2665

2666 2667
		COMPLETE_WITH_LIST(constraint_list);
	}
2668
	/* Complete SET ROLE */
B
Bruce Momjian 已提交
2669
	else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
2670 2671
			 pg_strcasecmp(prev_wd, "ROLE") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2672
	/* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */
2673 2674
	else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
			 pg_strcasecmp(prev_wd, "SESSION") == 0)
2675
	{
B
Bruce Momjian 已提交
2676
		static const char *const my_list[] =
2677
		{"AUTHORIZATION", "CHARACTERISTICS AS TRANSACTION", NULL};
2678 2679 2680 2681

		COMPLETE_WITH_LIST(my_list);
	}
	/* Complete SET SESSION AUTHORIZATION with username */
2682 2683 2684
	else if (pg_strcasecmp(prev3_wd, "SET") == 0
			 && pg_strcasecmp(prev2_wd, "SESSION") == 0
			 && pg_strcasecmp(prev_wd, "AUTHORIZATION") == 0)
2685
		COMPLETE_WITH_QUERY(Query_for_list_of_roles " UNION SELECT 'DEFAULT'");
2686 2687 2688 2689
	/* Complete RESET SESSION with AUTHORIZATION */
	else if (pg_strcasecmp(prev2_wd, "RESET") == 0 &&
			 pg_strcasecmp(prev_wd, "SESSION") == 0)
		COMPLETE_WITH_CONST("AUTHORIZATION");
2690
	/* Complete SET <var> with "TO" */
2691
	else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
2692 2693
			 pg_strcasecmp(prev4_wd, "UPDATE") != 0 &&
			 pg_strcasecmp(prev_wd, "TABLESPACE") != 0 &&
2694
			 pg_strcasecmp(prev_wd, "SCHEMA") != 0 &&
2695
			 prev_wd[strlen(prev_wd) - 1] != ')' &&
2696
			 pg_strcasecmp(prev4_wd, "DOMAIN") != 0)
2697 2698
		COMPLETE_WITH_CONST("TO");
	/* Suggest possible variable values */
2699
	else if (pg_strcasecmp(prev3_wd, "SET") == 0 &&
B
Bruce Momjian 已提交
2700
			 (pg_strcasecmp(prev_wd, "TO") == 0 || strcmp(prev_wd, "=") == 0))
2701
	{
2702
		if (pg_strcasecmp(prev2_wd, "DateStyle") == 0)
2703
		{
B
Bruce Momjian 已提交
2704
			static const char *const my_list[] =
2705
			{"ISO", "SQL", "Postgres", "German",
B
Bruce Momjian 已提交
2706 2707
				"YMD", "DMY", "MDY",
				"US", "European", "NonEuropean",
B
Bruce Momjian 已提交
2708
			"DEFAULT", NULL};
2709 2710 2711

			COMPLETE_WITH_LIST(my_list);
		}
2712 2713 2714
		else if (pg_strcasecmp(prev2_wd, "IntervalStyle") == 0)
		{
			static const char *const my_list[] =
2715
			{"postgres", "postgres_verbose", "sql_standard", "iso_8601", NULL};
2716 2717 2718

			COMPLETE_WITH_LIST(my_list);
		}
2719
		else if (pg_strcasecmp(prev2_wd, "GEQO") == 0)
2720
		{
B
Bruce Momjian 已提交
2721
			static const char *const my_list[] =
2722
			{"ON", "OFF", "DEFAULT", NULL};
2723 2724 2725 2726 2727

			COMPLETE_WITH_LIST(my_list);
		}
		else
		{
B
Bruce Momjian 已提交
2728
			static const char *const my_list[] =
2729
			{"DEFAULT", NULL};
2730 2731 2732 2733

			COMPLETE_WITH_LIST(my_list);
		}
	}
2734

2735
/* START TRANSACTION */
2736
	else if (pg_strcasecmp(prev_wd, "START") == 0)
2737 2738
		COMPLETE_WITH_CONST("TRANSACTION");

2739 2740 2741 2742 2743
/* TABLE, but not TABLE embedded in other commands */
	else if (pg_strcasecmp(prev_wd, "TABLE") == 0 &&
			 prev2_wd[0] == '\0')
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL);

2744
/* TRUNCATE */
2745
	else if (pg_strcasecmp(prev_wd, "TRUNCATE") == 0)
2746
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
2747 2748

/* UNLISTEN */
2749
	else if (pg_strcasecmp(prev_wd, "UNLISTEN") == 0)
2750
		COMPLETE_WITH_QUERY("SELECT pg_catalog.quote_ident(channel) FROM pg_catalog.pg_listening_channels() AS channel WHERE substring(pg_catalog.quote_ident(channel),1,%d)='%s' UNION SELECT '*'");
2751

2752
/* UPDATE */
2753
	/* If prev. word is UPDATE suggest a list of tables */
2754
	else if (pg_strcasecmp(prev_wd, "UPDATE") == 0)
2755
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL);
2756
	/* Complete UPDATE <table> with "SET" */
2757
	else if (pg_strcasecmp(prev2_wd, "UPDATE") == 0)
2758 2759 2760
		COMPLETE_WITH_CONST("SET");

	/*
B
Bruce Momjian 已提交
2761 2762 2763
	 * If the previous word is SET (and it wasn't caught above as the _first_
	 * word) the word before it was (hopefully) a table name and we'll now
	 * make a list of attributes.
2764
	 */
2765
	else if (pg_strcasecmp(prev_wd, "SET") == 0)
2766
		COMPLETE_WITH_ATTR(prev2_wd, "");
2767

2768 2769
/* UPDATE xx SET yy = */
	else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
B
Bruce Momjian 已提交
2770
			 pg_strcasecmp(prev4_wd, "UPDATE") == 0)
2771 2772
		COMPLETE_WITH_CONST("=");

2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
/* USER MAPPING */
	else if ((pg_strcasecmp(prev3_wd, "ALTER") == 0 ||
			  pg_strcasecmp(prev3_wd, "CREATE") == 0 ||
			  pg_strcasecmp(prev3_wd, "DROP") == 0) &&
			 pg_strcasecmp(prev2_wd, "USER") == 0 &&
			 pg_strcasecmp(prev_wd, "MAPPING") == 0)
		COMPLETE_WITH_CONST("FOR");
	else if (pg_strcasecmp(prev4_wd, "CREATE") == 0 &&
			 pg_strcasecmp(prev3_wd, "USER") == 0 &&
			 pg_strcasecmp(prev2_wd, "MAPPING") == 0 &&
			 pg_strcasecmp(prev_wd, "FOR") == 0)
2784 2785 2786 2787
		COMPLETE_WITH_QUERY(Query_for_list_of_roles
							" UNION SELECT 'CURRENT_USER'"
							" UNION SELECT 'PUBLIC'"
							" UNION SELECT 'USER'");
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
	else if ((pg_strcasecmp(prev4_wd, "ALTER") == 0 ||
			  pg_strcasecmp(prev4_wd, "DROP") == 0) &&
			 pg_strcasecmp(prev3_wd, "USER") == 0 &&
			 pg_strcasecmp(prev2_wd, "MAPPING") == 0 &&
			 pg_strcasecmp(prev_wd, "FOR") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
	else if ((pg_strcasecmp(prev5_wd, "CREATE") == 0 ||
			  pg_strcasecmp(prev5_wd, "ALTER") == 0 ||
			  pg_strcasecmp(prev5_wd, "DROP") == 0) &&
			 pg_strcasecmp(prev4_wd, "USER") == 0 &&
			 pg_strcasecmp(prev3_wd, "MAPPING") == 0 &&
			 pg_strcasecmp(prev2_wd, "FOR") == 0)
		COMPLETE_WITH_CONST("SERVER");

2802 2803 2804 2805
/*
 * VACUUM [ FULL | FREEZE ] [ VERBOSE ] [ table ]
 * VACUUM [ FULL | FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]
 */
2806
	else if (pg_strcasecmp(prev_wd, "VACUUM") == 0)
2807
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
2808
								   " UNION SELECT 'FULL'"
2809
								   " UNION SELECT 'FREEZE'"
2810 2811
								   " UNION SELECT 'ANALYZE'"
								   " UNION SELECT 'VERBOSE'");
2812 2813
	else if (pg_strcasecmp(prev2_wd, "VACUUM") == 0 &&
			 (pg_strcasecmp(prev_wd, "FULL") == 0 ||
2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
			  pg_strcasecmp(prev_wd, "FREEZE") == 0))
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
								   " UNION SELECT 'ANALYZE'"
								   " UNION SELECT 'VERBOSE'");
	else if (pg_strcasecmp(prev3_wd, "VACUUM") == 0 &&
			 pg_strcasecmp(prev_wd, "ANALYZE") == 0 &&
			 (pg_strcasecmp(prev2_wd, "FULL") == 0 ||
			  pg_strcasecmp(prev2_wd, "FREEZE") == 0))
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
								   " UNION SELECT 'VERBOSE'");
	else if (pg_strcasecmp(prev3_wd, "VACUUM") == 0 &&
			 pg_strcasecmp(prev_wd, "VERBOSE") == 0 &&
			 (pg_strcasecmp(prev2_wd, "FULL") == 0 ||
			  pg_strcasecmp(prev2_wd, "FREEZE") == 0))
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
								   " UNION SELECT 'ANALYZE'");
	else if (pg_strcasecmp(prev2_wd, "VACUUM") == 0 &&
			 pg_strcasecmp(prev_wd, "VERBOSE") == 0)
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
								   " UNION SELECT 'ANALYZE'");
	else if (pg_strcasecmp(prev2_wd, "VACUUM") == 0 &&
			 pg_strcasecmp(prev_wd, "ANALYZE") == 0)
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
								   " UNION SELECT 'VERBOSE'");
	else if ((pg_strcasecmp(prev_wd, "ANALYZE") == 0 &&
			  pg_strcasecmp(prev2_wd, "VERBOSE") == 0) ||
			 (pg_strcasecmp(prev_wd, "VERBOSE") == 0 &&
			  pg_strcasecmp(prev2_wd, "ANALYZE") == 0))
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);

2844
/* WITH [RECURSIVE] */
2845 2846 2847 2848
	/* Only match when WITH is the first word, as WITH may appear in many other
	   contexts. */
	else if (pg_strcasecmp(prev_wd, "WITH") == 0 &&
			 prev2_wd[0] == '\0')
2849 2850
		COMPLETE_WITH_CONST("RECURSIVE");

2851
/* ANALYZE */
2852 2853
	/* If the previous word is ANALYZE, produce list of tables */
	else if (pg_strcasecmp(prev_wd, "ANALYZE") == 0)
2854
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tf, NULL);
2855

2856 2857
/* WHERE */
	/* Simple case of the word before the where being the table name */
2858
	else if (pg_strcasecmp(prev_wd, "WHERE") == 0)
2859
		COMPLETE_WITH_ATTR(prev2_wd, "");
2860

2861
/* ... FROM ... */
2862
/* TODO: also include SRF ? */
2863 2864 2865
	else if (pg_strcasecmp(prev_wd, "FROM") == 0 &&
			 pg_strcasecmp(prev3_wd, "COPY") != 0 &&
			 pg_strcasecmp(prev3_wd, "\\copy") != 0)
R
Robert Haas 已提交
2866
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvf, NULL);
2867

2868 2869 2870 2871
/* ... JOIN ... */
	else if (pg_strcasecmp(prev_wd, "JOIN") == 0)
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvf, NULL);

2872
/* Backslash commands */
2873
/* TODO:  \dc \dd \dl */
2874 2875
	else if (strcmp(prev_wd, "\\connect") == 0 || strcmp(prev_wd, "\\c") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2876 2877

	else if (strncmp(prev_wd, "\\da", strlen("\\da")) == 0)
2878
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates, NULL);
2879
	else if (strncmp(prev_wd, "\\db", strlen("\\db")) == 0)
2880
		COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
2881
	else if (strncmp(prev_wd, "\\dD", strlen("\\dD")) == 0)
2882
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL);
2883
	else if (strncmp(prev_wd, "\\des", strlen("\\des")) == 0)
2884
		COMPLETE_WITH_QUERY(Query_for_list_of_servers);
2885
	else if (strncmp(prev_wd, "\\deu", strlen("\\deu")) == 0)
2886
		COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
2887
	else if (strncmp(prev_wd, "\\dew", strlen("\\dew")) == 0)
2888
		COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
2889 2890

	else if (strncmp(prev_wd, "\\df", strlen("\\df")) == 0)
2891
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
2892
	else if (strncmp(prev_wd, "\\dFd", strlen("\\dFd")) == 0)
2893
		COMPLETE_WITH_QUERY(Query_for_list_of_ts_dictionaries);
2894
	else if (strncmp(prev_wd, "\\dFp", strlen("\\dFp")) == 0)
2895
		COMPLETE_WITH_QUERY(Query_for_list_of_ts_parsers);
2896
	else if (strncmp(prev_wd, "\\dFt", strlen("\\dFt")) == 0)
2897
		COMPLETE_WITH_QUERY(Query_for_list_of_ts_templates);
2898 2899 2900 2901 2902
	/* must be at end of \dF */
	else if (strncmp(prev_wd, "\\dF", strlen("\\dF")) == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_ts_configurations);

	else if (strncmp(prev_wd, "\\di", strlen("\\di")) == 0)
2903
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL);
2904 2905
	else if (strncmp(prev_wd, "\\dL", strlen("\\dL")) == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_languages);
2906
	else if (strncmp(prev_wd, "\\dn", strlen("\\dn")) == 0)
2907
		COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
2908 2909
	else if (strncmp(prev_wd, "\\dp", strlen("\\dp")) == 0
			 || strncmp(prev_wd, "\\z", strlen("\\z")) == 0)
R
Robert Haas 已提交
2910
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvf, NULL);
2911
	else if (strncmp(prev_wd, "\\ds", strlen("\\ds")) == 0)
2912
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL);
2913
	else if (strncmp(prev_wd, "\\dt", strlen("\\dt")) == 0)
2914
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
2915
	else if (strncmp(prev_wd, "\\dT", strlen("\\dT")) == 0)
2916
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL);
2917 2918
	else if (strncmp(prev_wd, "\\du", strlen("\\du")) == 0
			 || (strncmp(prev_wd, "\\dg", strlen("\\dg")) == 0))
2919
		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2920
	else if (strncmp(prev_wd, "\\dv", strlen("\\dv")) == 0)
2921
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL);
2922 2923 2924

	/* must be at end of \d list */
	else if (strncmp(prev_wd, "\\d", strlen("\\d")) == 0)
2925
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_relations, NULL);
2926

B
Bruce Momjian 已提交
2927 2928 2929
	else if (strcmp(prev_wd, "\\ef") == 0)
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);

2930 2931
	else if (strcmp(prev_wd, "\\encoding") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_encodings);
2932 2933
	else if (strcmp(prev_wd, "\\h") == 0 || strcmp(prev_wd, "\\help") == 0)
		COMPLETE_WITH_LIST(sql_commands);
2934 2935
	else if (strcmp(prev_wd, "\\password") == 0)
		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
2936 2937
	else if (strcmp(prev_wd, "\\pset") == 0)
	{
B
Bruce Momjian 已提交
2938
		static const char *const my_list[] =
2939
		{"format", "border", "expanded",
B
Bruce Momjian 已提交
2940 2941
			"null", "fieldsep", "tuples_only", "title", "tableattr",
		"linestyle", "pager", "recordsep", NULL};
2942

2943
		COMPLETE_WITH_LIST_CS(my_list);
2944
	}
2945 2946 2947 2948 2949
	else if (strcmp(prev2_wd, "\\pset") == 0)
	{
		if (strcmp(prev_wd, "format") == 0)
		{
			static const char *const my_list[] =
2950 2951
			{"unaligned", "aligned", "wrapped", "html", "latex",
			"troff-ms", NULL};
2952

2953
			COMPLETE_WITH_LIST_CS(my_list);
2954 2955 2956 2957
		}
		else if (strcmp(prev_wd, "linestyle") == 0)
		{
			static const char *const my_list[] =
2958
			{"ascii", "old-ascii", "unicode", NULL};
2959

2960
			COMPLETE_WITH_LIST_CS(my_list);
2961 2962
		}
	}
2963 2964 2965 2966
	else if (strcmp(prev_wd, "\\set") == 0)
	{
		matches = complete_from_variables(text, "", "");
	}
2967 2968
	else if (strcmp(prev_wd, "\\sf") == 0 || strcmp(prev_wd, "\\sf+") == 0)
		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
P
Peter Eisentraut 已提交
2969
	else if (strcmp(prev_wd, "\\cd") == 0 ||
B
Bruce Momjian 已提交
2970
			 strcmp(prev_wd, "\\e") == 0 || strcmp(prev_wd, "\\edit") == 0 ||
2971
			 strcmp(prev_wd, "\\g") == 0 ||
B
Bruce Momjian 已提交
2972
		  strcmp(prev_wd, "\\i") == 0 || strcmp(prev_wd, "\\include") == 0 ||
R
Robert Haas 已提交
2973
		  strcmp(prev_wd, "\\ir") == 0 || strcmp(prev_wd, "\\include_relative") == 0 ||
B
Bruce Momjian 已提交
2974
			 strcmp(prev_wd, "\\o") == 0 || strcmp(prev_wd, "\\out") == 0 ||
2975
			 strcmp(prev_wd, "\\s") == 0 ||
B
Bruce Momjian 已提交
2976
			 strcmp(prev_wd, "\\w") == 0 || strcmp(prev_wd, "\\write") == 0
2977
		)
2978 2979 2980 2981
	{
		completion_charp = "\\";
		matches = completion_matches(text, complete_from_files);
	}
2982

2983 2984 2985 2986 2987
	/*
	 * Finally, we look through the list of "things", such as TABLE, INDEX and
	 * check if that was the previous word. If so, execute the query to get a
	 * list of them.
	 */
2988 2989 2990 2991 2992
	else
	{
		int			i;

		for (i = 0; words_after_create[i].name; i++)
2993
		{
2994
			if (pg_strcasecmp(prev_wd, words_after_create[i].name) == 0)
2995
			{
2996
				if (words_after_create[i].query)
2997
					COMPLETE_WITH_QUERY(words_after_create[i].query);
2998 2999 3000
				else if (words_after_create[i].squery)
					COMPLETE_WITH_SCHEMA_QUERY(*words_after_create[i].squery,
											   NULL);
3001 3002
				break;
			}
3003
		}
3004 3005
	}

3006 3007 3008 3009 3010 3011 3012 3013
	/*
	 * If we still don't have anything to match we have to fabricate some sort
	 * of default list. If we were to just return NULL, readline automatically
	 * attempts filename completion, and that's usually no good.
	 */
	if (matches == NULL)
	{
		COMPLETE_WITH_CONST("");
3014
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
3015
		rl_completion_append_character = '\0';
3016
#endif
3017 3018 3019
	}

	/* free storage */
3020 3021 3022 3023 3024 3025
	{
		int			i;

		for (i = 0; i < lengthof(previous_words); i++)
			free(previous_words[i]);
	}
3026 3027 3028 3029

	/* Return our Grand List O' Matches */
	return matches;
}
3030 3031


3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044
/*
 * GENERATOR FUNCTIONS
 *
 * These functions do all the actual work of completing the input. They get
 * passed the text so far and the count how many times they have been called
 * so far with the same text.
 * If you read the above carefully, you'll see that these don't get called
 * directly but through the readline interface.
 * The return value is expected to be the full completion of the text, going
 * through a list each time, or NULL if there are no more matches. The string
 * will be free()'d by readline, so you must run it through strdup() or
 * something of that sort.
 */
3045

3046
/*
3047 3048
 * Common routine for create_command_generator and drop_command_generator.
 * Entries that have 'excluded' flags are not returned.
3049
 */
3050
static char *
3051
create_or_drop_command_generator(const char *text, int state, bits32 excluded)
3052
{
B
Bruce Momjian 已提交
3053 3054
	static int	list_index,
				string_length;
3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
	const char *name;

	/* If this is the first time for this completion, init some values */
	if (state == 0)
	{
		list_index = 0;
		string_length = strlen(text);
	}

	/* find something that matches */
	while ((name = words_after_create[list_index++].name))
3066
	{
3067
		if ((pg_strncasecmp(name, text, string_length) == 0) &&
3068
			!(words_after_create[list_index - 1].flags & excluded))
3069
			return pg_strdup_same_case(name, text);
3070
	}
3071 3072 3073
	/* if nothing matches, return NULL */
	return NULL;
}
3074

3075 3076 3077 3078 3079 3080 3081 3082 3083 3084
/*
 * This one gives you one from a list of things you can put after CREATE
 * as defined above.
 */
static char *
create_command_generator(const char *text, int state)
{
	return create_or_drop_command_generator(text, state, THING_NO_CREATE);
}

3085 3086 3087 3088 3089 3090
/*
 * This function gives you a list of things you can put after a DROP command.
 */
static char *
drop_command_generator(const char *text, int state)
{
3091
	return create_or_drop_command_generator(text, state, THING_NO_DROP);
3092
}
3093

3094 3095
/* The following two functions are wrappers for _complete_from_query */

3096 3097 3098 3099 3100
static char *
complete_from_query(const char *text, int state)
{
	return _complete_from_query(0, text, state);
}
3101

3102 3103 3104 3105 3106
static char *
complete_from_schema_query(const char *text, int state)
{
	return _complete_from_query(1, text, state);
}
3107 3108


3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129
/*
 * This creates a list of matching things, according to a query pointed to
 * by completion_charp.
 * The query can be one of two kinds:
 *
 * 1. A simple query which must contain a %d and a %s, which will be replaced
 * by the string length of the text and the text itself. The query may also
 * have up to four more %s in it; the first two such will be replaced by the
 * value of completion_info_charp, the next two by the value of
 * completion_info_charp2.
 *
 * 2. A schema query used for completion of both schema and relation names.
 * These are more complex and must contain in the following order:
 * %d %s %d %s %d %s %s %d %s
 * where %d is the string length of the text and %s the text itself.
 *
 * It is assumed that strings should be escaped to become SQL literals
 * (that is, what is in the query is actually ... '%s' ...)
 *
 * See top of file for examples of both kinds of query.
 */
3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145
static char *
_complete_from_query(int is_schema_query, const char *text, int state)
{
	static int	list_index,
				string_length;
	static PGresult *result = NULL;

	/*
	 * If this is the first time for this completion, we fetch a list of our
	 * "things" from the backend.
	 */
	if (state == 0)
	{
		PQExpBufferData query_buffer;
		char	   *e_text;
		char	   *e_info_charp;
3146
		char	   *e_info_charp2;
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170

		list_index = 0;
		string_length = strlen(text);

		/* Free any prior result */
		PQclear(result);
		result = NULL;

		/* Set up suitably-escaped copies of textual inputs */
		e_text = pg_malloc(string_length * 2 + 1);
		PQescapeString(e_text, text, string_length);

		if (completion_info_charp)
		{
			size_t		charp_len;

			charp_len = strlen(completion_info_charp);
			e_info_charp = pg_malloc(charp_len * 2 + 1);
			PQescapeString(e_info_charp, completion_info_charp,
						   charp_len);
		}
		else
			e_info_charp = NULL;

3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182
		if (completion_info_charp2)
		{
			size_t		charp_len;

			charp_len = strlen(completion_info_charp2);
			e_info_charp2 = pg_malloc(charp_len * 2 + 1);
			PQescapeString(e_info_charp2, completion_info_charp2,
						   charp_len);
		}
		else
			e_info_charp2 = NULL;

3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277
		initPQExpBuffer(&query_buffer);

		if (is_schema_query)
		{
			/* completion_squery gives us the pieces to assemble */
			const char *qualresult = completion_squery->qualresult;

			if (qualresult == NULL)
				qualresult = completion_squery->result;

			/* Get unqualified names matching the input-so-far */
			appendPQExpBuffer(&query_buffer, "SELECT %s FROM %s WHERE ",
							  completion_squery->result,
							  completion_squery->catname);
			if (completion_squery->selcondition)
				appendPQExpBuffer(&query_buffer, "%s AND ",
								  completion_squery->selcondition);
			appendPQExpBuffer(&query_buffer, "substring(%s,1,%d)='%s'",
							  completion_squery->result,
							  string_length, e_text);
			appendPQExpBuffer(&query_buffer, " AND %s",
							  completion_squery->viscondition);

			/*
			 * When fetching relation names, suppress system catalogs unless
			 * the input-so-far begins with "pg_".	This is a compromise
			 * between not offering system catalogs for completion at all, and
			 * having them swamp the result when the input is just "p".
			 */
			if (strcmp(completion_squery->catname,
					   "pg_catalog.pg_class c") == 0 &&
				strncmp(text, "pg_", 3) !=0)
			{
				appendPQExpBuffer(&query_buffer,
								  " AND c.relnamespace <> (SELECT oid FROM"
				   " pg_catalog.pg_namespace WHERE nspname = 'pg_catalog')");
			}

			/*
			 * Add in matching schema names, but only if there is more than
			 * one potential match among schema names.
			 */
			appendPQExpBuffer(&query_buffer, "\nUNION\n"
						   "SELECT pg_catalog.quote_ident(n.nspname) || '.' "
							  "FROM pg_catalog.pg_namespace n "
							  "WHERE substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d)='%s'",
							  string_length, e_text);
			appendPQExpBuffer(&query_buffer,
							  " AND (SELECT pg_catalog.count(*)"
							  " FROM pg_catalog.pg_namespace"
			" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
							  " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) > 1",
							  string_length, e_text);

			/*
			 * Add in matching qualified names, but only if there is exactly
			 * one schema matching the input-so-far.
			 */
			appendPQExpBuffer(&query_buffer, "\nUNION\n"
					 "SELECT pg_catalog.quote_ident(n.nspname) || '.' || %s "
							  "FROM %s, pg_catalog.pg_namespace n "
							  "WHERE %s = n.oid AND ",
							  qualresult,
							  completion_squery->catname,
							  completion_squery->namespace);
			if (completion_squery->selcondition)
				appendPQExpBuffer(&query_buffer, "%s AND ",
								  completion_squery->selcondition);
			appendPQExpBuffer(&query_buffer, "substring(pg_catalog.quote_ident(n.nspname) || '.' || %s,1,%d)='%s'",
							  qualresult,
							  string_length, e_text);

			/*
			 * This condition exploits the single-matching-schema rule to
			 * speed up the query
			 */
			appendPQExpBuffer(&query_buffer,
			" AND substring(pg_catalog.quote_ident(n.nspname) || '.',1,%d) ="
							  " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(n.nspname))+1)",
							  string_length, e_text);
			appendPQExpBuffer(&query_buffer,
							  " AND (SELECT pg_catalog.count(*)"
							  " FROM pg_catalog.pg_namespace"
			" WHERE substring(pg_catalog.quote_ident(nspname) || '.',1,%d) ="
							  " substring('%s',1,pg_catalog.length(pg_catalog.quote_ident(nspname))+1)) = 1",
							  string_length, e_text);

			/* If an addon query was provided, use it */
			if (completion_charp)
				appendPQExpBuffer(&query_buffer, "\n%s", completion_charp);
		}
		else
		{
			/* completion_charp is an sprintf-style format string */
			appendPQExpBuffer(&query_buffer, completion_charp,
3278 3279 3280
							  string_length, e_text,
							  e_info_charp, e_info_charp,
							  e_info_charp2, e_info_charp2);
3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292
		}

		/* Limit the number of records in the result */
		appendPQExpBuffer(&query_buffer, "\nLIMIT %d",
						  completion_max_records);

		result = exec_query(query_buffer.data);

		termPQExpBuffer(&query_buffer);
		free(e_text);
		if (e_info_charp)
			free(e_info_charp);
3293 3294
		if (e_info_charp2)
			free(e_info_charp2);
3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312
	}

	/* Find something that matches */
	if (result && PQresultStatus(result) == PGRES_TUPLES_OK)
	{
		const char *item;

		while (list_index < PQntuples(result) &&
			   (item = PQgetvalue(result, list_index++, 0)))
			if (pg_strncasecmp(text, item, string_length) == 0)
				return pg_strdup(item);
	}

	/* If nothing matches, free the db structure and return null */
	PQclear(result);
	result = NULL;
	return NULL;
}
3313 3314


3315 3316 3317 3318 3319
/*
 * This function returns in order one of a fixed, NULL pointer terminated list
 * of strings (if matching). This can be used if there are only a fixed number
 * SQL words that can appear at certain spot.
 */
3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
static char *
complete_from_list(const char *text, int state)
{
	static int	string_length,
				list_index,
				matches;
	static bool casesensitive;
	const char *item;

	/* need to have a list */
	psql_assert(completion_charpp);

	/* Initialization */
	if (state == 0)
	{
		list_index = 0;
		string_length = strlen(text);
3337
		casesensitive = completion_case_sensitive;
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351
		matches = 0;
	}

	while ((item = completion_charpp[list_index++]))
	{
		/* First pass is case sensitive */
		if (casesensitive && strncmp(text, item, string_length) == 0)
		{
			matches++;
			return pg_strdup(item);
		}

		/* Second pass is case insensitive, don't bother counting matches */
		if (!casesensitive && pg_strncasecmp(text, item, string_length) == 0)
3352 3353 3354 3355 3356 3357 3358 3359
		{
			if (completion_case_sensitive)
				return pg_strdup(item);
			else
				/* If case insensitive matching was requested initially, return
				 * it in the case of what was already entered. */
				return pg_strdup_same_case(item, text);
		}
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370
	}

	/*
	 * No matches found. If we're not case insensitive already, lets switch to
	 * being case insensitive and try again
	 */
	if (casesensitive && matches == 0)
	{
		casesensitive = false;
		list_index = 0;
		state++;
3371
		return complete_from_list(text, state);
3372 3373 3374 3375 3376
	}

	/* If no more matches, return null. */
	return NULL;
}
3377 3378


3379 3380 3381 3382 3383 3384 3385
/*
 * This function returns one fixed string the first time even if it doesn't
 * match what's there, and nothing the second time. This should be used if
 * there is only one possibility that can appear at a certain spot, so
 * misspellings will be overwritten.  The string to be passed must be in
 * completion_charp.
 */
3386 3387 3388 3389 3390
static char *
complete_from_const(const char *text, int state)
{
	psql_assert(completion_charp);
	if (state == 0)
3391 3392 3393 3394 3395 3396 3397 3398
	{
		if (completion_case_sensitive)
			return pg_strdup(completion_charp);
		else
			/* If case insensitive matching was requested initially, return it
			 * in the case of what was already entered. */
			return pg_strdup_same_case(completion_charp, text);
	}
3399 3400 3401
	else
		return NULL;
}
3402 3403


3404 3405 3406 3407 3408 3409 3410 3411 3412 3413
/*
 * This function supports completion with the name of a psql variable.
 * The variable names can be prefixed and suffixed with additional text
 * to support quoting usages.
 */
static char **
complete_from_variables(char *text, const char *prefix, const char *suffix)
{
	char	  **matches;
	int			overhead = strlen(prefix) + strlen(suffix) + 1;
3414
	char	  **varnames;
3415 3416 3417 3418 3419
	int			nvars = 0;
	int			maxvars = 100;
	int			i;
	struct _variable *ptr;

3420
	varnames = (char **) pg_malloc((maxvars + 1) * sizeof(char *));
3421 3422 3423

	for (ptr = pset.vars->next; ptr; ptr = ptr->next)
	{
3424
		char	   *buffer;
3425 3426 3427 3428

		if (nvars >= maxvars)
		{
			maxvars *= 2;
3429 3430
			varnames = (char **) realloc(varnames,
										 (maxvars + 1) * sizeof(char *));
3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443
			if (!varnames)
			{
				psql_error("out of memory\n");
				exit(EXIT_FAILURE);
			}
		}

		buffer = (char *) pg_malloc(strlen(ptr->name) + overhead);
		sprintf(buffer, "%s%s%s", prefix, ptr->name, suffix);
		varnames[nvars++] = buffer;
	}

	varnames[nvars] = NULL;
3444
	COMPLETE_WITH_LIST_CS((const char * const *) varnames);
3445 3446

	for (i = 0; i < nvars; i++)
3447
		free(varnames[i]);
3448 3449 3450 3451 3452
	free(varnames);

	return matches;
}

3453

3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500
/*
 * This function wraps rl_filename_completion_function() to strip quotes from
 * the input before searching for matches and to quote any matches for which
 * the consuming command will require it.
 */
static char *
complete_from_files(const char *text, int state)
{
	static const char *unquoted_text;
	char	   *unquoted_match;
	char	   *ret = NULL;

	if (state == 0)
	{
		/* Initialization: stash the unquoted input. */
		unquoted_text = strtokx(text, "", NULL, "'", *completion_charp,
								false, true, pset.encoding);
		/* expect a NULL return for the empty string only */
		if (!unquoted_text)
		{
			psql_assert(!*text);
			unquoted_text = text;
		}
	}

	unquoted_match = filename_completion_function(unquoted_text, state);
	if (unquoted_match)
	{
		/*
		 * Caller sets completion_charp to a zero- or one-character string
		 * containing the escape character.  This is necessary since \copy has
		 * no escape character, but every other backslash command recognizes
		 * "\" as an escape character.  Since we have only two callers, don't
		 * bother providing a macro to simplify this.
		 */
		ret = quote_if_needed(unquoted_match, " \t\r\n\"`",
							  '\'', *completion_charp, pset.encoding);
		if (ret)
			free(unquoted_match);
		else
			ret = unquoted_match;
	}

	return ret;
}


3501 3502 3503
/* HELPER FUNCTIONS */


3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528
/*
 * Make a pg_strdup copy of s and convert it to the same case as ref.
 */
static char *
pg_strdup_same_case(const char *s, const char *ref)
{
	char *ret, *p;
	unsigned char first = ref[0];

	if (isalpha(first))
	{
		ret = pg_strdup(s);
		if (islower(first))
			for (p = ret; *p; p++)
				*p = pg_tolower((unsigned char) *p);
		else
			for (p = ret; *p; p++)
				*p = pg_toupper((unsigned char) *p);
		return ret;
	}
	else
		return pg_strdup(s);
}


3529 3530 3531 3532
/*
 * Execute a query and report any errors. This should be the preferred way of
 * talking to the database in this file.
 */
3533 3534 3535 3536
static PGresult *
exec_query(const char *query)
{
	PGresult   *result;
3537

3538 3539
	if (query == NULL || !pset.db || PQstatus(pset.db) != CONNECTION_OK)
		return NULL;
3540

3541
	result = PQexec(pset.db, query);
3542

3543
	if (PQresultStatus(result) != PGRES_TUPLES_OK)
3544
	{
3545
#ifdef NOT_USED
3546 3547
		psql_error("tab completion query failed: %s\nQuery was:\n%s\n",
				   PQerrorMessage(pset.db), query);
3548
#endif
3549 3550 3551
		PQclear(result);
		result = NULL;
	}
3552

3553 3554
	return result;
}
3555 3556


3557
/*
3558 3559 3560 3561
 * Return the nwords word(s) before point.  Words are returned right to left,
 * that is, previous_words[0] gets the last word before point.
 * If we run out of words, remaining array elements are set to empty strings.
 * Each array element is filled with a malloc'd string.
3562
 */
3563 3564
static void
get_previous_words(int point, char **previous_words, int nwords)
3565
{
3566
	const char *buf = rl_line_buffer;	/* alias */
3567
	int			i;
3568

3569
	/* first we look for a non-word char before the current point */
3570 3571 3572 3573
	for (i = point - 1; i >= 0; i--)
		if (strchr(WORD_BREAKS, buf[i]))
			break;
	point = i;
3574

3575
	while (nwords-- > 0)
3576
	{
3577 3578 3579
		int			start,
					end;
		char	   *s;
3580 3581

		/* now find the first non-space which then constitutes the end */
3582
		end = -1;
3583
		for (i = point; i >= 0; i--)
3584 3585
		{
			if (!isspace((unsigned char) buf[i]))
3586 3587 3588 3589
			{
				end = i;
				break;
			}
3590
		}
3591 3592

		/*
3593 3594
		 * If no end found we return an empty string, because there is no word
		 * before the point
3595
		 */
3596 3597 3598 3599 3600 3601
		if (end < 0)
		{
			point = end;
			s = pg_strdup("");
		}
		else
3602
		{
3603 3604 3605 3606 3607 3608 3609 3610 3611 3612
			/*
			 * Otherwise we now look for the start. The start is either the
			 * last character before any word-break character going backwards
			 * from the end, or it's simply character 0. We also handle open
			 * quotes and parentheses.
			 */
			bool		inquotes = false;
			int			parentheses = 0;

			for (start = end; start > 0; start--)
3613
			{
3614 3615
				if (buf[start] == '"')
					inquotes = !inquotes;
3616
				if (!inquotes)
3617
				{
3618 3619 3620 3621 3622 3623 3624 3625 3626
					if (buf[start] == ')')
						parentheses++;
					else if (buf[start] == '(')
					{
						if (--parentheses <= 0)
							break;
					}
					else if (parentheses == 0 &&
							 strchr(WORD_BREAKS, buf[start - 1]))
3627 3628 3629
						break;
				}
			}
3630

3631
			point = start - 1;
3632

3633 3634 3635 3636
			/* make a copy of chars from start to end inclusive */
			s = pg_malloc(end - start + 2);
			strlcpy(s, &buf[start], end - start + 2);
		}
3637

3638 3639
		*previous_words++ = s;
	}
3640
}
3641

3642
#ifdef NOT_USED
3643

P
Peter Eisentraut 已提交
3644 3645
/*
 * Surround a string with single quotes. This works for both SQL and
3646
 * psql internal. Currently disabled because it is reported not to
P
Peter Eisentraut 已提交
3647 3648
 * cooperate with certain versions of readline.
 */
3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
static char *
quote_file_name(char *text, int match_type, char *quote_pointer)
{
	char	   *s;
	size_t		length;

	(void) quote_pointer;		/* not used */

	length = strlen(text) +(match_type == SINGLE_MATCH ? 3 : 2);
	s = pg_malloc(length);
	s[0] = '\'';
	strcpy(s + 1, text);
	if (match_type == SINGLE_MATCH)
		s[length - 2] = '\'';
	s[length - 1] = '\0';
	return s;
}

static char *
dequote_file_name(char *text, char quote_char)
{
	char	   *s;
	size_t		length;

	if (!quote_char)
		return pg_strdup(text);

	length = strlen(text);
	s = pg_malloc(length - 2 + 1);
3678
	strlcpy(s, text +1, length - 2 + 1);
3679 3680 3681

	return s;
}
3682
#endif   /* NOT_USED */
3683

3684
#endif   /* USE_READLINE */