View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.distsql.parser.core.kernel;
19  
20  import org.antlr.v4.runtime.tree.ParseTree;
21  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementBaseVisitor;
22  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlgorithmDefinitionContext;
23  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterComputeNodeContext;
24  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterStorageUnitContext;
25  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ConvertYamlConfigurationContext;
26  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DatabaseNameContext;
27  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DisableComputeNodeContext;
28  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.EnableComputeNodeContext;
29  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ExportDatabaseConfigurationContext;
30  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ExportMetaDataContext;
31  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ExportStorageNodesContext;
32  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.FromSegmentContext;
33  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.IgnoreBroadcastTablesContext;
34  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.IgnoreSingleAndBroadcastTablesContext;
35  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.IgnoreSingleTablesContext;
36  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ImportDatabaseConfigurationContext;
37  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ImportMetaDataContext;
38  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.InstanceIdContext;
39  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.LabelComputeNodeContext;
40  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.LockClusterContext;
41  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.PasswordContext;
42  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.PropertiesDefinitionContext;
43  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.PropertyContext;
44  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.RefreshDatabaseMetadataContext;
45  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.RefreshTableMetadataContext;
46  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.RegisterStorageUnitContext;
47  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.SetDistVariableContext;
48  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowComputeNodeInfoContext;
49  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowComputeNodeModeContext;
50  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowComputeNodesContext;
51  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowDistVariableContext;
52  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowDistVariablesContext;
53  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowKeyGenerateAlgorithmPluginsContext;
54  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowLoadBalanceAlgorithmPluginsContext;
55  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowLogicalTablesContext;
56  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowPluginImplementationsContext;
57  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowRulesUsedStorageUnitContext;
58  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowStorageUnitsContext;
59  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowTableMetadataContext;
60  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.StorageUnitDefinitionContext;
61  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnlabelComputeNodeContext;
62  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnlockClusterContext;
63  import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnregisterStorageUnitContext;
64  import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;
65  import org.apache.shardingsphere.distsql.segment.DataSourceSegment;
66  import org.apache.shardingsphere.distsql.segment.HostnameAndPortBasedDataSourceSegment;
67  import org.apache.shardingsphere.distsql.segment.URLBasedDataSourceSegment;
68  import org.apache.shardingsphere.distsql.statement.ral.queryable.convert.ConvertYamlConfigurationStatement;
69  import org.apache.shardingsphere.distsql.statement.ral.queryable.export.ExportDatabaseConfigurationStatement;
70  import org.apache.shardingsphere.distsql.statement.ral.queryable.export.ExportMetaDataStatement;
71  import org.apache.shardingsphere.distsql.statement.ral.queryable.export.ExportStorageNodesStatement;
72  import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowComputeNodeInfoStatement;
73  import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowComputeNodeModeStatement;
74  import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowComputeNodesStatement;
75  import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowDistVariableStatement;
76  import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowDistVariablesStatement;
77  import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowPluginsStatement;
78  import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowTableMetaDataStatement;
79  import org.apache.shardingsphere.distsql.statement.ral.updatable.AlterComputeNodeStatement;
80  import org.apache.shardingsphere.distsql.statement.ral.updatable.ImportDatabaseConfigurationStatement;
81  import org.apache.shardingsphere.distsql.statement.ral.updatable.ImportMetaDataStatement;
82  import org.apache.shardingsphere.distsql.statement.ral.updatable.LabelComputeNodeStatement;
83  import org.apache.shardingsphere.distsql.statement.ral.updatable.LockClusterStatement;
84  import org.apache.shardingsphere.distsql.statement.ral.updatable.RefreshDatabaseMetaDataStatement;
85  import org.apache.shardingsphere.distsql.statement.ral.updatable.RefreshTableMetaDataStatement;
86  import org.apache.shardingsphere.distsql.statement.ral.updatable.SetDistVariableStatement;
87  import org.apache.shardingsphere.distsql.statement.ral.updatable.SetInstanceStatusStatement;
88  import org.apache.shardingsphere.distsql.statement.ral.updatable.UnlabelComputeNodeStatement;
89  import org.apache.shardingsphere.distsql.statement.ral.updatable.UnlockClusterStatement;
90  import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.AlterStorageUnitStatement;
91  import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.RegisterStorageUnitStatement;
92  import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.UnregisterStorageUnitStatement;
93  import org.apache.shardingsphere.distsql.statement.rql.resource.ShowLogicalTablesStatement;
94  import org.apache.shardingsphere.distsql.statement.rql.resource.ShowStorageUnitsStatement;
95  import org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowRulesUsedStorageUnitStatement;
96  import org.apache.shardingsphere.sql.parser.api.ASTNode;
97  import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
98  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment;
99  import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
100 import org.apache.shardingsphere.sql.parser.sql.common.value.literal.impl.StringLiteralValue;
101 
102 import java.util.Collection;
103 import java.util.Objects;
104 import java.util.Properties;
105 import java.util.stream.Collectors;
106 
107 /**
108  * SQL statement visitor for kernel DistSQL.
109  */
110 public final class KernelDistSQLStatementVisitor extends KernelDistSQLStatementBaseVisitor<ASTNode> implements SQLVisitor<ASTNode> {
111     
112     @Override
113     public ASTNode visitRegisterStorageUnit(final RegisterStorageUnitContext ctx) {
114         return new RegisterStorageUnitStatement(null != ctx.ifNotExists(), ctx.storageUnitDefinition().stream().map(each -> (DataSourceSegment) visit(each)).collect(Collectors.toList()));
115     }
116     
117     @Override
118     public ASTNode visitAlterStorageUnit(final AlterStorageUnitContext ctx) {
119         return new AlterStorageUnitStatement(ctx.storageUnitDefinition().stream().map(each -> (DataSourceSegment) visit(each)).collect(Collectors.toList()));
120     }
121     
122     @Override
123     public ASTNode visitShowTableMetadata(final ShowTableMetadataContext ctx) {
124         Collection<String> tableNames = ctx.tableName().stream().map(this::getIdentifierValue).collect(Collectors.toSet());
125         return new ShowTableMetaDataStatement(tableNames, null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()));
126     }
127     
128     @Override
129     public ASTNode visitStorageUnitDefinition(final StorageUnitDefinitionContext ctx) {
130         String user = getIdentifierValue(ctx.user());
131         String password = null == ctx.password() ? "" : getPassword(ctx.password());
132         Properties props = getProperties(ctx.propertiesDefinition());
133         return null == ctx.urlSource()
134                 ? new HostnameAndPortBasedDataSourceSegment(getIdentifierValue(ctx.storageUnitName()),
135                         getIdentifierValue(ctx.simpleSource().hostname()), ctx.simpleSource().port().getText(), getIdentifierValue(ctx.simpleSource().dbName()), user, password, props)
136                 : new URLBasedDataSourceSegment(getIdentifierValue(ctx.storageUnitName()), getIdentifierValue(ctx.urlSource().url()), user, password, props);
137     }
138     
139     private String getPassword(final PasswordContext ctx) {
140         return null == ctx ? null : StringLiteralValue.getStandardEscapesStringLiteralValue(ctx.getText()).getValue();
141     }
142     
143     @Override
144     public ASTNode visitShowComputeNodes(final ShowComputeNodesContext ctx) {
145         return new ShowComputeNodesStatement();
146     }
147     
148     @Override
149     public ASTNode visitShowComputeNodeInfo(final ShowComputeNodeInfoContext ctx) {
150         return new ShowComputeNodeInfoStatement();
151     }
152     
153     @Override
154     public ASTNode visitShowComputeNodeMode(final ShowComputeNodeModeContext ctx) {
155         return new ShowComputeNodeModeStatement();
156     }
157     
158     @Override
159     public ASTNode visitEnableComputeNode(final EnableComputeNodeContext ctx) {
160         return buildSetInstanceStatusStatement(ctx.ENABLE().getText().toUpperCase(), ctx.instanceId());
161     }
162     
163     @Override
164     public ASTNode visitDisableComputeNode(final DisableComputeNodeContext ctx) {
165         return buildSetInstanceStatusStatement(ctx.DISABLE().getText().toUpperCase(), ctx.instanceId());
166     }
167     
168     @Override
169     public ASTNode visitLabelComputeNode(final LabelComputeNodeContext ctx) {
170         Collection<String> labels = ctx.label().stream().map(this::getIdentifierValue).collect(Collectors.toList());
171         return new LabelComputeNodeStatement(null != ctx.RELABEL(), getIdentifierValue(ctx.instanceId()), labels);
172     }
173     
174     @Override
175     public ASTNode visitUnlabelComputeNode(final UnlabelComputeNodeContext ctx) {
176         Collection<String> labels = ctx.label().stream().map(this::getIdentifierValue).collect(Collectors.toList());
177         return new UnlabelComputeNodeStatement(getIdentifierValue(ctx.instanceId()), labels);
178     }
179     
180     private SetInstanceStatusStatement buildSetInstanceStatusStatement(final String status, final InstanceIdContext instanceIdContext) {
181         return new SetInstanceStatusStatement(status, getIdentifierValue(instanceIdContext));
182     }
183     
184     @Override
185     public ASTNode visitAlterComputeNode(final AlterComputeNodeContext ctx) {
186         return new AlterComputeNodeStatement(getIdentifierValue(ctx.instanceId()), getIdentifierValue(ctx.variableName()), getIdentifierValue(ctx.variableValues()));
187     }
188     
189     private Properties getProperties(final PropertiesDefinitionContext ctx) {
190         Properties result = new Properties();
191         if (null == ctx || null == ctx.properties()) {
192             return result;
193         }
194         for (PropertyContext each : ctx.properties().property()) {
195             result.setProperty(IdentifierValue.getQuotedContent(each.key.getText()), IdentifierValue.getQuotedContent(each.value.getText()));
196         }
197         return result;
198     }
199     
200     @Override
201     public ASTNode visitUnregisterStorageUnit(final UnregisterStorageUnitContext ctx) {
202         boolean ignoreSingleTables = ctx.ignoreTables() instanceof IgnoreSingleAndBroadcastTablesContext || ctx.ignoreTables() instanceof IgnoreSingleTablesContext;
203         boolean ignoreBroadcastTables = ctx.ignoreTables() instanceof IgnoreSingleAndBroadcastTablesContext || ctx.ignoreTables() instanceof IgnoreBroadcastTablesContext;
204         return new UnregisterStorageUnitStatement(null != ctx.ifExists(),
205                 ctx.storageUnitName().stream().map(ParseTree::getText).map(each -> new IdentifierValue(each).getValue()).collect(Collectors.toList()),
206                 ignoreSingleTables, ignoreBroadcastTables);
207     }
208     
209     @Override
210     public ASTNode visitShowStorageUnits(final ShowStorageUnitsContext ctx) {
211         DatabaseSegment database = null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName());
212         Integer usageCount = null == ctx.usageCount() ? null : Integer.parseInt(ctx.usageCount().getText());
213         return new ShowStorageUnitsStatement(database, usageCount);
214     }
215     
216     @Override
217     public ASTNode visitDatabaseName(final DatabaseNameContext ctx) {
218         return new DatabaseSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
219     }
220     
221     @Override
222     public ASTNode visitSetDistVariable(final SetDistVariableContext ctx) {
223         return new SetDistVariableStatement(getIdentifierValue(ctx.variableName()), getIdentifierValue(ctx.variableValue()));
224     }
225     
226     @Override
227     public ASTNode visitShowLogicalTables(final ShowLogicalTablesContext ctx) {
228         return new ShowLogicalTablesStatement(null == ctx.showLike() ? null : getIdentifierValue(ctx.showLike().likePattern()),
229                 null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()));
230     }
231     
232     @Override
233     public ASTNode visitShowDistVariable(final ShowDistVariableContext ctx) {
234         return new ShowDistVariableStatement(Objects.requireNonNull(getIdentifierValue(ctx.variableName())).toUpperCase());
235     }
236     
237     @Override
238     public ASTNode visitShowDistVariables(final ShowDistVariablesContext ctx) {
239         return new ShowDistVariablesStatement(null == ctx.showLike() ? null : getIdentifierValue(ctx.showLike().likePattern()));
240     }
241     
242     @Override
243     public ASTNode visitRefreshDatabaseMetadata(final RefreshDatabaseMetadataContext ctx) {
244         return new RefreshDatabaseMetaDataStatement(null == ctx.databaseName() ? null : getIdentifierValue(ctx.databaseName()), null != ctx.FORCE());
245     }
246     
247     @Override
248     public ASTNode visitRefreshTableMetadata(final RefreshTableMetadataContext ctx) {
249         if (null == ctx.refreshScope()) {
250             return new RefreshTableMetaDataStatement();
251         }
252         String storageUnitName = null;
253         String schemaName = null;
254         String tableName = getIdentifierValue(ctx.refreshScope().tableName());
255         if (null != ctx.refreshScope().fromSegment()) {
256             FromSegmentContext fromSegment = ctx.refreshScope().fromSegment();
257             storageUnitName = getIdentifierValue(fromSegment.storageUnitName());
258             schemaName = null == fromSegment.schemaName() ? null : getIdentifierValue(fromSegment.schemaName());
259         }
260         return new RefreshTableMetaDataStatement(tableName, storageUnitName, schemaName);
261     }
262     
263     @Override
264     public ASTNode visitExportDatabaseConfiguration(final ExportDatabaseConfigurationContext ctx) {
265         return new ExportDatabaseConfigurationStatement(null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()), getIdentifierValue(ctx.filePath()));
266     }
267     
268     @Override
269     public ASTNode visitExportMetaData(final ExportMetaDataContext ctx) {
270         return new ExportMetaDataStatement(null == ctx.filePath() ? null : getIdentifierValue(ctx.filePath()));
271     }
272     
273     @Override
274     public ASTNode visitExportStorageNodes(final ExportStorageNodesContext ctx) {
275         return new ExportStorageNodesStatement(null == ctx.databaseName() ? null : getIdentifierValue(ctx.databaseName()), null == ctx.filePath() ? null : getIdentifierValue(ctx.filePath()));
276     }
277     
278     @Override
279     public ASTNode visitConvertYamlConfiguration(final ConvertYamlConfigurationContext ctx) {
280         return new ConvertYamlConfigurationStatement(getIdentifierValue(ctx.filePath()));
281     }
282     
283     @Override
284     public ASTNode visitShowRulesUsedStorageUnit(final ShowRulesUsedStorageUnitContext ctx) {
285         return new ShowRulesUsedStorageUnitStatement(getIdentifierValue(ctx.storageUnitName()), null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()));
286     }
287     
288     @Override
289     public ASTNode visitImportDatabaseConfiguration(final ImportDatabaseConfigurationContext ctx) {
290         return new ImportDatabaseConfigurationStatement(getIdentifierValue(ctx.filePath()));
291     }
292     
293     @Override
294     public ASTNode visitImportMetaData(final ImportMetaDataContext ctx) {
295         return new ImportMetaDataStatement(null == ctx.metaDataValue() ? null : getQuotedContent(ctx.metaDataValue()), getIdentifierValue(ctx.filePath()));
296     }
297     
298     @Override
299     public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
300         return new AlgorithmSegment(getIdentifierValue(ctx.algorithmTypeName()), buildProperties(ctx.propertiesDefinition()));
301     }
302     
303     private Properties buildProperties(final PropertiesDefinitionContext ctx) {
304         Properties result = new Properties();
305         if (null == ctx) {
306             return result;
307         }
308         for (PropertyContext each : ctx.properties().property()) {
309             result.setProperty(IdentifierValue.getQuotedContent(each.key.getText()), IdentifierValue.getQuotedContent(each.value.getText()));
310         }
311         return result;
312     }
313     
314     @Override
315     public ASTNode visitLockCluster(final LockClusterContext ctx) {
316         return new LockClusterStatement((AlgorithmSegment) visitAlgorithmDefinition(ctx.lockStrategy().algorithmDefinition()));
317     }
318     
319     @Override
320     public ASTNode visitUnlockCluster(final UnlockClusterContext ctx) {
321         return new UnlockClusterStatement();
322     }
323     
324     private String getIdentifierValue(final ParseTree context) {
325         return null == context ? null : new IdentifierValue(context.getText()).getValue();
326     }
327     
328     private String getQuotedContent(final ParseTree context) {
329         return IdentifierValue.getQuotedContent(context.getText());
330     }
331     
332     @Override
333     public ASTNode visitShowPluginImplementations(final ShowPluginImplementationsContext ctx) {
334         return new ShowPluginsStatement("COMMON", getIdentifierValue(ctx.pluginClass()));
335     }
336     
337     @Override
338     public ASTNode visitShowKeyGenerateAlgorithmPlugins(final ShowKeyGenerateAlgorithmPluginsContext ctx) {
339         return new ShowPluginsStatement("KEY_GENERATE_ALGORITHM");
340     }
341     
342     @Override
343     public ASTNode visitShowLoadBalanceAlgorithmPlugins(final ShowLoadBalanceAlgorithmPluginsContext ctx) {
344         return new ShowPluginsStatement("LOAD_BALANCE_ALGORITHM");
345     }
346 }