在另一个对象Postgresql 9.6的现有jsonarray元素中添加节点/对象

时间:2018-09-03 15:22:56

标签: postgresql-9.5 postgresql-9.6

我有一个格式为json的现有

{
    "createdDate": "2017-06-29 07:32:18",
    "name": "Messi",    
    "lastName": "Tester",
    "addressDetails": {},    
    "products": [
        {
            "lasttimeOfResourceScreenVisit": "2017-08-20T20:29:20.681+05:30",
            "readArticles": [],
            "preferences": {
                "hasLoggedIntoProgramForFirstTime": false,
                "webLatestViewedProductLabelVersion": "",
                "progressTrackingStartDate": 1502443894189
            },
            "vcProductId": "a1G0S000000zvVq"            
        },
        {
            "lasttimeOfResourceScreenVisit": "2017-08-20T20:29:20.681+05:30",
            "readArticles": [],
            "preferences": {
                "hasLoggedIntoProgramForFirstTime": false,
                "webLatestViewedProductLabelVersion": "",
                "progressTrackingStartDate": 1502443894189
            },
            "vcProductId": "tala1G0S000000zvVqdkuylfguib9ytz",           
        }
    ],
    "lastModifiedDate": "2017-08-07 06:16:53",
    "patientId" : "1122334"
}

我必须在产品数组中添加一个新节点

"primaryNumber" : {
   "phoneNumber" : "999876997900",
   "consentVersion" : "99999"
}

根据csv文件中的产品和患者添加此节点。 我创建了以下函数来做到这一点,但是我无法安静地更新产品数组中的特定索引。

我到目前为止有什么

CREATE OR REPLACE FUNCTION add_primaryNumber_product_node() RETURNS
VOID AS $$
DECLARE
patient_profile record;
 exception_message TEXT;
 patient_details json;
 products json;
 product json;
 primaryNumber jsonb;
 count SMALLINT;
 BEGIN
     --copy primary number for product and patient in a temporary table from csv file
    create table primaryNumber_temp(pcpPatientId varchar, vcProductId varchar, primaryNumber jsonb);
    copy primaryNumber_temp(pcpPatientId, vcProductId, primaryNumber) from '\path\PrimaryNumber.csv' delimiter ',' escape '"' csv header;

    FOR patient_profile IN SELECT * from patientprofile
    LOOP
        patient_details:=patient_profile.jdata;
        IF(patient_details->'products' IS NULL) THEN
            EXIT;
        END IF;
        products:=patient_details->>'products';
        count := 0;
        FOR product IN SELECT * FROM json_array_elements(products)
        LOOP
            IF(product->'primaryNumber' IS NULL)
            THEN
            SELECT primaryNumber_temp.primaryNumber into primaryNumber from primaryNumber_temp where vcProductId = product->>'vcProductId' and pcpPatientId = patient_details->>'pcpPatientId';
            raise notice 'Value primarynumber: %', primaryNumber;
                IF(primaryNumber is NOT NULL)
                THEN
                 raise notice 'Value index: %', count;
                product := product || '{"primaryNumber": {}}';
                raise notice 'value of product: %',product;
--                  UPDATE patientprofile SET jdata =jsonb_set(jdata,'{products}',jdata->'products'->count ||'{"primaryNumber": {}}') where id=1;

                    UPDATE patientprofile SET jdata =jsonb_set(jdata,'{products}', jdata->'products' || (jdata->'products'->0 || '{"primaryNumber": {}}')::jsonb , true)  where id=21;
                    UPDATE patientprofile SET jdata = jdata #- ('{products,0} ') where id=21;
--                      where patientprofile.id=patient_profile.id AND patientprofile.jdata->'products'->>'vcProductId' = product->>'vcProductId';

                    UPDATE patientprofile SET jdata =jsonb_set(jdata,'{products,primaryNumber}', ('{"phoneNumber":"917842672492"}')::jsonb , false)  where id = 21 and jdata->'products'->1->'primaryNumber' IS NOT NULL;
--                      where patientprofile.id=patient_profile.id AND patientprofile.jdata->'products'->>'vcProductId' = product->>'vcProductId';
                END IF;
            END IF;
            count := count + 1;
        END LOOP;
    END LOOP;
    DROP TABLE primaryNumber_temp;
    EXCEPTION WHEN OTHERS THEN
    GET STACKED DIAGNOSTICS exception_message := MESSAGE_TEXT;
    RAISE NOTICE 'Exception occurred in database function while adding primary number node patient profile json, exception: %', exception_message;
    END;
$$ LANGUAGE PLPGSQL;
select add_primaryNumber_product_node();

从csv复制到表工作正常,它只是我坚持的更新 有什么想法..

0 个答案:

没有答案