PostgreSQL函数与jsonb一起使用-解析问题

时间:2018-07-10 14:53:34

标签: postgresql

我需要将此查询转换为函数`

WITH pick AS (
                            SELECT odm.orderid, odm.partnerid, odm.magnitude, no.properties
                            FROM test.order_partner_magnitude odm
                            INNER JOIN test.new_orders no on odm.orderid = no.orderid
                            LEFT JOIN test.order_partner od on odm.orderid = od.orderid
                            WHERE no.properties ->> 'partitionid' = '${partitionId}'
                            AND od.partnerid ISNULL
                            AND no.properties ->> 'statusid' IN ( ${validStatusIDs})
                            ORDER BY odm.magnitude
                            LIMIT 1),Delete as (
                            DELETE
                                FROM test.order_partner_magnitude
                            WHERE orderid = (SELECT orderid FROM pick)
                            or partnerid = (SELECT partnerid FROM pick)
                            ),Insert as (
                                INSERT into test.order_partner(orderid,partnerid,magnitude) SELECT orderid,partnerid,magnitude FROM pick
                            )
                        UPDATE test.new_orders
                            SET properties = properties || '${orderProperties}'
                        WHERE orderid = (SELECT orderid FROM pick);

其中partitionId是文本类型(例如'6'),validStatusIDs是文本(例如'1','2'可以包含1个以上),orderProp是文本('{"statusid":3,"updatedAt":"2018-7-10 17:49:34"}')。我坚持了这个`

CREATE  OR REPLACE FUNCTION test.fetcher(partitionId text, validStatusIDs json, orderProperties text)
RETURNS  RECORD AS --for test
$$
DECLARE
  orderid integer;
  partnerid integer;
  magnitude float;
  ret RECORD;
BEGIN
  SELECT  odm.orderid,odm.partnerid, odm.magnitude  INTO ret
  FROM test.order_partner_magnitude odm
                            INNER JOIN test.new_orders no on odm.orderid = no.orderid
                            LEFT JOIN test.order_partner od on odm.orderid = od.orderid
                            WHERE no.properties ->> 'partitionid' = partitionId
                            AND od.partnerid ISNULL
                            AND no.properties ->> 'statusid' IN (validStatusIDs) --this where i cant do anything,with this i get this res (,,) but it's not correct
                            ORDER BY odm.magnitude
                            LIMIT 1;
  RETURN ret;
end;
$$
LANGUAGE 'plpgsql' IMMUTABLE;

查询必须像这样SELECT test.fetcher('6', ' '1', '2' ' ,'{"statusid":3,"updatedAt":"2018-7-10 17:49:34"}');

但是此' '1', '2' '无效

我该如何解决?

PS可以更改参数类型

0 个答案:

没有答案