Loading...
Searching...
No Matches
mpe_refreshtables.sas
Go to the documentation of this file.
1/**
2 @file mpe_refreshtables.sas
3 @brief Refreshes the data catalog
4 @details
5
6 @param [in] etlsource= (0) Defaults to %mf_getuniquename(prefix=refreshtables), identifies this load
7 in the MPE_DATALOADS audit row. That table is keyed on etlsource together
8 with processed_dttm, libref and dsn, so the uid keeps each load distinct.
9 Usage:
10
11 %mpe_refreshtables(sashelp)
12
13 <h4> SAS Macros </h4>
14 @li mf_getuniquename.sas
15 @li mp_abort.sas
16 @li bitemporal_dataloader.sas
17 @li mf_existfeature.sas
18 @li mf_getengine.sas
19 @li mf_getschema.sas
20 @li mp_getconstraints.sas
21
22 @version 9.3
23 @author 4GL Apps Ltd
24
25 @sasjslint {"maxLineLength": 102}
26
27**/
28
29%macro mpe_refreshtables(lib,ds=#all
30 ,etlsource=0
31);
32/* a default of 0 rather than a %mf_uid() expression: a default is
33 resolved when the macro is compiled, so it would be one value for the
34 whole session and the audit folder below it would collide on the
35 second call - as it did between the refresh steps of one run */
36%if "&etlsource"="0" %then %let etlsource=%mf_getuniquename(prefix=refreshtables);
37%let lib=%upcase(&lib);
38%let ds=%upcase(&ds);
39%local engine; %let engine=%mf_getengine(&lib);
40%local schema; %let schema=%mf_getschema(&lib);
41%put running &sysmacroname &lib(&engine &schema) for &ds;
42proc sql;
43create table cols as
44 select libname as libref
45 ,upcase(memname) as dsn
46 ,memtype
47 ,upcase(name) as name
48 ,type
49 ,length
50 ,varnum
51 ,label
52 ,format
53 ,idxusage
54 ,notnull
55 from dictionary.columns
56 where upcase(libname)="&lib"
57%if &ds ne #ALL %then %do;
58 and upcase(memname)="&ds"
59%end;
60 ;
61
62%mp_abort(iftrue= (&syscc ne 0)
63 ,mac=&_program
64 ,msg=%str(syscc=&syscc afer &lib cols extraction)
65)
66
67%if &engine=SQLSVR %then %do;
68 proc sql;
69 connect using &lib;
70 create table work.indexes as
71 select * from connection to &lib(
72 select
73 s.name as SchemaName,
74 t.name as memname,
75 tc.name as name,
76 ic.key_ordinal as KeyOrderNr
77 from
78 sys.schemas s
79 inner join sys.tables t on s.schema_id=t.schema_id
80 inner join sys.indexes i on t.object_id=i.object_id
81 inner join sys.index_columns ic on i.object_id=ic.object_id
82 and i.index_id=ic.index_id
83 inner join sys.columns tc on ic.object_id=tc.object_id
84 and ic.column_id=tc.column_id
85 where i.is_primary_key=1
86 and s.name=%str(%')&schema%str(%')
87 order by t.name, ic.key_ordinal ;
88 );disconnect from &lib;
89 create table finalcols as
90 select a.*
91 ,case when b.name is not null then 1 else 0 end as pk_ind
92 from work.cols a
93 left join work.indexes b
94 on a.dsn=b.memname
95 and upcase(a.name)=upcase(b.name)
96 order by libref,dsn;
97%end;
98%else %do;
99 %local dsn;
100 %if &ds = #ALL %then %let dsn=;
101
102 %mp_getconstraints(lib=&lib.,ds=&dsn,outds=work.constraints)
103
104
105 /* extract cols that are clearly primary keys */
106 proc sql;
107 create table work.pk4sure as
108 select libref
109 ,table_name
110 ,constraint_name
111 ,constraint_order
112 ,column_name as name
113 from work.constraints
114 where constraint_type='PRIMARY'
115 order by 1,2,3,4;
116
117 /* extract unique constraints where every col is also NOT NULL */
118 proc sql;
119 create table work.sum as
120 select a.libref
121 ,a.table_name
122 ,a.constraint_name
123 ,count(a.column_name) as unq_cnt
124 ,count(b.column_name) as nul_cnt
125 from work.constraints(where=(constraint_type ='UNIQUE')) a
126 left join work.constraints(where=(constraint_type ='NOT NULL')) b
127 on a.libref=b.libref
128 and a.table_name=b.table_name
129 and a.column_name=b.column_name
130 group by 1,2,3
131 having unq_cnt=nul_cnt;
132
133
134 /* extract cols from the relevant unique constraints */
135 create table work.pkdefault as
136 select a.libref
137 ,a.table_name
138 ,a.constraint_name
139 ,b.constraint_order
140 ,b.column_name as name
141 from work.sum a
142 left join work.constraints(where=(constraint_type ='UNIQUE')) b
143 on a.libref=b.libref
144 and a.table_name=b.table_name
145 and a.constraint_name=b.constraint_name
146 order by 1,2,3,4;
147
148 /* extract cols from the relevant unique INDEXES */
149 create table work.pkfromindex as
150 select libname as libref
151 ,memname as table_name
152 ,indxname as constraint_name
153 ,indxpos as constraint_order
154 ,name
155 from dictionary.indexes
156 where nomiss='yes' and unique='yes' and upcase(libname)="&lib"
157 %if &ds ne #ALL %then %do;
158 and upcase(memname)="&ds"
159 %end;
160 order by 1,2,3,4;
161
162 /* create one table */
163 data work.finalpks;
164 set pkdefault pk4sure pkfromindex;
165 pk_ind=1;
166 /* if there are multiple unique constraints, take the first */
167 by libref table_name constraint_name;
168 retain keepme;
169 if first.table_name then keepme=1;
170 if first.constraint_name and not first.table_name then keepme=0;
171 if keepme=1;
172 run;
173
174 /* join back to starting table */
175 proc sql;
176 create table finalcols as
177 select a.*
178 ,b.constraint_order
179 ,case when b.pk_ind=1 then 1 else 0 end as pk_ind
180 from work.cols a
181 left join work.finalpks b
182 on a.libref=b.libref
183 and a.dsn=b.table_name
184 and upcase(a.name)=upcase(b.name)
185 order by libref,dsn,constraint_order;
186
187%end;
188
189%mp_abort(iftrue= (&syscc ne 0)
190 ,mac=&_program
191 ,msg=%str(syscc=&syscc afer &lib indexes extraction)
192)
193
194/* load columns */
195%bitemporal_dataloader(base_lib=&mpelib
196 ,base_dsn=mpe_datacatalog_vars
197 ,append_dsn=finalcols
198 ,PK=LIBREF DSN NAME
199 ,etlsource=&etlsource
200 ,loadtype=TXTEMPORAL
201 ,tech_from=TX_FROM
202 ,tech_to=TX_TO
203%if &ds ne #ALL %then %do;
204 ,close_vars=LIBREF DSN
205%end;
206 ,dclib=&mpelib
207)
208
209/* prepare tables */
210proc sql;
211create table work.tabs as select
212 libname as libref
213 ,upcase(memname) as dsn
214 ,memtype
215%if %mf_existfeature(DBMS_MEMTYPE)=1 %then %do;
216 ,dbms_memtype
217%end;
218%else %do;
219 ,'n/a' as dbms_memtype format=$32.
220%end;
221 ,typemem
222 ,memlabel
223 ,nvar
224 ,compress
225from dictionary.tables
226 where upcase(libname)="&lib"
227%if &ds ne #ALL %then %do;
228 and upcase(memname)="&ds"
229%end;
230 ;
231data tabs2;
232 set finalcols;
233 length pk_fields $512;
234 retain pk_fields;
235 by libref dsn;
236 if first.dsn then pk_fields='';
237 if pk_ind=1 then pk_fields=catx(' ',pk_fields,name);
238 if last.dsn then output;
239run;
240
241proc sql;
242create table work.finaltabs as
243 select a.libref
244 ,a.dsn
245 ,a.memtype
246 ,a.dbms_memtype
247 ,a.typemem
248 ,a.memlabel
249 ,a.nvar
250 ,a.compress
251 ,b.pk_fields
252 from work.tabs a
253 left join work.tabs2 b
254 on a.libref=b.libref
255 and a.dsn=b.dsn;
256
257%bitemporal_dataloader(base_lib=&mpelib
258 ,base_dsn=mpe_datacatalog_tabs
259 ,append_dsn=finaltabs
260 ,PK=LIBREF DSN
261 ,etlsource=&etlsource
262 ,loadtype=TXTEMPORAL
263 ,tech_from=TX_FROM
264 ,tech_to=TX_TO
265 ,dclib=&mpelib
266%if &ds ne #ALL %then %do;
267 ,close_vars=LIBREF
268%end;
269)
270
271/* prepare table frequently changing attributes */
272proc sql;
273%if &engine=SQLSVR %then %do;
274 connect using &lib;
275 create table work.attrs as select * from connection to &lib(
276 SELECT SCHEMA_NAME(schema_id) as 'schema', name, create_date, modify_date
277 FROM sys.tables ;
278 );
279 create table work.nobs as select * from connection to &lib(
280 SELECT SCHEMA_NAME(A.schema_id) AS 'schema'
281 ,A.Name, AVG(B.rows) AS 'RowCount'
282 FROM sys.objects A
283 INNER JOIN sys.partitions B ON A.object_id = B.object_id
284 WHERE A.type = 'U'
285 GROUP BY A.schema_id, A.Name
286 );
287 disconnect from &lib;
288 create table statustabs as select
289 a.libref
290 ,a.dsn
291 ,b.create_date as crdate
292 ,b.modify_date as modate
293 ,. as filesize
294 ,c.RowCount as nobs
295 from work.tabs a
296 left join work.attrs(where=(schema="&schema")) b
297 on upcase(a.dsn)=upcase(b.name)
298 left join work.nobs(where=(schema="&schema")) c
299 on upcase(a.dsn)=upcase(c.name);
300%end;
301%else %do;
302 create table statustabs as select
303 libname as libref
304 ,upcase(memname) as dsn
305 ,crdate
306 ,modate
307 ,filesize
308 ,nobs
309 from dictionary.tables
310 where upcase(libname)="&lib"
311 %if &ds ne #ALL %then %do;
312 and upcase(memname)="&ds"
313 %end;
314 ;
315%end;
316
317%bitemporal_dataloader(base_lib=&mpelib
318 ,base_dsn=mpe_datastatus_tabs
319 ,append_dsn=statustabs
320 ,PK=LIBREF DSN
321 ,etlsource=&etlsource
322 ,loadtype=TXTEMPORAL
323 ,tech_from=TX_FROM
324 ,tech_to=TX_TO
325 ,dclib=&mpelib
326%if &ds ne #ALL %then %do;
327 ,close_vars=LIBREF
328%end;
329)
330
331%if &ds = #ALL %then %do;
332 proc sql;
333 create table work.sumcat as
334 select libname as libref,
335 count(distinct memname) as catalog_cnt
336 from dictionary.catalogs
337 where upcase(libname)="&lib"
338 group by 1;
339 create table work.sumdsn as select
340 libref
341 ,sum(filesize) as libsize
342 ,count(*) as table_cnt
343 from statustabs
344 group by 1;
345 create table work.libs as
346 select libref from work.sumcat
347 union
348 select libref from work.sumdsn;
349 create table work.statuslibs as
350 select a.libref,
351 b.libsize,
352 b.table_cnt,
353 c.catalog_cnt
354 from work.libs a
355 left join work.sumdsn b
356 on a.libref=b.libref
357 left join work.sumcat c
358 on a.libref=c.libref;
359
360 %bitemporal_dataloader(base_lib=&mpelib
361 ,base_dsn=mpe_datastatus_libs
362 ,append_dsn=statuslibs
363 ,PK=LIBREF
364 ,etlsource=&etlsource
365 ,loadtype=TXTEMPORAL
366 ,tech_from=TX_FROM
367 ,tech_to=TX_TO
368 ,dclib=&mpelib
369 )
370%end;
371
372%mend mpe_refreshtables;