You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/02/05 16:17:17 UTC

[GitHub] [shardingsphere] TeslaCN commented on a change in pull request #15251: Add `SHOW RULES USED RESOURCE` syntax to DistSQL.

TeslaCN commented on a change in pull request #15251:
URL: https://github.com/apache/shardingsphere/pull/15251#discussion_r800072948



##########
File path: shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/rule/RulesUsedResourceQueryResultSet.java
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.proxy.backend.text.distsql.rql.rule;
+
+import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
+import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRulesUsedResourceStatement;
+import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Result set for show rules used resource.
+ */
+public final class RulesUsedResourceQueryResultSet implements DistSQLResultSet {
+    
+    private static final String TYPE = ShowRulesUsedResourceStatement.class.getName();
+    
+    private static final String SHARDING = "sharding";
+    
+    private static final String READWRITE_SPLITTING = "readwrite_splitting";
+    
+    private static final String DB_DISCOVERY = "db_discovery";
+    
+    private static final String ENCRYPT = "encrypt";
+    
+    private static final String SHADOW = "shadow";
+    
+    private static final Map<String, Class<? extends RuleConfiguration>> FEATURE_MAP = new HashMap<>(5, 1);
+    
+    private Iterator<Collection<Object>> data;
+    
+    static {
+        FEATURE_MAP.put(SHARDING, ShardingRuleConfiguration.class);
+        FEATURE_MAP.put(READWRITE_SPLITTING, ReadwriteSplittingRuleConfiguration.class);
+        FEATURE_MAP.put(DB_DISCOVERY, DatabaseDiscoveryRuleConfiguration.class);
+        FEATURE_MAP.put(ENCRYPT, EncryptRuleConfiguration.class);
+        FEATURE_MAP.put(SHADOW, ShadowRuleConfiguration.class);
+    }
+    
+    @Override
+    public void init(final ShardingSphereMetaData metaData, final SQLStatement sqlStatement) {
+        List<Collection<Object>> result = new ArrayList<>();
+        ShowRulesUsedResourceStatement statement = (ShowRulesUsedResourceStatement) sqlStatement;
+        String resourceName = statement.getResourceName().get();
+        if (hasRulesConfig(metaData) && metaData.getResource().getDataSources().keySet().contains(resourceName)) {
+            getRulesConfig(metaData.getRuleMetaData().getConfigurations(), resourceName, result);
+        }
+        this.data = result.iterator();
+    }
+    
+    private void getRulesConfig(final Collection<RuleConfiguration> ruleConfigurations, final String resourceName, final List<Collection<Object>> result) {
+        ruleConfigurations.stream().forEach(each -> {
+            getRulesConfigForSharding(each, result);
+            getRulesConfigForReadwriteSplitting(each, resourceName, result);
+            getRulesConfigForDBDiscovery(each, resourceName, result);
+            getRulesConfigForEncrypt(each, result);
+            getRulesConfigForShadow(each, resourceName, result);
+        });
+    }
+    
+    private void getRulesConfigForSharding(final RuleConfiguration ruleConfig, final List<Collection<Object>> result) {
+        if (!(ruleConfig.getClass().getCanonicalName().equals(FEATURE_MAP.get(SHARDING).getCanonicalName()))) {

Review comment:
       Why not use `getName()`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org