Skip to content

Commit 05cf0ea

Browse files
committed
psql: Add more constraint completion
- ALTER DOMAIN ... DROP/RENAME/VALIDATE CONSTRAINT - ALTER TABLE ... RENAME/VALIDATE CONSTRAINT - COMMENT ON CONSTRAINT - SET CONSTRAINTS
1 parent bd9b4f1 commit 05cf0ea

File tree

1 file changed

+83
-5
lines changed

1 file changed

+83
-5
lines changed

src/bin/psql/tab-complete.c

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,21 @@ static const SchemaQuery Query_for_list_of_tables = {
354354
NULL
355355
};
356356

357+
static const SchemaQuery Query_for_list_of_constraints_with_schema = {
358+
/* catname */
359+
"pg_catalog.pg_constraint c",
360+
/* selcondition */
361+
"c.conrelid <> 0",
362+
/* viscondition */
363+
"true", /* there is no pg_constraint_is_visible */
364+
/* namespace */
365+
"c.connamespace",
366+
/* result */
367+
"pg_catalog.quote_ident(c.conname)",
368+
/* qualresult */
369+
NULL
370+
};
371+
357372
/* The bit masks for the following three functions come from
358373
* src/include/catalog/pg_trigger.h.
359374
*/
@@ -587,6 +602,28 @@ static const SchemaQuery Query_for_list_of_views = {
587602
" and pg_catalog.quote_ident(c1.relname)='%s'"\
588603
" and pg_catalog.pg_table_is_visible(c1.oid)"
589604

605+
#define Query_for_all_table_constraints \
606+
"SELECT pg_catalog.quote_ident(conname) "\
607+
" FROM pg_catalog.pg_constraint c "\
608+
" WHERE c.conrelid <> 0 "
609+
610+
/* the silly-looking length condition is just to eat up the current word */
611+
#define Query_for_constraint_of_type \
612+
"SELECT pg_catalog.quote_ident(conname) "\
613+
" FROM pg_catalog.pg_type t, pg_catalog.pg_constraint con "\
614+
" WHERE t.oid=contypid and (%d = pg_catalog.length('%s'))"\
615+
" and pg_catalog.quote_ident(t.typname)='%s'"\
616+
" and pg_catalog.pg_type_is_visible(t.oid)"
617+
618+
/* the silly-looking length condition is just to eat up the current word */
619+
#define Query_for_list_of_tables_for_constraint \
620+
"SELECT pg_catalog.quote_ident(relname) "\
621+
" FROM pg_catalog.pg_class"\
622+
" WHERE (%d = pg_catalog.length('%s'))"\
623+
" AND oid IN "\
624+
" (SELECT conrelid FROM pg_catalog.pg_constraint "\
625+
" WHERE pg_catalog.quote_ident(conname)='%s')"
626+
590627
/* the silly-looking length condition is just to eat up the current word */
591628
#define Query_for_list_of_tables_for_trigger \
592629
"SELECT pg_catalog.quote_ident(relname) "\
@@ -1147,6 +1184,17 @@ psql_completion(char *text, int start, int end)
11471184

11481185
COMPLETE_WITH_LIST(list_ALTERDOMAIN2);
11491186
}
1187+
/* ALTER DOMAIN <sth> DROP|RENAME|VALIDATE CONSTRAINT */
1188+
else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
1189+
pg_strcasecmp(prev4_wd, "DOMAIN") == 0 &&
1190+
(pg_strcasecmp(prev2_wd, "DROP") == 0 ||
1191+
pg_strcasecmp(prev2_wd, "RENAME") == 0 ||
1192+
pg_strcasecmp(prev2_wd, "VALIDATE") == 0) &&
1193+
pg_strcasecmp(prev_wd, "CONSTRAINT") == 0)
1194+
{
1195+
completion_info_charp = prev3_wd;
1196+
COMPLETE_WITH_QUERY(Query_for_constraint_of_type);
1197+
}
11501198
/* ALTER DOMAIN <sth> RENAME */
11511199
else if (pg_strcasecmp(prev4_wd, "ALTER") == 0 &&
11521200
pg_strcasecmp(prev3_wd, "DOMAIN") == 0 &&
@@ -1340,14 +1388,18 @@ psql_completion(char *text, int start, int end)
13401388

13411389
COMPLETE_WITH_LIST(list_TABLEDROP);
13421390
}
1343-
/* If we have TABLE <sth> DROP COLUMN, provide list of columns */
1344-
else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
1391+
/* If we have ALTER TABLE <sth> DROP COLUMN, provide list of columns */
1392+
else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
1393+
pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
13451394
pg_strcasecmp(prev2_wd, "DROP") == 0 &&
13461395
pg_strcasecmp(prev_wd, "COLUMN") == 0)
13471396
COMPLETE_WITH_ATTR(prev3_wd, "");
1348-
/* If we have TABLE <sth> DROP CONSTRAINT, provide list of constraints */
1349-
else if (pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
1350-
pg_strcasecmp(prev2_wd, "DROP") == 0 &&
1397+
/* If we have ALTER TABLE <sth> DROP|RENAME|VALIDATE CONSTRAINT, provide list of constraints */
1398+
else if (pg_strcasecmp(prev5_wd, "ALTER") == 0 &&
1399+
pg_strcasecmp(prev4_wd, "TABLE") == 0 &&
1400+
(pg_strcasecmp(prev2_wd, "DROP") == 0 ||
1401+
pg_strcasecmp(prev2_wd, "RENAME") == 0 ||
1402+
pg_strcasecmp(prev2_wd, "VALIDATE") == 0) &&
13511403
pg_strcasecmp(prev_wd, "CONSTRAINT") == 0)
13521404
{
13531405
completion_info_charp = prev3_wd;
@@ -1744,6 +1796,26 @@ psql_completion(char *text, int start, int end)
17441796

17451797
COMPLETE_WITH_LIST(list_TRANS2);
17461798
}
1799+
else if (pg_strcasecmp(prev3_wd, "COMMENT") == 0 &&
1800+
pg_strcasecmp(prev2_wd, "ON") == 0 &&
1801+
pg_strcasecmp(prev_wd, "CONSTRAINT") == 0)
1802+
{
1803+
COMPLETE_WITH_QUERY(Query_for_all_table_constraints);
1804+
}
1805+
else if (pg_strcasecmp(prev4_wd, "COMMENT") == 0 &&
1806+
pg_strcasecmp(prev3_wd, "ON") == 0 &&
1807+
pg_strcasecmp(prev2_wd, "CONSTRAINT") == 0)
1808+
{
1809+
COMPLETE_WITH_CONST("ON");
1810+
}
1811+
else if (pg_strcasecmp(prev5_wd, "COMMENT") == 0 &&
1812+
pg_strcasecmp(prev4_wd, "ON") == 0 &&
1813+
pg_strcasecmp(prev3_wd, "CONSTRAINT") == 0 &&
1814+
pg_strcasecmp(prev_wd, "ON") == 0)
1815+
{
1816+
completion_info_charp = prev2_wd;
1817+
COMPLETE_WITH_QUERY(Query_for_list_of_tables_for_constraint);
1818+
}
17471819
else if ((pg_strcasecmp(prev4_wd, "COMMENT") == 0 &&
17481820
pg_strcasecmp(prev3_wd, "ON") == 0) ||
17491821
(pg_strcasecmp(prev5_wd, "COMMENT") == 0 &&
@@ -2805,6 +2877,12 @@ psql_completion(char *text, int start, int end)
28052877

28062878
COMPLETE_WITH_LIST(my_list);
28072879
}
2880+
/* SET CONSTRAINTS */
2881+
else if (pg_strcasecmp(prev2_wd, "SET") == 0 &&
2882+
pg_strcasecmp(prev_wd, "CONSTRAINTS") == 0)
2883+
{
2884+
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_constraints_with_schema, "UNION SELECT 'ALL'");
2885+
}
28082886
/* Complete SET CONSTRAINTS <foo> with DEFERRED|IMMEDIATE */
28092887
else if (pg_strcasecmp(prev3_wd, "SET") == 0 &&
28102888
pg_strcasecmp(prev2_wd, "CONSTRAINTS") == 0)

0 commit comments

Comments
 (0)