/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.core.jdbc.core.connection;

import io.shardingsphere.core.jdbc.adapter.AbstractConnectionAdapter;
import io.shardingsphere.core.jdbc.core.ShardingContext;
import io.shardingsphere.core.jdbc.core.statement.ShardingPreparedStatement;
import io.shardingsphere.core.jdbc.core.statement.ShardingStatement;
import io.shardingsphere.core.rule.DataNode;
import io.shardingsphere.core.rule.MasterSlaveRule;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Connection that support sharding.
 * 
 * @author zhangliang
 * @author caohao
 * @author gaohongtao
 */
@RequiredArgsConstructor
public final class ShardingConnection extends AbstractConnectionAdapter {
    
    @Getter
    private final ShardingContext shardingContext;
    
    /**
     * Release connection.
     *
     * @param connection to be released connection
     */
    public void release(final Connection connection) {
        removeCache(connection);
        try {
            connection.close();
        } catch (final SQLException ignore) {
        }
    }
    
    /**
     * Get all connections via logic table for each actual tables.
     * 
     * @param logicTableName logic table name
     * @return map of all connections
     * @throws SQLException SQL exception
     */
    public Map<String, Connection> getConnections(final String logicTableName) throws SQLException {
        Map<String, Connection> result = new HashMap<>();
        for (DataNode each : shardingContext.getShardingRule().getTableRuleByLogicTableName(logicTableName).getActualDataNodes()) {
            String dataSourceName = shardingContext.getShardingRule().getShardingDataSourceNames().getRawMasterDataSourceName(each.getDataSourceName());
            result.put(dataSourceName, getConnection(dataSourceName));
        }
        return result;
    }
    
    @Override
    protected Map<String, DataSource> getDataSourceMap() {
        return shardingContext.getDataSourceMap();
    }
    
    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
        Collection<MasterSlaveRule> masterSlaveRules = shardingContext.getShardingRule().getMasterSlaveRules();
        if (masterSlaveRules.isEmpty()) {
            return getConnection(shardingContext.getDataSourceMap().keySet().iterator().next()).getMetaData();
        }
        for (MasterSlaveRule each : masterSlaveRules) {
            if (getDataSourceMap().containsKey(each.getMasterDataSourceName())) {
                return getConnection(each.getMasterDataSourceName()).getMetaData();
            }
        }
        throw new UnsupportedOperationException();
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql) {
        return new ShardingPreparedStatement(this, sql);
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) {
        return new ShardingPreparedStatement(this, sql, resultSetType, resultSetConcurrency);
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
        return new ShardingPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) {
        return new ShardingPreparedStatement(this, sql, autoGeneratedKeys);
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) {
        return new ShardingPreparedStatement(this, sql, Statement.RETURN_GENERATED_KEYS);
    }
    
    @Override
    public PreparedStatement prepareStatement(final String sql, final String[] columnNames) {
        return new ShardingPreparedStatement(this, sql, Statement.RETURN_GENERATED_KEYS);
    }
    
    @Override
    public Statement createStatement() {
        return new ShardingStatement(this);
    }
    
    @Override
    public Statement createStatement(final int resultSetType, final int resultSetConcurrency) {
        return new ShardingStatement(this, resultSetType, resultSetConcurrency);
    }
    
    @Override
    public Statement createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
        return new ShardingStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability);
    }
}

@@ -944,53 +944,8 @@
 nt;%0A
-import io.shardingsphere.core.rule.DataNode;%0A
 impo
@@ -992,16 +992,16 @@
 veRule;%0A
+
 import l
@@ -1270,34 +1270,8 @@
 on;%0A
-import java.util.HashMap;%0A
 impo
@@ -1849,24 +1849,24 @@
 n ignore) %7B%0A
+
         %7D%0A  
@@ -1878,784 +1878,8 @@
    %0A
-    /**%0A     * Get all connections via logic table for each actual tables.%0A     * %0A     * @param logicTableName logic table name%0A     * @return map of all connections%0A     * @throws SQLException SQL exception%0A     */%0A    public Map%3CString, Connection%3E getConnections(final String logicTableName) throws SQLException %7B%0A        Map%3CString, Connection%3E result = new HashMap%3C%3E();%0A        for (DataNode each : shardingContext.getShardingRule().getTableRuleByLogicTableName(logicTableName).getActualDataNodes()) %7B%0A            String dataSourceName = shardingContext.getShardingRule().getShardingDataSourceNames().getRawMasterDataSourceName(each.getDataSourceName());%0A            result.put(dataSourceName, getConnection(dataSourceName));%0A        %7D%0A        return result;%0A    %7D%0A    %0A
     

delete unused code