This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 60556634981 add `show mask rule` DistSQL resultSet (#23199)
60556634981 is described below

commit 6055663498167f9f784a7935b90975e81077dafd
Author: Zichao <[email protected]>
AuthorDate: Fri Dec 30 23:39:24 2022 +1300

    add `show mask rule` DistSQL resultSet (#23199)
    
    * add `show mask rule` DistSQL resultSet
    
    * add `show mask rule` DistSQL resultSet
---
 .../distsql/handler/query/MaskRuleResultSet.java   | 87 ++++++++++++++++++++++
 ...here.distsql.handler.resultset.DistSQLResultSet |  2 +-
 .../parser/core/MaskDistSQLStatementVisitor.java   |  9 +++
 .../parser/statement/ShowMaskRulesStatement.java   | 36 +++++++++
 4 files changed, 133 insertions(+), 1 deletion(-)

diff --git 
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/MaskRuleResultSet.java
 
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/MaskRuleResultSet.java
new file mode 100644
index 00000000000..0b30ea2c6a3
--- /dev/null
+++ 
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/MaskRuleResultSet.java
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.distsql.handler.query;
+
+import 
org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.util.props.PropertiesConverter;
+import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
+import 
org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import 
org.apache.shardingsphere.mask.distsql.parser.statement.ShowMaskRulesStatement;
+import org.apache.shardingsphere.mask.rule.MaskRule;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * Result set for show mask rule.
+ */
+public final class MaskRuleResultSet implements DatabaseDistSQLResultSet {
+    
+    private Iterator<Collection<Object>> data = Collections.emptyIterator();
+    
+    @Override
+    public void init(final ShardingSphereDatabase database, final SQLStatement 
sqlStatement) {
+        Optional<MaskRule> rule = 
database.getRuleMetaData().findSingleRule(MaskRule.class);
+        rule.ifPresent(optional -> data = buildData((MaskRuleConfiguration) 
optional.getConfiguration(), (ShowMaskRulesStatement) sqlStatement).iterator());
+    }
+    
+    private Collection<Collection<Object>> buildData(final 
MaskRuleConfiguration ruleConfig, final ShowMaskRulesStatement sqlStatement) {
+        return ruleConfig.getTables().stream().filter(each -> 
Objects.isNull(sqlStatement.getTableName()) || 
each.getName().equals(sqlStatement.getTableName()))
+                .map(each -> buildColumnData(each, 
ruleConfig.getMaskAlgorithms())).flatMap(Collection::stream).collect(Collectors.toList());
+    }
+    
+    private Collection<Collection<Object>> buildColumnData(final 
MaskTableRuleConfiguration tableRuleConfig, final Map<String, 
AlgorithmConfiguration> algorithmMap) {
+        Collection<Collection<Object>> result = new LinkedList<>();
+        tableRuleConfig.getColumns().forEach(each -> {
+            AlgorithmConfiguration maskAlgorithmConfig = 
algorithmMap.get(each.getMaskAlgorithm());
+            result.add(Arrays.asList(tableRuleConfig.getName(), 
each.getLogicColumn(),
+                    maskAlgorithmConfig.getType(), 
PropertiesConverter.convert(maskAlgorithmConfig.getProps())));
+        });
+        return result;
+    }
+    
+    @Override
+    public Collection<String> getColumnNames() {
+        return Arrays.asList("table", "column", "algorithm_type", 
"algorithm_props");
+    }
+    
+    @Override
+    public boolean next() {
+        return data.hasNext();
+    }
+    
+    @Override
+    public Collection<Object> getRowData() {
+        return data.next();
+    }
+    
+    @Override
+    public String getType() {
+        return ShowMaskRulesStatement.class.getName();
+    }
+}
diff --git 
a/features/mask/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
 
b/features/mask/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
index eac90149e7d..8bb10a389bb 100644
--- 
a/features/mask/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++ 
b/features/mask/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
@@ -15,5 +15,5 @@
 # limitations under the License.
 #
 
-#org.apache.shardingsphere.mask.distsql.handler.query.MaskRuleResultSet
+org.apache.shardingsphere.mask.distsql.handler.query.MaskRuleResultSet
 #org.apache.shardingsphere.mask.distsql.handler.query.CountMaskRuleResultSet
diff --git 
a/features/mask/distsql/parser/src/main/java/org/apache/shardingsphere/mask/distsql/parser/core/MaskDistSQLStatementVisitor.java
 
b/features/mask/distsql/parser/src/main/java/org/apache/shardingsphere/mask/distsql/parser/core/MaskDistSQLStatementVisitor.java
index a8fff137e8f..db6a776574e 100644
--- 
a/features/mask/distsql/parser/src/main/java/org/apache/shardingsphere/mask/distsql/parser/core/MaskDistSQLStatementVisitor.java
+++ 
b/features/mask/distsql/parser/src/main/java/org/apache/shardingsphere/mask/distsql/parser/core/MaskDistSQLStatementVisitor.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.mask.distsql.parser.core;
 
 import org.antlr.v4.runtime.tree.ParseTree;
 import 
org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementBaseVisitor;
+import 
org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser;
 import 
org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.AlgorithmDefinitionContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.AlterMaskRuleContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.MaskDistSQLStatementParser.ColumnDefinitionContext;
@@ -33,8 +34,10 @@ import 
org.apache.shardingsphere.mask.distsql.parser.segment.MaskRuleSegment;
 import 
org.apache.shardingsphere.mask.distsql.parser.statement.AlterMaskRuleStatement;
 import 
org.apache.shardingsphere.mask.distsql.parser.statement.CreateMaskRuleStatement;
 import 
org.apache.shardingsphere.mask.distsql.parser.statement.DropMaskRuleStatement;
+import 
org.apache.shardingsphere.mask.distsql.parser.statement.ShowMaskRulesStatement;
 import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
 import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 
 import java.util.Properties;
@@ -60,6 +63,12 @@ public final class MaskDistSQLStatementVisitor extends 
MaskDistSQLStatementBaseV
         return new DropMaskRuleStatement(null != ctx.ifExists(), 
ctx.ruleName().stream().map(this::getIdentifierValue).collect(Collectors.toList()));
     }
     
+    @Override
+    public ASTNode visitShowMaskRules(final 
MaskDistSQLStatementParser.ShowMaskRulesContext ctx) {
+        return new ShowMaskRulesStatement(null == ctx.RULE() ? null : 
getIdentifierValue(ctx.ruleName()),
+                null == ctx.databaseName() ? null : (DatabaseSegment) 
visit(ctx.databaseName()));
+    }
+    
     @Override
     public ASTNode visitMaskRuleDefinition(final MaskRuleDefinitionContext 
ctx) {
         return new MaskRuleSegment(getIdentifierValue(ctx.ruleName()), 
ctx.columnDefinition().stream().map(each -> (MaskColumnSegment) 
visit(each)).collect(Collectors.toList()));
diff --git 
a/features/mask/distsql/statement/src/main/java/org/apache/shardingsphere/mask/distsql/parser/statement/ShowMaskRulesStatement.java
 
b/features/mask/distsql/statement/src/main/java/org/apache/shardingsphere/mask/distsql/parser/statement/ShowMaskRulesStatement.java
new file mode 100644
index 00000000000..d445bc788ba
--- /dev/null
+++ 
b/features/mask/distsql/statement/src/main/java/org/apache/shardingsphere/mask/distsql/parser/statement/ShowMaskRulesStatement.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.distsql.parser.statement;
+
+import lombok.Getter;
+import 
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRulesStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment;
+
+/**
+ * Show mask rules statement.
+ */
+@Getter
+public final class ShowMaskRulesStatement extends ShowRulesStatement {
+    
+    private final String tableName;
+    
+    public ShowMaskRulesStatement(final String tableName, final 
DatabaseSegment database) {
+        super(database);
+        this.tableName = tableName;
+    }
+}

Reply via email to