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

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.constant.SQLType;
import io.shardingsphere.core.executor.sql.execute.SQLExecuteCallback;
import io.shardingsphere.core.executor.sql.execute.SQLExecuteTemplate;
import io.shardingsphere.core.executor.sql.prepare.SQLExecutePrepareTemplate;
import io.shardingsphere.core.jdbc.core.connection.ShardingConnection;
import io.shardingsphere.core.routing.BatchRouteUnit;
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;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Abstract statement executor.
 *
 * @author panjuan
 */
public abstract class AbstractStatementExecutor {
    
    private final DatabaseType databaseType;
    
    private SQLType sqlType;
    
    private int batchCount;
    
    private final int resultSetType;
    
    private final int resultSetConcurrency;
    
    private final int resultSetHoldability;
    
    private final boolean returnGeneratedKeys;
    
    private final ShardingConnection connection;
    
    private final Collection<BatchRouteUnit> routeUnits = new LinkedList<>();
    
    private final SQLExecutePrepareTemplate sqlExecutePrepareTemplate;
    
    private final SQLExecuteTemplate sqlExecuteTemplate;
    
    @Getter
    private final List<ResultSet> resultSets = new CopyOnWriteArrayList<>();
    
    private final Collection<Connection> connections = new LinkedList<>();
    
    private final Collection<ShardingExecuteGroup<StatementExecuteUnit>> executeGroups = new LinkedList<>();
    
    public AbstractStatementExecutor(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability, final boolean returnGeneratedKeys,
                                     final ShardingConnection shardingConnection) {
        this.databaseType = shardingConnection.getShardingDataSource().getShardingContext().getDatabaseType();
        this.resultSetType = resultSetType;
        this.resultSetConcurrency = resultSetConcurrency;
        this.resultSetHoldability = resultSetHoldability;
        this.returnGeneratedKeys = returnGeneratedKeys;
        this.connection = shardingConnection;
        sqlExecuteTemplate = new SQLExecuteTemplate(connection.getShardingDataSource().getShardingContext().getExecuteEngine());
        sqlExecutePrepareTemplate = new SQLExecutePrepareTemplate(connection.getShardingDataSource().getShardingContext().getMaxConnectionsSizePerQuery());
    }
    
    /**
     * Initialize executor.
     *
     * @exception SQLException sql exception
     */
    public void init() throws SQLException {
        executeGroups.addAll(obtainExecuteGroups(routeUnits));
    }
    
    protected abstract Collection<ShardingExecuteGroup<StatementExecuteUnit>> obtainExecuteGroups(final Collection<BatchRouteUnit> routeUnits) throws SQLException;
    
    @SuppressWarnings("unchecked")
    private <T> List<T> executeCallback(final SQLExecuteCallback<T> executeCallback) throws SQLException {
        return sqlExecuteTemplate.executeGroup((Collection) executeGroups, executeCallback);
    }
    
    /**
     * Get statements.
     *
     * @return statements
     */
    public List<Statement> getStatements() {
        List<Statement> result = new LinkedList<>();
        for (ShardingExecuteGroup<StatementExecuteUnit> each : executeGroups) {
            result.addAll(Lists.transform(each.getInputs(), new Function<StatementExecuteUnit, Statement>() {
                
                @Override
                public Statement apply(final StatementExecuteUnit input) {
                    return input.getStatement();
                }
            }));
        }
        return result;
    }
    
    /**
     * Get parameter sets.
     *
     * @param statement statement
     * @return parameter sets
     */
    public List<List<Object>> getParameterSet(final Statement statement) {
        Optional<StatementExecuteUnit> target;
        List<List<Object>> result = new LinkedList<>();
        for (ShardingExecuteGroup<StatementExecuteUnit> each : executeGroups) {
            target = Iterators.tryFind(each.getInputs().iterator(), new Predicate<StatementExecuteUnit>() {
                @Override
                public boolean apply(final StatementExecuteUnit input) {
                    return input.getStatement().equals(statement);
                }
            });
            if (target.isPresent()) {
                result.addAll(target.get().getRouteUnit().getSqlUnit().getParameterSets());
                break;
            }
        }
        return result;
    }
    
    /**
     * Clear data.
     *
     * @throws SQLException sql exception
     */
    public void clear() throws SQLException {
        clearStatements();
        clearConnections();
        batchCount = 0;
        connections.clear();
        routeUnits.clear();
        resultSets.clear();
        executeGroups.clear();
    }
    
    private void clearStatements() throws SQLException {
        for (Statement each : getStatements()) {
            each.close();
        }
    }
    
    private void clearConnections() {
        for (Connection each : connections) {
            connection.release(each);
        }
    }
}



@@ -659,213 +659,8 @@
 r;%0A%0A
-import com.google.common.base.Function;%0Aimport com.google.common.base.Optional;%0Aimport com.google.common.base.Predicate;%0Aimport com.google.common.collect.Iterators;%0Aimport com.google.common.collect.Lists;%0A
 impo
@@ -1573,41 +1573,8 @@
    %0A
-    private int batchCount;%0A    %0A
     
@@ -3551,22 +3551,16 @@
 eGroups(
-final 
 Collecti
@@ -3657,21 +3657,24 @@
 )%0A    pr
-ivate
+otected 
  %3CT%3E Lis
@@ -3952,1416 +3952,49 @@
 lic 
-List%3CStatement%3E getStatements() %7B%0A        List%3CStatement%3E result = new LinkedList%3C%3E();%0A        for (ShardingExecuteGroup%3CStatementExecuteUnit%3E each : executeGroups) %7B%0A            result.addAll(Lists.transform(each.getInputs(), new Function%3CStatementExecuteUnit, Statement%3E() %7B%0A                %0A                @Override%0A                public Statement apply(final StatementExecuteUnit input) %7B%0A                    return input.getStatement();%0A                %7D%0A            %7D));%0A        %7D%0A        return result;%0A    %7D%0A    %0A    /**%0A     * Get parameter sets.%0A     *%0A     * @param statement statement%0A     * @return parameter sets%0A     */%0A    public List%3CList%3CObject%3E%3E getParameterSet(final Statement statement) %7B%0A        Optional%3CStatementExecuteUnit%3E target;%0A        List%3CList%3CObject%3E%3E result = new LinkedList%3C%3E();%0A        for (ShardingExecuteGroup%3CStatementExecuteUnit%3E each : executeGroups) %7B%0A            target = Iterators.tryFind(each.getInputs().iterator(), new Predicate%3CStatementExecuteUnit%3E() %7B%0A                @Override%0A                public boolean apply(final StatementExecuteUnit input) %7B%0A                    return input.getStatement().equals(statement);%0A                %7D%0A            %7D);%0A            if (target.isPresent()) %7B%0A                result.addAll(target.get().getRouteUnit().getSqlUnit().getParameterSets());%0A                break;%0A            %7D%0A        %7D%0A        return result;%0A    %7D
+abstract List%3CStatement%3E getStatements();
 %0A   
@@ -4184,32 +4184,8 @@
 ();%0A
-        batchCount = 0;%0A
     

delete private int batchCount;