Search in sources :

Example 41 with FieldPacket

use of io.mycat.net.mysql.FieldPacket 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) {
            }
        }
    }
}
Also used : ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 42 with FieldPacket

use of io.mycat.net.mysql.FieldPacket in project Mycat-Server by MyCATApache.

the class ShowVariables method justReturnValue.

public static void justReturnValue(ServerConnection c, String value) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    if (value != null) {
        RowDataPacket row = new RowDataPacket(1);
        row.add(StringUtil.encode(value, c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write lastEof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 43 with FieldPacket

use of io.mycat.net.mysql.FieldPacket in project Mycat-Server by MyCATApache.

the class ShowVariables method execute.

public static void execute(ServerConnection c, String sql) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    List<String> variableList = parseVariable(sql);
    for (String key : variableList) {
        String value = variables.get(key);
        if (value != null) {
            RowDataPacket row = getRow(key, value, c.getCharset());
            row.packetId = ++packetId;
            buffer = row.write(buffer, c, true);
        }
    }
    // write lastEof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 44 with FieldPacket

use of io.mycat.net.mysql.FieldPacket 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("响应句柄为空");
    }
}
Also used : ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) ArrayList(java.util.ArrayList) EOFPacket(io.mycat.net.mysql.EOFPacket) DataRow(io.mycat.backend.postgresql.packet.DataRow) RowDescription(io.mycat.backend.postgresql.packet.RowDescription) BufferArray(io.mycat.buffer.BufferArray) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 45 with FieldPacket

use of io.mycat.net.mysql.FieldPacket in project Mycat-Server by MyCATApache.

the class ShareRowOutPutDataHandler method getFieldIndex.

public int getFieldIndex(List<byte[]> fields, String fkey) {
    int i = 0;
    for (byte[] field : fields) {
        FieldPacket fieldPacket = new FieldPacket();
        fieldPacket.read(field);
        if (ByteUtil.getString(fieldPacket.name).equals(fkey)) {
            joinKeyType = fieldPacket.type;
            return i;
        }
        i++;
    }
    return i;
}
Also used : FieldPacket(io.mycat.net.mysql.FieldPacket)

Aggregations

FieldPacket (io.mycat.net.mysql.FieldPacket)84 ByteBuffer (java.nio.ByteBuffer)75 EOFPacket (io.mycat.net.mysql.EOFPacket)74 RowDataPacket (io.mycat.net.mysql.RowDataPacket)69 ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)9 NIOProcessor (io.mycat.net.NIOProcessor)7 UserStat (io.mycat.statistic.stat.UserStat)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 SchemaConfig (io.mycat.config.model.SchemaConfig)4 MycatConfig (io.mycat.config.MycatConfig)3 FrontendConnection (io.mycat.net.FrontendConnection)3 BackendConnection (io.mycat.backend.BackendConnection)2 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)2 UserConfig (io.mycat.config.model.UserConfig)2 ServerConnection (io.mycat.server.ServerConnection)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2