|
| 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.List; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.function.Consumer; |
| 16 | +import net.sf.jsqlparser.expression.Expression; |
| 17 | +import net.sf.jsqlparser.expression.StringValue; |
| 18 | +import net.sf.jsqlparser.parser.ASTNodeAccessImpl; |
| 19 | + |
| 20 | +/** |
| 21 | + * The MySQL statements that load and unload server plugins and components: |
| 22 | + * {@code INSTALL PLUGIN name SONAME 'library'}, {@code UNINSTALL PLUGIN name}, |
| 23 | + * {@code INSTALL COMPONENT 'urn'[, ...] [SET [GLOBAL | PERSIST] variable = value[, ...]]} and |
| 24 | + * {@code UNINSTALL COMPONENT 'urn'[, ...]}. DuckDB's {@code INSTALL extension} is an |
| 25 | + * {@link ExtensionStatement}. |
| 26 | + * |
| 27 | + * @see <a href="https://dev.mysql.com/doc/refman/8.4/en/component-statements.html">MySQL Plugin and |
| 28 | + * Component Statements</a> |
| 29 | + */ |
| 30 | +public class PluginStatement extends ASTNodeAccessImpl implements Statement { |
| 31 | + /** The statement's first word. */ |
| 32 | + public enum Action { |
| 33 | + INSTALL, UNINSTALL |
| 34 | + } |
| 35 | + |
| 36 | + /** What the statement loads or unloads. */ |
| 37 | + public enum ObjectType { |
| 38 | + PLUGIN, COMPONENT |
| 39 | + } |
| 40 | + |
| 41 | + private Action action; |
| 42 | + private ObjectType objectType; |
| 43 | + private String pluginName; |
| 44 | + private StringValue library; |
| 45 | + private List<StringValue> components = new ArrayList<>(); |
| 46 | + private List<ComponentVariable> variables = new ArrayList<>(); |
| 47 | + |
| 48 | + public PluginStatement(Action action, ObjectType objectType) { |
| 49 | + this.action = Objects.requireNonNull(action, "action"); |
| 50 | + this.objectType = Objects.requireNonNull(objectType, "objectType"); |
| 51 | + } |
| 52 | + |
| 53 | + public Action getAction() { |
| 54 | + return action; |
| 55 | + } |
| 56 | + |
| 57 | + public void setAction(Action action) { |
| 58 | + this.action = Objects.requireNonNull(action, "action"); |
| 59 | + } |
| 60 | + |
| 61 | + public ObjectType getObjectType() { |
| 62 | + return objectType; |
| 63 | + } |
| 64 | + |
| 65 | + public void setObjectType(ObjectType objectType) { |
| 66 | + this.objectType = Objects.requireNonNull(objectType, "objectType"); |
| 67 | + } |
| 68 | + |
| 69 | + /** The plugin name as written, or null for a component statement. */ |
| 70 | + public String getPluginName() { |
| 71 | + return pluginName; |
| 72 | + } |
| 73 | + |
| 74 | + public void setPluginName(String pluginName) { |
| 75 | + this.pluginName = pluginName; |
| 76 | + } |
| 77 | + |
| 78 | + public PluginStatement withPluginName(String pluginName) { |
| 79 | + setPluginName(pluginName); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + /** The plugin library file after SONAME; only INSTALL PLUGIN names one. */ |
| 84 | + public StringValue getLibrary() { |
| 85 | + return library; |
| 86 | + } |
| 87 | + |
| 88 | + public void setLibrary(StringValue library) { |
| 89 | + this.library = library; |
| 90 | + } |
| 91 | + |
| 92 | + public PluginStatement withLibrary(StringValue library) { |
| 93 | + setLibrary(library); |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + /** The component URNs, in the order written; empty for a plugin statement. */ |
| 98 | + public List<StringValue> getComponents() { |
| 99 | + return components; |
| 100 | + } |
| 101 | + |
| 102 | + public void setComponents(List<StringValue> components) { |
| 103 | + this.components = Objects.requireNonNull(components, "components"); |
| 104 | + } |
| 105 | + |
| 106 | + public PluginStatement addComponents(StringValue... components) { |
| 107 | + for (StringValue component : components) { |
| 108 | + this.components.add(component); |
| 109 | + } |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + /** The assignments after SET; only INSTALL COMPONENT has them. */ |
| 114 | + public List<ComponentVariable> getVariables() { |
| 115 | + return variables; |
| 116 | + } |
| 117 | + |
| 118 | + public void setVariables(List<ComponentVariable> variables) { |
| 119 | + this.variables = Objects.requireNonNull(variables, "variables"); |
| 120 | + } |
| 121 | + |
| 122 | + public PluginStatement addVariables(ComponentVariable... variables) { |
| 123 | + for (ComponentVariable variable : variables) { |
| 124 | + this.variables.add(variable); |
| 125 | + } |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + /** Passes the literals and the assigned values to {@code visitor}. */ |
| 130 | + public void visitExpressions(Consumer<Expression> visitor) { |
| 131 | + if (objectType == ObjectType.PLUGIN) { |
| 132 | + if (library != null) { |
| 133 | + visitor.accept(library); |
| 134 | + } |
| 135 | + return; |
| 136 | + } |
| 137 | + components.forEach(visitor); |
| 138 | + for (ComponentVariable variable : variables) { |
| 139 | + visitor.accept(variable.getValue()); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressionPrinter) { |
| 144 | + builder.append(action).append(' ').append(objectType); |
| 145 | + if (objectType == ObjectType.PLUGIN) { |
| 146 | + builder.append(' ').append(pluginName); |
| 147 | + if (library != null) { |
| 148 | + builder.append(" SONAME "); |
| 149 | + expressionPrinter.accept(library); |
| 150 | + } |
| 151 | + return builder; |
| 152 | + } |
| 153 | + for (int i = 0; i < components.size(); i++) { |
| 154 | + builder.append(i == 0 ? " " : ", "); |
| 155 | + expressionPrinter.accept(components.get(i)); |
| 156 | + } |
| 157 | + for (int i = 0; i < variables.size(); i++) { |
| 158 | + builder.append(i == 0 ? " SET " : ", "); |
| 159 | + variables.get(i).appendTo(builder, expressionPrinter); |
| 160 | + } |
| 161 | + return builder; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public String toString() { |
| 166 | + StringBuilder builder = new StringBuilder(); |
| 167 | + return appendTo(builder, builder::append).toString(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) { |
| 172 | + return statementVisitor.visit(this, context); |
| 173 | + } |
| 174 | +} |
0 commit comments