Commit 1b706651 authored by Bruce Momjian's avatar Bruce Momjian

Here's the patch I mentioned on HACKERS to fix up ALTER command tab

completion. Note that it's based on 7.3 tarball, not CVS HEAD, or 7.3rel
branch. Damn, looking at CVS, this will patch into 7.3rel (just tested,
it does) probably collide with Rod Taylor's patch adding ALTER TRIGGER
stuff. O.K, second patch attached against HEAD - not tested, hand
merged.

Ross Reedstrom
parent d2741059
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright 2000-2002 by PostgreSQL Global Development Group * Copyright 2000-2002 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.69 2002/11/21 00:42:19 tgl Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.70 2002/12/13 05:36:24 momjian Exp $
*/ */
/*---------------------------------------------------------------------- /*----------------------------------------------------------------------
...@@ -363,68 +363,96 @@ psql_completion(char *text, int start, int end) ...@@ -363,68 +363,96 @@ psql_completion(char *text, int start, int end)
else if (!prev_wd) else if (!prev_wd)
COMPLETE_WITH_LIST(sql_commands); COMPLETE_WITH_LIST(sql_commands);
/* CREATE or DROP */ /* CREATE or DROP but not ALTER TABLE sth DROP */
/* complete with something you can create or drop */ /* complete with something you can create or drop */
else if (strcasecmp(prev_wd, "CREATE") == 0 || strcasecmp(prev_wd, "DROP") == 0) else if (strcasecmp(prev_wd, "CREATE") == 0 ||
(strcasecmp(prev_wd, "DROP") == 0 &&
strcasecmp(prev3_wd,"TABLE") != 0 ))
matches = completion_matches(text, create_command_generator); matches = completion_matches(text, create_command_generator);
/* ALTER */ /* ALTER */
/* complete with what you can alter (TABLE, GROUP, USER, ...) */ /* complete with what you can alter (TABLE, GROUP, USER, ...)
else if (strcasecmp(prev_wd, "ALTER") == 0) * unless we're in ALTER TABLE sth ALTER*/
else if (strcasecmp(prev_wd, "ALTER") == 0 &&
strcasecmp(prev3_wd, "TABLE") != 0 )
{ {
char *list_ALTER[] = {"DATABASE", "GROUP", "SCHEMA", "TABLE", "TRIGGER", "USER", NULL}; char *list_ALTER[] = {"DATABASE", "GROUP", "SCHEMA", "TABLE",
"TRIGGER", "USER", NULL};
COMPLETE_WITH_LIST(list_ALTER); COMPLETE_WITH_LIST(list_ALTER);
} }
/* ALTER DATABASE <name> */ /* ALTER DATABASE <name> */
else if (strcasecmp(prev3_wd, "ALTER") == 0 && strcasecmp(prev2_wd, "DATABASE") == 0) else if (strcasecmp(prev3_wd, "ALTER") == 0 &&
strcasecmp(prev2_wd, "DATABASE") == 0)
{ {
char *list_ALTERDATABASE[] = {"RESET", "SET", NULL}; char *list_ALTERDATABASE[] = {"RESET", "SET", NULL};
COMPLETE_WITH_LIST(list_ALTERDATABASE); COMPLETE_WITH_LIST(list_ALTERDATABASE);
} }
/* ALTER TRIGGER <name>, add ON */ /* ALTER TRIGGER <name>, add ON */
else if (strcasecmp(prev3_wd, "ALTER") == 0 && strcasecmp(prev2_wd, "TRIGGER") == 0) else if (strcasecmp(prev3_wd, "ALTER") == 0 &&
strcasecmp(prev2_wd, "TRIGGER") == 0)
COMPLETE_WITH_CONST("ON"); COMPLETE_WITH_CONST("ON");
/* /*
* If we have ALTER TRIGGER <sth> ON, then add the correct tablename * If we have ALTER TRIGGER <sth> ON, then add the correct tablename
*/ */
else if (strcasecmp(prev4_wd, "ALTER") == 0 && strcasecmp(prev3_wd, "TRIGGER") == 0 else if (strcasecmp(prev4_wd, "ALTER") == 0 &&
&& strcasecmp(prev_wd, "ON") == 0) strcasecmp(prev3_wd, "TRIGGER") == 0 &&
strcasecmp(prev_wd, "ON") == 0)
COMPLETE_WITH_QUERY(Query_for_list_of_tables); COMPLETE_WITH_QUERY(Query_for_list_of_tables);
/* /*
* If we detect ALTER TABLE <name>, suggest either ADD, ALTER, or * If we detect ALTER TABLE <name>, suggest either ADD, DROP, ALTER,
* RENAME * RENAME, or OWNER
*/ */
else if (strcasecmp(prev3_wd, "ALTER") == 0 && strcasecmp(prev2_wd, "TABLE") == 0) else if (strcasecmp(prev3_wd, "ALTER") == 0 &&
strcasecmp(prev2_wd, "TABLE") == 0)
{ {
char *list_ALTER2[] = {"ADD", "ALTER", "RENAME", NULL}; char *list_ALTER2[] = {"ADD", "ALTER", "DROP", "RENAME",
"OWNER TO", NULL};
COMPLETE_WITH_LIST(list_ALTER2); COMPLETE_WITH_LIST(list_ALTER2);
} }
/* If we have TABLE <sth> ALTER|RENAME, provide list of columns */ /* If we have TABLE <sth> ALTER|RENAME, provide list of columns */
else if (strcasecmp(prev3_wd, "TABLE") == 0 && else if (strcasecmp(prev3_wd, "TABLE") == 0 &&
(strcasecmp(prev_wd, "ALTER") == 0 || strcasecmp(prev_wd, "RENAME") == 0)) (strcasecmp(prev_wd, "ALTER") == 0 ||
strcasecmp(prev_wd, "RENAME") == 0))
COMPLETE_WITH_ATTR(prev2_wd); COMPLETE_WITH_ATTR(prev2_wd);
/* If we have TABLE <sth> DROP, provide COLUMN or CONSTRAINT */
else if (strcasecmp(prev3_wd, "TABLE") == 0 &&
strcasecmp(prev_wd, "DROP") == 0)
{
char *list_TABLEDROP[] = {"COLUMN", "CONSTRAINT", NULL};
COMPLETE_WITH_LIST(list_TABLEDROP);
}
/* If we have TABLE <sth> DROP COLUMN, provide list of columns */
else if (strcasecmp(prev4_wd, "TABLE") == 0 &&
strcasecmp(prev2_wd, "DROP") == 0 &&
strcasecmp(prev_wd, "COLUMN") == 0)
COMPLETE_WITH_ATTR(prev3_wd);
/* complete ALTER GROUP <foo> with ADD or DROP */ /* complete ALTER GROUP <foo> with ADD or DROP */
else if (strcasecmp(prev3_wd, "ALTER") == 0 && strcasecmp(prev2_wd, "GROUP") == 0) else if (strcasecmp(prev3_wd, "ALTER") == 0 &&
strcasecmp(prev2_wd, "GROUP") == 0)
{ {
char *list_ALTERGROUP[] = {"ADD", "DROP", NULL}; char *list_ALTERGROUP[] = {"ADD", "DROP", NULL};
COMPLETE_WITH_LIST(list_ALTERGROUP); COMPLETE_WITH_LIST(list_ALTERGROUP);
} }
/* complete ALTER GROUP <foo> ADD|DROP with USER */ /* complete ALTER GROUP <foo> ADD|DROP with USER */
else if (strcasecmp(prev4_wd, "ALTER") == 0 && strcasecmp(prev3_wd, "GROUP") == 0 else if (strcasecmp(prev4_wd, "ALTER") == 0 &&
&& (strcasecmp(prev_wd, "ADD") == 0 || strcasecmp(prev_wd, "DROP") == 0)) strcasecmp(prev3_wd, "GROUP") == 0 &&
(strcasecmp(prev_wd, "ADD") == 0 ||
strcasecmp(prev_wd, "DROP") == 0))
COMPLETE_WITH_CONST("USER"); COMPLETE_WITH_CONST("USER");
/* complete {ALTER} GROUP <foo> ADD|DROP USER with a user name */ /* complete {ALTER} GROUP <foo> ADD|DROP USER with a user name */
else if (strcasecmp(prev4_wd, "GROUP") == 0 else if (strcasecmp(prev4_wd, "GROUP") == 0 &&
&& (strcasecmp(prev2_wd, "ADD") == 0 || strcasecmp(prev2_wd, "DROP") == 0) (strcasecmp(prev2_wd, "ADD") == 0 ||
&& strcasecmp(prev_wd, "USER") == 0) strcasecmp(prev2_wd, "DROP") == 0) &&
strcasecmp(prev_wd, "USER") == 0)
COMPLETE_WITH_QUERY(Query_for_list_of_users); COMPLETE_WITH_QUERY(Query_for_list_of_users);
/* ANALYZE */ /* ANALYZE */
...@@ -447,7 +475,8 @@ psql_completion(char *text, int start, int end) ...@@ -447,7 +475,8 @@ psql_completion(char *text, int start, int end)
* If we have CLUSTER <sth> ON, then add the correct tablename as * If we have CLUSTER <sth> ON, then add the correct tablename as
* well. * well.
*/ */
else if (strcasecmp(prev3_wd, "CLUSTER") == 0 && strcasecmp(prev_wd, "ON") == 0) else if (strcasecmp(prev3_wd, "CLUSTER") == 0 &&
strcasecmp(prev_wd, "ON") == 0)
{ {
char query_buffer[BUF_SIZE]; /* Some room to build char query_buffer[BUF_SIZE]; /* Some room to build
* queries. */ * queries. */
...@@ -463,16 +492,18 @@ psql_completion(char *text, int start, int end) ...@@ -463,16 +492,18 @@ psql_completion(char *text, int start, int end)
/* COMMENT */ /* COMMENT */
else if (strcasecmp(prev_wd, "COMMENT") == 0) else if (strcasecmp(prev_wd, "COMMENT") == 0)
COMPLETE_WITH_CONST("ON"); COMPLETE_WITH_CONST("ON");
else if (strcasecmp(prev2_wd, "COMMENT") == 0 && strcasecmp(prev_wd, "ON") == 0) else if (strcasecmp(prev2_wd, "COMMENT") == 0 &&
strcasecmp(prev_wd, "ON") == 0)
{ {
char *list_COMMENT[] = char *list_COMMENT[] =
{"DATABASE", "INDEX", "RULE", "SCHEMA", "SEQUENCE", "TABLE", "TYPE", "VIEW", {"DATABASE", "INDEX", "RULE", "SCHEMA", "SEQUENCE", "TABLE",
"COLUMN", "AGGREGATE", "FUNCTION", "OPERATOR", "TRIGGER", "CONSTRAINT", "TYPE", "VIEW", "COLUMN", "AGGREGATE", "FUNCTION", "OPERATOR",
"DOMAIN", NULL}; "TRIGGER", "CONSTRAINT", "DOMAIN", NULL};
COMPLETE_WITH_LIST(list_COMMENT); COMPLETE_WITH_LIST(list_COMMENT);
} }
else if (strcasecmp(prev4_wd, "COMMENT") == 0 && strcasecmp(prev3_wd, "ON") == 0) else if (strcasecmp(prev4_wd, "COMMENT") == 0 &&
strcasecmp(prev3_wd, "ON") == 0)
COMPLETE_WITH_CONST("IS"); COMPLETE_WITH_CONST("IS");
/* COPY */ /* COPY */
...@@ -483,7 +514,8 @@ psql_completion(char *text, int start, int end) ...@@ -483,7 +514,8 @@ psql_completion(char *text, int start, int end)
*/ */
else if (strcasecmp(prev_wd, "COPY") == 0 || else if (strcasecmp(prev_wd, "COPY") == 0 ||
strcasecmp(prev_wd, "\\copy") == 0 || strcasecmp(prev_wd, "\\copy") == 0 ||
(strcasecmp(prev2_wd, "COPY") == 0 && strcasecmp(prev_wd, "BINARY") == 0)) (strcasecmp(prev2_wd, "COPY") == 0 &&
strcasecmp(prev_wd, "BINARY") == 0))
COMPLETE_WITH_QUERY(Query_for_list_of_tables); COMPLETE_WITH_QUERY(Query_for_list_of_tables);
/* If we have COPY|BINARY <sth>, complete it with "TO" or "FROM" */ /* If we have COPY|BINARY <sth>, complete it with "TO" or "FROM" */
else if (strcasecmp(prev2_wd, "COPY") == 0 || else if (strcasecmp(prev2_wd, "COPY") == 0 ||
...@@ -497,24 +529,29 @@ psql_completion(char *text, int start, int end) ...@@ -497,24 +529,29 @@ psql_completion(char *text, int start, int end)
/* CREATE INDEX */ /* CREATE INDEX */
/* First off we complete CREATE UNIQUE with "INDEX" */ /* First off we complete CREATE UNIQUE with "INDEX" */
else if (strcasecmp(prev2_wd, "CREATE") == 0 && strcasecmp(prev_wd, "UNIQUE") == 0) else if (strcasecmp(prev2_wd, "CREATE") == 0 &&
strcasecmp(prev_wd, "UNIQUE") == 0)
COMPLETE_WITH_CONST("INDEX"); COMPLETE_WITH_CONST("INDEX");
/* If we have CREATE|UNIQUE INDEX <sth>, then add "ON" */ /* If we have CREATE|UNIQUE INDEX <sth>, then add "ON" */
else if (strcasecmp(prev2_wd, "INDEX") == 0 && else if (strcasecmp(prev2_wd, "INDEX") == 0 &&
(strcasecmp(prev3_wd, "CREATE") == 0 || strcasecmp(prev3_wd, "UNIQUE") == 0)) (strcasecmp(prev3_wd, "CREATE") == 0 ||
strcasecmp(prev3_wd, "UNIQUE") == 0))
COMPLETE_WITH_CONST("ON"); COMPLETE_WITH_CONST("ON");
/* Complete ... INDEX <name> ON with a list of tables */ /* Complete ... INDEX <name> ON with a list of tables */
else if ((strcasecmp(prev3_wd, "INDEX") == 0 && strcasecmp(prev_wd, "ON") == 0) || (0)) else if (strcasecmp(prev3_wd, "INDEX") == 0 &&
strcasecmp(prev_wd, "ON") == 0)
COMPLETE_WITH_QUERY(Query_for_list_of_tables); COMPLETE_WITH_QUERY(Query_for_list_of_tables);
/* /*
* Complete INDEX <name> ON <table> with a list of table columns * Complete INDEX <name> ON <table> with a list of table columns
* (which should really be in parens) * (which should really be in parens)
*/ */
else if (strcasecmp(prev4_wd, "INDEX") == 0 && strcasecmp(prev2_wd, "ON") == 0) else if (strcasecmp(prev4_wd, "INDEX") == 0 &&
strcasecmp(prev2_wd, "ON") == 0)
COMPLETE_WITH_ATTR(prev_wd); COMPLETE_WITH_ATTR(prev_wd);
/* same if you put in USING */ /* same if you put in USING */
else if ((strcasecmp(prev4_wd, "ON") == 0 && strcasecmp(prev2_wd, "USING") == 0)) else if (strcasecmp(prev4_wd, "ON") == 0 &&
strcasecmp(prev2_wd, "USING") == 0)
COMPLETE_WITH_ATTR(prev3_wd); COMPLETE_WITH_ATTR(prev3_wd);
/* Complete USING with an index method */ /* Complete USING with an index method */
else if (strcasecmp(prev_wd, "USING") == 0) else if (strcasecmp(prev_wd, "USING") == 0)
...@@ -526,7 +563,8 @@ psql_completion(char *text, int start, int end) ...@@ -526,7 +563,8 @@ psql_completion(char *text, int start, int end)
/* CREATE RULE */ /* CREATE RULE */
/* Complete "CREATE RULE <sth>" with "AS" */ /* Complete "CREATE RULE <sth>" with "AS" */
else if (strcasecmp(prev3_wd, "CREATE") == 0 && strcasecmp(prev2_wd, "RULE") == 0) else if (strcasecmp(prev3_wd, "CREATE") == 0 &&
strcasecmp(prev2_wd, "RULE") == 0)
COMPLETE_WITH_CONST("AS"); COMPLETE_WITH_CONST("AS");
/* Complete "CREATE RULE <sth> AS with "ON" */ /* Complete "CREATE RULE <sth> AS with "ON" */
else if (strcasecmp(prev4_wd, "CREATE") == 0 && else if (strcasecmp(prev4_wd, "CREATE") == 0 &&
...@@ -538,7 +576,8 @@ psql_completion(char *text, int start, int end) ...@@ -538,7 +576,8 @@ psql_completion(char *text, int start, int end)
strcasecmp(prev2_wd, "AS") == 0 && strcasecmp(prev2_wd, "AS") == 0 &&
strcasecmp(prev_wd, "ON") == 0) strcasecmp(prev_wd, "ON") == 0)
{ {
char *rule_events[] = {"SELECT", "UPDATE", "INSERT", "DELETE", NULL}; char *rule_events[] = {"SELECT", "UPDATE", "INSERT",
"DELETE", NULL};
COMPLETE_WITH_LIST(rule_events); COMPLETE_WITH_LIST(rule_events);
} }
...@@ -556,7 +595,8 @@ psql_completion(char *text, int start, int end) ...@@ -556,7 +595,8 @@ psql_completion(char *text, int start, int end)
/* CREATE TABLE */ /* CREATE TABLE */
/* Complete CREATE TEMP with "TABLE" */ /* Complete CREATE TEMP with "TABLE" */
else if (strcasecmp(prev2_wd, "CREATE") == 0 && strcasecmp(prev_wd, "TEMP") == 0) else if (strcasecmp(prev2_wd, "CREATE") == 0 &&
strcasecmp(prev_wd, "TEMP") == 0)
COMPLETE_WITH_CONST("TABLE"); COMPLETE_WITH_CONST("TABLE");
/* CREATE TRIGGER */ /* CREATE TRIGGER */
...@@ -564,7 +604,8 @@ psql_completion(char *text, int start, int end) ...@@ -564,7 +604,8 @@ psql_completion(char *text, int start, int end)
/* CREATE VIEW */ /* CREATE VIEW */
/* Complete "CREATE VIEW <name>" with "AS" */ /* Complete "CREATE VIEW <name>" with "AS" */
else if (strcasecmp(prev3_wd, "CREATE") == 0 && strcasecmp(prev2_wd, "VIEW") == 0) else if (strcasecmp(prev3_wd, "CREATE") == 0 &&
strcasecmp(prev2_wd, "VIEW") == 0)
COMPLETE_WITH_CONST("AS"); COMPLETE_WITH_CONST("AS");
/* Complete "CREATE VIEW <sth> AS with "SELECT" */ /* Complete "CREATE VIEW <sth> AS with "SELECT" */
else if (strcasecmp(prev4_wd, "CREATE") == 0 && else if (strcasecmp(prev4_wd, "CREATE") == 0 &&
...@@ -585,10 +626,12 @@ psql_completion(char *text, int start, int end) ...@@ -585,10 +626,12 @@ psql_completion(char *text, int start, int end)
strcasecmp(prev2_wd, "AFTER") == 0)) strcasecmp(prev2_wd, "AFTER") == 0))
COMPLETE_WITH_CONST("FROM"); COMPLETE_WITH_CONST("FROM");
/* Complete DELETE FROM with a list of tables */ /* Complete DELETE FROM with a list of tables */
else if (strcasecmp(prev2_wd, "DELETE") == 0 && strcasecmp(prev_wd, "FROM") == 0) else if (strcasecmp(prev2_wd, "DELETE") == 0 &&
strcasecmp(prev_wd, "FROM") == 0)
COMPLETE_WITH_QUERY(Query_for_list_of_tables); COMPLETE_WITH_QUERY(Query_for_list_of_tables);
/* Complete DELETE FROM <table> with "WHERE" (perhaps a safe idea?) */ /* Complete DELETE FROM <table> with "WHERE" (perhaps a safe idea?) */
else if (strcasecmp(prev3_wd, "DELETE") == 0 && strcasecmp(prev2_wd, "FROM") == 0) else if (strcasecmp(prev3_wd, "DELETE") == 0 &&
strcasecmp(prev2_wd, "FROM") == 0)
COMPLETE_WITH_CONST("WHERE"); COMPLETE_WITH_CONST("WHERE");
/* EXPLAIN */ /* EXPLAIN */
...@@ -598,19 +641,22 @@ psql_completion(char *text, int start, int end) ...@@ -598,19 +641,22 @@ psql_completion(char *text, int start, int end)
* the list of SQL commands * the list of SQL commands
*/ */
else if (strcasecmp(prev_wd, "EXPLAIN") == 0 || else if (strcasecmp(prev_wd, "EXPLAIN") == 0 ||
(strcasecmp(prev2_wd, "EXPLAIN") == 0 && strcasecmp(prev_wd, "VERBOSE") == 0)) (strcasecmp(prev2_wd, "EXPLAIN") == 0 &&
strcasecmp(prev_wd, "VERBOSE") == 0))
COMPLETE_WITH_LIST(sql_commands); COMPLETE_WITH_LIST(sql_commands);
/* FETCH && MOVE */ /* FETCH && MOVE */
/* Complete FETCH with one of FORWARD, BACKWARD, RELATIVE */ /* Complete FETCH with one of FORWARD, BACKWARD, RELATIVE */
else if (strcasecmp(prev_wd, "FETCH") == 0 || strcasecmp(prev_wd, "MOVE") == 0) else if (strcasecmp(prev_wd, "FETCH") == 0 ||
strcasecmp(prev_wd, "MOVE") == 0)
{ {
char *list_FETCH1[] = {"FORWARD", "BACKWARD", "RELATIVE", NULL}; char *list_FETCH1[] = {"FORWARD", "BACKWARD", "RELATIVE", NULL};
COMPLETE_WITH_LIST(list_FETCH1); COMPLETE_WITH_LIST(list_FETCH1);
} }
/* Complete FETCH <sth> with one of ALL, NEXT, PRIOR */ /* Complete FETCH <sth> with one of ALL, NEXT, PRIOR */
else if (strcasecmp(prev2_wd, "FETCH") == 0 || strcasecmp(prev2_wd, "MOVE") == 0) else if (strcasecmp(prev2_wd, "FETCH") == 0 ||
strcasecmp(prev2_wd, "MOVE") == 0)
{ {
char *list_FETCH2[] = {"ALL", "NEXT", "PRIOR", NULL}; char *list_FETCH2[] = {"ALL", "NEXT", "PRIOR", NULL};
...@@ -621,7 +667,8 @@ psql_completion(char *text, int start, int end) ...@@ -621,7 +667,8 @@ psql_completion(char *text, int start, int end)
* Complete FETCH <sth1> <sth2> with "FROM" or "TO". (Is there a * Complete FETCH <sth1> <sth2> with "FROM" or "TO". (Is there a
* difference? If not, remove one.) * difference? If not, remove one.)
*/ */
else if (strcasecmp(prev3_wd, "FETCH") == 0 || strcasecmp(prev3_wd, "MOVE") == 0) else if (strcasecmp(prev3_wd, "FETCH") == 0 ||
strcasecmp(prev3_wd, "MOVE") == 0)
{ {
char *list_FROMTO[] = {"FROM", "TO", NULL}; char *list_FROMTO[] = {"FROM", "TO", NULL};
...@@ -630,14 +677,16 @@ psql_completion(char *text, int start, int end) ...@@ -630,14 +677,16 @@ psql_completion(char *text, int start, int end)
/* GRANT && REVOKE*/ /* GRANT && REVOKE*/
/* Complete GRANT/REVOKE with a list of privileges */ /* Complete GRANT/REVOKE with a list of privileges */
else if (strcasecmp(prev_wd, "GRANT") == 0 || strcasecmp(prev_wd, "REVOKE") == 0) else if (strcasecmp(prev_wd, "GRANT") == 0 ||
strcasecmp(prev_wd, "REVOKE") == 0)
{ {
char *list_privileg[] = {"SELECT", "INSERT", "UPDATE", "DELETE", "RULE", "REFERENCES", "TRIGGER", "CREATE", "TEMPORARY", "EXECUTE", "USAGE", "ALL", NULL}; char *list_privileg[] = {"SELECT", "INSERT", "UPDATE", "DELETE", "RULE", "REFERENCES", "TRIGGER", "CREATE", "TEMPORARY", "EXECUTE", "USAGE", "ALL", NULL};
COMPLETE_WITH_LIST(list_privileg); COMPLETE_WITH_LIST(list_privileg);
} }
/* Complete GRANT/REVOKE <sth> with "ON" */ /* Complete GRANT/REVOKE <sth> with "ON" */
else if (strcasecmp(prev2_wd, "GRANT") == 0 || strcasecmp(prev2_wd, "REVOKE") == 0) else if (strcasecmp(prev2_wd, "GRANT") == 0 ||
strcasecmp(prev2_wd, "REVOKE") == 0)
COMPLETE_WITH_CONST("ON"); COMPLETE_WITH_CONST("ON");
/* /*
...@@ -646,16 +695,19 @@ psql_completion(char *text, int start, int end) ...@@ -646,16 +695,19 @@ psql_completion(char *text, int start, int end)
* *
* XXX should also offer DATABASE, FUNCTION, LANGUAGE, SCHEMA here * XXX should also offer DATABASE, FUNCTION, LANGUAGE, SCHEMA here
*/ */
else if ((strcasecmp(prev3_wd, "GRANT") == 0 || strcasecmp(prev3_wd, "REVOKE") == 0) && else if ((strcasecmp(prev3_wd, "GRANT") == 0 ||
strcasecmp(prev3_wd, "REVOKE") == 0) &&
strcasecmp(prev_wd, "ON") == 0) strcasecmp(prev_wd, "ON") == 0)
COMPLETE_WITH_QUERY("SELECT relname FROM pg_catalog.pg_class " COMPLETE_WITH_QUERY("SELECT relname FROM pg_catalog.pg_class "
"WHERE relkind in ('r','i','S','v') AND " "WHERE relkind in ('r','i','S','v') AND "
"substr(relname,1,%d)='%s' AND pg_catalog.pg_table_is_visible(oid)"); "substr(relname,1,%d)='%s' AND pg_catalog.pg_table_is_visible(oid)");
/* Complete "GRANT * ON * " with "TO" */ /* Complete "GRANT * ON * " with "TO" */
else if (strcasecmp(prev4_wd, "GRANT") == 0 && strcasecmp(prev2_wd, "ON") == 0) else if (strcasecmp(prev4_wd, "GRANT") == 0 &&
strcasecmp(prev2_wd, "ON") == 0)
COMPLETE_WITH_CONST("TO"); COMPLETE_WITH_CONST("TO");
/* Complete "REVOKE * ON * " with "FROM" */ /* Complete "REVOKE * ON * " with "FROM" */
else if (strcasecmp(prev4_wd, "REVOKE") == 0 && strcasecmp(prev2_wd, "ON") == 0) else if (strcasecmp(prev4_wd, "REVOKE") == 0 &&
strcasecmp(prev2_wd, "ON") == 0)
COMPLETE_WITH_CONST("FROM"); COMPLETE_WITH_CONST("FROM");
/* /*
...@@ -668,24 +720,29 @@ psql_completion(char *text, int start, int end) ...@@ -668,24 +720,29 @@ psql_completion(char *text, int start, int end)
else if (strcasecmp(prev_wd, "INSERT") == 0) else if (strcasecmp(prev_wd, "INSERT") == 0)
COMPLETE_WITH_CONST("INTO"); COMPLETE_WITH_CONST("INTO");
/* Complete INSERT INTO with table names */ /* Complete INSERT INTO with table names */
else if (strcasecmp(prev2_wd, "INSERT") == 0 && strcasecmp(prev_wd, "INTO") == 0) else if (strcasecmp(prev2_wd, "INSERT") == 0 &&
strcasecmp(prev_wd, "INTO") == 0)
COMPLETE_WITH_QUERY(Query_for_list_of_tables); COMPLETE_WITH_QUERY(Query_for_list_of_tables);
/* Complete "INSERT INTO <table> (" with attribute names */ /* Complete "INSERT INTO <table> (" with attribute names */
else if (rl_line_buffer[start - 1] == '(' && strcasecmp(prev3_wd, "INSERT") == 0 && strcasecmp(prev2_wd, "INTO") == 0) else if (rl_line_buffer[start - 1] == '(' &&
strcasecmp(prev3_wd, "INSERT") == 0 &&
strcasecmp(prev2_wd, "INTO") == 0)
COMPLETE_WITH_ATTR(prev_wd); COMPLETE_WITH_ATTR(prev_wd);
/* /*
* Complete INSERT INTO <table> with "VALUES" or "SELECT" or "DEFAULT * Complete INSERT INTO <table> with "VALUES" or "SELECT" or "DEFAULT
* VALUES" * VALUES"
*/ */
else if (strcasecmp(prev3_wd, "INSERT") == 0 && strcasecmp(prev2_wd, "INTO") == 0) else if (strcasecmp(prev3_wd, "INSERT") == 0 &&
strcasecmp(prev2_wd, "INTO") == 0)
{ {
char *list_INSERT[] = {"DEFAULT VALUES", "SELECT", "VALUES", NULL}; char *list_INSERT[] = {"DEFAULT VALUES", "SELECT", "VALUES", NULL};
COMPLETE_WITH_LIST(list_INSERT); COMPLETE_WITH_LIST(list_INSERT);
} }
/* Complete INSERT INTO <table> (attribs) with "VALUES" or "SELECT" */ /* Complete INSERT INTO <table> (attribs) with "VALUES" or "SELECT" */
else if (strcasecmp(prev4_wd, "INSERT") == 0 && strcasecmp(prev3_wd, "INTO") == 0 && else if (strcasecmp(prev4_wd, "INSERT") == 0 &&
strcasecmp(prev3_wd, "INTO") == 0 &&
prev_wd[strlen(prev_wd) - 1] == ')') prev_wd[strlen(prev_wd) - 1] == ')')
{ {
char *list_INSERT[] = {"SELECT", "VALUES", NULL}; char *list_INSERT[] = {"SELECT", "VALUES", NULL};
...@@ -694,29 +751,36 @@ psql_completion(char *text, int start, int end) ...@@ -694,29 +751,36 @@ psql_completion(char *text, int start, int end)
} }
/* Insert an open parenthesis after "VALUES" */ /* Insert an open parenthesis after "VALUES" */
else if (strcasecmp(prev_wd, "VALUES") == 0 && strcasecmp(prev2_wd, "DEFAULT") != 0) else if (strcasecmp(prev_wd, "VALUES") == 0 &&
strcasecmp(prev2_wd, "DEFAULT") != 0)
COMPLETE_WITH_CONST("("); COMPLETE_WITH_CONST("(");
/* LOCK */ /* LOCK */
/* Complete LOCK [TABLE] with a list of tables */ /* Complete LOCK [TABLE] with a list of tables */
else if ((strcasecmp(prev_wd, "LOCK") == 0) || else if (strcasecmp(prev_wd, "LOCK") == 0 ||
(strcasecmp(prev_wd, "TABLE") == 0 && strcasecmp(prev2_wd, "LOCK"))) (strcasecmp(prev_wd, "TABLE") == 0 &&
strcasecmp(prev2_wd, "LOCK")))
COMPLETE_WITH_QUERY(Query_for_list_of_tables); COMPLETE_WITH_QUERY(Query_for_list_of_tables);
/* For the following, handle the case of a single table only for now */ /* For the following, handle the case of a single table only for now */
/* Complete LOCK [TABLE] <table> with "IN" */ /* Complete LOCK [TABLE] <table> with "IN" */
else if ((strcasecmp(prev2_wd, "LOCK") == 0 && strcasecmp(prev_wd, "TABLE")) || else if ((strcasecmp(prev2_wd, "LOCK") == 0 &&
(strcasecmp(prev2_wd, "TABLE") == 0 && strcasecmp(prev3_wd, "LOCK") == 0)) strcasecmp(prev_wd, "TABLE")) ||
(strcasecmp(prev2_wd, "TABLE") == 0 &&
strcasecmp(prev3_wd, "LOCK") == 0))
COMPLETE_WITH_CONST("IN"); COMPLETE_WITH_CONST("IN");
/* Complete LOCK [TABLE] <table> IN with a lock mode */ /* Complete LOCK [TABLE] <table> IN with a lock mode */
else if (strcasecmp(prev_wd, "IN") == 0 && else if (strcasecmp(prev_wd, "IN") == 0 &&
(strcasecmp(prev3_wd, "LOCK") == 0 || (strcasecmp(prev3_wd, "LOCK") == 0 ||
(strcasecmp(prev3_wd, "TABLE") == 0 && strcasecmp(prev3_wd, "LOCK")))) (strcasecmp(prev3_wd, "TABLE") == 0 &&
strcasecmp(prev3_wd, "LOCK"))))
{ {
char *lock_modes[] = {"ACCESS SHARE MODE", "ROW SHARE MODE", "ROW EXCLUSIVE MODE", char *lock_modes[] = {"ACCESS SHARE MODE",
"SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE", "SHARE ROW EXCLUSIVE MODE", "ROW SHARE MODE", "ROW EXCLUSIVE MODE",
"SHARE UPDATE EXCLUSIVE MODE", "SHARE MODE",
"SHARE ROW EXCLUSIVE MODE",
"EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE", NULL}; "EXCLUSIVE MODE", "ACCESS EXCLUSIVE MODE", NULL};
COMPLETE_WITH_LIST(lock_modes); COMPLETE_WITH_LIST(lock_modes);
...@@ -748,18 +812,21 @@ psql_completion(char *text, int start, int end) ...@@ -748,18 +812,21 @@ psql_completion(char *text, int start, int end)
/* SET, RESET, SHOW */ /* SET, RESET, SHOW */
/* Complete with a variable name */ /* Complete with a variable name */
else if ((strcasecmp(prev_wd, "SET") == 0 && strcasecmp(prev3_wd, "UPDATE") != 0) || else if ((strcasecmp(prev_wd, "SET") == 0 &&
strcasecmp(prev3_wd, "UPDATE") != 0) ||
strcasecmp(prev_wd, "RESET") == 0 || strcasecmp(prev_wd, "RESET") == 0 ||
strcasecmp(prev_wd, "SHOW") == 0) strcasecmp(prev_wd, "SHOW") == 0)
COMPLETE_WITH_LIST(pgsql_variables); COMPLETE_WITH_LIST(pgsql_variables);
/* Complete "SET TRANSACTION ISOLOLATION LEVEL" */ /* Complete "SET TRANSACTION ISOLOLATION LEVEL" */
else if (strcasecmp(prev2_wd, "SET") == 0 && strcasecmp(prev_wd, "TRANSACTION") == 0) else if (strcasecmp(prev2_wd, "SET") == 0 &&
strcasecmp(prev_wd, "TRANSACTION") == 0)
COMPLETE_WITH_CONST("ISOLATION"); COMPLETE_WITH_CONST("ISOLATION");
else if (strcasecmp(prev3_wd, "SET") == 0 && else if (strcasecmp(prev3_wd, "SET") == 0 &&
strcasecmp(prev2_wd, "TRANSACTION") == 0 && strcasecmp(prev2_wd, "TRANSACTION") == 0 &&
strcasecmp(prev_wd, "ISOLATION") == 0) strcasecmp(prev_wd, "ISOLATION") == 0)
COMPLETE_WITH_CONST("LEVEL"); COMPLETE_WITH_CONST("LEVEL");
else if ((strcasecmp(prev4_wd, "SET") == 0 || strcasecmp(prev4_wd, "AS") == 0) && else if ((strcasecmp(prev4_wd, "SET") == 0 ||
strcasecmp(prev4_wd, "AS") == 0) &&
strcasecmp(prev3_wd, "TRANSACTION") == 0 && strcasecmp(prev3_wd, "TRANSACTION") == 0 &&
strcasecmp(prev2_wd, "ISOLATION") == 0 && strcasecmp(prev2_wd, "ISOLATION") == 0 &&
strcasecmp(prev_wd, "LEVEL") == 0) strcasecmp(prev_wd, "LEVEL") == 0)
...@@ -774,14 +841,16 @@ psql_completion(char *text, int start, int end) ...@@ -774,14 +841,16 @@ psql_completion(char *text, int start, int end)
strcasecmp(prev_wd, "READ") == 0) strcasecmp(prev_wd, "READ") == 0)
COMPLETE_WITH_CONST("COMMITTED"); COMPLETE_WITH_CONST("COMMITTED");
/* Complete SET CONSTRAINTS <foo> with DEFERRED|IMMEDIATE */ /* Complete SET CONSTRAINTS <foo> with DEFERRED|IMMEDIATE */
else if (strcasecmp(prev3_wd, "SET") == 0 && strcasecmp(prev2_wd, "CONSTRAINTS") == 0) else if (strcasecmp(prev3_wd, "SET") == 0 &&
strcasecmp(prev2_wd, "CONSTRAINTS") == 0)
{ {
char *constraint_list[] = {"DEFERRED", "IMMEDIATE", NULL}; char *constraint_list[] = {"DEFERRED", "IMMEDIATE", NULL};
COMPLETE_WITH_LIST(constraint_list); COMPLETE_WITH_LIST(constraint_list);
} }
/* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */ /* Complete SET SESSION with AUTHORIZATION or CHARACTERISTICS... */
else if (strcasecmp(prev2_wd, "SET") == 0 && strcasecmp(prev_wd, "SESSION") == 0) else if (strcasecmp(prev2_wd, "SET") == 0 &&
strcasecmp(prev_wd, "SESSION") == 0)
{ {
char *my_list[] = {"AUTHORIZATION", char *my_list[] = {"AUTHORIZATION",
"CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL", "CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL",
...@@ -804,7 +873,8 @@ psql_completion(char *text, int start, int end) ...@@ -804,7 +873,8 @@ psql_completion(char *text, int start, int end)
{ {
if (strcasecmp(prev2_wd, "DateStyle") == 0) if (strcasecmp(prev2_wd, "DateStyle") == 0)
{ {
char *my_list[] = {"'ISO'", "'SQL'", "'Postgres'", "'European'", "'NonEuropean'", "'German'", "DEFAULT", NULL}; char *my_list[] = {"'ISO'", "'SQL'", "'Postgres'",
"'European'", "'NonEuropean'", "'German'", "DEFAULT", NULL};
COMPLETE_WITH_LIST(my_list); COMPLETE_WITH_LIST(my_list);
} }
...@@ -849,7 +919,9 @@ psql_completion(char *text, int start, int end) ...@@ -849,7 +919,9 @@ psql_completion(char *text, int start, int end)
/* VACUUM */ /* VACUUM */
else if (strcasecmp(prev_wd, "VACUUM") == 0) else if (strcasecmp(prev_wd, "VACUUM") == 0)
COMPLETE_WITH_QUERY("SELECT relname FROM pg_catalog.pg_class WHERE relkind='r' and substr(relname,1,%d)='%s' and pg_catalog.pg_table_is_visible(oid) UNION SELECT 'FULL'::name UNION SELECT 'ANALYZE'::name"); COMPLETE_WITH_QUERY("SELECT relname FROM pg_catalog.pg_class WHERE relkind='r' and substr(relname,1,%d)='%s' and pg_catalog.pg_table_is_visible(oid) UNION SELECT 'FULL'::name UNION SELECT 'ANALYZE'::name");
else if (strcasecmp(prev2_wd, "VACUUM") == 0 && (strcasecmp(prev_wd, "FULL") == 0 || strcasecmp(prev_wd, "ANALYZE") == 0)) else if (strcasecmp(prev2_wd, "VACUUM") == 0 &&
(strcasecmp(prev_wd, "FULL") == 0 ||
strcasecmp(prev_wd, "ANALYZE") == 0))
COMPLETE_WITH_QUERY(Query_for_list_of_tables); COMPLETE_WITH_QUERY(Query_for_list_of_tables);
/* WHERE */ /* WHERE */
...@@ -871,8 +943,8 @@ psql_completion(char *text, int start, int end) ...@@ -871,8 +943,8 @@ psql_completion(char *text, int start, int end)
COMPLETE_WITH_LIST(sql_commands); COMPLETE_WITH_LIST(sql_commands);
else if (strcmp(prev_wd, "\\pset") == 0) else if (strcmp(prev_wd, "\\pset") == 0)
{ {
char *my_list[] = {"format", "border", "expanded", "null", "fieldsep", char *my_list[] = {"format", "border", "expanded",
"tuples_only", "title", "tableattr", "pager", "null", "fieldsep", "tuples_only", "title", "tableattr", "pager",
"recordsep", NULL}; "recordsep", NULL};
COMPLETE_WITH_LIST(my_list); COMPLETE_WITH_LIST(my_list);
...@@ -1015,7 +1087,8 @@ complete_from_query(char *text, int state) ...@@ -1015,7 +1087,8 @@ complete_from_query(char *text, int state)
/* Find something that matches */ /* Find something that matches */
if (result && PQresultStatus(result) == PGRES_TUPLES_OK) if (result && PQresultStatus(result) == PGRES_TUPLES_OK)
while (list_index < PQntuples(result) && (item = PQgetvalue(result, list_index++, 0))) while (list_index < PQntuples(result) &&
(item = PQgetvalue(result, list_index++, 0)))
if (strncasecmp(text, item, string_length) == 0) if (strncasecmp(text, item, string_length) == 0)
return xstrdup(item); return xstrdup(item);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment