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;
}
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());
}
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());
}
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;
}
}
}
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);
}
Aggregations