/*
 * 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.proxy.backend.common.jdbc.execute.response;

import io.shardingsphere.proxy.backend.common.jdbc.execute.response.unit.ExecuteResponseUnit;
import io.shardingsphere.proxy.backend.common.jdbc.execute.response.unit.ExecuteUpdateResponseUnit;
import io.shardingsphere.proxy.transport.common.packet.DatabasePacket;
import io.shardingsphere.proxy.transport.mysql.packet.command.reponse.CommandResponsePackets;
import io.shardingsphere.proxy.transport.mysql.packet.generic.OKPacket;
import lombok.Getter;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * Execute update response.
 * 
 * @author zhangliang
 */
public final class ExecuteUpdateResponse implements ExecuteResponse {
    
    @Getter
    private final List<DatabasePacket> packets = new LinkedList<>();
    
    public ExecuteUpdateResponse(final DatabasePacket packet) {
        packets.add(packet);
    }
    
    public ExecuteUpdateResponse(final Collection<ExecuteResponseUnit> responseUnits) {
        for (ExecuteResponseUnit each : responseUnits) {
            packets.add(((ExecuteUpdateResponseUnit) each).getOkPacket());
        }
    }
    
    /**
     * Merge packets.
     * 
     * @return merged packet.
     */
    public CommandResponsePackets merge() {
        int affectedRows = 0;
        long lastInsertId = 0;
        for (DatabasePacket each : packets) {
            if (each instanceof OKPacket) {
                OKPacket okPacket = (OKPacket) each;
                affectedRows += okPacket.getAffectedRows();
                // TODO consider about insert multiple values
                lastInsertId = okPacket.getLastInsertId();
            }
        }
        return new CommandResponsePackets(new OKPacket(1, affectedRows, lastInsertId));
    }
}

@@ -882,79 +882,8 @@
 it;%0A
-import io.shardingsphere.proxy.transport.common.packet.DatabasePacket;%0A
 impo
@@ -1321,24 +1321,18 @@
 al List%3C
-Database
+OK
 Packet%3E 
@@ -1405,24 +1405,18 @@
 e(final 
-Database
+OK
 Packet p
@@ -1903,16 +1903,10 @@
 or (
-Database
+OK
 Pack
@@ -1930,109 +1930,8 @@
 ) %7B%0A
-            if (each instanceof OKPacket) %7B%0A                OKPacket okPacket = (OKPacket) each;%0A    
     
@@ -1954,24 +1954,20 @@
 Rows += 
-okPacket
+each
 .getAffe
@@ -1974,28 +1974,24 @@
 ctedRows();%0A
-    
             
@@ -2048,20 +2048,16 @@
         
-    
 lastInse
@@ -2067,16 +2067,12 @@
 d = 
-okPacket
+each
 .get
@@ -2091,22 +2091,8 @@
 ();%0A
-            %7D%0A
     

refactor ExecuteUpdateResponse