use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class PrefetchNodeStage method processJoint.
private void processJoint(TranslatorContext context) {
QueryMetadata queryMetadata = context.getMetadata();
PrefetchTreeNode prefetch = queryMetadata.getPrefetchTree();
if (prefetch == null) {
return;
}
ObjEntity objEntity = queryMetadata.getObjEntity();
boolean warnPrefetchWithLimit = false;
for (PrefetchTreeNode node : prefetch.adjacentJointNodes()) {
Expression prefetchExp = ExpressionFactory.exp(node.getPath());
ASTDbPath dbPrefetch = (ASTDbPath) objEntity.translateToDbPath(prefetchExp);
final String dbPath = dbPrefetch.getPath();
DbEntity dbEntity = objEntity.getDbEntity();
PathComponents components = new PathComponents(dbPath);
StringBuilder fullPath = new StringBuilder();
for (String c : components.getAll()) {
DbRelationship rel = dbEntity.getRelationship(c);
if (rel == null) {
throw new CayenneRuntimeException("Unable to resolve path %s for entity %s", dbPath, objEntity.getName());
}
if (fullPath.length() > 0) {
fullPath.append('.');
}
context.getTableTree().addJoinTable("p:" + fullPath.append(c).toString(), rel, JoinType.LEFT_OUTER);
dbEntity = rel.getTargetEntity();
}
ObjRelationship targetRel = (ObjRelationship) prefetchExp.evaluate(objEntity);
ClassDescriptor prefetchClassDescriptor = context.getResolver().getClassDescriptor(targetRel.getTargetEntityName());
DescriptorColumnExtractor columnExtractor = new DescriptorColumnExtractor(context, prefetchClassDescriptor);
columnExtractor.extract("p:" + dbPath);
if (!warnPrefetchWithLimit && targetRel.isToMany() && (queryMetadata.getFetchLimit() > 0 || queryMetadata.getFetchOffset() > 0)) {
warnPrefetchWithLimit = true;
}
}
// warn about a potentially faulty joint prefetch + limit combination
if (warnPrefetchWithLimit) {
LOGGER.warn("The query uses both limit/offset and a joint prefetch, this most probably will lead to an incorrect result. " + "Either use disjointById prefetch or get a full result set.");
}
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class QualifierTranslationStage method perform.
@Override
public void perform(TranslatorContext context) {
QualifierTranslator translator = context.getQualifierTranslator();
Expression expression = context.getQuery().getQualifier();
// Attaching Obj entity's qualifier
ObjEntity entity = context.getMetadata().getObjEntity();
if (entity != null) {
ClassDescriptor descriptor = context.getMetadata().getClassDescriptor();
Expression entityQualifier = descriptor.getEntityInheritanceTree().qualifierForEntityAndSubclasses();
if (entityQualifier != null) {
expression = expression == null ? entityQualifier : expression.andExp(entityQualifier);
}
}
Node qualifierNode = translator.translate(expression);
context.setQualifierNode(qualifierNode);
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class ObjPathProcessorIT3 method prepareTranslationContext.
@Before
public void prepareTranslationContext() {
TranslatorContext translatorContext = new TranslatorContext(new FluentSelectWrapper(ObjectSelect.query(Object.class)), Mockito.mock(DbAdapter.class), context.getEntityResolver(), null);
ObjEntity entity = context.getEntityResolver().getObjEntity("FlattenedTest5");
pathProcessor = new ObjPathProcessor(translatorContext, entity, null);
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class OrderingStageTest method prepareContext.
@Before
public void prepareContext() {
DbEntity dbEntity = new DbEntity();
dbEntity.setName("mock");
DbAttribute dbAttribute = new DbAttribute();
dbAttribute.setName("path");
dbEntity.addAttribute(dbAttribute);
ObjEntity objEntity = new ObjEntity();
objEntity.setName("mock");
objEntity.setDbEntity(dbEntity);
ObjAttribute objAttribute = new ObjAttribute();
objAttribute.setName("path");
objAttribute.setDbAttributePath("path");
objEntity.addAttribute(objAttribute);
DataMap dataMap = new DataMap();
dataMap.addObjEntity(objEntity);
dataMap.addDbEntity(dbEntity);
Ordering ordering = new Ordering("path");
ordering.setDescending();
TranslatableQueryWrapper wrapper = new MockQueryWrapperBuilder().withOrderings(Collections.singleton(ordering)).withMetaData(new MockQueryMetadataBuilder().withDbEntity(dbEntity).withObjEntity(objEntity).build()).build();
context = new MockTranslatorContext(wrapper);
}
use of org.apache.cayenne.map.ObjEntity in project cayenne by apache.
the class CustomColumnSetExtractorTest method testExtractWithoutPrefix.
@Test
public void testExtractWithoutPrefix() {
DbEntity mockDbEntity = createMockDbEntity("mock");
TranslatableQueryWrapper wrapper = new MockQueryWrapperBuilder().withNeedsResultSetMapping(true).withMetaData(new MockQueryMetadataBuilder().withDbEntity(mockDbEntity).build()).build();
TranslatorContext context = new MockTranslatorContext(wrapper);
DataMap dataMap = new DataMap();
dataMap.addDbEntity(mockDbEntity);
ObjEntity entity = new ObjEntity();
entity.setName("mock");
entity.setDataMap(dataMap);
entity.setDbEntity(mockDbEntity);
ObjAttribute attribute = new ObjAttribute();
attribute.setName("not_name");
attribute.setDbAttributePath("name");
attribute.setType("my.type");
entity.addAttribute(attribute);
dataMap.addObjEntity(entity);
EntityResolver resolver = new EntityResolver();
resolver.addDataMap(dataMap);
BaseProperty<?> property0 = PropertyFactory.createBase(ExpressionFactory.dbPathExp("name"), String.class);
Collection<Property<?>> properties = Collections.singleton(property0);
CustomColumnSetExtractor extractor = new CustomColumnSetExtractor(context, properties);
extractor.extract();
assertEquals(1, context.getResultNodeList().size());
ResultNodeDescriptor descriptor0 = context.getResultNodeList().get(0);
assertSame(property0, descriptor0.getProperty());
assertNotNull(descriptor0.getNode());
assertThat(descriptor0.getNode(), instanceOf(ColumnNode.class));
assertFalse(descriptor0.isAggregate());
assertTrue(descriptor0.isInDataRow());
assertNotNull(descriptor0.getDbAttribute());
assertNull(descriptor0.getDataRowKey());
assertEquals(Types.VARBINARY, descriptor0.getJdbcType());
assertEquals("java.lang.String", descriptor0.getJavaType());
}
Aggregations