Skip to content

Commit 55e05d0

Browse files
committed
Merge branch 'policies'
Fixes #103
2 parents 089894a + b4d981d commit 55e05d0

File tree

9 files changed

+341
-2
lines changed

9 files changed

+341
-2
lines changed

src/cst/AlterAction.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ export type AlterTriggerAction =
163163
| AlterActionDependsOnExtension
164164
| AlterActionNoDependsOnExtension;
165165

166+
export type AlterPolicyAction = AlterActionRename;
167+
166168
export interface AlterActionRename extends BaseNode {
167169
type: "alter_action_rename";
168170
renameKw: Keyword<"RENAME"> | [Keyword<"RENAME">, Keyword<"TO" | "AS">];

src/cst/Dcl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export interface RevokeRoleStmt extends BaseNode {
266266
behaviorKw?: Keyword<"CASCADE" | "RESTRICT">;
267267
}
268268

269-
type Grantee = Identifier | FuncCall | GranteeGroup | GranteePublic;
269+
export type Grantee = Identifier | FuncCall | GranteeGroup | GranteePublic;
270270

271271
export interface GranteeGroup extends BaseNode {
272272
type: "grantee_group";

src/cst/Node.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export * from "./Merge";
2121
export * from "./dialects/Mysql";
2222
export * from "./dialects/Postgresql";
2323
export * from "./OtherClauses";
24+
export * from "./Policy";
2425
export * from "./PreparedStatements";
2526
export * from "./ProcClause";
2627
export * from "./Procedure";
@@ -60,6 +61,7 @@ import { AllInsertNodes } from "./Insert";
6061
import { AllMergeNodes } from "./Merge";
6162
import { AllMysqlNodes } from "./dialects/Mysql";
6263
import { AllOtherClauses } from "./OtherClauses";
64+
import { AllPolicyNodes } from "./Policy";
6365
import { AllPostgresqlNodes } from "./dialects/Postgresql";
6466
import { AllPreparedStatementNodes } from "./PreparedStatements";
6567
import { AllProcClauseNodes } from "./ProcClause";
@@ -99,6 +101,7 @@ export type Node =
99101
| AllMysqlNodes
100102
| AllOtherClauses
101103
| AllPostgresqlNodes
104+
| AllPolicyNodes
102105
| AllPreparedStatementNodes
103106
| AllProcClauseNodes
104107
| AllProceduralNodes

src/cst/Policy.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { AlterPolicyAction } from "./AlterAction";
2+
import { BaseNode, Keyword } from "./Base";
3+
import { Grantee } from "./Dcl";
4+
import { Identifier, EntityName, Expr, ParenExpr, ListExpr } from "./Expr";
5+
6+
export type AllPolicyNodes = AllPolicyStatements | CreatePolicyClause;
7+
8+
export type AllPolicyStatements =
9+
| CreatePolicyStmt
10+
| AlterPolicyStmt
11+
| DropPolicyStmt;
12+
13+
// CREATE POLICY name ON table
14+
export interface CreatePolicyStmt extends BaseNode {
15+
type: "create_policy_stmt";
16+
createPolicyKw: [Keyword<"CREATE">, Keyword<"POLICY">];
17+
name: Identifier;
18+
onKw: Keyword<"ON">;
19+
table: EntityName;
20+
clauses: CreatePolicyClause[];
21+
}
22+
23+
export type CreatePolicyClause =
24+
| PolicyPermissiveClause
25+
| PolicyRestrictiveClause
26+
| PolicyCommandClause
27+
| PolicyRolesClause
28+
| PolicyUsingClause
29+
| PolicyCheckClause;
30+
31+
export interface PolicyPermissiveClause extends BaseNode {
32+
type: "policy_permissive_clause";
33+
asKw: Keyword<"AS">;
34+
permissiveKw: Keyword<"PERMISSIVE">;
35+
}
36+
37+
export interface PolicyRestrictiveClause extends BaseNode {
38+
type: "policy_restrictive_clause";
39+
asKw: Keyword<"AS">;
40+
restrictiveKw: Keyword<"RESTRICTIVE">;
41+
}
42+
43+
export interface PolicyCommandClause extends BaseNode {
44+
type: "policy_command_clause";
45+
forKw: Keyword<"FOR">;
46+
commandKw: Keyword<"ALL" | "SELECT" | "INSERT" | "UPDATE" | "DELETE">;
47+
}
48+
49+
export interface PolicyRolesClause extends BaseNode {
50+
type: "policy_roles_clause";
51+
toKw: Keyword<"TO">;
52+
roles: ListExpr<Grantee>;
53+
}
54+
55+
export interface PolicyUsingClause extends BaseNode {
56+
type: "policy_using_clause";
57+
usingKw: Keyword<"USING">;
58+
expr: ParenExpr<Expr>;
59+
}
60+
61+
export interface PolicyCheckClause extends BaseNode {
62+
type: "policy_check_clause";
63+
withKw: Keyword<"WITH">;
64+
checkKw: Keyword<"CHECK">;
65+
expr: ParenExpr<Expr>;
66+
}
67+
68+
export interface AlterPolicyStmt extends BaseNode {
69+
type: "alter_policy_stmt";
70+
alterPolicyKw: [Keyword<"ALTER">, Keyword<"POLICY">];
71+
name: Identifier;
72+
onKw: Keyword<"ON">;
73+
table: EntityName;
74+
actions: (
75+
| AlterPolicyAction
76+
| PolicyRolesClause
77+
| PolicyUsingClause
78+
| PolicyCheckClause
79+
)[];
80+
}
81+
82+
export interface DropPolicyStmt extends BaseNode {
83+
type: "drop_policy_stmt";
84+
dropPolicyKw: [Keyword<"DROP">, Keyword<"POLICY">];
85+
ifExistsKw?: [Keyword<"IF">, Keyword<"EXISTS">];
86+
name: Identifier;
87+
onKw: Keyword<"ON">;
88+
table: EntityName;
89+
behaviorKw?: Keyword<"CASCADE" | "RESTRICT">;
90+
}

src/cst/Statement.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ import { TruncateStmt } from "./Truncate";
2525
import { UpdateStmt } from "./Update";
2626
import { AllPreparedStatements } from "./PreparedStatements";
2727
import { UnsupportedGrammarStmt } from "./UnsupportedGrammar";
28+
import { AllPolicyStatements } from "./Policy";
2829

2930
export type Statement =
3031
| AllAlterTableStatements
3132
| AllBigqueryStatements
3233
| AllDclStatements
3334
| AllFunctionStatements
3435
| AllIndexStatements
36+
| AllPolicyStatements
3537
| AllPreparedStatements
3638
| AllProceduralStatements
3739
| AllProcedureStatements

src/parser.pegjs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ ddl_statement_postgres
170170
/ drop_role_stmt
171171
/ set_role_stmt
172172
/ reset_role_stmt
173+
/ create_policy_stmt
174+
/ alter_policy_stmt
175+
/ drop_policy_stmt
173176

174177
dml_statement
175178
= compound_select_stmt
@@ -4636,6 +4639,103 @@ reset_role_stmt
46364639
return loc({ type: "reset_role_stmt", resetRoleKw: read(kw) });
46374640
}
46384641

4642+
/**
4643+
* ------------------------------------------------------------------------------------ *
4644+
* *
4645+
* CREATE/ALTER/DROP POLICY *
4646+
* *
4647+
* ------------------------------------------------------------------------------------ *
4648+
*/
4649+
create_policy_stmt
4650+
= kw:(CREATE __ POLICY __) name:(ident __) onKw:(ON __) table:entity_name
4651+
clauses:(__ create_policy_clause)* {
4652+
return loc({
4653+
type: "create_policy_stmt",
4654+
createPolicyKw: read(kw),
4655+
name: read(name),
4656+
onKw: read(onKw),
4657+
table,
4658+
clauses: clauses.map(read),
4659+
});
4660+
}
4661+
4662+
create_policy_clause
4663+
= policy_permissive_clause
4664+
/ policy_restrictive_clause
4665+
/ policy_command_clause
4666+
/ policy_roles_clause
4667+
/ policy_using_clause
4668+
/ policy_check_clause
4669+
4670+
policy_permissive_clause
4671+
= asKw:(AS __) permissiveKw:PERMISSIVE {
4672+
return loc({
4673+
type: "policy_permissive_clause",
4674+
asKw: read(asKw),
4675+
permissiveKw,
4676+
});
4677+
}
4678+
4679+
policy_restrictive_clause
4680+
= asKw:(AS __) restrictiveKw:RESTRICTIVE {
4681+
return loc({
4682+
type: "policy_restrictive_clause",
4683+
asKw: read(asKw),
4684+
restrictiveKw,
4685+
});
4686+
}
4687+
4688+
policy_command_clause
4689+
= kw:(FOR __) commandKw:(ALL / SELECT / INSERT / UPDATE / DELETE) {
4690+
return loc({ type: "policy_command_clause", forKw: read(kw), commandKw });
4691+
}
4692+
4693+
policy_roles_clause
4694+
= kw:(TO __) roles:list$grantee {
4695+
return loc({ type: "policy_roles_clause", toKw: read(kw), roles });
4696+
}
4697+
4698+
policy_using_clause
4699+
= kw:(USING __) expr:paren$expr {
4700+
return loc({ type: "policy_using_clause", usingKw: read(kw), expr });
4701+
}
4702+
4703+
policy_check_clause
4704+
= withKw:(WITH __) checkKw:(CHECK __) expr:paren$expr {
4705+
return loc({ type: "policy_check_clause", withKw: read(withKw), checkKw: read(checkKw), expr });
4706+
}
4707+
4708+
alter_policy_stmt
4709+
= kw:(ALTER __ POLICY __) name:(ident __) onKw:(ON __) table:entity_name actions:(__ alter_policy_action)+ {
4710+
return loc({
4711+
type: "alter_policy_stmt",
4712+
alterPolicyKw: read(kw),
4713+
name: read(name),
4714+
onKw: read(onKw),
4715+
table,
4716+
actions: actions.map(read),
4717+
});
4718+
}
4719+
4720+
alter_policy_action
4721+
= alter_action_rename
4722+
/ policy_roles_clause
4723+
/ policy_using_clause
4724+
/ policy_check_clause
4725+
4726+
drop_policy_stmt
4727+
= kw:(DROP __ POLICY __) ifExistsKw:(if_exists __)? name:(ident __) onKw:(ON __) table:entity_name behaviorKw:(__ (CASCADE / RESTRICT))? {
4728+
return loc({
4729+
type: "drop_policy_stmt",
4730+
dropPolicyKw: read(kw),
4731+
ifExistsKw: read(ifExistsKw),
4732+
name: read(name),
4733+
onKw: read(onKw),
4734+
table,
4735+
behaviorKw: read(behaviorKw),
4736+
});
4737+
}
4738+
46394739
/**
46404740
* ------------------------------------------------------------------------------------ *
46414741
* *
@@ -9010,6 +9110,7 @@ PARTITION = kw:"PARTITION"i !ident_part { return loc(createK
90109110
PASSWORD = kw:"PASSWORD"i !ident_part { return loc(createKeyword(kw)); }
90119111
PERCENT = kw:"PERCENT"i !ident_part { return loc(createKeyword(kw)); }
90129112
PERCENT_RANK = kw:"PERCENT_RANK"i !ident_part { return loc(createKeyword(kw)); }
9113+
PERMISSIVE = kw:"PERMISSIVE"i !ident_part { return loc(createKeyword(kw)); }
90139114
PERSIST = kw:"PERSIST"i !ident_part { return loc(createKeyword(kw)); }
90149115
PERSIST_ONLY = kw:"PERSIST_ONLY"i !ident_part { return loc(createKeyword(kw)); }
90159116
PIVOT = kw:"PIVOT"i !ident_part { return loc(createKeyword(kw)); }
@@ -9058,6 +9159,7 @@ RESPECT = kw:"RESPECT"i !ident_part { return loc(createK
90589159
RESTART = kw:"RESTART"i !ident_part { return loc(createKeyword(kw)); }
90599160
RESTRICT = kw:"RESTRICT"i !ident_part { return loc(createKeyword(kw)); }
90609161
RESTRICTED = kw:"RESTRICTED"i !ident_part { return loc(createKeyword(kw)); }
9162+
RESTRICTIVE = kw:"RESTRICTIVE"i !ident_part { return loc(createKeyword(kw)); }
90619163
RETURN = kw:"RETURN"i !ident_part { return loc(createKeyword(kw)); }
90629164
RETURNING = kw:"RETURNING"i !ident_part { return loc(createKeyword(kw)); }
90639165
RETURNS = kw:"RETURNS"i !ident_part { return loc(createKeyword(kw)); }

src/showNode/policy.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { show } from "../show";
2+
import { FullTransformMap } from "../cstTransformer";
3+
import { AllPolicyNodes } from "src/main";
4+
5+
export const policyMap: FullTransformMap<string, AllPolicyNodes> = {
6+
// CREATE POLICY
7+
create_policy_stmt: (node) =>
8+
show([node.createPolicyKw, node.name, node.onKw, node.table, node.clauses]),
9+
10+
// Policy clauses
11+
policy_permissive_clause: (node) => show([node.asKw, node.permissiveKw]),
12+
policy_restrictive_clause: (node) => show([node.asKw, node.restrictiveKw]),
13+
policy_command_clause: (node) => show([node.forKw, node.commandKw]),
14+
policy_roles_clause: (node) => show([node.toKw, node.roles]),
15+
policy_using_clause: (node) => show([node.usingKw, node.expr]),
16+
policy_check_clause: (node) => show([node.withKw, node.checkKw, node.expr]),
17+
18+
// ALTER POLICY
19+
alter_policy_stmt: (node) =>
20+
show([node.alterPolicyKw, node.name, node.onKw, node.table, node.actions]),
21+
22+
// DROP POLICY
23+
drop_policy_stmt: (node) =>
24+
show([
25+
node.dropPolicyKw,
26+
node.ifExistsKw,
27+
node.name,
28+
node.onKw,
29+
node.table,
30+
node.behaviorKw,
31+
]),
32+
};

src/showNode/transformMap.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { functionMap } from "./function";
1515
import { indexMap } from "./index";
1616
import { insertMap } from "./insert";
1717
import { mergeMap } from "./merge";
18+
import { policyMap } from "./policy";
1819
import { preparedStatementsMap } from "./prepared_statements";
1920
import { proceduralLanguageMap } from "./procedural_language";
2021
import { procedureMap } from "./procedure";
@@ -60,7 +61,7 @@ export const transformMap: FullTransformMap<string> = {
6061
...dropTableMap,
6162
...renameTableMap,
6263

63-
// CREATE/DROP/ALTER SCHEMA/VIEW/INDEX/TRIGGER/SEQUENCE/TYPE/DOMAIN/ROLE
64+
// CREATE/DROP/ALTER SCHEMA/VIEW/INDEX/TRIGGER/SEQUENCE/TYPE/DOMAIN/ROLE/POLICY
6465
...schemaMap,
6566
...viewMap,
6667
...indexMap,
@@ -69,6 +70,7 @@ export const transformMap: FullTransformMap<string> = {
6970
...typeMap,
7071
...domainMap,
7172
...roleMap,
73+
...policyMap,
7274

7375
// CREATE/DROP FUNCTION/PROCEDURE
7476
...functionMap,

0 commit comments

Comments
 (0)