表达式语言到 SQL where 子句的转换?
我们有一个域对象,
Person
firstName
lastName
role
我正在寻找一种简单的表达式语言,我们可以编写它来生成 SQL。 (特别是SQL的where子句)
例如。
firstName contains "joe"
翻译为
FIRST_NAME LIKE '%joe%'
是否存在这样的东西,或者我必须自己编写吗?
如果您认为我可能正在寻找青春之泉,或者您认为我写这篇文章是在浪费时间,请告诉我!
We have one domain object,
Person
firstName
lastName
role
I'm looking for a simple expression language that we can write that will generate SQL. (particulary the where clause of SQL)
For example.
firstName contains "joe"
translates to
FIRST_NAME LIKE '%joe%'
Does anything like this exist, or do I have to write my own?
Please let me know if you think I might as well be searching for the fountain of youth, or if you think i'm wasting my time writing this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是来自 Donald D. Chamberlin 和 Raymond F. Boyce 的 1974 年论文 介绍了 SQL(然后是 SEQUEL)。从他们的介绍来看,我猜他们是为非技术用户设计 SQL 的。
这并不是说 SQL是一种完美的语言,尽管经历了三十多年的变化,但它仍然存在缺陷。您有可能构建出更好的东西,但有两件事可能会出错:
也许可以考虑一个折衷方案:提供构建表达式的 UI。技术水平较低的用户可以选择一个属性、一个运算符,然后输入将出现在表达式文本区域中的值,他们可以逐个构建中等复杂的 SQL 表达式,而无需记住 SQL 语法。更熟练的用户可以直接编辑 SQL。
这并不能解决你所有的问题。您仍然需要一种方法来限制 SQL,以便用户不会损坏您的数据库或通过令人难以置信的笛卡尔连接无意中耗尽所有数据库资源。
That's from Donald D. Chamberlin and Raymond F. Boyce's 1974 paper introducing SQL (then SEQUEL). From their introduction, I'd guess they designed SQL for your non-technical users.
That's not to say SQL is a perfect language. Despite thirty-some years of changes, it still has flaws. There's a chance you could build something better, but two things could go wrong:
Maybe consider a compromise: offer an expression-building UI. Less technical users could choose a property, an operator, and enter values which would then appear in an expression text area. They could build moderately complex SQL expressions piece-by-piece without needing to remember SQL syntax. More sophisticated users could edit the SQL directly.
This doesn't solve all of your problems. You'll still need a way to limit SQL so users don't damage your database or inadvertently use up all of your db resources with a mind-boggling Cartesian join.
我建议您研究 Selection Factory 模式。
I suggest you look into the Selection Factory pattern.
我不知道这是否足够,但是有一个用于类似实体语言(域模型)的 xtext 项目(语言开发框架)的示例。也许你想检查一下。它还具有 jpa 代码生成功能(据我所知)。如果不酷,就编写自己的语言,使用 xtext 不应该花太长时间;)
i don't know whether that suffices, but there is an example of the xtext project (a language development framework) for an entity language like that (the domain model). maybe you want to check that out. it also features code generation for jpa (as far as i remember). if it's not cool, just write your own language, shouldn't take too long using xtext ;)