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

Commit a332b36

Browse files
committed
Grammar object type refactoring
Unify the grammar of COMMENT, DROP, and SECURITY LABEL further. They all effectively just take an object address for later processing, so we can make the grammar more generalized. Some extra checking about which object types are supported can be done later in the statement execution. Discussion: https://www.postgresql.org/message-id/flat/163c00a5-f634-ca52-fc7c-0e53deda8735%402ndquadrant.com
1 parent e78900a commit a332b36

File tree

2 files changed

+122
-114
lines changed

2 files changed

+122
-114
lines changed

src/backend/commands/seclabel.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,75 @@ typedef struct
3333

3434
static List *label_provider_list = NIL;
3535

36+
static bool
37+
SecLabelSupportsObjectType(ObjectType objtype)
38+
{
39+
switch (objtype)
40+
{
41+
case OBJECT_AGGREGATE:
42+
case OBJECT_COLUMN:
43+
case OBJECT_DATABASE:
44+
case OBJECT_DOMAIN:
45+
case OBJECT_EVENT_TRIGGER:
46+
case OBJECT_FOREIGN_TABLE:
47+
case OBJECT_FUNCTION:
48+
case OBJECT_LANGUAGE:
49+
case OBJECT_LARGEOBJECT:
50+
case OBJECT_MATVIEW:
51+
case OBJECT_PROCEDURE:
52+
case OBJECT_PUBLICATION:
53+
case OBJECT_ROLE:
54+
case OBJECT_ROUTINE:
55+
case OBJECT_SCHEMA:
56+
case OBJECT_SEQUENCE:
57+
case OBJECT_SUBSCRIPTION:
58+
case OBJECT_TABLE:
59+
case OBJECT_TABLESPACE:
60+
case OBJECT_TYPE:
61+
case OBJECT_VIEW:
62+
return true;
63+
64+
case OBJECT_ACCESS_METHOD:
65+
case OBJECT_AMOP:
66+
case OBJECT_AMPROC:
67+
case OBJECT_ATTRIBUTE:
68+
case OBJECT_CAST:
69+
case OBJECT_COLLATION:
70+
case OBJECT_CONVERSION:
71+
case OBJECT_DEFAULT:
72+
case OBJECT_DEFACL:
73+
case OBJECT_DOMCONSTRAINT:
74+
case OBJECT_EXTENSION:
75+
case OBJECT_FDW:
76+
case OBJECT_FOREIGN_SERVER:
77+
case OBJECT_INDEX:
78+
case OBJECT_OPCLASS:
79+
case OBJECT_OPERATOR:
80+
case OBJECT_OPFAMILY:
81+
case OBJECT_POLICY:
82+
case OBJECT_PUBLICATION_REL:
83+
case OBJECT_RULE:
84+
case OBJECT_STATISTIC_EXT:
85+
case OBJECT_TABCONSTRAINT:
86+
case OBJECT_TRANSFORM:
87+
case OBJECT_TRIGGER:
88+
case OBJECT_TSCONFIGURATION:
89+
case OBJECT_TSDICTIONARY:
90+
case OBJECT_TSPARSER:
91+
case OBJECT_TSTEMPLATE:
92+
case OBJECT_USER_MAPPING:
93+
return false;
94+
95+
/*
96+
* There's intentionally no default: case here; we want the
97+
* compiler to warn if a new ObjectType hasn't been handled above.
98+
*/
99+
}
100+
101+
/* Shouldn't get here, but if we do, say "no support" */
102+
return false;
103+
}
104+
36105
/*
37106
* ExecSecLabelStmt --
38107
*
@@ -83,6 +152,11 @@ ExecSecLabelStmt(SecLabelStmt *stmt)
83152
stmt->provider)));
84153
}
85154

155+
if (!SecLabelSupportsObjectType(stmt->objtype))
156+
ereport(ERROR,
157+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
158+
errmsg("security labels are not supported for this type of object")));
159+
86160
/*
87161
* Translate the parser representation which identifies this object into
88162
* an ObjectAddress. get_object_address() will throw an error if the

src/backend/parser/gram.y

Lines changed: 48 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,8 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
463463
%type <boolean> copy_from opt_program
464464

465465
%type <ival> opt_column event cursor_options opt_hold opt_set_data
466-
%type <objtype> drop_type_any_name drop_type_name drop_type_name_on_any_name
467-
comment_type_any_name comment_type_name
468-
security_label_type_any_name security_label_type_name
466+
%type <objtype> object_type_any_name object_type_name object_type_name_on_any_name
467+
drop_type_name
469468

470469
%type <node> fetch_args select_limit_value
471470
offset_clause select_offset_value
@@ -6190,7 +6189,7 @@ ReassignOwnedStmt:
61906189
*
61916190
*****************************************************************************/
61926191

6193-
DropStmt: DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
6192+
DropStmt: DROP object_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
61946193
{
61956194
DropStmt *n = makeNode(DropStmt);
61966195
n->removeType = $2;
@@ -6200,7 +6199,7 @@ DropStmt: DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
62006199
n->concurrent = false;
62016200
$$ = (Node *)n;
62026201
}
6203-
| DROP drop_type_any_name any_name_list opt_drop_behavior
6202+
| DROP object_type_any_name any_name_list opt_drop_behavior
62046203
{
62056204
DropStmt *n = makeNode(DropStmt);
62066205
n->removeType = $2;
@@ -6230,7 +6229,7 @@ DropStmt: DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
62306229
n->concurrent = false;
62316230
$$ = (Node *)n;
62326231
}
6233-
| DROP drop_type_name_on_any_name name ON any_name opt_drop_behavior
6232+
| DROP object_type_name_on_any_name name ON any_name opt_drop_behavior
62346233
{
62356234
DropStmt *n = makeNode(DropStmt);
62366235
n->removeType = $2;
@@ -6240,7 +6239,7 @@ DropStmt: DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
62406239
n->concurrent = false;
62416240
$$ = (Node *) n;
62426241
}
6243-
| DROP drop_type_name_on_any_name IF_P EXISTS name ON any_name opt_drop_behavior
6242+
| DROP object_type_name_on_any_name IF_P EXISTS name ON any_name opt_drop_behavior
62446243
{
62456244
DropStmt *n = makeNode(DropStmt);
62466245
n->removeType = $2;
@@ -6312,8 +6311,8 @@ DropStmt: DROP drop_type_any_name IF_P EXISTS any_name_list opt_drop_behavior
63126311
}
63136312
;
63146313

6315-
/* object types taking any_name_list */
6316-
drop_type_any_name:
6314+
/* object types taking any_name/any_name_list */
6315+
object_type_any_name:
63176316
TABLE { $$ = OBJECT_TABLE; }
63186317
| SEQUENCE { $$ = OBJECT_SEQUENCE; }
63196318
| VIEW { $$ = OBJECT_VIEW; }
@@ -6329,7 +6328,20 @@ drop_type_any_name:
63296328
| TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
63306329
;
63316330

6332-
/* object types taking name_list */
6331+
/*
6332+
* object types taking name/name_list
6333+
*
6334+
* DROP handles some of them separately
6335+
*/
6336+
6337+
object_type_name:
6338+
drop_type_name { $$ = $1; }
6339+
| DATABASE { $$ = OBJECT_DATABASE; }
6340+
| ROLE { $$ = OBJECT_ROLE; }
6341+
| SUBSCRIPTION { $$ = OBJECT_SUBSCRIPTION; }
6342+
| TABLESPACE { $$ = OBJECT_TABLESPACE; }
6343+
;
6344+
63336345
drop_type_name:
63346346
ACCESS METHOD { $$ = OBJECT_ACCESS_METHOD; }
63356347
| EVENT TRIGGER { $$ = OBJECT_EVENT_TRIGGER; }
@@ -6342,7 +6354,7 @@ drop_type_name:
63426354
;
63436355

63446356
/* object types attached to a table */
6345-
drop_type_name_on_any_name:
6357+
object_type_name_on_any_name:
63466358
POLICY { $$ = OBJECT_POLICY; }
63476359
| RULE { $$ = OBJECT_RULE; }
63486360
| TRIGGER { $$ = OBJECT_TRIGGER; }
@@ -6394,44 +6406,28 @@ opt_restart_seqs:
63946406

63956407
/*****************************************************************************
63966408
*
6397-
* The COMMENT ON statement can take different forms based upon the type of
6398-
* the object associated with the comment. The form of the statement is:
6399-
*
6400-
* COMMENT ON [ [ ACCESS METHOD | CONVERSION | COLLATION |
6401-
* DATABASE | DOMAIN |
6402-
* EXTENSION | EVENT TRIGGER | FOREIGN DATA WRAPPER |
6403-
* FOREIGN TABLE | INDEX | [PROCEDURAL] LANGUAGE |
6404-
* MATERIALIZED VIEW | POLICY | ROLE | SCHEMA | SEQUENCE |
6405-
* SERVER | STATISTICS | TABLE | TABLESPACE |
6406-
* TEXT SEARCH CONFIGURATION | TEXT SEARCH DICTIONARY |
6407-
* TEXT SEARCH PARSER | TEXT SEARCH TEMPLATE | TYPE |
6408-
* VIEW] <objname> |
6409-
* AGGREGATE <aggname> (arg1, ...) |
6410-
* CAST (<src type> AS <dst type>) |
6411-
* COLUMN <relname>.<colname> |
6412-
* CONSTRAINT <constraintname> ON <relname> |
6413-
* CONSTRAINT <constraintname> ON DOMAIN <domainname> |
6414-
* FUNCTION <funcname> (arg1, arg2, ...) |
6415-
* LARGE OBJECT <oid> |
6416-
* OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
6417-
* OPERATOR CLASS <name> USING <access-method> |
6418-
* OPERATOR FAMILY <name> USING <access-method> |
6419-
* RULE <rulename> ON <relname> |
6420-
* TRIGGER <triggername> ON <relname> ]
6421-
* IS { 'text' | NULL }
6409+
* COMMENT ON <object> IS <text>
64226410
*
64236411
*****************************************************************************/
64246412

64256413
CommentStmt:
6426-
COMMENT ON comment_type_any_name any_name IS comment_text
6414+
COMMENT ON object_type_any_name any_name IS comment_text
64276415
{
64286416
CommentStmt *n = makeNode(CommentStmt);
64296417
n->objtype = $3;
64306418
n->object = (Node *) $4;
64316419
n->comment = $6;
64326420
$$ = (Node *) n;
64336421
}
6434-
| COMMENT ON comment_type_name name IS comment_text
6422+
| COMMENT ON COLUMN any_name IS comment_text
6423+
{
6424+
CommentStmt *n = makeNode(CommentStmt);
6425+
n->objtype = OBJECT_COLUMN;
6426+
n->object = (Node *) $4;
6427+
n->comment = $6;
6428+
$$ = (Node *) n;
6429+
}
6430+
| COMMENT ON object_type_name name IS comment_text
64356431
{
64366432
CommentStmt *n = makeNode(CommentStmt);
64376433
n->objtype = $3;
@@ -6500,10 +6496,10 @@ CommentStmt:
65006496
n->comment = $9;
65016497
$$ = (Node *) n;
65026498
}
6503-
| COMMENT ON POLICY name ON any_name IS comment_text
6499+
| COMMENT ON object_type_name_on_any_name name ON any_name IS comment_text
65046500
{
65056501
CommentStmt *n = makeNode(CommentStmt);
6506-
n->objtype = OBJECT_POLICY;
6502+
n->objtype = $3;
65076503
n->object = (Node *) lappend($6, makeString($4));
65086504
n->comment = $8;
65096505
$$ = (Node *) n;
@@ -6524,14 +6520,6 @@ CommentStmt:
65246520
n->comment = $6;
65256521
$$ = (Node *) n;
65266522
}
6527-
| COMMENT ON RULE name ON any_name IS comment_text
6528-
{
6529-
CommentStmt *n = makeNode(CommentStmt);
6530-
n->objtype = OBJECT_RULE;
6531-
n->object = (Node *) lappend($6, makeString($4));
6532-
n->comment = $8;
6533-
$$ = (Node *) n;
6534-
}
65356523
| COMMENT ON TRANSFORM FOR Typename LANGUAGE name IS comment_text
65366524
{
65376525
CommentStmt *n = makeNode(CommentStmt);
@@ -6540,14 +6528,6 @@ CommentStmt:
65406528
n->comment = $9;
65416529
$$ = (Node *) n;
65426530
}
6543-
| COMMENT ON TRIGGER name ON any_name IS comment_text
6544-
{
6545-
CommentStmt *n = makeNode(CommentStmt);
6546-
n->objtype = OBJECT_TRIGGER;
6547-
n->object = (Node *) lappend($6, makeString($4));
6548-
n->comment = $8;
6549-
$$ = (Node *) n;
6550-
}
65516531
| COMMENT ON OPERATOR CLASS any_name USING name IS comment_text
65526532
{
65536533
CommentStmt *n = makeNode(CommentStmt);
@@ -6582,40 +6562,6 @@ CommentStmt:
65826562
}
65836563
;
65846564

6585-
/* object types taking any_name */
6586-
comment_type_any_name:
6587-
COLUMN { $$ = OBJECT_COLUMN; }
6588-
| INDEX { $$ = OBJECT_INDEX; }
6589-
| SEQUENCE { $$ = OBJECT_SEQUENCE; }
6590-
| STATISTICS { $$ = OBJECT_STATISTIC_EXT; }
6591-
| TABLE { $$ = OBJECT_TABLE; }
6592-
| VIEW { $$ = OBJECT_VIEW; }
6593-
| MATERIALIZED VIEW { $$ = OBJECT_MATVIEW; }
6594-
| COLLATION { $$ = OBJECT_COLLATION; }
6595-
| CONVERSION_P { $$ = OBJECT_CONVERSION; }
6596-
| FOREIGN TABLE { $$ = OBJECT_FOREIGN_TABLE; }
6597-
| TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
6598-
| TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
6599-
| TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
6600-
| TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
6601-
;
6602-
6603-
/* object types taking name */
6604-
comment_type_name:
6605-
ACCESS METHOD { $$ = OBJECT_ACCESS_METHOD; }
6606-
| DATABASE { $$ = OBJECT_DATABASE; }
6607-
| EVENT TRIGGER { $$ = OBJECT_EVENT_TRIGGER; }
6608-
| EXTENSION { $$ = OBJECT_EXTENSION; }
6609-
| FOREIGN DATA_P WRAPPER { $$ = OBJECT_FDW; }
6610-
| opt_procedural LANGUAGE { $$ = OBJECT_LANGUAGE; }
6611-
| PUBLICATION { $$ = OBJECT_PUBLICATION; }
6612-
| ROLE { $$ = OBJECT_ROLE; }
6613-
| SCHEMA { $$ = OBJECT_SCHEMA; }
6614-
| SERVER { $$ = OBJECT_FOREIGN_SERVER; }
6615-
| SUBSCRIPTION { $$ = OBJECT_SUBSCRIPTION; }
6616-
| TABLESPACE { $$ = OBJECT_TABLESPACE; }
6617-
;
6618-
66196565
comment_text:
66206566
Sconst { $$ = $1; }
66216567
| NULL_P { $$ = NULL; }
@@ -6632,7 +6578,7 @@ comment_text:
66326578
*****************************************************************************/
66336579

66346580
SecLabelStmt:
6635-
SECURITY LABEL opt_provider ON security_label_type_any_name any_name
6581+
SECURITY LABEL opt_provider ON object_type_any_name any_name
66366582
IS security_label
66376583
{
66386584
SecLabelStmt *n = makeNode(SecLabelStmt);
@@ -6642,7 +6588,17 @@ SecLabelStmt:
66426588
n->label = $8;
66436589
$$ = (Node *) n;
66446590
}
6645-
| SECURITY LABEL opt_provider ON security_label_type_name name
6591+
| SECURITY LABEL opt_provider ON COLUMN any_name
6592+
IS security_label
6593+
{
6594+
SecLabelStmt *n = makeNode(SecLabelStmt);
6595+
n->provider = $3;
6596+
n->objtype = OBJECT_COLUMN;
6597+
n->object = (Node *) $6;
6598+
n->label = $8;
6599+
$$ = (Node *) n;
6600+
}
6601+
| SECURITY LABEL opt_provider ON object_type_name name
66466602
IS security_label
66476603
{
66486604
SecLabelStmt *n = makeNode(SecLabelStmt);
@@ -6728,28 +6684,6 @@ opt_provider: FOR NonReservedWord_or_Sconst { $$ = $2; }
67286684
| /* empty */ { $$ = NULL; }
67296685
;
67306686

6731-
/* object types taking any_name */
6732-
security_label_type_any_name:
6733-
COLUMN { $$ = OBJECT_COLUMN; }
6734-
| FOREIGN TABLE { $$ = OBJECT_FOREIGN_TABLE; }
6735-
| SEQUENCE { $$ = OBJECT_SEQUENCE; }
6736-
| TABLE { $$ = OBJECT_TABLE; }
6737-
| VIEW { $$ = OBJECT_VIEW; }
6738-
| MATERIALIZED VIEW { $$ = OBJECT_MATVIEW; }
6739-
;
6740-
6741-
/* object types taking name */
6742-
security_label_type_name:
6743-
DATABASE { $$ = OBJECT_DATABASE; }
6744-
| EVENT TRIGGER { $$ = OBJECT_EVENT_TRIGGER; }
6745-
| opt_procedural LANGUAGE { $$ = OBJECT_LANGUAGE; }
6746-
| PUBLICATION { $$ = OBJECT_PUBLICATION; }
6747-
| ROLE { $$ = OBJECT_ROLE; }
6748-
| SCHEMA { $$ = OBJECT_SCHEMA; }
6749-
| SUBSCRIPTION { $$ = OBJECT_SUBSCRIPTION; }
6750-
| TABLESPACE { $$ = OBJECT_TABLESPACE; }
6751-
;
6752-
67536687
security_label: Sconst { $$ = $1; }
67546688
| NULL_P { $$ = NULL; }
67556689
;

0 commit comments

Comments
 (0)