OrderByStreamMergedResult extends StreamMergedResult { @Override public boolean next() throws SQLException { if (orderByValuesQueue.isEmpty()) { return false; } if (isFirstNext) { isFirstNext = false; return true; } OrderByValue firstOrderByValue = orderByValuesQueue.poll(); if (firstOrderByValue.next()) { orderByValuesQueue.offer(firstOrderByValue); } if (orderByValuesQueue.isEmpty()) { return false; } setCurrentQueryResult(orderByValuesQueue.peek().getQueryResult()); return true; } OrderByStreamMergedResult(final List<QueryResult> queryResults, final SelectStatementContext selectStatementContext, final SchemaMetaData schemaMetaData); @Override boolean next();  }
@Test public void assertNextForResultSetsAllEmpty() throws SQLException { List<QueryResult> queryResults = Arrays.asList(mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, createSchemaMetaData()); assertFalse(actual.next()); }
@Test public void assertNextForSomeResultSetsEmpty() throws SQLException { List<QueryResult> queryResults = Arrays.asList(mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); for (int i = 0; i < 3; i++) { when(queryResults.get(i).getColumnName(1)).thenReturn("col1"); when(queryResults.get(i).getColumnName(2)).thenReturn("col2"); } ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); when(queryResults.get(0).next()).thenReturn(true, false); when(queryResults.get(0).getValue(1, Object.class)).thenReturn("2"); when(queryResults.get(2).next()).thenReturn(true, true, false); when(queryResults.get(2).getValue(1, Object.class)).thenReturn("1", "1", "3", "3"); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, createSchemaMetaData()); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("1")); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("2")); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("3")); assertFalse(actual.next()); }
@Test public void assertNextForMix() throws SQLException { List<QueryResult> queryResults = Arrays.asList(mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); for (int i = 0; i < 3; i++) { when(queryResults.get(i).getColumnName(1)).thenReturn("col1"); when(queryResults.get(i).getColumnName(2)).thenReturn("col2"); } ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); when(queryResults.get(0).next()).thenReturn(true, false); when(queryResults.get(0).getValue(1, Object.class)).thenReturn("2"); when(queryResults.get(1).next()).thenReturn(true, true, true, false); when(queryResults.get(1).getValue(1, Object.class)).thenReturn("2", "2", "3", "3", "4", "4"); when(queryResults.get(2).next()).thenReturn(true, true, false); when(queryResults.get(2).getValue(1, Object.class)).thenReturn("1", "1", "3", "3"); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, createSchemaMetaData()); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("1")); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("2")); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("2")); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("3")); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("3")); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("4")); assertFalse(actual.next()); }
@Test public void assertNextForCaseSensitive() throws SQLException { List<QueryResult> queryResults = Arrays.asList(mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); for (int i = 0; i < 3; i++) { when(queryResults.get(i).getColumnName(1)).thenReturn("col1"); when(queryResults.get(i).getColumnName(2)).thenReturn("col2"); } when(queryResults.get(0).next()).thenReturn(true, false); when(queryResults.get(0).getValue(1, Object.class)).thenReturn("b"); when(queryResults.get(1).next()).thenReturn(true, true, false); when(queryResults.get(1).getValue(1, Object.class)).thenReturn("B", "B", "a", "a"); when(queryResults.get(2).next()).thenReturn(true, false); when(queryResults.get(2).getValue(1, Object.class)).thenReturn("A"); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, createSchemaMetaData()); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("A")); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("B")); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("a")); assertTrue(actual.next()); assertThat(actual.getValue(1, Object.class).toString(), is("b")); assertFalse(actual.next()); }
@Test public void assertNextForCaseInsensitive() throws SQLException { List<QueryResult> queryResults = Arrays.asList(mock(QueryResult.class), mock(QueryResult.class), mock(QueryResult.class)); for (int i = 0; i < 3; i++) { when(queryResults.get(i).getColumnName(1)).thenReturn("col1"); when(queryResults.get(i).getColumnName(2)).thenReturn("col2"); } when(queryResults.get(0).next()).thenReturn(true, false); when(queryResults.get(0).getValue(2, Object.class)).thenReturn("b"); when(queryResults.get(1).next()).thenReturn(true, true, false); when(queryResults.get(1).getValue(2, Object.class)).thenReturn("a", "a", "B", "B"); when(queryResults.get(2).next()).thenReturn(true, false); when(queryResults.get(2).getValue(2, Object.class)).thenReturn("A"); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(DatabaseTypes.getActualDatabaseType("MySQL")); MergedResult actual = resultMerger.merge(queryResults, selectStatementContext, createSchemaMetaData()); assertTrue(actual.next()); assertThat(actual.getValue(2, Object.class).toString(), is("a")); assertTrue(actual.next()); assertThat(actual.getValue(2, Object.class).toString(), is("A")); assertTrue(actual.next()); assertThat(actual.getValue(2, Object.class).toString(), is("B")); assertTrue(actual.next()); assertThat(actual.getValue(2, Object.class).toString(), is("b")); assertFalse(actual.next()); }