Search in sources :

Example 51 with Query

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

the class QueriesTest method queryRestrictions_sortWrongOrderOnInequality_isInvalid.

@Test
public void queryRestrictions_sortWrongOrderOnInequality_isInvalid() throws Exception {
    long minBirthYear = 1940;
    // [START inequality_filters_sort_orders_invalid_example_2]
    Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);
    // Not valid. Sort on birthYear needs to be first.
    Query q = new Query("Person").setFilter(birthYearMinFilter).addSort("lastName", SortDirection.ASCENDING).addSort("birthYear", SortDirection.ASCENDING);
// [END inequality_filters_sort_orders_invalid_example_2]
// 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.
}
Also used : 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 52 with Query

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

the class QueriesTest method queryRestrictions_surprisingMultipleValuesNotEquals_returnsMatchedEntities.

@Test
public void queryRestrictions_surprisingMultipleValuesNotEquals_returnsMatchedEntities() throws Exception {
    Entity a = new Entity("Widget", "a");
    a.setProperty("x", ImmutableList.<Long>of(1L, 2L));
    Entity b = new Entity("Widget", "b");
    b.setProperty("x", ImmutableList.<Long>of(1L, 3L));
    Entity c = new Entity("Widget", "c");
    c.setProperty("x", ImmutableList.<Long>of(-6L, 2L));
    Entity d = new Entity("Widget", "d");
    d.setProperty("x", ImmutableList.<Long>of(-6L, 4L));
    Entity e = new Entity("Widget", "e");
    e.setProperty("x", ImmutableList.<Long>of(1L));
    datastore.put(ImmutableList.<Entity>of(a, b, c, d, e));
    // [START surprising_behavior_example_3]
    Query q = new Query("Widget").setFilter(new FilterPredicate("x", FilterOperator.NOT_EQUAL, 1));
    // [END surprising_behavior_example_3]
    // The query matches any entity that has a some value other than 1. Only
    // entity "e" is not matched.  See the documentation for more details:
    // https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(a, b, c, d);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Example 53 with Query

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

the class QueriesTest method queryInterface_multipleFilters_printsMatchedEntities.

@Test
public void queryInterface_multipleFilters_printsMatchedEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Person", "a");
    a.setProperty("firstName", "Alph");
    a.setProperty("lastName", "Alpha");
    a.setProperty("height", 60);
    Entity b = new Entity("Person", "b");
    b.setProperty("firstName", "Bee");
    b.setProperty("lastName", "Bravo");
    b.setProperty("height", 70);
    Entity c = new Entity("Person", "c");
    c.setProperty("firstName", "Charles");
    c.setProperty("lastName", "Charlie");
    c.setProperty("height", 100);
    datastore.put(ImmutableList.<Entity>of(a, b, c));
    StringWriter buf = new StringWriter();
    PrintWriter out = new PrintWriter(buf);
    long minHeight = 60;
    long maxHeight = 72;
    // Act
    // [START interface_1]
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Filter heightMinFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);
    Filter heightMaxFilter = new FilterPredicate("height", FilterOperator.LESS_THAN_OR_EQUAL, maxHeight);
    // Use CompositeFilter to combine multiple filters
    CompositeFilter heightRangeFilter = CompositeFilterOperator.and(heightMinFilter, heightMaxFilter);
    // Use class Query to assemble a query
    Query q = new Query("Person").setFilter(heightRangeFilter);
    // Use PreparedQuery interface to retrieve results
    PreparedQuery pq = datastore.prepare(q);
    for (Entity result : pq.asIterable()) {
        String firstName = (String) result.getProperty("firstName");
        String lastName = (String) result.getProperty("lastName");
        Long height = (Long) result.getProperty("height");
        out.println(firstName + " " + lastName + ", " + height + " inches tall");
    }
    // [END interface_1]
    // Assert
    assertThat(buf.toString()).contains("Alph Alpha, 60 inches tall");
    assertThat(buf.toString()).contains("Bee Bravo, 70 inches tall");
    assertThat(buf.toString()).doesNotContain("Charlie");
}
Also used : Entity(com.google.appengine.api.datastore.Entity) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) StringWriter(java.io.StringWriter) 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) DatastoreService(com.google.appengine.api.datastore.DatastoreService) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 54 with Query

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

the class QueriesTest method keyFilterExample_kindless_returnsMatchingEntities.

@Test
public void keyFilterExample_kindless_returnsMatchingEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Child", "a");
    Entity b = new Entity("Child", "b");
    Entity c = new Entity("Child", "c");
    Entity aa = new Entity("Child", "aa", b.getKey());
    Entity bb = new Entity("Child", "bb", b.getKey());
    Entity aaa = new Entity("Child", "aaa", bb.getKey());
    Entity bbb = new Entity("Child", "bbb", bb.getKey());
    Entity adult = new Entity("Adult", "a");
    Entity zooAnimal = new Entity("ZooAnimal", "a");
    datastore.put(ImmutableList.<Entity>of(a, b, c, aa, bb, aaa, bbb, adult, zooAnimal));
    // Act
    Key lastSeenKey = bb.getKey();
    // [START kindless_query_example]
    Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
    Query q = new Query().setFilter(keyFilter);
    // [END kindless_query_example]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(// Ancestor path "b/bb/aaa" is greater than "b/bb".
    aaa, // Ancestor path "b/bb/bbb" is greater than "b/bb".
    bbb, // Kind "ZooAnimal" is greater than "Child"
    zooAnimal, // Key name identifier "c" is greater than b.
    c);
}
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) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 55 with Query

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

the class QueriesTest method ancestorQueryExample_returnsMatchingEntities.

@Test
public void ancestorQueryExample_returnsMatchingEntities() throws Exception {
    // [START ancestor_query_example]
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity tom = new Entity("Person", "Tom");
    Key tomKey = tom.getKey();
    datastore.put(tom);
    Entity weddingPhoto = new Entity("Photo", tomKey);
    weddingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/wedding_photo.jpg");
    Entity babyPhoto = new Entity("Photo", tomKey);
    babyPhoto.setProperty("imageURL", "http://domain.com/some/path/to/baby_photo.jpg");
    Entity dancePhoto = new Entity("Photo", tomKey);
    dancePhoto.setProperty("imageURL", "http://domain.com/some/path/to/dance_photo.jpg");
    Entity campingPhoto = new Entity("Photo");
    campingPhoto.setProperty("imageURL", "http://domain.com/some/path/to/camping_photo.jpg");
    List<Entity> photoList = Arrays.asList(weddingPhoto, babyPhoto, dancePhoto, campingPhoto);
    datastore.put(photoList);
    Query photoQuery = new Query("Photo").setAncestor(tomKey);
    // This returns weddingPhoto, babyPhoto, and dancePhoto,
    // but not campingPhoto, because tom is not an ancestor
    List<Entity> results = datastore.prepare(photoQuery).asList(FetchOptions.Builder.withDefaults());
    // [END ancestor_query_example]
    assertThat(results).named("query results").containsExactly(weddingPhoto, babyPhoto, dancePhoto);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Aggregations

Query (com.google.appengine.api.datastore.Query)74 Entity (com.google.appengine.api.datastore.Entity)59 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)47 Test (org.junit.Test)38 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)31 Filter (com.google.appengine.api.datastore.Query.Filter)20 Key (com.google.appengine.api.datastore.Key)16 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)16 DatastoreService (com.google.appengine.api.datastore.DatastoreService)13 ArrayList (java.util.ArrayList)13 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)6 FetchOptions (com.google.appengine.api.datastore.FetchOptions)6 PrintWriter (java.io.PrintWriter)6 Instant (java.time.Instant)6 UUID (java.util.UUID)6 Conversation (codeu.model.data.Conversation)2 Message (codeu.model.data.Message)2 User (codeu.model.data.User)2 Book (com.example.getstarted.objects.Book)2 Result (com.example.getstarted.objects.Result)2