将HQL解析为AST结构并将AST转换回HQL

时间:2022-11-21 15:49:46

I have a HQL query:

我有一个HQL查询:

query = select item.itemNumber from items item where item.stock>0 and item.price<100.00

i like to parse this query and convert it into a tree structure:

我想解析此查询并将其转换为树结构:

AST queryTree = parse(query);

than i like to iterate through the nodes, change some values, and convert the tree back to a string represenation:

比我喜欢迭代节点,更改一些值,并将树转换回字符串表示:

Iterator<ASTNode> it = queryTree.nodeIterator();
while(it.hasNext())
{
  ASTNode node = it.next();
  System.out.println( node.text() + "->" + node.value() );
}
query = queryTree.toString();

it would be nice if the parse method would throw Exceptions in case the HQL grammer is violated, but its not necessary. Has anyone an idea how this can be accomplished? Are there any API methods offered by hibernate to accomplish that task?

如果parse方法在违反HQL语法的情况下抛出异常会很好,但是没有必要。有谁知道如何实现这一目标?是否有任何hibernate提供的API方法来完成该任务?

Thanks,

谢谢,

1 个解决方案

#1


0  

You could take a look at the new experimental parser that is being worked on here: https://github.com/hibernate/hibernate-hql-parser

您可以查看正在处理的新实验解析器:https://github.com/hibernate/hibernate-hql-parser

#1


0  

You could take a look at the new experimental parser that is being worked on here: https://github.com/hibernate/hibernate-hql-parser

您可以查看正在处理的新实验解析器:https://github.com/hibernate/hibernate-hql-parser