|
| 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; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Collection; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import java.util.stream.Collectors; |
| 17 | +import net.sf.jsqlparser.schema.Table; |
| 18 | + |
| 19 | +/** |
| 20 | + * A MySQL/MariaDB {@code CHECK TABLE}, {@code OPTIMIZE TABLE} or {@code REPAIR TABLE} statement. |
| 21 | + * {@code ANALYZE TABLE} remains {@link net.sf.jsqlparser.statement.analyze.Analyze}. |
| 22 | + */ |
| 23 | +public class TableMaintenanceStatement implements Statement { |
| 24 | + |
| 25 | + public enum Operation { |
| 26 | + CHECK, OPTIMIZE, REPAIR; |
| 27 | + |
| 28 | + /** Whether this operation accepts the option after its table list. */ |
| 29 | + public boolean allows(Option option) { |
| 30 | + switch (this) { |
| 31 | + case CHECK: |
| 32 | + return option != Option.USE_FRM; |
| 33 | + case REPAIR: |
| 34 | + return option == Option.QUICK || option == Option.EXTENDED |
| 35 | + || option == Option.USE_FRM; |
| 36 | + default: |
| 37 | + return false; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** Whether this operation accepts {@code NO_WRITE_TO_BINLOG} or {@code LOCAL}. */ |
| 42 | + public boolean allowsModifier() { |
| 43 | + return this != CHECK; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public enum Modifier { |
| 48 | + NO_WRITE_TO_BINLOG, LOCAL |
| 49 | + } |
| 50 | + |
| 51 | + public enum Option { |
| 52 | + FOR_UPGRADE, QUICK, FAST, MEDIUM, EXTENDED, CHANGED, USE_FRM; |
| 53 | + |
| 54 | + /** Returns the SQL spelling, for example {@code FOR UPGRADE}. */ |
| 55 | + public String getKeyword() { |
| 56 | + return this == FOR_UPGRADE ? "FOR UPGRADE" : name(); |
| 57 | + } |
| 58 | + |
| 59 | + /** Returns the single-word option spelled by {@code word}, or null. */ |
| 60 | + public static Option fromKeyword(String word) { |
| 61 | + for (Option option : values()) { |
| 62 | + if (option != FOR_UPGRADE && option.name().equalsIgnoreCase(word)) { |
| 63 | + return option; |
| 64 | + } |
| 65 | + } |
| 66 | + return null; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private Operation operation; |
| 71 | + private Modifier modifier; |
| 72 | + private boolean tablesKeyword; |
| 73 | + private final List<Table> tables = new ArrayList<>(); |
| 74 | + private final List<Option> options = new ArrayList<>(); |
| 75 | + |
| 76 | + public TableMaintenanceStatement() { |
| 77 | + // empty constructor |
| 78 | + } |
| 79 | + |
| 80 | + public TableMaintenanceStatement(Operation operation) { |
| 81 | + this.operation = operation; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) { |
| 86 | + return statementVisitor.visit(this, context); |
| 87 | + } |
| 88 | + |
| 89 | + public Operation getOperation() { |
| 90 | + return operation; |
| 91 | + } |
| 92 | + |
| 93 | + public void setOperation(Operation operation) { |
| 94 | + this.operation = operation; |
| 95 | + } |
| 96 | + |
| 97 | + public Modifier getModifier() { |
| 98 | + return modifier; |
| 99 | + } |
| 100 | + |
| 101 | + public void setModifier(Modifier modifier) { |
| 102 | + this.modifier = modifier; |
| 103 | + } |
| 104 | + |
| 105 | + /** Whether the statement is spelled with {@code TABLES} instead of {@code TABLE}. */ |
| 106 | + public boolean isTablesKeyword() { |
| 107 | + return tablesKeyword; |
| 108 | + } |
| 109 | + |
| 110 | + public void setTablesKeyword(boolean tablesKeyword) { |
| 111 | + this.tablesKeyword = tablesKeyword; |
| 112 | + } |
| 113 | + |
| 114 | + public List<Table> getTables() { |
| 115 | + return Collections.unmodifiableList(tables); |
| 116 | + } |
| 117 | + |
| 118 | + public void setTables(Collection<? extends Table> tables) { |
| 119 | + this.tables.clear(); |
| 120 | + if (tables != null) { |
| 121 | + this.tables.addAll(tables); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** Returns the options in source order; repeated options are retained as written. */ |
| 126 | + public List<Option> getOptions() { |
| 127 | + return Collections.unmodifiableList(options); |
| 128 | + } |
| 129 | + |
| 130 | + public void setOptions(Collection<Option> options) { |
| 131 | + this.options.clear(); |
| 132 | + if (options != null) { |
| 133 | + this.options.addAll(options); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public String toString() { |
| 139 | + StringBuilder builder = new StringBuilder().append(operation); |
| 140 | + if (modifier != null) { |
| 141 | + builder.append(' ').append(modifier); |
| 142 | + } |
| 143 | + builder.append(tablesKeyword ? " TABLES " : " TABLE ") |
| 144 | + .append(tables.stream().map(Table::toString).collect(Collectors.joining(", "))); |
| 145 | + for (Option option : options) { |
| 146 | + builder.append(' ').append(option.getKeyword()); |
| 147 | + } |
| 148 | + return builder.toString(); |
| 149 | + } |
| 150 | + |
| 151 | + public TableMaintenanceStatement withOperation(Operation operation) { |
| 152 | + setOperation(operation); |
| 153 | + return this; |
| 154 | + } |
| 155 | + |
| 156 | + public TableMaintenanceStatement withModifier(Modifier modifier) { |
| 157 | + setModifier(modifier); |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + public TableMaintenanceStatement withTablesKeyword(boolean tablesKeyword) { |
| 162 | + setTablesKeyword(tablesKeyword); |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + public TableMaintenanceStatement withTables(Collection<? extends Table> tables) { |
| 167 | + setTables(tables); |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + public TableMaintenanceStatement addTables(Table... tables) { |
| 172 | + Collections.addAll(this.tables, tables); |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + public TableMaintenanceStatement withOptions(Collection<Option> options) { |
| 177 | + setOptions(options); |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + public TableMaintenanceStatement addOptions(Option... options) { |
| 182 | + Collections.addAll(this.options, options); |
| 183 | + return this; |
| 184 | + } |
| 185 | +} |
0 commit comments