first pass at insert trigger

This commit is contained in:
Paul Trowbridge 2018-09-17 23:38:53 -04:00
parent f47d13e815
commit 847cc0e87a

View File

@ -1,49 +1,112 @@
CREATE FUNCTION log_insert() RETURNS trigger CREATE FUNCTION log_insert() RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
AS AS
$func$ $func$
BEGIN BEGIN
WITH WITH
/*
ins AS (
SELECT
1 id
,$${
"gl": {
"lines": [
{
"amount": 2.19,
"account": "h.food"
},
{
"amount": -2.19,
"account": "h.dcard"
}
],
"jpath": [
[
"{item,0}",
"{header}"
],
[
"{item,0}",
"{header}"
]
]
},
"item": [
{
"item": "green olives",
"amount": 2.19,
"reason": "food",
"account": "h.food"
}
],
"header": {
"entity": "home",
"module": "MHI",
"offset": "h.dcard",
"transaction": "purchase"
}
}$$::jsonb bpr
),
*/
------------------------------------full extraction------------------------------------------- ------------------------------------full extraction-------------------------------------------
full_ex AS ( full_ex AS (
SELECT SELECT
ins.id ins.id
--th econtents of the gl line
,a.i gl_line ,a.i gl_line
,a.rn gl_row --the array position of the gl line
,NEW.bpr#>ARRAY['gl','jpath',(a.rn - 1)::text] gl_ref ,a.rn gl_rownum
--array of references
,ins.bpr#>ARRAY['gl','jpath',(a.rn - 1)::text] gl_ref
--each item in the reference array
,p.i ref_line ,p.i ref_line
,p.rn ref_row --array postition of the reference item
,NEW.bpr#>(p.i->>0)::text[] bpr_extract ,p.rn ref_rownum
--follow the path
,ins.bpr#>(p.i->>0)::text[] bpr_extract
FROM FROM
ins ins
--gl array hold each gl line --gl array hold each gl line
LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(NEW.bpr->'gl'->'lines') WITH ORDINALITY a(i, rn) ON TRUE LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(ins.bpr->'gl'->'lines') WITH ORDINALITY a(i, rn) ON TRUE
--for each --for each
LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(NEW.bpr#>ARRAY['gl','jpath',(a.rn - 1)::text]) WITH ORDINALITY p(i, rn) ON TRUE LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(ins.bpr#>ARRAY['gl','jpath',(a.rn - 1)::text]) WITH ORDINALITY p(i, rn) ON TRUE
) )
--select * from full_ex
--------------------------------re-ggregate extraction to gl line level---------------------- --------------------------------re-ggregate extraction to gl line level----------------------
,ex_gl_line ,ex_gl_line AS (
SELECT SELECT
id id
,gl_line->>'account' account ,gl_line->>'account' account
,(gl_line->>'amount')::numeric amount ,(gl_line->>'amount')::numeric amount
,gl_row ,gl_rownum
,gl_ref --aggregate all the path references back to the gl line
,public.jsonb_concat(bpr_extract) ref_extract ,public.jsonb_concat(bpr_extract) bprkeys
FROM FROM
full_ex full_ex
GROUP BY GROUP BY
id id
,gl_line ,gl_line
,gl_row ,gl_rownum
,gl_ref; )
--select * from ex_gl_line
INSERT INTO
evt.gl (bprid,account, amount,glline, bprkeys)
SELECT
id
,account
,amount
,gl_rownum
,bprkeys
FROM
ex_gl_line;
RETURN NULL; RETURN NULL;
END; END;
$func$ $func$
CREATE TRIGGER log_insert CREATE TRIGGER log_insert
AFTER INSERT ON log AFTER INSERT ON evt.log
REFERENCING NEW TABLE AS ins REFERENCING NEW TABLE AS ins
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE PROCEDURE log_insert(); EXECUTE PROCEDURE evt.log_insert();