Loading...
Searching...
No Matches
mpe_tables_postedit.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Post Edit Hook script for the MPE_TABLES table
4 @details Post edit hooks provide additional backend validation for user
5 provided data. The incoming dataset is named `work.staging_ds` and is
6 provided in mpe_loader.sas.
7
8 Available macro variables:
9 @li DC_LIBREF - The DC control library
10 @li LIBREF - The library of the dataset being edited (is assigned)
11 @li DS - The dataset being edited
12
13 This validation checks MPE_TABLES to ensure modified / added records are
14 valid. If a non-default AUDIT_LIBDS is being used, there is also a check
15 to ensure that this table already exists.
16
17 Hook scripts run inside the service that includes them, so the macros
18 they call are already in that session. They are listed as ignored rather
19 than declared, so the script keeps no macro list of its own to maintain.
20
21 @sasjslint {"ignoredMacros": ["mp_abort", "dc_assignlib"], "maxLineLength": 81}
22
23**/
24
25%let errmsg=;
26%let errflag=0;
27
28/* ensure uppercasing */
29data work.staging_ds;
30 set work.staging_ds;
31 /* PK fields should not be upcased if we are trying to delete records */
32 if upcase(_____DELETE__THIS__RECORD_____) ne "YES" then do;
33 LIBREF=upcase(LIBREF);
34 DSN=upcase(DSN);
35 end;
36 loadtype=upcase(loadtype);
37 buskey=upcase(buskey);
38 var_txfrom=upcase(var_txfrom);
39 var_txto=upcase(var_txto);
40 var_busfrom=upcase(var_busfrom);
41 var_busto=upcase(var_busto);
42 var_processed=upcase(var_processed);
43 close_vars=upcase(close_vars);
44 audit_libds=upcase(audit_libds);
45 rk_underlying=upcase(rk_underlying);
46
47 /* do not accept certain librefs */
48 if LIBREF in ('WORK','CASUSER','SASUSER')
49 then do;
50 call symputx('errmsg',"Invalid LIBREF: "!!LIBREF);
51 call symputx('errflag',1);
52 end;
53
54 /* check for valid loadtype */
55 if LOADTYPE not in ('UPDATE','TXTEMPORAL','FORMAT_CAT','BITEMPORAL','REPLACE')
56 then do;
57 call symputx('errmsg',"Invalid LOADTYPE: "!!LOADTYPE);
58 call symputx('errflag',1);
59 end;
60
61 /* force correct BUSKEY and DSN when loading format catalogs */
62 if LOADTYPE='FORMAT_CAT' then do;
63 BUSKEY='TYPE FMTNAME FMTROW';
64 DSN=scan(dsn,1,'-')!!'-FC';
65 end;
66
67 /* convert tabs into spaces */
68 buskey=translate(buskey," ","09"x);
69 rk_underlying=translate(rk_underlying," ","09"x);
70run;
71
72%mp_abort(iftrue=(&errflag=1)
73 ,mac=mpe_tables_postedit
74 ,msg=%superq(errmsg)
75)
76
77/* REPLACE loadtype is incompatible with row level EDIT security (#211) */
78proc sql noprint;
79create table work.badloadtypes as
80 select distinct cats(upcase(a.libref),'.',upcase(a.dsn)) as libds
81 from work.staging_ds(where=(loadtype='REPLACE'
82 and upcase(_____DELETE__THIS__RECORD_____) ne 'YES')) a
83 inner join &dc_libref..mpe_row_level_security(where=(
84 rls_active=1 and &dc_dttmtfmt. lt tx_to
85 and upcase(rls_scope) in ('EDIT','ALL'))) b
86 on upcase(a.libref)=upcase(b.rls_libref)
87 and upcase(a.dsn)=upcase(b.rls_table);
88
89%let badloadtypes=0;
90%let badloadtype=;
91data _null_;
92 set work.badloadtypes;
93 call symputx('badloadtypes',_n_);
94 call symputx('badloadtype',libds);
95run;
96
97%mp_abort(iftrue=(&badloadtypes ne 0)
98 ,mac=mpe_tables_postedit
99 ,msg=%str(REPLACE loadtype cannot be applied to table(s) with row level%trim(
100 ) EDIT security, eg: &badloadtype. Remove the security rule(s) or change%trim(
101 ) the loadtype.)
102)
103
104/* get distinct list of audit libs */
105proc sql;
106create table work.liblist as
107 select distinct audit_libds
108 from work.staging_ds
109 where audit_libds not in ('','0', "&dc_libref..MPE_AUDIT")
110 and upcase(_____DELETE__THIS__RECORD_____) ne "YES";
111
112/* assign the libs */
113data _null_;
114 set work.liblist;
115 call symputx(cats('lib',_n_),audit_libds);
116 libref=scan(audit_libds,1,'.');
117 call execute('%dc_assignlib(WRITE,'!!libref!!')');
118run;
119
120/* check the audit tables exist */
121data _null_;
122 set work.liblist;
123
124 if exist(audit_libds,"DATA")=0 then do;
125 call symputx('errmsg',
126 "Audit Table "!!audit_libds!!" does not exist, or could not be assigned."
127 );
128 call symputx('errflag',1);
129 end;
130run;
131
132%mp_abort(iftrue=(&errflag=1)
133 ,mac=mpe_tables_postedit
134 ,msg=%superq(errmsg)
135)
136