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.encrypt.distsql.parser.core;
19  
20  import org.antlr.v4.runtime.tree.ParseTree;
21  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementBaseVisitor;
22  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.AlgorithmDefinitionContext;
23  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.AlterEncryptRuleContext;
24  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.CountEncryptRuleContext;
25  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.CreateEncryptRuleContext;
26  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.DatabaseNameContext;
27  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.DropEncryptRuleContext;
28  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.EncryptColumnDefinitionContext;
29  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.EncryptRuleDefinitionContext;
30  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.PropertiesDefinitionContext;
31  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.PropertyContext;
32  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.ShowEncryptAlgorithmPluginsContext;
33  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.ShowEncryptRulesContext;
34  import org.apache.shardingsphere.distsql.parser.autogen.EncryptDistSQLStatementParser.TableNameContext;
35  import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;
36  import org.apache.shardingsphere.distsql.statement.ral.queryable.show.ShowPluginsStatement;
37  import org.apache.shardingsphere.distsql.statement.rql.rule.database.CountRuleStatement;
38  import org.apache.shardingsphere.encrypt.distsql.segment.EncryptColumnItemSegment;
39  import org.apache.shardingsphere.encrypt.distsql.segment.EncryptColumnSegment;
40  import org.apache.shardingsphere.encrypt.distsql.segment.EncryptRuleSegment;
41  import org.apache.shardingsphere.encrypt.distsql.statement.AlterEncryptRuleStatement;
42  import org.apache.shardingsphere.encrypt.distsql.statement.CreateEncryptRuleStatement;
43  import org.apache.shardingsphere.encrypt.distsql.statement.DropEncryptRuleStatement;
44  import org.apache.shardingsphere.encrypt.distsql.statement.ShowEncryptRulesStatement;
45  import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
46  import org.apache.shardingsphere.sql.parser.api.ASTNode;
47  import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
48  import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.FromDatabaseSegment;
49  import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseSegment;
50  import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
51  import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
52  
53  import java.util.Properties;
54  import java.util.stream.Collectors;
55  
56  /**
57   * SQL statement visitor for encrypt DistSQL.
58   */
59  public final class EncryptDistSQLStatementVisitor extends EncryptDistSQLStatementBaseVisitor<ASTNode> implements SQLVisitor<ASTNode> {
60      
61      @Override
62      public ASTNode visitCreateEncryptRule(final CreateEncryptRuleContext ctx) {
63          return new CreateEncryptRuleStatement(null != ctx.ifNotExists(), ctx.encryptRuleDefinition().stream().map(each -> (EncryptRuleSegment) visit(each)).collect(Collectors.toList()));
64      }
65      
66      @Override
67      public ASTNode visitAlterEncryptRule(final AlterEncryptRuleContext ctx) {
68          return new AlterEncryptRuleStatement(ctx.encryptRuleDefinition().stream().map(each -> (EncryptRuleSegment) visit(each)).collect(Collectors.toList()));
69      }
70      
71      @Override
72      public ASTNode visitDropEncryptRule(final DropEncryptRuleContext ctx) {
73          return new DropEncryptRuleStatement(null != ctx.ifExists(), ctx.tableName().stream().map(this::getIdentifierValue).collect(Collectors.toList()));
74      }
75      
76      @Override
77      public ASTNode visitShowEncryptRules(final ShowEncryptRulesContext ctx) {
78          return new ShowEncryptRulesStatement(null == ctx.tableRule()
79                  ? null
80                  : getIdentifierValue(ctx.tableRule().tableName()),
81                  null == ctx.databaseName() ? null : new FromDatabaseSegment(ctx.FROM().getSymbol().getStartIndex(), (DatabaseSegment) visit(ctx.databaseName())));
82      }
83      
84      @Override
85      public ASTNode visitEncryptRuleDefinition(final EncryptRuleDefinitionContext ctx) {
86          return new EncryptRuleSegment(getIdentifierValue(ctx.tableName()),
87                  ctx.encryptTableRuleDefinition().encryptColumnDefinition().stream().map(each -> (EncryptColumnSegment) visit(each)).collect(Collectors.toList()));
88      }
89      
90      @Override
91      public ASTNode visitEncryptColumnDefinition(final EncryptColumnDefinitionContext ctx) {
92          EncryptColumnItemSegment cipher = new EncryptColumnItemSegment(
93                  getIdentifierValue(ctx.cipherColumnDefinition().cipherColumnName()), (AlgorithmSegment) visit(ctx.encryptAlgorithm().algorithmDefinition()));
94          EncryptColumnItemSegment assistedQuery = null == ctx.assistedQueryColumnDefinition()
95                  ? null
96                  : new EncryptColumnItemSegment(getIdentifierValue(ctx.assistedQueryColumnDefinition().assistedQueryColumnName()), getAssistedEncryptor(ctx));
97          EncryptColumnItemSegment likeQuery = null == ctx.likeQueryColumnDefinition()
98                  ? null
99                  : new EncryptColumnItemSegment(getIdentifierValue(ctx.likeQueryColumnDefinition().likeQueryColumnName()), getLikeEncryptor(ctx));
100         return new EncryptColumnSegment(getIdentifierValue(ctx.columnDefinition().columnName()), cipher, assistedQuery, likeQuery);
101     }
102     
103     private AlgorithmSegment getAssistedEncryptor(final EncryptColumnDefinitionContext ctx) {
104         return null == ctx.assistedQueryAlgorithm() ? null : (AlgorithmSegment) visit(ctx.assistedQueryAlgorithm().algorithmDefinition());
105     }
106     
107     private AlgorithmSegment getLikeEncryptor(final EncryptColumnDefinitionContext ctx) {
108         return null == ctx.likeQueryAlgorithm() ? null : (AlgorithmSegment) visit(ctx.likeQueryAlgorithm().algorithmDefinition());
109     }
110     
111     @Override
112     public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
113         return new AlgorithmSegment(getIdentifierValue(ctx.algorithmTypeName()), getProperties(ctx.propertiesDefinition()));
114     }
115     
116     private String getIdentifierValue(final ParseTree context) {
117         return null == context ? null : new IdentifierValue(context.getText()).getValue();
118     }
119     
120     private Properties getProperties(final PropertiesDefinitionContext ctx) {
121         Properties result = new Properties();
122         if (null == ctx || null == ctx.properties()) {
123             return result;
124         }
125         for (PropertyContext each : ctx.properties().property()) {
126             result.setProperty(QuoteCharacter.unwrapAndTrimText(each.key.getText()), QuoteCharacter.unwrapAndTrimText(each.value.getText()));
127         }
128         return result;
129     }
130     
131     @Override
132     public ASTNode visitTableName(final TableNameContext ctx) {
133         return new TableNameSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
134     }
135     
136     @Override
137     public ASTNode visitDatabaseName(final DatabaseNameContext ctx) {
138         return new DatabaseSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
139     }
140     
141     @Override
142     public ASTNode visitCountEncryptRule(final CountEncryptRuleContext ctx) {
143         return new CountRuleStatement(null == ctx.databaseName() ? null : new FromDatabaseSegment(ctx.FROM().getSymbol().getStartIndex(), (DatabaseSegment) visit(ctx.databaseName())), "ENCRYPT");
144     }
145     
146     @Override
147     public ASTNode visitShowEncryptAlgorithmPlugins(final ShowEncryptAlgorithmPluginsContext ctx) {
148         return new ShowPluginsStatement("ENCRYPT_ALGORITHM");
149     }
150 }