1111package net .sf .jsqlparser .statement ;
1212
1313import java .util .Objects ;
14+ import java .util .function .Consumer ;
1415
16+ import net .sf .jsqlparser .expression .Expression ;
1517import net .sf .jsqlparser .parser .ASTNodeAccessImpl ;
1618import net .sf .jsqlparser .schema .Table ;
1719import net .sf .jsqlparser .statement .create .table .Index ;
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
2529public 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}
0 commit comments