use of io.mycat.net.mysql.ResultSetHeaderPacket in project Mycat-Server by MyCATApache.
the class EngineCtx method writeHeader.
public void writeHeader(List<byte[]> afields) {
if (headerWrited.compareAndSet(false, true)) {
try {
writeLock.lock();
// write new header
ResultSetHeaderPacket headerPkg = new ResultSetHeaderPacket();
// -1;
headerPkg.fieldCount = afields.size();
headerPkg.packetId = incPackageId();
LOGGER.debug("packge id " + headerPkg.packetId);
ServerConnection sc = session.getSource();
ByteBuffer buf = headerPkg.write(sc.allocate(), sc, true);
// wirte a fields
for (byte[] field : afields) {
field[3] = incPackageId();
buf = sc.writeToBuffer(field, buf);
}
// write field eof
EOFPacket eofPckg = new EOFPacket();
eofPckg.packetId = incPackageId();
buf = eofPckg.write(buf, sc, true);
sc.write(buf);
//LOGGER.info("header outputed ,packgId:" + eofPckg.packetId);
} finally {
writeLock.unlock();
}
}
}
use of io.mycat.net.mysql.ResultSetHeaderPacket in project Mycat-Server by MyCATApache.
the class JDBCConnection method ouputResultSet.
private void ouputResultSet(ServerConnection sc, String sql) throws SQLException {
ResultSet rs = null;
Statement stmt = null;
try {
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
List<FieldPacket> fieldPks = new LinkedList<FieldPacket>();
ResultSetUtil.resultSetToFieldPacket(sc.getCharset(), fieldPks, rs, this.isSpark);
int colunmCount = fieldPks.size();
ByteBuffer byteBuf = sc.allocate();
ResultSetHeaderPacket headerPkg = new ResultSetHeaderPacket();
headerPkg.fieldCount = fieldPks.size();
headerPkg.packetId = ++packetId;
byteBuf = headerPkg.write(byteBuf, sc, true);
byteBuf.flip();
byte[] header = new byte[byteBuf.limit()];
byteBuf.get(header);
byteBuf.clear();
List<byte[]> fields = new ArrayList<byte[]>(fieldPks.size());
Iterator<FieldPacket> itor = fieldPks.iterator();
while (itor.hasNext()) {
FieldPacket curField = itor.next();
curField.packetId = ++packetId;
byteBuf = curField.write(byteBuf, sc, false);
byteBuf.flip();
byte[] field = new byte[byteBuf.limit()];
byteBuf.get(field);
byteBuf.clear();
fields.add(field);
itor.remove();
}
EOFPacket eofPckg = new EOFPacket();
eofPckg.packetId = ++packetId;
byteBuf = eofPckg.write(byteBuf, sc, false);
byteBuf.flip();
byte[] eof = new byte[byteBuf.limit()];
byteBuf.get(eof);
byteBuf.clear();
this.respHandler.fieldEofResponse(header, fields, eof, this);
// output row
while (rs.next()) {
RowDataPacket curRow = new RowDataPacket(colunmCount);
for (int i = 0; i < colunmCount; i++) {
int j = i + 1;
curRow.add(StringUtil.encode(rs.getString(j), sc.getCharset()));
}
curRow.packetId = ++packetId;
byteBuf = curRow.write(byteBuf, sc, false);
byteBuf.flip();
byte[] row = new byte[byteBuf.limit()];
byteBuf.get(row);
byteBuf.clear();
this.respHandler.rowResponse(row, this);
}
// end row
eofPckg = new EOFPacket();
eofPckg.packetId = ++packetId;
byteBuf = eofPckg.write(byteBuf, sc, false);
byteBuf.flip();
eof = new byte[byteBuf.limit()];
byteBuf.get(eof);
sc.recycle(byteBuf);
this.respHandler.rowEofResponse(eof, this);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
}
}
}
}
use of io.mycat.net.mysql.ResultSetHeaderPacket in project Mycat-Server by MyCATApache.
the class PostgreSQLBackendConnectionHandler method doProcessBusinessQuery.
/***************
* 处理简单查询结果 ,每一个查询都是一件 CommandComplete 为结束
* @param con PostgreSQL 后端连接
* @param response
* @param commandComplete
*/
private void doProcessBusinessQuery(PostgreSQLBackendConnection con, SelectResponse response, CommandComplete commandComplete) {
RowDescription rowHd = response.getDescription();
List<FieldPacket> fieldPks = PgPacketApaterUtils.rowDescConvertFieldPacket(rowHd);
List<RowDataPacket> rowDatas = new ArrayList<>();
for (DataRow dataRow : response.getDataRows()) {
rowDatas.add(PgPacketApaterUtils.rowDataConvertRowDataPacket(dataRow));
}
BufferArray bufferArray = MycatServer.getInstance().getBufferPool().allocateArray();
ResultSetHeaderPacket headerPkg = new ResultSetHeaderPacket();
headerPkg.fieldCount = fieldPks.size();
headerPkg.packetId = ++packetId;
headerPkg.write(bufferArray);
byte[] header = bufferArray.writeToByteArrayAndRecycle();
List<byte[]> fields = new ArrayList<byte[]>(fieldPks.size());
Iterator<FieldPacket> itor = fieldPks.iterator();
while (itor.hasNext()) {
bufferArray = MycatServer.getInstance().getBufferPool().allocateArray();
FieldPacket curField = itor.next();
curField.packetId = ++packetId;
curField.write(bufferArray);
byte[] field = bufferArray.writeToByteArrayAndRecycle();
fields.add(field);
itor.remove();
}
bufferArray = MycatServer.getInstance().getBufferPool().allocateArray();
EOFPacket eofPckg = new EOFPacket();
eofPckg.packetId = ++packetId;
eofPckg.write(bufferArray);
byte[] eof = bufferArray.writeToByteArrayAndRecycle();
if (con.getResponseHandler() != null) {
con.getResponseHandler().fieldEofResponse(header, fields, eof, con);
} else {
LOGGER.error("响应句柄为空");
}
// output row
for (RowDataPacket curRow : rowDatas) {
bufferArray = MycatServer.getInstance().getBufferPool().allocateArray();
curRow.packetId = ++packetId;
curRow.write(bufferArray);
byte[] row = bufferArray.writeToByteArrayAndRecycle();
con.getResponseHandler().rowResponse(row, con);
}
// end row
bufferArray = MycatServer.getInstance().getBufferPool().allocateArray();
eofPckg = new EOFPacket();
eofPckg.packetId = ++packetId;
eofPckg.write(bufferArray);
eof = bufferArray.writeToByteArrayAndRecycle();
if (con.getResponseHandler() != null) {
con.getResponseHandler().rowEofResponse(eof, con);
} else {
LOGGER.error("响应句柄为空");
}
}
use of io.mycat.net.mysql.ResultSetHeaderPacket in project Mycat-Server by MyCATApache.
the class ExplainHandler method handle.
public static void handle(String stmt, ServerConnection c, int offset) {
stmt = stmt.substring(offset).trim();
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, true);
// write fields
for (FieldPacket field : fields) {
field.packetId = ++packetId;
buffer = field.write(buffer, c, true);
}
// write eof
EOFPacket eof = new EOFPacket();
eof.packetId = ++packetId;
buffer = eof.write(buffer, c, true);
// write rows
RouteResultsetNode[] rrsn = rrs.getNodes();
for (RouteResultsetNode node : rrsn) {
RowDataPacket row = getRow(node, c.getCharset());
row.packetId = ++packetId;
buffer = row.write(buffer, c, true);
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.packetId = ++packetId;
buffer = lastEof.write(buffer, c, true);
// post write
c.write(buffer);
}
use of io.mycat.net.mysql.ResultSetHeaderPacket in project Mycat-Server by MyCATApache.
the class MysqlInformationSchemaHandler method doWrite.
/**
* 写入数据包
* @param field_count
* @param fields
* @param c
*/
private static void doWrite(int field_count, FieldPacket[] fields, ServerConnection c) {
ByteBuffer buffer = c.allocate();
// write header
ResultSetHeaderPacket header = PacketUtil.getHeader(field_count);
byte packetId = header.packetId;
buffer = header.write(buffer, c, true);
// write fields
for (FieldPacket field : fields) {
field.packetId = ++packetId;
buffer = field.write(buffer, c, true);
}
// write eof
EOFPacket eof = new EOFPacket();
eof.packetId = ++packetId;
buffer = eof.write(buffer, c, true);
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.packetId = ++packetId;
buffer = lastEof.write(buffer, c, true);
// post write
c.write(buffer);
}
Aggregations