Search in sources :

Example 11 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryRestrictions_compositeFilter_returnsMatchedEntities.

@Test
public void queryRestrictions_compositeFilter_returnsMatchedEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Person", "a");
    a.setProperty("birthYear", 1930);
    Entity b = new Entity("Person", "b");
    b.setProperty("birthYear", 1960);
    Entity c = new Entity("Person", "c");
    c.setProperty("birthYear", 1990);
    datastore.put(ImmutableList.<Entity>of(a, b, c));
    // Act
    long minBirthYear = 1940;
    long maxBirthYear = 1980;
    // [START inequality_filters_one_property_valid_example_1]
    Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);
    Filter birthYearMaxFilter = new FilterPredicate("birthYear", FilterOperator.LESS_THAN_OR_EQUAL, maxBirthYear);
    Filter birthYearRangeFilter = CompositeFilterOperator.and(birthYearMinFilter, birthYearMaxFilter);
    Query q = new Query("Person").setFilter(birthYearRangeFilter);
    // [END inequality_filters_one_property_valid_example_1]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(b);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Example 12 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryRestrictions_inequalitySortedFirst_returnsMatchedEntities.

@Test
public void queryRestrictions_inequalitySortedFirst_returnsMatchedEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Person", "a");
    a.setProperty("birthYear", 1930);
    a.setProperty("lastName", "Someone");
    Entity b = new Entity("Person", "b");
    b.setProperty("birthYear", 1990);
    b.setProperty("lastName", "Bravo");
    Entity c = new Entity("Person", "c");
    c.setProperty("birthYear", 1960);
    c.setProperty("lastName", "Charlie");
    Entity d = new Entity("Person", "d");
    d.setProperty("birthYear", 1960);
    d.setProperty("lastName", "Delta");
    datastore.put(ImmutableList.<Entity>of(a, b, c, d));
    long minBirthYear = 1940;
    // [START inequality_filters_sort_orders_valid_example]
    Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);
    Query q = new Query("Person").setFilter(birthYearMinFilter).addSort("birthYear", SortDirection.ASCENDING).addSort("lastName", SortDirection.ASCENDING);
    // [END inequality_filters_sort_orders_valid_example]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(c, d, b).inOrder();
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Example 13 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class StartupServletTest method doGet_emptyDatastore_writesPresidents.

@Test
public void doGet_emptyDatastore_writesPresidents() throws Exception {
    servletUnderTest.doGet(mockRequest, mockResponse);
    Filter nameFilter = new FilterPredicate("name", FilterOperator.EQUAL, "George Washington");
    Query q = new Query("Person").setFilter(nameFilter);
    Entity result = datastore.prepare(q).asSingleEntity();
    assertThat(result.getProperty("name")).named("name").isEqualTo("George Washington");
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) Filter(com.google.appengine.api.datastore.Query.Filter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Example 14 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class IndexesTest method propertyFilterExample_returnsMatchingEntities.

@Test
public void propertyFilterExample_returnsMatchingEntities() throws Exception {
    // [START unindexed_properties_1]
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key acmeKey = KeyFactory.createKey("Company", "Acme");
    Entity tom = new Entity("Person", "Tom", acmeKey);
    tom.setProperty("name", "Tom");
    tom.setProperty("age", 32);
    datastore.put(tom);
    Entity lucy = new Entity("Person", "Lucy", acmeKey);
    lucy.setProperty("name", "Lucy");
    lucy.setUnindexedProperty("age", 29);
    datastore.put(lucy);
    Filter ageFilter = new FilterPredicate("age", FilterOperator.GREATER_THAN, 25);
    Query q = new Query("Person").setAncestor(acmeKey).setFilter(ageFilter);
    // Returns tom but not lucy, because her age is unindexed
    List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
    // [END unindexed_properties_1]
    assertThat(results).named("query results").containsExactly(tom);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) Filter(com.google.appengine.api.datastore.Query.Filter) DatastoreService(com.google.appengine.api.datastore.DatastoreService) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 15 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class MetadataNamespacesTest method getNamespaces.

// [START namespace_query_example]
List<String> getNamespaces(DatastoreService ds, String start, String end) {
    // Start with unrestricted namespace query
    Query q = new Query(Entities.NAMESPACE_METADATA_KIND);
    List<Filter> subFilters = new ArrayList();
    // Limit to specified range, if any
    if (start != null) {
        subFilters.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN_OR_EQUAL, Entities.createNamespaceKey(start)));
    }
    if (end != null) {
        subFilters.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN_OR_EQUAL, Entities.createNamespaceKey(end)));
    }
    q.setFilter(CompositeFilterOperator.and(subFilters));
    // Initialize result list
    List<String> results = new ArrayList<String>();
    // Build list of query results
    for (Entity e : ds.prepare(q).asIterable()) {
        results.add(Entities.getNamespaceFromNamespaceKey(e.getKey()));
    }
    // Return result list
    return results;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) Filter(com.google.appengine.api.datastore.Query.Filter) ArrayList(java.util.ArrayList) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate)

Aggregations

FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)38 Query (com.google.appengine.api.datastore.Query)31 Entity (com.google.appengine.api.datastore.Entity)26 Filter (com.google.appengine.api.datastore.Query.Filter)24 Test (org.junit.Test)21 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)20 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)20 Key (com.google.appengine.api.datastore.Key)6 DatastoreService (com.google.appengine.api.datastore.DatastoreService)3 PrintWriter (java.io.PrintWriter)3 ArrayList (java.util.ArrayList)3 StringWriter (java.io.StringWriter)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)1 Transaction (com.google.appengine.api.datastore.Transaction)1 Collection (java.util.Collection)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1