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);
}
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;
}
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);
}
Aggregations