-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclob_json.sql
544 lines (522 loc) · 17 KB
/
clob_json.sql
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
create or replace
function clob_json
/*******************************************************************************
* Copyright (c) 2013 Vladyslav Kozlovskyy
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
*
* Contributors:
* Vladyslav Kozlovskyy - dbdeveloper at rambler.ru
******************************************************************************/
-- PARAMETERS:
( I_json_clob IN clob )
return pairs
is
res pairs := pairs();
res_idx pls_integer := 0;
err_msg varchar2(200);
i pls_integer := 1;
--------------------------------------------------------------------------------
function fmt_err( err_id pls_integer
, p1 varchar2 := NULL
, p2 varchar2 := NULL
)
return varchar2
is
owner varchar2(30);
name varchar2(30);
lineno pls_integer;
caller_t varchar2(30);
who_called_me varchar2(100);
begin
OWA_UTIL.WHO_CALLED_ME(owner, name, lineno, caller_t);
who_called_me := ' (' || owner || '.' || name || ':' || lineno || ')';
case err_id
when 1 then
return ''''||p1||''' character is detected when one of '
||p2||' characters is expected.' || who_called_me;
when 2 then
return ''''||p1||''' character is detected when '''
||p2||''' is expected.' || who_called_me;
when 3 then
return p1||' is not terminated! '||p2||
' is expected but END OF TEXT is detected!' || who_called_me;
else
return 'unknown error ' || err_id || who_called_me;
end case;
end fmt_err;
--------------------------------------------------------------------------------
function parse_string( I_text IN clob
, I_pos IN OUT pls_integer
, I_term IN varchar2
, I_err_msg IN OUT varchar2
)
return varchar2
is
str varchar2(4000);
len pls_integer := length(I_text);
c varchar2(4);
begin
-- string
I_pos := I_pos +1;
while I_pos<=len loop
c := substr(I_text,I_pos,1);
if c = '\' then -- to use \x -> x transformation
I_pos := I_pos + 1;
c := substr(I_text,I_pos,1);
else
if c = I_term then
return str;
end if;
end if;
str := concat(str, c);
I_pos := I_pos + 1;
end loop;
I_err_msg := fmt_err(3, 'String', '''' || I_term || ''' character');
return NULL;
end parse_string;
--------------------------------------------------------------------------------
procedure parse_comment( I_text IN clob
, I_pos IN OUT pls_integer
, I_err_msg IN OUT varchar2
)
is
len pls_integer := length(I_text);
c varchar2(4);
begin
I_pos := I_pos + 1;
while I_pos<=len loop
if substr(I_text,I_pos,2) = '*/' then
I_pos := I_pos + 1;
return;
end if;
I_pos := I_pos + 1;
end loop;
I_err_msg := fmt_err(3, 'Comment', '''*/'' pair');
end parse_comment;
--------------------------------------------------------------------------------
procedure parse_obj( I_text in clob
, I_pos in out pls_integer
, I_res in out nocopy pairs
, I_res_idx in out pls_integer
, I_err_msg in out nocopy varchar2
, I_prefix in varchar2 := NULL
);
--------------------------------------------------------------------------------
procedure parse_array( I_text in clob
, I_pos in out pls_integer
, I_res in out nocopy pairs
, I_res_idx in out pls_integer
, I_err_msg in out nocopy varchar2
, I_prefix in varchar2 := NULL
)
is
res_rec pair := pair;
len pls_integer := length(I_text);
c varchar2(4);
str varchar2(2000);
L_array boolean := false;
L_idx pls_integer := 0;
begin
while I_pos<=len loop
c := substr(I_text, I_pos,1);
case c
when '[' then
if L_array then
if res_rec.value is NULL then
-- subarray
parse_array(I_text, I_pos, I_res, I_res_idx, I_err_msg, res_rec.name);
if I_err_msg is not NULL then
exit;
end if;
res_rec.value := NULL;
else
I_err_msg := fmt_err(1, '[', ''',''|'']''');
exit;
end if;
else
L_array := true;
res_rec.name := I_prefix || '[' || L_idx || ']';
end if;
when ']' then
if not L_array then
I_err_msg := fmt_err(2, ']', '[');
exit;
end if;
L_array := false;
if res_rec.value is not NULL then
I_res_idx := I_res_idx + 1;
I_res.EXTEND(1);
I_res(I_res_idx) := res_rec;
end if;
exit;
when ',' then
if not L_array then
I_err_msg := fmt_err(2, ',', '[');
exit;
end if;
if res_rec.value is not NULL then
I_res_idx := I_res_idx + 1;
I_res.EXTEND(1);
I_res(I_res_idx) := res_rec;
end if;
L_idx := L_idx + 1;
res_rec.name := I_prefix || '[' || L_idx || ']';
res_rec.value := NULL;
when '"' then
if not L_array then
I_err_msg := fmt_err(2, '"', '[');
exit;
end if;
if res_rec.value is not NULL then
I_err_msg := fmt_err(1, '"', ''',''|'']''');
exit;
end if;
res_rec.value := parse_string(I_text, I_pos, '"', I_err_msg);
when '''' then
if not L_array then
I_err_msg := fmt_err(2, '''', '[');
exit;
end if;
if res_rec.value is not NULL then
I_err_msg := fmt_err(1, '''', ''',''|'']''');
exit;
end if;
res_rec.value := parse_string(I_text, I_pos, '''', I_err_msg);
when '/' then
-- ? comment /* ... */ ?
if substr(I_text, I_pos+1, 1) = '*' then
parse_comment(I_text, I_pos, I_err_msg);
end if;
when '{' then
if not L_array then
I_err_msg := fmt_err(2, '{', '[');
exit;
end if;
if res_rec.value is not NULL then
I_err_msg := fmt_err(1, '{', ''',''|'']''');
exit;
end if;
parse_obj(I_text, I_pos, I_res, I_res_idx, I_err_msg, res_rec.name);
if I_err_msg is not NULL then
exit;
end if;
res_rec.value := NULL;
when ' ' then NULL;
when chr(9) then NULL;
when chr(10) then NULL;
else
if not L_array then
I_err_msg := fmt_err(2, c, '[');
exit;
end if;
if res_rec.value is not NULL then
I_err_msg := fmt_err(1, c, ''',''|'']''');
exit;
end if;
-- string
str := NULL;
while I_pos<=len loop
c := substr(I_text,I_pos,1);
exit when c in (',',']','/',' ',chr(9),chr(10));
str := concat(str, c);
I_pos := I_pos + 1;
end loop;
res_rec.value := str;
continue;
end case;
I_pos := I_pos + 1;
end loop;
if I_err_msg is NULL and L_array then
I_err_msg := fmt_err(3, 'Array', ''']'' character');
end if;
end parse_array;
--------------------------------------------------------------------------------
procedure parse_obj( I_text in clob
, I_pos in out pls_integer
, I_res in out nocopy pairs
, I_res_idx in out pls_integer
, I_err_msg in out nocopy varchar2
, I_prefix in varchar2 := NULL
)
is
LEFT_PART constant pls_integer := 0;
RIGHT_PART constant pls_integer := 1;
res_rec pair := pair;
res_start_idx pls_integer := I_res_idx;
len pls_integer := length(I_text);
c varchar2(4);
L_collection boolean := False;
L_part pls_integer; -- 0 - left part (name), 1 - right part (value)
str varchar2(2000);
begin
while I_pos<=len loop
c := substr(I_text, I_pos,1);
case c
when '{' then
if L_collection then
if L_part = LEFT_PART then
-- ERROR!
I_err_msg := fmt_err(1, '{', '''"''|alpha_numeric_characters|''}''');
exit;
else -- RIGHT_PART then
-- recursive call of this program
parse_obj(I_text, I_pos, I_res, I_res_idx, I_err_msg,
concat( case when I_prefix is not NULL
then concat(I_prefix, '.')
else NULL
end
, res_rec.name
)
);
if I_err_msg is not NULL then
exit;
end if;
end if;
else
L_collection := true;
L_part := LEFT_PART;
end if;
res_rec.name := NULL;
res_rec.value := NULL;
when '[' then
if not L_collection then
I_err_msg := fmt_err(2, '[', '{');
exit;
end if;
if L_part = RIGHT_PART then
if res_rec.value is NULL then
if res_rec.name is not NULL then
-- array
parse_array(I_text, I_pos, I_res, I_res_idx, I_err_msg,
concat( case when I_prefix is not NULL
then concat(I_prefix, '.')
else NULL
end
, res_rec.name
)
);
if I_err_msg is not NULL then
exit;
end if;
res_rec.name := NULL;
res_rec.value := NULL;
else
I_err_msg := fmt_err(1, '[', ''',''|''}''');
exit;
end if;
else
I_err_msg := fmt_err(1, '[', ''',''|''}''');
exit;
end if;
else -- LEFT_PART
if res_rec.name is not NULL then
I_err_msg := fmt_err(2, '[', ':');
exit;
else
I_err_msg := fmt_err(1, '[', 'name of a parameter or ''}''');
exit;
end if;
end if;
when '"' then
if not L_collection then
I_err_msg := fmt_err(2, '"', '{');
exit;
end if;
if L_part = LEFT_PART then
if res_rec.name is not NULL then
I_err_msg := fmt_err(2, '"', ':');
exit;
end if;
else
if res_rec.value is not NULL then
I_err_msg := fmt_err(1, '"', ''',''|''}''');
exit;
end if;
end if;
str := parse_string(I_text, I_pos, '"', I_err_msg);
if L_part = LEFT_PART then
res_rec.name := str;
else -- RIGHT_PART
res_rec.value := str;
end if;
when '/' then
-- ? comment /* ... */ ?
if substr(I_text, I_pos+1, 1) = '*' then
parse_comment(I_text, I_pos, I_err_msg);
end if;
when '''' then
if not L_collection then
I_err_msg := fmt_err(2, '''', '{');
exit;
end if;
if L_part = LEFT_PART then
if res_rec.name is not NULL then
I_err_msg := fmt_err(2, '''', ':');
exit;
end if;
else
if res_rec.value is not NULL then
I_err_msg := fmt_err(1, '''', ''',''|''}''');
exit;
end if;
end if;
str := parse_string(I_text, I_pos, '''', I_err_msg);
if L_part = LEFT_PART then
res_rec.name := str;
else -- RIGHT_PART
res_rec.value := str;
end if;
when ':' then
if not L_collection then
I_err_msg := fmt_err(2, ':', '{');
exit;
end if;
if L_part = LEFT_PART then
if res_rec.name is not NULL then
L_part := RIGHT_PART;
else
-- ERROR!
I_err_msg := fmt_err(1, ':', 'name of a parameter or ''}''');
exit;
end if;
else -- RIGHT_PART;
-- ERROR!
if res_rec.value is not NULL then
I_err_msg := fmt_err(1, ':', ''',''|''}''');
else
I_err_msg := fmt_err(1, ':', 'value string or ''}''');
end if;
exit;
end if;
when ',' then
if not L_collection then
I_err_msg := fmt_err(2, ',', '{');
exit;
end if;
if L_part = RIGHT_PART then
if res_rec.value is not NULL then
if I_prefix is not null then
res_rec.name := I_prefix || '.' || res_rec.name;
end if;
-- save pair in result table:
I_res_idx := I_res_idx + 1;
I_res.EXTEND(1);
I_res(I_res_idx) := res_rec;
L_part := LEFT_PART;
res_rec.name := NULL;
res_rec.value := NULL;
else
if res_rec.name is NULL then
-- special case: return from recurse (subcollections were
-- already inserted into I_res)
L_part := LEFT_PART;
else
I_err_msg := fmt_err(1, ',', 'value string or ''{''');
exit;
end if;
end if;
else -- LEFT_PART
if res_rec.name is NULL then
I_err_msg := fmt_err(1, ',', 'name of a parameter or ''}''');
else
I_err_msg := fmt_err(2, ',', ':');
end if;
exit;
end if;
when '}' then
if not L_collection then
I_err_msg := fmt_err(2, '}', '{');
exit;
end if;
if L_part = RIGHT_PART then
if res_rec.value is not NULL then
if I_prefix is not null then
res_rec.name := I_prefix || '.' || res_rec.name;
end if;
-- save pair in result table:
I_res_idx := I_res_idx + 1;
I_res.EXTEND(1);
I_res(I_res_idx) := res_rec;
L_collection := false;
exit;
else
if res_rec.name is NULL then
-- special case: return from recurse (subcollections were
-- already inserted into I_res)
L_collection := false;
exit;
else
I_err_msg := fmt_err(1, '}', 'value string');
exit;
end if;
end if;
else
if res_rec.name is NULL then
if res_start_idx = I_res_idx then
-- empty collection
L_collection := false;
exit;
else
I_err_msg := fmt_err(1, '}', 'name of a parameter');
exit;
end if;
else
I_err_msg := fmt_err(2, '}', ':');
exit;
end if;
end if;
when ' ' then NULL;
when chr(9) then NULL;
when chr(10) then NULL;
else -- any other characters
if not L_collection then
I_err_msg := fmt_err(2, c, '{');
exit;
end if;
if L_part = LEFT_PART then
if res_rec.name is not NULL then
I_err_msg := fmt_err(2, c, ':');
exit;
end if;
else -- RIGHT_PART
if res_rec.value is not NULL then
I_err_msg := fmt_err(1, c, ''',''|''}''');
exit;
end if;
end if;
-- string
str := NULL;
while I_pos<=len loop
c := substr(I_text,I_pos,1);
if L_part = LEFT_PART then
exit when c in (':','/',' ',chr(9),chr(10));
else -- RIGHT_PART
exit when c in (',','/','}',' ',chr(9),chr(10));
end if;
str := concat( str, c);
I_pos := I_pos + 1;
end loop;
if L_part = LEFT_PART then
res_rec.name := str;
else -- RIGHT_PART
res_rec.value := str;
end if;
continue;
end case;
I_pos := I_pos + 1;
end loop;
if I_err_msg is NULL and L_collection then
I_err_msg := fmt_err(3, 'Object', '''}'' character');
end if;
end parse_obj;
begin
parse_obj(I_json_clob, i, res, res_idx, err_msg);
if err_msg is not NULL then
raise_application_error(-20000, 'ERROR: '||err_msg||' POSITION: '||i);
end if;
return res;
end clob_json;
/