ConditionBuilder { public ConditionList buildQueryConditions(List<Segment> filtered) { ConditionList providers = ConditionList.andList(); List<Segment> conditionList = new ArrayList<>(); for(Segment entry: filtered) { if(isConditionCandidate(entry)) { conditionList.add(entry); } else { parseCondition(providers, conditionList); } } parseCondition(providers, conditionList); return providers; }  ConditionList buildQueryConditions(List<Segment> filtered); ConditionList buildUpdateConditions(List<Segment> filtered);  }
@Test public void testSelectWhere() throws SQLException { SqlBuilder builder = SqlBuilder.selectAllFrom(p).where(p.CityID.eq(1), OR, p.CountryID.eq(1), AND, p.Name.like("A"), OR, p.PeopleID.eq(1)); ConditionList cl = builder.buildQueryConditions(); cl = (ConditionList)cl.get(0); assertFalse(cl.isIntersected()); assertEquals(3, cl.size()); cl = (ConditionList)cl.get(1); assertTrue(cl.isIntersected()); assertEquals(2, cl.size()); }
@Test public void testSelectWhereIncludeAll() throws SQLException { SqlBuilder builder = SqlBuilder.selectAllFrom(p).where().includeAll().and().append(p.CityID.eq(1), OR, p.CountryID.eq(1), AND, p.Name.like("A"), OR, p.PeopleID.eq(1)); ConditionList cl = builder.buildQueryConditions(); cl = (ConditionList)cl.get(0); assertFalse(cl.isIntersected()); assertEquals(3, cl.size()); ConditionList cl0; cl0 = (ConditionList)cl.get(0); assertTrue(cl0.isIntersected()); assertEquals(2, cl0.size()); cl0 = (ConditionList)cl.get(1); assertTrue(cl0.isIntersected()); assertEquals(2, cl0.size()); }
@Test public void testSelectWhereExcludeAll() throws SQLException { SqlBuilder builder = SqlBuilder.selectAllFrom(p).where().excludeAll().and().append(p.CityID.eq(1), OR, p.CountryID.eq(1), AND, p.Name.like("A"), OR, p.PeopleID.eq(1)); ConditionList cl = builder.buildQueryConditions(); cl = (ConditionList)cl.get(0); assertFalse(cl.isIntersected()); assertEquals(4, cl.size()); assertTrue(cl.get(0) instanceof ColumnCondition); cl = (ConditionList)cl.get(2); assertTrue(cl.isIntersected()); assertEquals(2, cl.size()); }
@Test public void testSelectWhereAll() throws SQLException { SqlBuilder builder = SqlBuilder.selectAllFrom(p).where().includeAll().and().excludeAll().and(SegmentConstants.TRUE).or(SegmentConstants.FALSE); ConditionList cl = builder.buildQueryConditions(); cl = (ConditionList)cl.get(0); assertFalse(cl.isIntersected()); assertEquals(3, cl.size()); assertTrue(cl.get(1) instanceof ColumnCondition); assertTrue(cl.get(2) instanceof ColumnCondition); cl = (ConditionList)cl.get(0); assertTrue(cl.isIntersected()); assertEquals(2, cl.size()); }
@Test public void testSelectWhereSimple() throws SQLException { SqlBuilder builder = SqlBuilder.selectAllFrom(p).where().includeAll().append(SegmentConstants.TRUE).or(SegmentConstants.FALSE); ConditionList cl = builder.buildQueryConditions(); cl = (ConditionList)cl.get(0); assertFalse(cl.isIntersected()); assertEquals(2, cl.size()); assertTrue(cl.get(1) instanceof ColumnCondition); cl = (ConditionList)cl.get(0); assertEquals(2, cl.size()); }
@Test public void testSelectWhereNotSimple() throws SQLException { SqlBuilder builder = SqlBuilder.selectAllFrom(p).where().not().includeAll().and().append(SegmentConstants.TRUE); ConditionList cl = builder.buildQueryConditions(); assertTrue(cl.isIntersected()); assertEquals(2, cl.size()); assertTrue(cl.get(0) instanceof ColumnCondition); }
@Test public void testSelectWhereNotOr() throws SQLException { SqlBuilder builder = SqlBuilder.selectAllFrom(p).where().not().includeAll().and().not().excludeAll().and(SegmentConstants.TRUE).or(SegmentConstants.FALSE); ConditionList cl = builder.buildQueryConditions(); cl = (ConditionList)cl.get(0); assertFalse(cl.isIntersected()); assertEquals(3, cl.size()); assertTrue(cl.get(1) instanceof ColumnCondition); assertTrue(cl.get(2) instanceof ColumnCondition); cl = (ConditionList)cl.get(0); assertTrue(cl.isIntersected()); assertEquals(2, cl.size()); }
@Test public void testSelectWhereNotAnd() throws SQLException { SqlBuilder builder = SqlBuilder.selectAllFrom(p).where().not().excludeAll().and().not().includeAll().and(SegmentConstants.TRUE).and(SegmentConstants.FALSE); ConditionList cl = builder.buildQueryConditions(); cl = (ConditionList)cl.get(0); assertFalse(cl.isIntersected()); assertEquals(2, cl.size()); assertTrue(cl.get(0) instanceof ColumnCondition); cl = (ConditionList)cl.get(1); assertTrue(cl.isIntersected()); assertEquals(3, cl.size()); assertTrue(cl.get(0) instanceof ColumnCondition); assertTrue(cl.get(1) instanceof ColumnCondition); assertTrue(cl.get(2) instanceof ColumnCondition); }
@Test public void testSelectWhereNotWellFormated() throws SQLException { SqlBuilder builder = SqlBuilder.selectAllFrom(p).where().not(); try { builder.buildQueryConditions(); fail(); } catch (IllegalArgumentException e) { } builder = SqlBuilder.selectAllFrom(p).where().append(SegmentConstants.TRUE, SegmentConstants.AND); try { builder.buildQueryConditions(); fail(); } catch (IllegalArgumentException e) { } }