/** * @return an {@link Order} that sorts ascending by this field's value */ public Order asc() { return Order.asc(this); }
/** * @return an {@link Order} that sorts by this field's value in the order specified by the array of values */ public Order byArray(TYPE[] order) { return Order.byArray(this, order); } }
/** * @return an {@link Order} that sorts descending by this field's value */ public Order desc() { return Order.desc(this); }
/** * Sort the results in ascending ('ASC') order by the given expression */ public static Order asc(Object expression) { return new Order(expression); }
/** * 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; }
public void testReverseOrder() { long max = database.countAll(Employee.class); SquidCursor<Employee> cursor = database.query(Employee.class, Query.select(Employee.ID).orderBy(Employee.ID.asc().reverse())); try { assertEquals(max, cursor.getCount()); assertTrue(max > 0); while (cursor.moveToNext()) { long nextId = cursor.get(Employee.ID); if (nextId > max) { fail("IDs not in reverse order"); } max = nextId; } } finally { cursor.close(); } }
/** * Sort the results in descending ('DESC') order by the given expression */ public static Order desc(Object expression) { return new Order(expression, OrderType.DESC); }
/** * 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; }
/** * 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()); }
public static Order fromExpression(String expression) { return new Order(expression, OrderType.RAW); }
/** * @return an {@link Order} that sorts descending by this field's value */ public Order desc() { return Order.desc(this); }
/** * @return an {@link Order} that sorts by this field's value in the order specified by the array of values */ public Order byArray(TYPE[] order) { return Order.byArray(this, order); } }
/** * @return an {@link Order} that sorts ascending by this field's value */ public Order asc() { return Order.asc(this); }
/** * @return an Order term that is the reverse of this one */ public Order reverse() { if (orderType == OrderType.ASC) { return new Order(expression, OrderType.DESC); } else if (orderType == OrderType.DESC) { return new Order(expression, OrderType.ASC); } else { throw new UnsupportedOperationException("reverse() is not currently supported for order type " + orderType.toString()); } } }
public void testInsertWithDefaultValues() { // insert into things default values; Insert insert = Insert.into(Thing.TABLE).defaultValues(); CompiledStatement compiled = insert.compile(database.getCompileContext()); verifyCompiledSqlArgs(compiled, 0); int rowsBeforeInsert = database.countAll(Thing.class); assertEquals(3, database.insert(insert)); int rowsAfterInsert = database.countAll(Thing.class); assertEquals(rowsBeforeInsert + 1, rowsAfterInsert); // get the newest Thing newThing = null; SquidCursor<Thing> cursor = null; try { cursor = database.query(Thing.class, Query.select(Thing.PROPERTIES).orderBy(Order.desc(Thing.ID)).limit(1)); if (cursor.moveToFirst()) { newThing = new Thing(cursor); } } finally { if (cursor != null) { cursor.close(); } } assertNotNull(newThing); assertEquals(Thing.DEFAULT_FOO, newThing.getFoo()); assertEquals(Thing.DEFAULT_BAR, newThing.getBar().intValue()); assertEquals(Thing.DEFAULT_IS_ALIVE, newThing.isAlive().booleanValue()); }
public void testOrderByField() { TestModel one = new TestModel().setFirstName("Sam").setLastName("Bosley"); TestModel two = new TestModel().setFirstName("Kevin").setLastName("Lim"); TestModel three = new TestModel().setFirstName("Jonathan").setLastName("Koren"); database.persist(one); database.persist(two); database.persist(three); String[] nameOrder = new String[]{"Kevin", "Sam", "Jonathan"}; SquidCursor<TestModel> nameOrderCursor = database.query(TestModel.class, Query.select(TestModel.PROPERTIES) .orderBy(Order.byArray(TestModel.FIRST_NAME, nameOrder))); try { assertEquals(3, nameOrderCursor.getCount()); for (nameOrderCursor.moveToFirst(); !nameOrderCursor.isAfterLast(); nameOrderCursor.moveToNext()) { assertEquals(nameOrder[nameOrderCursor.getPosition()], nameOrderCursor.get(TestModel.FIRST_NAME)); } } finally { nameOrderCursor.close(); } Long[] idOrder = new Long[]{3L, 1L, 2L}; SquidCursor<TestModel> idOrderCursor = database.query(TestModel.class, Query.select(TestModel.PROPERTIES) .orderBy(Order.byArray(TestModel.ID, idOrder))); try { assertEquals(3, idOrderCursor.getCount()); for (idOrderCursor.moveToFirst(); !idOrderCursor.isAfterLast(); idOrderCursor.moveToNext()) { assertEquals(idOrder[idOrderCursor.getPosition()], idOrderCursor.get(TestModel.ID)); } } finally { idOrderCursor.close(); } }
public void testBooleanFunctionOnCriterion() { BooleanProperty onCriterion = BooleanProperty .fromFunction(Function.caseWhen(TestModel.FIRST_NAME.eq("Sam")), "firstNameSam"); SquidCursor<TestModel> cursor = database .query(TestModel.class, Query.select(onCriterion).orderBy(Order.asc(TestModel.ID))); try { cursor.moveToFirst(); assertTrue(cursor.get(onCriterion)); cursor.moveToNext(); assertFalse(cursor.get(onCriterion)); } finally { cursor.close(); } }
/** * Sort the results in ascending ('ASC') order by the given expression */ public static Order asc(Object expression) { return new Order(expression); }
/** * 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()); }
/** * Sort the results in descending ('DESC') order by the given expression */ public static Order desc(Object expression) { return new Order(expression, OrderType.DESC); }