Search in sources :

Example 1 with QueryResult

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

the class GroupByMemoryMergedResult method init.

private Iterator<MemoryQueryResultRow> init(final List<QueryResult> queryResults) throws SQLException {
    Map<GroupByValue, MemoryQueryResultRow> dataMap = new HashMap<>(1024);
    Map<GroupByValue, Map<AggregationSelectItem, AggregationUnit>> aggregationMap = new HashMap<>(1024);
    for (QueryResult each : queryResults) {
        while (each.next()) {
            GroupByValue groupByValue = new GroupByValue(each, selectStatement.getGroupByItems());
            initForFirstGroupByValue(each, groupByValue, dataMap, aggregationMap);
            aggregate(each, groupByValue, aggregationMap);
        }
    }
    setAggregationValueToMemoryRow(dataMap, aggregationMap);
    List<MemoryQueryResultRow> result = getMemoryResultSetRows(dataMap);
    if (!result.isEmpty()) {
        setCurrentResultSetRow(result.get(0));
    }
    return result.iterator();
}
Also used : QueryResult(io.shardingjdbc.core.merger.QueryResult) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with QueryResult

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

the class ShowCreateTableMergedResult 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 tableName = memoryResultSetRow.getCell(1).toString();
            Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByActualTable(tableName);
            if (tableRule.isPresent()) {
                String logicTableName = tableRule.get().getLogicTable();
                memoryResultSetRow.setCell(1, logicTableName);
                String createTableDDL = memoryResultSetRow.getCell(2).toString();
                SQLParsingEngine sqlParsingEngine = new SQLParsingEngine(DatabaseType.MySQL, createTableDDL, shardingRule);
                String actualTableName = sqlParsingEngine.parse().getTables().getSingleTableName();
                if (actualTableName.startsWith("`")) {
                    logicTableName = "`" + logicTableName + "`";
                }
                memoryResultSetRow.setCell(2, createTableDDL.replaceFirst(actualTableName, logicTableName));
                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) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) LinkedList(java.util.LinkedList)

Example 3 with QueryResult

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

the class ShardingPreparedStatement method executeQuery.

@Override
public ResultSet executeQuery() throws SQLException {
    ResultSet result;
    try {
        Collection<PreparedStatementUnit> preparedStatementUnits = route();
        List<ResultSet> resultSets = new PreparedStatementExecutor(getConnection().getShardingContext().getExecutorEngine(), routeResult.getSqlStatement().getType(), preparedStatementUnits, getParameters()).executeQuery();
        List<QueryResult> queryResults = new ArrayList<>(resultSets.size());
        for (ResultSet each : resultSets) {
            queryResults.add(new JDBCQueryResult(each));
        }
        MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getShardingContext().getShardingRule(), queryResults, routeResult.getSqlStatement());
        result = new ShardingResultSet(resultSets, mergeEngine.merge(), this);
    } finally {
        clearBatch();
    }
    currentResultSet = result;
    return result;
}
Also used : BatchPreparedStatementExecutor(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor) PreparedStatementExecutor(io.shardingjdbc.core.executor.type.prepared.PreparedStatementExecutor) QueryResult(io.shardingjdbc.core.merger.QueryResult) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult) MergeEngine(io.shardingjdbc.core.merger.MergeEngine) 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) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult) PreparedStatementUnit(io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit) BatchPreparedStatementUnit(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)

Example 4 with QueryResult

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

the class ShardingStatement 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 (Statement 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) DQLStatement(io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement) SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) DALStatement(io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement) Statement(java.sql.Statement) 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)

Example 5 with QueryResult

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

the class ShardingStatement method executeQuery.

@Override
public ResultSet executeQuery(final String sql) throws SQLException {
    ResultSet result;
    try {
        List<ResultSet> resultSets = generateExecutor(sql).executeQuery();
        List<QueryResult> queryResults = new ArrayList<>(resultSets.size());
        for (ResultSet each : resultSets) {
            queryResults.add(new JDBCQueryResult(each));
        }
        MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getShardingContext().getShardingRule(), queryResults, routeResult.getSqlStatement());
        result = new ShardingResultSet(resultSets, mergeEngine.merge(), this);
    } finally {
        currentResultSet = null;
    }
    currentResultSet = result;
    return result;
}
Also used : QueryResult(io.shardingjdbc.core.merger.QueryResult) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult) MergeEngine(io.shardingjdbc.core.merger.MergeEngine) 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) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult)

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