Search in sources :

Example 1 with QueryCommandPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.client.QueryCommandPacket in project canal by alibaba.

the class MysqlQueryExecutor method query.

/**
     * (Result Set Header Packet) the number of columns <br>
     * (Field Packets) column descriptors <br>
     * (EOF Packet) marker: end of Field Packets <br>
     * (Row Data Packets) row contents <br>
     * (EOF Packet) marker: end of Data Packets
     * 
     * @param queryString
     * @return
     * @throws IOException
     */
public ResultSetPacket query(String queryString) throws IOException {
    QueryCommandPacket cmd = new QueryCommandPacket();
    cmd.setQueryString(queryString);
    byte[] bodyBytes = cmd.toBytes();
    PacketManager.write(channel, bodyBytes);
    byte[] body = readNextPacket();
    if (body[0] < 0) {
        ErrorPacket packet = new ErrorPacket();
        packet.fromBytes(body);
        throw new IOException(packet + "\n with command: " + queryString);
    }
    ResultSetHeaderPacket rsHeader = new ResultSetHeaderPacket();
    rsHeader.fromBytes(body);
    List<FieldPacket> fields = new ArrayList<FieldPacket>();
    for (int i = 0; i < rsHeader.getColumnCount(); i++) {
        FieldPacket fp = new FieldPacket();
        fp.fromBytes(readNextPacket());
        fields.add(fp);
    }
    readEofPacket();
    List<RowDataPacket> rowData = new ArrayList<RowDataPacket>();
    while (true) {
        body = readNextPacket();
        if (body[0] == -2) {
            break;
        }
        RowDataPacket rowDataPacket = new RowDataPacket();
        rowDataPacket.fromBytes(body);
        rowData.add(rowDataPacket);
    }
    ResultSetPacket resultSet = new ResultSetPacket();
    resultSet.getFieldDescriptors().addAll(fields);
    for (RowDataPacket r : rowData) {
        resultSet.getFieldValues().addAll(r.getColumns());
    }
    resultSet.setSourceAddress(channel.socket().getRemoteSocketAddress());
    return resultSet;
}
Also used : ResultSetHeaderPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetHeaderPacket) QueryCommandPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.client.QueryCommandPacket) ErrorPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket) RowDataPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.RowDataPacket) ResultSetPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FieldPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.FieldPacket)

Example 2 with QueryCommandPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.client.QueryCommandPacket in project canal by alibaba.

the class MysqlUpdateExecutor method update.

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);
    }
    OKPacket packet = new OKPacket();
    packet.fromBytes(body);
    return packet;
}
Also used : QueryCommandPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.client.QueryCommandPacket) ErrorPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket) IOException(java.io.IOException) OKPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.OKPacket)

Aggregations

QueryCommandPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.client.QueryCommandPacket)2 ErrorPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket)2 IOException (java.io.IOException)2 FieldPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.FieldPacket)1 OKPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.OKPacket)1 ResultSetHeaderPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetHeaderPacket)1 ResultSetPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket)1 RowDataPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.RowDataPacket)1 ArrayList (java.util.ArrayList)1