Search in sources :

Example 1 with DatabaseProtocolPacket

use of io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket 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 DatabaseProtocolPacket

use of io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket 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 DatabaseProtocolPacket

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

the class SQLExecuteBackendHandler method mergeDQLorDAL.

private List<DatabaseProtocolPacket> mergeDQLorDAL(final SQLStatement sqlStatement, final List<List<DatabaseProtocolPacket>> packets) {
    List<QueryResult> queryResults = new ArrayList<>(packets.size());
    for (List<DatabaseProtocolPacket> each : packets) {
        // TODO replace to a common PacketQueryResult
        queryResults.add(new MySQLPacketQueryResult(each));
    }
    MergedResult mergedResult;
    try {
        mergedResult = MergeEngineFactory.newInstance(ShardingRuleRegistry.getInstance().getShardingRule(), queryResults, sqlStatement).merge();
    } catch (final SQLException ex) {
        return Collections.<DatabaseProtocolPacket>singletonList(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage()));
    }
    return buildPackets(packets, mergedResult);
}
Also used : MySQLPacketQueryResult(io.shardingjdbc.proxy.backend.mysql.MySQLPacketQueryResult) QueryResult(io.shardingjdbc.core.merger.QueryResult) SQLException(java.sql.SQLException) MergedResult(io.shardingjdbc.core.merger.MergedResult) ArrayList(java.util.ArrayList) ErrPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) MySQLPacketQueryResult(io.shardingjdbc.proxy.backend.mysql.MySQLPacketQueryResult)

Example 4 with DatabaseProtocolPacket

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

the class ComStmtPreparePacket method execute.

@Override
public List<DatabaseProtocolPacket> execute() {
    log.debug("COM_STMT_PREPARE received for Sharding-Proxy: {}", sql);
    List<DatabaseProtocolPacket> result = new LinkedList<>();
    int currentSequenceId = 0;
    SQLStatement sqlStatement = new SQLParsingEngine(DatabaseType.MySQL, sql, ShardingRuleRegistry.getInstance().getShardingRule()).parse();
    result.add(new ComStmtPrepareOKPacket(++currentSequenceId, PreparedStatementRegistry.getInstance().register(sql), getNumColumns(sqlStatement), sqlStatement.getParametersIndex(), 0));
    for (int i = 0; i < sqlStatement.getParametersIndex(); i++) {
        // TODO add column name
        result.add(new ColumnDefinition41Packet(++currentSequenceId, ShardingConstant.LOGIC_SCHEMA_NAME, sqlStatement.getTables().isSingleTable() ? sqlStatement.getTables().getSingleTableName() : "", "", "", "", 100, ColumnType.MYSQL_TYPE_VARCHAR, 0));
    }
    if (sqlStatement.getParametersIndex() > 0) {
        result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
    }
    // TODO add If numColumns > 0
    return result;
}
Also used : ColumnDefinition41Packet(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) EofPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket) LinkedList(java.util.LinkedList)

Example 5 with DatabaseProtocolPacket

use of io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket 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)

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