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