/*
 * Copyright 1999-2015 dangdang.com.
 * <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 com.dangdang.ddframe.rdb.sharding.jdbc.core.statement;

import com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractStatementAdapter;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.connection.MasterSlaveConnection;
import com.google.common.base.Preconditions;
import lombok.Getter;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.LinkedList;

/**
 * Statement that support master-slave.
 * 
 * @author zhangliang
 */
@Getter
public final class MasterSlaveStatement extends AbstractStatementAdapter {
    
    private final MasterSlaveConnection connection;
    
    private final int resultSetType;
    
    private final int resultSetConcurrency;
    
    private final int resultSetHoldability;
    
    private final Collection<Statement> routedStatements = new LinkedList<>();
    
    public MasterSlaveStatement(final MasterSlaveConnection connection) {
        this(connection, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
    }
    
    public MasterSlaveStatement(final MasterSlaveConnection connection, final int resultSetType, final int resultSetConcurrency) {
        this(connection, resultSetType, resultSetConcurrency, ResultSet.HOLD_CURSORS_OVER_COMMIT);
    }
    
    public MasterSlaveStatement(final MasterSlaveConnection connection, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
        super(Statement.class);
        this.connection = connection;
        this.resultSetType = resultSetType;
        this.resultSetConcurrency = resultSetConcurrency;
        this.resultSetHoldability = resultSetHoldability;
    }
    
    @Override
    public ResultSet executeQuery(final String sql) throws SQLException {
        return getSingleStatement(sql).executeQuery(sql);
    }
    
    @Override
    public int executeUpdate(final String sql) throws SQLException {
        return getSingleStatement(sql).executeUpdate(sql);
    }
    
    @Override
    public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
        return getSingleStatement(sql).executeUpdate(sql, autoGeneratedKeys);
    }
    
    @Override
    public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
        return getSingleStatement(sql).executeUpdate(sql, columnIndexes);
    }
    
    @Override
    public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
        return getSingleStatement(sql).executeUpdate(sql, columnNames);
    }
    
    private Statement getSingleStatement(final String sql) throws SQLException {
        Collection<Connection> connections = connection.getConnection(sql);
        Preconditions.checkState(1 == connections.size());
        Statement result = connections.iterator().next().createStatement();
        routedStatements.add(result);
        return result;
    }
    
    @Override
    public boolean execute(final String sql) throws SQLException {
        boolean result = false;
        for (Connection each : connection.getConnection(sql)) {
            Statement statement = each.createStatement();
            routedStatements.add(statement);
            result = statement.execute(sql);
        }
        return result;
    }
    
    @Override
    public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
        boolean result = false;
        for (Connection each : connection.getConnection(sql)) {
            Statement statement = each.createStatement();
            routedStatements.add(statement);
            result = statement.execute(sql, autoGeneratedKeys);
        }
        return result;
    }
    
    @Override
    public boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
        boolean result = false;
        for (Connection each : connection.getConnection(sql)) {
            Statement statement = each.createStatement();
            routedStatements.add(statement);
            result = statement.execute(sql, columnIndexes);
        }
        return result;
    }
    
    @Override
    public boolean execute(final String sql, final String[] columnNames) throws SQLException {
        boolean result = false;
        for (Connection each : connection.getConnection(sql)) {
            Statement statement = each.createStatement();
            routedStatements.add(statement);
            result = statement.execute(sql, columnNames);
        }
        return result;
    }
    
    @Override
    public ResultSet getGeneratedKeys() throws SQLException {
        Preconditions.checkState(1 == routedStatements.size());
        return routedStatements.iterator().next().getGeneratedKeys();
    }
    
    @Override
    public ResultSet getResultSet() throws SQLException {
        Preconditions.checkState(1 == routedStatements.size());
        return routedStatements.iterator().next().getResultSet();
    }
}

@@ -3550,35 +3550,92 @@
 createStatement(
+resultSetType, resultSetConcurrency, resultSetHoldability
 );%0A
-
         routedSt
@@ -3914,32 +3914,89 @@
 createStatement(
+resultSetType, resultSetConcurrency, resultSetHoldability
 );%0A            r
@@ -4369,32 +4369,89 @@
 createStatement(
+resultSetType, resultSetConcurrency, resultSetHoldability
 );%0A            r
@@ -4841,32 +4841,89 @@
 createStatement(
+resultSetType, resultSetConcurrency, resultSetHoldability
 );%0A            r
@@ -5255,32 +5255,32 @@
 nection(sql)) %7B%0A
-
             Stat
@@ -5318,16 +5318,73 @@
 atement(
+resultSetType, resultSetConcurrency, resultSetHoldability
 );%0A     

refactor MasterSlaveStatement