JPQL 合并非标量?

发布于 2024-11-16 15:29:16 字数 1930 浏览 2 评论 0原文

我有一个存储应用程序配置参数的表,如下所示:

 setName | key        | value
----------------------------------
 dev     | FooEnabled | true
 dev     | BarEnabled | true
 qa      | FooEnabled | false
 ...etc...

每个参数(键/值组合)都属于特定的“配置集”。选择特定集合中的所有属性很容易:

SELECT p Parameter p WHERE p.setName = :name

因此,如果我想要名为 "dev" 的配置集中的所有参数,我会返回

 dev | FooEnabled | true
 dev | BarEnabled | true

现在我想选择两个集合的并集:

SELECT p Parameter p WHERE p.setName = :name1 OR p.setName = :name2

...但通过为name1集中的属性赋予比name2集中的属性更高的优先级来消除具有重复键的行。如果 name1"qa" 并且 name2"dev",那么这将返回

 dev | FooEnabled | true
 qa  | BarEnabled | false

为了澄清,查询应该选择:

  • 名称为 name2 的集合中的每个属性,除非...
  • 在名称为 name1 的集合中存在具有相同键的属性,在这种情况下...
  • 选择带有 setName = :name1 的行而不是行与 setName = :name2

什么 JPQL 查询可以有效地完成此操作? 如果 COALESCE 使用非标量值,那么我想像这样的事情可以工作:

SELECT
    COALESCE(x, y)
    FROM Property x, Property y
    WHERE
        x.setName = :name1 AND
        y.setName = :name2 AND
        x.key = y.key

但是COALESCE只接受标量值。有什么想法吗?


其他查询尝试/失败:

SELECT
    CASE
        WHEN (NOT EXISTS (SELECT y FROM Property y WHERE y.setName = :name1 AND y.key = x.key))
        THEN x
        ELSE y
    END
    FROM Property x
    WHERE
        x.setName = :name2

我也有兴趣以相同的方式通过 key 选择单独的行,我想它看起来像下面这样(如果只有 COALESCE 适用于非标量):

SELECT
    COALESCE(y, x)
    FROM Property x, Property y
    WHERE
        x.key = :key AND
        x.setName = :name1 AND
        y.key = :key AND
        y.setName = :name2

I have a table storing application configuration parameters like so:

 setName | key        | value
----------------------------------
 dev     | FooEnabled | true
 dev     | BarEnabled | true
 qa      | FooEnabled | false
 ...etc...

Each parameter, a key/value combination, belongs to a particular "configuration set." It's easy to select all properties in a particular set:

SELECT p Parameter p WHERE p.setName = :name

So if I wanted all the parameters in config set named "dev" I'd get back

 dev | FooEnabled | true
 dev | BarEnabled | true

Now I'd like to select the union of two sets:

SELECT p Parameter p WHERE p.setName = :name1 OR p.setName = :name2

...but eliminate rows with duplicate keys by giving properties in set with name1 higher precedence than properties in set with name2. If name1 was "qa" and name2 was "dev", then this would return

 dev | FooEnabled | true
 qa  | BarEnabled | false

To clarify, the query should SELECT:

  • Every property from set with name name2, except when...
  • There is a property with the same key in set with name name1, in which case...
  • Select the row with setName = :name1 instead of the row with setName = :name2

What JPQL query will do this efficiently? If COALESCE worked with non-scalar values, then I imagine that something like this could work:

SELECT
    COALESCE(x, y)
    FROM Property x, Property y
    WHERE
        x.setName = :name1 AND
        y.setName = :name2 AND
        x.key = y.key

but COALESCE only accepts scalar values. Any ideas?


Other queries attempted/failed:

SELECT
    CASE
        WHEN (NOT EXISTS (SELECT y FROM Property y WHERE y.setName = :name1 AND y.key = x.key))
        THEN x
        ELSE y
    END
    FROM Property x
    WHERE
        x.setName = :name2

I'm also interested in selecting individual rows in the same manner, by key, which I suppose would look like the following (if only COALESCE worked for non-scalars):

SELECT
    COALESCE(y, x)
    FROM Property x, Property y
    WHERE
        x.key = :key AND
        x.setName = :name1 AND
        y.key = :key AND
        y.setName = :name2

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

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

发布评论

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

评论(1

美人迟暮 2024-11-23 15:29:16

也许您可以创建一个基本上具有以下结构的查询:(

SELECT
  Property p,
  CASE WHEN setName=:name1 
       THEN 1
       ELSE 0
  as orderColumn
FROM Property x
WHERE x.setName = :name1 OR (x.setName =:name2 AND (NOT EXISTS (SELECT y FROM Property y WHERE y.setName = :name1 AND y.key = x.key)))
ORDER BY orderColumn

仅是我的想法,没有尝试过)

Possibly you can create a query with basically following structure:

SELECT
  Property p,
  CASE WHEN setName=:name1 
       THEN 1
       ELSE 0
  as orderColumn
FROM Property x
WHERE x.setName = :name1 OR (x.setName =:name2 AND (NOT EXISTS (SELECT y FROM Property y WHERE y.setName = :name1 AND y.key = x.key)))
ORDER BY orderColumn

(Just from my mind without having it tried)

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