保存 HQL 或 EJB QL 的数据结构
我们需要生成一个相当复杂的动态查询构建器来动态检索报告。我们对哪种数据结构最好有点摸不着头脑。
它实际上只不过是保存一个 selectParts 列表、一个 fromParts 列表、一个 where criteria、order by、group by 的列表,以实现持久性。当我们开始考虑连接,尤其是外连接、having 子句和聚合函数时,事情开始变得有点模糊。
我们现在首先构建界面,并尝试尽可能超前地思考,但当我们发现结构的局限性时,肯定会进行一系列重构。
我在这里发布这个问题是希望有人已经提出了一些我们可以作为基础的东西。或者知道一些图书馆或类似的图书馆。在我们下周深入实施之前,最好能获得一些关于潜在问题的提示或提示。
We need to produce a fairly complex dynamic query builder for retrieving reports on the fly. We're scratching our heads a little on what sort of data structure would be best.
It's really nothing more than holding a list of selectParts, a list of fromParts, a list of where criteria, order by, group by, that sort of thing, for persistence. When we start thinking about joins, especially outer joins, having clauses, and aggregate functions, things start getting a little fuzzy.
We're building it up interfaces first for now and trying to think as far ahead as we can, but definitely will go through a series of refactorings when we discover limitations with our structures.
I'm posting this question here in the hopes that someone has already come up with something that we can base it on. Or know of some library or some such. It would be nice to get some tips or heads-up on potential issues before we dive into implementations next week.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我过去曾做过几次类似的事情。我想到了一些更重要的事情。
例如,如果您有
那么树就是
那么您遍历树。深度优先并在备份时合并 HQL 的渲染位,例如,upper 函数期望渲染一段子 HQL,然后生成
“upper( " + childHql + " )"
并将其传递给它的父级。 Between 需要三个子 HQL 片段。
然后,您可以在 select/group by/order by 子句中重新使用表达式模型
您可以如果您愿意,只需存储选择并在查询构造扫描之前扫描聚合即可跳过存储组。如果有一个或多个,则只需将所有非聚合选择表达式复制到分组依据中。
From 子句只是表引用的列表 + 零个或多个连接子句。每个连接子句都有一个类型(内/左/右)和一个表引用。表引用是表名 + 可选别名。
另外,如果您想要解析查询语言(或任何真正的语言),那么我强烈推荐 ANTLR。学习曲线相当陡峭,但有很多语法示例可供查看。
HTH。
I've done something similar couple of times in the past. A couple of the bigger things spring to mind..
e.g. If you had
Then the tree is
Then you walk the tree depth first and merge rendered bits of HQL on the way back up. The upper function for example would expect one piece of child HQL to be rendered and it would then generate
"upper( " + childHql + " )"
and pass that up to it's parent. Something like Between expects three child HQL pieces.
You can then re-use the expression model in the select/group by/order by clauses
You can skip storing the group by if you wish by just storing the select and before query construction scan for aggregate. If there is one or more then just copy all the non-aggregate select expressions into the group by.
From clause is just a list of table reference + zero or more join clauses. Each join clause has a type (inner/left/right) and a table reference. Table reference is a table name + optional alias.
Plus, if you ever get into wanting to parse a query language (or anything really) then I can highly recommend ANTLR. Learning curve is quite steep but there are plenty of example grammars to look at.
HTH.
如果您需要 EJB-QL 解析器和数据结构,EclipseLink(以及它的几个内部类)有一个很好的选择:
JPQLParseTree
包含所有数据。但是从修改后的 JPQLParseTree 生成 EJB-QL 是您必须自己做的事情。
if you need EJB-QL parser and data structures, EclipseLink (well several of it's internal classes) have good one:
JPQLParseTree
contains the all the data.but generating EJB-QL back from modified
JPQLParseTree
is something you have to do yourself.