IndexScanStrategy extends ScanStrategy { @Override public DataContainer process(DataSource dataSource, Map<String, String> tableAliases) { checkLeftDataSource(dataSource); String tableAlias = dataSource.getTableAlias().get(); IndexBooleanExpression condition = getCondition(dataSource); if (dataSource.getRightDataSource().isPresent()) { DataSource nextDataSource = dataSource.getRightDataSource().get(); checkRightDataSource(nextDataSource); String rightTableAlias = nextDataSource.getTableAlias().get(); IndexBooleanExpression rightCondition = getCondition(nextDataSource); List<ColumnRelation> columnRelations = rightCondition.getColumnRelations(); Assert.isTrue(columnRelations.size() > 0, "At least one column relation should be present"); return foreignKeyJoin(tableAlias, columnRelations, condition.getFixedValueConditions(tableAlias), rightTableAlias, rightCondition.getFixedValueConditions(rightTableAlias), nextDataSource.getJoinType().get(), tableAliases); } else { return fetch(tableAlias, condition, tableAliases); } }  @Override DataContainer process(DataSource dataSource, Map<String, String> tableAliases);  }
@Test public void testInnerJoin() { IndexBooleanExpression emptyCondition = new IndexBooleanExpression(); DataSource instancesDataSource = new DataSource(INSTANCES_ALIAS); instancesDataSource.setCondition(emptyCondition); DataSource projectsDataSource = new DataSource(PROJECTS_ALIAS); ColumnRelation columnRelation = new ColumnRelation(INSTANCES_ALIAS, PROJECT_ID, PROJECTS_ALIAS, ID); emptyCondition.getColumnRelations().add(columnRelation); projectsDataSource.setCondition(emptyCondition); projectsDataSource.setJoinType(INNER); instancesDataSource.setRightDataSource(projectsDataSource); DataSourceStrategy strategy = getStrategy(); DataContainer result = strategy.process(instancesDataSource, TWO_TABLE_ALIASES); assertThat(result.getRows(), hasSize(5)); assertThat(rowsAsValues(result.getRows()), containsInAnyOrder( Arrays.asList("1", "first", "2", "2", "second_project"), Arrays.asList("2", "second", "1", "1", "first_project"), Arrays.asList("3", "third", "2", "2", "second_project"), Arrays.asList("4", "third", "3", "3", "third_project"), Arrays.asList("5", "fifth", "2", "2", "second_project") )); }
@Test public void testInnerJoinWithCondition() { DataSource dataSource = prepareJoinDataSource(INNER); DataSourceStrategy strategy = getStrategy(); DataContainer result = strategy.process(dataSource, TWO_TABLE_ALIASES); assertContainerColumns(result); assertThat(result.getRows(), hasSize(2)); assertThat(rowsAsValues(result.getRows()), containsInAnyOrder( Arrays.asList("1", "first", "2", "1", "first_project"), Arrays.asList("1", "first", "2", "2", "second_project") )); }
@Test public void testLeftJoin() { DataSource dataSource = prepareJoinDataSource(LEFT); DataSourceStrategy strategy = getStrategy(); DataContainer result = strategy.process(dataSource, TWO_TABLE_ALIASES); assertContainerColumns(result); assertThat(result.getRows(), hasSize(6)); assertThat(rowsAsValues(result.getRows()), containsInAnyOrder( Arrays.asList("1", "first", "2", "1", "first_project"), Arrays.asList("1", "first", "2", "2", "second_project"), Arrays.asList("2", "second", "1", null, null), Arrays.asList("3", "third", "2", null, null), Arrays.asList("4", "third", "3", null, null), Arrays.asList("5", "fifth", "2", null, null) )); }
@Test public void testRightJoin() { DataSource dataSource = prepareJoinDataSource(RIGHT); DataSourceStrategy strategy = getStrategy(); DataContainer result = strategy.process(dataSource, TWO_TABLE_ALIASES); assertContainerColumns(result); assertThat(result.getRows(), hasSize(3)); assertThat(rowsAsValues(result.getRows()), containsInAnyOrder( Arrays.asList("1", "first", "2", "1", "first_project"), Arrays.asList("1", "first", "2", "2", "second_project"), Arrays.asList(null, null, null, "3", "third_project") )); }
@Test(expected = IllegalArgumentException.class) public void testNoTableAlias() { DataSource dataSource = new DataSource(new DataSource(TABLE_ALIAS)); assertThat(dataSource.getTableAlias().isPresent(), is(false)); DataSourceStrategy strategy = getStrategy(); strategy.process(dataSource, TABLE_ALIASES); }
@Test(expected = IllegalArgumentException.class) public void testMoreThanTwoDataSources() { DataSource firstDataSource = new DataSource(TABLE_ALIAS); firstDataSource.setCondition(new IndexBooleanExpression()); DataSource secondDataSource = new DataSource(TABLE_ALIAS); DataSource thirdDataSource = new DataSource(TABLE_ALIAS); secondDataSource.setRightDataSource(thirdDataSource); firstDataSource.setRightDataSource(secondDataSource); DataSourceStrategy strategy = getStrategy(); strategy.process(firstDataSource, TABLE_ALIASES); }