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.mask.distsql.parser.core;
19  
20  import org.antlr.v4.runtime.tree.ParseTree;
21  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementBaseVisitor;
22  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.AlgorithmDefinitionContext;
23  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.AlterMaskRuleContext;
24  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.ColumnDefinitionContext;
25  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.CountMaskRuleContext;
26  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.CreateMaskRuleContext;
27  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.DatabaseNameContext;
28  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.DropMaskRuleContext;
29  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.MaskRuleDefinitionContext;
30  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.PropertiesDefinitionContext;
31  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.PropertyContext;
32  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.ShowMaskAlgorithmPluginsContext;
33  import org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.ShowMaskRulesContext;
34  import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;
35  import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowPluginsStatement;
36  import org.apache.shardingsphere.distsql.statement.rql.rule.database.CountRuleStatement;
37  import org.apache.shardingsphere.mask.distsql.segment.MaskColumnSegment;
38  import org.apache.shardingsphere.mask.distsql.segment.MaskRuleSegment;
39  import org.apache.shardingsphere.mask.distsql.statement.AlterMaskRuleStatement;
40  import org.apache.shardingsphere.mask.distsql.statement.CreateMaskRuleStatement;
41  import org.apache.shardingsphere.mask.distsql.statement.DropMaskRuleStatement;
42  import org.apache.shardingsphere.mask.distsql.statement.ShowMaskRulesStatement;
43  import org.apache.shardingsphere.sql.parser.api.ASTNode;
44  import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
45  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment;
46  import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
47  
48  import java.util.Properties;
49  import java.util.stream.Collectors;
50  
51  /**
52   * SQL statement visitor for mask DistSQL.
53   */
54  public final class MaskDistSQLStatementVisitor extends MaskDistSQLStatementBaseVisitor<ASTNode> implements SQLVisitor<ASTNode> {
55      
56      @Override
57      public ASTNode visitCreateMaskRule(final CreateMaskRuleContext ctx) {
58          return new CreateMaskRuleStatement(null != ctx.ifNotExists(), ctx.maskRuleDefinition().stream().map(each -> (MaskRuleSegment) visit(each)).collect(Collectors.toList()));
59      }
60      
61      @Override
62      public ASTNode visitAlterMaskRule(final AlterMaskRuleContext ctx) {
63          return new AlterMaskRuleStatement(ctx.maskRuleDefinition().stream().map(each -> (MaskRuleSegment) visit(each)).collect(Collectors.toList()));
64      }
65      
66      @Override
67      public ASTNode visitDropMaskRule(final DropMaskRuleContext ctx) {
68          return new DropMaskRuleStatement(null != ctx.ifExists(), ctx.ruleName().stream().map(this::getIdentifierValue).collect(Collectors.toList()));
69      }
70      
71      @Override
72      public ASTNode visitShowMaskRules(final ShowMaskRulesContext ctx) {
73          return new ShowMaskRulesStatement(null == ctx.RULE() ? null : getIdentifierValue(ctx.ruleName()),
74                  null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()));
75      }
76      
77      @Override
78      public ASTNode visitCountMaskRule(final CountMaskRuleContext ctx) {
79          return new CountRuleStatement(null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()), "MASK");
80      }
81      
82      @Override
83      public ASTNode visitMaskRuleDefinition(final MaskRuleDefinitionContext ctx) {
84          return new MaskRuleSegment(getIdentifierValue(ctx.ruleName()), ctx.columnDefinition().stream().map(each -> (MaskColumnSegment) visit(each)).collect(Collectors.toList()));
85      }
86      
87      @Override
88      public ASTNode visitColumnDefinition(final ColumnDefinitionContext ctx) {
89          return new MaskColumnSegment(getIdentifierValue(ctx.columnName()), (AlgorithmSegment) visit(ctx.algorithmDefinition()));
90      }
91      
92      @Override
93      public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
94          return new AlgorithmSegment(getIdentifierValue(ctx.algorithmTypeName()), getProperties(ctx.propertiesDefinition()));
95      }
96      
97      private String getIdentifierValue(final ParseTree context) {
98          return null == context ? null : new IdentifierValue(context.getText()).getValue();
99      }
100     
101     private Properties getProperties(final PropertiesDefinitionContext ctx) {
102         Properties result = new Properties();
103         if (null == ctx || null == ctx.properties()) {
104             return result;
105         }
106         for (PropertyContext each : ctx.properties().property()) {
107             result.setProperty(IdentifierValue.getQuotedContent(each.key.getText()), IdentifierValue.getQuotedContent(each.value.getText()));
108         }
109         return result;
110     }
111     
112     @Override
113     public ASTNode visitDatabaseName(final DatabaseNameContext ctx) {
114         return new DatabaseSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
115     }
116     
117     @Override
118     public ASTNode visitShowMaskAlgorithmPlugins(final ShowMaskAlgorithmPluginsContext ctx) {
119         return new ShowPluginsStatement("MASK_ALGORITHM");
120     }
121 }