[1147] | 1 | BMXPRS ; IHS/OIT/HMW - BMX WINDOWS UTILS ;
|
---|
| 2 | ;;4.1000;BMX;;Apr 17, 2011
|
---|
| 3 | ;
|
---|
| 4 | ;
|
---|
| 5 | PARSE(X) ;EP-Parse SQL Statement into array
|
---|
| 6 | ;Input SQL statement as X
|
---|
| 7 | ;Returns BMXTK() array
|
---|
| 8 | ;Errors returned in BMXERR
|
---|
| 9 | ;
|
---|
| 10 | D PRE
|
---|
| 11 | Q:$D(BMXERR)
|
---|
| 12 | D POST
|
---|
| 13 | Q
|
---|
| 14 | ;
|
---|
| 15 | POST2 ;EP - Remove commas from BMXTK
|
---|
| 16 | N J,K
|
---|
| 17 | S J=0 F S J=$O(BMXTK(J)) Q:'+J D
|
---|
| 18 | . S K=$O(BMXTK(J))
|
---|
| 19 | . I +K,","=$G(BMXTK(K)) D
|
---|
| 20 | . . K BMXTK(K)
|
---|
| 21 | . . D PACK(J)
|
---|
| 22 | . . Q
|
---|
| 23 | . Q
|
---|
| 24 | Q
|
---|
| 25 | ;
|
---|
| 26 | POST ;
|
---|
| 27 | ;Combine multi-character operators
|
---|
| 28 | N J
|
---|
| 29 | S J=0 F S J=$O(BMXTK(J)) Q:'+J D
|
---|
| 30 | . I ">"=BMXTK(J) D Q
|
---|
| 31 | . . I "="[$G(BMXTK(J+1)) D Q
|
---|
| 32 | . . . S BMXTK(J)=BMXTK(J)_"="
|
---|
| 33 | . . . K BMXTK(J+1)
|
---|
| 34 | . . . D PACK(J)
|
---|
| 35 | . . I "<"[$G(BMXTK(J+1)) D Q
|
---|
| 36 | . . . S BMXTK(J)="<"_BMXTK(J)
|
---|
| 37 | . . . K BMXTK(J+1)
|
---|
| 38 | . . . D PACK(J)
|
---|
| 39 | . I "<"=BMXTK(J) D Q
|
---|
| 40 | . . I "=>"[$G(BMXTK(J+1)) D
|
---|
| 41 | . . . S BMXTK(J)=BMXTK(J)_BMXTK(J+1)
|
---|
| 42 | . . . K BMXTK(J+1)
|
---|
| 43 | . . . D PACK(J)
|
---|
| 44 | . I "="=BMXTK(J) D Q
|
---|
| 45 | . . I "<>"[$G(BMXTK(J+1)) D
|
---|
| 46 | . . . S BMXTK(J)=BMXTK(J+1)_BMXTK(J)
|
---|
| 47 | . . . K BMXTK(J+1)
|
---|
| 48 | . . . D PACK(J)
|
---|
| 49 | Q
|
---|
| 50 | ;
|
---|
| 51 | PACK(J) ;
|
---|
| 52 | F S J=$O(BMXTK(J)) Q:'+J D
|
---|
| 53 | . S BMXTK(J-1)=BMXTK(J)
|
---|
| 54 | . K BMXTK(J)
|
---|
| 55 | Q
|
---|
| 56 | ;
|
---|
| 57 | PRE N P,T,Q,Q1,A,B S (P,T,Q)=0,BMXTK="",A=0
|
---|
| 58 | START S A=A+1
|
---|
| 59 | S B=$E(X,A)
|
---|
| 60 | I B="" G B5
|
---|
| 61 | I 'Q G QUOTE
|
---|
| 62 | I B=$C(39) G QUOTE
|
---|
| 63 | S BMXTK=BMXTK_B G START
|
---|
| 64 | QUOTE I B'=$C(39) G SPACE
|
---|
| 65 | I Q G QUOTE2
|
---|
| 66 | ;S Q=1,BMXTK=B G START
|
---|
| 67 | S Q=1,BMXTK=BMXTK_B G START
|
---|
| 68 | QUOTE2 S Q1=B,A=A+1,B=$E(X,A)
|
---|
| 69 | I B']"" G QUOTE3
|
---|
| 70 | I B'=$C(39) G QUOTE3
|
---|
| 71 | S BMXTK=BMXTK_Q1_B G START
|
---|
| 72 | QUOTE3 S A=A-1,B=Q1,BMXTK=BMXTK_B,Q=0 G START
|
---|
| 73 | SPACE I B'=" " G OP
|
---|
| 74 | I BMXTK]"" S T=T+1,BMXTK(T)=BMXTK,BMXTK=""
|
---|
| 75 | G START
|
---|
| 76 | OP I "=><"'[B G OPAREN
|
---|
| 77 | I BMXTK]"" S T=T+1,BMXTK(T)=BMXTK,BMXTK=""
|
---|
| 78 | S T=T+1,BMXTK(T)=B,BMXTK=""
|
---|
| 79 | G START
|
---|
| 80 | OPAREN I B'="(" G CPAREN
|
---|
| 81 | S P=P+1
|
---|
| 82 | I BMXTK]"" S T=T+1,BMXTK(T)=BMXTK,BMXTK=""
|
---|
| 83 | S T=T+1,BMXTK(T)=B G START
|
---|
| 84 | CPAREN I B'=")" G B2
|
---|
| 85 | I P G B1
|
---|
| 86 | G B0
|
---|
| 87 | ;
|
---|
| 88 | B0 S BMXERR="SQL SYNTAX ERROR" D ERROR G B5
|
---|
| 89 | B1 S P=P-1
|
---|
| 90 | I BMXTK]"" S T=T+1,BMXTK(T)=BMXTK,BMXTK=""
|
---|
| 91 | S T=T+1,BMXTK(T)=B G START
|
---|
| 92 | B2 I B'="," G B3
|
---|
| 93 | S T=T+1,BMXTK(T)=BMXTK,T=T+1,BMXTK(T)=",",BMXTK="" G START
|
---|
| 94 | B3 S BMXTK=BMXTK_B
|
---|
| 95 | B4 G START
|
---|
| 96 | B5 I BMXTK]"" S T=T+1,BMXTK(T)=BMXTK
|
---|
| 97 | I $D(BMXERR) G B6
|
---|
| 98 | I P S BMXERR="SQL SYNTAX ERROR: MATCHING PARENTHESIS NOT FOUND" D ERROR
|
---|
| 99 | E I Q S BMXERR="SQL SYNTAX ERROR: MATCHING QUOTE NOT FOUND" D ERROR
|
---|
| 100 | I P>0 G START
|
---|
| 101 | B6 Q
|
---|
| 102 | ;
|
---|
| 103 | ERROR ;W !,"ERROR=",BMXERR,! Q
|
---|
| 104 | Q
|
---|