@Override public Query<T> ancestor(Object keyOrEntity) { this.actual.setAncestor(this.factory.getRawKey(keyOrEntity)); return this; }
private void addParentFilter(Query.FilterOperator op, Key key, Query datastoreQuery) { // We only support queries on parent if it is an equality filter. if (op != Query.FilterOperator.EQUAL) { throw new UnsupportedDatastoreFeatureException("Operator is of type " + op + " but the " + "datastore only supports parent queries using the equality operator."); } if (key == null) { throw new UnsupportedDatastoreFeatureException( "Received a null parent parameter. The datastore does not support querying for null parents."); } datastoreQuery.setAncestor(key); }
private void prepareQueryAncestor(QueryBuilder<?> builder, Query q) { IdRef<?> parentId = builder.getParentId(); if (parentId == null) { return; } q.setAncestor(IdRefToKey.toKey(r, parentId)); }
@Test(expected = DatastoreNeedIndexException.class) public void testQueryWithAncestorAndSortOrderRequiresConfiguredIndex() throws Exception { executeQuery( new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .addSort("someProperty")); }
@Test(expected = DatastoreNeedIndexException.class) public void testQueryWithAncestorAndInequalityFiltersRequiresConfiguredIndex() throws Exception { executeQuery( new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter(new Query.FilterPredicate("someProperty", GREATER_THAN, "a"))); }
@Test public void testQueryUsingOnlyAncestorAndEqualityFiltersDoesNotRequireConfiguredIndex() throws Exception { executeQuery(new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter(new Query.FilterPredicate("someProperty", EQUAL, "foo"))); executeQuery(new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter( and(new Query.FilterPredicate("someProperty", EQUAL, "foo"), new Query.FilterPredicate("otherProperty", EQUAL, "bar")))); }
@Test public void testKindlessQueryUsingOnlyAncestorAndKeyFiltersDoesNotRequireConfiguredIndex() throws Exception { executeQuery(new Query() .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, GREATER_THAN, KeyFactory.createKey("Kind", 1)))); }
@Test public void testFilteringByKind() throws Exception { Key parentKey = createQueryBasicsTestParent("testFilteringByKind"); Entity foo = createEntity("foo", parentKey).store(); Entity bar = createEntity("bar", parentKey).store(); PreparedQuery preparedQuery = service.prepare(new Query("foo").setAncestor(parentKey)); List<Entity> results = preparedQuery.asList(withDefaults()); assertEquals(1, results.size()); assertEquals(foo, results.get(0)); }
@Test public void queriesDontReturnDeletedEntities() throws Exception { String methodName = "queriesDontReturnDeletedEntities"; Entity entity = createTestEntityWithUniqueMethodNameKey(SMOKE_TEST_ENTITY, methodName); Key key = entity.getKey(); service.put(entity); service.delete(key); List<Entity> entities = service.prepare(new Query(SMOKE_TEST_ENTITY).setAncestor(key)) .asList(FetchOptions.Builder.withDefaults()); assertEquals(0, entities.size()); }
@Test public void testQueryWithAncestorAndInequalityFilterOnKeyPropertyDoesNotRequireConfiguredIndex() throws Exception { executeQuery( new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, GREATER_THAN, KeyFactory.createKey("Unindexed", 1)))); }
@Test public void testIntegerPropertySortingIsNotLexicographic() throws Exception { String methodName = "testIntegerPropertySortingIsNotLexicographic"; Entity parent = createTestEntityWithUniqueMethodNameKey(QUERY_SORTING_ENTITY, methodName); Key key = parent.getKey(); Entity ten = storeTestEntityWithSingleProperty(key, 10); Entity five = storeTestEntityWithSingleProperty(key, 5); Query query = createQuery().setAncestor(key).addSort(SINGLE_PROPERTY_NAME, ASCENDING); List<Entity> results = service.prepare(query).asList(withDefaults()); assertTrue(results.indexOf(five) < results.indexOf(ten)); // if sorting were lexicographic, "10" would come before "5" service.delete(ten.getKey(), five.getKey()); }
@Test public void testFilterOnMultiValuedProperty() throws Exception { Key parentKey = createQueryBasicsTestParent("testFilterOnMultiValuedProperty"); createEntity("Entry", parentKey) .withProperty("letters", Arrays.asList("a", "b", "c")) .store(); Query query = new Query("Entry") .setAncestor(parentKey) .setFilter(new Query.FilterPredicate("letters", EQUAL, "a")); assertNotNull(service.prepare(query).asSingleEntity()); }
@Test(expected = DatastoreNeedIndexException.class) public void testAncestorQueryWithInequalityFilterOnSomePropertyAndEqualityFilterOnSamePropertyRequiresConfiguredIndex() throws Exception { executeQuery( new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter( and(new Query.FilterPredicate("someProperty", EQUAL, "b"), new Query.FilterPredicate("someProperty", GREATER_THAN, "a")))); }
@Test public void testQueryUsingOnlyAncestorFiltersAndEqualityFiltersOnPropertiesAndInequalityFiltersOnKeysDoesNotRequireConfiguredIndex() throws Exception { executeQuery(new Query("Unindexed") .setAncestor(KeyFactory.createKey("Ancestor", 1)) .setFilter( and(new Query.FilterPredicate("someProperty", EQUAL, "foo"), new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, GREATER_THAN, KeyFactory.createKey("Unindexed", 1))))); }
@Test public void testProjectionQueryReturnsEntitiesContainingProjectedPropertyEvenIfPropertyValueIsNull() throws Exception { String methodName = "testProjectionQueryOnlyReturnsEntitiesContainingProjectedPropertyEvenIfPropertyValueIsNull"; Entity parent = createTestEntityWithUniqueMethodNameKey("Kind", methodName); Key key = parent.getKey(); Entity e1 = createEntity("Kind", key) .withProperty("foo", null) .store(); Query query = new Query("Kind") .setAncestor(key) .addProjection(new PropertyProjection("foo", String.class)); List<Entity> results = service.prepare(query).asList(withDefaults()); assertEquals(Collections.singletonList(e1), results); }
@Test public void queryingByKindOnlyReturnsEntitiesOfRequestedKind() throws Exception { Key parentKey = createQueryBasicsTestParent("queryingByKindOnlyReturnsEntitiesOfRequestedKind"); Entity person = new Entity(KeyFactory.createKey(parentKey, "Person", 1)); service.put(person); Entity address = new Entity(KeyFactory.createKey(parentKey, "Address", 1)); service.put(address); assertSingleResult(person, new Query("Person").setAncestor(parentKey)); }
@Test public void testNullPropertyValue() throws Exception { Key parentKey = createQueryBasicsTestParent("testNullPropertyValue"); createEntity("Entry", parentKey) .withProperty("user", null) .store(); Entity entity = service.prepare(new Query("Entry") .setAncestor(parentKey)).asSingleEntity(); assertNull(entity.getProperty("user")); }
@Test public void testFilterEqualNull() throws Exception { Key parentKey = createQueryBasicsTestParent("testFilterEqualNull"); createEntity("Entry", parentKey) .withProperty("user", null) .store(); Query query = new Query("Entry") .setAncestor(parentKey) .setFilter(new Query.FilterPredicate("user", EQUAL, null)); assertNotNull(service.prepare(query).asSingleEntity()); }
@Test public void testFilterNotEqualNull() throws Exception { Key parentKey = createQueryBasicsTestParent("testFilterNotEqualNull"); createEntity("Entry", parentKey) .withProperty("user", "joe") .store(); Query query = new Query("Entry") .setAncestor(parentKey) .setFilter(new Query.FilterPredicate("user", NOT_EQUAL, null)); assertNotNull(service.prepare(query).asSingleEntity()); }
@Before public void setUp() { super.setUp(); johnsParent = createTestEntityWithUniqueMethodNameKey("Person", "PreparedQueryTest"); Key key = johnsParent.getKey(); john = createEntity("Person", key) .withProperty("name", "John") .store(); Query query = new Query("Person") .setAncestor(key) .setFilter(new Query.FilterPredicate("name", EQUAL, "John")); preparedQuery = service.prepare(query); }