Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: DMSQF1

DMSQF1.m

Go to the documentation of this file.
DMSQF1 ;SFISC/JHM-INITIALIZE SQLI_FILE (CONT) ;5/7/98  14:53
 ;;22.2;VA FileMan;;Jan 05, 2016;Build 42
 ;;Per VA Directive 6402, this routine should not be modified.
 ;;Submitted to OSEHRA 5 January 2015 by the VISTA Expertise Network.
 ;;Based on Medsphere Systems Corporation's MSC FileMan 1051.
 ;;Licensed under the terms of the Apache License, Version 2.0.
 ;
 Q
PK(TI) ;GENERATE PRIMARY KEY ELEMENTS AND COLUMNS FOR TABLE IDENTIFIER TI
 I '$G(DIFM) D ENV^DMSQU
 N X,N,G,PAR,PEI,TIEN,T,DIERR S T=1.5215,TIEN=TI_","
 K DIERR D GETS^DIQ(T,TIEN,".01;6;8","I","X")
 I $D(DIERR) D ERR^DMSQU(999999999,"","PRIMARY KEY: CAN'T GET TABLE DATA") Q ""
 S N=X(T,TIEN,.01,"I"),G=X(T,TIEN,8,"I"),F=X(T,TIEN,6,"I")
 K FDA S TT=1.5212,DI=$O(^DMSQ("DM","C",TI,"")) ;BUILD DOMAIN
 S IEN=$S(DI:DI,1:"+1")_"," ;TABLE DOMAIN
 S FDA(TT,IEN,.01)=N_"_ID" ;DOMAIN NAME
 S FDA(TT,IEN,1)=1 ;DATA TYPE = PRIMARY KEY
 S FDA(TT,IEN,2)="Domain of table "_N ;COMMENT
 S FDA(TT,IEN,3)=TI ;TABLE ID
 S DI=$$PUT^DMSQU(IEN,"FDA","ERR")
 I 'DI D ERR^DMSQU(F,"","PRIMARY KEY: DOMAIN INSERT FAILED") Q ""
 ; BUILD PRIMARY KEY TABLE ELEMENT
 S PEI=$O(^DMSQ("E","F",TI,"P","")),IEN=$S(PEI:PEI,1:"+1")_",",TT=1.5216
 S FDA(TT,IEN,.01)=N_"_PK" ;TABLE ELEMENT NAME
 S FDA(TT,IEN,1)=DI ;TE DOMAIN SAME AS TABLE
 S FDA(TT,IEN,2)=TI ;TABLE ID
 S FDA(TT,IEN,3)="P" ;TYPE = PRIMARY KEY
 S FDA(TT,IEN,4)="Primary key header for table "_N ;COMMENT
 S PEI=$$PUT^DMSQU(IEN,"FDA","ERR")
 I 'PEI D ERR^DMSQU(F,"","PRIMARY KEY: TABLE ELEMENT INSERT FAILED") Q ""
 ;BUILD TABLE PRIMARY KEYS
 S F=$$GET^DMSQU(1.5215,TI_",",6,"I","","ERR"),KL=$$KL^DMSQU(F),PAR=""
 I $D(ERR)!'F D ERR^DMSQU(F,"","PRIMARY KEY: CAN'T GET TABLE'S FILE #") Q ""
 F I=1:1:$L(G,"{K}")-1 D PKI(PEI,I,$P(KL,",",I),$P(G,"{K}",I),.PAR)
 Q CEI
PKI(PEI,SEQ,F,GF,PAR) ;BUILD COLUMN ELEMENT, COLUMN AND PRIMARY KEY DEFS
 N TN,KI,KN,PI,CI,DM,W,S,TT,I,DI,ERR
 S TN=$$GET^DMSQU(1.5216,PEI_",",2,"","","ERR")
 I TN="" D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: CAN'T GET DATA FOR MASTER TABLE") Q ""
 S KI=$O(^DMSQ("T","C",F,""))
 I 'KI D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: MISSING TABLE RECORD") Q ""
 S KN=$$GET^DMSQU(1.5215,KI_",",.01),(CI,CEI)=""
 S PI=$O(^DMSQ("P","C",PEI,SEQ,""))
 I PI K ERR,DIERR D  Q:'CI!'CEI
 . S CI=$$GET^DMSQU(1.5218,PI_",",1,"I","","ERR")
 . I $D(ERR) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: TABLE MISSING COLUMN POINTER") Q
 . I CI S CEI=$$GET^DMSQU(1.5217,CI_",",.01,"I","","ERR")
 . I $D(ERR) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: CAN'T GET COLUMN'S TABLE ELEMENT") Q
 . I 'CI!('CEI) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: MISSING COLUMN POINTER") Q
 ;DEFINE COLUMN ELEMENT
 S IEN=$S(CEI:CEI,1:"+1")_",",TT=1.5216
 S DM=$$DOM^DMSQU(F,.001),W=$P(DM,U,2),S=$P(DM,U,3),DM=$P(DM,U)
 I DM=""!("FM_DATE_TIME;FM_MOMENT;FM_DATE;INTEGER;NUMERIC"'[DM) S DM="INTEGER",W=10
 S DI=$O(^DMSQ("DM","B",DM,""))
 S FDA(TT,IEN,.01)=$$KWC^DMSQU(KN_"_ID") ;COLUMN NAME = TABLE_NAME_ID
 S FDA(TT,IEN,1)=DI ;DOMAIN
 S FDA(TT,IEN,2)=TI ;TABLE ID
 S FDA(TT,IEN,3)="C" ; TYPE = COLUMN
 S FDA(TT,IEN,4)="Primary key #"_SEQ_" of table "_TN ; COMMENT
 S CEI=$$PUT^DMSQU(IEN,"FDA","ERR")
 I $D(ERR) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: COLUMN ELEMENT INSERT FAILED") Q
 ;DEFINE COLUMN
 S TT=1.5217,CI=$O(^DMSQ("C","B",CEI,"")),IEN=$S(CI:CI,1:"+1")_","
 S FDA(TT,IEN,.01)=CEI ;COL ELEMENT ID
 S FDA(TT,IEN,2)=W ;WIDTH
 S:S FDA(TT,IEN,3)=S ;SCALE
 S FDA(TT,IEN,5)=1 ;REQUIRED
 S FDA(TT,IEN,7)=0 ;BASE COLUMN
 S FDA(TT,IEN,9)=GF ;GLOBAL FRAGMENT
 S FDA(TT,IEN,8)=PAR ;PARENT
 S CI=$$PUT^DMSQU(IEN,"FDA","ERR")
 I $D(ERR) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: COLUMN INSERT FAILED") Q
 S PAR=CI
 ;DEFINE PRIMARY KEY
 S TT=1.5218,PI=$O(^DMSQ("P","C",PEI,SEQ,"")),IEN=$S(PI:PI,1:"+1")_","
 S FDA(TT,IEN,.01)=PEI ; TABLE ELEMENT
 S FDA(TT,IEN,1)=CI ; COLUMN
 S FDA(TT,IEN,2)=SEQ ; SEQUENCE NUMBER
 S FDA(TT,IEN,3)=0 ; ALWAYS START AT 0
 S FDA(TT,IEN,4)="'{K}" ; STOP AT EXPRESSION
 S CI=$$PUT^DMSQU(IEN,"FDA","ERR")
 I $D(ERR) D ERR^DMSQU(F,SEQ,"INDEX PRIMARY KEY: TABLE ELEMENT INSERT FAILED")
 Q
FK(CI) ;DEFINE FOREIGN KEY FOR POINTER COLUMN CI
 N C,CEI,CE,CN,F,FI,TI,SP,FF,FIEN,FTI,FTN,FPEI,FPE,FPI,FNM,TT,IEN,FDA
 S C=^DMSQ("C",CI,0)
 S CEI=$P(C,U),CE=^DMSQ("E",CEI,0),CN=$P(CE,U) Q:$P(CE,U,2)'=13 ""
 S F=$P(C,U,5),FI=$P(C,U,6) I 'F!'FI Q ""
 S TI=$P(CE,U,3),SP=$P(^DD(F,FI,0),U,2)
 S FF=+$P(SP,"P",2)
 I 'FF D ERR^DMSQU(F,FI,"FOREIGN KEY: NO POINTED-TO FILE IN SPECIFIER") Q ""
 S FTI=$O(^DMSQ("T","C",FF,""))
 I 'FTI D ERR^DMSQU(F,FI,"FOREIGN KEY: NO TABLE FOR POINTED-TO FILE") Q ""
 S FTN=$P(^DMSQ("T",FTI,0),U)
 S FPEI=$O(^DMSQ("E","F",FTI,"P",""))
 I 'FPEI D ERR^DMSQU(F,FI,"FOREIGN KEY: NO PRIMARY KEY TABLE ELEMENT") Q ""
 S FPE=^DMSQ("E",FPEI,0),FDI=$P(FPE,U,2)
 S FPI=$O(^DMSQ("P","B",FPEI,""))
 I 'FPI D ERR^DMSQU(F,FI,"FOREIGN KEY: NO ASSOCIATED PRIMARY KEY") Q ""
 S FCI=$P(^DMSQ("P",FPI,0),U,2)
 S FNM=CN_"_FK",IEN=$O(^DMSQ("E","G",TI,FNM,""))
 ;BUILD FOREIGN KEY TABLE ELEMENT
 S TT=1.5216,IEN=$S(IEN:IEN,1:"+1")_","
 S FDA(TT,IEN,.01)=FNM ;FOREIGN KEY NAME
 S FDA(TT,IEN,1)=FDI ;DOMAIN = FOREIGN KEY TABLE DOMAIN
 S FDA(TT,IEN,2)=TI ;TABLE ID
 S FDA(TT,IEN,3)="F" ;TYPE F = FOREIGN KEY
 S FDA(TT,IEN,4)="Foreign key to "_FTN ;COMMENT
 S FIEN=$$PUT^DMSQU(IEN,"FDA","ERR")
 I $D(ERR) D ERR^DMSQU(F,FI,"FOREIGN KEY: TABLE ELEMENT INSERT FAILED") Q ""
 ;BUILD FOREIGN KEY COLUMN ELEMENT
 S TT=1.5219,IEN=$O(^DMSQ("F","B",FIEN,"")),IEN=$S(IEN:IEN,1:"+1")_","
 S FDA(TT,IEN,.01)=FIEN ;FK TABLE ELEMENT ID
 S FDA(TT,IEN,1)=FPI ;FOREIGN TABLE PRIMARY KEY ELEMENT ID
 S FDA(TT,IEN,2)=CI ;COLUMN ID IN LOCAL TABLE
 S FIEN=$$PUT^DMSQU(IEN,"FDA","ERR")
 I $D(ERR) D ERR^DMSQU(F,FI,"FOREIGN KEY: COLUMN ELEMENT INSERT FAILED")
 Q FIEN