-
Notifications
You must be signed in to change notification settings - Fork 1
/
ROOK.m
330 lines (330 loc) · 10 KB
/
ROOK.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
ROOK
D REPL^ROOKTUI($ZCOMMAND)
Q
INIT
;
; SUBROUTINE DATA
;
N RESULT,RETURN,CHARSET,COLL
Q:$D(^ROOK("INIT"))
;
; PRINT BANNER AND SET CHARSET AND COLLATION
;
W !,!,"ROOK Enterprise Server - System Setup",!
W " Copyright (C) 2013 Coherent Logic Development LLC",!,!,!
D SETCFG("CHARSET","M")
D SETCFG("COLLATION","M")
D SETCFG("PASSWORD_HASH","MD5")
;
; POPULATE DD KEY TO INFORMATION_SCHEMA MAPPINGS
;
S ^ROOK("DDISX","EMBRYO","TYPE")="COLUMNS.DATA_TYPE"
S ^ROOK("DDISX","EMBRYO","INPUTXFORM")="COLUMNS.INPUT_TRANSFORM"
S ^ROOK("DDISX","EMBRYO","OUTPUTXFORM")="COLUMNS.OUTPUT_TRANSFORM"
S ^ROOK("DDISX","EMBRYO","PATTERN")="COLUMNS.PATTERN"
S ^ROOK("DDISX","EMBRYO","PRIMARY")="COLUMNS.PRIMARY"
S ^ROOK("DDISX","EMBRYO","UNIQUE")="COLUMNS.UNIQUE"
S ^ROOK("DDISX","EMBRYO","REQUIRED")="COLUMNS.REQUIRED"
S ^ROOK("DDISX","EMBRYO","MAXLENGTH")="COLUMNS.CHARACTER_MAXIMUM_LENGTH"
;
; POPULATE ERROR LOOKUP GLOBAL
;
S ^RKERRXREF(1)="NEST ALREADY EXISTS"
S ^RKERRXREF(1,"S")="ERROR"
S ^RKERRXREF(2)="ACCESS DENIED"
S ^RKERRXREF(2,"S")="ERROR"
S ^RKERRXREF(3)="DATA UNIQUENESS VIOLATION"
S ^RKERRXREF(3,"S")="ERROR"
S ^RKERRXREF(4)="PATTERN MATCH FAILURE"
S ^RKERRXREF(4,"S")="ERROR"
S ^RKERRXREF(5)="REQUIRED FIELD MISSING"
S ^RKERRXREF(5,"S")="ERROR"
S ^RKERRXREF(6)="DATA TYPE VIOLATION"
S ^RKERRXREF(6,"S")="ERROR"
S ^RKERRXREF(7)="NEST DOES NOT EXIST"
S ^RKERRXREF(7,"S")="ERROR"
S ^RKERRXREF(8)="EMBRYO ALREADY EXISTS"
S ^RKERRXREF(8,"S")="ERROR"
S ^RKERRXREF(9)="EMBRYO DOES NOT EXIST"
S ^RKERRXREF(9,"S")="ERROR"
S ^RKERRXREF(10)="NO NEST SELECTED"
S ^RKERRXREF(10,"S")="ERROR"
S ^RKERRXREF(11)="EGG ALREADY EXISTS"
S ^RKERRXREF(11,"S")="ERROR"
S ^RKERRXREF(12)="EGG DOES NOT EXIST"
S ^RKERRXREF(12,"S")="ERROR"
S ^RKERRXREF(13)="NO CONNECTION TO ROOK"
S ^RKERRXREF(13,"S")="ERROR"
S ^RKERRXREF(14)="PRIMARY KEY EMBRYO MUST BE REQUIRED AND UNIQUE"
S ^RKERRXREF(14,"S")="ERROR"
S ^RKERRXREF(15)="INVALID NEST, EGG, OR EMBRYO IDENTIFIER"
S ^RKERRXREF(15,"S")="ERROR"
S ^RKERRXREF(16)="INVALID DATA TYPE IN EMBRYO DEFINITION"
S ^RKERRXREF(16,"S")="ERROR"
S ^RKERRXREF(17)="INVALID VALUE FOR 'UNIQUE' IN EMBRYO DEFINITION"
S ^RKERRXREF(17,"S")="ERROR"
S ^RKERRXREF(18)="INVALID VALUE FOR 'REQUIRED' IN EMBRYO DEFINITION"
S ^RKERRXREF(18,"S")="ERROR"
S ^RKERRXREF(19)="INVALID VALUE FOR 'PRIMARY' IN EMBRYO DEFINITION"
S ^RKERRXREF(19,"S")="ERROR"
S ^RKERRXREF(20)="FAILED TO SECURE LOCK"
S ^RKERRXREF(20,"S")="ERROR"
S ^RKERRXREF(21)="'MAXLENGTH' MUST BE SPECIFIED FOR 'STRING' EMBRYOS"
S ^RKERRXREF(21,"S")="ERROR"
S ^RKERRXREF(22)="STRING DATA EXCEEDS 'MAXLENGTH'"
S ^RKERRXREF(22,"S")="ERROR"
S ^RKERRXREF(23)="SYNTAX ERROR IN SQL STATEMENT"
S ^RKERRXREF(23,"S")="ERROR"
S ^RKERRXREF(24)="SQL STATEMENT UNSUPPORTED"
S ^RKERRXREF(24,"S")="ERROR"
;
; FOOL ROOK INTO THINKING WE'RE CONNECTED
;
S ^ROOK("SESSION",$J,"NEST")="INFOSCHEMA"
S ^ROOK("SESSION",$J,"USERNAME")="SYSTEM"
S ^ROOK("SESSION",$J,"CACHE_HITS_DISK")=0
S ^ROOK("SESSION",$J,"CACHE_HITS_MEMORY")=0
S ^ROOK("SESSION",$J,"CACHE_MISSES")=0
S ^ROOK("SESSION",$J,"START")=$H
S (RKIN,RKOUT)="",(RKRESULT,RKQIDX)=0,RKFLAGS="V",RKQUEUE=""
S RKTIMERCT=0
;
; CREATE INFORMATION_SCHEMA NEST
;
S RETURN=$$MKNEST^ROOKDD("INFOSCHEMA",.RESULT)
;
; CREATE CHARACTER_SETS TABLE
;
N EGGDEF,EMBDEF
S EGGDEF("CHARACTER_SETS","ENTRYNUMBER")=1
S EGGDEF("CHARACTER_SETS","EMBRYOS")=""
S RETURN=$$MKEGG^ROOKDD(.EGGDEF,.RESULT)
S EMBDEF("CHARACTER_SET_NAME","TYPE")="STRING"
S EMBDEF("CHARACTER_SET_NAME","MAXLENGTH")=80
S EMBDEF("DEFAULT_COLLATE_NAME","TYPE")="STRING"
S EMBDEF("DEFAULT_COLLATE_NAME","MAXLENGTH")=80
S RETURN=$$MKEMBRYO^ROOKDD("CHARACTER_SETS",.EMBDEF,.RESULT)
;
; CREATE USERS TABLE
;
K EGGDEF,EMBDEF
S EGGDEF("USERS","ENTRYNUMBER")=1
S EGGDEF("USERS","EMBRYOS")=""
S RETURN=$$MKEGG^ROOKDD(.EGGDEF,.RESULT)
S EMBDEF("USERNAME","TYPE")="STRING"
S EMBDEF("USERNAME","MAXLENGTH")=255
S EMBDEF("PASSWORD","TYPE")="STRING"
S EMBDEF("PASSWORD","MAXLENGTH")=255
S EMBDEF("PASSWORD","INPUTXFORM")="S RKOUT=$$PWIXF^ROOKAUTH(RKIN)"
S EMBDEF("ENABLED","TYPE")="BOOLEAN"
S RETURN=$$MKEMBRYO^ROOKDD("USERS",.EMBDEF,.RESULT)
;
; CREATE COLUMNS TABLE
;
K EGGDEF,EMBDEF
S EGGDEF("COLUMNS","ENTRYNUMBER")=1
S EGGDEF("COLUMNS","EMBRYOS")=""
S RETURN=$$MKEGG^ROOKDD(.EGGDEF,.RESULT)
S EMBDEF("INPUT_TRANSFORM","TYPE")="STRING"
S EMBDEF("INPUT_TRANSFORM","MAXLENGTH")=4096
S EMBDEF("OUTPUT_TRANSFORM","TYPE")="STRING"
S EMBDEF("OUTPUT_TRANSFORM","MAXLENGTH")=4096
S EMBDEF("UNIQUE","TYPE")="BOOLEAN"
S EMBDEF("REQUIRED","TYPE")="BOOLEAN"
S EMBDEF("PRIMARY","TYPE")="BOOLEAN"
S EMBDEF("PATTERN","TYPE")="STRING"
S EMBDEF("PATTERN","MAXLENGTH")=255
S EMBDEF("TABLE_CATALOG","TYPE")="STRING"
S EMBDEF("TABLE_CATALOG","MAXLENGTH")=80
S EMBDEF("TABLE_SCHEMA","TYPE")="STRING"
S EMBDEF("TABLE_SCHEMA","MAXLENGTH")=80
S EMBDEF("TABLE_NAME","TYPE")="STRING"
S EMBDEF("TABLE_NAME","MAXLENGTH")=80
S EMBDEF("COLUMN_NAME","TYPE")="STRING"
S EMBDEF("COLUMN_NAME","MAXLENGTH")=80
S EMBDEF("ORDINAL_POSITION","TYPE")="STRING"
S EMBDEF("ORDINAL_POSITION","MAXLENGTH")=80
S EMBDEF("COLUMN_DEFAULT","TYPE")="STRING"
S EMBDEF("COLUMN_DEFAULT","MAXLENGTH")=1024
S EMBDEF("IS_NULLABLE","TYPE")="BOOLEAN"
S EMBDEF("DATA_TYPE","TYPE")="STRING"
S EMBDEF("DATA_TYPE","MAXLENGTH")=10
S EMBDEF("CHARACTER_MAXIMUM_LENGTH","TYPE")="NUMBER"
S EMBDEF("CHARACTER_OCTET_LENGTH","TYPE")="NUMBER"
;
; NUMERIC PRECISION IN ROOK IS ONLY LIMITED BY MUMPS,
; THEREFORE, WE USE AN INPUT TRANSFORM TO FORCE
; INFORMATION_SCHEMA.COLUMNS.NUMERIC_PRECISION TO
; ALWAYS BE 17, IGNORING RKIN
;
S EMBDEF("NUMERIC_PRECISION","TYPE")="NUMBER"
S EMBDEF("NUMERIC_PRECISION","INPUTXFORM")="S RKOUT=17"
S EMBDEF("NUMERIC_SCALE","TYPE")="NUMBER"
S EMBDEF("DATETIME_PRECISION","TYPE")="NUMBER"
S EMBDEF("CHARACTER_SET_NAME","TYPE")="STRING"
S EMBDEF("CHARACTER_SET_NAME","MAXLENGTH")=80
S RETURN=$$MKEMBRYO^ROOKDD("COLUMNS",.EMBDEF,.RESULT)
;
; CREATE SCHEMATA TABLE
;
K EGGDEF,EMBDEF
S EGGDEF("SCHEMATA","ENTRYNUMBER")=1
S EGGDEF("SCHEMATA","EMBRYOS")=""
S RETURN=$$MKEGG^ROOKDD(.EGGDEF,.RESULT)
S EMBDEF("CATALOG_NAME","TYPE")="STRING"
S EMBDEF("CATALOG_NAME","MAXLENGTH")=80
S EMBDEF("SCHEMA_NAME","TYPE")="STRING"
S EMBDEF("SCHEMA_NAME","MAXLENGTH")=80
S EMBDEF("DEFAULT_CHARACTER_SET_NAME","TYPE")="STRING"
S EMBDEF("DEFAULT_CHARACTER_SET_NAME","MAXLENGTH")=80
S EMBDEF("DEFAULT_COLLATION_NAME","TYPE")="STRING"
S EMBDEF("DEFAULT_COLLATION_NAME","MAXLENGTH")=80
S EMBDEF("SQL_PATH","TYPE")="STRING"
S EMBDEF("SQL_PATH","MAXLENGTH")=80
S RETURN=$$MKEMBRYO^ROOKDD("SCHEMATA",.EMBDEF,.RESULT)
;
; CREATE TABLES TABLE
;
K EGGDEF,EMBDEF
S EGGDEF("TABLES","ENTRYNUMBER")=1
S EGGDEF("TABLES","EMBRYOS")=""
S RETURN=$$MKEGG^ROOKDD(.EGGDEF,.RESULT)
S EMBDEF("TABLE_CATALOG","TYPE")="STRING"
S EMBDEF("TABLE_CATALOG","MAXLENGTH")=80
S EMBDEF("TABLE_SCHEMA","TYPE")="STRING"
S EMBDEF("TABLE_SCHEMA","MAXLENGTH")=80
S EMBDEF("NEXT_ENTRY_NUMBER","TYPE")="NUMBER"
S EMBDEF("TABLE_NAME","TYPE")="STRING"
S EMBDEF("TABLE_NAME","MAXLENGTH")=80
S EMBDEF("TABLE_TYPE","TYPE")="STRING"
S EMBDEF("TABLE_TYPE","MAXLENGTH")=80
S RETURN=$$MKEMBRYO^ROOKDD("TABLES",.EMBDEF,.RESULT)
;
; SET UP THE "SYSTEM" USER
;
N AURET,AURES,SYSPASSWD
W !,"SYSTEM ACCOUNT PASSWORD: "
U $P:NOECHO R SYSPASSWD U $P:ECHO
S AURET=$$ADDUSER^ROOKAUTH("SYSTEM",SYSPASSWD,1,.AURES)
S ^ROOK("INIT")=""
Q
;
CONNECT(USERNAME,PASSWORD,FLAGS,RESULT)
N AUTHSTAT S AUTHSTAT=0
N DAT S DAT(0)=1
S (RKIN,RKOUT)="",(RKRESULT,RKQIDX)=0,RKFLAGS=FLAGS,RKQUEUE=""
S RKTIMERCT=0
; TODO: authentication here
D SET^ROOKSESSION("USERNAME","BASE") ;set username to BASE so we can have a fake connection while authenticating the real one
S AUTHSTAT=$$AUTHUSER^ROOKAUTH(USERNAME,PASSWORD)
I AUTHSTAT=1 D
. D SET^ROOKSESSION("USERNAME",USERNAME)
. D SET^ROOKSESSION("CACHE_HITS_MEMORY",0)
. D SET^ROOKSESSION("CACHE_HITS_DISK",0)
. D SET^ROOKSESSION("CACHE_MISSES",0)
. D SET^ROOKSESSION("SESSION_START",$H)
. D MSG("")
. D MSG("ROOK Enterprise Server")
. D MSG(" Copyright (C) 2013 Coherent Logic Development LLC")
. D MSG("")
. D MSG("ROOK: ATTACHED "_USERNAME_" TO SESSION "_$J)
. D SUCCESS(.RESULT,0,0,0,.DAT)
E D
. D ERR(2,.RESULT)
Q RKRESULT
;
DISCONNECT()
;
; MERGE PROCESS-LOCAL QUERY CACHE INTO GLOBAL QUERY CACHE
;
M ^RKQCACHE=RKQCACHE
;
; KILL THE SESSION
;
M ^ROOK("SESSION",$J_" (DISCONNECTED)")=^ROOK("SESSION",$J)
K ^ROOK("SESSION",$J)
Q
CONNECTED()
Q $D(^ROOK("SESSION",$J))
;
GETCFG(KEY)
Q $G(^ROOK("CONFIGURATION",KEY))
;
SETCFG(KEY,VALUE)
LOCK (^ROOK("CONFIGURATION",KEY)):30
I $TEST D
. S ^ROOK("CONFIGURATION",KEY)=VALUE
. LOCK
Q
;
USE(NEST,RESULT)
I NEST="INFORMATION_SCHEMA" S NEST="INFOSCHEMA"
N DAT S DAT(0)=1
N NERES,NERET
S NERET=$$NESTEXISTS^ROOKDD(NEST,.NERES)
I NERES("RETURN")>0 D
. S ^ROOK("SESSION",$J,"NEST")=NEST,RKRESULT=1
. D SUCCESS(.RESULT,0,0,0,.DAT)
E D
. D ERR(7,.RESULT)
Q RKRESULT
;
USED()
Q $D(^ROOK("SESSION",$J,"NEST"))
;
CURRENTNEST()
Q $G(^ROOK("SESSION",$J,"NEST"),"")
;
ERR(CODE,RESULT)
N DAT S DAT(0)=1
S RESULT("ERROR","CODE")=CODE
S RESULT("ERROR","MESSAGE")=^RKERRXREF(CODE)
S RESULT("ERROR","SEVERITY")=^RKERRXREF(CODE,"S")
S RESULT("QUERYTIME")=0
S RESULT("RECORDCOUNT")=0
S RESULT("RETURN")=""
M RESULT("DATA")=DAT
I $$FLAG("V") D
. D MSG("ERROR "_CODE_" (SEVERITY: "_RESULT("ERROR","SEVERITY")_"): "_RESULT("ERROR","MESSAGE"))
S RKRESULT=0
Q
;
SUCCESS(RESULT,RETURN,QUERYTIME,RECORDCOUNT,DATA)
S RESULT("ERROR","CODE")=0
S RESULT("ERROR","MESSAGE")=""
S RESULT("ERROR","SEVERITY")=""
S RESULT("QUERYTIME")=QUERYTIME
S RESULT("RECORDCOUNT")=RECORDCOUNT
S RESULT("RETURN")=RETURN
I $$FLAG("V") D
. D MSG("QUERY OK. "_RECORDCOUNT_" ROWS AFFECTED. ("_QUERYTIME_" SECONDS)")
M RESULT("DATA")=DATA
S RKRESULT=1
Q
;
FLAG(FLG)
I $G(RKFLAGS,"")="" S RKFLAGS="V"
Q RKFLAGS[FLG
;
MSG(MSG)
I $$FLAG("V") D
. W MSG,!
Q
;
BTIME()
N RET
S RKTIMERS(RKTIMERCT)=$P($H,",",2)
S RET=RKTIMERCT
S RKTIMERCT=RKTIMERCT+1
Q RET
;
ETIME(TIMER)
N RET,CTIME
S CTIME=$P($H,",",2)
Q CTIME-RKTIMERS(TIMER)
;
VALNAME(NAME)
N RES S RES=1
S INV="~!@#$%^&*()+=-`{}|[]\:;'<>?,./ "
I NAME[INV S RES=0
Q RES