@Override public void init(Repository r, Class<?> clazz) { boolean allSubConditionsHasPreFilter = true; boolean oneSubConditionHasPreFilter = false; for (BaseCondition c : conditions) { c.init(r, clazz); if (!c.hasPreFilter()) { allSubConditionsHasPreFilter = false; } else { oneSubConditionHasPreFilter = true; } if (c.hasPostFilter()) { hasPostFilter = true; } } hasPreFilter = (oneSubConditionHasPreFilter && logicalOperator == LogicalOperator.AND) || (allSubConditionsHasPreFilter && logicalOperator == LogicalOperator.OR); }
private boolean evaluateOr(Object object) { for (BaseCondition condition : conditions) { if (condition.evaluate(object)) { return true; } } return false; }
public static BaseCondition not(BaseCondition c) { return c.not(); }
@Test public void testJoinedConditions() { BasicObject object = new BasicObject(); object.setIntValue(10); object.setStringValue("ccc"); object.setLongValue(100l); assertTrue(c("intValue", "=", 10).and(c("stringValue", "=", "ccc")).evaluate(object)); assertFalse(c("intValue", "=", 10).and(c("stringValue", "!=", "ccc")).evaluate(object)); assertTrue(c("intValue", "=", 9).or(c("stringValue", "=", "ccc")).evaluate(object)); assertFalse(c("intValue", "=", 10).not().evaluate(object)); assertTrue(c("intValue", "=", 9).not().evaluate(object)); assertTrue(c("intValue", "=", 9).or(c("stringValue", "=", "bbb").or(c("longValue", "=", 100l))).evaluate(object)); }
private boolean hasPropertyFilter() { return builder.getCondition() != null && builder.getCondition().hasPreFilter(); }
@Test public void testParentRef() { Parent parent = new Parent("parent"); yawp.save(parent); Child child = new Child("child", parent.getId()); yawp.save(child); Grandchild grandchild = new Grandchild("grandchild", child.getId()); yawp.save(grandchild); BaseCondition c = c("parent->parent->name", "=", "parent"); c.init(yawp, Grandchild.class); assertTrue(c.evaluate(grandchild)); assertTrue(c.evaluate(child)); assertTrue(c.evaluate(parent)); }
@Test public void testWhereWithoutIndexComplexConditions() { yawp.save(new BasicObject("a", 1l)); yawp.save(new BasicObject("a", 2l)); yawp.save(new BasicObject("c", 3l)); yawp.save(new BasicObject("c", 4l)); yawp.save(new BasicObject("d", 5l)); yawp.save(new BasicObject("e", 6l)); BaseCondition c2 = c("stringValue", "=", "c").and(c("longValue", "=", 3l)); List<BasicObject> objects = yawp(BasicObject.class).where(c("stringValue", "=", "a").or(c2)).list(); assertObjects(objects, "a", "a", "c"); }
private void initConditions() { if (condition == null) { return; } condition.init(r, endpointClazz); }
@Test public void testWhereWithoutIndexAndCondition() { yawp.save(new BasicObject("a", 1l)); yawp.save(new BasicObject("a", 2l)); yawp.save(new BasicObject("b", 1l)); List<BasicObject> objects = yawp(BasicObject.class).where(c("stringValue", "=", "a").and(c("longValue", "=", 1l))).list(); assertObjects(objects, "a"); }
@Override public Map<String, Object> toMap() { Map<String, Object> map = new HashMap<>(); map.put("logicalOperator", logicalOperator.toString()); List<Map<String, Object>> cs = new ArrayList<>(); for (BaseCondition c : conditions) { cs.add(c.toMap()); } map.put("conditions", cs); return map; }
private boolean hasPostFilter() { return condition != null && condition.hasPostFilter(); }
private List<T> postFilter(List<T> objects) { if (!hasPostFilter()) { return objects; } return condition.applyPostFilter(objects); }
@Test public void testWhereWithoutIndexOrCondition() { yawp.save(new BasicObject("a", 1l)); yawp.save(new BasicObject("b", 2l)); yawp.save(new BasicObject("c", 1l)); List<BasicObject> objects = yawp(BasicObject.class).where(c("stringValue", "=", "a").or(c("longValue", "=", 1l))).list(); assertObjects(objects, "a", "c"); }
private void prepareQueryWhere(QueryBuilder<?> builder, Query q) throws FalsePredicateException { BaseCondition condition = builder.getCondition(); if (condition != null && condition.hasPreFilter()) { q.setFilter(createFilter(builder, condition)); } }
@Test public void testIgnoreConditionForChildFields() { Parent parent = new Parent("parent"); yawp.save(parent); Child child = new Child("child", parent.getId()); yawp.save(child); Grandchild grandchild = new Grandchild("grandchild", child.getId()); grandchild.setAge(10); yawp.save(grandchild); BaseCondition c = c("age", "=", 11); c.init(yawp, Grandchild.class); assertFalse(c.evaluate(grandchild)); assertTrue(c.evaluate(child)); assertTrue(c.evaluate(parent)); } }
@Test public void testWhereWithoutIndexWithId() { yawp.save(setId(new BasicObject("a", 1l), 1l)); yawp.save(setId(new BasicObject("b", 2l), 2l)); yawp.save(setId(new BasicObject("c", 3l), 3l)); IdRef<BasicObject> id = IdRef.create(yawp, BasicObject.class, 1l); List<BasicObject> objects = yawp(BasicObject.class).where(c("id", "=", id).or(c("longValue", "=", 3l))).list(); assertObjects(objects, "a", "c"); objects = yawp(BasicObject.class).where(c("id", "=", id).and(c("longValue", "=", 1l))).list(); assertObjects(objects, "a"); objects = yawp(BasicObject.class).where(c("id", "=", id).and(c("longValue", "=", 3l))).list(); assertObjects(objects); }
public QueryBuilder<T> where(BaseCondition c) { if (condition == null) { condition = c; } else { condition = Condition.and(condition, c); } condition.init(r, clazz); return this; }
@Test public void testQueryRefWithPreFilter() { BasicObject ref1 = new BasicObject("right"); BasicObject ref2 = new BasicObject("right"); yawp.save(ref1); yawp.save(ref2); yawp.save(new BasicObject("a", ref1.getId())); yawp.save(new BasicObject("b", ref2.getId())); BasicObject object = yawp(BasicObject.class).where(c("objectId->stringValue", "=", "right").and(c("stringValue", "=", "a"))).only(); assertEquals("a", object.getStringValue()); }
public Map<String, Object> toMap() { Map<String, Object> result = new HashMap<>(); result.put("clazz", clazz.getCanonicalName()); result.put("parentId", parentId == null ? null : parentId.toString()); result.put("condition", condition == null ? null : condition.toMap()); result.put("preOrders", preOrders); result.put("postOrders", postOrders); result.put("limit", limit); return result; } }
private String joinedWhere(JoinedCondition joinedCondition) throws FalsePredicateException { BaseCondition[] conditions = joinedCondition.getConditions(); LogicalOperator logicalOperator = joinedCondition.getLogicalOperator(); List<String> wheres = new ArrayList<>(); for (int i = 0; i < conditions.length; i++) { try { BaseCondition condition = conditions[i]; if (!condition.hasPreFilter()) { continue; } wheres.add(where(condition)); } catch (FalsePredicateException e) { if (logicalOperator == LogicalOperator.AND) { throw e; } } } if (wheres.isEmpty()) { throw new FalsePredicateException(); } if (wheres.size() == 1) { return wheres.get(0); } return applyLogicalOperator(logicalOperator, wheres); }