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.apache.shardingsphere.database.connector.core.metadata.database.enums.QuoteCharacter;
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.type.ral.queryable.show.ShowPluginsStatement;
37  import org.apache.shardingsphere.distsql.statement.type.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.sql.parser.api.ASTNode;
46  import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
47  import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.FromDatabaseSegment;
48  import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseSegment;
49  import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
50  import org.apache.shardingsphere.sql.parser.statement.core.util.IdentifierValueUtils;
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(IdentifierValueUtils::getValue).collect(Collectors.toList()));
74      }
75      
76      @Override
77      public ASTNode visitShowEncryptRules(final ShowEncryptRulesContext ctx) {
78          return new ShowEncryptRulesStatement(null == ctx.tableRule()
79                  ? null
80                  : IdentifierValueUtils.getValue(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(IdentifierValueUtils.getValue(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                  IdentifierValueUtils.getValue(ctx.cipherColumnDefinition().cipherColumnName()), (AlgorithmSegment) visit(ctx.encryptAlgorithm().algorithmDefinition()));
94          EncryptColumnItemSegment assistedQuery = null == ctx.assistedQueryColumnDefinition()
95                  ? null
96                  : new EncryptColumnItemSegment(IdentifierValueUtils.getValue(ctx.assistedQueryColumnDefinition().assistedQueryColumnName()), getAssistedEncryptor(ctx));
97          EncryptColumnItemSegment likeQuery = null == ctx.likeQueryColumnDefinition()
98                  ? null
99                  : new EncryptColumnItemSegment(IdentifierValueUtils.getValue(ctx.likeQueryColumnDefinition().likeQueryColumnName()), getLikeEncryptor(ctx));
100         return new EncryptColumnSegment(IdentifierValueUtils.getValue(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(IdentifierValueUtils.getValue(ctx.algorithmTypeName()), getProperties(ctx.propertiesDefinition()));
114     }
115     
116     private Properties getProperties(final PropertiesDefinitionContext ctx) {
117         Properties result = new Properties();
118         if (null == ctx || null == ctx.properties()) {
119             return result;
120         }
121         for (PropertyContext each : ctx.properties().property()) {
122             result.setProperty(QuoteCharacter.unwrapAndTrimText(each.key.getText()), QuoteCharacter.unwrapAndTrimText(each.value.getText()));
123         }
124         return result;
125     }
126     
127     @Override
128     public ASTNode visitTableName(final TableNameContext ctx) {
129         return new TableNameSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
130     }
131     
132     @Override
133     public ASTNode visitDatabaseName(final DatabaseNameContext ctx) {
134         return new DatabaseSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
135     }
136     
137     @Override
138     public ASTNode visitCountEncryptRule(final CountEncryptRuleContext ctx) {
139         return new CountRuleStatement(null == ctx.databaseName() ? null : new FromDatabaseSegment(ctx.FROM().getSymbol().getStartIndex(), (DatabaseSegment) visit(ctx.databaseName())), "ENCRYPT");
140     }
141     
142     @Override
143     public ASTNode visitShowEncryptAlgorithmPlugins(final ShowEncryptAlgorithmPluginsContext ctx) {
144         return new ShowPluginsStatement("ENCRYPT_ALGORITHM");
145     }
146 }