Search in sources :

Example 1 with MemoryQueryResultRow

use of io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow 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 MemoryQueryResultRow

use of io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow 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 MemoryQueryResultRow

use of io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow in project sharding-jdbc by shardingjdbc.

the class GroupByRowComparatorTest method assertCompareToForEqualWithGroupByItems.

@Test
public void assertCompareToForEqualWithGroupByItems() throws SQLException {
    MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    SelectStatement selectStatement = new SelectStatement();
    selectStatement.getGroupByItems().addAll(Arrays.asList(new OrderItem(1, OrderDirection.ASC, OrderDirection.ASC), new OrderItem(2, OrderDirection.DESC, OrderDirection.ASC)));
    GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatement);
    assertThat(groupByRowComparator.compare(o1, o2), is(0));
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) Test(org.junit.Test)

Example 4 with MemoryQueryResultRow

use of io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow in project sharding-jdbc by shardingjdbc.

the class GroupByRowComparatorTest method assertCompareToForEqualWithOrderByItems.

@Test
public void assertCompareToForEqualWithOrderByItems() throws SQLException {
    MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    SelectStatement selectStatement = new SelectStatement();
    selectStatement.getOrderByItems().addAll(Arrays.asList(new OrderItem(1, OrderDirection.ASC, OrderDirection.ASC), new OrderItem(2, OrderDirection.DESC, OrderDirection.ASC)));
    selectStatement.getGroupByItems().addAll(Arrays.asList(new OrderItem(1, OrderDirection.DESC, OrderDirection.ASC), new OrderItem(2, OrderDirection.ASC, OrderDirection.ASC)));
    GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatement);
    assertThat(groupByRowComparator.compare(o1, o2), is(0));
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) Test(org.junit.Test)

Example 5 with MemoryQueryResultRow

use of io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow in project sharding-jdbc by shardingjdbc.

the class GroupByRowComparatorTest method assertCompareToForAscWithGroupByItems.

@Test
public void assertCompareToForAscWithGroupByItems() throws SQLException {
    MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2"));
    MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("3", "4"));
    SelectStatement selectStatement = new SelectStatement();
    selectStatement.getGroupByItems().addAll(Arrays.asList(new OrderItem(1, OrderDirection.ASC, OrderDirection.ASC), new OrderItem(2, OrderDirection.ASC, OrderDirection.ASC)));
    GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatement);
    assertTrue(groupByRowComparator.compare(o1, o2) < 0);
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) MemoryQueryResultRow(io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem) Test(org.junit.Test)

Aggregations

MemoryQueryResultRow (io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow)10 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)6 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)6 Test (org.junit.Test)6 QueryResult (io.shardingjdbc.core.merger.QueryResult)3 TableRule (io.shardingjdbc.core.rule.TableRule)2 LinkedList (java.util.LinkedList)2 AggregationUnit (io.shardingjdbc.core.merger.dql.groupby.aggregation.AggregationUnit)1 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)1 AggregationSelectItem (io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1