com.google.appengine.api.datastore.Query Java Examples

The following examples show how to use com.google.appengine.api.datastore.Query. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 9 votes vote down vote up
@Test
public void queryInterface_orFilter_printsMatchedEntities() throws Exception {
  // Arrange
  Entity a = new Entity("Person", "a");
  a.setProperty("height", 100);
  Entity b = new Entity("Person", "b");
  b.setProperty("height", 150);
  Entity c = new Entity("Person", "c");
  c.setProperty("height", 200);
  datastore.put(ImmutableList.<Entity>of(a, b, c));

  StringWriter buf = new StringWriter();
  PrintWriter out = new PrintWriter(buf);
  long minHeight = 125;
  long maxHeight = 175;

  // Act
  // [START gae_java8_datastore_interface_3]
  Filter tooShortFilter = new FilterPredicate("height", FilterOperator.LESS_THAN, minHeight);

  Filter tooTallFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN, maxHeight);

  Filter heightOutOfRangeFilter = CompositeFilterOperator.or(tooShortFilter, tooTallFilter);

  Query q = new Query("Person").setFilter(heightOutOfRangeFilter);
  // [END gae_java8_datastore_interface_3]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(a, c);
}
 
Example #2
Source File: DatastoreMultitenancyTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testQueriesByAncestorInOtherNamespaceThrowsIllegalArgumentException() {
    deleteNsKinds("one", "foo");
    deleteNsKinds("two", "foo");
    sync();

    NamespaceManager.set("one");
    Entity fooOne = new Entity("foo");
    service.put(fooOne);

    NamespaceManager.set("two");
    Entity fooTwo = new Entity("foo");
    service.put(fooTwo);
    sync();

    // java.lang.IllegalArgumentException: Namespace of ancestor key and query must match.
    service.prepare(new Query("foo").setAncestor(fooOne.getKey())).asList(withDefaults());
}
 
Example #3
Source File: QueryOptimizationsTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testOrderOfReturnedResultsIsSameAsOrderOfElementsInInStatementWhenUsingProjections() throws Exception {
    String methodName = "testOrderOfReturnedResultsIsSameAsOrderOfElementsInInStatementWhenUsingProjections";
    Entity parent = createTestEntityWithUniqueMethodNameKey("Product", methodName);
    Key key = parent.getKey();

    Entity b = createEntity("Product", key)
        .withProperty("name", "b")
        .withProperty("price", 1L)
        .store();

    Entity a = createEntity("Product", key)
        .withProperty("name", "a")
        .withProperty("price", 2L)
        .store();

    Query query = new Query("Product")
        .setAncestor(key)
        .addProjection(new PropertyProjection("price", Long.class))
        .setFilter(new Query.FilterPredicate("name", IN, Arrays.asList("a", "b")));
    assertResultsInOrder(query, a, b);

    query = query.setFilter(new Query.FilterPredicate("name", IN, Arrays.asList("b", "a")));
    assertResultsInOrder(query, b, a);
}
 
Example #4
Source File: CableKeyPair.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
public static KeyPair get(Long sessionId) throws IOException {
  Key sessionKey = KeyFactory.createKey(SessionData.KIND, sessionId);

  Query query = new Query(KIND).setAncestor(sessionKey);

  List<Entity> results =
      Datastore.getDatastore().prepare(query).asList(FetchOptions.Builder.withDefaults());
  
  for (Entity result : results) {
    if (result.getParent().getId() == sessionId.longValue()) {
      byte[] serializedKeyPair =
          BaseEncoding.base64().decode((String) result.getProperty(KEY_PAIR_PROPERTY));
      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedKeyPair));
      try {
        return (KeyPair) in.readObject();
      } catch (ClassNotFoundException e1) {
        throw new IOException(e1);
      }
    }
  }
  throw new IOException("KeyPair " + String.valueOf(sessionId) + "not found");
}
 
Example #5
Source File: IndexQueryTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Before
public void addData() throws InterruptedException {
    Query query = new Query(kindName, rootKey);
    if (service.prepare(query).countEntities(fetchOption) == 0) {
        List<Entity> elist = new ArrayList<Entity>();
        for (int i = 0; i < count; i++) {
            Entity newRec = new Entity(kindName, rootKey);
            newRec.setProperty("stringData", "string test data " + i);
            newRec.setProperty("intData", 10 * i);
            newRec.setProperty("stringList", Arrays.asList("abc" + i, "xyz" + i, "abc" + i));
            newRec.setProperty("intList", Arrays.asList(i, 50 + i, 90 + i));
            newRec.setProperty("timestamp", new Date());
            newRec.setProperty("floatData", new Float(i + 0.1));
            newRec.setProperty("ratingData", new Rating(i + 20));
            newRec.setProperty("booleanData", true);
            newRec.setProperty("geoptData", new GeoPt((float) (i * 20 - 90), new Float(i * 30 - 179.1)));
            newRec.setProperty("byteStrProp", new ShortBlob(("shortblob" + (i * 30)).getBytes()));
            elist.add(newRec);
        }
        service.put(elist);
        sync(waitTime);
    }
}
 
Example #6
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooks(String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title"
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example #7
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooks(String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title"
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example #8
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Book> listBooksByUser(String userId, String startCursorString) {
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time
  if (startCursorString != null && !startCursorString.equals("")) {
    fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off
  }
  Query query = new Query(BOOK_KIND) // We only care about Books
      // Only for this user
      .setFilter(new Query.FilterPredicate(
          Book.CREATED_BY_ID, Query.FilterOperator.EQUAL, userId))
      // a custom datastore index is required since you are filtering by one property
      // but ordering by another
      .addSort(Book.TITLE, SortDirection.ASCENDING);
  PreparedQuery preparedQuery = datastore.prepare(query);
  QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);

  List<Book> resultBooks = entitiesToBooks(results);     // Retrieve and convert Entities
  Cursor cursor = results.getCursor();              // Where to start next time
  if (cursor != null && resultBooks.size() == 10) {         // Are we paging? Save Cursor
    String cursorString = cursor.toWebSafeString();               // Cursors are WebSafe
    return new Result<>(resultBooks, cursorString);
  } else {
    return new Result<>(resultBooks);
  }
}
 
Example #9
Source File: DatastoreMultitenancyTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueriesOnlyReturnResultsInCurrentNamespace() {
    deleteNsKinds("one", "foo");
    deleteNsKinds("two", "foo");
    sync();

    NamespaceManager.set("one");
    Entity fooOne = new Entity("foo");
    service.put(fooOne);

    NamespaceManager.set("two");
    Entity fooTwo = new Entity("foo");
    service.put(fooTwo);
    sync();

    List<Entity> listTwo = service.prepare(new Query("foo").setAncestor(fooTwo.getKey())).asList(withDefaults());
    assertEquals(Collections.singletonList(fooTwo), listTwo);

    NamespaceManager.set("one");
    List<Entity> listOne = service.prepare(new Query("foo").setAncestor(fooOne.getKey())).asList(withDefaults());
    assertEquals(Collections.singletonList(fooOne), listOne);

    service.delete(fooOne.getKey());
    service.delete(fooTwo.getKey());
}
 
Example #10
Source File: QueryBasicsTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleFilters() throws Exception {
    Key parentKey = createQueryBasicsTestParent("testMultipleFilters");
    Entity johnDoe = createEntity("Person", parentKey)
        .withProperty("name", "John")
        .withProperty("lastName", "Doe")
        .store();

    Entity johnBooks = createEntity("Person", parentKey)
        .withProperty("name", "John")
        .withProperty("lastName", "Books")
        .store();

    Entity janeDoe = createEntity("Person", parentKey)
        .withProperty("name", "Jane")
        .withProperty("lastName", "Doe")
        .store();

    Query query = new Query("Person")
        .setAncestor(parentKey)
        .setFilter(and(
            new Query.FilterPredicate("name", EQUAL, "John"),
            new Query.FilterPredicate("lastName", EQUAL, "Doe")));

    assertSingleResult(johnDoe, query);
}
 
Example #11
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void queryRestrictions_missingSortOnInequality_isInvalid() throws Exception {
  long minBirthYear = 1940;
  // [START gae_java8_datastore_inequality_filters_sort_orders_invalid_1]
  Filter birthYearMinFilter =
      new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);

  // Not valid. Missing sort on birthYear.
  Query q =
      new Query("Person")
          .setFilter(birthYearMinFilter)
          .addSort("lastName", SortDirection.ASCENDING);
  // [END gae_java8_datastore_inequality_filters_sort_orders_invalid_1]

  // Note: The local devserver behavior is different than the production
  // version of Cloud Datastore, so there aren't any assertions we can make
  // in this test.  The query appears to work with the local test runner,
  // but will fail in production.
}
 
Example #12
Source File: LocalDatastoreSmoketestServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  resp.setContentType("text/plain");

  DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
  Entity e = new Entity("foo");
  e.setProperty("foo", 23);
  Key key = ds.put(e);

  try {
    e = ds.get(key);
  } catch (EntityNotFoundException e1) {
    throw new ServletException(e1);
  }

  e.setProperty("bar", 44);
  ds.put(e);

  Query q = new Query("foo");
  q.addFilter("foo", Query.FilterOperator.GREATER_THAN_OR_EQUAL, 22);
  Iterator<Entity> iter = ds.prepare(q).asIterator();
  iter.next();
}
 
Example #13
Source File: TestBase.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
private static <T extends TempData> List<T> getAllTempData(Class<T> type, boolean unreadOnly) {
    try {
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        String kind = getKind(type);
        Query query = new Query(kind);
        if (unreadOnly) {
            query.setFilter(new Query.FilterPredicate(TEMP_DATA_READ_PROPERTY, Query.FilterOperator.EQUAL, false));
        } else {
            query.addSort("timestamp", Query.SortDirection.ASCENDING);
        }
        PreparedQuery pq = ds.prepare(query);
        Iterator<Entity> iter = pq.asIterator();
        List<T> result = new ArrayList<>();
        while (iter.hasNext()) {
            Entity entity = iter.next();
            T data = readTempData(type, entity, ds);
            result.add(data);
        }
        return result;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
 
Example #14
Source File: QueryTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithPropertyProjection() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("geoptData", GeoPt.class));
    Filter filter1 = Query.CompositeFilterOperator.or(
        Query.FilterOperator.LESS_THAN.of("intList", 5),
        Query.FilterOperator.GREATER_THAN.of("intList", 90));
    Filter filter2 = Query.FilterOperator.EQUAL.of("intList", 52);
    query.setFilter(Query.CompositeFilterOperator.and(filter1, filter2));
    // sql statement
    String sql = "SELECT geoptData FROM " + kindName;
    sql += " WHERE ((intList < 5 or intList > 90) AND intList = 52)";
    sql += " AND __ancestor__ is " + rootKey;
    assertEquals(sql.toLowerCase(), query.toString().toLowerCase());
    // check query result
    List<Entity> results = service.prepare(query).asList(fo);
    Assert.assertTrue(results.size() > 0);
    assertEquals(new GeoPt((float) (2.12), (float) (2.98)), results.get(0).getProperty("geoptData"));
    for (Entity e : results) {
        assertEquals(1, e.getProperties().size());
        assertTrue(e.getProperties().containsKey("geoptData"));
    }
}
 
Example #15
Source File: QueryFilteringByGAEPropertyTypesTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testEntityKeyInequalityFilter() {
    Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, "testFilterByInequalityFilter");
    Key parentKey = parentEntity.getKey();

    Entity entity1 = new Entity("foo", parentKey);
    service.put(entity1);

    Entity entity2 = new Entity("foo", parentKey);
    service.put(entity2);

    Query query = new Query("foo")
        .setAncestor(parentKey)
        .setFilter(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, GREATER_THAN, entity1.getKey()));
    List<Entity> list = service.prepare(query).asList(FetchOptions.Builder.withDefaults());
    assertEquals(1, list.size());
    assertEquals(entity2.getKey(), list.get(0).getKey());
}
 
Example #16
Source File: QueryOptimizationsTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testKeysOnly() throws Exception {
    Entity parent = createTestEntityWithUniqueMethodNameKey("Person", "testKeysOnly");
    Key key = parent.getKey();

    Entity john = createEntity("Person", key)
        .withProperty("name", "John")
        .withProperty("surname", "Doe")
        .store();

    Query query = new Query("Person")
        .setAncestor(key)
        .setKeysOnly();

    PreparedQuery preparedQuery = service.prepare(query);

    Entity entity = preparedQuery.asSingleEntity();
    assertEquals(john.getKey(), entity.getKey());
    assertNull(entity.getProperty("name"));
    assertNull(entity.getProperty("surname"));
}
 
Example #17
Source File: AppEngineBackEnd.java    From appengine-pipelines with Apache License 2.0 6 votes vote down vote up
private void deleteAll(final String kind, final Key rootJobKey) {
  logger.info("Deleting all " + kind + " with rootJobKey=" + rootJobKey);
  final int chunkSize = 100;
  final FetchOptions fetchOptions = FetchOptions.Builder.withChunkSize(chunkSize);
  final PreparedQuery preparedQuery = dataStore.prepare(new Query(kind).setKeysOnly().setFilter(
      new FilterPredicate(ROOT_JOB_KEY_PROPERTY, EQUAL, rootJobKey)));
  tryFiveTimes(new Operation<Void>("delete") {
    @Override
    public Void call() {
      Iterator<Entity> iter = preparedQuery.asIterator(fetchOptions);
      while (iter.hasNext()) {
        ArrayList<Key> keys = new ArrayList<>(chunkSize);
        for (int i = 0; i < chunkSize && iter.hasNext(); i++) {
          keys.add(iter.next().getKey());
        }
        logger.info("Deleting  " + keys.size() + " " + kind + "s with rootJobKey=" + rootJobKey);
        dataStore.delete(null, keys);
      }
      return null;
    }
  });
}
 
Example #18
Source File: MetadataPropertiesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
List<String> propertiesOfKind(DatastoreService ds, String kind) {

    // Start with unrestricted keys-only property query
    Query q = new Query(Entities.PROPERTY_METADATA_KIND).setKeysOnly();

    // Limit to specified kind
    q.setAncestor(Entities.createKindKey(kind));

    // Initialize result list
    ArrayList<String> results = new ArrayList<String>();

    //Build list of query results
    for (Entity e : ds.prepare(q).asIterable()) {
      results.add(e.getKey().getName());
    }

    // Return result list
    return results;
  }
 
Example #19
Source File: QueryOptimizationsTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testProjectionQueryOnlyReturnsEntitiesContainingProjectedProperty() throws Exception {
    String methodName = "testProjectionQueryOnlyReturnsEntitiesContainingProjectedProperty";
    Entity parent = createTestEntityWithUniqueMethodNameKey("Kind", methodName);
    Key key = parent.getKey();

    Entity e1 = createEntity("Kind", key)
        .withProperty("foo", "foo")
        .store();

    Entity e2 = createEntity("Kind", key)
        .withProperty("bar", "bar")
        .store();

    Query query = new Query("Kind")
        .setAncestor(key)
        .addProjection(new PropertyProjection("foo", String.class));

    List<Entity> results = service.prepare(query).asList(withDefaults());
    assertEquals(Collections.singletonList(e1), results);
}
 
Example #20
Source File: PersistingTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void putStoresEntity() throws Exception {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Entity client = new Entity("Client");
    client.setProperty("username", "alesj");
    client.setProperty("password", "password");
    final Key key = ds.put(client);
    try {
        Query query = new Query("Client");
        query.setFilter(new Query.FilterPredicate("username", Query.FilterOperator.EQUAL, "alesj"));
        PreparedQuery pq = ds.prepare(query);
        Entity result = pq.asSingleEntity();
        Assert.assertNotNull(result);
        Assert.assertEquals(key, result.getKey());
        Assert.assertEquals("alesj", result.getProperty("username"));
        Assert.assertEquals("password", result.getProperty("password"));
    } finally {
        ds.delete(key);
    }
}
 
Example #21
Source File: QueryFilteringByGAEPropertyTypesTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilterByEntityKey() {
    Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, "testFilterByEntityKey");
    Key parentKey = parentEntity.getKey();

    Key fooKey = KeyFactory.createKey(parentKey, "foo", 1);
    Entity fooEntity = new Entity(fooKey);
    service.put(fooEntity);

    Query query = new Query("foo")
        .setAncestor(parentKey)
        .setFilter(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, EQUAL, fooKey));

    PreparedQuery preparedQuery = service.prepare(query);
    List<Entity> results = preparedQuery.asList(FetchOptions.Builder.withDefaults());

    assertEquals(1, results.size());
    assertEquals(fooEntity, results.get(0));
}
 
Example #22
Source File: QueryResultTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #23
Source File: AppEngineBackEnd.java    From appengine-pipelines with Apache License 2.0 6 votes vote down vote up
@Override
public Set<String> getRootPipelinesDisplayName() {
  Query query = new Query(JobRecord.DATA_STORE_KIND);
  query.addProjection(
      new PropertyProjection(JobRecord.ROOT_JOB_DISPLAY_NAME, String.class));
  query.setDistinct(true);
  final PreparedQuery preparedQuery = dataStore.prepare(query);
  return tryFiveTimes(new Operation<Set<String>>("getRootPipelinesDisplayName") {
    @Override
    public Set<String> call() {
      Set<String> pipelines = new LinkedHashSet<>();
      for (Entity entity : preparedQuery.asIterable()) {
        pipelines.add((String) entity.getProperty(JobRecord.ROOT_JOB_DISPLAY_NAME));
      }
      return pipelines;
    }
  });
}
 
Example #24
Source File: SecurityChecker.java    From io2014-codelabs with Apache License 2.0 6 votes vote down vote up
/**
 * Creates {@link com.google.appengine.api.datastore.Query} from the specified kindName. If the kindName has
 * _private suffix, the key will be created under a namespace for the
 * specified {@link com.google.appengine.api.users.User}.
 *
 * @param kindName
 *          Name of kind
 * @param user
 *          {@link com.google.appengine.api.users.User} of the requestor
 * @return {@link com.google.appengine.api.datastore.Query}
 */
public Query createKindQueryWithNamespace(String kindName, User user) {

  // save the original namespace
  String origNamespace = NamespaceManager.get();

  // set namespace based on the kind suffix
  if (kindName.startsWith(KIND_PREFIX_PRIVATE)) {
    String userId = getUserId(user);
    NamespaceManager.set(userId);
  } else {
    NamespaceManager.set(NAMESPACE_DEFAULT);
  }

  // create a key
  Query q = new Query(kindName);

  // restore the original namespace
  NamespaceManager.set(origNamespace);
  return q;
}
 
Example #25
Source File: MetadataNamespacesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
void printAllNamespaces(DatastoreService ds, PrintWriter writer) {
  Query q = new Query(Entities.NAMESPACE_METADATA_KIND);

  for (Entity e : ds.prepare(q).asIterable()) {
    // A nonzero numeric id denotes the default namespace;
    // see <a href="#Namespace_Queries">Namespace Queries</a>, below
    if (e.getKey().getId() != 0) {
      writer.println("<default>");
    } else {
      writer.println(e.getKey().getName());
    }
  }
}
 
Example #26
Source File: QueryIssuingServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  String kind = req.getParameter("kind");
  Query query = new Query(kind);
  query.addFilter("p1", Query.FilterOperator.EQUAL, "a");
  query.addSort("p2", Query.SortDirection.DESCENDING);
  DatastoreServiceFactory.getDatastoreService().prepare(query).asSingleEntity();
}
 
Example #27
Source File: IndexQueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
private void checkQueryType(String property, Class<?> type) {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection(property, type));
    String sql = "SELECT " + property + " FROM " + kindName + " WHERE __ancestor__ is " + rootKey;
    assertEquals(sql.toLowerCase(), query.toString().toLowerCase());
    List<Entity> results = service.prepare(query).asList(fetchOption);
    for (Entity e : results) {
        assertEquals(1, e.getProperties().size());
        assertTrue(e.getProperties().containsKey(property));
    }
}
 
Example #28
Source File: DatastoreHelperTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected Object[] getResult(Query query, String pName) {
    int count = service.prepare(query).countEntities(FetchOptions.Builder.withDefaults());
    Object result[] = new Object[count];
    int pt = 0;
    for (Entity readRec : service.prepare(query).asIterable()) {
        result[pt++] = readRec.getProperty(pName);
    }
    return result;
}
 
Example #29
Source File: ListPeopleServletTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private String getFirstCursor() {
  Query q = new Query("Person").addSort("name", SortDirection.ASCENDING);
  PreparedQuery pq = datastore.prepare(q);
  FetchOptions fetchOptions = FetchOptions.Builder.withLimit(ListPeopleServlet.PAGE_SIZE);
  QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);
  return results.getCursor().toWebSafeString();
}
 
Example #30
Source File: StringDataTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected void doInFilter(String kind, String pName, String[] inDat) {
    Query query = new Query(kind, rootKey);
    query.setFilter(new FilterPredicate(pName, Query.FilterOperator.IN, Arrays.asList(inDat)));
    Object[] result = getResult(query, pName);
    assertEquals(inDat.length, result.length);
    assertArrayEquals(inDat, result);
}