Search in sources :

Example 1 with ResultSetHeaderPacket

use of com.alibaba.cobar.net.mysql.ResultSetHeaderPacket in project cobar by alibaba.

the class ExplainHandler method handle.

public static void handle(String stmt, ServerConnection c, int offset) {
    stmt = stmt.substring(offset);
    RouteResultset rrs = getRouteResultset(c, stmt);
    if (rrs == null)
        return;
    ByteBuffer buffer = c.allocate();
    // write header
    ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
    byte packetId = header.packetId;
    buffer = header.write(buffer, c);
    // write fields
    for (FieldPacket field : fields) {
        field.packetId = ++packetId;
        buffer = field.write(buffer, c);
    }
    // write eof
    EOFPacket eof = new EOFPacket();
    eof.packetId = ++packetId;
    buffer = eof.write(buffer, c);
    // write rows
    RouteResultsetNode[] rrsn = (rrs != null) ? rrs.getNodes() : EMPTY_ARRAY;
    for (RouteResultsetNode node : rrsn) {
        RowDataPacket row = getRow(node, c.getCharset());
        row.packetId = ++packetId;
        buffer = row.write(buffer, c);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c);
    // post write
    c.write(buffer);
}
Also used : ResultSetHeaderPacket(com.alibaba.cobar.net.mysql.ResultSetHeaderPacket) RouteResultsetNode(com.alibaba.cobar.route.RouteResultsetNode) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket) RouteResultset(com.alibaba.cobar.route.RouteResultset)

Example 2 with ResultSetHeaderPacket

use of com.alibaba.cobar.net.mysql.ResultSetHeaderPacket in project cobar by alibaba.

the class PacketUtil method getHeader.

public static final ResultSetHeaderPacket getHeader(int fieldCount) {
    ResultSetHeaderPacket packet = new ResultSetHeaderPacket();
    packet.packetId = 1;
    packet.fieldCount = fieldCount;
    return packet;
}
Also used : ResultSetHeaderPacket(com.alibaba.cobar.net.mysql.ResultSetHeaderPacket)

Example 3 with ResultSetHeaderPacket

use of com.alibaba.cobar.net.mysql.ResultSetHeaderPacket in project cobar by alibaba.

the class SampleResponseHandler method response.

public static void response(SampleConnection c, String message) {
    byte packetId = 0;
    ByteBuffer buffer = c.allocate();
    // header
    ResultSetHeaderPacket header = new ResultSetHeaderPacket();
    header.packetId = ++packetId;
    header.fieldCount = 1;
    buffer = header.write(buffer, c);
    // fields
    FieldPacket[] fields = new FieldPacket[header.fieldCount];
    for (FieldPacket field : fields) {
        field = new FieldPacket();
        field.packetId = ++packetId;
        field.charsetIndex = CharsetUtil.getIndex("Cp1252");
        field.name = "SampleServer".getBytes();
        field.type = Fields.FIELD_TYPE_VAR_STRING;
        buffer = field.write(buffer, c);
    }
    // eof
    EOFPacket eof = new EOFPacket();
    eof.packetId = ++packetId;
    buffer = eof.write(buffer, c);
    // rows
    RowDataPacket row = new RowDataPacket(header.fieldCount);
    row.add(message != null ? encode(message, c.getCharset()) : encode("HelloWorld!", c.getCharset()));
    row.packetId = ++packetId;
    buffer = row.write(buffer, c);
    // write lastEof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c);
    // write buffer
    c.write(buffer);
}
Also used : ResultSetHeaderPacket(com.alibaba.cobar.net.mysql.ResultSetHeaderPacket) RowDataPacket(com.alibaba.cobar.net.mysql.RowDataPacket) EOFPacket(com.alibaba.cobar.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(com.alibaba.cobar.net.mysql.FieldPacket)

Aggregations

ResultSetHeaderPacket (com.alibaba.cobar.net.mysql.ResultSetHeaderPacket)3 EOFPacket (com.alibaba.cobar.net.mysql.EOFPacket)2 FieldPacket (com.alibaba.cobar.net.mysql.FieldPacket)2 RowDataPacket (com.alibaba.cobar.net.mysql.RowDataPacket)2 ByteBuffer (java.nio.ByteBuffer)2 RouteResultset (com.alibaba.cobar.route.RouteResultset)1 RouteResultsetNode (com.alibaba.cobar.route.RouteResultsetNode)1