表达式语言到 SQL where 子句的转换?

发布于 2024-10-06 09:21:34 字数 346 浏览 0 评论 0原文

我们有一个域对象,

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 技术交流群。

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

发布评论

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

评论(3

迷爱 2024-10-13 09:21:34

“不过,还有一个大类
的用户,虽然他们不是
计算机专家,愿意
学习与计算机交互
在相当高的水平上,
非过程查询语言。示例
此类用户中有会计师,
工程师、建筑师和城市
规划者。”

是来自 Donald D. Chamberlin 和 Raymond F. Boyce 的 1974 年论文 介绍了 SQL(然后是 SEQUEL)。从他们的介绍来看,我猜他们是为非技术用户设计 SQL 的。

这并不是说 SQL是一种完美的语言,尽管经历了三十多年的变化,但它仍然存在缺陷。您有可能构建出更好的东西,但有两件事可能会出错:

  1. 您的语言可能会更糟。
  2. 如果您的非技术用户想要的话, 提高他们的关系数据技能,他们会受到你的语言与 SQL 的限制。有很多很多资源可以提高 SQL 技能,而提高新语言技能的方法却少得多。

也许可以考虑一个折衷方案:提供构建表达式的 UI。技术水平较低的用户可以选择一个属性、一个运算符,然后输入将出现在表达式文本区域中的值,他们可以逐个构建中等复杂的 SQL 表达式,而无需记住 SQL 语法。更熟练的用户可以直接编辑 SQL。

这并不能解决你所有的问题。您仍然需要一种方法来限制 SQL,以便用户不会损坏您的数据库或通过令人难以置信的笛卡尔连接无意中耗尽所有数据库资源。

"However, there is also a large class
of users who, while they are not
computer specialists, would be willing
to learn to interact with a computer
in a reasonably high-level,
non-procedural query language. Examples
of such users are accountants,
engineers, architects, and urban
planners."

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:

  1. Your language could be worse.
  2. If one of your non-technical users wanted to beef up their relational data skills, they would be limited by your language versus SQL. There are many, many resources to improve SQL skills and far fewer ways to improve skills in a new language.

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.

庆幸我还是我 2024-10-13 09:21:34

我建议您研究 Selection Factory 模式。

I suggest you look into the Selection Factory pattern.

小情绪 2024-10-13 09:21:34

我不知道这是否足够,但是有一个用于类似实体语言(域模型)的 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 ;)

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