Search in sources :

Example 11 with JpqlEntityModel

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);
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) CommonTree(org.antlr.runtime.tree.CommonTree) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) QueryTreeTransformer(com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer) VariableEntityReference(com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference) Test(org.junit.Test)

Example 12 with JpqlEntityModel

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));
}
Also used : VirtualJpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.VirtualJpqlEntityModel) PathNode(com.haulmont.cuba.core.sys.jpql.tree.PathNode) VirtualJpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.VirtualJpqlEntityModel) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 13 with JpqlEntityModel

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;
}
Also used : IdentificationVariableNode(com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 14 with JpqlEntityModel

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;
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 15 with JpqlEntityModel

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());
        }
    });
}
Also used : HashSet(java.util.HashSet) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) Pointer(com.haulmont.cuba.core.sys.jpql.pointer.Pointer) CommonTree(org.antlr.runtime.tree.CommonTree) List(java.util.List) IdentificationVariableNode(com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode) Set(java.util.Set) HasEntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.HasEntityPointer) TreeVisitorAction(org.antlr.runtime.tree.TreeVisitorAction) PathNode(com.haulmont.cuba.core.sys.jpql.tree.PathNode) JoinVariableNode(com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode) ArrayList(java.util.ArrayList) HasEntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.HasEntityPointer) Pointer(com.haulmont.cuba.core.sys.jpql.pointer.Pointer) HasEntityPointer(com.haulmont.cuba.core.sys.jpql.pointer.HasEntityPointer) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Aggregations

JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)57 EntityBuilder (com.haulmont.cuba.core.sys.jpql.model.EntityBuilder)46 DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)45 Test (org.junit.Test)40 HintProvider (com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider)17 HintResponse (com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse)16 QueryTransformerAstBased (com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)9 ArrayList (java.util.ArrayList)5 Attribute (com.haulmont.cuba.core.sys.jpql.model.Attribute)4 PathNode (com.haulmont.cuba.core.sys.jpql.tree.PathNode)4 IdentificationVariableNode (com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 UnknownEntityNameException (com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException)2 NoJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.NoJpqlEntityModel)2 Pointer (com.haulmont.cuba.core.sys.jpql.pointer.Pointer)2 List (java.util.List)2 CommonTree (org.antlr.runtime.tree.CommonTree)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 JPA2RecognitionException (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException)1 VirtualJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.VirtualJpqlEntityModel)1