Search in sources :

Example 6 with DatabaseProtocolPacket

use of io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket in project sharding-jdbc by shardingjdbc.

the class SQLExecuteBackendHandler method buildPackets.

private List<DatabaseProtocolPacket> buildPackets(final List<List<DatabaseProtocolPacket>> packets, final MergedResult mergedResult) {
    List<DatabaseProtocolPacket> result = new LinkedList<>();
    Iterator<DatabaseProtocolPacket> databaseProtocolPacketsSampling = packets.iterator().next().iterator();
    FieldCountPacket fieldCountPacketSampling = (FieldCountPacket) databaseProtocolPacketsSampling.next();
    result.add(fieldCountPacketSampling);
    int columnCount = fieldCountPacketSampling.getColumnCount();
    for (int i = 0; i < columnCount; i++) {
        result.add(databaseProtocolPacketsSampling.next());
    }
    result.add(databaseProtocolPacketsSampling.next());
    int currentSequenceId = result.size();
    try {
        while (mergedResult.next()) {
            List<Object> data = new ArrayList<>(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                data.add(mergedResult.getValue(i, Object.class));
            }
            result.add(new TextResultSetRowPacket(++currentSequenceId, data));
        }
    } catch (final SQLException ex) {
        return Collections.<DatabaseProtocolPacket>singletonList(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage()));
    }
    result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
    return result;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ErrPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) LinkedList(java.util.LinkedList) TextResultSetRowPacket(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket) FieldCountPacket(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.FieldCountPacket) EofPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket)

Example 7 with DatabaseProtocolPacket

use of io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket in project sharding-jdbc by shardingjdbc.

the class SQLExecuteBackendHandler method getDatabaseProtocolPackets.

private List<DatabaseProtocolPacket> getDatabaseProtocolPackets(final ResultSet resultSet) throws SQLException {
    List<DatabaseProtocolPacket> result = new LinkedList<>();
    int currentSequenceId = 0;
    ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
    int columnCount = resultSetMetaData.getColumnCount();
    if (0 == columnCount) {
        result.add(new OKPacket(++currentSequenceId, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
        return result;
    }
    result.add(new FieldCountPacket(++currentSequenceId, columnCount));
    for (int i = 1; i <= columnCount; i++) {
        result.add(new ColumnDefinition41Packet(++currentSequenceId, resultSetMetaData.getSchemaName(i), resultSetMetaData.getTableName(i), resultSetMetaData.getTableName(i), resultSetMetaData.getColumnLabel(i), resultSetMetaData.getColumnName(i), resultSetMetaData.getColumnDisplaySize(i), ColumnType.valueOfJDBCType(resultSetMetaData.getColumnType(i)), 0));
    }
    result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
    while (resultSet.next()) {
        List<Object> data = new ArrayList<>(columnCount);
        for (int i = 1; i <= columnCount; i++) {
            data.add(resultSet.getObject(i));
        }
        result.add(new TextResultSetRowPacket(++currentSequenceId, data));
    }
    result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
    return result;
}
Also used : ColumnDefinition41Packet(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet) ArrayList(java.util.ArrayList) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) LinkedList(java.util.LinkedList) ResultSetMetaData(java.sql.ResultSetMetaData) TextResultSetRowPacket(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket) FieldCountPacket(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.FieldCountPacket) OKPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket) EofPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket)

Example 8 with DatabaseProtocolPacket

use of io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket in project sharding-jdbc by shardingjdbc.

the class MySQLFrontendHandler method executeCommand.

@Override
protected void executeCommand(final ChannelHandlerContext context, final ByteBuf message) {
    MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message);
    int sequenceId = mysqlPacketPayload.readInt1();
    CommandPacket commandPacket = CommandPacketFactory.getCommandPacket(sequenceId, mysqlPacketPayload);
    for (DatabaseProtocolPacket each : commandPacket.execute()) {
        context.write(each);
    }
    context.flush();
}
Also used : MySQLPacketPayload(io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) CommandPacket(io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacket)

Aggregations

DatabaseProtocolPacket (io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket)8 OKPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket)4 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 EofPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket)3 ErrPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket)3 SQLException (java.sql.SQLException)3 SQLStatement (io.shardingjdbc.core.parsing.parser.sql.SQLStatement)2 ColumnDefinition41Packet (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet)2 FieldCountPacket (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.FieldCountPacket)2 TextResultSetRowPacket (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket)2 MergedResult (io.shardingjdbc.core.merger.MergedResult)1 QueryResult (io.shardingjdbc.core.merger.QueryResult)1 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)1 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)1 SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)1 SQLRouteResult (io.shardingjdbc.core.routing.SQLRouteResult)1 MySQLPacketQueryResult (io.shardingjdbc.proxy.backend.mysql.MySQLPacketQueryResult)1 MySQLPacketPayload (io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload)1 CommandPacket (io.shardingjdbc.proxy.transport.mysql.packet.command.CommandPacket)1