|
| 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.admin; |
| 11 | + |
| 12 | +import java.util.Locale; |
| 13 | +import java.util.Objects; |
| 14 | +import net.sf.jsqlparser.expression.StringValue; |
| 15 | +import net.sf.jsqlparser.parser.ASTNodeAccessImpl; |
| 16 | + |
| 17 | +/** |
| 18 | + * One flush option of MySQL {@code FLUSH}, such as {@code BINARY LOGS} or |
| 19 | + * {@code RELAY LOGS FOR CHANNEL 'c1'}. |
| 20 | + * |
| 21 | + * @see <a href="https://dev.mysql.com/doc/refman/8.4/en/flush.html">MySQL FLUSH</a> |
| 22 | + */ |
| 23 | +public class FlushOption extends ASTNodeAccessImpl { |
| 24 | + private Kind kind; |
| 25 | + private StringValue channel; |
| 26 | + |
| 27 | + /** The flush options of MySQL 8.4. */ |
| 28 | + public enum Kind { |
| 29 | + BINARY_LOGS, ENGINE_LOGS, ERROR_LOGS, GENERAL_LOGS, LOGS, OPTIMIZER_COSTS, PRIVILEGES, RELAY_LOGS, SLOW_LOGS, STATUS, USER_RESOURCES; |
| 30 | + |
| 31 | + /** Returns the SQL spelling, for example {@code BINARY LOGS}. */ |
| 32 | + public String getKeyword() { |
| 33 | + return this == OPTIMIZER_COSTS || this == USER_RESOURCES ? name() |
| 34 | + : name().replace('_', ' '); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns the option spelled by the keyword, ignoring case, or null. |
| 39 | + * |
| 40 | + * @param keyword the words of the option separated by one space, such as |
| 41 | + * {@code binary logs} |
| 42 | + */ |
| 43 | + public static Kind fromKeyword(String keyword) { |
| 44 | + String upper = keyword == null ? null : keyword.toUpperCase(Locale.ROOT); |
| 45 | + for (Kind kind : values()) { |
| 46 | + if (kind.getKeyword().equals(upper)) { |
| 47 | + return kind; |
| 48 | + } |
| 49 | + } |
| 50 | + return null; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public FlushOption(Kind kind) { |
| 55 | + setKind(kind); |
| 56 | + } |
| 57 | + |
| 58 | + public Kind getKind() { |
| 59 | + return kind; |
| 60 | + } |
| 61 | + |
| 62 | + public void setKind(Kind kind) { |
| 63 | + this.kind = Objects.requireNonNull(kind); |
| 64 | + } |
| 65 | + |
| 66 | + public FlushOption withKind(Kind kind) { |
| 67 | + setKind(kind); |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + /** Returns the channel of {@code RELAY LOGS FOR CHANNEL 'name'}, or null. */ |
| 72 | + public StringValue getChannel() { |
| 73 | + return channel; |
| 74 | + } |
| 75 | + |
| 76 | + public void setChannel(StringValue channel) { |
| 77 | + this.channel = channel; |
| 78 | + } |
| 79 | + |
| 80 | + public FlushOption withChannel(StringValue channel) { |
| 81 | + setChannel(channel); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + public StringBuilder appendTo(StringBuilder builder) { |
| 86 | + builder.append(kind.getKeyword()); |
| 87 | + if (channel != null) { |
| 88 | + builder.append(" FOR CHANNEL ").append(channel); |
| 89 | + } |
| 90 | + return builder; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public String toString() { |
| 95 | + return appendTo(new StringBuilder()).toString(); |
| 96 | + } |
| 97 | +} |
0 commit comments