Search in sources :

Example 26 with JpqlEntityModel

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.

the class QueryTransformerAstBasedTest method getResult_noChangesMade_withMacros.

@Test
public void getResult_noChangesMade_withMacros() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name");
    builder.startNewEntity("Player");
    builder.addSingleValueAttribute(Date.class, "birthDate");
    builder.addReferenceAttribute("team", "Team");
    JpqlEntityModel playerEntity = builder.produce();
    DomainModel model = new DomainModel(playerEntity, teamEntity);
    QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @between(p.birthDate, now-2, now+2, month) ");
    String result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @between ( p.birthDate, now - 2, now + 2, month)", result);
    transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateBefore(p.birthDate, :d) ");
    result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @dateBefore ( p.birthDate, :d)", result);
    transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateAfter(p.birthDate, :d) ");
    result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @dateAfter ( p.birthDate, :d)", result);
    transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateEquals(p.birthDate, :d) ");
    result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @dateEquals ( p.birthDate, :d)", result);
    transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateEquals(p.birthDate, :d, USER_TIMEZONE) ");
    result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @dateEquals ( p.birthDate, :d, USER_TIMEZONE)", result);
    transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @today(p.birthDate) ");
    result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @today ( p.birthDate)", result);
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) Test(org.junit.Test)

Example 27 with JpqlEntityModel

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.

the class QueryTransformationTest method prepareDomainModel.

private DomainModel prepareDomainModel() {
    EntityBuilder builder = new EntityBuilder();
    builder.startNewEntity("sec$GroupHierarchy");
    builder.addStringAttribute("group");
    builder.addStringAttribute("createdBy");
    builder.addReferenceAttribute("parent", "sec$GroupHierarchy");
    builder.addReferenceAttribute("other", "sec$GroupHierarchy");
    builder.addCollectionReferenceAttribute("constraints", "sec$Constraint");
    JpqlEntityModel groupHierarchy = builder.produce();
    JpqlEntityModel constraintEntity = builder.produceImmediately("sec$Constraint");
    return new DomainModel(groupHierarchy, constraintEntity);
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 28 with JpqlEntityModel

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.

the class EntityPointer method next.

@Override
public Pointer next(DomainModel model, String field) {
    Attribute attribute = entity.getAttributeByName(field);
    if (attribute == null) {
        return NoPointer.instance();
    }
    if (!attribute.isEntityReferenceAttribute()) {
        return new SimpleAttributePointer(entity, attribute);
    }
    String targetEntityName = attribute.getReferencedEntityName();
    try {
        JpqlEntityModel targetEntity = model.getEntityByName(targetEntityName);
        return attribute.isCollection() ? new CollectionPointer(targetEntity) : new EntityPointer(targetEntity);
    } catch (UnknownEntityNameException e) {
        return NoPointer.instance();
    }
}
Also used : Attribute(com.haulmont.cuba.core.sys.jpql.model.Attribute) UnknownEntityNameException(com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 29 with JpqlEntityModel

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.

the class QueryTransformerAstBased method getReturnedEntityName.

private String getReturnedEntityName() {
    if (returnedEntityName == null) {
        String returnedVariableName = getQueryTransformer().getFirstReturnedVariableName();
        JpqlEntityModel entity = getQueryTransformer().getRootQueryVariableContext().getEntityByVariableName(returnedVariableName);
        if (entity != null) {
            returnedEntityName = entity.getName();
        }
    }
    return returnedEntityName;
}
Also used : JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 30 with JpqlEntityModel

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.

the class QueryTreeTransformer method getPathNodesForOrderBy.

protected List<PathNode> getPathNodesForOrderBy(PathEntityReference pathEntityReference) {
    List<PathNode> pathNodes = new ArrayList<>();
    String entityName = pathEntityReference.getPathStartingEntityName();
    PathNode pathNode = pathEntityReference.getPathNode();
    try {
        JpqlEntityModel entity = model.getEntityByName(entityName);
        String currentVariableName = pathNode.getEntityVariableName();
        PathNode currentPathNode = new PathNode(pathNode.getToken(), currentVariableName);
        for (int i = 0; i < pathNode.getChildCount(); i++) {
            String fieldName = pathNode.getChild(i).toString();
            Attribute entityAttribute = entity.getAttributeByName(fieldName);
            if (entityAttribute.isEntityReferenceAttribute() && !entityAttribute.isEmbedded()) {
                currentPathNode.addDefaultChild(fieldName);
                pathNodes.add(currentPathNode);
                currentVariableName = currentPathNode.asPathString('_');
                currentPathNode = new PathNode(pathNode.getToken(), currentVariableName);
            } else {
                currentPathNode.addDefaultChild(fieldName);
            }
            if (entityAttribute.isEntityReferenceAttribute()) {
                entityName = entityAttribute.getReferencedEntityName();
                entity = model.getEntityByName(entityName);
            }
        }
        pathNodes.add(currentPathNode);
    } catch (UnknownEntityNameException e) {
        throw new RuntimeException(String.format("Could not find entity by name %s", entityName), e);
    }
    return pathNodes;
}
Also used : Attribute(com.haulmont.cuba.core.sys.jpql.model.Attribute) UnknownEntityNameException(com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException) ArrayList(java.util.ArrayList) 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