SQL条件的Json表示[英] Json representation of SQL conditions

本文是小编为大家收集整理的关于SQL条件的Json表示的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否有任何方法将SQL条件转换为JSON?我的意思是,我需要写这样的东西:

x > 5 and (y like '%b%' or z > 5) and b = true

作为JSON?

推荐答案

您可以使用 jOOQ and gson 为此.这是一个可以如何工作的快速而肮脏的例子:

new Gson()
    .newBuilder()
    .setPrettyPrinting()
    // Some jOOQ types can be serialised out of the box
    // For others, you might have to register adapters explicitly:
    .registerTypeHierarchyAdapter(
       Field.class, 
       (JsonSerializer<Field<?>>) (s, t, ctx) -> new JsonPrimitive(s.getName()))
    .create()
    .toJson(DSL.using(SQLDialect.DEFAULT)
       .parser()
       .parseCondition("x > 5 and (y like '%b%' or z > 5) and b = true"), 
       System.out
    );

以上印刷:

{
  "operator": "AND",
  "conditions": [
    {
      "field1": "X",
      "field2": "5",
      "comparator": "GREATER"
    },
    {
      "operator": "OR",
      "conditions": [
        {
          "field1": "Y",
          "field2": "%b%",
          "comparator": "LIKE"
        },
        {
          "field1": "Z",
          "field2": "5",
          "comparator": "GREATER"
        }
      ]
    },
    {
      "field1": "B",
      "field2": "true",
      "comparator": "EQUALS"
    }
  ]
}

当然,这不是很兼容,因为它使用反射来访问JOOQ的内部设备.您的JSON对象的名称(例如operator,conditions)可能随时更改,但它可能仍然足够好.

未来的JOOQ版本可能会提供其表达树的更稳定的JSON出口: https://github.com/jooq/jooq/jooq/sissues/9628 >

免责声明:我在Jooq背后的公司工作.

其他推荐答案

最近我找到并使用了JSONLOGIC.这也是很好的方法. https://jsonlogic.com/

本文地址:https://itbaoku.cn/post/2513517.html

问题描述

Is there any method to convert a SQL condition to Json? I mean, I need to write something like this:

x > 5 and (y like '%b%' or z > 5) and b = true

as Json?

推荐答案

You could use jOOQ and Gson for that. Here's a quick and dirty example of how that could work:

new Gson()
    .newBuilder()
    .setPrettyPrinting()
    // Some jOOQ types can be serialised out of the box
    // For others, you might have to register adapters explicitly:
    .registerTypeHierarchyAdapter(
       Field.class, 
       (JsonSerializer<Field<?>>) (s, t, ctx) -> new JsonPrimitive(s.getName()))
    .create()
    .toJson(DSL.using(SQLDialect.DEFAULT)
       .parser()
       .parseCondition("x > 5 and (y like '%b%' or z > 5) and b = true"), 
       System.out
    );

The above prints:

{
  "operator": "AND",
  "conditions": [
    {
      "field1": "X",
      "field2": "5",
      "comparator": "GREATER"
    },
    {
      "operator": "OR",
      "conditions": [
        {
          "field1": "Y",
          "field2": "%b%",
          "comparator": "LIKE"
        },
        {
          "field1": "Z",
          "field2": "5",
          "comparator": "GREATER"
        }
      ]
    },
    {
      "field1": "B",
      "field2": "true",
      "comparator": "EQUALS"
    }
  ]
}

Of course, this is not very forward compatible, because it uses reflection to access jOOQ's internals. The names of your JSON objects (e.g. operator, conditions) might change at any time, but it may still be good enough for you.

A future jOOQ version might offer more stable JSON exports of its expression tree: https://github.com/jOOQ/jOOQ/issues/9628

Disclaimer: I work for the company behind jOOQ.

其他推荐答案

Recently I found and used JsonLogic. It is very good approach as well. https://jsonlogic.com/