|
| 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.Objects; |
| 13 | +import net.sf.jsqlparser.parser.ASTNodeAccessImpl; |
| 14 | +import net.sf.jsqlparser.statement.Statement; |
| 15 | +import net.sf.jsqlparser.statement.StatementVisitor; |
| 16 | + |
| 17 | +/** |
| 18 | + * MySQL {@code ALTER INSTANCE}, which acts on the server instance: |
| 19 | + * {@code {ENABLE | DISABLE} INNODB REDO_LOG}, {@code ROTATE INNODB MASTER KEY}, |
| 20 | + * {@code ROTATE BINLOG MASTER KEY}, {@code RELOAD TLS [FOR CHANNEL channel] [NO ROLLBACK ON ERROR]} |
| 21 | + * and {@code RELOAD KEYRING}. |
| 22 | + * |
| 23 | + * <p> |
| 24 | + * MySQL reads INNODB, BINLOG and REDO_LOG as identifiers, so they may be quoted; the statement |
| 25 | + * renders them as plain words. The channel is kept as written: MySQL's grammar takes any |
| 26 | + * identifier, and the server accepts {@code mysql_main} and {@code mysql_admin} when it runs the |
| 27 | + * statement. |
| 28 | + * |
| 29 | + * @see <a href="https://dev.mysql.com/doc/refman/8.4/en/alter-instance.html">MySQL ALTER |
| 30 | + * INSTANCE</a> |
| 31 | + */ |
| 32 | +public class AlterInstance extends ASTNodeAccessImpl implements Statement { |
| 33 | + /** The words after ALTER INSTANCE, without the options of RELOAD TLS. */ |
| 34 | + public enum Action { |
| 35 | + ENABLE_INNODB_REDO_LOG("ENABLE INNODB REDO_LOG"), DISABLE_INNODB_REDO_LOG( |
| 36 | + "DISABLE INNODB REDO_LOG"), ROTATE_INNODB_MASTER_KEY( |
| 37 | + "ROTATE INNODB MASTER KEY"), ROTATE_BINLOG_MASTER_KEY( |
| 38 | + "ROTATE BINLOG MASTER KEY"), RELOAD_TLS( |
| 39 | + "RELOAD TLS"), RELOAD_KEYRING("RELOAD KEYRING"); |
| 40 | + |
| 41 | + private final String sql; |
| 42 | + |
| 43 | + Action(String sql) { |
| 44 | + this.sql = sql; |
| 45 | + } |
| 46 | + |
| 47 | + public String getSql() { |
| 48 | + return sql; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private Action action; |
| 53 | + private String channel; |
| 54 | + private boolean noRollbackOnError; |
| 55 | + |
| 56 | + public AlterInstance(Action action) { |
| 57 | + this.action = Objects.requireNonNull(action, "action"); |
| 58 | + } |
| 59 | + |
| 60 | + public Action getAction() { |
| 61 | + return action; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sets the action. |
| 66 | + * |
| 67 | + * @throws IllegalArgumentException if the statement has options of RELOAD TLS and the action is |
| 68 | + * another one |
| 69 | + */ |
| 70 | + public void setAction(Action action) { |
| 71 | + Objects.requireNonNull(action, "action"); |
| 72 | + if (action != Action.RELOAD_TLS && (channel != null || noRollbackOnError)) { |
| 73 | + throw new IllegalArgumentException( |
| 74 | + "FOR CHANNEL and NO ROLLBACK ON ERROR belong to RELOAD TLS"); |
| 75 | + } |
| 76 | + this.action = action; |
| 77 | + } |
| 78 | + |
| 79 | + public AlterInstance withAction(Action action) { |
| 80 | + setAction(action); |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + /** The identifier of {@code RELOAD TLS FOR CHANNEL channel} as written, or null. */ |
| 85 | + public String getChannel() { |
| 86 | + return channel; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Sets the channel of RELOAD TLS; null removes the clause. |
| 91 | + * |
| 92 | + * @throws IllegalArgumentException if the action is not RELOAD TLS |
| 93 | + */ |
| 94 | + public void setChannel(String channel) { |
| 95 | + if (channel != null && action != Action.RELOAD_TLS) { |
| 96 | + throw new IllegalArgumentException("FOR CHANNEL belongs to RELOAD TLS"); |
| 97 | + } |
| 98 | + this.channel = channel; |
| 99 | + } |
| 100 | + |
| 101 | + public AlterInstance withChannel(String channel) { |
| 102 | + setChannel(channel); |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + /** Whether RELOAD TLS ends with NO ROLLBACK ON ERROR. */ |
| 107 | + public boolean isNoRollbackOnError() { |
| 108 | + return noRollbackOnError; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Sets NO ROLLBACK ON ERROR. |
| 113 | + * |
| 114 | + * @throws IllegalArgumentException if the action is not RELOAD TLS |
| 115 | + */ |
| 116 | + public void setNoRollbackOnError(boolean noRollbackOnError) { |
| 117 | + if (noRollbackOnError && action != Action.RELOAD_TLS) { |
| 118 | + throw new IllegalArgumentException("NO ROLLBACK ON ERROR belongs to RELOAD TLS"); |
| 119 | + } |
| 120 | + this.noRollbackOnError = noRollbackOnError; |
| 121 | + } |
| 122 | + |
| 123 | + public AlterInstance withNoRollbackOnError(boolean noRollbackOnError) { |
| 124 | + setNoRollbackOnError(noRollbackOnError); |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + public StringBuilder appendTo(StringBuilder builder) { |
| 129 | + builder.append("ALTER INSTANCE ").append(action.getSql()); |
| 130 | + if (channel != null) { |
| 131 | + builder.append(" FOR CHANNEL ").append(channel); |
| 132 | + } |
| 133 | + if (noRollbackOnError) { |
| 134 | + builder.append(" NO ROLLBACK ON ERROR"); |
| 135 | + } |
| 136 | + return builder; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public String toString() { |
| 141 | + return appendTo(new StringBuilder()).toString(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) { |
| 146 | + return statementVisitor.visit(this, context); |
| 147 | + } |
| 148 | +} |
0 commit comments