com.yahoo.squidb.sql.Query.orderBy()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(461)

本文整理了Java中com.yahoo.squidb.sql.Query.orderBy方法的一些代码示例,展示了Query.orderBy的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.orderBy方法的具体详情如下:
包路径:com.yahoo.squidb.sql.Query
类名称:Query
方法名:orderBy

Query.orderBy介绍

[英]Add an ORDER BY clause (or an additional ordering term) to this query
[中]向该查询中添加ORDER BY子句(或附加的订购术语)

代码示例

代码示例来源:origin: yahoo/squidb

  1. /**
  2. * Add an ORDER BY clause (or an additional ordering term) to this query
  3. *
  4. * @param orders one or more ordering terms
  5. * @return this Query object, to allow chaining method calls
  6. */
  7. public Query orderBy(Order... orders) {
  8. if (immutable) {
  9. return fork().orderBy(orders);
  10. }
  11. if (this.orders == null) {
  12. this.orders = new ArrayList<>();
  13. }
  14. SquidUtilities.addAll(this.orders, orders);
  15. invalidateCompileCache();
  16. return this;
  17. }

代码示例来源:origin: yahoo/squidb

  1. /**
  2. * Build a {@link Query} combining this object's internal state with the arguments passed. If a
  3. * {@link ProjectionMap} is set, the projection elements will be evaluated and transformed accordingly. If the
  4. * sortOrder is null or empty, the default order will be used (if one was set).
  5. *
  6. * @param projection the raw column names to be selected
  7. * @param selection a raw selection string
  8. * @param selectionArgs array of strings which substitute replaceable arguments in the selection string
  9. * @param sortOrder a raw ordering clause
  10. * @return a {@link Query} using the projection, selection, selection args, and sort order
  11. */
  12. public Query build(String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  13. Query query = Query.select(computeProjection(projection)).from(dataSource);
  14. boolean hasUserSelection = !SqlUtils.isEmpty(selection);
  15. if (hasUserSelection) {
  16. query.where(Criterion.fromRawSelection(selection, selectionArgs));
  17. }
  18. if (!SqlUtils.isEmpty(sortOrder)) {
  19. query.orderBy(Order.fromExpression(sortOrder));
  20. } else if (defaultOrder != null && defaultOrder.length > 0) {
  21. query.orderBy(defaultOrder);
  22. }
  23. if (strictMode && hasUserSelection) {
  24. query.requestValidation();
  25. }
  26. return query;
  27. }

代码示例来源:origin: yahoo/squidb

  1. public void testIdColumnForTableModels() {
  2. Query query = Query.select(TestModel.PROPERTIES).orderBy(TestModel.ID.asc());
  3. testCursorAdapterInternal(new TestModel(), null, query, new CursorAdapterTest() {
  4. @Override
  5. public void testCursorAdapter(SquidCursorAdapter<AbstractModel> adapter) {
  6. assertTrue(adapter.hasStableIds());
  7. assertEquals(1, adapter.getItemId(0));
  8. assertEquals(2, adapter.getItemId(1));
  9. }
  10. });
  11. }

代码示例来源:origin: yahoo/squidb

  1. public void testReusableModel() {
  2. Query query = Query.select(TestModel.PROPERTIES).orderBy(TestModel.ID.asc());
  3. testCursorAdapterInternal(new TestModel(), null, query, new CursorAdapterTest() {
  4. @Override
  5. public void testCursorAdapter(SquidCursorAdapter<AbstractModel> adapter) {
  6. AbstractModel first = adapter.getItem(0);
  7. AbstractModel second = adapter.getItem(1);
  8. assertEquals(first, second);
  9. }
  10. });
  11. }

代码示例来源:origin: yahoo/squidb

  1. public void testInCriterion() {
  2. List<String> expectedNames = Arrays.asList("bigBird", "cookieMonster");
  3. Query query = Query.select().where(Employee.NAME.in("bigBird", "cookieMonster")).orderBy(Employee.NAME.asc());
  4. testInQuery(expectedNames, query);
  5. query = Query.select().where(Employee.NAME.notIn("bigBird", "cookieMonster")).orderBy(Employee.NAME.asc());
  6. testInQuery(Arrays.asList("bert", "elmo", "ernie", "oscar"), query);
  7. List<String> list = Arrays.asList("bigBird", "cookieMonster");
  8. query = Query.select().where(Employee.NAME.in(list)).orderBy(Employee.NAME.asc());
  9. testInQuery(expectedNames, query);
  10. // Test off-by-one error that used to occur when the in criterion wasn't the last criterion in the list
  11. query = Query.select().where(Employee.NAME.in(list).or(Field.field("1").neq(1))).orderBy(Employee.NAME.asc());
  12. testInQuery(expectedNames, query);
  13. }

代码示例来源:origin: yahoo/squidb

  1. public Query getOrderedTasksWithTags() {
  2. Function<Long> unixNow = Function.multiply(1000, Function.functionWithArguments("strftime", "%s", "now"));
  3. Function<Long> sinceCompletion = Function.subtract(unixNow, Task.COMPLETION_DATE);
  4. return getTasksWithTagsQuery(Task.COMPLETION_DATE.eq(0)
  5. .or(sinceCompletion.lt(60000 * 5)))
  6. .orderBy(Function.caseWhen(Task.DUE_DATE.neq(0)).desc(), Task.DUE_DATE.asc());
  7. }

代码示例来源:origin: yahoo/squidb

  1. private void testBetween(List<Long> expectedIds, boolean useNotBetween) {
  2. SquidCursor<Employee> cursor = database.query(Employee.class,
  3. Query.select(Employee.ID).where(useNotBetween ? Employee.ID.notBetween(2, 5) :
  4. Employee.ID.between(2, 5)).orderBy(Employee.ID.asc()));
  5. try {
  6. assertEquals(expectedIds.size(), cursor.getCount());
  7. for (Long id : expectedIds) {
  8. cursor.moveToNext();
  9. assertEquals(id.longValue(), cursor.get(Employee.ID).longValue());
  10. }
  11. } finally {
  12. cursor.close();
  13. }
  14. }

代码示例来源:origin: yahoo/squidb

  1. public void testCustomIdColumn() {
  2. Function<Long> idSquared = Function.rawFunction("_id * _id");
  3. LongProperty idSquaredProperty = LongProperty.fromFunction(idSquared, "idSquared");
  4. Query query = Query.select(TestModel.PROPERTIES).selectMore(idSquaredProperty).orderBy(TestModel.ID.asc());
  5. testCursorAdapterInternal(new TestModel(), idSquaredProperty, query, new CursorAdapterTest() {
  6. @Override
  7. public void testCursorAdapter(SquidCursorAdapter<AbstractModel> adapter) {
  8. assertTrue(adapter.hasStableIds());
  9. assertEquals(1, adapter.getItemId(0));
  10. assertEquals(4, adapter.getItemId(1));
  11. }
  12. });
  13. }

代码示例来源:origin: yahoo/squidb

  1. public void testSelectDistinct() {
  2. Query query = Query.selectDistinct(Employee.MANAGER_ID).orderBy(Employee.MANAGER_ID.asc());
  3. SquidCursor<Employee> cursor = database.query(Employee.class, query);
  4. try {
  5. assertEquals(4, cursor.getCount());
  6. cursor.moveToFirst();
  7. assertEquals(Long.valueOf(0), cursor.get(Employee.MANAGER_ID));
  8. cursor.moveToNext();
  9. assertEquals(Long.valueOf(1), cursor.get(Employee.MANAGER_ID));
  10. cursor.moveToNext();
  11. assertEquals(Long.valueOf(2), cursor.get(Employee.MANAGER_ID));
  12. cursor.moveToNext();
  13. assertEquals(Long.valueOf(5), cursor.get(Employee.MANAGER_ID));
  14. } finally {
  15. cursor.close();
  16. }
  17. }

代码示例来源:origin: yahoo/squidb

  1. private void testGlob(List<Employee> expected, boolean useNotGlob) {
  2. SquidCursor<Employee> cursor = database.query(Employee.class,
  3. Query.select(Employee.ID, Employee.NAME).where(useNotGlob ? Employee.NAME.notGlob("b*") :
  4. Employee.NAME.glob("b*"))
  5. .orderBy(Employee.ID.asc()));
  6. try {
  7. assertEquals(expected.size(), cursor.getCount());
  8. for (Employee e : expected) {
  9. cursor.moveToNext();
  10. assertEquals(e.getRowId(), cursor.get(Employee.ID).longValue());
  11. assertEquals(e.getName(), cursor.get(Employee.NAME));
  12. }
  13. } finally {
  14. cursor.close();
  15. }
  16. }

代码示例来源:origin: yahoo/squidb

  1. private void testRecyclerAdapterInternal(LongProperty idProperty, RecyclerAdapterTest test) {
  2. Query query = Query.select(TestModel.PROPERTIES)
  3. .orderBy(TestModel.BIRTHDAY.asc())
  4. .limit(2);
  5. if (idProperty != null) {
  6. query.selectMore(idProperty);
  7. }
  8. SquidCursor<TestModel> cursor = database.query(TestModel.class, query);
  9. TestRecyclerAdapter adapter = new TestRecyclerAdapter(idProperty);
  10. adapter.changeCursor(cursor);
  11. try {
  12. test.testRecyclerAdapter(adapter);
  13. } finally {
  14. cursor.close();
  15. }
  16. }

代码示例来源:origin: yahoo/squidb

  1. public void testOrderByArray() {
  2. Long[] order = new Long[]{5L, 1L, 4L};
  3. SquidCursor<Employee> cursor = database.query(Employee.class,
  4. Query.select(Employee.ID).limit(order.length).orderBy(Employee.ID.byArray(order)));
  5. try {
  6. assertEquals(order.length, cursor.getCount());
  7. for (int i = 0; i < order.length; i++) {
  8. cursor.moveToPosition(i);
  9. assertEquals(order[i], cursor.get(Employee.ID));
  10. }
  11. } finally {
  12. cursor.close();
  13. }
  14. }

代码示例来源:origin: yahoo/squidb

  1. public void testBooleanFunctionOnCriterion() {
  2. BooleanProperty onCriterion = BooleanProperty
  3. .fromFunction(Function.caseWhen(TestModel.FIRST_NAME.eq("Sam")), "firstNameSam");
  4. SquidCursor<TestModel> cursor = database
  5. .query(TestModel.class, Query.select(onCriterion).orderBy(Order.asc(TestModel.ID)));
  6. try {
  7. cursor.moveToFirst();
  8. assertTrue(cursor.get(onCriterion));
  9. cursor.moveToNext();
  10. assertFalse(cursor.get(onCriterion));
  11. } finally {
  12. cursor.close();
  13. }
  14. }

代码示例来源:origin: yahoo/squidb

  1. public void testReverseOrder() {
  2. long max = database.countAll(Employee.class);
  3. SquidCursor<Employee> cursor = database.query(Employee.class,
  4. Query.select(Employee.ID).orderBy(Employee.ID.asc().reverse()));
  5. try {
  6. assertEquals(max, cursor.getCount());
  7. assertTrue(max > 0);
  8. while (cursor.moveToNext()) {
  9. long nextId = cursor.get(Employee.ID);
  10. if (nextId > max) {
  11. fail("IDs not in reverse order");
  12. }
  13. max = nextId;
  14. }
  15. } finally {
  16. cursor.close();
  17. }
  18. }

代码示例来源:origin: yahoo/squidb

  1. public void testIntersect() {
  2. Query query = Query.select().from(Employee.TABLE).where(Employee.MANAGER_ID.eq(1))
  3. .intersect(Query.select().from(Employee.TABLE).where(Employee.ID.eq(2)))
  4. .orderBy(Employee.ID.asc());
  5. SquidCursor<Employee> cursor = database.query(Employee.class, query);
  6. try {
  7. assertEquals(1, cursor.getCount());
  8. cursor.moveToFirst();
  9. assertEquals(cookieMonster, new Employee(cursor));
  10. } finally {
  11. cursor.close();
  12. }
  13. }

代码示例来源:origin: yahoo/squidb

  1. public void testExcept() {
  2. Query query = Query.select().from(Employee.TABLE).where(Employee.MANAGER_ID.eq(1))
  3. .except(Query.select().from(Employee.TABLE).where(Employee.ID.eq(2)))
  4. .orderBy(Employee.ID.asc());
  5. SquidCursor<Employee> cursor = database.query(Employee.class, query);
  6. try {
  7. assertEquals(2, cursor.getCount());
  8. cursor.moveToFirst();
  9. assertEquals(elmo, new Employee(cursor));
  10. cursor.moveToNext();
  11. assertEquals(oscar, new Employee(cursor));
  12. } finally {
  13. cursor.close();
  14. }
  15. }

代码示例来源:origin: yahoo/squidb

  1. public void testSelectFromView() {
  2. View view = View.fromQuery(Query.select(Employee.PROPERTIES)
  3. .from(Employee.TABLE).where(Employee.MANAGER_ID.eq(bigBird.getRowId())), "bigBirdsEmployees");
  4. database.tryCreateView(view);
  5. Query fromView = Query.fromView(view).orderBy(view.qualifyField(Employee.ID).asc());
  6. SquidCursor<Employee> cursor = database.query(Employee.class, fromView);
  7. try {
  8. assertEquals(3, cursor.getCount());
  9. cursor.moveToFirst();
  10. assertEquals("cookieMonster", cursor.get(Employee.NAME));
  11. cursor.moveToNext();
  12. assertEquals("elmo", cursor.get(Employee.NAME));
  13. cursor.moveToNext();
  14. assertEquals("oscar", cursor.get(Employee.NAME));
  15. } finally {
  16. cursor.close();
  17. }
  18. }

代码示例来源:origin: yahoo/squidb

  1. public void testUnion() {
  2. Query query = Query.select().from(Employee.TABLE).where(Employee.MANAGER_ID.eq(1))
  3. .union(Query.select().from(Employee.TABLE).where(Employee.ID.eq(2)))
  4. .orderBy(Employee.ID.asc());
  5. SquidCursor<Employee> cursor = database.query(Employee.class, query);
  6. try {
  7. assertEquals(3, cursor.getCount());
  8. cursor.moveToFirst();
  9. assertEquals(cookieMonster, new Employee(cursor));
  10. cursor.moveToNext();
  11. assertEquals(elmo, new Employee(cursor));
  12. cursor.moveToNext();
  13. assertEquals(oscar, new Employee(cursor));
  14. } finally {
  15. cursor.close();
  16. }
  17. }

代码示例来源:origin: yahoo/squidb

  1. public void testLimitAndOffsetWithExpressions() {
  2. // limit = 1 + (count(*) / 4), offset = count(*) / 2
  3. Field<Integer> limit = Function.add(1, Function.divide(
  4. Query.select(IntegerProperty.countProperty()).from(Employee.TABLE).asFunction(), 4));
  5. Field<Integer> offset = Function.divide(
  6. Query.select(IntegerProperty.countProperty()).from(Employee.TABLE).asFunction(), 2);
  7. Query query = Query.select().orderBy(Employee.NAME.asc()).limit(limit, offset);
  8. SquidCursor<Employee> cursor = database.query(Employee.class, query);
  9. try {
  10. assertEquals(2, cursor.getCount());
  11. cursor.moveToFirst();
  12. assertEquals(elmo, new Employee(cursor));
  13. cursor.moveToNext();
  14. assertEquals(ernie, new Employee(cursor));
  15. } finally {
  16. cursor.close();
  17. }
  18. }

代码示例来源:origin: yahoo/squidb

  1. public void testUnionAll() {
  2. Query query = Query.select().from(Employee.TABLE).where(Employee.MANAGER_ID.eq(1))
  3. .unionAll(Query.select().from(Employee.TABLE).where(Employee.ID.eq(2)))
  4. .orderBy(Employee.ID.asc());
  5. SquidCursor<Employee> cursor = database.query(Employee.class, query);
  6. try {
  7. assertEquals(4, cursor.getCount());
  8. cursor.moveToFirst();
  9. assertEquals(cookieMonster, new Employee(cursor));
  10. cursor.moveToNext();
  11. assertEquals(cookieMonster, new Employee(cursor));
  12. cursor.moveToNext();
  13. assertEquals(elmo, new Employee(cursor));
  14. cursor.moveToNext();
  15. assertEquals(oscar, new Employee(cursor));
  16. } finally {
  17. cursor.close();
  18. }
  19. }

相关文章