Search in sources :

Example 16 with FilterPredicate

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

the class MetadataKindsTest method printLowercaseKinds.

// [START kind_query_example]
void printLowercaseKinds(DatastoreService ds, PrintWriter writer) {
    // Start with unrestricted kind query
    Query q = new Query(Entities.KIND_METADATA_KIND);
    List<Filter> subFils = new ArrayList();
    // Limit to lowercase initial letters
    subFils.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN_OR_EQUAL, Entities.createKindKey("a")));
    // Character after 'z'
    String endChar = Character.toString((char) ('z' + 1));
    subFils.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN, Entities.createKindKey(endChar)));
    q.setFilter(CompositeFilterOperator.and(subFils));
    // Print heading
    writer.println("Lowercase kinds:");
    // Print query results
    for (Entity e : ds.prepare(q).asIterable()) {
        writer.println("  " + e.getKey().getName());
    }
}
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)

Example 17 with FilterPredicate

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

the class IndexesServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    // [START exploding_index_example_1]
    Query q = new Query("Widget").setFilter(CompositeFilterOperator.and(new FilterPredicate("x", FilterOperator.EQUAL, 1), new FilterPredicate("y", FilterOperator.EQUAL, 2))).addSort("date", Query.SortDirection.ASCENDING);
    // [END exploding_index_example_1]
    List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
    PrintWriter out = resp.getWriter();
    out.printf("Got %d widgets.\n", results.size());
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) PrintWriter(java.io.PrintWriter)

Example 18 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project iosched by google.

the class ServingUrlManagerTest method testGetServingUrl_withSourceUrl.

@Test
public void testGetServingUrl_withSourceUrl() throws Exception {
    PreparedQuery mockPreparedQuery = mock(PreparedQuery.class);
    when(mockDatastoreService.prepare(any(Query.class))).thenReturn(mockPreparedQuery);
    when(mockPreparedQuery.asList(any(FetchOptions.class))).thenAnswer(new Answer<List<Entity>>() {

        @Override
        public List<Entity> answer(InvocationOnMock invocation) throws Throwable {
            Key key = mock(Key.class);
            when(key.getKind()).thenReturn(ServingUrlManager.ENTITY_KIND);
            when(key.getName()).thenReturn(GCS_FULLPATH);
            Entity entity = new Entity(key);
            entity.setProperty(ServingUrlManager.SERVING_URL_PROPERTY, SERVING_URL);
            return new ArrayList<>(Arrays.asList(entity));
        }
    });
    assertEquals(SERVING_URL, ServingUrlManager.INSTANCE.getServingUrl(SOURCE_URL));
    ArgumentCaptor<Query> queryCaptor = ArgumentCaptor.forClass(Query.class);
    verify(mockDatastoreService).prepare(queryCaptor.capture());
    Query query = queryCaptor.getValue();
    assertEquals(ServingUrlManager.ENTITY_KIND, query.getKind());
    assertEquals(new FilterPredicate(ServingUrlManager.SOURCE_URL_PROPERTY, FilterOperator.EQUAL, SOURCE_URL), query.getFilter());
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) List(java.util.List) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project Cached-Datastore by Emperorlou.

the class QueryHelper method getFilteredList_Keys.

public List<Key> getFilteredList_Keys(String kind, int limit, String fieldName, FilterOperator operator, Object equalToValue, String fieldName2, FilterOperator operator2, Object equalToValue2) {
    FilterPredicate f1 = new FilterPredicate(fieldName, operator, equalToValue);
    FilterPredicate f2 = new FilterPredicate(fieldName2, operator2, equalToValue2);
    Filter filter = CompositeFilterOperator.and(f1, f2);
    return ds.fetchAsList_Keys(kind, filter, 1000);
}
Also used : CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) Filter(com.google.appengine.api.datastore.Query.Filter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate)

Example 20 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project Cached-Datastore by Emperorlou.

the class QueryHelper method getFilteredORList.

public List<CachedEntity> getFilteredORList(Cursor cursor, String kind, String fieldName, Object equalToValue, String fieldName2, Object equalToValue2, String fieldName3, Object equalToValue3) {
    FilterPredicate f1 = new FilterPredicate(fieldName, FilterOperator.EQUAL, equalToValue);
    FilterPredicate f2 = new FilterPredicate(fieldName2, FilterOperator.EQUAL, equalToValue2);
    FilterPredicate f3 = new FilterPredicate(fieldName3, FilterOperator.EQUAL, equalToValue3);
    Filter filter = CompositeFilterOperator.or(f1, f2, f3);
    Query q = new Query(kind);
    q.setFilter(filter);
    return ds.fetchAsList(q, 1000, cursor);
}
Also used : Query(com.google.appengine.api.datastore.Query) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) Filter(com.google.appengine.api.datastore.Query.Filter) 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