@Test public void ScalarQueries() { BooleanExpression filter = product.name.startsWith("A"); // count assertEquals(10l, sql().from(product).where(filter).count()); // countDistinct assertEquals(10l, sql().from(product).where(filter).distinct().count()); // list assertEquals(10, sql().from(product).where(filter).list(product.name).size()); // list with limit assertEquals(3, sql().from(product).limit(3).list(product.name).size()); // list with offset // assertEquals(7, sql().from(product).offset(3).list(product.name).size()); // list with limit and offset assertEquals(3, sql().from(product).offset(3).limit(3).list(product.name).size()); // list multiple for (Tuple row : sql().from(product).list(product.productId, product.name, product.amount)) { assertNotNull(row.get(0, Object.class)); assertNotNull(row.get(1, Object.class)); assertNotNull(row.get(2, Object.class)); } // listResults SearchResults<String> results = sql().from(product).limit(3).listResults(product.name); assertEquals(3, results.getResults().size()); assertEquals(30l, results.getTotal()); }
@Test public void ListResults() { SearchResults<User> results = query().limit(2).orderBy(user.age.asc()).listResults(); assertEquals(4l, results.getTotal()); assertEquals(2, results.getResults().size()); results = query().offset(2).orderBy(user.age.asc()).listResults(); assertEquals(4l, results.getTotal()); assertEquals(2, results.getResults().size()); }
@SuppressWarnings("unchecked") public <RT> SearchResults<RT> listResults(Expression<RT> expr) { queryMixin.addToProjection(expr); Query countQuery = createQuery(true); countQuery.setUnique(true); countQuery.setResult("count(this)"); long total = (Long) execute(countQuery); if (total > 0) { QueryModifiers modifiers = queryMixin.getMetadata().getModifiers(); Query query = createQuery(false); reset(); return new SearchResults<RT>((List<RT>) execute(query), modifiers, total); } else { reset(); return SearchResults.emptyResults(); } }
public <RT> SearchResults<RT> listResults(Expression<RT> expr) { getQueryMixin().addToProjection(expr); Query countQuery = createQuery(toCountRowsString(), null, true); long total = (Long) countQuery.uniqueResult(); try { if (total > 0) { QueryModifiers modifiers = getMetadata().getModifiers(); String queryString = toQueryString(); logQuery(queryString); Query query = createQuery(queryString, modifiers, false); @SuppressWarnings("unchecked") List<RT> list = query.list(); return new SearchResults<RT>(list, modifiers, total); } else { return SearchResults.emptyResults(); } } finally { reset(); } }
@Test public void EmptyResults() { SearchResults<User> results = query().where(user.firstName.eq("XXX")).listResults(); assertEquals(0l, results.getTotal()); assertEquals(Collections.emptyList(), results.getResults()); }