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);
}
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();
}
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;
}
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());
}
Aggregations