/** * 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()); }
/** * Begins a CASE statement, populating it with the first WHEN ... THEN branch */ public static CaseBuilder caseWhen(Criterion when, Object then) { if (when == null) { throw new IllegalArgumentException("Can't construct a CASE WHEN statement with a null criterion"); } return new CaseBuilder(null).when(when, then); }
/** * Begins a CASE statement with a base expression. Note: to use SQLite NULL literal as the base expression, * use {@link Field#NULL}. */ public static CaseBuilder caseExpr(Object baseExpression) { return new CaseBuilder(baseExpression); }
/** * Begins a CASE statement with a base expression. Note: to use SQLite NULL literal as the base expression, * use {@link Field#NULL}. */ public static CaseBuilder caseExpr(Object baseExpression) { return new CaseBuilder(baseExpression); }
final String FAIL = "FAIL"; Function<String> caseWhen = Function.caseExpr("Hello") .when("a", FAIL) .when("b", FAIL) .when("c", FAIL) .elseExpr(PASS) .end(); Property<?> test = StringProperty.fromFunction(caseWhen, "test"); assertExpectedValues(Query.select(test), test, PASS); .when("a", FAIL) .when("b", FAIL) .when("c", FAIL) .end(); test = StringProperty.fromFunction(caseWhen, "test"); assertExpectedValues(Query.select(test), test, new Object[]{null}); .when(null, FAIL) .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);
/** * Begins a CASE statement, populating it with the first WHEN ... THEN branch */ public static CaseBuilder caseWhen(Criterion when, Object then) { if (when == null) { throw new IllegalArgumentException("Can't construct a CASE WHEN statement with a null criterion"); } return new CaseBuilder(null).when(when, then); }
/** * 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()); }