From b47630cf6155fe19c77af0f09af8c6a9af7bf765 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Tue, 12 Apr 2022 12:48:05 -0400 Subject: [PATCH] include files from iter branch; need to be reviewed --- setup_sql/clone.sql | 111 ++++++++++++++++++++++++++++++++ setup_sql/clone_meta.sql | 31 +++++++++ setup_sql/gen_clone.sql | 49 ++++++++++++++ setup_sql/generate_clone_sql.sh | 4 ++ 4 files changed, 195 insertions(+) create mode 100644 setup_sql/clone.sql create mode 100644 setup_sql/clone_meta.sql create mode 100644 setup_sql/gen_clone.sql create mode 100755 setup_sql/generate_clone_sql.sh diff --git a/setup_sql/clone.sql b/setup_sql/clone.sql new file mode 100644 index 0000000..82fbf5f --- /dev/null +++ b/setup_sql/clone.sql @@ -0,0 +1,111 @@ +CREATE TABLE IF NOT EXISTS fc.live AS ( +SELECT +o."ddord#" + ,o."dditm#" + ,o."fgbol#" + ,o."fgent#" + ,o."diinv#" + ,o."dilin#" + ,o.quoten + ,o.quotel + ,o.dcodat + ,o.ddqdat + ,o.dcmdat + ,o.fesdat + ,o.dhidat + ,o.fesind + ,o.dhpost + ,o.fspr + ,o.ddqtoi + ,o.ddqtsi + ,o.fgqshp + ,o.diqtsh + ,o.diext + ,o.ditdis + ,o.discj + ,o.dhincr + ,o.plnt + ,o.promo + ,o.return_reas + ,o.terms + ,o.custpo + ,o.remit_to + ,o.bill_class + ,o.bill_cust + ,o.bill_rep + ,o.bill_terr + ,o.ship_class + ,o.ship_cust + ,o.ship_rep + ,o.ship_terr + ,o.dsm + ,o.account + ,o.shipgrp + ,o.geo + ,o.chan + ,o.chansub + ,o.orig_ctry + ,o.orig_prov + ,o.orig_post + ,o.bill_ctry + ,o.bill_prov + ,o.bill_post + ,o.dest_ctry + ,o.dest_prov + ,o.dest_post + ,o.part + ,o.styc + ,o.colc + ,o.colgrp + ,o.coltier + ,o.colstat + ,o.sizc + ,o.pckg + ,o.kit + ,o.brnd + ,o.majg + ,o.ming + ,o.majs + ,o.mins + ,o.gldco + ,o.gldc + ,o.glec + ,o.harm + ,o.clss + ,o.brand + ,o.assc + ,o.ddunit + ,o.unti + ,o.lbs + ,o.plt + ,o.plcd + ,o.fs_line + ,o.r_currency + ,o.r_rate + ,o.c_currency + ,o.c_rate + ,o.fb_qty + ,o.fb_val_loc + ,o.fb_val_loc_dis + ,o.fb_val_loc_qt + ,o.fb_val_loc_pl + ,o.fb_val_loc_tar + ,o.fb_cst_loc + ,o.fb_cst_loc_cur + ,o.fb_cst_loc_fut + ,o.calc_status + ,o.flag + ,o.odate + ,o.oseas + ,o.rdate + ,o.rseas + ,o.sdate + ,o.sseas + ,o.version + ,o.iter + ,null::text AS _comment + ,null::integer AS _logid + ,null::text AS _tag +FROM + rlarp.osm_dev o +) WITH DATA; diff --git a/setup_sql/clone_meta.sql b/setup_sql/clone_meta.sql new file mode 100644 index 0000000..2b42e79 --- /dev/null +++ b/setup_sql/clone_meta.sql @@ -0,0 +1,31 @@ +BEGIN; +INSERT INTO + fc.target_meta +SELECT + --hard-coded name of new clone table + 'fc.live' tname + ,cname + ,opos + ,func + ,fkey + ,pretty + ,dtype + ,mastcol + ,appcol + ,dateref +FROM + fc.target_meta +WHERE + --hard-coded original sales data with mapped fields + tname = 'rlarp.osm_dev' +ON CONFLICT ON CONSTRAINT target_meta_pk DO UPDATE SET + func = EXCLUDED.func + ,pretty = EXCLUDED.pretty + ,mastcol = EXCLUDED.mastcol + ,appcol = EXCLUDED.appcol + ,dateref = EXCLUDED.dateref + ,fkey = EXCLUDED.fkey; +--SELECT * FROM fc.target_meta WHERE tname = 'fc.live'; +--ROLLBACK; +COMMIT; +END; diff --git a/setup_sql/gen_clone.sql b/setup_sql/gen_clone.sql new file mode 100644 index 0000000..346d5d0 --- /dev/null +++ b/setup_sql/gen_clone.sql @@ -0,0 +1,49 @@ +DO +$func$ +DECLARE + _clist text; + _targ text; + _sql text; + +BEGIN +-----------------------------this target would be replaced with a parameter-------------- +SELECT + 'rlarp.osm_dev o' +INTO + _targ; + +-------------------------------build a column list----------------------------------------- +-----------a list of required columns is in fc.appcols, if they are not present------------ +-----------they will have to build included------------------------------------------------ +SELECT + string_agg( + --if the colum name is empty that means we are dealig with a required appcol + --that isn't present: use the appcol.col for the name preceded by underscore + CASE WHEN m.cname IS NULL + THEN COALESCE(a.dflt,'null::'||a.dtype)||' AS _'||a.col + ELSE 'o.'||format('%I',COALESCE(cname,'')) + END + ,E'\n ,' ORDER BY opos ASC) +INTO + _clist +FROM + fc.target_meta m + FULL OUTER JOIN fc.appcols a ON + m.appcol = a.col + AND m.dtype = a.dtype +WHERE + tname = _targ; + +_sql:= $$CREATE TABLE IF NOT EXISTS fc.live AS ( +SELECT +$$||_clist||$$ +FROM + $$||_targ||$$ +) WITH DATA;$$; + +--RAISE NOTICE '%', _sql; + +INSERT INTO fc.sql SELECT 'live', _sql ON CONFLICT ON CONSTRAINT sql_pkey DO UPDATE SET t = EXCLUDED.t; + +END; +$func$ diff --git a/setup_sql/generate_clone_sql.sh b/setup_sql/generate_clone_sql.sh new file mode 100755 index 0000000..89af4ad --- /dev/null +++ b/setup_sql/generate_clone_sql.sh @@ -0,0 +1,4 @@ +# execure the sql for scale which builds the sql and inserts into a table +$PGD -f gen_clone.sql +# pull the sql out of the table and write it to route directory +$PGD -c "SELECT t FROM fc.sql WHERE cmd = 'clone'" -t -A -o clone.sql