package com.alibaba.otter.canal.parse.driver.mysql;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.otter.canal.parse.driver.mysql.packets.client.QueryCommandPacket;
import com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket;
import com.alibaba.otter.canal.parse.driver.mysql.packets.server.OKPacket;
import com.alibaba.otter.canal.parse.driver.mysql.socket.SocketChannel;
import com.alibaba.otter.canal.parse.driver.mysql.utils.PacketManager;

/**
 * 默认输出的数据编码为UTF-8，如有需要请正确转码
 * 
 * @author jianghang 2013-9-4 上午11:51:11
 * @since 1.0.0
 */
public class MysqlUpdateExecutor {

    private static final Logger logger = LoggerFactory.getLogger(MysqlUpdateExecutor.class);

    private SocketChannel       channel;

    public MysqlUpdateExecutor(MysqlConnector connector){
        if (!connector.isConnected()) {
            throw new RuntimeException("should execute connector.connect() first");
        }

        this.channel = connector.getChannel();
    }

    public MysqlUpdateExecutor(SocketChannel ch){
        this.channel = ch;
    }

    public OKPacket update(String updateString) throws IOException {
        QueryCommandPacket cmd = new QueryCommandPacket();
        cmd.setQueryString(updateString);
        byte[] bodyBytes = cmd.toBytes();
        PacketManager.write(channel, bodyBytes);

        logger.debug("read update result...");
        byte[] body = PacketManager.readBytes(channel, PacketManager.readHeader(channel, 4).getPacketBodyLength());
        if (body[0] < 0) {
            ErrorPacket packet = new ErrorPacket();
            packet.fromBytes(body);
            throw new IOException(packet + "\n with command: " + updateString);
        }
        //channel.lock();//锁定读

        OKPacket packet = new OKPacket();
        packet.fromBytes(body);
        return packet;
    }
}

@@ -76,16 +76,89 @@
 eption;%0D
+%0Aimport com.alibaba.otter.canal.parse.driver.mysql.socket.SocketChannel;%0D
 %0A%0D%0Aimpor
@@ -458,81 +458,8 @@
 t;%0D%0A
-import com.alibaba.otter.canal.parse.driver.mysql.socket.SocketChannel;%0D%0A
 impo
@@ -782,29 +782,30 @@
 ate 
-SocketChannel
+MysqlConnector
        c
 hann
@@ -800,22 +800,24 @@
        c
-hannel
+onnector
 ;%0D%0A%0D%0A   
@@ -1021,30 +1021,32 @@
       this.c
-hannel
+onnector
  = connector
@@ -1049,21 +1049,8 @@
 ctor
-.getChannel()
 ;%0D%0A 
@@ -1052,32 +1052,34 @@
 r;%0D%0A    %7D%0D%0A%0D%0A   
+/*
  public MysqlUpd
@@ -1139,24 +1139,26 @@
 = ch;%0D%0A    %7D
+*/
 %0D%0A%0D%0A    publ
@@ -1392,24 +1392,43 @@
 er.write
-(c
+Body(connector.getC
 hannel
+()
 , bodyBy
@@ -1531,22 +1531,37 @@
 dBytes(c
+onnector.getC
 hannel
+()
 , Packet
@@ -1580,22 +1580,37 @@
 Header(c
+onnector.getC
 hannel
+()
 , 4).get
@@ -1844,40 +1844,8 @@
   %7D%0D
-%0A        //channel.lock();//%E9%94%81%E5%AE%9A%E8%AF%BB%0D
 %0A%0D%0A 

Update MysqlUpdateExecutor.java