Search in sources :

Example 11 with Query

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

the class QueriesTest method queryInterface_singleFilter_returnsMatchedEntities.

@Test
public void queryInterface_singleFilter_returnsMatchedEntities() 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", 300);
    datastore.put(ImmutableList.<Entity>of(a, b, c));
    // Act
    long minHeight = 150;
    // [START interface_2]
    Filter heightMinFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);
    Query q = new Query("Person").setFilter(heightMinFilter);
    // [END interface_2]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(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) Test(org.junit.Test)

Example 12 with Query

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

the class QueriesTest method keyFilterExample_returnsMatchingEntities.

@Test
public void keyFilterExample_returnsMatchingEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Person", "a");
    Entity b = new Entity("Person", "b");
    Entity c = new Entity("Person", "c");
    Entity aa = new Entity("Person", "aa", b.getKey());
    Entity bb = new Entity("Person", "bb", b.getKey());
    Entity aaa = new Entity("Person", "aaa", bb.getKey());
    Entity bbb = new Entity("Person", "bbb", bb.getKey());
    datastore.put(ImmutableList.<Entity>of(a, b, c, aa, bb, aaa, bbb));
    // Act
    Key lastSeenKey = bb.getKey();
    // [START key_filter_example]
    Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
    Query q = new Query("Person").setFilter(keyFilter);
    // [END key_filter_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, // 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 13 with Query

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

the class QueriesTest method queryInterface_orFilter_printsMatchedEntities.

@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 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 interface_3]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(a, c);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) 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) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 14 with Query

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

the class QueriesTest method ancestorFilterExample_returnsMatchingEntities.

@Test
public void ancestorFilterExample_returnsMatchingEntities() throws Exception {
    Entity a = new Entity("Person", "a");
    Entity b = new Entity("Person", "b");
    Entity aa = new Entity("Person", "aa", a.getKey());
    Entity ab = new Entity("Person", "ab", a.getKey());
    Entity bb = new Entity("Person", "bb", b.getKey());
    datastore.put(ImmutableList.<Entity>of(a, b, aa, ab, bb));
    Key ancestorKey = a.getKey();
    // [START ancestor_filter_example]
    Query q = new Query("Person").setAncestor(ancestorKey);
    // [END ancestor_filter_example]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(a, aa, ab);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 15 with Query

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

the class QueriesTest method queryRestrictions_missingSortOnInequality_isInvalid.

@Test
public void queryRestrictions_missingSortOnInequality_isInvalid() throws Exception {
    long minBirthYear = 1940;
    // [START inequality_filters_sort_orders_invalid_example_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 inequality_filters_sort_orders_invalid_example_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.
}
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)

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