use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class QueryAnalyzerTest method mixinJoinIntoTree_with_in_collections.
@Test
public void mixinJoinIntoTree_with_in_collections() throws RecognitionException {
EntityBuilder builder = new EntityBuilder();
builder.startNewEntity("HomeBase");
builder.addStringAttribute("name");
JpqlEntityModel homeBase = builder.produce();
builder.startNewEntity("Driver");
builder.addStringAttribute("name");
builder.addStringAttribute("signal");
JpqlEntityModel driver = builder.produce();
builder.startNewEntity("Car");
builder.addStringAttribute("model");
builder.addCollectionReferenceAttribute("drivers", "Driver");
builder.addReferenceAttribute("station", "HomeBase");
JpqlEntityModel car = builder.produce();
DomainModel model = new DomainModel(car, driver, homeBase);
JoinVariableNode join = Parser.parseJoinClause("join c.station h").get(0);
QueryTreeTransformer qa = new QueryTreeTransformer();
qa.prepare(model, "select d.name from Car c, in(c.drivers) d");
CommonTree tree = qa.getTree();
CommonTree sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
assertEquals(2, sources.getChildCount());
assertTrue(sources.getChild(0) instanceof SelectionSourceNode);
CommonTree source0 = (CommonTree) sources.getChild(0);
assertEquals(1, source0.getChildCount());
assertTrue(source0.getChild(0) instanceof IdentificationVariableNode);
assertTrue(sources.getChild(1) instanceof SelectionSourceNode);
CommonTree source1 = (CommonTree) sources.getChild(1);
assertTrue(source1.getChild(0) instanceof CollectionMemberNode);
qa.mixinJoinIntoTree(join, new VariableEntityReference("Car", "c"), true);
tree = qa.getTree();
sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
assertEquals(2, sources.getChildCount());
assertTrue(sources.getChild(0) instanceof SelectionSourceNode);
source0 = (CommonTree) sources.getChild(0);
assertEquals(2, source0.getChildCount());
assertTrue(source0.getChild(0) instanceof IdentificationVariableNode);
assertTrue(source0.getChild(1) instanceof JoinVariableNode);
assertTrue(sources.getChild(1) instanceof SelectionSourceNode);
source1 = (CommonTree) sources.getChild(1);
assertTrue(source1.getChild(0) instanceof CollectionMemberNode);
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class EntityReferenceInferer method infer.
public EntityReference infer(QueryTreeTransformer queryAnalyzer) {
String entityVariableNameInQuery = queryAnalyzer.getRootEntityVariableName(entityName);
if (entityVariableNameInQuery != null) {
return new VariableEntityReference(entityName, entityVariableNameInQuery);
}
PathNode path = queryAnalyzer.getSelectedPathNode();
JpqlEntityModel entity = queryAnalyzer.getSelectedEntity(path);
if (!(entity instanceof VirtualJpqlEntityModel) && entity.getName().equals(entityName)) {
JpqlEntityModel pathStartingEntity = queryAnalyzer.getRootQueryVariableContext().getEntityByVariableName(path.getEntityVariableName());
return new PathEntityReference(path, pathStartingEntity.getName());
}
throw new RuntimeException(String.format("No variable or selected field of entity %s found in query", entityName));
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class QueryTransformerAstBased method getMainEntityName.
private String getMainEntityName() {
if (mainEntityName == null) {
IdentificationVariableNode mainEntityIdentification = getQueryTransformer().getMainEntityIdentification();
if (mainEntityIdentification != null) {
try {
JpqlEntityModel entityByName = model.getEntityByName(mainEntityIdentification.getEntityNameFromQuery());
mainEntityName = entityByName.getName();
} catch (UnknownEntityNameException e) {
throw new RuntimeException("Could not resolve entity for name " + mainEntityIdentification.getEntityNameFromQuery());
}
}
}
return mainEntityName;
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class DomainModel method getEntityByName.
public JpqlEntityModel getEntityByName(String requiredEntityName) throws UnknownEntityNameException {
if (extendedEntities != null) {
MetaClass effectiveMetaClass = extendedEntities.getEffectiveMetaClass(requiredEntityName);
requiredEntityName = effectiveMetaClass.getName();
}
JpqlEntityModel entity = entities.get(requiredEntityName);
if (entity == null) {
throw new UnknownEntityNameException(requiredEntityName);
} else {
return entity;
}
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class EntitiesFinder method resolveEntityNames.
protected void resolveEntityNames(Set<String> entityNames, PathNode node, DomainModel model, QueryVariableContext queryVariableContext) {
List<Pointer> pointers = node.resolveTransitionalPointers(model, queryVariableContext);
pointers.stream().filter(pointer -> pointer instanceof HasEntityPointer).forEach(pointer -> {
JpqlEntityModel entityModel = ((HasEntityPointer) pointer).getEntity();
if (entityModel != null) {
entityNames.add(entityModel.getName());
}
});
}
Aggregations