Search in sources :

Example 6 with QueryMetadata

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

the class DataDomainQueryActionIT method testCachedQuery.

@Test
public void testCachedQuery() {
    DataDomain domain = runtime.getDataDomain();
    Painting p = context.newObject(Painting.class);
    p.setPaintingTitle("sample");
    SelectQuery query = new SelectQuery(Painting.class);
    query.addPrefetch(Painting.TO_GALLERY.disjoint());
    query.addPrefetch(Painting.TO_ARTIST.disjoint());
    query.addOrdering(Painting.PAINTING_TITLE.asc());
    query.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
    query.setPageSize(5);
    QueryCache cache = domain.queryCache;
    domain.queryCache = new MockQueryCache() {

        @Override
        public List<?> get(QueryMetadata metadata, QueryCacheEntryFactory factory) {
            Object results = factory.createObject();
            assertTrue("Query cache is not serializable.", results instanceof Serializable);
            return null;
        }

        @SuppressWarnings("all")
        @Override
        public void put(QueryMetadata metadata, List results) {
            assertTrue("Query cache is not serializable.", results instanceof Serializable);
        }
    };
    DataDomainQueryAction action = new DataDomainQueryAction(context, domain, query);
    action.execute();
    domain.queryCache = cache;
}
Also used : MockQueryCache(org.apache.cayenne.cache.MockQueryCache) QueryCache(org.apache.cayenne.cache.QueryCache) Serializable(java.io.Serializable) MockQueryCache(org.apache.cayenne.cache.MockQueryCache) QueryMetadata(org.apache.cayenne.query.QueryMetadata) QueryCacheEntryFactory(org.apache.cayenne.cache.QueryCacheEntryFactory) Painting(org.apache.cayenne.testdo.testmap.Painting) SelectQuery(org.apache.cayenne.query.SelectQuery) List(java.util.List) Test(org.junit.Test)

Example 7 with QueryMetadata

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

the class PrefetchProcessorTreeBuilderIT method testBuildTreeNoPrefetches.

@Test
public void testBuildTreeNoPrefetches() {
    final ClassDescriptor descriptor = resolver.getClassDescriptor("Artist");
    List<Object> dataRows = new ArrayList<Object>();
    dataRows.add(new DataRow(4));
    dataRows.add(new DataRow(4));
    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;
        }
    };
    PrefetchTreeNode tree = new PrefetchTreeNode();
    HierarchicalObjectResolver resolver = new HierarchicalObjectResolver(context, metadata);
    PrefetchProcessorTreeBuilder builder = new PrefetchProcessorTreeBuilder(resolver, dataRows, new HashMap<Object, Object>());
    PrefetchProcessorNode processingTree = builder.buildTree(tree);
    assertTrue(processingTree.getChildren().isEmpty());
    assertFalse(processingTree.isPhantom());
    assertFalse(processingTree.isPartitionedByParent());
    assertTrue(processingTree.isDisjointPrefetch());
    assertSame(dataRows, processingTree.getDataRows());
    assertSame(descriptor.getEntity(), processingTree.getResolver().getEntity());
    assertNull(processingTree.getIncoming());
}
Also used : MockQueryMetadata(org.apache.cayenne.query.MockQueryMetadata) QueryMetadata(org.apache.cayenne.query.QueryMetadata) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) MockQueryMetadata(org.apache.cayenne.query.MockQueryMetadata) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) ArrayList(java.util.ArrayList) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 8 with QueryMetadata

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

the class DataContextIT method testPerformQuery_Routing.

@Test
public void testPerformQuery_Routing() {
    Query query = mock(Query.class);
    QueryMetadata md = mock(QueryMetadata.class);
    when(query.getMetaData(any(EntityResolver.class))).thenReturn(md);
    context.performGenericQuery(query);
    verify(query).route(any(QueryRouter.class), eq(context.getEntityResolver()), (Query) isNull());
}
Also used : QueryMetadata(org.apache.cayenne.query.QueryMetadata) SelectQuery(org.apache.cayenne.query.SelectQuery) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Query(org.apache.cayenne.query.Query) QueryRouter(org.apache.cayenne.query.QueryRouter) EntityResolver(org.apache.cayenne.map.EntityResolver) Test(org.junit.Test)

Example 9 with QueryMetadata

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

the class BaseSQLAction method readResultSet.

/**
 * Helper method to process a ResultSet.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void readResultSet(ResultSet resultSet, RowDescriptor descriptor, Query query, OperationObserver delegate) throws SQLException, Exception {
    long t1 = System.currentTimeMillis();
    QueryMetadata metadata = query.getMetaData(dataNode.getEntityResolver());
    RowReader<?> rowReader = dataNode.rowReader(descriptor, metadata);
    JDBCResultIterator resultReader = new JDBCResultIterator(null, resultSet, rowReader);
    LimitResultIterator it = new LimitResultIterator(resultReader, getInMemoryOffset(metadata.getFetchOffset()), metadata.getFetchLimit());
    if (!delegate.isIteratedResult()) {
        List resultRows = it.allRows();
        dataNode.getJdbcEventLogger().logSelectCount(resultRows.size(), System.currentTimeMillis() - t1);
        delegate.nextRows(query, resultRows);
    } else {
        try {
            delegate.nextRows(query, it);
        } catch (Exception ex) {
            it.close();
            throw ex;
        }
    }
}
Also used : QueryMetadata(org.apache.cayenne.query.QueryMetadata) List(java.util.List) SQLException(java.sql.SQLException)

Example 10 with QueryMetadata

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

the class EJBQLAction method performAction.

@Override
public void performAction(Connection connection, OperationObserver observer) throws SQLException, Exception {
    EJBQLCompiledExpression compiledExpression = query.getExpression(dataNode.getEntityResolver());
    final EJBQLTranslatorFactory translatorFactory = dataNode.getAdapter().getEjbqlTranslatorFactory();
    final EJBQLTranslationContext context = new EJBQLTranslationContext(dataNode.getEntityResolver(), query, compiledExpression, translatorFactory, dataNode.getAdapter().getQuotingStrategy());
    compiledExpression.getExpression().visit(new EJBQLBaseVisitor(false) {

        @Override
        public boolean visitSelect(EJBQLExpression expression) {
            EJBQLExpressionVisitor visitor = translatorFactory.getSelectTranslator(context);
            expression.visit(visitor);
            return false;
        }

        @Override
        public boolean visitDelete(EJBQLExpression expression) {
            EJBQLExpressionVisitor visitor = translatorFactory.getDeleteTranslator(context);
            expression.visit(visitor);
            return false;
        }

        @Override
        public boolean visitUpdate(EJBQLExpression expression) {
            EJBQLExpressionVisitor visitor = translatorFactory.getUpdateTranslator(context);
            expression.visit(visitor);
            return false;
        }
    });
    SQLTemplate sqlQuery = context.getQuery();
    // update with metadata
    QueryMetadata md = query.getMetaData(dataNode.getEntityResolver());
    sqlQuery.setFetchLimit(md.getFetchLimit());
    sqlQuery.setFetchOffset(md.getFetchOffset());
    sqlQuery.setResult(compiledExpression.getResult());
    sqlQuery.setPageSize(md.getPageSize());
    if (md.getStatementFetchSize() != 0) {
        sqlQuery.setStatementFetchSize(md.getStatementFetchSize());
    }
    actionFactory.sqlAction(sqlQuery).performAction(connection, observer);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) EJBQLTranslationContext(org.apache.cayenne.access.translator.ejbql.EJBQLTranslationContext) EJBQLBaseVisitor(org.apache.cayenne.ejbql.EJBQLBaseVisitor) QueryMetadata(org.apache.cayenne.query.QueryMetadata) EJBQLCompiledExpression(org.apache.cayenne.ejbql.EJBQLCompiledExpression) EJBQLTranslatorFactory(org.apache.cayenne.access.translator.ejbql.EJBQLTranslatorFactory) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression) EJBQLExpressionVisitor(org.apache.cayenne.ejbql.EJBQLExpressionVisitor)

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