QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions); for (Entity entity : results) { resp.getWriter().println("<li>" + entity.getProperty("name") + "</li>");
private QueryResultList<Entity> generateResults(QueryBuilder<?> builder, boolean keysOnly) throws FalsePredicateException { QueryResultList<Entity> queryResult = prepareQuery(builder, keysOnly).asQueryResultList(configureFetchOptions(builder)); setCursor(builder, queryResult); return queryResult; }
public Entity fetchResult(PreparedQuery preparedQuery) { return preparedQuery.asQueryResultList(withDefaults()).get(0); } });
private QueryResultList<Entity> getNamespaces() { FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); fetchOptions.limit(BATCH_SIZE); if (cursor != null) { fetchOptions.startCursor(Cursor.fromWebSafeString(cursor)); } Query q = new Query(NAMESPACE_METADATA_KIND); q.setKeysOnly(); return datastore.prepare(q).asQueryResultList(fetchOptions); }
private QueryResultList<Entity> executeQuery(FetchOptions fetchOptions) { Query query = new Query("Foo").addSort("bar"); return service.prepare(query).asQueryResultList(fetchOptions); }
@Test public void testAsQueryResultList() throws Exception { QueryResultList<Entity> list = preparedQuery.asQueryResultList(withDefaults()); assertNotNull(list); assertEquals(1, list.size()); assertEquals(john, list.get(0)); } }
@Test public void testStartEndCursor() { int limit = total / testDat.length; Query query = new Query(kindName, rootKey); query.addSort("name", Query.SortDirection.ASCENDING); FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit); // fetch 1st page and get cursor1 QueryResultList<Entity> nextBatch = service.prepare(query) .asQueryResultList(fetchOption); Cursor cursor1 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString()); // fetch 2nd page and get cursor2 nextBatch = service.prepare(query).asQueryResultList(fetchOption.startCursor(cursor1)); Cursor cursor2 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString()); // cursor1 as start and cursor2 as end and 15 in limit -- -- should return 2nd page. checkPage(query, cursor1, cursor2, limit, limit, testDat[1], testDat[1]); // cursor1 as start and cursor2 as end and 30 in limit -- should return 2nd page. checkPage(query, cursor1, cursor2, 2 * limit, limit, testDat[1], testDat[1]); // cursor2 as start and cursor1 as end and 15 in limit -- should not return any. checkPage(query, cursor2, cursor1, limit, 0, null, null); }
@Test public void testIndexListFromList() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey("Person", "testKeysOnly"); Key key = parent.getKey(); Entity joe = createEntity("Person", key) .withProperty("name", "Joe") .withProperty("surname", "Moe") .store(); Query query = new Query("Person") .setAncestor(key) .setKeysOnly(); PreparedQuery preparedQuery = service.prepare(query); QueryResultList<Entity> list = preparedQuery.asQueryResultList(FetchOptions.Builder.withDefaults()); List<Index> indexes = list.getIndexList(); if (indexes != null) { // TODO -- something useful System.out.println("indexes = " + indexes); } } }
if (opts != null) { if (opts.getLimit() != null) { QueryResultList<Entity> entities = preparedQuery.asQueryResultList(opts); endCursor = entities.getCursor(); entityIterable = entities;
private Cursor checkPage(Query query, Cursor stCursor, Cursor endCursor, int limit, int exptRet, String chkSt, String chkEnd) { FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit); if (stCursor != null) { fetchOption = fetchOption.startCursor(stCursor); } if (endCursor != null) { fetchOption = fetchOption.endCursor(endCursor); } QueryResultList<Entity> nextBatch = service.prepare(query) .asQueryResultList(fetchOption); assertEquals(exptRet, nextBatch.size()); if (chkSt != null) { assertEquals(chkSt, nextBatch.get(0).getProperty("name")); } if (chkEnd != null) { assertEquals(chkEnd, nextBatch.get(nextBatch.size() - 1).getProperty("name")); } return nextBatch.getCursor(); } }
public static QueryResult queryEntityPrivate(LeanQuery leanQuery) throws LeanException { LeanAccount account = findCurrentAccount(); Query query = new Query(leanQuery.getKind()); query.addFilter("_account", Query.FilterOperator.EQUAL, account.id); for (QueryFilter queryFilter : leanQuery.getFilters()) { query.addFilter( queryFilter.getProperty(), queryFilter.getOperator().getFilterOperator(), queryFilter.getValue()); } for (QuerySort querySort : leanQuery.getSorts()) { query.addSort(querySort.getProperty(), querySort.getDirection().getSortDirection()); } FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); if(leanQuery.getCursor() != null ) fetchOptions.startCursor(leanQuery.getCursor()); if(leanQuery.getOffset() != null) fetchOptions.offset(leanQuery.getOffset()); if(leanQuery.getLimit() != null) fetchOptions.limit(leanQuery.getLimit()); try { PreparedQuery pq = datastore.prepare(query); QueryResultList<Entity> result; result = pq.asQueryResultList(fetchOptions); return new QueryResult(result, result.getCursor()); } catch (DatastoreNeedIndexException dnie) { throw new LeanException(LeanException.Error.AppEngineMissingIndex); } }
@Test public void testQueriesWithDifferentAncestorsInsideSameTransactionNoUsage() { Transaction tx = service.beginTransaction(); try { Key a1 = KeyFactory.createKey("ancestor", "1"); prepareQueryWithAncestor(tx, a1).asIterator(); Key a2 = KeyFactory.createKey("ancestor", "2"); prepareQueryWithAncestor(tx, a2).asList(FetchOptions.Builder.withDefaults()); Key a3 = KeyFactory.createKey("ancestor", "3"); prepareQueryWithAncestor(tx, a3).asIterable(); Key a4 = KeyFactory.createKey("ancestor", "4"); prepareQueryWithAncestor(tx, a4).asQueryResultIterable(); Key a5 = KeyFactory.createKey("ancestor", "5"); prepareQueryWithAncestor(tx, a5).asQueryResultIterator(); Key a6 = KeyFactory.createKey("ancestor", "6"); prepareQueryWithAncestor(tx, a6).asQueryResultList(FetchOptions.Builder.withDefaults()); } finally { tx.rollback(); } }