RaigorJiang commented on a change in pull request #15069: URL: https://github.com/apache/shardingsphere/pull/15069#discussion_r793210134
########## File path: shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/executor/ExportSchemaConfigurationExecutor.java ########## @@ -0,0 +1,348 @@ +/* + * 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.ral.common.show.executor; + +import com.google.common.base.Strings; +import com.zaxxer.hikari.HikariDataSource; +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.distsql.parser.statement.ral.common.ExportSchemaConfigurationStatement; +import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rulealtered.OnRuleAlteredActionConfiguration; +import org.apache.shardingsphere.infra.exception.SchemaNotExistedException; +import org.apache.shardingsphere.infra.exception.ShardingSphereException; +import org.apache.shardingsphere.infra.merge.result.MergedResult; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.proxy.backend.context.ProxyContext; +import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException; +import org.apache.shardingsphere.proxy.backend.response.header.query.impl.QueryHeader; +import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.HintShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.merge.dal.common.MultipleLocalDataMergedResult; + +import javax.sql.DataSource; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.sql.Types; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +/** + * Export schema configuration executor. + */ +@RequiredArgsConstructor +public final class ExportSchemaConfigurationExecutor extends AbstractShowExecutor { + + private static final String CONFIG = "config"; + + private static final String COLON = ":"; + + private static final String SPACE = " "; + + private static final String NEWLINE = "\n"; + + private static final String COLON_SPACE = COLON + SPACE; + + private static final String COLON_NEWLINE = COLON + NEWLINE; + + private static final String INDENT = SPACE + SPACE; + + private final ExportSchemaConfigurationStatement sqlStatement; + + private final ConnectionSession connectionSession; + + @Override + protected List<QueryHeader> createQueryHeaders() { + return Arrays.asList(new QueryHeader("", "", CONFIG, CONFIG, Types.VARCHAR, "VARCHAR", 128, 0, false, false, false, false)); + } + + @Override + protected MergedResult createMergedResult() { + String schemaName = getSchemaName(); + ShardingSphereMetaData metaData = ProxyContext.getInstance().getMetaData(schemaName); + StringBuilder result = new StringBuilder(); + result.append("schemaName").append(COLON_SPACE).append(schemaName).append(NEWLINE); + getDataSourcesConfig(metaData, result); + Optional<ShardingRuleConfiguration> ruleConfig = metaData.getRuleMetaData().getConfigurations() + .stream().filter(each -> each instanceof ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration) each).findAny(); + ruleConfig.ifPresent(rule -> getRulesConfig(rule, result)); + if (!sqlStatement.getFilePath().isPresent()) { + return new MultipleLocalDataMergedResult(Collections.singleton(Collections.singletonList(result.toString()))); + } + + File outFile = new File(sqlStatement.getFilePath().get()); + try (FileOutputStream stream = new FileOutputStream(outFile)) { + stream.write(result.toString().getBytes()); + stream.flush(); + } catch (final IOException ex) { + throw new ShardingSphereException(ex); + } + return null; + } + + private void getDataSourcesConfig(final ShardingSphereMetaData metaData, final StringBuilder result) { + if (null == metaData.getResource().getDataSources() || metaData.getResource().getDataSources().isEmpty()) { + return; + } + result.append(NEWLINE).append("dataSources").append(COLON_NEWLINE); + for (Map.Entry<String, DataSource> each : metaData.getResource().getDataSources().entrySet()) { + HikariDataSource dataSource = (HikariDataSource) each.getValue(); Review comment: Please use the DataSourcePropertiesCreator.create method to obtain the data source properties. It is not a good idea to cast it to HikariDataSource. It can be adjusted in the next PR. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
