简体   繁体   English

Postgres jsonb 查询动态值

[英]Postgres jsonb query for dynamic values

In the users table I have a jsob column experience with following json structure:在用户表中,我有一个具有以下 json 结构的 jsob 列experience

[
    {
        "field": "devops",
        "years": 9
    },
    {
        "field": "backend dev",
        "years": 7
    } 
... // could be N number of objects with different values
]

Business requirement业务需求

Client can request for people with experience in any field and with their respective years experience in each客户可以要求在任何领域都有经验的人,并且在每个领域都有各自的年经验

This is an example query这是一个示例查询

SELECT * FROM users
WHERE
jsonb_path_exists(experience, '$[*] ? (@.field == "devops" && @.years > 5)') and
jsonb_path_exists(experience, '$[*] ? (@.field == "backend dev" && @.years > 5)')
LIMIT 3;

Issue问题

Lets say if I get a request for让我们说如果我收到请求

[
  { field: "devops", years: 5 }, 
  { field: "java", years: 6 }, 
  { field: "ui/ux", years: 2 }] // and so on

How do I dynamically create a query without worrying about sql injection?如何动态创建查询而不用担心 sql 注入?

Techstack技术栈

  • Nodejs节点
  • Typescript打字稿
  • TypeORM类型ORM
  • Postgres Postgres

This query is fit for parameterizing.此查询适合参数化。 The parameter in the query below is $1 .下面查询中的参数是$1 You may need to change its syntax depending on the flavour of your environment.您可能需要根据环境的风格更改其语法。

select * from 
(
  select u.*,
    (
      select count(*)
      from jsonb_array_elements(u.experience::jsonb) ej -- jsonb list of experiences 
      inner join jsonb_array_elements($1::jsonb) rj     -- jsonb list of request items
        on ej ->> 'field' =  rj ->> 'field'
        and (ej ->> 'years')::numeric >= (rj ->> 'years')::numeric
    ) cnt
 from users u
) t
where t.cnt = jsonb_array_length($1::jsonb);

Index指数

First of all, you want index support .首先,您需要索引支持 I suggest a jsonb_path_ops index like:我建议使用jsonb_path_ops索引,例如:

CREATE INDEX users_experience_gin_idx ON users USING gin (experience jsonb_path_ops);

See:看:

Query询问

And a query that can tap into that index (100 % equivalent to your original):以及可以利用该索引的查询(100% 相当于您的原始索引):

SELECT *
FROM   users
WHERE  experience @? '$[*] ? (@.field == "devops" && @.years > 5 )'
AND    experience @? '$[*] ? (@.field == "backend dev" && @.years > 5)'
LIMIT  3;

Requires Postgres 12 or later, where the SQL/JSON path language was added.需要Postgres 12或更高版本,其中添加了 SQL/JSON 路径语言。

Index support is bound to operators in Postgres.索引支持绑定到 Postgres 中的运算符 The operator @? 运算符@? is the equivalent of jsonb_path_exists() .相当于jsonb_path_exists() See:看:

Generate query dynamically动态生成查询

SELECT 'SELECT * FROM users
WHERE  experience @? '
       || string_agg(quote_nullable(format('$[*] ? (@.field == %s && @.years > %s)'
                                         , f->'field'
                                         , f->'years')) || '::jsonpath'
                   , E'\nAND    experience @? ')
       || E'\nLIMIT  3'
FROM   jsonb_array_elements('[{"field": "devops", "years": 5 }, 
                              {"field": "java", "years": 6 }, 
                              {"field": "ui/ux", "years": 2 }]') f;

Generates a query of the above form:生成上述形式的查询:

SELECT * FROM users
WHERE  experience @? '$[*] ? (@.field == "devops" && @.years > 5)'::jsonpath
AND    experience @? '$[*] ? (@.field == "java" && @.years > 6)'::jsonpath
AND    experience @? '$[*] ? (@.field == "ui/ux" && @.years > 2)'::jsonpath
LIMIT  3;

Full automation全自动化

How do I dynamically create a query without worrying about sql injection?如何动态创建查询而不用担心 sql 注入?

Put above query generation into a PL/pgSQL function to execute dynamically:将上面的查询生成放到一个 PL/pgSQL 函数中动态执行:

CREATE OR REPLACE FUNCTION f_users_with_experience(_filter_arr jsonb, _limit int = 3)
  RETURNS SETOF users
  LANGUAGE plpgsql PARALLEL SAFE STABLE STRICT AS
$func$
DECLARE
   _sql text;
BEGIN
   -- assert (you may want to be stricter?)
   IF jsonb_path_exists (_filter_arr, '$[*] ? (!exists(@.field) || !exists(@.years))') THEN
      RAISE EXCEPTION 'Parameter $2 (_filter_arr) must be a JSON array with keys "field" and "years" in every object. Invalid input was: >>%<<', _filter_arr;
   END IF;

   -- generate query string
   SELECT INTO _sql
'SELECT * FROM users
WHERE  experience @? '
       || string_agg(quote_nullable(format('$[*] ? (@.field == %s && @.years > %s)'
                                         , f->'field'
                                         , f->'years'))
                   , E'\nAND    experience @? ')
       || E'\nLIMIT   ' || _limit
   FROM   jsonb_array_elements(_filter_arr) f;

   -- execute
   IF _sql IS NULL THEN
      RAISE EXCEPTION 'SQL statement is NULL. Should not occur!';
   ELSE
   -- RAISE NOTICE '%', _sql;     -- debug first if in doubt
      RETURN QUERY EXECUTE _sql;
   END IF;
END
$func$;

Call:称呼:

SELECT * FROM f_users_with_experience('[{"field": "devops", "years": 5 }, 
                                      , {"field": "backend dev", "years": 6}]');

Or with a different LIMIT :或者使用不同的LIMIT

SELECT * FROM f_users_with_experience('[{"field": "devops", "years": 5 }]', 123);

db<>fiddle here db<> 在这里摆弄

You should be comfortable with PL/pgSQL to work with this and understand it.您应该熟悉 PL/pgSQL 来使用它并理解它。

SQL injection is impossible because ... SQL注入是不可能的,因为......

  1. valid JSON input is enforced强制执行有效的 JSON 输入
  2. JSON values are concatenated with original JSON double-quotes. JSON 值与原始 JSON 双引号连接。
  3. Most importantly, each generated jsonpath value is single-quoted with quote_nullable() .最重要的是,每个生成的jsonpath值都用quote_nullable()单引号。

While being at the topic of SQL/JSON path expressions I use one to assert valid input:在讨论 SQL/JSON 路径表达式的主题时,我使用一个来断言有效输入:

jsonb_path_exists (_filter_arr, '$[*] ? (!exists(@.field) || !exists(@.years))')

Checks every object in the JSON array and whether one of the two required keys ( field , years ) is missing.检查 JSON 数组中的每个对象,以及是否缺少两个必需的键( fieldyears )之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM