use of com.alibaba.cobar.net.mysql.FieldPacket 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.FieldPacket in project cobar by alibaba.
the class PacketUtil method getField.
public static final FieldPacket getField(String name, int type) {
FieldPacket packet = new FieldPacket();
packet.charsetIndex = CharsetUtil.getIndex(CODE_PAGE_1252);
packet.name = encode(name, CODE_PAGE_1252);
packet.type = (byte) type;
return packet;
}
use of com.alibaba.cobar.net.mysql.FieldPacket in project cobar by alibaba.
the class MultiNodeExecutor method execute0.
/**
* 执行
*/
private void execute0(RouteResultsetNode rrn, Channel c, boolean autocommit, BlockingSession ss, int flag) {
ServerConnection sc = ss.getSource();
if (isFail.get() || sc.isClosed()) {
c.setRunning(false);
handleFailure(ss, rrn, null);
return;
}
try {
// 执行并等待返回
BinaryPacket bin = ((MySQLChannel) c).execute(rrn, sc, autocommit);
// 接收和处理数据
final ReentrantLock lock = MultiNodeExecutor.this.lock;
lock.lock();
try {
switch(bin.data[0]) {
case ErrorPacket.FIELD_COUNT:
c.setRunning(false);
handleFailure(ss, rrn, new BinaryErrInfo((MySQLChannel) c, bin, sc, rrn));
break;
case OkPacket.FIELD_COUNT:
OkPacket ok = new OkPacket();
ok.read(bin);
affectedRows += ok.affectedRows;
// set lastInsertId
if (ok.insertId > 0) {
insertId = (insertId == 0) ? ok.insertId : Math.min(insertId, ok.insertId);
}
c.setRunning(false);
handleSuccessOK(ss, rrn, autocommit, ok);
break;
default:
// HEADER|FIELDS|FIELD_EOF|ROWS|LAST_EOF
final MySQLChannel mc = (MySQLChannel) c;
if (fieldEOF) {
for (; ; ) {
bin = mc.receive();
switch(bin.data[0]) {
case ErrorPacket.FIELD_COUNT:
c.setRunning(false);
handleFailure(ss, rrn, new BinaryErrInfo(mc, bin, sc, rrn));
return;
case EOFPacket.FIELD_COUNT:
handleRowData(rrn, c, ss);
return;
default:
continue;
}
}
} else {
// HEADER
bin.packetId = ++packetId;
List<MySQLPacket> headerList = new LinkedList<MySQLPacket>();
headerList.add(bin);
for (; ; ) {
bin = mc.receive();
switch(bin.data[0]) {
case ErrorPacket.FIELD_COUNT:
c.setRunning(false);
handleFailure(ss, rrn, new BinaryErrInfo(mc, bin, sc, rrn));
return;
case EOFPacket.FIELD_COUNT:
// FIELD_EOF
bin.packetId = ++packetId;
for (MySQLPacket packet : headerList) {
buffer = packet.write(buffer, sc);
}
headerList = null;
buffer = bin.write(buffer, sc);
fieldEOF = true;
handleRowData(rrn, c, ss);
return;
default:
// FIELDS
bin.packetId = ++packetId;
switch(flag) {
case RouteResultset.REWRITE_FIELD:
StringBuilder fieldName = new StringBuilder();
fieldName.append("Tables_in_").append(ss.getSource().getSchema());
FieldPacket field = PacketUtil.getField(bin, fieldName.toString());
headerList.add(field);
break;
default:
headerList.add(bin);
}
}
}
}
}
} finally {
lock.unlock();
}
} catch (final IOException e) {
c.close();
handleFailure(ss, rrn, new SimpleErrInfo(e, ErrorCode.ER_YES, sc, rrn));
} catch (final RuntimeException e) {
c.close();
handleFailure(ss, rrn, new SimpleErrInfo(e, ErrorCode.ER_YES, sc, rrn));
}
}
use of com.alibaba.cobar.net.mysql.FieldPacket in project cobar by alibaba.
the class PacketUtil method getField.
public static final FieldPacket getField(BinaryPacket src, String fieldName) {
FieldPacket field = new FieldPacket();
field.read(src);
field.name = encode(fieldName, CODE_PAGE_1252);
field.packetLength = field.calcPacketSize();
return field;
}
use of com.alibaba.cobar.net.mysql.FieldPacket in project cobar by alibaba.
the class ShowThreadPool method execute.
public static void execute(ManagerConnection c) {
ByteBuffer buffer = c.allocate();
// write header
buffer = header.write(buffer, c);
// write fields
for (FieldPacket field : fields) {
buffer = field.write(buffer, c);
}
// write eof
buffer = eof.write(buffer, c);
// write rows
byte packetId = eof.packetId;
List<NameableExecutor> executors = getExecutors();
for (NameableExecutor exec : executors) {
if (exec != null) {
RowDataPacket row = getRow(exec, 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);
// write buffer
c.write(buffer);
}
Aggregations