1
0
mirror of https://github.com/sasjs/core.git synced 2025-12-11 14:34:35 +00:00

Merge pull request #213 from sasjs/issue212

fix: ensuring indexes are picked up in mp_getpk().  Closes #212
This commit is contained in:
Allan Bowe
2022-04-13 15:59:22 +03:00
committed by GitHub
3 changed files with 86 additions and 7 deletions

23
all.sas
View File

@@ -7362,7 +7362,8 @@ create table &outds (rename=(
)/*/STORE SOURCE*/;
%local engine schema ds1 ds2 ds3 dsn tabs1 tabs2 sum pk4sure pkdefault finalpks;
%local engine schema ds1 ds2 ds3 dsn tabs1 tabs2 sum pk4sure pkdefault finalpks
pkfromindex;
%let lib=%upcase(&lib);
%let ds=%upcase(&ds);
@@ -7377,6 +7378,7 @@ create table &outds (rename=(
%let sum=%mf_getuniquename(prefix=getpk_sum);
%let pk4sure=%mf_getuniquename(prefix=getpk_pk4sure);
%let pkdefault=%mf_getuniquename(prefix=getpk_pkdefault);
%let pkfromindex=%mf_getuniquename(prefix=getpk_pkfromindex);
%let finalpks=%mf_getuniquename(prefix=getpk_finalpks);
%local dbg;
@@ -7487,9 +7489,23 @@ create table &ds1 as
and a.constraint_name=b.constraint_name
order by 1,2,3,4;
/* extract cols from the relevant unique INDEXES */
create table &pkfromindex as
select libname as libref
,memname as table_name
,indxname as constraint_name
,indxpos as constraint_order
,name
from dictionary.indexes
where nomiss='yes' and unique='yes' and upcase(libname)="&lib"
%if &ds ne 0 %then %do;
and upcase(memname)="&ds"
%end;
order by 1,2,3,4;
/* create one table */
data &finalpks;
set &pkdefault &pk4sure ;
set &pkdefault &pk4sure &pkfromindex;
pk_ind=1;
/* if there are multiple unique constraints, take the first */
by libref table_name constraint_name;
@@ -7563,7 +7579,8 @@ create table &outds as
iftrue=(&mdebug=0)
)
%mend mp_getpk;/**
%mend mp_getpk;
/**
@file
@brief Performs a text substitution on a file
@details Makes use of the GSUB function in LUA to perform a text substitution

View File

@@ -55,7 +55,8 @@
)/*/STORE SOURCE*/;
%local engine schema ds1 ds2 ds3 dsn tabs1 tabs2 sum pk4sure pkdefault finalpks;
%local engine schema ds1 ds2 ds3 dsn tabs1 tabs2 sum pk4sure pkdefault finalpks
pkfromindex;
%let lib=%upcase(&lib);
%let ds=%upcase(&ds);
@@ -70,6 +71,7 @@
%let sum=%mf_getuniquename(prefix=getpk_sum);
%let pk4sure=%mf_getuniquename(prefix=getpk_pk4sure);
%let pkdefault=%mf_getuniquename(prefix=getpk_pkdefault);
%let pkfromindex=%mf_getuniquename(prefix=getpk_pkfromindex);
%let finalpks=%mf_getuniquename(prefix=getpk_finalpks);
%local dbg;
@@ -180,9 +182,23 @@ create table &ds1 as
and a.constraint_name=b.constraint_name
order by 1,2,3,4;
/* extract cols from the relevant unique INDEXES */
create table &pkfromindex as
select libname as libref
,memname as table_name
,indxname as constraint_name
,indxpos as constraint_order
,name
from dictionary.indexes
where nomiss='yes' and unique='yes' and upcase(libname)="&lib"
%if &ds ne 0 %then %do;
and upcase(memname)="&ds"
%end;
order by 1,2,3,4;
/* create one table */
data &finalpks;
set &pkdefault &pk4sure ;
set &pkdefault &pk4sure &pkfromindex;
pk_ind=1;
/* if there are multiple unique constraints, take the first */
by libref table_name constraint_name;
@@ -256,4 +272,4 @@ create table &outds as
iftrue=(&mdebug=0)
)
%mend mp_getpk;
%mend mp_getpk;

View File

@@ -88,4 +88,50 @@ run;
/* constraint capture at library level is functional - uses first 2 tests */
%mp_getpk(work,outds=test4)
%mp_assertdsobs(work.test4,test=ATLEAST 2)
%mp_assertdsobs(work.test4,test=ATLEAST 2)
/* unique & not null INDEX captured */
proc sql;
create table work.example5(
TX_FROM float format=datetime19.,
DD_TYPE char(16),
DD_SOURCE char(2048),
DD_SHORTDESC char(256)
);
proc datasets lib=work noprint;
modify example5;
index create tx_from /nomiss unique;
quit;
%mp_getpk(work,ds=example5,outds=test5)
data _null_;
set work.test5;
call symputx('test5',pk_fields);
run;
%mp_assert(
iftrue=("&test5"="TX_FROM"),
desc=mp_getpk captures single column not null unique index,
outds=work.test_results
)
/* unique & not null COMPOSITE INDEX captured */
proc sql;
create table work.example6(
TX_FROM float format=datetime19.,
DD_TYPE char(16),
DD_SOURCE char(2048),
DD_SHORTDESC char(256)
);
proc datasets lib=work noprint;
modify example6;
index create pk_6=(tx_from dd_type) /nomiss unique;
quit;
%mp_getpk(work,ds=example6,outds=test6)
data _null_;
set work.test6;
call symputx('test6',pk_fields);
run;
%mp_assert(
iftrue=("&test6"="TX_FROM DD_TYPE"),
desc=mp_getpk captures multiple column not null unique index,
outds=work.test_results
)