QueryBuilder { public String build() throws QueryBuilderException { String inputQuery = query.getDiql(); StringBuilder sb = new StringBuilder(); DiqlStmtContext diqlStmt; try { diqlStmt = DiqlParseUtil.parseWithAntlr(inputQuery); } catch (ParseException e) { throw new QueryBuilderException(e.getMessage(), e); } SelectStmtContext selectStmt = diqlStmt.getChild(SelectStmtContext.class, 0); if (selectStmt.getChild(TableNameContext.class, 0) != null) throw new QueryBuilderException("Query contains a FROM clause."); int lastResultValueIdx = 0; while (selectStmt.getChild(ResultValueContext.class, lastResultValueIdx) != null) lastResultValueIdx++; lastResultValueIdx--; ResultValueContext lastResultValue = selectStmt.getChild(ResultValueContext.class, lastResultValueIdx); sb.append(inputQuery.substring(0, lastResultValue.getStop().getStopIndex() + 1)); sb.append(" from "); sb.append(analysis.getTable()); sb.append(" "); int firstIndexOfRemainingInputString = lastResultValue.getStop().getStopIndex() + 1; boolean whereKeyWordAppended = false; ComparisonContext whereCtx = selectStmt.getChild(ComparisonContext.class, 0); if (whereCtx != null) { sb.append(" where ("); sb.append(inputQuery.substring(whereCtx.getStart().getStartIndex(), whereCtx.getStop().getStopIndex() + 1)); sb.append(") "); whereKeyWordAppended = true; firstIndexOfRemainingInputString = whereCtx.getStop().getStopIndex() + 1; } if (!slice.getSliceDisjunctions().isEmpty() || (slice.getManualConjunction() != null && !"".equals(slice.getManualConjunction()))) { if (whereKeyWordAppended) sb.append(" and ("); else sb.append(" where ("); boolean andNeeded = false; for (UiSliceDisjunction disjunction : slice.getSliceDisjunctions()) { if (!disjunction.getDisjunctionValues().isEmpty()) { if (andNeeded) sb.append(" and "); andNeeded = true; sb.append("("); boolean firstValue = true; for (String disjunctionValue : disjunction.getDisjunctionValues()) { if (!firstValue) sb.append(" or "); firstValue = false; sb.append(disjunction.getFieldName()); sb.append(" = "); sb.append(disjunctionValue); } sb.append(")"); } } if (slice.getManualConjunction() != null && !"".equals(slice.getManualConjunction())) { if (andNeeded) sb.append(" and "); sb.append("("); sb.append(slice.getManualConjunction()); sb.append(")"); } sb.append(")"); } sb.append(inputQuery.substring(firstIndexOfRemainingInputString)); String res = sb.toString(); logger.info("Created final query for UiQuery {}: {}", query.getId(), res); return res; }  QueryBuilder withAnalysis(UiAnalysis analysis); QueryBuilder withQuery(UiQuery query); QueryBuilder withSlice(UiSlice slice); String build();  }
@Test public void simpleTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() group by a order by a asc", null, Arrays.asList()); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }
@Test(expectedExceptions = QueryBuilderException.class) public void partialContainsFrom() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() from abc group by a order by a asc", null, Arrays.asList()); generateQueryBuilder(analysis).build(); }
@Test public void singleWhereInQueryTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() where a = 5 group by a order by a asc", null, Arrays.asList()); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " where a = 5 group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }
@Test public void singleWhereInSliceTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() group by a order by a asc", null, Arrays.asList(new UiSliceDisjunction("a", Arrays.asList("5")))); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " where a = 5 group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }
@Test public void whereInSliceAndManualConjunctionTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() group by a order by a asc", "b = 3", Arrays.asList(new UiSliceDisjunction("a", Arrays.asList("5")))); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " where a = 5 and b = 3 group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }
@Test public void singleWhereWithTwoValuesInSliceTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() group by a order by a asc", null, Arrays.asList(new UiSliceDisjunction("a", Arrays.asList("5", "6")))); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " where a = 5 or a = 6 group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }
@Test public void singleWhereWithTwoValuesAndManualConjunctionInSliceTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() group by a order by a asc", "b=3 or b=4", Arrays.asList(new UiSliceDisjunction("a", Arrays.asList("5", "6")))); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " where (a = 5 or a = 6) and (b = 3 or b = 4) group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }
@Test public void multipleWhereWithTwoValuesInSliceTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() group by a order by a asc", null, Arrays.asList( new UiSliceDisjunction("a", Arrays.asList("5", "6")), new UiSliceDisjunction("b", Arrays.asList("3", "4")))); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " where (a = 5 or a = 6) and (b = 3 or b = 4) group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }
@Test public void multipleWhereWithSingleTwoValuesInSliceTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() group by a order by a asc", null, Arrays .asList(new UiSliceDisjunction("a", Arrays.asList("5", "6")), new UiSliceDisjunction("b", Arrays.asList("3")))); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " where (a = 5 or a = 6) and b = 3 group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }
@Test public void multipleWhereWithSingleTwoValuesAndManualConjunctionInSliceTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() group by a order by a asc", "c=5", Arrays .asList(new UiSliceDisjunction("a", Arrays.asList("5", "6")), new UiSliceDisjunction("b", Arrays.asList("3")))); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " where (a = 5 or a = 6) and b = 3 and c=5 group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }
@Test public void multipleWhereWithSingleTwoValuesAndManualConjunctionAndManualWhereInSliceTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() where d = 1 group by a order by a asc", "c=5", Arrays .asList(new UiSliceDisjunction("a", Arrays.asList("5", "6")), new UiSliceDisjunction("b", Arrays.asList("3")))); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " where d=1 and ((a = 5 or a = 6) and b = 3 and c=5) group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }
@Test public void emptyDisjunctionValuesTest() throws QueryBuilderException { UiAnalysis analysis = createAnalysis("select a, count() group by a order by a asc", null, Arrays .asList(new UiSliceDisjunction("a", Arrays.asList("5", "6")), new UiSliceDisjunction("b", Arrays.asList()))); String queryBuilderDiql = generateQueryBuilder(analysis).build(); ExecutionRequest queryBuilderRequest = parse(queryBuilderDiql); String expectedDiql = "select a, count() from " + TABLE + " where a = 5 or a = 6 group by a order by a asc"; ExecutionRequest expectedRequest = parse(expectedDiql); Assert.assertEquals(queryBuilderRequest, expectedRequest, "Built query found to be not equal to the genrated one. Expected: '" + expectedDiql + "' but was '" + queryBuilderDiql + "'"); }