Search in sources :

Example 1 with PathEntityReference

use of com.haulmont.cuba.core.sys.jpql.transform.PathEntityReference in project cuba by cuba-platform.

the class QueryAnalyzerTest method replaceOrderBy.

@Test
public void replaceOrderBy() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTreeTransformer qa = new QueryTreeTransformer();
    qa.prepare(model, "select c from Car c order by c.model");
    CommonTree tree = qa.getTree();
    CommonTree orderByNode = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_ORDER_BY);
    Tree orderByField = orderByNode.getFirstChildWithType(JPA2Lexer.T_ORDER_BY_FIELD);
    assertEquals(1, orderByField.getChildCount());
    PathNode pathNode = (PathNode) orderByField.getChild(0);
    assertEquals("c", pathNode.getEntityVariableName());
    assertEquals("model", pathNode.getChild(0).getText());
    pathNode = new PathNode(JPA2Lexer.T_SELECTED_FIELD, "c");
    pathNode.addDefaultChild("regNumber");
    qa.replaceOrderBy(true, new PathEntityReference(pathNode, "Car"));
    orderByNode = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_ORDER_BY);
    orderByField = orderByNode.getFirstChildWithType(JPA2Lexer.T_ORDER_BY_FIELD);
    assertEquals(2, orderByField.getChildCount());
    pathNode = (PathNode) orderByField.getChild(0);
    assertEquals("c", pathNode.getEntityVariableName());
    assertEquals("regNumber", pathNode.getChild(0).getText());
    assertEquals("desc", orderByField.getChild(1).getText());
}
Also used : PathEntityReference(com.haulmont.cuba.core.sys.jpql.transform.PathEntityReference) DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) CommonTree(org.antlr.runtime.tree.CommonTree) CommonTree(org.antlr.runtime.tree.CommonTree) Tree(org.antlr.runtime.tree.Tree) QueryTreeTransformer(com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer) Test(org.junit.Test)

Aggregations

DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)1 PathEntityReference (com.haulmont.cuba.core.sys.jpql.transform.PathEntityReference)1 QueryTreeTransformer (com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer)1 CommonTree (org.antlr.runtime.tree.CommonTree)1 Tree (org.antlr.runtime.tree.Tree)1 Test (org.junit.Test)1