/** * Create a Function that returns the given values based on whether the specified criterion passes */ public static <T> Function<T> caseWhen(final Criterion when, final Object ifTrue, final Object ifFalse) { return caseWhen(when, ifTrue).elseExpr(ifFalse).end(); }
/** * Sort the results by the value of the {@link Field}, ordered by values in the array. This is a bit of a hack. * * @param field the Field to order by * @param order values for the specified field, in the order they should appear in the result set */ public static <T> Order byArray(Field<T> field, T[] order) { if (order == null || order.length == 0) { return Order.asc("0"); } CaseBuilder caseBuilder = Function.caseExpr(field); for (int i = 0; i < order.length; i++) { caseBuilder.when(order[i], i); } caseBuilder.elseExpr(order.length); return Order.asc(caseBuilder.end()); }
/** * Create a Function that returns the given values based on whether the specified criterion passes */ public static <T> Function<T> caseWhen(final Criterion when, final Object ifTrue, final Object ifFalse) { return caseWhen(when, ifTrue).elseExpr(ifFalse).end(); }
.when("c", FAIL) .elseExpr(PASS) .end(); Property<?> test = StringProperty.fromFunction(caseWhen, "test"); assertExpectedValues(Query.select(test), test, PASS); .when("b", FAIL) .when("c", FAIL) .end(); test = StringProperty.fromFunction(caseWhen, "test"); assertExpectedValues(Query.select(test), test, new Object[]{null}); .when(Field.NULL, FAIL) .elseExpr(PASS) .end(); test = StringProperty.fromFunction(caseWhen, "test"); assertExpectedValues(Query.select(test), test, PASS); .when(IntegerProperty.literal(3, null).gt(0), PASS) .elseExpr(FAIL) .end(); test = StringProperty.fromFunction(caseWhen, "test"); assertExpectedValues(Query.select(test), test, PASS); .when(TestModel.LUCKY_NUMBER.lt(0), "negative") .elseExpr("zero") .end(); test = StringProperty.fromFunction(caseWhen, "test"); assertExpectedValues(Query.select(test).from(TestModel.TABLE).orderBy(TestModel.ID.asc()), test, "negative",
/** * Sort the results by the value of the {@link Field}, ordered by values in the array. This is a bit of a hack. * * @param field the Field to order by * @param order values for the specified field, in the order they should appear in the result set */ public static <T> Order byArray(Field<T> field, T[] order) { if (order == null || order.length == 0) { return Order.asc("0"); } CaseBuilder caseBuilder = Function.caseExpr(field); for (int i = 0; i < order.length; i++) { caseBuilder.when(order[i], i); } caseBuilder.elseExpr(order.length); return Order.asc(caseBuilder.end()); }