Search in sources :

Example 1 with ASTIterator

use of org.hibernate.hql.internal.ast.util.ASTIterator in project hibernate-orm by hibernate.

the class ASTIteratorTest method testSimpleTree.

@Test
public void testSimpleTree() throws Exception {
    String input = "select foo from foo in class org.hibernate.test.Foo, fee in class org.hibernate.test.Fee where foo.dependent = fee order by foo.string desc, foo.component.count asc, fee.id";
    HqlParser parser = HqlParser.getInstance(input);
    parser.statement();
    AST ast = parser.getAST();
    ASTPrinter printer = new ASTPrinter(HqlTokenTypes.class);
    printer.showAst(ast, new PrintWriter(System.out));
    ASTIterator iterator = new ASTIterator(ast);
    int count = 0;
    while (iterator.hasNext()) {
        assertTrue(iterator.next() instanceof AST);
        count++;
    }
    assertEquals(43, count);
    UnsupportedOperationException uoe = null;
    try {
        iterator.remove();
    } catch (UnsupportedOperationException e) {
        uoe = e;
    }
    assertNotNull(uoe);
}
Also used : AST(antlr.collections.AST) ASTPrinter(org.hibernate.hql.internal.ast.util.ASTPrinter) HqlParser(org.hibernate.hql.internal.ast.HqlParser) ASTIterator(org.hibernate.hql.internal.ast.util.ASTIterator) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 2 with ASTIterator

use of org.hibernate.hql.internal.ast.util.ASTIterator in project hibernate-orm by hibernate.

the class HqlParserTest method testLineAndColumnNumber.

@Test
public void testLineAndColumnNumber() throws Exception {
    AST ast = doParse("from Foo f\nwhere f.name = 'fred'", false);
    // Find some of the nodes and check line and column values.
    ASTIterator iter = new ASTIterator(ast);
    boolean foundFoo = false;
    boolean foundName = false;
    while (iter.hasNext()) {
        AST n = iter.nextNode();
        if ("Foo".equals(n.getText())) {
            if (foundFoo)
                fail("Already found 'Foo'!");
            foundFoo = true;
            Node node = (Node) n;
            assertEquals(1, node.getLine());
            assertEquals(6, node.getColumn());
        } else if ("name".equals(n.getText())) {
            if (foundName)
                fail("Already found 'name'!");
            foundName = true;
            Node node = (Node) n;
            assertEquals(2, node.getLine());
            assertEquals(9, node.getColumn());
        }
    }
    assertTrue(foundFoo);
    assertTrue(foundName);
}
Also used : AST(antlr.collections.AST) Node(org.hibernate.hql.internal.ast.tree.Node) ASTIterator(org.hibernate.hql.internal.ast.util.ASTIterator) Test(org.junit.Test)

Example 3 with ASTIterator

use of org.hibernate.hql.internal.ast.util.ASTIterator in project hibernate-orm by hibernate.

the class SelectClause method renderNonScalarProperties.

private void renderNonScalarProperties(ASTAppender appender, SelectExpression selectExpression, FromElement fromElement, int nonscalarSize, int k) {
    final String text;
    if (selectExpression instanceof MapKeyNode) {
        final MapKeyNode mapKeyNode = (MapKeyNode) selectExpression;
        if (mapKeyNode.getMapKeyEntityFromElement() != null) {
            text = mapKeyNode.getMapKeyEntityFromElement().renderMapKeyPropertySelectFragment(nonscalarSize, k);
        } else {
            text = fromElement.renderPropertySelect(nonscalarSize, k);
        }
    } else if (selectExpression instanceof MapEntryNode) {
        text = fromElement.renderMapEntryPropertySelectFragment(nonscalarSize, k);
    } else {
        text = fromElement.renderPropertySelect(nonscalarSize, k);
    }
    appender.append(SqlTokenTypes.SQL_TOKEN, text, false);
    if (fromElement.getQueryableCollection() != null && fromElement.isFetch()) {
        String subText1 = fromElement.renderCollectionSelectFragment(nonscalarSize, k);
        appender.append(SqlTokenTypes.SQL_TOKEN, subText1, false);
    }
    // Look through the FromElement's children to find any collections of values that should be fetched...
    ASTIterator itr = new ASTIterator(fromElement);
    while (itr.hasNext()) {
        FromElement child = (FromElement) itr.next();
        if (child.isCollectionOfValuesOrComponents() && child.isFetch()) {
            // Need a better way to define the suffixes here...
            final String subText2 = child.renderValueCollectionSelectFragment(nonscalarSize, nonscalarSize + k);
            appender.append(SqlTokenTypes.SQL_TOKEN, subText2, false);
        }
    }
}
Also used : ASTIterator(org.hibernate.hql.internal.ast.util.ASTIterator)

Example 4 with ASTIterator

use of org.hibernate.hql.internal.ast.util.ASTIterator in project hibernate-orm by hibernate.

the class FromClause method resolve.

public void resolve() {
    // Make sure that all from elements registered with this FROM clause are actually in the AST.
    ASTIterator iter = new ASTIterator(this.getFirstChild());
    Set childrenInTree = new HashSet();
    while (iter.hasNext()) {
        childrenInTree.add(iter.next());
    }
    for (FromElement fromElement : fromElements) {
        if (!childrenInTree.contains(fromElement)) {
            throw new IllegalStateException("Element not in AST: " + fromElement);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ASTIterator(org.hibernate.hql.internal.ast.util.ASTIterator) HashSet(java.util.HashSet)

Example 5 with ASTIterator

use of org.hibernate.hql.internal.ast.util.ASTIterator in project hibernate-orm by hibernate.

the class ASTIteratorTest method testParentsFirstIterator.

@Test
public void testParentsFirstIterator() throws Exception {
    AST[] tree = new AST[4];
    AST grandparent = tree[0] = ASTUtil.create(factory, 1, "grandparent");
    AST parent = tree[1] = ASTUtil.create(factory, 2, "parent");
    AST child = tree[2] = ASTUtil.create(factory, 3, "child");
    AST baby = tree[3] = ASTUtil.create(factory, 4, "baby");
    AST t = ASTUtil.createTree(factory, tree);
    AST brother = ASTUtil.create(factory, 10, "brother");
    child.setNextSibling(brother);
    AST sister = ASTUtil.create(factory, 11, "sister");
    brother.setNextSibling(sister);
    AST uncle = factory.make(new AST[] { factory.create(20, "uncle"), factory.create(21, "cousin1"), factory.create(22, "cousin2"), factory.create(23, "cousin3") });
    parent.setNextSibling(uncle);
    System.out.println(t.toStringTree());
    System.out.println("--- ASTParentsFirstIterator ---");
    ASTParentsFirstIterator iter = new ASTParentsFirstIterator(t);
    int count = 0;
    while (iter.hasNext()) {
        AST n = iter.nextNode();
        count++;
        System.out.println(n);
    }
    assertEquals(10, count);
    System.out.println("--- ASTIterator ---");
    ASTIterator iter2 = new ASTIterator(t);
    int count2 = 0;
    while (iter2.hasNext()) {
        AST n = iter2.nextNode();
        count2++;
        System.out.println(n);
    }
    assertEquals(10, count2);
    System.out.println("--- ASTParentsFirstIterator (parent) ---");
    ASTParentsFirstIterator iter3 = new ASTParentsFirstIterator(parent);
    int count3 = 0;
    while (iter3.hasNext()) {
        AST n = iter3.nextNode();
        count3++;
        System.out.println(n);
    }
    assertEquals(5, count3);
}
Also used : AST(antlr.collections.AST) ASTIterator(org.hibernate.hql.internal.ast.util.ASTIterator) ASTParentsFirstIterator(org.hibernate.hql.internal.ast.util.ASTParentsFirstIterator) Test(org.junit.Test)

Aggregations

ASTIterator (org.hibernate.hql.internal.ast.util.ASTIterator)5 AST (antlr.collections.AST)3 Test (org.junit.Test)3 PrintWriter (java.io.PrintWriter)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 HqlParser (org.hibernate.hql.internal.ast.HqlParser)1 Node (org.hibernate.hql.internal.ast.tree.Node)1 ASTParentsFirstIterator (org.hibernate.hql.internal.ast.util.ASTParentsFirstIterator)1 ASTPrinter (org.hibernate.hql.internal.ast.util.ASTPrinter)1