Search in sources :

Example 1 with OKPacket

use of io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket in project sharding-jdbc by shardingjdbc.

the class SQLExecuteBackendHandler method execute.

@Override
public List<DatabaseProtocolPacket> execute() {
    SQLRouteResult routeResult = routingEngine.route(sql);
    if (routeResult.getExecutionUnits().isEmpty()) {
        return Collections.<DatabaseProtocolPacket>singletonList(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
    }
    List<List<DatabaseProtocolPacket>> result = new LinkedList<>();
    for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
        // TODO multiple threads
        result.add(execute(routeResult.getSqlStatement(), each));
    }
    return merge(routeResult.getSqlStatement(), result);
}
Also used : SQLRouteResult(io.shardingjdbc.core.routing.SQLRouteResult) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) OKPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket) LinkedList(java.util.LinkedList)

Example 2 with OKPacket

use of io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket in project sharding-jdbc by shardingjdbc.

the class SQLExecuteBackendHandler method mergeDML.

private List<DatabaseProtocolPacket> mergeDML(final List<DatabaseProtocolPacket> firstPackets) {
    int affectedRows = 0;
    for (DatabaseProtocolPacket each : firstPackets) {
        if (each instanceof OKPacket) {
            OKPacket okPacket = (OKPacket) each;
            affectedRows += okPacket.getAffectedRows();
        }
    }
    return Collections.<DatabaseProtocolPacket>singletonList(new OKPacket(1, affectedRows, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
}
Also used : DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) OKPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket)

Example 3 with OKPacket

use of io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket in project sharding-jdbc by shardingjdbc.

the class MySQLFrontendHandler method auth.

@Override
protected void auth(final ChannelHandlerContext context, final ByteBuf message) {
    MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message);
    // TODO use authPluginData to auth
    HandshakeResponse41Packet response41 = new HandshakeResponse41Packet(mysqlPacketPayload);
    context.writeAndFlush(new OKPacket(response41.getSequenceId() + 1, 0L, 0L, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
}
Also used : HandshakeResponse41Packet(io.shardingjdbc.proxy.transport.mysql.packet.handshake.HandshakeResponse41Packet) MySQLPacketPayload(io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload) OKPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket)

Example 4 with OKPacket

use of io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket in project sharding-jdbc by shardingjdbc.

the class SQLExecuteBackendHandler method executeUpdate.

private List<DatabaseProtocolPacket> executeUpdate(final DataSource dataSource, final String sql, final SQLStatement sqlStatement) {
    try (Connection conn = dataSource.getConnection();
        Statement statement = conn.createStatement()) {
        int affectedRows;
        long lastInsertId = 0;
        if (sqlStatement instanceof InsertStatement) {
            affectedRows = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
            lastInsertId = getGeneratedKey(statement);
        } else {
            affectedRows = statement.executeUpdate(sql);
        }
        return Collections.<DatabaseProtocolPacket>singletonList(new OKPacket(1, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
    } catch (final SQLException ex) {
        return Collections.<DatabaseProtocolPacket>singletonList(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage()));
    }
}
Also used : SQLException(java.sql.SQLException) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ErrPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) OKPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)

Example 5 with OKPacket

use of io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket 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)

Aggregations

OKPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket)5 DatabaseProtocolPacket (io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket)4 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 SQLStatement (io.shardingjdbc.core.parsing.parser.sql.SQLStatement)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 MySQLPacketPayload (io.shardingjdbc.proxy.transport.mysql.packet.MySQLPacketPayload)1 ColumnDefinition41Packet (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet)1 FieldCountPacket (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.FieldCountPacket)1 TextResultSetRowPacket (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket)1 EofPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket)1 ErrPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket)1 HandshakeResponse41Packet (io.shardingjdbc.proxy.transport.mysql.packet.handshake.HandshakeResponse41Packet)1 Connection (java.sql.Connection)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 List (java.util.List)1