source: FOIAVistA/trunk/r/VA_FILEMAN-ARJT-DI-DD-DM-DT-%DT-%RCR/DMSQF2.m@ 1397

Last change on this file since 1397 was 628, checked in by George Lilly, 15 years ago

initial load of FOIAVistA 6/30/08 version

File size: 6.0 KB
Line 
1DMSQF2 ;SFISC/JHM-BUILD INDEX AND PARENT FOREIGN KEYS ;7/28/97 11:10
2 ;;22.0;VA FileMan;;Mar 30, 1999
3 ;Per VHA Directive 10-93-142, this routine should not be modified.
4 Q
5PFK(TI) ;BUILD FOREIGN KEYS FOR PARENT TABLES
6 N T,GL,FL,PEI,S,TE,TC
7 D INIT F I=1:1:$O(TE(""),-1)-1 D BFK(I)
8 Q
9BFK(L) ;BUILD A LEVEL L FOREIGN KEY FOR TABLE PRIMARY KEYS
10 N I,FF,FTI,FT,FTN,FKN,FPEI,FPE,IEN,TT,FDA,ERR,FIEN,PI
11 S FF=$P(FL,",",L),FTI=$O(^DMSQ("T","C",FF,"")) Q:FTI=""
12 S FT=^DMSQ("T",FTI,0),FTN=$P(FT,U),FKN=FTN_"_PFK"
13 S FPEI=$O(^DMSQ("E","F",FTI,"P","")) Q:FPEI=""
14 S FPE=^DMSQ("E",FPEI,0),FDI=$P(FPE,U,2)
15 ;BUILD FOREIGN KEY TABLE ELEMENT
16 S IEN=$O(^DMSQ("E","G",TI,FKN,"")),TT=1.5216,IEN=$S(IEN:IEN,1:"+1")_","
17 S FDA(TT,IEN,.01)=FKN ; FOREIGN KEY NAME
18 S FDA(TT,IEN,1)=FDI ; DOMAIN OF FOREIGN TABLE
19 S FDA(TT,IEN,2)=TI ; FOREIGN KEY IS IN THIS TABLE
20 S FDA(TT,IEN,3)="F" ; TYPE F FOR FOREIGN KEY
21 S FDA(TT,IEN,4)="Foreign key to ancestor "_FTN ; COMMENT
22 S FIEN=$$PUT^DMSQU(IEN,"FDA","ERR")
23 I $D(ERR)!'FIEN D ERR^DMSQU(F,L,"FOREIGN KEY: ANCESTOR FOREIGN KEY INSERT FAILED") Q
24 S (IENL,FKI)=""
25 F I=1:1:L S FKI=$O(^DMSQ("F","B",FIEN,FKI)) Q:FKI="" D
26 . S $P(IENL,U,I)=FKI
27 F I=1:1:L D BFKI(I)
28 Q
29BFKI(L) ;BUILD FOREIGN KEY COLUMN ELEMENT
30 S CEI=$P(TC(L),U),CI=$O(^DMSQ("C","B",CEI,""))
31 I 'CI D ERR^DMSQU(F,L,"FOREIGN KEY: NO POINTED-TO COLUMN AT LEVEL") Q
32 S PI=$O(^DMSQ("P","C",FPEI,L,""))
33 I 'PI D ERR^DMSQU(F,L,"FOREIGN KEY: NO ANCESTOR PRIMARY KEY") Q
34 ;BUILD FK COLUMN
35 S FI=$P(IENL,U,I),TT=1.5219,IEN=$S(FI:FI,1:"+1")_","
36 S FDA(TT,IEN,.01)=FIEN ; FK TABLE ELEMENT ID
37 S FDA(TT,IEN,1)=PI ; FOREIGN PRIMARY KEY ID
38 S FDA(TT,IEN,2)=CI ; LOCAL PRIMARY KEY COLUMN ID
39 S FI=$$PUT^DMSQU(IEN,"FDA","ERR")
40 I $D(ERR)!'FI D ERR^DMSQU(F,L,"FOREIGN KEY: ANCESTOR FOREIGN KEY COLUMN INSERT FAILED")
41 Q
42INIT ;SET PRIMARY KEY VARIABLES FOR TABLE TI
43 N S,P,PI,K,KCI,KC,PEI
44 S S=0,T=^DMSQ("T",TI,0),GL=^(1),(F,P,FL)=$P(T,U,7)
45 F S P=$G(^DD(P,0,"UP")) Q:P="" S FL=P_","_FL
46 S PEI=$O(^DMSQ("E","F",TI,"P","")),PI=""
47 F S PI=$O(^DMSQ("P","B",PEI,PI)) Q:PI="" D
48 . S K=^DMSQ("P",PI,0),S=$P(K,U,3),KCI=$P(K,U,2),KC=^DMSQ("C",KCI,0)
49 . S TE(S)=$P(^DMSQ("E",$P(KC,U),0),U,1,2),TC(S)=KC
50 Q
51INDEX(TI) ;BUILD ALL REGULAR INDICIES FOR TABLE TI
52 I '$G(DIFM) D ENV^DMSQU
53 N T,GL,FL,PI,K,TE,TC,KC,KCI,IN,FI,CI,IC,IE,I,IF,IX,C,CEI,L,TN,CN,INM,IEI,IEN,FDA,ERR,TIEN,PIEN,DIEN,CIEN
54 D INIT S FI=0
55FI S FI=$O(^DD(F,FI)) Q:'FI S IN=0
56IN S IN=$O(^DD(F,FI,1,IN)) G FI:'IN D IDX(F,FI,IN,.TC,.TE) G IN
57 ;
58IDX(F,FI,IN,TC,TE) ;BUILD INDEX
59 N P,S,IGL,IC,IE,I,IF,IX,CI,ITI,CEI,CE,L,TN,GF,INM1,INM2,TN1,TN2
60 S I=$G(^DD(F,FI,1,IN,0)) I $P(I,U,3)]"" Q
61 I I="" D ERR^DMSQU(F,FI,"INDEX: MISSING DATA DICTIONARY DATA") Q
62 S IF=+I,IX=$P(I,U,2) I IX=""!'IF Q
63 I $G(^DD(F,FI,1,IN,1))'[",DA)" D Q
64 . D ERR^DMSQU(F,FI,"INDEX: IRREGULAR FORMAT")
65 S CI=$O(^DMSQ("C","D",F,FI,"")),ITI=$$T(IF)
66 I 'CI D ERR^DMSQU(F,FI,"INDEX: NO ASSOCIATED COLUMN RECORD") Q
67 F L=$L(FL,","):-1:1 Q:$P(FL,",",L)=IF
68 M IC=TC,IE=TE
69 S C=^DMSQ("C",CI,0),CEI=$P(C,U),CE=^DMSQ("E",CEI,0)
70 S IC(L-.5)=C,IE(L-.5)=CE
71 ;S TN=$P(T,U),CN=$P(CE,U),INM=$$SQLI^DMSQU(TN_"_X"_IX_"_"_CN,30)
72 S TN=$P(T,U),TN1=$E(TN,1,$L(TN)-1),TN2=$E(TN,$L(TN)),CN=$P(CE,U)
73 S INM1=$$SQLI^DMSQU(TN1,18),INM2=$$SQLI^DMSQU("X"_IX_"_"_CN,10)
74 S INM=INM1_TN2_"_"_INM2
75 S IGL=GL,GF=$P(IGL,"{K}",L)_$C(34)_IX_$C(34)_",{K},"
76 S $P(IGL,"{K}",L)=GF F I=L+1:1:$L(GL,"{K}") S $P(IGL,"{K}",I)=","
77 S IEI=$O(^DMSQ("T","B",INM,""))
78 ;BUILD TABLE
79 S TT=1.5215,IEN=$S(IEI:IEI,1:"+1")_","
80 S FDA(TT,IEN,.01)=INM ; INDEX TABLE NAME
81 S FDA(TT,IEN,1)=1 ; SCHEMA SQLI
82 S FDA(TT,IEN,2)="Index of "_TN_" by "_CN ;COMMENT
83 S FDA(TT,IEN,3)=TI ; MASTER TABLE ID
84 S FDA(TT,IEN,4)=1 ; VERSION NUMBER
85 S FDA(TT,IEN,7)=DT ; UPDATE DATE
86 S FDA(TT,IEN,8)=IGL ; GLOBAL NAME
87 S TIEN=$$PUT^DMSQU(IEN,"FDA","ERR")
88 I $D(ERR) D ERR^DMSQU(F,FI,"INDEX: TABLE INSERT FAILED") Q
89 S DIEN=$O(^DMSQ("DM","C",TIEN,""))
90 ; BUILD TABLE DOMAIN
91 S TT=1.5212,IEN=$S(DIEN:DIEN,1:"+1")_","
92 S FDA(TT,IEN,.01)=$$SQLI^DMSQU(INM_"_ID",30) ; DOMAIN NAME
93 S FDA(TT,IEN,1)=1 ; TYPE = PRIMARY KEY
94 S FDA(TT,IEN,2)="Domain of table "_INM ; COMMENT
95 S FDA(TT,IEN,3)=TIEN ; TABLE ID
96 S DIEN=$$PUT^DMSQU(IEN,"FDA","ERR")
97 I $D(ERR) D ERR^DMSQU(F,FI,"INDEX: TABLE DOMAIN INSERT FAILED") Q
98 S PIEN=$O(^DMSQ("E","F",TIEN,"P",""))
99 ;BUILD PRIMARY KEY HEADER ELEMENT
100 S TT=1.5216,IEN=$S(PIEN:PIEN,1:"+1")_","
101 S FDA(TT,IEN,.01)=$$SQLI^DMSQU(INM_"_PK",30) ; PRIMARY KEY NAME
102 S FDA(TT,IEN,1)=DIEN ; TABLE DOMAIN
103 S FDA(TT,IEN,2)=TIEN ; TABLE ID
104 S FDA(TT,IEN,3)="P" ; TYPE = P FOR PRIMARY KEY
105 S FDA(TT,IEN,4)="Primary key header for "_INM ; COMMENT
106 S PIEN=$$PUT^DMSQU(IEN,"FDA","ERR")
107 I $D(ERR) D ERR^DMSQU(F,FI,"INDEX: PRIMARY KEY ELEMENT INSERT FAILED") Q
108 S S=0,(K,P)="" F S K=$O(IE(K)) Q:K="" D
109 . S S=S+1 D PKI(S,PIEN,$P(IGL,"{K}",S),IE(K),IC(K),.P)
110 Q
111PKI(S,PEI,G,E,C,P) ;BUILD COLUMN ELEMENT, COLUMN AND PRIMARY KEY ELEMENT
112 N ICN,CI,CEI,PI,W,KFI S (CI,CEI,PI,KFI)="",W=$P(C,U,2)
113 I W>30 S KFI=$O(^DMSQ("KF","B","LONG_CHARACTER",""))
114 S PI=$O(^DMSQ("P","C",PEI,S,""))
115 I PI S CI=$P($G(^DMSQ("P",PI,0)),U,2)
116 I CI S CEI=$P($G(^DMSQ("C",CI,0)),U)
117 S ICN=$P(E,U)
118 ;BUILD COLUMN ELEMENT
119 S TT=1.5216,IEN=$S(CEI:CEI,1:"+1")_","
120 S FDA(TT,IEN,.01)=ICN ; COLUMN NAME
121 S FDA(TT,IEN,1)=$P(E,U,2) ; DOMAIN ID
122 S FDA(TT,IEN,2)=TIEN ; TABLE ID
123 S FDA(TT,IEN,3)="C" ; TYPE = COLUMN
124 S FDA(TT,IEN,4)="Index Primary Key #"_S_" for "_INM_"."_ICN
125 S CEI=$$PUT^DMSQU(IEN,"FDA","ERR")
126 I $D(ERR)!'CEI D ERR^DMSQU(F,FI,"INDEX: COLUMN ELEMENT INSERT FAILED") Q
127 ;BUILD COLUMN
128 S TT=1.5217,IEN=$S(CI:CI,1:"+1")_","
129 S FDA(TT,IEN,.01)=CEI ; COLUMN ELEMENT ID
130 I P S FDA(TT,IEN,8)=P ; PARENT POINTER
131 S FDA(TT,IEN,9)=G ; GLOBAL FRAGMENT
132 S CI=$$PUT^DMSQU(IEN,"FDA","ERR")
133 I $D(ERR)!'CI D ERR^DMSQU(F,FI,"INDEX: COLUMN INSERT FAILED")
134 ;BUILD PRIMARY KEY
135 S TT=1.5218,IEN=$S(PI:PI,1:"+1")_","
136 S FDA(TT,IEN,.01)=PEI ; PRIMARY KEY HEADER ID
137 S FDA(TT,IEN,1)=CI ; COLUMN ID
138 S FDA(TT,IEN,2)=S ; KEY SEQUENCE
139 I KFI S FDA(TT,IEN,7)=KFI ; KEY FORMAT
140 S PI=$$PUT^DMSQU(IEN,"FDA","ERR")
141 I $D(ERR)!'PI D ERR^DMSQU(F,FI,"INDEX: PRIMARY KEY INSERT FAILED")
142 S P=CI
143 Q
144T(F) Q $O(^DMSQ("T","C",F,""))
Note: See TracBrowser for help on using the repository browser.