Search in sources :

Example 1 with ErrPacket

use of io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket 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 2 with ErrPacket

use of io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket 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 3 with ErrPacket

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

Aggregations

DatabaseProtocolPacket (io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket)3 ErrPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)2 MergedResult (io.shardingjdbc.core.merger.MergedResult)1 QueryResult (io.shardingjdbc.core.merger.QueryResult)1 SQLStatement (io.shardingjdbc.core.parsing.parser.sql.SQLStatement)1 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)1 MySQLPacketQueryResult (io.shardingjdbc.proxy.backend.mysql.MySQLPacketQueryResult)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 OKPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 LinkedList (java.util.LinkedList)1