Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9878e29

Browse files
committed
Improved tab completion for views with triggers.
Allow INSERT INTO, UPDATE, and DELETE FROM to be completed with either the name of a table (as before) or the name of a view with an appropriate INSTEAD OF rule. Along the way, allow CREATE TRIGGER to be completed with INSTEAD OF, as well as BEFORE and AFTER. David Fetter, reviewed by Itagaki Takahiro
1 parent f9224c8 commit 9878e29

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

src/bin/psql/tab-complete.c

+55-4
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,57 @@ static const SchemaQuery Query_for_list_of_tables = {
303303
NULL
304304
};
305305

306+
/* The bit masks for the following three functions come from
307+
* src/include/catalog/pg_trigger.h.
308+
*/
309+
static const SchemaQuery Query_for_list_of_insertables = {
310+
/* catname */
311+
"pg_catalog.pg_class c",
312+
/* selcondition */
313+
"(c.relkind = 'r' OR (c.relkind = 'v' AND c.relhastriggers AND EXISTS "
314+
"(SELECT 1 FROM pg_catalog.pg_trigger t WHERE t.tgrelid = c.oid AND t.tgtype & (1 << 2) <> 0)))",
315+
/* viscondition */
316+
"pg_catalog.pg_table_is_visible(c.oid)",
317+
/* namespace */
318+
"c.relnamespace",
319+
/* result */
320+
"pg_catalog.quote_ident(c.relname)",
321+
/* qualresult */
322+
NULL
323+
};
324+
325+
static const SchemaQuery Query_for_list_of_deletables = {
326+
/* catname */
327+
"pg_catalog.pg_class c",
328+
/* selcondition */
329+
"(c.relkind = 'r' OR (c.relkind = 'v' AND c.relhastriggers AND EXISTS "
330+
"(SELECT 1 FROM pg_catalog.pg_trigger t WHERE t.tgrelid = c.oid AND t.tgtype & (1 << 3) <> 0)))",
331+
/* viscondition */
332+
"pg_catalog.pg_table_is_visible(c.oid)",
333+
/* namespace */
334+
"c.relnamespace",
335+
/* result */
336+
"pg_catalog.quote_ident(c.relname)",
337+
/* qualresult */
338+
NULL
339+
};
340+
341+
static const SchemaQuery Query_for_list_of_updatables = {
342+
/* catname */
343+
"pg_catalog.pg_class c",
344+
/* selcondition */
345+
"(c.relkind = 'r' OR (c.relkind = 'v' AND c.relhastriggers AND EXISTS "
346+
"(SELECT 1 FROM pg_catalog.pg_trigger t WHERE t.tgrelid = c.oid AND t.tgtype & (1 << 4) <> 0)))",
347+
/* viscondition */
348+
"pg_catalog.pg_table_is_visible(c.oid)",
349+
/* namespace */
350+
"c.relnamespace",
351+
/* result */
352+
"pg_catalog.quote_ident(c.relname)",
353+
/* qualresult */
354+
NULL
355+
};
356+
306357
static const SchemaQuery Query_for_list_of_tisv = {
307358
/* catname */
308359
"pg_catalog.pg_class c",
@@ -1655,7 +1706,7 @@ psql_completion(char *text, int start, int end)
16551706
pg_strcasecmp(prev2_wd, "TRIGGER") == 0)
16561707
{
16571708
static const char *const list_CREATETRIGGER[] =
1658-
{"BEFORE", "AFTER", NULL};
1709+
{"BEFORE", "AFTER", "INSTEAD OF", NULL};
16591710

16601711
COMPLETE_WITH_LIST(list_CREATETRIGGER);
16611712
}
@@ -1778,7 +1829,7 @@ psql_completion(char *text, int start, int end)
17781829
/* Complete DELETE FROM with a list of tables */
17791830
else if (pg_strcasecmp(prev2_wd, "DELETE") == 0 &&
17801831
pg_strcasecmp(prev_wd, "FROM") == 0)
1781-
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
1832+
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_deletables, NULL);
17821833
/* Complete DELETE FROM <table> */
17831834
else if (pg_strcasecmp(prev3_wd, "DELETE") == 0 &&
17841835
pg_strcasecmp(prev2_wd, "FROM") == 0)
@@ -2066,7 +2117,7 @@ psql_completion(char *text, int start, int end)
20662117
/* Complete INSERT INTO with table names */
20672118
else if (pg_strcasecmp(prev2_wd, "INSERT") == 0 &&
20682119
pg_strcasecmp(prev_wd, "INTO") == 0)
2069-
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
2120+
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_insertables, NULL);
20702121
/* Complete "INSERT INTO <table> (" with attribute names */
20712122
else if (pg_strcasecmp(prev4_wd, "INSERT") == 0 &&
20722123
pg_strcasecmp(prev3_wd, "INTO") == 0 &&
@@ -2423,7 +2474,7 @@ psql_completion(char *text, int start, int end)
24232474
/* UPDATE */
24242475
/* If prev. word is UPDATE suggest a list of tables */
24252476
else if (pg_strcasecmp(prev_wd, "UPDATE") == 0)
2426-
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
2477+
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_updatables, NULL);
24272478
/* Complete UPDATE <table> with "SET" */
24282479
else if (pg_strcasecmp(prev2_wd, "UPDATE") == 0)
24292480
COMPLETE_WITH_CONST("SET");

0 commit comments

Comments
 (0)