Search in sources :

Example 11 with DomainModel

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

the class HintProviderTest method requestHint_entityNameHint_simple.

@Test
public void requestHint_entityNameHint_simple() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModelImpl playerEntity = builder.produceImmediately("Player");
    DomainModel model = new DomainModel();
    model.add(playerEntity);
    HintProvider hintProvider = createTestHintProvider(model);
    HintResponse response = hintProvider.requestHint("SELECT p FROM P~");
    List<String> options = response.getOptions();
    assertEquals(1, options.size());
    assertEquals("Player", options.get(0));
    response = hintProvider.requestHint("FROM N~");
    options = response.getOptions();
    assertEquals(0, options.size());
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) JpqlEntityModelImpl(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModelImpl) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) HintProvider(com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider) HintResponse(com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse) Test(org.junit.Test)

Example 12 with DomainModel

use of com.haulmont.cuba.core.sys.jpql.DomainModel 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)

Example 13 with DomainModel

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

the class QueryAnalyzerTest method testTree.

@Test
public void testTree() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTreeTransformer qa = new QueryTreeTransformer();
    String query = "select f from sec$SearchFolder f " + "left join f.user u " + "left join f.presentation p " + "where (f.user.id = ?1 or f.user is null) " + "order by f.sortOrder, f.name";
    qa.prepare(model, query);
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) QueryTreeTransformer(com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer) Test(org.junit.Test)

Example 14 with DomainModel

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

the class QueryAnalyzerTest method replaceWithCount_distinct.

@Test
public void replaceWithCount_distinct() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTreeTransformer qa = new QueryTreeTransformer();
    qa.prepare(model, "select distinct d from Car c, in(c.drivers) d order by d.name");
    CommonTree tree = qa.getTree();
    CommonTree selectedItems = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SELECTED_ITEMS);
    Tree selectedItem = selectedItems.getFirstChildWithType(JPA2Lexer.T_SELECTED_ITEM);
    PathNode pathNode = (PathNode) selectedItem.getChild(0);
    assertEquals("d", pathNode.getEntityVariableName());
    assertEquals(0, pathNode.getChildCount());
    CommonTree orderByNode = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_ORDER_BY);
    assertNotNull(orderByNode.getFirstChildWithType(JPA2Lexer.T_ORDER_BY_FIELD));
    qa.replaceWithCount(new VariableEntityReference("Driver", "d"));
    selectedItems = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SELECTED_ITEMS);
    assertEquals(1, selectedItems.getChildCount());
    selectedItem = selectedItems.getFirstChildWithType(JPA2Lexer.T_SELECTED_ITEM);
    assertTrue(selectedItem.getChild(0) instanceof AggregateExpressionNode);
    AggregateExpressionNode countExpr = (AggregateExpressionNode) selectedItem.getChild(0);
    assertEquals("count", countExpr.getChild(0).getText());
    assertEquals("distinct", countExpr.getChild(2).getText());
    assertEquals("d", countExpr.getChild(3).getText());
    assertEquals(5, countExpr.getChildCount());
    assertNull(orderByNode.getFirstChildWithType(JPA2Lexer.T_ORDER_BY));
}
Also used : 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) VariableEntityReference(com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference) Test(org.junit.Test)

Example 15 with DomainModel

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

the class QueryAnalyzerTest method prepareDomainModel.

private DomainModel prepareDomainModel() {
    EntityBuilder builder = new EntityBuilder();
    builder.startNewEntity("Driver");
    builder.addStringAttribute("name");
    builder.addStringAttribute("signal");
    builder.addReferenceAttribute("car", "Driver");
    JpqlEntityModel driver = builder.produce();
    builder.startNewEntity("Car");
    builder.addStringAttribute("model");
    builder.addStringAttribute("regNumber");
    builder.addCollectionReferenceAttribute("drivers", "Driver");
    JpqlEntityModel car = builder.produce();
    return new DomainModel(car, driver);
}
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)

Aggregations

DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)94 Test (org.junit.Test)86 EntityBuilder (com.haulmont.cuba.core.sys.jpql.model.EntityBuilder)49 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)45 QueryTransformerAstBased (com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)37 HintProvider (com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider)21 HintResponse (com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse)20 QueryTreeTransformer (com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer)8 CommonTree (org.antlr.runtime.tree.CommonTree)7 VariableEntityReference (com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference)5 JpqlEntityModelImpl (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModelImpl)4 JpqlSyntaxException (com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException)3 Tree (org.antlr.runtime.tree.Tree)3 DomainModelBuilder (com.haulmont.cuba.core.sys.jpql.DomainModelBuilder)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 TreeToQuery (com.haulmont.cuba.core.sys.jpql.TreeToQuery)1 PathEntityReference (com.haulmont.cuba.core.sys.jpql.transform.PathEntityReference)1 HintRequest (com.haulmont.cuba.gui.components.autocomplete.impl.HintRequest)1 Option (com.haulmont.cuba.gui.components.autocomplete.impl.Option)1 ArrayList (java.util.ArrayList)1