Skip to content

Commit ddde4fc

Browse files
authored
feat: parse the MySQL replication statements (#2906)
CHANGE REPLICATION SOURCE TO, CHANGE REPLICATION FILTER, START REPLICA, STOP REPLICA, RESET REPLICA, RESET BINARY LOGS AND GTIDS and PURGE BINARY LOGS failed to parse, so a replica setup script failed at its first statement. The new statements of net.sf.jsqlparser.statement.replication follow the MySQL 8.4 grammar. ReplicationSourceOption extends ReplicationOption, and each option kind says which value it takes: a string, a number, NULL, 0 or 1, a keyword, the server ID list or an account. ReplicationFilter keeps database names, tables, patterns or rewrite pairs, and the statements share one FOR CHANNEL production. RESET reads MySQL's comma list into ResetReplicationStatement, apart from ResetStatement. PurgeStatement gains BINARY LOGS TO and BEFORE, which MariaDB also reads, with its MASTER LOGS; as in MySQL, BEFORE takes no subquery. Without a dialect a bare RESET REPLICA stays a ResetStatement, and other dialects keep their readings. STOP becomes a non-reserved keyword and remains usable as a name, also in Oracle's PURGE TABLESPACE. Signed-off-by: minleejae <mmj9808@gmail.com>
1 parent f925239 commit ddde4fc

27 files changed

Lines changed: 2418 additions & 12 deletions

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ and missing syntax gets added on demand — [open an issue](https://github.com/J
156156
| **MySQL servers, plugins and components** | `CREATE SERVER` · `ALTER SERVER` with `OPTIONS (… PORT 3306)` · `INSTALL`/`UNINSTALL PLUGIN` · `INSTALL`/`UNINSTALL COMPONENT` |
157157
| **PostgreSQL session statements** | `DISCARD` · `LISTEN` · `UNLISTEN` · `SHOW`/`RESET TIME ZONE`/`TRANSACTION ISOLATION LEVEL`/`SESSION AUTHORIZATION` · `COPY` options from before 9.0 |
158158
| **MySQL server administration** | `FLUSH` · `KILL` · `RESET PERSIST` · `HANDLER … OPEN`/`READ`/`CLOSE` |
159+
| **MySQL replication** | `CHANGE REPLICATION SOURCE TO` · `CHANGE REPLICATION FILTER` · `START`/`STOP REPLICA` · `RESET REPLICA` · `RESET BINARY LOGS AND GTIDS` · `PURGE BINARY LOGS` |
159160
| **MySQL data loading** | `LOAD DATA` · `LOAD XML` · `SELECT … INTO OUTFILE` with the same `FIELDS` and `LINES` clauses |
160161
| **PostgreSQL cursors** | `DECLARE … CURSOR` · `FETCH` · `MOVE` · `CLOSE` |
161162
| **MySQL account management** | `CREATE`/`ALTER`/`RENAME USER` · `CREATE ROLE` · `SET ROLE` · `SET DEFAULT ROLE` · `SET PASSWORD` |

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,12 @@ public enum Feature {
612612
* @see net.sf.jsqlparser.statement.PurgeStatement
613613
*/
614614
purge,
615+
/**
616+
* MySQL/MariaDB "PURGE BINARY LOGS" and MariaDB "PURGE MASTER LOGS" statements are allowed
617+
*
618+
* @see net.sf.jsqlparser.statement.PurgeStatement
619+
*/
620+
purgeBinaryLogs,
615621
/**
616622
* PostgreSQL "REASSIGN OWNED BY ... TO ..." statement is allowed
617623
*
@@ -874,6 +880,36 @@ public enum Feature {
874880
* @see SetResourceGroup
875881
*/
876882
setResourceGroup,
883+
/**
884+
* MySQL "CHANGE REPLICATION SOURCE TO" statement is allowed
885+
*
886+
* @see net.sf.jsqlparser.statement.replication.ChangeReplicationSourceStatement
887+
*/
888+
changeReplicationSource,
889+
/**
890+
* MySQL "CHANGE REPLICATION FILTER" statement is allowed
891+
*
892+
* @see net.sf.jsqlparser.statement.replication.ChangeReplicationFilterStatement
893+
*/
894+
changeReplicationFilter,
895+
/**
896+
* MySQL "START REPLICA" statement is allowed
897+
*
898+
* @see net.sf.jsqlparser.statement.replication.StartReplicaStatement
899+
*/
900+
startReplica,
901+
/**
902+
* MySQL "STOP REPLICA" statement is allowed
903+
*
904+
* @see net.sf.jsqlparser.statement.replication.StopReplicaStatement
905+
*/
906+
stopReplica,
907+
/**
908+
* MySQL "RESET BINARY LOGS AND GTIDS" and "RESET REPLICA" statements are allowed
909+
*
910+
* @see net.sf.jsqlparser.statement.replication.ResetReplicationStatement
911+
*/
912+
resetReplication,
877913
/**
878914
* SQL "COMMENT ON" statement is allowed
879915
*

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
* @author <a href="mailto:andreas@manticore-projects.com">Andreas Reichel</a>
1616
*/
1717
public enum PurgeObjectType {
18-
TABLE, INDEX, RECYCLEBIN, DBA_RECYCLEBIN, TABLESPACE;
18+
TABLE, INDEX, RECYCLEBIN, DBA_RECYCLEBIN, TABLESPACE,
19+
/** MySQL and MariaDB {@code PURGE BINARY LOGS} */
20+
BINARY_LOGS,
21+
/** MariaDB {@code PURGE MASTER LOGS}, which MySQL 8.4 removed */
22+
MASTER_LOGS;
1923

2024
public static PurgeObjectType from(String type) {
2125
return Enum.valueOf(PurgeObjectType.class, type.toUpperCase(Locale.ROOT));

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
package net.sf.jsqlparser.statement;
1212

1313
import java.util.Objects;
14+
import java.util.function.Consumer;
1415

16+
import net.sf.jsqlparser.expression.Expression;
1517
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
1618
import net.sf.jsqlparser.schema.Table;
1719
import net.sf.jsqlparser.statement.create.table.Index;
@@ -20,12 +22,23 @@
2022
* @author <a href="mailto:andreas@manticore-projects.com">Andreas Reichel</a>
2123
* @see <a href=
2224
* "https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9018.htm">Purge</a>
25+
* @see <a href="https://dev.mysql.com/doc/refman/8.4/en/purge-binary-logs.html">MySQL PURGE BINARY
26+
* LOGS</a>
2327
*/
2428

2529
public class PurgeStatement extends ASTNodeAccessImpl implements Statement {
2630
private final PurgeObjectType purgeObjectType;
2731
private final Object object;
2832
private String userName;
33+
private LogBound logBound;
34+
35+
/** How {@code PURGE BINARY LOGS} gives the first log it keeps. */
36+
public enum LogBound {
37+
/** {@code TO 'log_name'}: the logs before the named one */
38+
TO,
39+
/** {@code BEFORE datetime_expr}: the logs older than a point in time */
40+
BEFORE
41+
}
2942

3043
public PurgeStatement(Table table) {
3144
this.purgeObjectType = PurgeObjectType.TABLE;
@@ -51,15 +64,43 @@ public PurgeStatement(PurgeObjectType purgeObjectType, String tableSpaceName, St
5164
this.userName = userName;
5265
}
5366

67+
/**
68+
* MySQL {@code PURGE {BINARY | MASTER} LOGS {TO 'log_name' | BEFORE expr}}; for {@code TO} the
69+
* expression is the string literal of the log name.
70+
*/
71+
public PurgeStatement(PurgeObjectType purgeObjectType, LogBound logBound,
72+
Expression logExpression) {
73+
if (purgeObjectType != PurgeObjectType.BINARY_LOGS
74+
&& purgeObjectType != PurgeObjectType.MASTER_LOGS) {
75+
throw new IllegalArgumentException(
76+
"Only BINARY LOGS and MASTER LOGS take TO or BEFORE: " + purgeObjectType);
77+
}
78+
this.purgeObjectType = purgeObjectType;
79+
this.logBound = Objects.requireNonNull(logBound,
80+
"The TO or BEFORE of the PURGE LOGS statement must not be null.");
81+
this.object = Objects.requireNonNull(logExpression,
82+
"The log name or point in time of the PURGE LOGS statement must not be null.");
83+
}
84+
5485
@Override
5586
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
5687
return statementVisitor.visit(this, context);
5788
}
5889

90+
public StringBuilder appendTo(StringBuilder builder) {
91+
return appendTo(builder, builder::append);
92+
}
93+
5994
@SuppressWarnings({"PMD.MissingBreakInSwitch", "PMD.SwitchStmtsShouldHaveDefault",
6095
"PMD.CyclomaticComplexity"})
61-
public StringBuilder appendTo(StringBuilder builder) {
96+
public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressions) {
6297
builder.append("PURGE ");
98+
if (logBound != null) {
99+
builder.append(purgeObjectType == PurgeObjectType.BINARY_LOGS ? "BINARY" : "MASTER")
100+
.append(" LOGS ").append(logBound).append(' ');
101+
expressions.accept((Expression) object);
102+
return builder;
103+
}
63104

64105
switch (purgeObjectType) {
65106
case RECYCLEBIN:
@@ -106,4 +147,17 @@ public PurgeObjectType getPurgeObjectType() {
106147
public Object getObject() {
107148
return object;
108149
}
150+
151+
/** TO or BEFORE of {@code PURGE BINARY LOGS}, null for the other object types. */
152+
public LogBound getLogBound() {
153+
return logBound;
154+
}
155+
156+
/**
157+
* The log name literal of {@code TO} or the expression of {@code BEFORE}, null for the other
158+
* object types. It is the same object as {@link #getObject()}.
159+
*/
160+
public Expression getLogExpression() {
161+
return logBound != null ? (Expression) object : null;
162+
}
109163
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@
123123
import net.sf.jsqlparser.statement.lock.BackupLockStatement;
124124
import net.sf.jsqlparser.statement.lock.LockStatement;
125125
import net.sf.jsqlparser.statement.lock.UnlockStatement;
126+
import net.sf.jsqlparser.statement.replication.ChangeReplicationFilterStatement;
127+
import net.sf.jsqlparser.statement.replication.ChangeReplicationSourceStatement;
128+
import net.sf.jsqlparser.statement.replication.ResetReplicationStatement;
129+
import net.sf.jsqlparser.statement.replication.StartReplicaStatement;
130+
import net.sf.jsqlparser.statement.replication.StopReplicaStatement;
126131
import net.sf.jsqlparser.statement.maintenance.CheckpointStatement;
127132
import net.sf.jsqlparser.statement.maintenance.ClusterStatement;
128133
import net.sf.jsqlparser.statement.maintenance.VacuumStatement;
@@ -724,6 +729,10 @@ public <S> Void visit(MySqlLoadDataStatement loadData, S context) {
724729

725730
@Override
726731
public <S> Void visit(PurgeStatement purgeStatement, S context) {
732+
if (purgeStatement.getLogBound() != null) {
733+
// removes binary log files, a server operation like the replication statements
734+
return sessionOnly();
735+
}
727736
analysis.claimTopLevel();
728737
analysis.certain(StmtFeature.MODIFIES_SCHEMA, StmtFeature.MODIFIES_DATA);
729738
return null;
@@ -1056,6 +1065,32 @@ public <S> Void visit(BackupLockStatement backupLock, S context) {
10561065
return transactionOnly();
10571066
}
10581067

1068+
// the replication statements change server state, like ALTER SYSTEM, and no data or schema
1069+
@Override
1070+
public <S> Void visit(ChangeReplicationSourceStatement changeReplicationSource, S context) {
1071+
return sessionOnly();
1072+
}
1073+
1074+
@Override
1075+
public <S> Void visit(ChangeReplicationFilterStatement changeReplicationFilter, S context) {
1076+
return sessionOnly();
1077+
}
1078+
1079+
@Override
1080+
public <S> Void visit(StartReplicaStatement startReplica, S context) {
1081+
return sessionOnly();
1082+
}
1083+
1084+
@Override
1085+
public <S> Void visit(StopReplicaStatement stopReplica, S context) {
1086+
return sessionOnly();
1087+
}
1088+
1089+
@Override
1090+
public <S> Void visit(ResetReplicationStatement resetReplication, S context) {
1091+
return sessionOnly();
1092+
}
1093+
10591094
private Void transactionOnly() {
10601095
analysis.claimTopLevel();
10611096
analysis.certain(StmtFeature.MODIFIES_TRANSACTION);

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
import net.sf.jsqlparser.statement.maintenance.VacuumStatement;
118118
import net.sf.jsqlparser.statement.merge.Merge;
119119
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
120+
import net.sf.jsqlparser.statement.replication.ChangeReplicationFilterStatement;
121+
import net.sf.jsqlparser.statement.replication.ChangeReplicationSourceStatement;
122+
import net.sf.jsqlparser.statement.replication.ResetReplicationStatement;
123+
import net.sf.jsqlparser.statement.replication.StartReplicaStatement;
124+
import net.sf.jsqlparser.statement.replication.StopReplicaStatement;
120125
import net.sf.jsqlparser.statement.reindex.Reindex;
121126
import net.sf.jsqlparser.statement.resourcegroup.AlterResourceGroup;
122127
import net.sf.jsqlparser.statement.resourcegroup.CreateResourceGroup;
@@ -851,6 +856,46 @@ default void visit(BackupLockStatement backupLock) {
851856
visit(backupLock, null);
852857
}
853858

859+
default <S> T visit(ChangeReplicationSourceStatement changeReplicationSource, S context) {
860+
return null;
861+
}
862+
863+
default void visit(ChangeReplicationSourceStatement changeReplicationSource) {
864+
visit(changeReplicationSource, null);
865+
}
866+
867+
default <S> T visit(ChangeReplicationFilterStatement changeReplicationFilter, S context) {
868+
return null;
869+
}
870+
871+
default void visit(ChangeReplicationFilterStatement changeReplicationFilter) {
872+
visit(changeReplicationFilter, null);
873+
}
874+
875+
default <S> T visit(StartReplicaStatement startReplica, S context) {
876+
return null;
877+
}
878+
879+
default void visit(StartReplicaStatement startReplica) {
880+
visit(startReplica, null);
881+
}
882+
883+
default <S> T visit(StopReplicaStatement stopReplica, S context) {
884+
return null;
885+
}
886+
887+
default void visit(StopReplicaStatement stopReplica) {
888+
visit(stopReplica, null);
889+
}
890+
891+
default <S> T visit(ResetReplicationStatement resetReplication, S context) {
892+
return null;
893+
}
894+
895+
default void visit(ResetReplicationStatement resetReplication) {
896+
visit(resetReplication, null);
897+
}
898+
854899
default <S> T visit(DeclareCursorStatement declareCursor, S context) {
855900
return null;
856901
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@
114114
import net.sf.jsqlparser.statement.merge.MergeOperationVisitor;
115115
import net.sf.jsqlparser.statement.merge.MergeOperationVisitorAdapter;
116116
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
117+
import net.sf.jsqlparser.statement.replication.ChangeReplicationFilterStatement;
118+
import net.sf.jsqlparser.statement.replication.ChangeReplicationSourceStatement;
119+
import net.sf.jsqlparser.statement.replication.ResetReplicationStatement;
120+
import net.sf.jsqlparser.statement.replication.StartReplicaStatement;
121+
import net.sf.jsqlparser.statement.replication.StopReplicaStatement;
117122
import net.sf.jsqlparser.statement.reindex.Reindex;
118123
import net.sf.jsqlparser.statement.resourcegroup.AlterResourceGroup;
119124
import net.sf.jsqlparser.statement.resourcegroup.CreateResourceGroup;
@@ -626,6 +631,31 @@ public <S> T visit(BackupLockStatement backupLock, S context) {
626631
return null;
627632
}
628633

634+
@Override
635+
public <S> T visit(ChangeReplicationSourceStatement changeReplicationSource, S context) {
636+
return null;
637+
}
638+
639+
@Override
640+
public <S> T visit(ChangeReplicationFilterStatement changeReplicationFilter, S context) {
641+
return null;
642+
}
643+
644+
@Override
645+
public <S> T visit(StartReplicaStatement startReplica, S context) {
646+
return null;
647+
}
648+
649+
@Override
650+
public <S> T visit(StopReplicaStatement stopReplica, S context) {
651+
return null;
652+
}
653+
654+
@Override
655+
public <S> T visit(ResetReplicationStatement resetReplication, S context) {
656+
return null;
657+
}
658+
629659
@Override
630660
public <S> T visit(DeclareCursorStatement declareCursor, S context) {
631661
if (declareCursor.getQuery() != null) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.replication;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import net.sf.jsqlparser.expression.StringValue;
15+
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
16+
import net.sf.jsqlparser.statement.Statement;
17+
import net.sf.jsqlparser.statement.StatementVisitor;
18+
19+
/**
20+
* MySQL {@code CHANGE REPLICATION FILTER filter [, filter] ... [FOR CHANNEL 'channel']}.<br>
21+
* Example:<br>
22+
* CHANGE REPLICATION FILTER REPLICATE_DO_DB = (d1, d2), REPLICATE_REWRITE_DB = ((a, b))<br>
23+
*
24+
* @see <a href="https://dev.mysql.com/doc/refman/8.4/en/change-replication-filter.html">MySQL
25+
* CHANGE REPLICATION FILTER</a>
26+
*/
27+
public class ChangeReplicationFilterStatement extends ASTNodeAccessImpl implements Statement {
28+
private List<ReplicationFilter> filters = new ArrayList<>();
29+
private StringValue channel;
30+
31+
public List<ReplicationFilter> getFilters() {
32+
return filters;
33+
}
34+
35+
public void setFilters(List<ReplicationFilter> filters) {
36+
this.filters = filters;
37+
}
38+
39+
/** The channel of FOR CHANNEL, or null when the filters apply to every channel. */
40+
public StringValue getChannel() {
41+
return channel;
42+
}
43+
44+
public void setChannel(StringValue channel) {
45+
this.channel = channel;
46+
}
47+
48+
public StringBuilder appendTo(StringBuilder builder) {
49+
builder.append("CHANGE REPLICATION FILTER ");
50+
for (int i = 0; i < filters.size(); i++) {
51+
if (i > 0) {
52+
builder.append(", ");
53+
}
54+
filters.get(i).appendTo(builder);
55+
}
56+
if (channel != null) {
57+
builder.append(" FOR CHANNEL ").append(channel);
58+
}
59+
return builder;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return appendTo(new StringBuilder()).toString();
65+
}
66+
67+
@Override
68+
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
69+
return statementVisitor.visit(this, context);
70+
}
71+
}

0 commit comments

Comments
 (0)