/*
 * 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;

import com.google.gson.Gson;
import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.distsql.RQLResultSet;
import org.apache.shardingsphere.proxy.config.util.DataSourceParameterConverter;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Result set for show data source.
 */
public final class DataSourceQueryResultSet implements RQLResultSet {
    
    private Map<String, DataSourceParameter> dataSourceParameterMap;
    
    private Iterator<String> dataSourceNames;
    
    private ShardingSphereMetaData metaData;
    
    @Override
    public void init(final ShardingSphereMetaData metaData, final SQLStatement sqlStatement) {
        this.metaData = metaData;
        dataSourceParameterMap = DataSourceParameterConverter.getDataSourceParameterMap(DataSourceConverter.getDataSourceConfigurationMap(metaData.getResource().getDataSources()));
        dataSourceNames = dataSourceParameterMap.keySet().iterator();
    }
    
    @Override
    public Collection<String> getColumnNames() {
        return Arrays.asList("name", "type", "host", "port", "db", "attribute");
    }
    
    @Override
    public boolean next() {
        return dataSourceNames.hasNext();
    }
    
    @Override
    public Collection<Object> getRowData() {
        String dataSourceName = dataSourceNames.next();
        DataSourceMetaData dataSourceMetaData = metaData.getResource().getDataSourcesMetaData().getDataSourceMetaData(dataSourceName);
        String type = metaData.getResource().getDatabaseType().getName();
        String host = dataSourceMetaData.getHostName();
        int port = dataSourceMetaData.getPort();
        String db = dataSourceMetaData.getCatalog();
        return Arrays.asList(dataSourceName, type, host, port, db, (new Gson()).toJson(getAttributeMap(dataSourceParameterMap.get(dataSourceName))));
    }
    
    private Map<Object, Object> getAttributeMap(final DataSourceParameter dataSourceParameter) {
        Map<Object, Object> result = new HashMap<>(7, 1);
        result.put("connectionTimeoutMilliseconds", dataSourceParameter.getConnectionTimeoutMilliseconds());
        result.put("idleTimeoutMilliseconds", dataSourceParameter.getIdleTimeoutMilliseconds());
        result.put("maxLifetimeMilliseconds", dataSourceParameter.getMaxLifetimeMilliseconds());
        result.put("maxPoolSize", dataSourceParameter.getMaxPoolSize());
        result.put("minPoolSize", dataSourceParameter.getMinPoolSize());
        result.put("maintenanceIntervalMilliseconds", dataSourceParameter.getMaintenanceIntervalMilliseconds());
        result.put("readOnly", dataSourceParameter.isReadOnly());
        return result;
    }
    
    @Override
    public String getType() {
        return ShowResourcesStatement.class.getCanonicalName();
    }
}

@@ -1208,32 +1208,93 @@
 SourceMetaData;%0A
+import org.apache.shardingsphere.infra.distsql.RQLResultSet;%0A
 import org.apach
@@ -1388,36 +1388,56 @@
 e.infra.
-distsql.RQLResultSet
+metadata.resource.ShardingSphereResource
 ;%0Aimport
@@ -1991,33 +1991,33 @@
 ngSphere
-MetaData metaData
+Resource resource
 ;%0A    %0A 
@@ -2136,32 +2136,41 @@
     
-this.metaData = metaData
+resource = metaData.getResource()
 ;%0A  
@@ -2842,38 +2842,24 @@
 aData = 
-metaData.getR
+r
 esource
-()
 .getData
@@ -2937,38 +2937,24 @@
  type = 
-metaData.getR
+r
 esource
-()
 .getData

Refactor DataSourceQueryResultSet (#10925)