/** * @param criterion another criterion to be appended with OR. If null, this Criterion will be returned unmodified. * @return a criterion equivalent to (this OR criterion) */ public Criterion or(Criterion criterion) { if (criterion == null) { return this; } return or(this, criterion); }
/** * @param criterion another criterion to be appended with AND. If null, this Criterion will be returned unmodified. * @return a criterion equivalent to (this AND criterion) */ public Criterion and(Criterion criterion) { if (criterion == null) { return this; } return and(this, criterion); }
/** * @return a {@link Criterion} using this field as a literal value */ public Criterion asCriterion() { return Criterion.literal(this); }
public void testInstanceVsStaticConjunction() { Criterion c1 = TestModel.FIRST_NAME.eq("Sam"); Criterion c2 = TestModel.LAST_NAME.eq("Bosley"); Criterion c3 = TestModel.LUCKY_NUMBER.eq(7); Criterion c = c1.and(c2); Criterion cand = c.and(c3); assertNotSame(c, cand); // Tests immutability of conjunction criterions when appending with the same operator assertEquals(Criterion.and(c1, c2, c3), c1.and(c2).and(c3)); assertEquals(Criterion.and(Arrays.asList(c1, c2, c3)), c1.and(c2).and(c3)); assertEquals(Criterion.or(c1, c2, c3), c1.or(c2).or(c3)); assertEquals(Criterion.or(Arrays.asList(c1, c2, c3)), c1.or(c2).or(c3)); assertEquals(Criterion.or(Criterion.and(c1, c2), c3), c1.and(c2).or(c3)); assertEquals(Criterion.and(Criterion.or(c1, c2), c3), c1.or(c2).and(c3)); assertEquals(Criterion.and(c1, Criterion.or(c2, c3)), c1.and(c2.or(c3))); assertEquals(Criterion.or(c1, Criterion.and(c2, c3)), c1.or(c2.and(c3))); }
public void testLiteralCriterions() { // null and not null evaluate to false assertEquals(0, database.count(Employee.class, Criterion.literal(null))); assertEquals(0, database.count(Employee.class, Criterion.literal(null).negate())); // numeric literal; values other than 0 (including negative) evaluate to true assertEquals(0, database.count(Employee.class, Criterion.literal(0))); assertEquals(6, database.count(Employee.class, Criterion.literal(10))); assertEquals(6, database.count(Employee.class, Criterion.literal(-10))); assertEquals(6, database.count(Employee.class, Criterion.literal(0).negate())); assertEquals(0, database.count(Employee.class, Criterion.literal(10).negate())); // text literal; SQLite will try to coerce to a number assertEquals(0, database.count(Employee.class, Criterion.literal("sqlite"))); assertEquals(6, database.count(Employee.class, Criterion.literal("sqlite").negate())); assertEquals(6, database.count(Employee.class, Criterion.literal("1sqlite"))); // coerces to 1 assertEquals(0, database.count(Employee.class, Criterion.literal("1sqlite").negate())); // numeric column Criterion isHappyCriterion = Employee.IS_HAPPY.asCriterion(); assertEquals(5, database.count(Employee.class, isHappyCriterion)); assertEquals(1, database.count(Employee.class, isHappyCriterion.negate())); // text column Criterion nameCriterion = Employee.NAME.asCriterion(); assertEquals(0, database.count(Employee.class, nameCriterion)); assertEquals(6, database.count(Employee.class, nameCriterion.negate())); // function Criterion f = Function.functionWithArguments("length", "sqlite").asCriterion(); assertEquals(6, database.count(Employee.class, f)); assertEquals(0, database.count(Employee.class, f.negate())); }
@Override public int hashCode() { return toString().hashCode(); } }
@Override public Criterion negate() { Operator contrary = operator.getContrary(); if (contrary != null) { return constructNegatedCriterion(contrary); } return super.negate(); }
/** * @return a logically negated version of this criterion. If it is a case of simple operator negation, * only the operator will be negated (e.g. {@code "myField = 3"} would become {@code "myField <> 3"}). If the * negation cannot be performed in the operator, the criterion will be wrapped in a "not" statement (e.g. {@code * "not (myField like 'Sam')"}). */ public Criterion negate() { return not(this); }
/** * Build a {@link Query} combining this object's internal state with the arguments passed. If a * {@link ProjectionMap} is set, the projection elements will be evaluated and transformed accordingly. If the * sortOrder is null or empty, the default order will be used (if one was set). * * @param projection the raw column names to be selected * @param selection a raw selection string * @param selectionArgs array of strings which substitute replaceable arguments in the selection string * @param sortOrder a raw ordering clause * @return a {@link Query} using the projection, selection, selection args, and sort order */ public Query build(String[] projection, String selection, String[] selectionArgs, String sortOrder) { Query query = Query.select(computeProjection(projection)).from(dataSource); boolean hasUserSelection = !SqlUtils.isEmpty(selection); if (hasUserSelection) { query.where(Criterion.fromRawSelection(selection, selectionArgs)); } if (!SqlUtils.isEmpty(sortOrder)) { query.orderBy(Order.fromExpression(sortOrder)); } else if (defaultOrder != null && defaultOrder.length > 0) { query.orderBy(defaultOrder); } if (strictMode && hasUserSelection) { query.requestValidation(); } return query; }
@Override protected void populate(SqlBuilder builder, boolean forSqlValidation) { builder.sql.append(operator); toNegate.appendToSqlBuilder(builder, forSqlValidation); }
/** * Append a string representation of this Criterion * * @param builder The {@link SqlBuilder} for building the SQL statement * @param forSqlValidation forSqlValidation true if this statement is being compiled to validate against malicious * SQL */ @Override void appendToSqlBuilder(SqlBuilder builder, boolean forSqlValidation) { builder.sql.append("("); populate(builder, forSqlValidation); builder.sql.append(")"); }
@Override public boolean equals(Object o) { return this == o || !(o == null || getClass() != o.getClass()) && this.toString().equals(o.toString()); }
@Override public void run() { Criterion.not(null); } }, IllegalArgumentException.class);
public void testEmptyRawSelectionReturnsNull() { assertNull(Criterion.fromRawSelection(null, null)); assertNull(Criterion.fromRawSelection("", null)); }
@Override protected void populate(SqlBuilder builder, boolean forSqlValidation) { criterions.get(0).appendToSqlBuilder(builder, forSqlValidation); for (int i = 1; i < criterions.size(); i++) { Criterion c = criterions.get(i); if (c != null) { builder.sql.append(operator); c.appendToSqlBuilder(builder, forSqlValidation); } } }
/** * Append a string representation of this Criterion * * @param builder The {@link SqlBuilder} for building the SQL statement * @param forSqlValidation forSqlValidation true if this statement is being compiled to validate against malicious * SQL */ @Override void appendToSqlBuilder(SqlBuilder builder, boolean forSqlValidation) { builder.sql.append("("); populate(builder, forSqlValidation); builder.sql.append(")"); }
@Override public Criterion or(Criterion criterion) { Criterion toReturn = checkOperatorAndAppendCriterions(Operator.or, criterion); if (toReturn == null) { return super.or(criterion); } return toReturn; }
@Override public Criterion and(Criterion criterion) { Criterion toReturn = checkOperatorAndAppendCriterions(Operator.and, criterion); if (toReturn == null) { return super.and(criterion); } return toReturn; }
public void testInCriterionWithEmptySets() { String expectedInString = "(testModels._id IN ())"; String expectedNotInString = "(testModels._id NOT IN ())"; Object[] emptyArray = new Object[0]; assertEquals(expectedInString, TestModel.ID.in((List<Long>) null).toString()); assertEquals(expectedInString, TestModel.ID.in((Object[]) null).toString()); assertEquals(expectedInString, TestModel.ID.in((Query) null).toString()); assertEquals(expectedInString, TestModel.ID.in(new ArrayList<Long>()).toString()); assertEquals(expectedInString, TestModel.ID.in((Object[]) emptyArray).toString()); assertEquals(expectedInString, TestModel.ID.in().toString()); assertEquals(expectedNotInString, TestModel.ID.notIn((List<Long>) null).toString()); assertEquals(expectedNotInString, TestModel.ID.notIn((Object[]) null).toString()); assertEquals(expectedNotInString, TestModel.ID.notIn((Query) null).toString()); assertEquals(expectedNotInString, TestModel.ID.notIn(new ArrayList<Long>()).toString()); assertEquals(expectedNotInString, TestModel.ID.notIn((Object[]) emptyArray).toString()); assertEquals(expectedNotInString, TestModel.ID.notIn().toString()); }