SelectTask implements Task { @Override public ExecutionResult execute(ExecutionResult previousTaskResult) throws SQLException { try { if (selectAll) { return previousTaskResult; } DataContainer newData = new DataContainer(selectionMap.keySet()); previousTaskResult.getData().getRows().stream() .map(dr -> selectionMap.keySet().stream() .map(alias -> expressionEvaluator.evaluate(selectionMap.get(alias), dr)) .collect(Collectors.toList())) .forEach(newData::addRow); return new ExecutionResult(){ { DataContainer effectiveData = extractSingletonDataContainerIfNeeded(newData); setData(effectiveData); setCount(effectiveData.getRows().size()); } }; } catch (Exception e) { throw new SQLException(e); } } SelectTask(Map<String, Object> selectionMap, Map<String, String> tableAliases); @PostConstruct void init(); @Override ExecutionResult execute(ExecutionResult previousTaskResult); Map<String, Object> getSelectionMap(); boolean isSelectAll(); @Override String toString();  }
@Test public void testSimpleSelect() throws Exception { Map<String, Object> selectionMap = new HashMap<String, Object>(){ { put(FIRST_ALIAS, new ColumnExpression(FIRST_COLUMN, TABLE_NAME)); put(SECOND_ALIAS, new FunctionExpression("abs", Collections.singletonList(new ColumnExpression(FOURTH_COLUMN, TABLE_NAME)))); put(THIRD_ALIAS, 42); } }; SelectTask selectTask = applicationContext.getBean( SelectTask.class, selectionMap, Collections.singletonMap(TABLE_ALIAS, TABLE_NAME) ); ExecutionResult inputData = getInput(); ExecutionResult executionResult = selectTask.execute(inputData); assertThat(executionResult.getCount(), equalTo(2)); List<DataRow> rows = executionResult.getData().getRows(); assertThat(rows.size(), equalTo(2)); DataRow firstRow = rows.get(0); assertThat(firstRow.getValues(), hasSize(3)); assertThat(firstRow.get(FIRST_ALIAS), equalTo("one")); assertThat(firstRow.get(SECOND_ALIAS), equalTo(333d)); assertThat(firstRow.get(THIRD_ALIAS), equalTo(42)); DataRow secondRow = rows.get(1); assertThat(secondRow.getValues(), hasSize(3)); assertThat(secondRow.get(FIRST_ALIAS), equalTo("two")); assertThat(secondRow.get(SECOND_ALIAS), equalTo(222d)); assertThat(secondRow.get(THIRD_ALIAS), equalTo(42)); }
@Test public void testSelectAggregatedData() throws Exception { Map<String, Object> selectionMap = new HashMap<String, Object>(){ { put(FIRST_ALIAS, new FunctionExpression("tables", Collections.emptyList())); } }; SelectTask selectTask = applicationContext.getBean( SelectTask.class, selectionMap, Collections.emptyMap() ); ExecutionResult inputData = new DummyFetchTask().execute(new ExecutionResult()); ExecutionResult executionResult = selectTask.execute(inputData); assertThat(executionResult.getCount(), is(greaterThan(0))); assertThat(executionResult.getData().getColumnNames(), contains("table_name")); assertThat(executionResult.getData().getRows().size(), is(greaterThan(0))); }
@Test public void testSelectAll() throws Exception { Map<String, Object> selectionMap = new HashMap<String, Object>() { { put("*", new ColumnExpression()); } }; SelectTask selectTask = applicationContext.getBean( SelectTask.class, selectionMap, Collections.singletonMap(TABLE_ALIAS, TABLE_NAME) ); ExecutionResult inputData = getInput(); ExecutionResult executionResult = selectTask.execute(inputData); assertThat(executionResult, equalTo(inputData)); }