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

发布于 2024-08-29 12:06:21 字数 589 浏览 1 评论 0原文

我有一个 HQL 查询:

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

我喜欢解析这个查询并将其转换为树结构:

AST queryTree = parse(query);

而不是我喜欢迭代节点,更改一些值,然后将树转换回字符串表示形式:

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

如果解析会很好如果违反 HQL 语法,方法会抛出异常,但这不是必需的。 有谁知道如何实现这一点? hibernate 是否提供任何 API 方法来完成该任务?

谢谢,

I have a HQL query:

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?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヅ她的身影、若隐若现 2024-09-05 12:06:21

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文