-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathROOKSQL.m
181 lines (174 loc) · 4.39 KB
/
ROOKSQL.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
ROOKSQL
Q
;
EXEC(SQL,RESULT)
;
; SUBROUTINE DATA
;
N DATA,TIMER,QTIME,RCNT,ERRCT S TIMER=$$BTIME^ROOK,(ERRCT,RCNT)=0
;
; LEXER STUFF
;
N GDELIM,GDELINC,TERMS S GDELIM=" ;",GDELINC=""
N CDELIM,CDELINC,LIST S CDELIM=";,",CDELINC=""
K TERMS S TERMS=""
;
; REQUIRE CONNECTION TO ROOK
;
I '$$CONNECTED^ROOK D
. D ERR^ROOK(13,.RESULT)
. S ERRCT=$I(ERRCT)
G:ERRCT>0 QERR
S RKRESULT=1
;
; FIND OUT WHAT SQL STATEMENT AND JUMP ACCORDINGLY
;
D LEX(SQL,.TERMS,GDELIM,GDELINC)
N SQLSTMT S SQLSTMT=$$UCASE($P(SQL," ",1))
G:SQLSTMT="USE" QUSE
G:SQLSTMT="SHOW" QSHOW
G:SQLSTMT="SELECT" QSELECT
G:SQLSTMT="INSERT" QINSERT
G:SQLSTMT="UPDATE" QUPDATE
G:SQLSTMT="DELETE" QDELETE
;
; IF WE REACH THIS POINT, THE STATEMENT IS WRONG OR IS UNSUPPORTED
;
D ERR^ROOK(23,.RESULT) G QERR
QUSE ;USE
N NEST S NEST=$P(SQL," ",2),ERRCT=0
S RKRESULT=$$USE^ROOK(NEST,.RESULT)
I RKRESULT=1 D
. D MSG^ROOK("NEST CHANGED")
E D
. S ERRCT=$I(ERRCT)
G:ERRCT=0 QSUCCESS
G QERR
QSHOW ;SHOW
;
G QSUCCESS
QSELECT ;SELECT
N FIELDS,TABLES,PREDICATES,F,T,P
S F=TERMS(2),T=TERMS(4),P=TERMS(6)
F I=1:1:$L(F,",") D
. S FIELDS(I)=$P(F,",",I)
F I=1:1:$L(T,",") D
. S TABLES(I)=$P(T,",",I)
F I=1:1:$L(P,"=") D
. S PREDICATES(I)=$P(P,"=",I)
N SELRES
S RKRESULT=$$GETROWS^ROOKIDX(TABLES(1),PREDICATES(1),PREDICATES(2),.SELRES)
S RCNT=SELRES("RECORDCOUNT")
M DATA=SELRES("DATA")
G QSUCCESS
QINSERT ;INSERT
G QSUCCESS
QUPDATE ;UPDATE
G QSUCCESS
QDELETE ;DELETE
G QSUCCESS
QERR
S RKRESULT=0
QSUCCESS
S QTIME=$$ETIME^ROOK(TIMER)
I RKRESULT D SUCCESS^ROOK(.RESULT,0,QTIME,RCNT,.DATA)
Q RKRESULT
;
UCASE(STR)
Q $TR(STR,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")
;
LCASE(STR)
Q $TR(STR,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")
;
LEX(SQL,WORDS,DELIM,DELINC)
D SPLIT(SQL,.WORDS,DELIM,DELINC)
Q
OLDLEX(SQL,WORDS,DELIM,DELINC)
N INSTR S INSTR=0
N LEN S LEN=$L(SQL)
N WI S WI=0
N C,B,I S I=1,B=""
F Q:I>LEN D
. S C=$E(SQL,I)
. I '(DELIM[C) D
. . I '("'"[C) S B=B_C
. I C="'" D
. . I INSTR=1 D
. . . S WI=$I(WI)
. . . S WORDS(WI)=$P(B,"'",2)
. . . S B=""
. . . S INSTR=0
. . E D
. . . S INSTR=1
. I INSTR=0 D
. . I (DELIM[C)!(I=LEN) D
. . . ; W "INSTR=0,DELIM[C!I=LEN",!
. . . S WI=$I(WI)
. . . S WORDS(WI)=B
. . . ;I I=LEN S WORDS(WI)=B_C E S WORDS(WI)=B
. . . I DELINC[C S WI=$I(WI) S WORDS(WI)=C
. . . S B=""
. ;W "I=",I," C=",C," B='",B,"' WI=",WI," INSTR=",INSTR,!
. S I=$I(I)
Q
SPLIT(STRING,WORDS,DELIM,DELINC)
;
; We want to split SQL into array WORDS:
; WORDS are delimited by any character in DELIM
; Any character in DELINC will be put into WORDS as its own word
; Delimiters are disregarded in quoted strings
;
new inQuotedString,stringLength,currentChar,currentPos,currentWord,working
new appendThis,thisIncluded
;
; initialize everything
;
set (inQuotedString,currentWord,thisIncluded)=0,(appendThis,currentPos)=1
set stringLength=$l(STRING)
set (currentChar,working,WORDS)=""
;
for currentPos=1:1:stringLength do
. ;
. ; get the next character
. ;
. set currentChar=$extract(STRING,currentPos)
. ;
. ; are we in a string?
. ;
. if currentChar="'" do
. . set appendThis=0
. . if inQuotedString do ; this is the end quote of the string
. . . do installWord(.WORDS,.currentWord,.working)
. . . set inQuotedString=0
. . else do ; this is the start quote of the string
. . . set inQuotedString=1
. . . ; ; end of quoted string processing
. else do ; this is NOT a quote
. . if $$isDelimiter(currentChar,DELIM,inQuotedString) do ;this is a delimiter
. . . set appendThis=0
. . . do installWord(.WORDS,.currentWord,.working)
. . else do ;this is not a delimiter
. . . set appendThis=1
. ; end of specific non-quoted-string processing
. s thisIncluded=$$isIncluded(currentChar,DELINC)
. if thisIncluded do installWord(.WORDS,.currentWord,.working)
. if appendThis do
. . set working=working_currentChar
set WORDS(currentWord)=$get(WORDS(currentWord))_$extract(STRING,stringLength)
;. write "working length: '",$l(working),"' currentChar: '",currentChar,"' currentPos: '",currentPos,"' currentWord ; : ",currentWord," appendThis: ",appendThis," inQuotedString: ",inQuotedString," thisIncluded: ",thisIncluded,!
Q
;
installWord(WORDS,NUM,WORD)
S NUM=$I(NUM)
S WORDS(NUM)=WORD
S WORD=""
Q
;
isDelimiter(C,DELIM,inQuotedString)
Q:inQuotedString=1 0
Q:'(DELIM[C) 0
Q:(DELIM[C) 1
;
isIncluded(C,DELINC)
Q (DELINC[C)
;