source: WorldVistAEHR/trunk/r/VA_FILEMAN-ARJT-DI-DD-DM-DT-%DT-%RCR/DMSQF1.m@ 1710

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

initial load of WorldVistAEHR

File size: 5.3 KB
Line 
1DMSQF1 ;SFISC/JHM-INITIALIZE SQLI_FILE (CONT) ;5/7/98 14:53
2 ;;22.0;VA FileMan;;Mar 30, 1999
3 ;Per VHA Directive 10-93-142, this routine should not be modified.
4 Q
5PK(TI) ;GENERATE PRIMARY KEY ELEMENTS AND COLUMNS FOR TABLE IDENTIFIER TI
6 I '$G(DIFM) D ENV^DMSQU
7 N X,N,G,PAR,PEI,TIEN,T,DIERR S T=1.5215,TIEN=TI_","
8 K DIERR D GETS^DIQ(T,TIEN,".01;6;8","I","X")
9 I $D(DIERR) D ERR^DMSQU(999999999,"","PRIMARY KEY: CAN'T GET TABLE DATA") Q ""
10 S N=X(T,TIEN,.01,"I"),G=X(T,TIEN,8,"I"),F=X(T,TIEN,6,"I")
11 K FDA S TT=1.5212,DI=$O(^DMSQ("DM","C",TI,"")) ;BUILD DOMAIN
12 S IEN=$S(DI:DI,1:"+1")_"," ;TABLE DOMAIN
13 S FDA(TT,IEN,.01)=N_"_ID" ;DOMAIN NAME
14 S FDA(TT,IEN,1)=1 ;DATA TYPE = PRIMARY KEY
15 S FDA(TT,IEN,2)="Domain of table "_N ;COMMENT
16 S FDA(TT,IEN,3)=TI ;TABLE ID
17 S DI=$$PUT^DMSQU(IEN,"FDA","ERR")
18 I 'DI D ERR^DMSQU(F,"","PRIMARY KEY: DOMAIN INSERT FAILED") Q ""
19 ; BUILD PRIMARY KEY TABLE ELEMENT
20 S PEI=$O(^DMSQ("E","F",TI,"P","")),IEN=$S(PEI:PEI,1:"+1")_",",TT=1.5216
21 S FDA(TT,IEN,.01)=N_"_PK" ;TABLE ELEMENT NAME
22 S FDA(TT,IEN,1)=DI ;TE DOMAIN SAME AS TABLE
23 S FDA(TT,IEN,2)=TI ;TABLE ID
24 S FDA(TT,IEN,3)="P" ;TYPE = PRIMARY KEY
25 S FDA(TT,IEN,4)="Primary key header for table "_N ;COMMENT
26 S PEI=$$PUT^DMSQU(IEN,"FDA","ERR")
27 I 'PEI D ERR^DMSQU(F,"","PRIMARY KEY: TABLE ELEMENT INSERT FAILED") Q ""
28 ;BUILD TABLE PRIMARY KEYS
29 S F=$$GET^DMSQU(1.5215,TI_",",6,"I","","ERR"),KL=$$KL^DMSQU(F),PAR=""
30 I $D(ERR)!'F D ERR^DMSQU(F,"","PRIMARY KEY: CAN'T GET TABLE'S FILE #") Q ""
31 F I=1:1:$L(G,"{K}")-1 D PKI(PEI,I,$P(KL,",",I),$P(G,"{K}",I),.PAR)
32 Q CEI
33PKI(PEI,SEQ,F,GF,PAR) ;BUILD COLUMN ELEMENT, COLUMN AND PRIMARY KEY DEFS
34 N TN,KI,KN,PI,CI,DM,W,S,TT,I,DI,ERR
35 S TN=$$GET^DMSQU(1.5216,PEI_",",2,"","","ERR")
36 I TN="" D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: CAN'T GET DATA FOR MASTER TABLE") Q ""
37 S KI=$O(^DMSQ("T","C",F,""))
38 I 'KI D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: MISSING TABLE RECORD") Q ""
39 S KN=$$GET^DMSQU(1.5215,KI_",",.01),(CI,CEI)=""
40 S PI=$O(^DMSQ("P","C",PEI,SEQ,""))
41 I PI K ERR,DIERR D Q:'CI!'CEI
42 . S CI=$$GET^DMSQU(1.5218,PI_",",1,"I","","ERR")
43 . I $D(ERR) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: TABLE MISSING COLUMN POINTER") Q
44 . I CI S CEI=$$GET^DMSQU(1.5217,CI_",",.01,"I","","ERR")
45 . I $D(ERR) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: CAN'T GET COLUMN'S TABLE ELEMENT") Q
46 . I 'CI!('CEI) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: MISSING COLUMN POINTER") Q
47 ;DEFINE COLUMN ELEMENT
48 S IEN=$S(CEI:CEI,1:"+1")_",",TT=1.5216
49 S DM=$$DOM^DMSQU(F,.001),W=$P(DM,U,2),S=$P(DM,U,3),DM=$P(DM,U)
50 I DM=""!("FM_DATE_TIME;FM_MOMENT;FM_DATE;INTEGER;NUMERIC"'[DM) S DM="INTEGER",W=10
51 S DI=$O(^DMSQ("DM","B",DM,""))
52 S FDA(TT,IEN,.01)=$$KWC^DMSQU(KN_"_ID") ;COLUMN NAME = TABLE_NAME_ID
53 S FDA(TT,IEN,1)=DI ;DOMAIN
54 S FDA(TT,IEN,2)=TI ;TABLE ID
55 S FDA(TT,IEN,3)="C" ; TYPE = COLUMN
56 S FDA(TT,IEN,4)="Primary key #"_SEQ_" of table "_TN ; COMMENT
57 S CEI=$$PUT^DMSQU(IEN,"FDA","ERR")
58 I $D(ERR) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: COLUMN ELEMENT INSERT FAILED") Q
59 ;DEFINE COLUMN
60 S TT=1.5217,CI=$O(^DMSQ("C","B",CEI,"")),IEN=$S(CI:CI,1:"+1")_","
61 S FDA(TT,IEN,.01)=CEI ;COL ELEMENT ID
62 S FDA(TT,IEN,2)=W ;WIDTH
63 S:S FDA(TT,IEN,3)=S ;SCALE
64 S FDA(TT,IEN,5)=1 ;REQUIRED
65 S FDA(TT,IEN,7)=0 ;BASE COLUMN
66 S FDA(TT,IEN,9)=GF ;GLOBAL FRAGMENT
67 S FDA(TT,IEN,8)=PAR ;PARENT
68 S CI=$$PUT^DMSQU(IEN,"FDA","ERR")
69 I $D(ERR) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: COLUMN INSERT FAILED") Q
70 S PAR=CI
71 ;DEFINE PRIMARY KEY
72 S TT=1.5218,PI=$O(^DMSQ("P","C",PEI,SEQ,"")),IEN=$S(PI:PI,1:"+1")_","
73 S FDA(TT,IEN,.01)=PEI ; TABLE ELEMENT
74 S FDA(TT,IEN,1)=CI ; COLUMN
75 S FDA(TT,IEN,2)=SEQ ; SEQUENCE NUMBER
76 S FDA(TT,IEN,3)=0 ; ALWAYS START AT 0
77 S FDA(TT,IEN,4)="'{K}" ; STOP AT EXPRESSION
78 S CI=$$PUT^DMSQU(IEN,"FDA","ERR")
79 I $D(ERR) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: TABLE ELEMENT INSERT FAILED")
80 Q
81FK(CI) ;DEFINE FOREIGN KEY FOR POINTER COLUMN CI
82 N C,CEI,CE,CN,F,FI,TI,SP,FF,FIEN,FTI,FTN,FPEI,FPE,FPI,FNM,TT,IEN,FDA
83 S C=^DMSQ("C",CI,0)
84 S CEI=$P(C,U),CE=^DMSQ("E",CEI,0),CN=$P(CE,U) Q:$P(CE,U,2)'=13 ""
85 S F=$P(C,U,5),FI=$P(C,U,6) I 'F!'FI Q ""
86 S TI=$P(CE,U,3),SP=$P(^DD(F,FI,0),U,2)
87 S FF=+$P(SP,"P",2)
88 I 'FF D ERR^DMSQU(F,FI,"FOREIGN KEY: NO POINTED-TO FILE IN SPECIFIER") Q ""
89 S FTI=$O(^DMSQ("T","C",FF,""))
90 I 'FTI D ERR^DMSQU(F,FI,"FOREIGN KEY: NO TABLE FOR POINTED-TO FILE") Q ""
91 S FTN=$P(^DMSQ("T",FTI,0),U)
92 S FPEI=$O(^DMSQ("E","F",FTI,"P",""))
93 I 'FPEI D ERR^DMSQU(F,FI,"FOREIGN KEY: NO PRIMARY KEY TABLE ELEMENT") Q ""
94 S FPE=^DMSQ("E",FPEI,0),FDI=$P(FPE,U,2)
95 S FPI=$O(^DMSQ("P","B",FPEI,""))
96 I 'FPI D ERR^DMSQU(F,FI,"FOREIGN KEY: NO ASSOCIATED PRIMARY KEY") Q ""
97 S FCI=$P(^DMSQ("P",FPI,0),U,2)
98 S FNM=CN_"_FK",IEN=$O(^DMSQ("E","G",TI,FNM,""))
99 ;BUILD FOREIGN KEY TABLE ELEMENT
100 S TT=1.5216,IEN=$S(IEN:IEN,1:"+1")_","
101 S FDA(TT,IEN,.01)=FNM ;FOREIGN KEY NAME
102 S FDA(TT,IEN,1)=FDI ;DOMAIN = FOREIGN KEY TABLE DOMAIN
103 S FDA(TT,IEN,2)=TI ;TABLE ID
104 S FDA(TT,IEN,3)="F" ;TYPE F = FOREIGN KEY
105 S FDA(TT,IEN,4)="Foreign key to "_FTN ;COMMENT
106 S FIEN=$$PUT^DMSQU(IEN,"FDA","ERR")
107 I $D(ERR) D ERR^DMSQU(F,FI,"FOREIGN KEY: TABLE ELEMENT INSERT FAILED") Q ""
108 ;BUILD FOREIGN KEY COLUMN ELEMENT
109 S TT=1.5219,IEN=$O(^DMSQ("F","B",FIEN,"")),IEN=$S(IEN:IEN,1:"+1")_","
110 S FDA(TT,IEN,.01)=FIEN ;FK TABLE ELEMENT ID
111 S FDA(TT,IEN,1)=FPI ;FOREIGN TABLE PRIMARY KEY ELEMENT ID
112 S FDA(TT,IEN,2)=CI ;COLUMN ID IN LOCAL TABLE
113 S FIEN=$$PUT^DMSQU(IEN,"FDA","ERR")
114 I $D(ERR) D ERR^DMSQU(F,FI,"FOREIGN KEY: COLUMN ELEMENT INSERT FAILED")
115 Q FIEN
Note: See TracBrowser for help on using the repository browser.