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

Skip to content

Commit 4cba9c2

Browse files
committed
Add more tab completion for CREATE TABLE in psql
The following completion patterns are added: - CREATE TABLE <name> with '(', OF or PARTITION OF. - CREATE TABLE <name> OF with list of composite types. - CREATE TABLE name (...) with PARTITION OF, WITH, TABLESPACE, ON COMMIT (depending on the presence of a temporary table). - CREATE TABLE ON COMMIT with actions (only for temporary tables). Author: Dagfinn Ilmari Mannsåker Discussion: https://postgr.es/m/[email protected]
1 parent 1075dfd commit 4cba9c2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/bin/psql/tab-complete.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,18 @@ static const SchemaQuery Query_for_list_of_datatypes = {
344344
.qualresult = "pg_catalog.quote_ident(t.typname)",
345345
};
346346

347+
static const SchemaQuery Query_for_list_of_composite_datatypes = {
348+
.catname = "pg_catalog.pg_type t",
349+
/* selcondition --- only get composite types */
350+
.selcondition = "(SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
351+
" FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid) "
352+
"AND t.typname !~ '^_'",
353+
.viscondition = "pg_catalog.pg_type_is_visible(t.oid)",
354+
.namespace = "t.typnamespace",
355+
.result = "pg_catalog.format_type(t.oid, NULL)",
356+
.qualresult = "pg_catalog.quote_ident(t.typname)",
357+
};
358+
347359
static const SchemaQuery Query_for_list_of_domains = {
348360
.catname = "pg_catalog.pg_type t",
349361
.selcondition = "t.typtype = 'd'",
@@ -2412,6 +2424,24 @@ psql_completion(const char *text, int start, int end)
24122424
/* Limited completion support for partition bound specification */
24132425
else if (TailMatches("PARTITION", "OF", MatchAny))
24142426
COMPLETE_WITH("FOR VALUES", "DEFAULT");
2427+
/* Complete CREATE TABLE <name> with '(', OF or PARTITION OF */
2428+
else if (TailMatches("CREATE", "TABLE", MatchAny) ||
2429+
TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny))
2430+
COMPLETE_WITH("(", "OF", "PARTITION OF");
2431+
/* Complete CREATE TABLE <name> OF with list of composite types */
2432+
else if (TailMatches("CREATE", "TABLE", MatchAny, "OF") ||
2433+
TailMatches("CREATE", "TEMP|TEMPORARY|UNLOGGED", "TABLE", MatchAny, "OF"))
2434+
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_composite_datatypes, NULL);
2435+
/* Complete CREATE TABLE name (...) with supported options */
2436+
else if (TailMatches("CREATE", "TABLE", MatchAny, "(*)") ||
2437+
TailMatches("CREATE", "UNLOGGED", "TABLE", MatchAny, "(*)"))
2438+
COMPLETE_WITH("INHERITS (", "PARTITION BY", "TABLESPACE", "WITH (");
2439+
else if (TailMatches("CREATE", "TEMP|TEMPORARY", "TABLE", MatchAny, "(*)"))
2440+
COMPLETE_WITH("INHERITS (", "ON COMMIT", "PARTITION BY",
2441+
"TABLESPACE", "WITH (");
2442+
/* Complete CREATE TABLE ON COMMIT with actions */
2443+
else if (TailMatches("CREATE", "TEMP|TEMPORARY", "TABLE", MatchAny, "(*)", "ON", "COMMIT"))
2444+
COMPLETE_WITH("DELETE ROWS", "DROP", "PRESERVE ROWS");
24152445

24162446
/* CREATE TABLESPACE */
24172447
else if (Matches("CREATE", "TABLESPACE", MatchAny))

0 commit comments

Comments
 (0)