Search in sources :

Example 1 with QueryMetadata

use of org.apache.cayenne.query.QueryMetadata in project cayenne by apache.

the class ClientChannel method onQuery.

@SuppressWarnings("unchecked")
public QueryResponse onQuery(ObjectContext context, Query query) {
    QueryResponse response = send(new QueryMessage(query), QueryResponse.class);
    if (context != null) {
        EntityResolver resolver = context.getEntityResolver();
        QueryMetadata info = query.getMetaData(resolver);
        if (!info.isFetchingDataRows()) {
            response.reset();
            while (response.next()) {
                if (response.isList()) {
                    List objects = response.currentList();
                    if (!objects.isEmpty()) {
                        DeepMergeOperation merger = new DeepMergeOperation(context);
                        List<Object> rsMapping = info.getResultSetMapping();
                        if (rsMapping == null) {
                            convertSingleObjects(objects, merger);
                        } else {
                            if (rsMapping.size() == 1) {
                                if (rsMapping.get(0) instanceof EntityResultSegment) {
                                    convertSingleObjects(objects, merger);
                                }
                            } else {
                                processMixedResult(objects, merger, rsMapping);
                            }
                        }
                    }
                }
            }
        }
    }
    return response;
}
Also used : QueryMetadata(org.apache.cayenne.query.QueryMetadata) DeepMergeOperation(org.apache.cayenne.util.DeepMergeOperation) QueryResponse(org.apache.cayenne.QueryResponse) List(java.util.List) EntityResolver(org.apache.cayenne.map.EntityResolver) EntityResultSegment(org.apache.cayenne.query.EntityResultSegment)

Example 2 with QueryMetadata

use of org.apache.cayenne.query.QueryMetadata in project cayenne by apache.

the class PrefetchProcessorTreeBuilderIT method testBuildTreeWithPrefetches.

@Test
public void testBuildTreeWithPrefetches() {
    final ClassDescriptor descriptor = resolver.getClassDescriptor("Artist");
    ObjEntity e2 = resolver.getObjEntity("Painting");
    ObjEntity e3 = resolver.getObjEntity("Gallery");
    ObjEntity e4 = resolver.getObjEntity("Exhibit");
    ObjEntity e5 = resolver.getObjEntity("ArtistExhibit");
    List<Object> mainRows = new ArrayList<Object>();
    Map<Object, Object> extraRows = new HashMap<Object, Object>();
    PrefetchTreeNode tree = new PrefetchTreeNode();
    tree.addPath(Artist.PAINTING_ARRAY.getName()).setPhantom(false);
    tree.addPath(Artist.PAINTING_ARRAY.dot(Painting.TO_GALLERY).dot(Gallery.EXHIBIT_ARRAY).getName()).setPhantom(false);
    tree.addPath(Artist.ARTIST_EXHIBIT_ARRAY.getName()).setPhantom(false);
    QueryMetadata metadata = new MockQueryMetadata() {

        @Override
        public ClassDescriptor getClassDescriptor() {
            return descriptor;
        }

        @Override
        public ObjEntity getObjEntity() {
            return descriptor.getEntity();
        }

        @Override
        public DbEntity getDbEntity() {
            return getObjEntity().getDbEntity();
        }

        @Override
        public DataMap getDataMap() {
            return getObjEntity().getDataMap();
        }

        @Override
        public boolean isRefreshingObjects() {
            return true;
        }

        @Override
        public boolean isResolvingInherited() {
            return true;
        }
    };
    HierarchicalObjectResolver resolver = new HierarchicalObjectResolver(context, metadata);
    PrefetchProcessorTreeBuilder builder = new PrefetchProcessorTreeBuilder(resolver, mainRows, extraRows);
    PrefetchProcessorNode n1 = builder.buildTree(tree);
    assertSame(mainRows, n1.getDataRows());
    assertSame(descriptor.getEntity(), n1.getResolver().getEntity());
    PrefetchProcessorNode n2 = (PrefetchProcessorNode) n1.getNode("paintingArray");
    assertNotNull(n2);
    assertSame(e2, n2.getResolver().getEntity());
    assertFalse(n2.isPhantom());
    assertTrue(n2.isPartitionedByParent());
    PrefetchProcessorNode n3 = (PrefetchProcessorNode) n1.getNode("paintingArray.toGallery");
    assertNotNull(n3);
    assertSame(e3, n3.getResolver().getEntity());
    assertTrue(n3.isPhantom());
    assertTrue(n3.isPartitionedByParent());
    PrefetchProcessorNode n4 = (PrefetchProcessorNode) n1.getNode("paintingArray.toGallery.exhibitArray");
    assertNotNull(n4);
    assertSame(e4, n4.getResolver().getEntity());
    assertFalse(n4.isPhantom());
    assertTrue(n4.isPartitionedByParent());
    PrefetchProcessorNode n5 = (PrefetchProcessorNode) n1.getNode("artistExhibitArray");
    assertNotNull(n5);
    assertSame(e5, n5.getResolver().getEntity());
    assertFalse(n5.isPhantom());
    assertTrue(n5.isPartitionedByParent());
}
Also used : MockQueryMetadata(org.apache.cayenne.query.MockQueryMetadata) QueryMetadata(org.apache.cayenne.query.QueryMetadata) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ObjEntity(org.apache.cayenne.map.ObjEntity) MockQueryMetadata(org.apache.cayenne.query.MockQueryMetadata) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) Test(org.junit.Test)

Example 3 with QueryMetadata

use of org.apache.cayenne.query.QueryMetadata in project cayenne by apache.

the class DataContextQueryCachingIT method testSharedCacheDataRowsRefresh.

@Test
public void testSharedCacheDataRowsRefresh() throws Exception {
    SelectQuery<DataRow> select = SelectQuery.dataRowQuery(Artist.class);
    select.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
    MockDataNode engine = MockDataNode.interceptNode(domain, getNode());
    try {
        // first run, no cache yet
        List<?> rows1 = mockupDataRows(2);
        engine.reset();
        engine.addExpectedResult(select, rows1);
        List<?> resultRows = context.performQuery(select);
        assertEquals(1, engine.getRunCount());
        assertEquals(rows1, resultRows);
        QueryMetadata cacheKey = select.getMetaData(context.getEntityResolver());
        assertEquals(rows1, context.getParentDataDomain().getQueryCache().get(cacheKey));
        assertNull(context.getQueryCache().get(cacheKey));
        // second run, must refresh the cache
        List<?> rows2 = mockupDataRows(5);
        engine.reset();
        engine.addExpectedResult(select, rows2);
        select.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE_REFRESH);
        List<?> freshResultRows = context.performQuery(select);
        assertEquals(1, engine.getRunCount());
        assertEquals(rows2, freshResultRows);
        assertEquals(rows2, context.getParentDataDomain().getQueryCache().get(cacheKey));
        assertNull(context.getQueryCache().get(cacheKey));
    } finally {
        engine.stopInterceptNode();
    }
}
Also used : QueryMetadata(org.apache.cayenne.query.QueryMetadata) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 4 with QueryMetadata

use of org.apache.cayenne.query.QueryMetadata in project cayenne by apache.

the class DataContextQueryCachingIT method testLocalCacheDataObjectsRefresh.

@Test
public void testLocalCacheDataObjectsRefresh() throws Exception {
    SelectQuery<Artist> select = new SelectQuery<>(Artist.class);
    select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
    MockDataNode engine = MockDataNode.interceptNode(domain, getNode());
    try {
        // first run, no cache yet
        List<?> rows1 = mockupDataRows(2);
        engine.reset();
        engine.addExpectedResult(select, rows1);
        List<?> resultRows = context.performQuery(select);
        assertEquals(1, engine.getRunCount());
        assertEquals(2, resultRows.size());
        assertTrue(resultRows.get(0) instanceof DataObject);
        QueryMetadata cacheKey = select.getMetaData(context.getEntityResolver());
        assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
        assertEquals(resultRows, context.getQueryCache().get(cacheKey));
        // second run, must refresh the cache
        List<?> rows2 = mockupDataRows(4);
        engine.reset();
        engine.addExpectedResult(select, rows2);
        select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE_REFRESH);
        List<?> freshResultRows = context.performQuery(select);
        assertEquals(1, engine.getRunCount());
        assertEquals(4, freshResultRows.size());
        assertTrue(resultRows.get(0) instanceof DataObject);
        assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
        assertEquals(freshResultRows, context.getQueryCache().get(cacheKey));
    } finally {
        engine.stopInterceptNode();
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) DataObject(org.apache.cayenne.DataObject) QueryMetadata(org.apache.cayenne.query.QueryMetadata) Test(org.junit.Test)

Example 5 with QueryMetadata

use of org.apache.cayenne.query.QueryMetadata in project cayenne by apache.

the class DataContextQueryCachingIT method testLocalCacheDataRowsRefresh.

@Test
public void testLocalCacheDataRowsRefresh() throws Exception {
    SelectQuery<DataRow> select = SelectQuery.dataRowQuery(Artist.class);
    select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
    MockDataNode engine = MockDataNode.interceptNode(domain, getNode());
    try {
        // first run, no cache yet
        List<?> rows1 = mockupDataRows(2);
        engine.reset();
        engine.addExpectedResult(select, rows1);
        List<?> resultRows = context.performQuery(select);
        assertEquals(1, engine.getRunCount());
        assertEquals(rows1, resultRows);
        QueryMetadata cacheKey = select.getMetaData(context.getEntityResolver());
        assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
        assertEquals(rows1, context.getQueryCache().get(cacheKey));
        // second run, must refresh the cache
        List<?> rows2 = mockupDataRows(4);
        engine.reset();
        engine.addExpectedResult(select, rows2);
        select.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE_REFRESH);
        List<?> freshResultRows = context.performQuery(select);
        assertEquals(1, engine.getRunCount());
        assertEquals(rows2, freshResultRows);
        assertNull(context.getParentDataDomain().getQueryCache().get(cacheKey));
        assertEquals(rows2, context.getQueryCache().get(cacheKey));
    } finally {
        engine.stopInterceptNode();
    }
}
Also used : QueryMetadata(org.apache.cayenne.query.QueryMetadata) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Aggregations

QueryMetadata (org.apache.cayenne.query.QueryMetadata)10 Test (org.junit.Test)7 List (java.util.List)3 DataRow (org.apache.cayenne.DataRow)3 SelectQuery (org.apache.cayenne.query.SelectQuery)3 ArrayList (java.util.ArrayList)2 EntityResolver (org.apache.cayenne.map.EntityResolver)2 MockQueryMetadata (org.apache.cayenne.query.MockQueryMetadata)2 PrefetchTreeNode (org.apache.cayenne.query.PrefetchTreeNode)2 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)2 Serializable (java.io.Serializable)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 DataObject (org.apache.cayenne.DataObject)1 QueryResponse (org.apache.cayenne.QueryResponse)1 EJBQLTranslationContext (org.apache.cayenne.access.translator.ejbql.EJBQLTranslationContext)1 EJBQLTranslatorFactory (org.apache.cayenne.access.translator.ejbql.EJBQLTranslatorFactory)1 MockQueryCache (org.apache.cayenne.cache.MockQueryCache)1 QueryCache (org.apache.cayenne.cache.QueryCache)1 QueryCacheEntryFactory (org.apache.cayenne.cache.QueryCacheEntryFactory)1