Java Code Examples for com.google.appengine.api.datastore.Query#addProjection()

The following examples show how to use com.google.appengine.api.datastore.Query#addProjection() . 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: ProjectionTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void projectionQuery_grouping_filtersDuplicates() {
  putTestData("some duplicate", 0L);
  putTestData("some duplicate", 0L);
  putTestData("too big", 1L);

  // [START grouping]
  Query q = new Query("TestKind");
  q.addProjection(new PropertyProjection("A", String.class));
  q.addProjection(new PropertyProjection("B", Long.class));
  q.setDistinct(true);
  q.setFilter(Query.FilterOperator.LESS_THAN.of("B", 1L));
  q.addSort("B", Query.SortDirection.DESCENDING);
  q.addSort("A");
  // [END grouping]

  List<Entity> entities = datastore.prepare(q).asList(FetchOptions.Builder.withLimit(5));
  assertThat(entities).hasSize(1);
  Entity entity = entities.get(0);
  assertWithMessage("entity.A")
      .that((String) entity.getProperty("A"))
      .isEqualTo("some duplicate");
  assertWithMessage("entity.B").that((long) entity.getProperty("B")).isEqualTo(0L);
}
 
Example 2
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 3
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 4
Source File: IndexQueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicQuery() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", String.class));
    query.addProjection(new PropertyProjection("intData", Integer.class));
    String sql = "SELECT stringData, intData FROM " + kindName + " WHERE __ancestor__ is " + rootKey;
    assertEquals(sql.toLowerCase(), query.toString().toLowerCase());
    List<Entity> results = service.prepare(query).asList(fetchOption);
    assertEquals(count, results.size());
    for (Entity e : results) {
        assertEquals(2, e.getProperties().size());
        assertTrue(e.getProperties().containsKey("stringData"));
        assertTrue(e.getProperties().containsKey("intData"));
    }
}
 
Example 5
Source File: IndexQueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testListQuery() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringList", String.class));
    query.addProjection(new PropertyProjection("intList", Integer.class));
    List<Entity> results = service.prepare(query).asList(fetchOption);
    // Distinct stringList data 2 * Distinct intList data 3 * entity's count 10
    assertEquals(60, results.size());
    Entity e = results.get(0);
    assertEquals(2, e.getProperties().size());
    assertTrue(e.getProperties().containsKey("stringList"));
    assertTrue(e.getProperties().containsKey("intList"));
}
 
Example 6
Source File: IndexQueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryOrder() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("intData", Integer.class));
    query.addSort("stringData", Query.SortDirection.DESCENDING);
    List<Entity> results = service.prepare(query).asList(fetchOption);
    assertEquals(count, results.size());
    int first = new Integer(results.get(0).getProperty("intData").toString());
    int last = new Integer(results.get(count - 1).getProperty("intData").toString());
    assertTrue(first > last);
}
 
Example 7
Source File: IndexQueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryFilter() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", String.class));
    query.setFilter(new FilterPredicate("intData", FilterOperator.NOT_EQUAL, 50));
    query.addSort("intData");
    List<Entity> results = service.prepare(query).asList(fetchOption);
    assertEquals(count - 1, results.size());
    for (Entity e : results) {
        assertTrue(e.getProperty("stringData").toString().contains("5") == false);
    }
}
 
Example 8
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 9
Source File: IndexQueryTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
private void checkQueryWithLimit(int limit) {
    FetchOptions fo = FetchOptions.Builder.withLimit(limit);
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", String.class));
    List<Entity> results = service.prepare(query).asList(fo);
    assertEquals(limit, results.size());
}
 
Example 10
Source File: DatastoreHelperTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected List<Entity> doQuery(String kind, String pName, Class<?> type, boolean indexed) {
    FetchOptions fo = FetchOptions.Builder.withDefaults();
    Query query = new Query(kind, rootKey);
    if (indexed) {
        query.addProjection(new PropertyProjection(pName, type));
        query.addSort(pName);
    }
    return service.prepare(query).asList(fo);
}
 
Example 11
Source File: DistinctTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testDistinctSort() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", String.class));
    query.addProjection(new PropertyProjection("floatData", Float.class));
    query.addSort("stringData", Query.SortDirection.DESCENDING);
    query.setDistinct(true);
    assertEquals(7, service.prepare(query).countEntities(fo));
    assertTrue(query.getDistinct());
    query.addSort("floatData", Query.SortDirection.DESCENDING);
    assertEquals(7, service.prepare(query).countEntities(fo));
}
 
Example 12
Source File: DistinctTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testDistinctFilter() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", String.class));
    query.addProjection(new PropertyProjection("floatData", Float.class));
    query.setFilter(new FilterPredicate("stringData", Query.FilterOperator.NOT_EQUAL, "string1"));
    query.addSort("stringData", Query.SortDirection.DESCENDING);
    query.setDistinct(true);
    assertEquals(5, service.prepare(query).countEntities(fo));
    assertTrue(query.getDistinct());
}
 
Example 13
Source File: DistinctTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testDistinctFilter2() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", String.class));
    query.addProjection(new PropertyProjection("floatData", Float.class));
    query.setFilter(new FilterPredicate("stringData", Query.FilterOperator.GREATER_THAN,
        "string0"));
    query.addSort("stringData", Query.SortDirection.DESCENDING);
    query.addSort("floatData", Query.SortDirection.DESCENDING);
    query.setDistinct(true);
    assertEquals(2, service.prepare(query).countEntities(fo));
    assertTrue(query.getDistinct());
}
 
Example 14
Source File: ProjectionServlet.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
private void addGuestbookProjections(Query query) {
  query.addProjection(new PropertyProjection("content", String.class));
  query.addProjection(new PropertyProjection("date", Date.class));
}
 
Example 15
Source File: IndexQueryTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testWrongType() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", Integer.class));
    service.prepare(query).asIterator(fetchOption).next();
}
 
Example 16
Source File: IndexQueryTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testCount() {
    Query query = new Query(kindName, rootKey);
    query.addProjection(new PropertyProjection("stringData", String.class));
    assertEquals(count, service.prepare(query).countEntities(fetchOption));
}