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