Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c951e90

Browse files
committed
Add tab completion for CREATE SCHEMA in psql
The following patterns are added for CREATE SCHEMA: - AUTHORIZATION, without a schema name or after a schema name. - Possible list of owner roles after AUTHORIZATION. - CREATE and GRANT within the supported set of commands. - Correct object types supported in an embedded CREATE SCHEMA command. While on it, this commit adjusts the completion done after CREATE UNLOGGED: - Addition of SEQUENCE. - Avoid suggesting MATERIALIZED VIEW in CREATE TABLE. Author: Dagfinn Ilmari Mannsåker Reviewed-by: Suraj Khamkar, Michael Paquier Discussion: https://postgr.es/m/[email protected]
1 parent 23d8624 commit c951e90

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

src/bin/psql/tab-complete.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,9 +1031,13 @@ static const SchemaQuery Query_for_trigger_of_table = {
10311031
" FROM pg_catalog.pg_roles "\
10321032
" WHERE rolname LIKE '%s'"
10331033

1034+
/* add these to Query_for_list_of_roles in OWNER contexts */
1035+
#define Keywords_for_list_of_owner_roles \
1036+
"CURRENT_ROLE", "CURRENT_USER", "SESSION_USER"
1037+
10341038
/* add these to Query_for_list_of_roles in GRANT contexts */
10351039
#define Keywords_for_list_of_grant_roles \
1036-
"PUBLIC", "CURRENT_ROLE", "CURRENT_USER", "SESSION_USER"
1040+
Keywords_for_list_of_owner_roles, "PUBLIC"
10371041

10381042
#define Query_for_all_table_constraints \
10391043
"SELECT conname "\
@@ -1785,8 +1789,15 @@ psql_completion(const char *text, int start, int end)
17851789
/* CREATE */
17861790
/* complete with something you can create */
17871791
else if (TailMatches("CREATE"))
1788-
matches = rl_completion_matches(text, create_command_generator);
1789-
1792+
{
1793+
/* only some object types can be created as part of CREATE SCHEMA */
1794+
if (HeadMatches("CREATE", "SCHEMA"))
1795+
COMPLETE_WITH("TABLE", "VIEW", "INDEX", "SEQUENCE", "TRIGGER",
1796+
/* for INDEX and TABLE/SEQUENCE, respectively */
1797+
"UNIQUE", "UNLOGGED");
1798+
else
1799+
matches = rl_completion_matches(text, create_command_generator);
1800+
}
17901801
/* complete with something you can create or replace */
17911802
else if (TailMatches("CREATE", "OR", "REPLACE"))
17921803
COMPLETE_WITH("FUNCTION", "PROCEDURE", "LANGUAGE", "RULE", "VIEW",
@@ -3154,6 +3165,20 @@ psql_completion(const char *text, int start, int end)
31543165
else if (TailMatches("AS", "ON", "SELECT|UPDATE|INSERT|DELETE", "TO"))
31553166
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables);
31563167

3168+
/* CREATE SCHEMA [ <name> ] [ AUTHORIZATION ] */
3169+
else if (Matches("CREATE", "SCHEMA"))
3170+
COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_schemas,
3171+
"AUTHORIZATION");
3172+
else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION") ||
3173+
Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION"))
3174+
COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3175+
Keywords_for_list_of_owner_roles);
3176+
else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION", MatchAny) ||
3177+
Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION", MatchAny))
3178+
COMPLETE_WITH("CREATE", "GRANT");
3179+
else if (Matches("CREATE", "SCHEMA", MatchAny))
3180+
COMPLETE_WITH("AUTHORIZATION", "CREATE", "GRANT");
3181+
31573182
/* CREATE SEQUENCE --- is allowed inside CREATE SCHEMA, so use TailMatches */
31583183
else if (TailMatches("CREATE", "SEQUENCE", MatchAny) ||
31593184
TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny))
@@ -3185,9 +3210,15 @@ psql_completion(const char *text, int start, int end)
31853210
/* Complete "CREATE TEMP/TEMPORARY" with the possible temp objects */
31863211
else if (TailMatches("CREATE", "TEMP|TEMPORARY"))
31873212
COMPLETE_WITH("SEQUENCE", "TABLE", "VIEW");
3188-
/* Complete "CREATE UNLOGGED" with TABLE or MATVIEW */
3213+
/* Complete "CREATE UNLOGGED" with TABLE, SEQUENCE or MATVIEW */
31893214
else if (TailMatches("CREATE", "UNLOGGED"))
3190-
COMPLETE_WITH("TABLE", "MATERIALIZED VIEW");
3215+
{
3216+
/* but not MATVIEW in CREATE SCHEMA */
3217+
if (HeadMatches("CREATE", "SCHEMA"))
3218+
COMPLETE_WITH("TABLE", "SEQUENCE");
3219+
else
3220+
COMPLETE_WITH("TABLE", "SEQUENCE", "MATERIALIZED VIEW");
3221+
}
31913222
/* Complete PARTITION BY with RANGE ( or LIST ( or ... */
31923223
else if (TailMatches("PARTITION", "BY"))
31933224
COMPLETE_WITH("RANGE (", "LIST (", "HASH (");
@@ -4263,9 +4294,7 @@ psql_completion(const char *text, int start, int end)
42634294
/* OWNER TO - complete with available roles */
42644295
else if (TailMatches("OWNER", "TO"))
42654296
COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
4266-
"CURRENT_ROLE",
4267-
"CURRENT_USER",
4268-
"SESSION_USER");
4297+
Keywords_for_list_of_owner_roles);
42694298

42704299
/* ORDER BY */
42714300
else if (TailMatches("FROM", MatchAny, "ORDER"))

0 commit comments

Comments
 (0)