forked from JSQLParser/JSqlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatePolicy.java
More file actions
131 lines (107 loc) · 3.4 KB
/
CreatePolicy.java
File metadata and controls
131 lines (107 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2025 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.create.policy;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import java.util.ArrayList;
import java.util.List;
/**
* PostgreSQL CREATE POLICY statement for Row Level Security (RLS).
*
* Syntax: CREATE POLICY name ON table_name [ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ] [ TO
* { role_name | PUBLIC | CURRENT_USER | SESSION_USER } [, ...] ] [ USING ( using_expression ) ] [
* WITH CHECK ( check_expression ) ]
*/
public class CreatePolicy implements Statement {
private String policyName;
private Table table;
private String command; // ALL, SELECT, INSERT, UPDATE, DELETE
private List<String> roles = new ArrayList<>();
private Expression usingExpression;
private Expression withCheckExpression;
public String getPolicyName() {
return policyName;
}
public CreatePolicy setPolicyName(String policyName) {
this.policyName = policyName;
return this;
}
public Table getTable() {
return table;
}
public CreatePolicy setTable(Table table) {
this.table = table;
return this;
}
public String getCommand() {
return command;
}
public CreatePolicy setCommand(String command) {
this.command = command;
return this;
}
public List<String> getRoles() {
return roles;
}
public CreatePolicy setRoles(List<String> roles) {
this.roles = roles;
return this;
}
public CreatePolicy addRole(String role) {
this.roles.add(role);
return this;
}
public Expression getUsingExpression() {
return usingExpression;
}
public CreatePolicy setUsingExpression(Expression usingExpression) {
this.usingExpression = usingExpression;
return this;
}
public Expression getWithCheckExpression() {
return withCheckExpression;
}
public CreatePolicy setWithCheckExpression(Expression withCheckExpression) {
this.withCheckExpression = withCheckExpression;
return this;
}
@Override
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statementVisitor.visit(this, context);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("CREATE POLICY ");
builder.append(policyName);
builder.append(" ON ");
builder.append(table.toString());
if (command != null) {
builder.append(" FOR ").append(command);
}
if (roles != null && !roles.isEmpty()) {
builder.append(" TO ");
for (int i = 0; i < roles.size(); i++) {
if (i > 0) {
builder.append(", ");
}
builder.append(roles.get(i));
}
}
if (usingExpression != null) {
builder.append(" USING (").append(usingExpression.toString()).append(")");
}
if (withCheckExpression != null) {
builder.append(" WITH CHECK (").append(withCheckExpression.toString()).append(")");
}
return builder.toString();
}
}