update for new columns

This commit is contained in:
Paul Trowbridge 2018-09-20 22:13:06 -04:00
parent f96a540210
commit 9f1d191b9e
3 changed files with 26 additions and 17 deletions

View File

@ -81,7 +81,7 @@ COMMENT ON COLUMN evt.bal.debits IS 'total debits';
COMMENT ON COLUMN evt.bal.credits IS 'total credits'; COMMENT ON COLUMN evt.bal.credits IS 'total credits';
COMMENT ON COLUMN evt.bal.cbal IS 'closing balance'; COMMENT ON COLUMN evt.bal.cbal IS 'closing balance';
-----------------------------------------process bpr insert trigger ----------------------------------- ---------------------------handle new logged event----------------------------------------
CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
@ -89,7 +89,7 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
$func$ $func$
BEGIN BEGIN
WITH WITH
------------------------------------full extraction------------------------------------------- --full extraction
full_ex AS ( full_ex AS (
SELECT SELECT
ins.id ins.id
@ -113,7 +113,7 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(ins.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 --select * from full_ex
--------------------------------re-ggregate extraction to gl line level---------------------- --re-ggregate extraction to gl line level
,ex_gl_line AS ( ,ex_gl_line AS (
SELECT SELECT
id id
@ -142,15 +142,19 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
RETURNING * RETURNING *
) )
INSERT INTO INSERT INTO
evt.gl (bprid,acct, amount,glline, bprkeys) evt.gl (bprid,acct, amount,tstmp , fspr, glline, bprkeys)
SELECT SELECT
id id
,account ,account
,amount ,amount
,(bprkeys->>'date')::timestamptz
,p.fspr
,gl_rownum ,gl_rownum
,bprkeys ,bprkeys
FROM FROM
ex_gl_line; ex_gl_line
LEFT OUTER JOIN evt.fspr p ON
p.dur @> (bprkeys->>'date')::timestamptz;
RETURN NULL; RETURN NULL;
END; END;
$func$; $func$;
@ -162,5 +166,4 @@ CREATE TRIGGER log_insert
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE PROCEDURE evt.log_insert(); EXECUTE PROCEDURE evt.log_insert();
COMMIT; COMMIT;

View File

@ -1,3 +1,4 @@
---------------------------handle new logged event----------------------------------------
CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
@ -5,7 +6,7 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
$func$ $func$
BEGIN BEGIN
WITH WITH
------------------------------------full extraction------------------------------------------- --full extraction
full_ex AS ( full_ex AS (
SELECT SELECT
ins.id ins.id
@ -29,7 +30,7 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(ins.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 --select * from full_ex
--------------------------------re-ggregate extraction to gl line level---------------------- --re-ggregate extraction to gl line level
,ex_gl_line AS ( ,ex_gl_line AS (
SELECT SELECT
id id
@ -58,18 +59,22 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
RETURNING * RETURNING *
) )
INSERT INTO INSERT INTO
evt.gl (bprid,acct, amount,glline, bprkeys) evt.gl (bprid,acct, amount,tstmp , fspr, glline, bprkeys)
SELECT SELECT
id e.id
,account ,e.account
,amount ,e.amount
,gl_rownum ,(e.bprkeys->>'date')::timestamptz
,bprkeys ,p.id
,e.gl_rownum
,e.bprkeys
FROM FROM
ex_gl_line; ex_gl_line e
LEFT OUTER JOIN evt.fspr p ON
p.dur @> (bprkeys->>'date')::timestamptz;
RETURN NULL; RETURN NULL;
END; END;
$func$ $func$;
CREATE TRIGGER log_insert CREATE TRIGGER log_insert

3
test
View File

@ -35,6 +35,7 @@ $${
"entity": "home", "entity": "home",
"module": "MHI", "module": "MHI",
"offset": "h.dcard", "offset": "h.dcard",
"transaction": "purchase" "transaction": "purchase",
"date": "2018-08-01"
} }
}$$::jsonb bpr }$$::jsonb bpr