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

/**
 * Connection that support sharding.
 * 
 * @author zhangliang
 * @author gaohongtao
 */
@RequiredArgsConstructor
public final class ShardingConnection extends AbstractConnectionAdapter {
    
    @Getter
    private final ShardingContext shardingContext;
    
    @Override
    protected Map<String, DataSource> getDataSourceMap() {
        return shardingContext.getDataSourceMap();
    }
    
    /**
     * Release connection.
     *
     * @param connection to be released connection
     */
    public void release(final Connection connection) {
        removeCache(connection);
        try {
            connection.close();
        } catch (final SQLException ignore) {
        }
    }
    
    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
        return getConnection(shardingContext.getDataSourceMap().keySet().iterator().next()).getMetaData();
    }
    
    @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);
    }
}

@@ -936,24 +936,76 @@
 gStatement;%0A
+import io.shardingsphere.core.rule.MasterSlaveRule;%0A
 import lombo
@@ -1010,24 +1010,24 @@
 bok.Getter;%0A
-
 import lombo
@@ -1229,32 +1229,61 @@
 .sql.Statement;%0A
+import java.util.Collection;%0A
 import java.util
@@ -1356,16 +1356,34 @@
 ngliang%0A
+ * @author caohao%0A
  * @auth
@@ -2099,103 +2099,568 @@
     
-return getConnection(shardingContext.getDataSourceMap().keySet().iterator().next()).getMetaData
+Collection%3CMasterSlaveRule%3E masterSlaveRules = shardingContext.getShardingRule().getMasterSlaveRules();%0A        if (masterSlaveRules.isEmpty()) %7B%0A            return getConnection(shardingContext.getDataSourceMap().keySet().iterator().next()).getMetaData();%0A        %7D%0A        for (MasterSlaveRule each : masterSlaveRules) %7B%0A            if (getDataSourceMap().containsKey(each.getMasterDataSourceName())) %7B%0A                return getConnection(each.getMasterDataSourceName()).getMetaData();%0A            %7D%0A        %7D%0A        throw new UnsupportedOperationException
 ();%0A

Refactor ShardingConnection to get the master datasource's metadata.