Skip to content

Commit 40ed289

Browse files
authored
refactor: consolidate shared ALTER enums (#2746)
Replace the server and foreign-data wrapper Action enums with ForeignObjectAlterAction. Remove the TriggerState compatibility enum and accessors in favor of EnableState, and document the API migration. Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent 3befd9d commit 40ed289

9 files changed

Lines changed: 155 additions & 37 deletions

File tree

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterForeignDataWrapper.java‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616
import net.sf.jsqlparser.statement.create.fdw.ForeignDataWrapperFunctions;
1717

1818
public class AlterForeignDataWrapper extends ForeignDataStatement {
19-
public enum Action {
20-
OPTIONS, OWNER, RENAME
21-
}
22-
2319
private String name;
24-
private Action action = Action.OPTIONS;
20+
private ForeignObjectAlterAction action = ForeignObjectAlterAction.OPTIONS;
2521
private String newName;
2622
private String owner;
2723
private ForeignDataWrapperFunctions functions = new ForeignDataWrapperFunctions();
@@ -34,11 +30,11 @@ public void setName(String value) {
3430
name = value;
3531
}
3632

37-
public Action getAction() {
33+
public ForeignObjectAlterAction getAction() {
3834
return action;
3935
}
4036

41-
public void setAction(Action value) {
37+
public void setAction(ForeignObjectAlterAction value) {
4238
action = value;
4339
}
4440

@@ -93,7 +89,7 @@ public StringBuilder appendTo(StringBuilder sql, Consumer<Expression> printer) {
9389

9490
@Override
9591
public void visitExpressions(Consumer<Expression> visitor) {
96-
if (action == Action.OPTIONS) {
92+
if (action == ForeignObjectAlterAction.OPTIONS) {
9793
super.visitExpressions(visitor);
9894
}
9995
}

‎src/main/java/net/sf/jsqlparser/statement/alter/AlterServer.java‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
import net.sf.jsqlparser.statement.foreign.ForeignDataStatement;
1616

1717
public class AlterServer extends ForeignDataStatement {
18-
public enum Action {
19-
OPTIONS, OWNER, RENAME
20-
}
21-
2218
private String name;
23-
private Action action = Action.OPTIONS;
19+
private ForeignObjectAlterAction action = ForeignObjectAlterAction.OPTIONS;
2420
private String newName;
2521
private String owner;
2622
private Expression version;
@@ -33,11 +29,11 @@ public void setName(String value) {
3329
name = value;
3430
}
3531

36-
public Action getAction() {
32+
public ForeignObjectAlterAction getAction() {
3733
return action;
3834
}
3935

40-
public void setAction(Action value) {
36+
public void setAction(ForeignObjectAlterAction value) {
4137
action = value;
4238
}
4339

@@ -95,7 +91,7 @@ public StringBuilder appendTo(StringBuilder sql, Consumer<Expression> printer) {
9591

9692
@Override
9793
public void visitExpressions(Consumer<Expression> visitor) {
98-
if (action != Action.OPTIONS) {
94+
if (action != ForeignObjectAlterAction.OPTIONS) {
9995
return;
10096
}
10197
super.visitExpressions(visitor);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
/** Shared operations for ALTER SERVER and ALTER FOREIGN DATA WRAPPER. */
13+
public enum ForeignObjectAlterAction {
14+
/** Changes options and the object's server version or wrapper functions, when supplied. */
15+
OPTIONS, OWNER, RENAME
16+
}

‎src/main/java/net/sf/jsqlparser/statement/alter/RelationAlterAction.java‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ public enum EnableState {
6363
ENABLE, DISABLE, ENABLE_ALWAYS, ENABLE_REPLICA
6464
}
6565

66-
/** Compatibility names for existing trigger clients. */
67-
public enum TriggerState {
68-
ENABLE, DISABLE, ENABLE_ALWAYS, ENABLE_REPLICA
69-
}
7066
public enum TriggerTarget {
7167
NAME, ALL, USER
7268
}
@@ -80,14 +76,6 @@ public void setEnableState(EnableState state) {
8076
enableState = state;
8177
}
8278

83-
public TriggerState getTriggerState() {
84-
return enableState == null ? null : TriggerState.valueOf(enableState.name());
85-
}
86-
87-
public void setTriggerState(TriggerState state) {
88-
enableState = state == null ? null : EnableState.valueOf(state.name());
89-
}
90-
9179
public TriggerTarget getTriggerTarget() {
9280
return triggerTarget;
9381
}

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21441,9 +21441,9 @@ AlterForeignDataWrapper AlterForeignDataWrapper():
2144121441
<K_FOREIGN> <K_DATA> ContextualKeyword("WRAPPER") name=RelObjectName() { statement.setName(name); }
2144221442
(
2144321443
<K_RENAME> <K_TO> name=RelObjectName()
21444-
{ statement.setAction(AlterForeignDataWrapper.Action.RENAME); statement.setNewName(name); }
21444+
{ statement.setAction(ForeignObjectAlterAction.RENAME); statement.setNewName(name); }
2144521445
| LOOKAHEAD({ isKeywordAhead("OWNER") }) ContextualKeyword("OWNER") <K_TO> name=RelObjectName()
21446-
{ statement.setAction(AlterForeignDataWrapper.Action.OWNER); statement.setOwner(name); }
21446+
{ statement.setAction(ForeignObjectAlterAction.OWNER); statement.setOwner(name); }
2144721447
| functions=ForeignDataWrapperFunctions() { statement.setFunctions(functions); }
2144821448
[ LOOKAHEAD({ isKeywordAhead("OPTIONS") }) options=ForeignDataOptions(true) { statement.setOptions(options); } ]
2144921449
{ requireDdlSyntax(functions.isHandlerSpecified() || functions.isValidatorSpecified()
@@ -21513,9 +21513,9 @@ AlterServer AlterServer():
2151321513
ContextualKeyword("SERVER") name=RelObjectName() { statement.setName(name); }
2151421514
(
2151521515
<K_RENAME> <K_TO> name=RelObjectName()
21516-
{ statement.setAction(AlterServer.Action.RENAME); statement.setNewName(name); }
21516+
{ statement.setAction(ForeignObjectAlterAction.RENAME); statement.setNewName(name); }
2151721517
| LOOKAHEAD({ isKeywordAhead("OWNER") }) ContextualKeyword("OWNER") <K_TO> name=RelObjectName()
21518-
{ statement.setAction(AlterServer.Action.OWNER); statement.setOwner(name); }
21518+
{ statement.setAction(ForeignObjectAlterAction.OWNER); statement.setOwner(name); }
2151921519
| [ <K_VERSION> version=ForeignServerVersion() { statement.setVersion(version); } ]
2152021520
[ LOOKAHEAD({ isKeywordAhead("OPTIONS") }) options=ForeignDataOptions(true) { statement.setOptions(options); } ]
2152121521
{ requireDdlSyntax(statement.getVersion() != null || statement.getOptions() != null,

‎src/site/sphinx/usage.rst‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,51 @@ References: `CREATE ROLE <https://www.postgresql.org/docs/18/sql-createrole.html
13401340
`ALTER DEFAULT PRIVILEGES <https://www.postgresql.org/docs/18/sql-alterdefaultprivileges.html>`_,
13411341
`CREATE TRIGGER <https://www.postgresql.org/docs/18/sql-createtrigger.html>`_.
13421342

1343+
Migrating shared ALTER enums
1344+
----------------------------
1345+
1346+
``AlterServer.Action`` and ``AlterForeignDataWrapper.Action`` have been replaced
1347+
by ``ForeignObjectAlterAction`` in ``net.sf.jsqlparser.statement.alter``. Both
1348+
statements' ``getAction()`` and ``setAction()`` now use this enum, with the same
1349+
``OPTIONS``, ``OWNER`` and ``RENAME`` constants. Update imports and recompile code
1350+
using these APIs; the removed nested enums are not compatibility aliases.
1351+
1352+
.. code-block:: java
1353+
1354+
AlterServer server = (AlterServer) CCJSqlParserUtil.parse(
1355+
"ALTER SERVER s VERSION NULL");
1356+
server.setAction(ForeignObjectAlterAction.OWNER);
1357+
server.setOwner("CURRENT_USER");
1358+
// ALTER SERVER s OWNER TO CURRENT_USER
1359+
1360+
AlterForeignDataWrapper wrapper = (AlterForeignDataWrapper) CCJSqlParserUtil.parse(
1361+
"ALTER FOREIGN DATA WRAPPER w NO HANDLER");
1362+
wrapper.setAction(ForeignObjectAlterAction.OWNER);
1363+
wrapper.setOwner("CURRENT_USER");
1364+
// ALTER FOREIGN DATA WRAPPER w OWNER TO CURRENT_USER
1365+
1366+
``OPTIONS`` remains the initial action. It includes server ``VERSION`` changes
1367+
and wrapper handler/validator changes as well as foreign-data options. These
1368+
payloads remain on their respective statement classes. Switching to ``OWNER`` or
1369+
``RENAME`` leaves the inactive payload available for switching back to ``OPTIONS``.
1370+
1371+
``RelationAlterAction.TriggerState``, ``getTriggerState()`` and
1372+
``setTriggerState()`` have been removed. Use the existing
1373+
``RelationAlterAction.EnableState``, ``getEnableState()`` and ``setEnableState()``
1374+
for both trigger and rewrite-rule actions:
1375+
1376+
.. code-block:: java
1377+
1378+
Alter alter = (Alter) CCJSqlParserUtil.parse(
1379+
"ALTER TABLE t ENABLE REPLICA TRIGGER trg",
1380+
parser -> parser.withDialect(Dialect.POSTGRESQL));
1381+
RelationAlterAction trigger = (RelationAlterAction) alter.getAlterExpressions().get(0);
1382+
trigger.setEnableState(RelationAlterAction.EnableState.DISABLE);
1383+
// ALTER TABLE t DISABLE TRIGGER trg
1384+
1385+
The action's ``Kind`` still distinguishes ``TRIGGER_STATE`` from ``RULE_STATE``.
1386+
Trigger targets also retain their ``NAME``, ``ALL`` and ``USER`` distinctions.
1387+
13431388
Oracle anonymous blocks
13441389
-----------------------
13451390

‎src/test/java/net/sf/jsqlparser/statement/alter/PostgreSqlRelationAlterTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ void objectBoundariesAreEnforced(String sql) {
193193
void triggerTargetsAndTypeNamesAreMutableWithoutInventingTables() throws JSQLParserException {
194194
Alter statement = (Alter) parse("ALTER TABLE t ENABLE REPLICA TRIGGER trg");
195195
RelationAlterAction action = (RelationAlterAction) statement.getAlterExpressions().get(0);
196-
assertEquals(RelationAlterAction.TriggerState.ENABLE_REPLICA, action.getTriggerState());
197-
action.setTriggerState(RelationAlterAction.TriggerState.DISABLE);
196+
assertEquals(RelationAlterAction.EnableState.ENABLE_REPLICA, action.getEnableState());
197+
action.setEnableState(RelationAlterAction.EnableState.DISABLE);
198198
action.setTriggerTarget(RelationAlterAction.TriggerTarget.USER);
199199
assertEquals("ALTER TABLE t DISABLE TRIGGER USER", statement.toString());
200200
assertRoundTrip(statement);

‎src/test/java/net/sf/jsqlparser/statement/alter/PostgreSqlRuleStateTest.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,20 @@ void ruleAndTriggerActionsShareStates(String state) throws JSQLParserException {
4343
p -> p.withDialect(Dialect.POSTGRESQL));
4444
RelationAlterAction triggerAction =
4545
(RelationAlterAction) trigger.getAlterExpressions().get(0);
46-
assertEquals(state.replace(' ', '_'), triggerAction.getTriggerState().name());
47-
triggerAction.setTriggerState(RelationAlterAction.TriggerState.ENABLE_ALWAYS);
46+
assertEquals(state.replace(' ', '_'), triggerAction.getEnableState().name());
47+
triggerAction.setEnableState(null);
48+
assertNull(triggerAction.getEnableState());
49+
assertEquals(RelationAlterAction.Kind.TRIGGER_STATE, triggerAction.getKind());
50+
assertEquals(RelationAlterAction.TriggerTarget.NAME, triggerAction.getTriggerTarget());
51+
assertEquals("trg", triggerAction.getValue());
52+
triggerAction.setEnableState(RelationAlterAction.EnableState.ENABLE_ALWAYS);
4853
assertEquals(RelationAlterAction.EnableState.ENABLE_ALWAYS, triggerAction.getEnableState());
54+
StringBuilder triggerSql = new StringBuilder();
55+
trigger.accept(new StatementDeParser(triggerSql));
56+
assertEquals("ALTER TABLE t ENABLE ALWAYS TRIGGER trg", triggerSql.toString());
57+
assertEquals(trigger.toString(), triggerSql.toString());
58+
assertEquals(triggerSql.toString(), CCJSqlParserUtil.parse(triggerSql.toString(),
59+
p -> p.withDialect(Dialect.POSTGRESQL)).toString());
4960
assertEquals(2, CCJSqlParserUtil.parseStatements(rule + "; SELECT 1").size());
5061
}
5162

‎src/test/java/net/sf/jsqlparser/statement/create/PostgreSqlForeignDataObjectsTest.java‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import net.sf.jsqlparser.statement.alter.AlterForeignDataWrapper;
2323
import net.sf.jsqlparser.statement.alter.AlterServer;
2424
import net.sf.jsqlparser.statement.alter.AlterUserMapping;
25+
import net.sf.jsqlparser.statement.alter.ForeignObjectAlterAction;
2526
import net.sf.jsqlparser.statement.create.fdw.CreateForeignDataWrapper;
2627
import net.sf.jsqlparser.statement.create.server.CreateServer;
2728
import net.sf.jsqlparser.statement.create.table.ForeignDataOption;
@@ -102,14 +103,79 @@ void serverLiteralsAndOptionsAreVisitedAndMutable() throws JSQLParserException {
102103
roundTrip(create);
103104
AlterServer alter = (AlterServer) CCJSqlParserUtil.parse("ALTER SERVER s VERSION NULL");
104105
assertInstanceOf(NullValue.class, alter.getVersion());
105-
alter.setAction(AlterServer.Action.RENAME);
106+
alter.setAction(ForeignObjectAlterAction.RENAME);
106107
alter.setNewName("new_server");
107108
visited.clear();
108109
alter.visitExpressions(visited::add);
109110
assertTrue(visited.isEmpty());
110111
assertEquals("ALTER SERVER s RENAME TO new_server", alter.toString());
111112
}
112113

114+
@Test
115+
void sharedActionsKeepObjectSpecificOptionsInactiveAndRestorable() throws JSQLParserException {
116+
AlterServer server = (AlterServer) CCJSqlParserUtil.parse(
117+
"ALTER SERVER s VERSION NULL OPTIONS(SET host 'remote')");
118+
AlterForeignDataWrapper wrapper = (AlterForeignDataWrapper) CCJSqlParserUtil.parse(
119+
"ALTER FOREIGN DATA WRAPPER w NO HANDLER VALIDATOR public.validator "
120+
+ "OPTIONS(SET x 'y')");
121+
assertEquals(ForeignObjectAlterAction.OPTIONS, server.getAction());
122+
assertSame(server.getAction(), wrapper.getAction());
123+
String serverOptions = server.toString();
124+
String wrapperOptions = wrapper.toString();
125+
126+
server.setOwner("CURRENT_ROLE");
127+
wrapper.setOwner("CURRENT_USER");
128+
server.setAction(ForeignObjectAlterAction.OWNER);
129+
wrapper.setAction(ForeignObjectAlterAction.OWNER);
130+
assertEquals("ALTER SERVER s OWNER TO CURRENT_ROLE", server.toString());
131+
assertEquals("ALTER FOREIGN DATA WRAPPER w OWNER TO CURRENT_USER", wrapper.toString());
132+
roundTrip(server);
133+
roundTrip(wrapper);
134+
135+
server.setNewName("\"new server\"");
136+
wrapper.setNewName("\"new wrapper\"");
137+
server.setAction(ForeignObjectAlterAction.RENAME);
138+
wrapper.setAction(ForeignObjectAlterAction.RENAME);
139+
assertEquals("ALTER SERVER s RENAME TO \"new server\"", server.toString());
140+
assertEquals("ALTER FOREIGN DATA WRAPPER w RENAME TO \"new wrapper\"", wrapper.toString());
141+
roundTrip(server);
142+
roundTrip(wrapper);
143+
144+
List<Expression> visited = new ArrayList<>();
145+
server.visitExpressions(visited::add);
146+
wrapper.visitExpressions(visited::add);
147+
assertTrue(visited.isEmpty());
148+
server.setAction(null);
149+
wrapper.setAction(null);
150+
assertNull(server.getAction());
151+
assertNull(wrapper.getAction());
152+
server.visitExpressions(visited::add);
153+
wrapper.visitExpressions(visited::add);
154+
assertTrue(visited.isEmpty());
155+
156+
server.setAction(ForeignObjectAlterAction.OPTIONS);
157+
wrapper.setAction(ForeignObjectAlterAction.OPTIONS);
158+
assertEquals(serverOptions, server.toString());
159+
assertEquals(wrapperOptions, wrapper.toString());
160+
server.visitExpressions(visited::add);
161+
wrapper.visitExpressions(visited::add);
162+
assertEquals(List.of(server.getOptions().get(0).getValue(), server.getVersion(),
163+
wrapper.getOptions().get(0).getValue()), visited);
164+
assertInstanceOf(NullValue.class, server.getVersion());
165+
assertTrue(wrapper.getFunctions().isHandlerSpecified());
166+
assertNull(wrapper.getFunctions().getHandler());
167+
roundTrip(server);
168+
roundTrip(wrapper);
169+
170+
server.setVersion(null);
171+
wrapper.getFunctions().clearHandler();
172+
assertEquals("ALTER SERVER s OPTIONS (SET host 'remote')", server.toString());
173+
assertEquals("ALTER FOREIGN DATA WRAPPER w VALIDATOR public.validator OPTIONS (SET x 'y')",
174+
wrapper.toString());
175+
roundTrip(server);
176+
roundTrip(wrapper);
177+
}
178+
113179
@Test
114180
void userMappingOptionsUseExistingForeignDataNodes() throws JSQLParserException {
115181
CreateUserMapping create = (CreateUserMapping) CCJSqlParserUtil.parse(

0 commit comments

Comments
 (0)