use of com.google.appengine.api.datastore.Query.Filter in project Cached-Datastore by Emperorlou.
the class QueryHelper method getFilteredList.
public List<CachedEntity> getFilteredList(String kind, int limit, Cursor cursor, String fieldName, FilterOperator operator, Object equalToValue, String fieldName2, FilterOperator operator2, Object equalToValue2, String fieldName3, FilterOperator operator3, Object equalToValue3, String fieldName4, FilterOperator operator4, Object equalToValue4) {
FilterPredicate f1 = new FilterPredicate(fieldName, operator, equalToValue);
FilterPredicate f2 = new FilterPredicate(fieldName2, operator2, equalToValue2);
FilterPredicate f3 = new FilterPredicate(fieldName3, operator3, equalToValue3);
FilterPredicate f4 = new FilterPredicate(fieldName4, operator4, equalToValue4);
Filter filter = CompositeFilterOperator.and(f1, f2, f3, f4);
return ds.fetchAsList(kind, filter, limit, cursor);
}
use of com.google.appengine.api.datastore.Query.Filter in project java-docs-samples by GoogleCloudPlatform.
the class QueriesTest method queryRestrictions_compositeEqualFilter_returnsMatchedEntities.
@Test
public void queryRestrictions_compositeEqualFilter_returnsMatchedEntities() throws Exception {
// Arrange
Entity a = new Entity("Person", "a");
a.setProperty("birthYear", 1930);
a.setProperty("city", "Somewhere");
a.setProperty("lastName", "Someone");
Entity b = new Entity("Person", "b");
b.setProperty("birthYear", 1960);
b.setProperty("city", "Somewhere");
b.setProperty("lastName", "Someone");
Entity c = new Entity("Person", "c");
c.setProperty("birthYear", 1990);
c.setProperty("city", "Somewhere");
c.setProperty("lastName", "Someone");
Entity d = new Entity("Person", "d");
d.setProperty("birthYear", 1960);
d.setProperty("city", "Nowhere");
d.setProperty("lastName", "Someone");
Entity e = new Entity("Person", "e");
e.setProperty("birthYear", 1960);
e.setProperty("city", "Somewhere");
e.setProperty("lastName", "Noone");
datastore.put(ImmutableList.<Entity>of(a, b, c, d, e));
long minBirthYear = 1940;
long maxBirthYear = 1980;
String targetCity = "Somewhere";
String targetLastName = "Someone";
// [START inequality_filters_one_property_valid_example_2]
Filter lastNameFilter = new FilterPredicate("lastName", FilterOperator.EQUAL, targetLastName);
Filter cityFilter = new FilterPredicate("city", FilterOperator.EQUAL, targetCity);
Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);
Filter birthYearMaxFilter = new FilterPredicate("birthYear", FilterOperator.LESS_THAN_OR_EQUAL, maxBirthYear);
Filter validFilter = CompositeFilterOperator.and(lastNameFilter, cityFilter, birthYearMinFilter, birthYearMaxFilter);
Query q = new Query("Person").setFilter(validFilter);
// [END inequality_filters_one_property_valid_example_2]
// Assert
List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(b);
}
use of com.google.appengine.api.datastore.Query.Filter 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.
}
use of com.google.appengine.api.datastore.Query.Filter 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");
}
use of com.google.appengine.api.datastore.Query.Filter 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);
}
Aggregations