2020/**
2121 * PostgreSQL CREATE POLICY statement for Row Level Security (RLS).
2222 *
23- * Syntax: CREATE POLICY name ON table_name [ FOR { ALL | SELECT | INSERT | UPDATE | DELETE } ] [ TO
24- * { role_name | PUBLIC | CURRENT_USER | SESSION_USER } [, ...] ] [ USING ( using_expression ) ] [
25- * WITH CHECK ( check_expression ) ]
23+ * Syntax: CREATE POLICY name ON table_name [ AS { PERMISSIVE | RESTRICTIVE } ] [ FOR { ALL | SELECT
24+ * | INSERT | UPDATE | DELETE } ] [ TO { role_name | PUBLIC | CURRENT_ROLE | CURRENT_USER |
25+ * SESSION_USER } [, ...] ] [ USING ( using_expression ) ] [ WITH CHECK ( check_expression ) ]
2626 */
2727public class CreatePolicy implements Statement {
2828
2929 private String policyName ;
3030 private Table table ;
31- private String command ; // ALL, SELECT, INSERT, UPDATE, DELETE
31+ private PolicyMode policyMode ;
32+ private PolicyCommand policyCommand ;
3233 private List <String > roles = new ArrayList <>();
3334 private Expression usingExpression ;
3435 private Expression withCheckExpression ;
@@ -51,15 +52,71 @@ public CreatePolicy setTable(Table table) {
5152 return this ;
5253 }
5354
55+ /**
56+ * Returns the explicitly specified policy mode, or {@code null} when the {@code AS} clause was
57+ * omitted.
58+ *
59+ * @return the explicitly specified policy mode
60+ */
61+ public PolicyMode getPolicyMode () {
62+ return policyMode ;
63+ }
64+
65+ public CreatePolicy setPolicyMode (PolicyMode policyMode ) {
66+ this .policyMode = policyMode ;
67+ return this ;
68+ }
69+
70+ /**
71+ * Returns the effective PostgreSQL policy mode, including the default for an omitted {@code AS}
72+ * clause.
73+ *
74+ * @return the explicit policy mode, or {@link PolicyMode#PERMISSIVE}
75+ */
76+ public PolicyMode getEffectivePolicyMode () {
77+ return policyMode != null ? policyMode : PolicyMode .PERMISSIVE ;
78+ }
79+
80+ /**
81+ * Returns the explicitly specified command as a string for backwards compatibility.
82+ *
83+ * @return the explicitly specified command, or {@code null} when the {@code FOR} clause was
84+ * omitted
85+ */
5486 public String getCommand () {
55- return command ;
87+ return policyCommand != null ? policyCommand . name () : null ;
5688 }
5789
5890 public CreatePolicy setCommand (String command ) {
59- this .command = command ;
91+ this .policyCommand = command != null ? PolicyCommand .from (command ) : null ;
92+ return this ;
93+ }
94+
95+ /**
96+ * Returns the explicitly specified command, or {@code null} when the {@code FOR} clause was
97+ * omitted.
98+ *
99+ * @return the explicitly specified command
100+ */
101+ public PolicyCommand getPolicyCommand () {
102+ return policyCommand ;
103+ }
104+
105+ public CreatePolicy setPolicyCommand (PolicyCommand policyCommand ) {
106+ this .policyCommand = policyCommand ;
60107 return this ;
61108 }
62109
110+ /**
111+ * Returns the effective PostgreSQL command, including the default for an omitted {@code FOR}
112+ * clause.
113+ *
114+ * @return the explicit command, or {@link PolicyCommand#ALL}
115+ */
116+ public PolicyCommand getEffectivePolicyCommand () {
117+ return policyCommand != null ? policyCommand : PolicyCommand .ALL ;
118+ }
119+
63120 public List <String > getRoles () {
64121 return roles ;
65122 }
@@ -104,8 +161,12 @@ public String toString() {
104161 builder .append (" ON " );
105162 builder .append (table .toString ());
106163
107- if (command != null ) {
108- builder .append (" FOR " ).append (command );
164+ if (policyMode != null ) {
165+ builder .append (" AS " ).append (policyMode );
166+ }
167+
168+ if (policyCommand != null ) {
169+ builder .append (" FOR " ).append (policyCommand );
109170 }
110171
111172 if (roles != null && !roles .isEmpty ()) {
0 commit comments