Search in sources :

Example 6 with QueryResult

use of io.shardingjdbc.core.merger.QueryResult 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 7 with QueryResult

use of io.shardingjdbc.core.merger.QueryResult in project sharding-jdbc by shardingjdbc.

the class ShowTablesMergedResult method init.

private Iterator<MemoryQueryResultRow> init(final List<QueryResult> queryResults) throws SQLException {
    List<MemoryQueryResultRow> result = new LinkedList<>();
    for (QueryResult each : queryResults) {
        while (each.next()) {
            MemoryQueryResultRow memoryResultSetRow = new MemoryQueryResultRow(each);
            String actualTableName = memoryResultSetRow.getCell(1).toString();
            Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByActualTable(actualTableName);
            if (!tableRule.isPresent()) {
                result.add(memoryResultSetRow);
            } else if (tableNames.add(tableRule.get().getLogicTable())) {
                memoryResultSetRow.setCell(1, tableRule.get().getLogicTable());
                result.add(memoryResultSetRow);
            }
        }
    }
    if (!result.isEmpty()) {
        setCurrentResultSetRow(result.get(0));
    }
    return result.iterator();
}
Also used : TableRule(io.shardingjdbc.core.rule.TableRule) QueryResult(io.shardingjdbc.core.merger.QueryResult) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) LinkedList(java.util.LinkedList)

Example 8 with QueryResult

use of io.shardingjdbc.core.merger.QueryResult in project sharding-jdbc by shardingjdbc.

the class ShardingPreparedStatement method getResultSet.

@Override
public ResultSet getResultSet() throws SQLException {
    if (null != currentResultSet) {
        return currentResultSet;
    }
    if (1 == routedStatements.size() && routeResult.getSqlStatement() instanceof DQLStatement) {
        currentResultSet = routedStatements.iterator().next().getResultSet();
        return currentResultSet;
    }
    List<ResultSet> resultSets = new ArrayList<>(routedStatements.size());
    List<QueryResult> queryResults = new ArrayList<>(routedStatements.size());
    for (PreparedStatement each : routedStatements) {
        ResultSet resultSet = each.getResultSet();
        resultSets.add(resultSet);
        queryResults.add(new JDBCQueryResult(resultSet));
    }
    if (routeResult.getSqlStatement() instanceof SelectStatement || routeResult.getSqlStatement() instanceof DALStatement) {
        MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getShardingContext().getShardingRule(), queryResults, routeResult.getSqlStatement());
        currentResultSet = new ShardingResultSet(resultSets, mergeEngine.merge(), this);
    }
    return currentResultSet;
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) QueryResult(io.shardingjdbc.core.merger.QueryResult) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult) MergeEngine(io.shardingjdbc.core.merger.MergeEngine) DQLStatement(io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement) ShardingResultSet(io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet) ResultSet(java.sql.ResultSet) GeneratedKeysResultSet(io.shardingjdbc.core.jdbc.core.resultset.GeneratedKeysResultSet) ShardingResultSet(io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet) ArrayList(java.util.ArrayList) DALStatement(io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult) PreparedStatement(java.sql.PreparedStatement)

Example 9 with QueryResult

use of io.shardingjdbc.core.merger.QueryResult in project sharding-jdbc by shardingjdbc.

the class IteratorStreamMergedResultTest method assertNextForResultSetsAllNotEmpty.

@Test
public void assertNextForResultSetsAllNotEmpty() throws SQLException {
    for (QueryResult each : queryResults) {
        when(each.next()).thenReturn(true, false);
    }
    mergeEngine = new DQLMergeEngine(queryResults, selectStatement);
    MergedResult actual = mergeEngine.merge();
    assertTrue(actual.next());
    assertTrue(actual.next());
    assertTrue(actual.next());
    assertFalse(actual.next());
}
Also used : QueryResult(io.shardingjdbc.core.merger.QueryResult) TestQueryResult(io.shardingjdbc.core.merger.fixture.TestQueryResult) MergedResult(io.shardingjdbc.core.merger.MergedResult) DQLMergeEngine(io.shardingjdbc.core.merger.dql.DQLMergeEngine) Test(org.junit.Test)

Aggregations

QueryResult (io.shardingjdbc.core.merger.QueryResult)9 ArrayList (java.util.ArrayList)5 GeneratedKeysResultSet (io.shardingjdbc.core.jdbc.core.resultset.GeneratedKeysResultSet)4 ShardingResultSet (io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet)4 JDBCQueryResult (io.shardingjdbc.core.merger.JDBCQueryResult)4 MergeEngine (io.shardingjdbc.core.merger.MergeEngine)4 ResultSet (java.sql.ResultSet)4 MemoryQueryResultRow (io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow)3 MergedResult (io.shardingjdbc.core.merger.MergedResult)2 DALStatement (io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement)2 DQLStatement (io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement)2 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)2 TableRule (io.shardingjdbc.core.rule.TableRule)2 LinkedList (java.util.LinkedList)2 BatchPreparedStatementExecutor (io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor)1 BatchPreparedStatementUnit (io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)1 PreparedStatementExecutor (io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor)1 PreparedStatementUnit (io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit)1 DQLMergeEngine (io.shardingjdbc.core.merger.dql.DQLMergeEngine)1 TestQueryResult (io.shardingjdbc.core.merger.fixture.TestQueryResult)1