forked from doberkofler/PLSQL-JSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_debug.pkb
More file actions
309 lines (278 loc) · 9.42 KB
/
json_debug.pkb
File metadata and controls
309 lines (278 loc) · 9.42 KB
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
CREATE OR REPLACE
PACKAGE BODY json_debug
IS
PROCEDURE dumpRaw(theNodes IN json_nodes, theResult IN OUT NOCOPY json_debug.debugTableType);
PROCEDURE dump(theNodes IN json_nodes, theFirstNodeID IN NUMBER, theLevel IN OUT NUMBER, theResult IN OUT NOCOPY json_debug.debugTableType);
FUNCTION dump(theNodes IN json_nodes, theNodeID IN NUMBER, theLevel IN NUMBER) RETURN json_debug.debugRecordType;
FUNCTION lalign(theString IN VARCHAR2, theSize IN BINARY_INTEGER) RETURN VARCHAR2;
FUNCTION ralign(theString IN VARCHAR2, theSize IN BINARY_INTEGER) RETURN VARCHAR2;
----------------------------------------------------------
-- output
--
FUNCTION dump(theNode IN json_node, theNodeID IN NUMBER DEFAULT NULL) RETURN VARCHAR2
IS
BEGIN
RETURN CASE theNodeID IS NOT NULL WHEN TRUE THEN 'nodeID=('||theNodeID||') ' ELSE '' END ||
'typ=('||theNode.typ||') nam=('||theNode.nam||') par=('||theNode.par||') nex=('||theNode.nex||') sub=('||theNode.sub||') str=('||theNode.str||') num=('||theNode.num||')';
END dump;
----------------------------------------------------------
-- output
--
PROCEDURE output(theData IN json_value, theRawFlag IN BOOLEAN DEFAULT FALSE, theTitle IN VARCHAR2 DEFAULT NULL)
IS
aTitle VARCHAR2(32767) := theTitle;
BEGIN
IF (aTitle IS NOT NULL) THEN
aTitle := aTitle || ' - ';
END IF;
aTitle := aTitle || 'json_data('||theData.typ||')';
output(theNodes=>theData.nodes, theRawFlag=>theRawFlag, theTitle=>aTitle);
END output;
----------------------------------------------------------
-- output
--
PROCEDURE output(theObject IN json_object, theRawFlag IN BOOLEAN DEFAULT FALSE, theTitle IN VARCHAR2 DEFAULT NULL)
IS
aTitle VARCHAR2(32767) := theTitle;
BEGIN
IF (aTitle IS NOT NULL) THEN
aTitle := aTitle || ' - ';
END IF;
aTitle := aTitle || 'json_object';
output(theNodes=>theObject.nodes, theRawFlag=>theRawFlag, theTitle=>aTitle);
END output;
----------------------------------------------------------
-- output
--
PROCEDURE output(theArray IN json_array, theRawFlag IN BOOLEAN DEFAULT FALSE, theTitle IN VARCHAR2 DEFAULT NULL)
IS
aTitle VARCHAR2(32767) := theTitle;
BEGIN
IF (aTitle IS NOT NULL) THEN
aTitle := aTitle || ' - ';
END IF;
aTitle := aTitle || 'json_array';
output(theNodes=>theArray.nodes, theRawFlag=>theRawFlag, theTitle=>aTitle);
END output;
----------------------------------------------------------
-- output
--
PROCEDURE output(theNodes IN json_nodes, theRawFlag IN BOOLEAN DEFAULT FALSE, theTitle IN VARCHAR2 DEFAULT NULL)
IS
r json_debug.debugTableType := json_debug.debugTableType();
i BINARY_INTEGER;
l BINARY_INTEGER;
n BINARY_INTEGER := 1;
PROCEDURE output_line(theLevel IN VARCHAR2, theNo IN VARCHAR2, theNodeID IN VARCHAR2, theType IN VARCHAR2, theName IN VARCHAR2, theArrayIndex IN VARCHAR2, theParentID IN VARCHAR2, theNextID IN VARCHAR2, theSubNodeID IN VARCHAR2, theValue IN VARCHAR2)
IS
BEGIN
dbms_output.put_line( lalign(theLevel, 10) || ' ' ||
ralign(theNo, 5) || ' ' ||
ralign(theNodeID, 10) || ' ' ||
lalign(theType, 10) || ' ' ||
lalign(NVL(theName, '-'), 30) || ' ' ||
ralign(NVL(theArrayIndex, '-'), 5) || ' ' ||
ralign(NVL(theParentID, '-'), 10) || ' ' ||
ralign(NVL(theNextID, '-'), 10) || ' ' ||
ralign(NVL(theSubNodeID, '-'), 10) || ' ' ||
lalign(theValue, 60)
);
END output_line;
BEGIN
IF (theTitle IS NOT NULL) THEN
dbms_output.put_line('.');
dbms_output.put_line('---------- '||theTitle||' - BEGIN ----------');
END IF;
IF (theRawFlag) THEN
json_debug.dumpRaw(theNodes=>theNodes, theResult=>r);
ELSE
json_debug.dump(theNodes=>theNodes, theFirstNodeID=>theNodes.FIRST, theLevel=>l, theResult=>r);
END IF;
output_line('Level', '#', 'Node#', 'Type', 'Name', 'Index', 'Parent', 'Next', 'Sub', 'Value');
output_line('----------', '-----', '----------', '----------', '------------------------------', '-----', '----------', '----------', '----------', '------------------------------------------------------------');
i := r.FIRST;
WHILE (i IS NOT NULL) LOOP
output_line( theLevel => RPAD('*', NVL(r(i).nodeLevel, 0) + 1, '*'),
theNo => n,
theNodeID => r(i).nodeID,
theType => r(i).nodeType,
theArrayIndex => r(i).arrayIndex,
theValue => r(i).nodeValue,
theParentID => r(i).parentID,
theNextID => r(i).nextID,
theSubNodeID => r(i).subNodeID,
theName => r(i).nodeName
);
n := n + 1;
i := r.NEXT(i);
END LOOP;
IF (theTitle IS NOT NULL) THEN
dbms_output.put_line('---------- '||theTitle||' - END ------------');
dbms_output.put_line('.');
END IF;
END output;
----------------------------------------------------------
-- asTable
--
FUNCTION asTable(theNodes IN json_nodes, theRawFlag IN BOOLEAN DEFAULT FALSE) RETURN json_debug.debugTableType PIPELINED
IS
r json_debug.debugTableType := json_debug.debugTableType();
i BINARY_INTEGER;
l BINARY_INTEGER;
BEGIN
IF (theRawFlag) THEN
json_debug.dumpRaw(theNodes=>theNodes, theResult=>r);
ELSE
json_debug.dump(theNodes=>theNodes, theFirstNodeID=>theNodes.FIRST, theLevel=>l, theResult=>r);
END IF;
i := r.FIRST;
WHILE (i IS NOT NULL) LOOP
PIPE ROW(r(i));
i := r.NEXT(i);
END LOOP;
RETURN;
END asTable;
----------------------------------------------------------
-- dumpRaw (private)
--
PROCEDURE dumpRaw(theNodes IN json_nodes, theResult IN OUT NOCOPY json_debug.debugTableType)
IS
i BINARY_INTEGER;
BEGIN
i := theNodes.FIRST;
WHILE (i IS NOT NULL) LOOP
theResult.EXTEND(1);
theResult(theResult.LAST) := json_debug.dump(theNodes=>theNodes, theNodeID=>i, theLevel=>NULL);
i := theNodes.NEXT(i);
END LOOP;
END dumpRaw;
----------------------------------------------------------
-- dump (private)
--
PROCEDURE dump(theNodes IN json_nodes, theFirstNodeID IN NUMBER, theLevel IN OUT NUMBER, theResult IN OUT NOCOPY json_debug.debugTableType)
IS
l BINARY_INTEGER := NVL(theLevel, 0);
i BINARY_INTEGER := theFirstNodeID;
BEGIN
WHILE (i IS NOT NULL) LOOP
IF (theNodes(i).typ IN ('O', 'A')) THEN
theResult.EXTEND(1);
theResult(theResult.LAST) := json_debug.dump(theNodes=>theNodes, theNodeID=>i, theLevel=>l);
l := l + 1;
json_debug.dump(theNodes=>theNodes, theFirstNodeID=>theNodes(i).sub, theLevel=>l, theResult=>theResult);
l := l - 1;
ELSE
theResult.EXTEND(1);
theResult(theResult.LAST) := json_debug.dump(theNodes=>theNodes, theNodeID=>i, theLevel=>l);
END IF;
i := theNodes(i).nex;
END LOOP;
END dump;
----------------------------------------------------------
-- dump (private)
--
FUNCTION dump(theNodes IN json_nodes, theNodeID IN NUMBER, theLevel IN NUMBER) RETURN json_debug.debugRecordType
IS
r json_debug.debugRecordType;
FUNCTION getArrayIndex(theNodes IN json_nodes, theNodeID IN NUMBER) RETURN NUMBER
IS
p BINARY_INTEGER;
i BINARY_INTEGER;
x BINARY_INTEGER;
BEGIN
r.arrayIndex := NULL;
p := theNodes(theNodeID).par;
IF (p IS NOT NULL AND theNodes(p).typ = 'A') THEN
--dbms_output.put_line('node '||theNodeID||' is part of an an array');
x := 0;
i := theNodes(p).sub;
WHILE (i IS NOT NULL) LOOP
IF (i = theNodeID) THEN
RETURN x;
END IF;
x := x + 1;
i := theNodes(i).nex;
END LOOP;
END IF;
RETURN NULL;
END getArrayIndex;
BEGIN
-- type independent information
r.nodeLevel := theLevel;
r.nodeID := theNodeID;
r.nodeName := theNodes(theNodeID).nam;
r.parentID := theNodes(theNodeID).par;
r.nextID := theNodes(theNodeID).nex;
r.subNodeID := theNodes(theNodeID).sub;
-- compute the array index
r.arrayIndex := getArrayIndex(theNodes=>theNodes, theNodeID=>theNodeID);
-- type dependent information
CASE theNodes(theNodeID).typ
WHEN '0' THEN
r.nodeType := 'NULL';
r.nodeValue := NULL;
WHEN 'S' THEN
r.nodeType := 'STRING';
IF (theNodes(theNodeID).str IS NOT NULL) THEN
r.nodeValue := SUBSTR(theNodes(theNodeID).str, 1, 2000);
END IF;
WHEN 'N' THEN
r.nodeType := 'NUMBER';
IF (theNodes(theNodeID).num IS NOT NULL) THEN
r.nodeValue := TO_CHAR(theNodes(theNodeID).num);
END IF;
WHEN 'D' THEN
r.nodeType := 'DATE';
IF (theNodes(theNodeID).dat IS NOT NULL) THEN
r.nodeValue := TO_CHAR(theNodes(theNodeID).dat, 'YYYYMMDD HH24MISS');
END IF;
WHEN 'B' THEN
r.nodeType := 'BOOL';
IF (theNodes(theNodeID).num IS NOT NULL) THEN
r.nodeValue := CASE theNodes(theNodeID).num WHEN 1 THEN 'true' ELSE 'false' END;
END IF;
WHEN 'O' THEN
r.nodeType := '[OBJECT]';
WHEN 'A' THEN
r.nodeType := '[ARRAY]';
ELSE
r.nodeType := TO_CHAR(theNodes(theNodeID).typ);
END CASE;
RETURN r;
END dump;
----------------------------------------------------------
-- lalign (private)
--
FUNCTION lalign(theString IN VARCHAR2, theSize IN BINARY_INTEGER) RETURN VARCHAR2
IS
l BINARY_INTEGER := LENGTH(theString);
BEGIN
IF (theString IS NULL OR l IS NULL OR l = 0) THEN
RETURN RPAD(' ', theSize, ' ');
ELSIF (l = theSize) THEN
RETURN theString;
ELSIF (l > theSize) THEN
RETURN SUBSTR(theString, 1, theSize);
ELSE
RETURN RPAD(theString, theSize, ' ');
END IF;
END lalign;
----------------------------------------------------------
-- ralign (private)
--
FUNCTION ralign(theString IN VARCHAR2, theSize IN BINARY_INTEGER) RETURN VARCHAR2
IS
l BINARY_INTEGER := LENGTH(theString);
BEGIN
IF (theString IS NULL OR l IS NULL OR l = 0) THEN
RETURN RPAD(' ', theSize, ' ');
ELSIF (l = theSize) THEN
RETURN theString;
ELSIF (l > theSize) THEN
RETURN SUBSTR(theString, 1, theSize);
ELSE
RETURN LPAD(theString, theSize, ' ');
END IF;
END ralign;
END json_debug;
/