Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
package net.sf.jsqlparser.statement;

import net.sf.jsqlparser.statement.alter.AlterPolicy;
import net.sf.jsqlparser.statement.drop.DropPolicy;
import net.sf.jsqlparser.statement.alter.AlterRelation;
import net.sf.jsqlparser.statement.alter.AlterTablespaceMove;
import net.sf.jsqlparser.statement.alter.database.AlterDatabase;
Expand Down Expand Up @@ -697,4 +699,20 @@ default <S> T visit(AlterTablespaceMove statement, S context) {
default void visit(AlterTablespaceMove statement) {
visit(statement, null);
}

default <S> T visit(AlterPolicy statement, S context) {
return null;
}

default void visit(AlterPolicy statement) {
visit(statement, null);
}

default <S> T visit(DropPolicy statement, S context) {
return null;
}

default void visit(DropPolicy statement) {
visit(statement, null);
}
}
83 changes: 83 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/alter/AlterPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*-
* #%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.alter;

import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.create.policy.PolicyOptions;

public class AlterPolicy implements Statement {
private String policyName;
private net.sf.jsqlparser.schema.Table table;
private String newName;
private PolicyOptions options = new PolicyOptions();


public String getPolicyName() {
return policyName;
}

public AlterPolicy setPolicyName(String policyName) {
this.policyName = policyName;
return this;
}

public net.sf.jsqlparser.schema.Table getTable() {
return table;
}

public AlterPolicy setTable(net.sf.jsqlparser.schema.Table table) {
this.table = table;
return this;
}


public String getNewName() {
return newName;
}

public void setNewName(String newName) {
this.newName = newName;
if (newName != null) {
options = new PolicyOptions();
}
}

public PolicyOptions getOptions() {
return options;
}

public void setOptions(PolicyOptions options) {
this.options = java.util.Objects.requireNonNull(options);
newName = null;
}

public StringBuilder appendTo(StringBuilder sql,
java.util.function.Consumer<net.sf.jsqlparser.expression.Expression> printer) {
sql.append("ALTER POLICY ").append(policyName).append(" ON ").append(table);
if (newName != null) {
sql.append(" RENAME TO ").append(newName);
} else {
options.appendTo(sql, printer);
}
return sql;
}

@Override
public String toString() {
StringBuilder b = new StringBuilder();
return appendTo(b, b::append).toString();
}

@Override
public <T, S> T accept(StatementVisitor<T> visitor, S context) {
return visitor.visit(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;

import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.List;

/**
Expand All @@ -30,9 +30,16 @@ public class CreatePolicy implements Statement {
private Table table;
private PolicyMode policyMode;
private PolicyCommand policyCommand;
private List<String> roles = new ArrayList<>();
private Expression usingExpression;
private Expression withCheckExpression;
private PolicyOptions options = new PolicyOptions();

public PolicyOptions getOptions() {
return options;
}

public CreatePolicy setOptions(PolicyOptions options) {
this.options = java.util.Objects.requireNonNull(options);
return this;
}

public String getPolicyName() {
return policyName;
Expand Down Expand Up @@ -118,34 +125,34 @@ public PolicyCommand getEffectivePolicyCommand() {
}

public List<String> getRoles() {
return roles;
return options.getRoles();
}

public CreatePolicy setRoles(List<String> roles) {
this.roles = roles;
options.setRoles(roles);
return this;
}

public CreatePolicy addRole(String role) {
this.roles.add(role);
options.getRoles().add(role);
return this;
}

public Expression getUsingExpression() {
return usingExpression;
return options.getUsingExpression();
}

public CreatePolicy setUsingExpression(Expression usingExpression) {
this.usingExpression = usingExpression;
options.setUsingExpression(usingExpression);
return this;
}

public Expression getWithCheckExpression() {
return withCheckExpression;
return options.getWithCheckExpression();
}

public CreatePolicy setWithCheckExpression(Expression withCheckExpression) {
this.withCheckExpression = withCheckExpression;
options.setWithCheckExpression(withCheckExpression);
return this;
}

Expand All @@ -154,39 +161,22 @@ 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());

public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> printer) {
builder.append("CREATE POLICY ").append(policyName).append(" ON ").append(table);
if (policyMode != null) {
builder.append(" AS ").append(policyMode);
}

if (policyCommand != null) {
builder.append(" FOR ").append(policyCommand);
}
options.appendTo(builder, printer);
return builder;
}

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();
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
return appendTo(builder, builder::append).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*-
* #%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 java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.Expression;

/** Roles and predicates shared by PostgreSQL CREATE and ALTER POLICY. */
public class PolicyOptions implements Serializable {
private List<String> roles = new ArrayList<>();
private Expression usingExpression;
private Expression withCheckExpression;

public List<String> getRoles() {
return roles;
}

public PolicyOptions setRoles(List<String> roles) {
this.roles = roles;
return this;
}

public Expression getUsingExpression() {
return usingExpression;
}

public PolicyOptions setUsingExpression(Expression usingExpression) {
this.usingExpression = usingExpression;
return this;
}

public Expression getWithCheckExpression() {
return withCheckExpression;
}

public PolicyOptions setWithCheckExpression(Expression withCheckExpression) {
this.withCheckExpression = withCheckExpression;
return this;
}

public void visitExpressions(Consumer<Expression> visitor) {
if (usingExpression != null) {
visitor.accept(usingExpression);
}
if (withCheckExpression != null) {
visitor.accept(withCheckExpression);
}
}

public void appendTo(StringBuilder sql, Consumer<Expression> printer) {
if (roles != null && !roles.isEmpty()) {
sql.append(" TO ").append(String.join(", ", roles));
}
if (usingExpression != null) {
sql.append(" USING (");
printer.accept(usingExpression);
sql.append(')');
}
if (withCheckExpression != null) {
sql.append(" WITH CHECK (");
printer.accept(withCheckExpression);
sql.append(')');
}
}
}
82 changes: 82 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/drop/DropPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*-
* #%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.drop;

import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;

public class DropPolicy implements Statement {
private String policyName;
private net.sf.jsqlparser.schema.Table table;
private boolean ifExists = false;
private Behavior behavior;

public String getPolicyName() {
return policyName;
}

public DropPolicy setPolicyName(String policyName) {
this.policyName = policyName;
return this;
}

public net.sf.jsqlparser.schema.Table getTable() {
return table;
}

public DropPolicy setTable(net.sf.jsqlparser.schema.Table table) {
this.table = table;
return this;
}

public boolean isIfExists() {
return ifExists;
}

public DropPolicy setIfExists(boolean ifExists) {
this.ifExists = ifExists;
return this;
}

public Behavior getBehavior() {
return behavior;
}

public DropPolicy setBehavior(Behavior behavior) {
this.behavior = behavior;
return this;
}

public enum Behavior {
CASCADE, RESTRICT
}

public StringBuilder appendTo(StringBuilder sql) {
sql.append("DROP POLICY ");
if (ifExists) {
sql.append("IF EXISTS ");
}
sql.append(policyName).append(" ON ").append(table);
if (behavior != null) {
sql.append(' ').append(behavior);
}
return sql;
}

@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}

@Override
public <T, S> T accept(StatementVisitor<T> visitor, S context) {
return visitor.visit(this, context);
}
}
Loading
Loading