Skip to content

Commit 79b9564

Browse files
committed
List psql tab completion for various default keywords.
Joachim Wieland
1 parent 20b5083 commit 79b9564

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

src/bin/psql/tab-complete.c

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.146 2006/02/12 03:22:19 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.147 2006/02/12 07:21:40 momjian Exp $
77
*/
88

99
/*----------------------------------------------------------------------
@@ -140,14 +140,16 @@ static const SchemaQuery *completion_squery; /* to pass a SchemaQuery */
140140
*/
141141
#define COMPLETE_WITH_QUERY(query) \
142142
do { completion_charp = query; matches = completion_matches(text, complete_from_query); } while(0)
143-
#define COMPLETE_WITH_SCHEMA_QUERY(query,addon) \
143+
#define COMPLETE_WITH_QUERY_ADDON(query, addon) \
144+
do { completion_charp = query addon; matches = completion_matches(text, complete_from_query); } while(0)
145+
#define COMPLETE_WITH_SCHEMA_QUERY(query, addon) \
144146
do { completion_squery = &(query); completion_charp = addon; matches = completion_matches(text, complete_from_schema_query); } while(0)
145147
#define COMPLETE_WITH_LIST(list) \
146148
do { completion_charpp = list; matches = completion_matches(text, complete_from_list); } while(0)
147149
#define COMPLETE_WITH_CONST(string) \
148150
do { completion_charp = string; matches = completion_matches(text, complete_from_const); } while(0)
149-
#define COMPLETE_WITH_ATTR(table) \
150-
do {completion_charp = Query_for_list_of_attributes; completion_info_charp = table; matches = completion_matches(text, complete_from_query); } while(0)
151+
#define COMPLETE_WITH_ATTR(table, addon) \
152+
do {completion_charp = Query_for_list_of_attributes addon; completion_info_charp = table; matches = completion_matches(text, complete_from_query); } while(0)
151153

152154
/*
153155
* Assembly instructions for schema queries
@@ -754,14 +756,28 @@ psql_completion(char *text, int start, int end)
754756
else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
755757
(pg_strcasecmp(prev_wd, "ALTER") == 0 ||
756758
pg_strcasecmp(prev_wd, "RENAME") == 0))
757-
COMPLETE_WITH_ATTR(prev2_wd);
759+
COMPLETE_WITH_ATTR(prev2_wd, " UNION SELECT 'COLUMN'");
758760

761+
/* If we have TABLE <sth> ALTER COLUMN|RENAME COLUMN, provide list of columns */
762+
else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
763+
(pg_strcasecmp(prev2_wd, "ALTER") == 0 ||
764+
pg_strcasecmp(prev2_wd, "RENAME") == 0) &&
765+
pg_strcasecmp(prev_wd, "COLUMN") == 0)
766+
COMPLETE_WITH_ATTR(prev3_wd, "");
767+
759768
/* ALTER TABLE xxx RENAME yyy */
760769
else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
761770
pg_strcasecmp(prev2_wd, "RENAME") == 0 &&
762771
pg_strcasecmp(prev_wd, "TO") != 0)
763772
COMPLETE_WITH_CONST("TO");
764773

774+
/* ALTER TABLE xxx RENAME COLUMN yyy */
775+
else if (pg_strcasecmp(prev5_wd, "TABLE") == 0 &&
776+
pg_strcasecmp(prev3_wd, "RENAME") == 0 &&
777+
pg_strcasecmp(prev2_wd, "COLUMN") == 0 &&
778+
pg_strcasecmp(prev_wd, "TO") != 0)
779+
COMPLETE_WITH_CONST("TO");
780+
765781
/* If we have TABLE <sth> DROP, provide COLUMN or CONSTRAINT */
766782
else if (pg_strcasecmp(prev3_wd, "TABLE") == 0 &&
767783
pg_strcasecmp(prev_wd, "DROP") == 0)
@@ -775,7 +791,7 @@ psql_completion(char *text, int start, int end)
775791
else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
776792
pg_strcasecmp(prev2_wd, "DROP") == 0 &&
777793
pg_strcasecmp(prev_wd, "COLUMN") == 0)
778-
COMPLETE_WITH_ATTR(prev3_wd);
794+
COMPLETE_WITH_ATTR(prev3_wd, "");
779795
/* ALTER TABLE ALTER [COLUMN] <foo> */
780796
else if ((pg_strcasecmp(prev3_wd, "ALTER") == 0 &&
781797
pg_strcasecmp(prev2_wd, "COLUMN") == 0) ||
@@ -1021,18 +1037,18 @@ psql_completion(char *text, int start, int end)
10211037
pg_strcasecmp(prev2_wd, "ON") == 0)
10221038
{
10231039
if (find_open_parenthesis(end))
1024-
COMPLETE_WITH_ATTR(prev_wd);
1040+
COMPLETE_WITH_ATTR(prev_wd, "");
10251041
else
10261042
COMPLETE_WITH_CONST("(");
10271043
}
10281044
else if (pg_strcasecmp(prev5_wd, "INDEX") == 0 &&
10291045
pg_strcasecmp(prev3_wd, "ON") == 0 &&
10301046
pg_strcasecmp(prev_wd, "(") == 0)
1031-
COMPLETE_WITH_ATTR(prev2_wd);
1047+
COMPLETE_WITH_ATTR(prev2_wd, "");
10321048
/* same if you put in USING */
10331049
else if (pg_strcasecmp(prev4_wd, "ON") == 0 &&
10341050
pg_strcasecmp(prev2_wd, "USING") == 0)
1035-
COMPLETE_WITH_ATTR(prev3_wd);
1051+
COMPLETE_WITH_ATTR(prev3_wd, "");
10361052
/* Complete USING with an index method */
10371053
else if (pg_strcasecmp(prev_wd, "USING") == 0)
10381054
{
@@ -1420,7 +1436,7 @@ psql_completion(char *text, int start, int end)
14201436
else if (rl_line_buffer[start - 1] == '(' &&
14211437
pg_strcasecmp(prev3_wd, "INSERT") == 0 &&
14221438
pg_strcasecmp(prev2_wd, "INTO") == 0)
1423-
COMPLETE_WITH_ATTR(prev_wd);
1439+
COMPLETE_WITH_ATTR(prev_wd, "");
14241440

14251441
/*
14261442
* Complete INSERT INTO <table> with "VALUES" or "SELECT" or "DEFAULT
@@ -1452,10 +1468,12 @@ psql_completion(char *text, int start, int end)
14521468

14531469
/* LOCK */
14541470
/* Complete LOCK [TABLE] with a list of tables */
1455-
else if (pg_strcasecmp(prev_wd, "LOCK") == 0 ||
1456-
(pg_strcasecmp(prev_wd, "TABLE") == 0 &&
1457-
pg_strcasecmp(prev2_wd, "LOCK") == 0))
1458-
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
1471+
else if (pg_strcasecmp(prev_wd, "LOCK") == 0)
1472+
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables,
1473+
" UNION SELECT 'TABLE'");
1474+
else if (pg_strcasecmp(prev_wd, "TABLE") == 0 &&
1475+
pg_strcasecmp(prev2_wd, "LOCK") == 0)
1476+
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, "");
14591477

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

@@ -1498,7 +1516,7 @@ psql_completion(char *text, int start, int end)
14981516
else if (pg_strcasecmp(prev4_wd, "FROM") == 0 &&
14991517
pg_strcasecmp(prev2_wd, "ORDER") == 0 &&
15001518
pg_strcasecmp(prev_wd, "BY") == 0)
1501-
COMPLETE_WITH_ATTR(prev3_wd);
1519+
COMPLETE_WITH_ATTR(prev3_wd, "");
15021520

15031521
/* PREPARE xx AS */
15041522
else if (pg_strcasecmp(prev_wd, "AS") == 0 &&
@@ -1639,7 +1657,7 @@ psql_completion(char *text, int start, int end)
16391657
else if (pg_strcasecmp(prev3_wd, "SET") == 0
16401658
&& pg_strcasecmp(prev2_wd, "SESSION") == 0
16411659
&& pg_strcasecmp(prev_wd, "AUTHORIZATION") == 0)
1642-
COMPLETE_WITH_QUERY(Query_for_list_of_roles);
1660+
COMPLETE_WITH_QUERY_ADDON(Query_for_list_of_roles, " UNION SELECT 'DEFAULT'");
16431661
/* Complete RESET SESSION with AUTHORIZATION */
16441662
else if (pg_strcasecmp(prev2_wd, "RESET") == 0 &&
16451663
pg_strcasecmp(prev_wd, "SESSION") == 0)
@@ -1706,7 +1724,7 @@ psql_completion(char *text, int start, int end)
17061724
* make a list of attributes.
17071725
*/
17081726
else if (pg_strcasecmp(prev_wd, "SET") == 0)
1709-
COMPLETE_WITH_ATTR(prev2_wd);
1727+
COMPLETE_WITH_ATTR(prev2_wd, "");
17101728

17111729
/* UPDATE xx SET yy = */
17121730
else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
@@ -1763,7 +1781,7 @@ psql_completion(char *text, int start, int end)
17631781
/* WHERE */
17641782
/* Simple case of the word before the where being the table name */
17651783
else if (pg_strcasecmp(prev_wd, "WHERE") == 0)
1766-
COMPLETE_WITH_ATTR(prev2_wd);
1784+
COMPLETE_WITH_ATTR(prev2_wd, "");
17671785

17681786
/* ... FROM ... */
17691787
/* TODO: also include SRF ? */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy