Search in sources :

Example 6 with JPA2Parser

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

the class Jpa2GrammarTest method testMemberOf.

@Test
@Ignore
public void testMemberOf() throws Exception {
    String query = "where p.owner.id = :userParam or (select u from tamsy$User u where u.id = :userParam) member of p.developers";
    CharStream cs = new AntlrNoCaseStringStream(query);
    JPA2Lexer lexer = new JPA2Lexer(cs);
    TokenStream tstream = new CommonTokenStream(lexer);
    JPA2Parser jpa2Parser = new JPA2Parser(tstream);
    JPA2Parser.where_clause_return aReturn = jpa2Parser.where_clause();
    Assert.assertTrue(isValid((CommonTree) aReturn.getTree()));
    testQuery("SELECT d FROM app$Department d WHERE (select e from app$Employee e where e.id = :eParam) MEMBER OF e.employees");
    testQuery("SELECT e FROM app$Employee e WHERE 'write code' MEMBER OF e.codes");
    testQuery("SELECT e FROM app$Employee e WHERE 'write code' NOT MEMBER OF e.codes");
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) JPA2Parser(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser) JPA2Lexer(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer) CharStream(org.antlr.runtime.CharStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with JPA2Parser

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

the class Jpa2GrammarTest method testQuery.

private void testQuery(String query) throws RecognitionException {
    CharStream cs = new AntlrNoCaseStringStream(query);
    JPA2Lexer lexer = new JPA2Lexer(cs);
    TokenStream tstream = new CommonTokenStream(lexer);
    JPA2Parser jpa2Parser = new JPA2Parser(tstream);
    JPA2Parser.ql_statement_return aReturn = jpa2Parser.ql_statement();
    Assert.assertTrue(isValid((CommonTree) aReturn.getTree()));
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) JPA2Parser(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser) JPA2Lexer(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer) CharStream(org.antlr.runtime.CharStream)

Example 8 with JPA2Parser

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

the class Parser method createParser.

private static JPA2Parser createParser(String input) {
    if (input.contains("~"))
        throw new IllegalArgumentException("Input string cannot contain \"~\"");
    CharStream cs = new AntlrNoCaseStringStream(input);
    JPA2Lexer lexer = new JPA2Lexer(cs);
    TokenStream tstream = new CommonTokenStream(lexer);
    return new JPA2Parser(tstream);
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) JPA2Parser(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser) JPA2Lexer(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer) CharStream(org.antlr.runtime.CharStream)

Example 9 with JPA2Parser

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

the class Parser method parseWhereClause.

public static CommonTree parseWhereClause(String input) throws RecognitionException {
    JPA2Parser parser = createParser(input);
    JPA2Parser.where_clause_return aReturn = parser.where_clause();
    CommonTree tree = (CommonTree) aReturn.getTree();
    checkTreeForExceptions(input, tree);
    return tree;
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) JPA2Parser(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser)

Example 10 with JPA2Parser

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

the class Parser method parseJoinClause.

public static List<JoinVariableNode> parseJoinClause(String join) throws RecognitionException {
    JPA2Parser parser = createParser(join);
    JPA2Parser.join_section_return aReturn = parser.join_section();
    CommonTree tree = (CommonTree) aReturn.getTree();
    if (tree == null) {
        parser = createParser("join " + join);
        aReturn = parser.join_section();
        tree = (CommonTree) aReturn.getTree();
    }
    if (tree instanceof JoinVariableNode) {
        checkTreeForExceptions(join, tree);
        return Collections.singletonList((JoinVariableNode) tree);
    } else {
        List<JoinVariableNode> joins = tree.getChildren().stream().filter(node -> node instanceof JoinVariableNode).map(JoinVariableNode.class::cast).collect(Collectors.toList());
        checkTreeForExceptions(join, tree);
        return joins;
    }
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) JPA2Parser(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser) JoinVariableNode(com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode)

Aggregations

JPA2Parser (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser)10 CommonTree (org.antlr.runtime.tree.CommonTree)9 JPA2Lexer (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer)6 CharStream (org.antlr.runtime.CharStream)6 CommonTokenStream (org.antlr.runtime.CommonTokenStream)6 TokenStream (org.antlr.runtime.TokenStream)6 Test (org.junit.Test)4 JoinVariableNode (com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode)1 Ignore (org.junit.Ignore)1