1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.sql.parser.api.ASTNode;
46 import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
47 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment;
48 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
49 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
50
51 import java.util.Properties;
52 import java.util.stream.Collectors;
53
54
55
56
57 public final class EncryptDistSQLStatementVisitor extends EncryptDistSQLStatementBaseVisitor<ASTNode> implements SQLVisitor<ASTNode> {
58
59 @Override
60 public ASTNode visitCreateEncryptRule(final CreateEncryptRuleContext ctx) {
61 return new CreateEncryptRuleStatement(null != ctx.ifNotExists(), ctx.encryptRuleDefinition().stream().map(each -> (EncryptRuleSegment) visit(each)).collect(Collectors.toList()));
62 }
63
64 @Override
65 public ASTNode visitAlterEncryptRule(final AlterEncryptRuleContext ctx) {
66 return new AlterEncryptRuleStatement(ctx.encryptRuleDefinition().stream().map(each -> (EncryptRuleSegment) visit(each)).collect(Collectors.toList()));
67 }
68
69 @Override
70 public ASTNode visitDropEncryptRule(final DropEncryptRuleContext ctx) {
71 return new DropEncryptRuleStatement(null != ctx.ifExists(), ctx.tableName().stream().map(this::getIdentifierValue).collect(Collectors.toList()));
72 }
73
74 @Override
75 public ASTNode visitShowEncryptRules(final ShowEncryptRulesContext ctx) {
76 return new ShowEncryptRulesStatement(null == ctx.tableRule()
77 ? null
78 : getIdentifierValue(ctx.tableRule().tableName()), null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()));
79 }
80
81 @Override
82 public ASTNode visitEncryptRuleDefinition(final EncryptRuleDefinitionContext ctx) {
83 return new EncryptRuleSegment(getIdentifierValue(ctx.tableName()),
84 ctx.encryptTableRuleDefinition().encryptColumnDefinition().stream().map(each -> (EncryptColumnSegment) visit(each)).collect(Collectors.toList()));
85 }
86
87 @Override
88 public ASTNode visitEncryptColumnDefinition(final EncryptColumnDefinitionContext ctx) {
89 EncryptColumnItemSegment cipher = new EncryptColumnItemSegment(
90 getIdentifierValue(ctx.cipherColumnDefinition().cipherColumnName()), (AlgorithmSegment) visit(ctx.encryptAlgorithm().algorithmDefinition()));
91 EncryptColumnItemSegment assistedQuery = null == ctx.assistedQueryColumnDefinition()
92 ? null
93 : new EncryptColumnItemSegment(getIdentifierValue(ctx.assistedQueryColumnDefinition().assistedQueryColumnName()), getAssistedEncryptor(ctx));
94 EncryptColumnItemSegment likeQuery = null == ctx.likeQueryColumnDefinition()
95 ? null
96 : new EncryptColumnItemSegment(getIdentifierValue(ctx.likeQueryColumnDefinition().likeQueryColumnName()), getLikeEncryptor(ctx));
97 return new EncryptColumnSegment(getIdentifierValue(ctx.columnDefinition().columnName()), cipher, assistedQuery, likeQuery);
98 }
99
100 private AlgorithmSegment getAssistedEncryptor(final EncryptColumnDefinitionContext ctx) {
101 return null == ctx.assistedQueryAlgorithm() ? null : (AlgorithmSegment) visit(ctx.assistedQueryAlgorithm().algorithmDefinition());
102 }
103
104 private AlgorithmSegment getLikeEncryptor(final EncryptColumnDefinitionContext ctx) {
105 return null == ctx.likeQueryAlgorithm() ? null : (AlgorithmSegment) visit(ctx.likeQueryAlgorithm().algorithmDefinition());
106 }
107
108 @Override
109 public ASTNode visitAlgorithmDefinition(final AlgorithmDefinitionContext ctx) {
110 return new AlgorithmSegment(getIdentifierValue(ctx.algorithmTypeName()), getProperties(ctx.propertiesDefinition()));
111 }
112
113 private String getIdentifierValue(final ParseTree context) {
114 return null == context ? null : new IdentifierValue(context.getText()).getValue();
115 }
116
117 private Properties getProperties(final PropertiesDefinitionContext ctx) {
118 Properties result = new Properties();
119 if (null == ctx || null == ctx.properties()) {
120 return result;
121 }
122 for (PropertyContext each : ctx.properties().property()) {
123 result.setProperty(IdentifierValue.getQuotedContent(each.key.getText()), IdentifierValue.getQuotedContent(each.value.getText()));
124 }
125 return result;
126 }
127
128 @Override
129 public ASTNode visitTableName(final TableNameContext ctx) {
130 return new TableNameSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
131 }
132
133 @Override
134 public ASTNode visitDatabaseName(final DatabaseNameContext ctx) {
135 return new DatabaseSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
136 }
137
138 @Override
139 public ASTNode visitCountEncryptRule(final CountEncryptRuleContext ctx) {
140 return new CountRuleStatement(null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()), "ENCRYPT");
141 }
142
143 @Override
144 public ASTNode visitShowEncryptAlgorithmPlugins(final ShowEncryptAlgorithmPluginsContext ctx) {
145 return new ShowPluginsStatement("ENCRYPT_ALGORITHM");
146 }
147 }