Skip to content

Commit 0de0e09

Browse files
authored
feat: reuse foreign-data options for PostgreSQL wrappers, servers and user mappings (#2720)
Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent 276b6d9 commit 0de0e09

17 files changed

Lines changed: 1073 additions & 2 deletions

File tree

‎src/main/java/net/sf/jsqlparser/parser/feature/Feature.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ public enum Feature {
580580
*/
581581
createSequence,
582582
/** Publication and subscription definitions. */
583+
createForeignDataWrapper, alterForeignDataWrapper, createServer, alterServer, createUserMapping, alterUserMapping,
584+
583585
createPublication, alterPublication, createSubscription, alterSubscription,
584586
/**
585587
* Structured type, domain and extension statements.

‎src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
*/
1010
package net.sf.jsqlparser.statement;
1111

12+
import net.sf.jsqlparser.statement.create.fdw.CreateForeignDataWrapper;
13+
import net.sf.jsqlparser.statement.alter.AlterForeignDataWrapper;
14+
import net.sf.jsqlparser.statement.create.server.CreateServer;
15+
import net.sf.jsqlparser.statement.alter.AlterServer;
16+
import net.sf.jsqlparser.statement.create.usermapping.CreateUserMapping;
17+
import net.sf.jsqlparser.statement.alter.AlterUserMapping;
1218
import net.sf.jsqlparser.statement.alter.AlterPolicy;
1319
import net.sf.jsqlparser.statement.drop.DropPolicy;
1420
import net.sf.jsqlparser.statement.create.statistics.CreateStatistics;
@@ -733,4 +739,52 @@ default <S> T visit(AlterStatistics statement, S context) {
733739
default void visit(AlterStatistics statement) {
734740
visit(statement, null);
735741
}
742+
743+
default <S> T visit(CreateForeignDataWrapper statement, S context) {
744+
return null;
745+
}
746+
747+
default void visit(CreateForeignDataWrapper statement) {
748+
visit(statement, null);
749+
}
750+
751+
default <S> T visit(AlterForeignDataWrapper statement, S context) {
752+
return null;
753+
}
754+
755+
default void visit(AlterForeignDataWrapper statement) {
756+
visit(statement, null);
757+
}
758+
759+
default <S> T visit(CreateServer statement, S context) {
760+
return null;
761+
}
762+
763+
default void visit(CreateServer statement) {
764+
visit(statement, null);
765+
}
766+
767+
default <S> T visit(AlterServer statement, S context) {
768+
return null;
769+
}
770+
771+
default void visit(AlterServer statement) {
772+
visit(statement, null);
773+
}
774+
775+
default <S> T visit(CreateUserMapping statement, S context) {
776+
return null;
777+
}
778+
779+
default void visit(CreateUserMapping statement) {
780+
visit(statement, null);
781+
}
782+
783+
default <S> T visit(AlterUserMapping statement, S context) {
784+
return null;
785+
}
786+
787+
default void visit(AlterUserMapping statement) {
788+
visit(statement, null);
789+
}
736790
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2026 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.alter;
11+
12+
import java.util.function.Consumer;
13+
import net.sf.jsqlparser.expression.Expression;
14+
import net.sf.jsqlparser.statement.StatementVisitor;
15+
import net.sf.jsqlparser.statement.foreign.ForeignDataStatement;
16+
import net.sf.jsqlparser.statement.create.fdw.ForeignDataWrapperFunctions;
17+
18+
public class AlterForeignDataWrapper extends ForeignDataStatement {
19+
public enum Action {
20+
OPTIONS, OWNER, RENAME
21+
}
22+
23+
private String name;
24+
private Action action = Action.OPTIONS;
25+
private String newName;
26+
private String owner;
27+
private ForeignDataWrapperFunctions functions = new ForeignDataWrapperFunctions();
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String value) {
34+
name = value;
35+
}
36+
37+
public Action getAction() {
38+
return action;
39+
}
40+
41+
public void setAction(Action value) {
42+
action = value;
43+
}
44+
45+
public String getNewName() {
46+
return newName;
47+
}
48+
49+
public void setNewName(String value) {
50+
newName = value;
51+
}
52+
53+
public String getOwner() {
54+
return owner;
55+
}
56+
57+
public void setOwner(String value) {
58+
owner = value;
59+
}
60+
61+
public ForeignDataWrapperFunctions getFunctions() {
62+
return functions;
63+
}
64+
65+
public void setFunctions(ForeignDataWrapperFunctions value) {
66+
functions = value;
67+
}
68+
69+
@Override
70+
public <T, S> T accept(StatementVisitor<T> visitor, S context) {
71+
return visitor.visit(this, context);
72+
}
73+
74+
@Override
75+
public StringBuilder appendTo(StringBuilder sql, Consumer<Expression> printer) {
76+
sql.append("ALTER FOREIGN DATA WRAPPER ").append(name);
77+
switch (action) {
78+
case RENAME:
79+
sql.append(" RENAME TO ").append(newName);
80+
break;
81+
case OWNER:
82+
sql.append(" OWNER TO ").append(owner);
83+
break;
84+
case OPTIONS:
85+
functions.appendTo(sql);
86+
appendOptionsTo(sql, printer);
87+
break;
88+
default:
89+
throw new IllegalStateException("Unknown foreign-data wrapper action: " + action);
90+
}
91+
return sql;
92+
}
93+
94+
@Override
95+
public void visitExpressions(Consumer<Expression> visitor) {
96+
if (action == Action.OPTIONS) {
97+
super.visitExpressions(visitor);
98+
}
99+
}
100+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2026 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.alter;
11+
12+
import java.util.function.Consumer;
13+
import net.sf.jsqlparser.expression.Expression;
14+
import net.sf.jsqlparser.statement.StatementVisitor;
15+
import net.sf.jsqlparser.statement.foreign.ForeignDataStatement;
16+
17+
public class AlterServer extends ForeignDataStatement {
18+
public enum Action {
19+
OPTIONS, OWNER, RENAME
20+
}
21+
22+
private String name;
23+
private Action action = Action.OPTIONS;
24+
private String newName;
25+
private String owner;
26+
private Expression version;
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String value) {
33+
name = value;
34+
}
35+
36+
public Action getAction() {
37+
return action;
38+
}
39+
40+
public void setAction(Action value) {
41+
action = value;
42+
}
43+
44+
public String getNewName() {
45+
return newName;
46+
}
47+
48+
public void setNewName(String value) {
49+
newName = value;
50+
}
51+
52+
public String getOwner() {
53+
return owner;
54+
}
55+
56+
public void setOwner(String value) {
57+
owner = value;
58+
}
59+
60+
public Expression getVersion() {
61+
return version;
62+
}
63+
64+
public void setVersion(Expression value) {
65+
version = value;
66+
}
67+
68+
@Override
69+
public <T, S> T accept(StatementVisitor<T> visitor, S context) {
70+
return visitor.visit(this, context);
71+
}
72+
73+
@Override
74+
public StringBuilder appendTo(StringBuilder sql, Consumer<Expression> printer) {
75+
sql.append("ALTER SERVER ").append(name);
76+
switch (action) {
77+
case RENAME:
78+
sql.append(" RENAME TO ").append(newName);
79+
break;
80+
case OWNER:
81+
sql.append(" OWNER TO ").append(owner);
82+
break;
83+
case OPTIONS:
84+
if (version != null) {
85+
sql.append(" VERSION ");
86+
printer.accept(version);
87+
}
88+
appendOptionsTo(sql, printer);
89+
break;
90+
default:
91+
throw new IllegalStateException("Unknown server action: " + action);
92+
}
93+
return sql;
94+
}
95+
96+
@Override
97+
public void visitExpressions(Consumer<Expression> visitor) {
98+
if (action != Action.OPTIONS) {
99+
return;
100+
}
101+
super.visitExpressions(visitor);
102+
visit(version, visitor);
103+
}
104+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2026 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.alter;
11+
12+
import java.util.function.Consumer;
13+
import net.sf.jsqlparser.expression.Expression;
14+
import net.sf.jsqlparser.statement.StatementVisitor;
15+
import net.sf.jsqlparser.statement.foreign.ForeignDataStatement;
16+
17+
public class AlterUserMapping extends ForeignDataStatement {
18+
private String user;
19+
private String server;
20+
21+
public String getUser() {
22+
return user;
23+
}
24+
25+
public void setUser(String value) {
26+
user = value;
27+
}
28+
29+
public String getServer() {
30+
return server;
31+
}
32+
33+
public void setServer(String value) {
34+
server = value;
35+
}
36+
37+
@Override
38+
public <T, S> T accept(StatementVisitor<T> visitor, S context) {
39+
return visitor.visit(this, context);
40+
}
41+
42+
@Override
43+
public StringBuilder appendTo(StringBuilder sql, Consumer<Expression> printer) {
44+
sql.append("ALTER USER MAPPING FOR ").append(user).append(" SERVER ").append(server);
45+
appendOptionsTo(sql, printer);
46+
return sql;
47+
}
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2026 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.create.fdw;
11+
12+
import java.util.function.Consumer;
13+
import net.sf.jsqlparser.expression.Expression;
14+
import net.sf.jsqlparser.statement.StatementVisitor;
15+
import net.sf.jsqlparser.statement.foreign.ForeignDataStatement;
16+
17+
public class CreateForeignDataWrapper extends ForeignDataStatement {
18+
private String name;
19+
private ForeignDataWrapperFunctions functions = new ForeignDataWrapperFunctions();
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String value) {
26+
name = value;
27+
}
28+
29+
public ForeignDataWrapperFunctions getFunctions() {
30+
return functions;
31+
}
32+
33+
public void setFunctions(ForeignDataWrapperFunctions value) {
34+
functions = value;
35+
}
36+
37+
@Override
38+
public <T, S> T accept(StatementVisitor<T> visitor, S context) {
39+
return visitor.visit(this, context);
40+
}
41+
42+
@Override
43+
public StringBuilder appendTo(StringBuilder sql, Consumer<Expression> printer) {
44+
sql.append("CREATE FOREIGN DATA WRAPPER ").append(name);
45+
functions.appendTo(sql);
46+
appendOptionsTo(sql, printer);
47+
return sql;
48+
}
49+
}

0 commit comments

Comments
 (0)