forked from orafce/orafce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorafce--3.14--3.15.sql
632 lines (574 loc) · 20 KB
/
orafce--3.14--3.15.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
-- Translate Oracle regexp modifier into PostgreSQl ones
-- Append the global modifier if $2 is true. Used internally
-- by regexp_*() functions bellow.
CREATE OR REPLACE FUNCTION oracle.translate_oracle_modifiers(text, bool)
RETURNS text
AS $$
DECLARE
modifiers text;
BEGIN
-- Check that we don't have modifier not supported by Oracle
IF $1 ~ '[^icnsmx]' THEN
-- Modifier 's' is not supported by Oracle but it is a synonym
-- of 'n', we translate 'n' into 's' bellow. It is safe to allow it.
RAISE EXCEPTION 'argument ''flags'' has unsupported modifier(s).';
END IF;
-- Oracle 'n' modifier correspond to 's' POSIX modifier
-- Oracle 'm' modifier correspond to 'n' POSIX modifier
modifiers := translate($1, 'nm', 'sn');
IF $2 THEN
modifiers := modifiers || 'g';
END IF;
RETURN modifiers;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_LIKE( string text, pattern text) -> boolean
CREATE OR REPLACE FUNCTION oracle.regexp_like(text, text)
RETURNS boolean
AS $$
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
SELECT CASE WHEN (count(*) > 0) THEN true ELSE false END FROM regexp_matches($1, $2, 'p');
$$
LANGUAGE 'sql';
-- REGEXP_LIKE( string text, pattern text, flags text ) -> boolean
CREATE OR REPLACE FUNCTION oracle.regexp_like(text, text, text)
RETURNS boolean
AS $$
DECLARE
modifiers text;
BEGIN
modifiers := oracle.translate_oracle_modifiers($3, false);
IF (regexp_matches($1, $2, modifiers))[1] IS NOT NULL THEN
RETURN true;
END IF;
RETURN false;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_COUNT( string text, pattern text ) -> integer
CREATE OR REPLACE FUNCTION oracle.regexp_count(text, text)
RETURNS integer
AS $$
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
SELECT count(*)::integer FROM regexp_matches($1, $2, 'pg');
$$
LANGUAGE 'sql';
-- REGEXP_COUNT( string text, pattern text, position int ) -> integer
CREATE OR REPLACE FUNCTION oracle.regexp_count(text, text, integer)
RETURNS integer
AS $$
DECLARE
v_cnt integer;
BEGIN
-- Check numeric arguments
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
v_cnt := (SELECT count(*)::integer FROM regexp_matches(substr($1, $3), $2, 'pg'));
RETURN v_cnt;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_COUNT( string text, pattern text, position int, flags text ) -> integer
CREATE OR REPLACE FUNCTION oracle.regexp_count(text, text, integer, text)
RETURNS integer
AS $$
DECLARE
modifiers text;
v_cnt integer;
BEGIN
-- Check numeric arguments
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
modifiers := oracle.translate_oracle_modifiers($4, true);
v_cnt := (SELECT count(*)::integer FROM regexp_matches(substr($1, $3), $2, modifiers));
RETURN v_cnt;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_INSTR( string text, pattern text ) -> integer
CREATE OR REPLACE FUNCTION oracle.regexp_instr(text, text)
RETURNS integer
AS $$
DECLARE
v_pos integer;
v_pattern text;
BEGIN
-- Without subexpression specified, assume 0 which mean that the first
-- position for the substring matching the whole pattern is returned.
-- We need to enclose the pattern between parentheses.
v_pattern := '(' || $2 || ')';
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
v_pos := (SELECT position((SELECT (regexp_matches($1, v_pattern, 'pg'))[1] OFFSET 0 LIMIT 1) IN $1));
-- position() returns NULL when not found, we need to return 0 instead
IF v_pos IS NOT NULL THEN
RETURN v_pos;
END IF;
RETURN 0;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_INSTR( string text, pattern text, position int ) -> integer
CREATE OR REPLACE FUNCTION oracle.regexp_instr(text, text, integer)
RETURNS integer
AS $$
DECLARE
v_pos integer;
v_pattern text;
BEGIN
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
-- Without subexpression specified, assume 0 which mean that the first
-- position for the substring matching the whole pattern is returned.
-- We need to enclose the pattern between parentheses.
v_pattern := '(' || $2 || ')';
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
v_pos := (SELECT position((SELECT (regexp_matches(substr($1, $3), v_pattern, 'pg'))[1] OFFSET 0 LIMIT 1) IN $1));
-- position() returns NULL when not found, we need to return 0 instead
IF v_pos IS NOT NULL THEN
RETURN v_pos;
END IF;
RETURN 0;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_INSTR( string text, pattern text, position int, occurence int ) -> integer
CREATE OR REPLACE FUNCTION oracle.regexp_instr(text, text, integer, integer)
RETURNS integer
AS $$
DECLARE
v_pos integer;
v_pattern text;
BEGIN
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
IF $4 < 1 THEN
RAISE EXCEPTION 'argument ''occurence'' must be a number greater than 0';
END IF;
-- Without subexpression specified, assume 0 which mean that the first
-- position for the substring matching the whole pattern is returned.
-- We need to enclose the pattern between parentheses.
v_pattern := '(' || $2 || ')';
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
v_pos := (SELECT position((SELECT (regexp_matches(substr($1, $3), v_pattern, 'pg'))[1] OFFSET $4 - 1 LIMIT 1) IN $1));
-- position() returns NULL when not found, we need to return 0 instead
IF v_pos IS NOT NULL THEN
RETURN v_pos;
END IF;
RETURN 0;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_INSTR( string text, pattern text, position int, occurence int, return_opt int ) -> integer
CREATE OR REPLACE FUNCTION oracle.regexp_instr(text, text, integer, integer, integer)
RETURNS integer
AS $$
DECLARE
v_pos integer;
v_len integer;
v_pattern text;
BEGIN
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
IF $4 < 1 THEN
RAISE EXCEPTION 'argument ''occurence'' must be a number greater than 0';
END IF;
IF $5 != 0 AND $5 != 1 THEN
RAISE EXCEPTION 'argument ''return_opt'' must be 0 or 1';
END IF;
-- Without subexpression specified, assume 0 which mean that the first
-- Without subexpression specified, assume 0 which mean that the first
-- position for the substring matching the whole pattern is returned.
-- We need to enclose the pattern between parentheses.
v_pattern := '(' || $2 || ')';
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
v_pos := (SELECT position((SELECT (regexp_matches(substr($1, $3), v_pattern, 'pg'))[1] OFFSET $4-1 LIMIT 1) IN $1));
-- position() returns NULL when not found, we need to return 0 instead
IF v_pos IS NOT NULL THEN
IF $5 = 1 THEN
v_len := (SELECT length((SELECT (regexp_matches(substr($1, $3), v_pattern, 'pg'))[1] OFFSET $4 - 1 LIMIT 1)));
v_pos := v_pos + v_len;
END IF;
RETURN v_pos;
END IF;
RETURN 0;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_INSTR( string text, pattern text, position int, occurence int, return_opt int, flags text ) -> integer
CREATE OR REPLACE FUNCTION oracle.regexp_instr(text, text, integer, integer, integer, text)
RETURNS integer
AS $$
DECLARE
v_pos integer;
v_len integer;
modifiers text;
v_pattern text;
BEGIN
-- Check numeric arguments
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
IF $4 < 1 THEN
RAISE EXCEPTION 'argument ''occurence'' must be a number greater than 0';
END IF;
IF $5 != 0 AND $5 != 1 THEN
RAISE EXCEPTION 'argument ''return_opt'' must be 0 or 1';
END IF;
modifiers := oracle.translate_oracle_modifiers($6, true);
-- Without subexpression specified, assume 0 which mean that the first
-- position for the substring matching the whole pattern is returned.
-- We need to enclose the pattern between parentheses.
v_pattern := '(' || $2 || ')';
v_pos := (SELECT position((SELECT (regexp_matches(substr($1, $3), v_pattern, modifiers))[1] OFFSET $4 - 1 LIMIT 1) IN $1));
-- position() returns NULL when not found, we need to return 0 instead
IF v_pos IS NOT NULL THEN
IF $5 = 1 THEN
v_len := (SELECT length((SELECT (regexp_matches(substr($1, $3), v_pattern, modifiers))[1] OFFSET $4-1 LIMIT 1)));
v_pos := v_pos + v_len;
END IF;
RETURN v_pos;
END IF;
RETURN 0;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_INSTR( string text, pattern text, position int, occurence int, return_opt int, flags text, group int ) -> integer
CREATE OR REPLACE FUNCTION oracle.regexp_instr(text, text, integer, integer, integer, text, integer)
RETURNS integer
AS $$
DECLARE
v_pos integer := 0;
v_pos_orig integer := $3;
v_len integer := 0;
modifiers text;
occurrence integer := $4;
idx integer := 1;
v_curr_pos integer := 0;
v_pattern text;
v_subexpr integer := $7;
BEGIN
-- Check numeric arguments
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
IF $4 < 1 THEN
RAISE EXCEPTION 'argument ''occurence'' must be a number greater than 0';
END IF;
IF $7 < 0 THEN
RAISE EXCEPTION 'argument ''group'' must be a positive number';
END IF;
IF $5 != 0 AND $5 != 1 THEN
RAISE EXCEPTION 'argument ''return_opt'' must be 0 or 1';
END IF;
-- Translate Oracle regexp modifier into PostgreSQl ones
modifiers := oracle.translate_oracle_modifiers($6, true);
-- If subexpression value is 0 we need to enclose the pattern between parentheses.
IF v_subexpr = 0 THEN
v_pattern := '(' || $2 || ')';
v_subexpr := 1;
ELSE
v_pattern := $2;
END IF;
-- To get position of occurrence > 1 we need a more complex code
LOOP
v_curr_pos := v_curr_pos + v_len;
v_pos := (SELECT position((SELECT (regexp_matches(substr($1, v_pos_orig), '('||$2||')', modifiers))[1] OFFSET 0 LIMIT 1) IN substr($1, v_pos_orig)));
v_len := (SELECT length((SELECT (regexp_matches(substr($1, v_pos_orig), '('||$2||')', modifiers))[1] OFFSET 0 LIMIT 1)));
EXIT WHEN v_len IS NULL;
v_pos_orig := v_pos_orig + v_pos + v_len;
v_curr_pos := v_curr_pos + v_pos;
idx := idx + 1;
EXIT WHEN idx > occurrence;
END LOOP;
v_pos := (SELECT position((SELECT (regexp_matches(substr($1, v_curr_pos), v_pattern, modifiers))[v_subexpr] OFFSET 0 LIMIT 1) IN substr($1, v_curr_pos)));
IF v_pos IS NOT NULL THEN
IF $5 = 1 THEN
v_len := (SELECT length((SELECT (regexp_matches(substr($1, v_curr_pos), v_pattern, modifiers))[v_subexpr] OFFSET 0 LIMIT 1)));
v_pos := v_pos + v_len;
END IF;
RETURN v_pos + v_curr_pos - 1;
END IF;
RETURN 0;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_SUBSTR( string text, pattern text ) -> text
CREATE OR REPLACE FUNCTION oracle.regexp_substr(text, text)
RETURNS text
AS $$
DECLARE
v_substr text;
v_pattern text;
BEGIN
-- Without subexpression specified, assume 0 which mean that the first
-- position for the substring matching the whole pattern is returned.
-- We need to enclose the pattern between parentheses.
v_pattern := '(' || $2 || ')';
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
v_substr := (SELECT (regexp_matches($1, v_pattern, 'pg'))[1] OFFSET 0 LIMIT 1);
RETURN v_substr;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_SUBSTR( string text, pattern text, position int ) -> text
CREATE OR REPLACE FUNCTION oracle.regexp_substr(text, text, int)
RETURNS text
AS $$
DECLARE
v_substr text;
v_pattern text;
BEGIN
-- Check numeric arguments
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
-- Without subexpression specified, assume 0 which mean that the first
-- position for the substring matching the whole pattern is returned.
-- We need to enclose the pattern between parentheses.
v_pattern := '(' || $2 || ')';
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
v_substr := (SELECT (regexp_matches(substr($1, $3), v_pattern, 'pg'))[1] OFFSET 0 LIMIT 1);
RETURN v_substr;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_SUBSTR( string text, pattern text, position int, occurence int ) -> text
CREATE OR REPLACE FUNCTION oracle.regexp_substr(text, text, integer, integer)
RETURNS text
AS $$
DECLARE
v_substr text;
v_pattern text;
BEGIN
-- Check numeric arguments
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
IF $4 < 1 THEN
RAISE EXCEPTION 'argument ''occurence'' must be a number greater than 0';
END IF;
-- Without subexpression specified, assume 0 which mean that the first
-- position for the substring matching the whole pattern is returned.
-- We need to enclose the pattern between parentheses.
v_pattern := '(' || $2 || ')';
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
v_substr := (SELECT (regexp_matches(substr($1, $3), v_pattern, 'pg'))[1] OFFSET $4 - 1 LIMIT 1);
RETURN v_substr;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_SUBSTR( string text, pattern text, position int, occurence int, flags text ) -> text
CREATE OR REPLACE FUNCTION oracle.regexp_substr(text, text, integer, integer, text)
RETURNS text
AS $$
DECLARE
v_substr text;
v_pattern text;
modifiers text;
BEGIN
-- Check numeric arguments
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
IF $4 < 1 THEN
RAISE EXCEPTION 'argument ''occurence'' must be a number greater than 0';
END IF;
modifiers := oracle.translate_oracle_modifiers($5, true);
-- Without subexpression specified, assume 0 which mean that the first
-- position for the substring matching the whole pattern is returned.
-- We need to enclose the pattern between parentheses.
v_pattern := '(' || $2 || ')';
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
v_substr := (SELECT (regexp_matches(substr($1, $3), v_pattern, modifiers))[1] OFFSET $4 - 1 LIMIT 1);
RETURN v_substr;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_SUBSTR( string text, pattern text, position int, occurence int, flags text, group int ) -> text
CREATE OR REPLACE FUNCTION oracle.regexp_substr(text, text, integer, integer, text, int)
RETURNS text
AS $$
DECLARE
v_substr text;
v_pattern text;
modifiers text;
v_subexpr integer := $6;
has_group integer;
BEGIN
-- Check numeric arguments
IF $3 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
IF $4 < 1 THEN
RAISE EXCEPTION 'argument ''occurence'' must be a number greater than 0';
END IF;
IF v_subexpr < 0 THEN
RAISE EXCEPTION 'argument ''group'' must be a positive number';
END IF;
-- Check that with v_subexpr = 1 we have a capture group otherwise return NULL
has_group := (SELECT count(*) FROM regexp_matches(v_pattern, '\(.*\)'));
IF $6 = 1 AND has_group = 0 THEN
RETURN NULL;
END IF;
modifiers := oracle.translate_oracle_modifiers($5, true);
-- If subexpression value is 0 we need to enclose the pattern between parentheses.
IF v_subexpr = 0 THEN
v_pattern := '(' || $2 || ')';
v_subexpr := 1;
ELSE
v_pattern := $2;
END IF;
-- Oracle default behavior is newline-sensitive,
-- PostgreSQL not, so force 'p' modifier to affect
-- newline-sensitivity but not ^ and $ search.
v_substr := (SELECT (regexp_matches(substr($1, $3), v_pattern, modifiers))[v_subexpr] OFFSET $4 - 1 LIMIT 1);
RETURN v_substr;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_REPLACE( string text, pattern text, replace_string text ) -> text
CREATE OR REPLACE FUNCTION oracle.regexp_replace(text, text, text)
RETURNS text
AS $$
-- Oracle default behavior is to replace all occurence
-- whereas PostgreSQL only replace the first occurrence
-- so we need to add 'g' modifier.
SELECT pg_catalog.regexp_replace($1, $2, $3, 'g');
$$
LANGUAGE sql;
-- REGEXP_REPLACE( string text, pattern text, replace_string text, position int ) -> text
CREATE OR REPLACE FUNCTION oracle.regexp_replace(text, text, text, integer)
RETURNS text
AS $$
DECLARE
v_replaced_str text;
v_before text;
BEGIN
-- Check numeric arguments
IF $4 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
v_before = substr($1, 1, $4 - 1);
-- Oracle default behavior is to replace all occurence
-- whereas PostgreSQL only replace the first occurrence
-- so we need to add 'g' modifier.
v_replaced_str := v_before || pg_catalog.regexp_replace(substr($1, $4), $2, $3, 'g');
RETURN v_replaced_str;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_REPLACE( string text, pattern text, replace_string text, position int, occurence int ) -> text
CREATE OR REPLACE FUNCTION oracle.regexp_replace(text, text, text, integer, integer)
RETURNS text
AS $$
DECLARE
v_replaced_str text;
v_pos integer := $4;
v_before text := '';
v_nummatch integer;
BEGIN
-- Check numeric arguments
IF $4 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
IF $5 < 0 THEN
RAISE EXCEPTION 'argument ''occurrence'' must be a positive number';
END IF;
-- Check if the occurrence queried exceeds the number of occurrences
IF $5 > 1 THEN
v_nummatch := (SELECT count(*) FROM regexp_matches(substr($1, $4), $2, 'g'));
IF $5 > v_nummatch THEN
RETURN $1;
END IF;
-- Get the position of the occurrence we are looking for
v_pos := oracle.regexp_instr($1, $2, $4, $5, 0, '', 1);
IF v_pos = 0 THEN
RETURN $1;
END IF;
END IF;
-- Get the substring before this position we will need to restore it
v_before := substr($1, 1, v_pos - 1);
-- Replace all occurrences
IF $5 = 0 THEN
v_replaced_str := v_before || pg_catalog.regexp_replace(substr($1, v_pos), $2, $3, 'g');
ELSE
-- Replace the first occurrence
v_replaced_str := v_before || pg_catalog.regexp_replace(substr($1, v_pos), $2, $3);
END IF;
RETURN v_replaced_str;
END;
$$
LANGUAGE plpgsql;
-- REGEXP_REPLACE( string text, pattern text, replace_string text, position int, occurence int, flags text ) -> text
CREATE OR REPLACE FUNCTION oracle.regexp_replace(text, text, text, integer, integer, text)
RETURNS text
AS $$
DECLARE
v_replaced_str text;
v_pos integer := $4;
v_nummatch integer;
v_before text := '';
modifiers text := '';
BEGIN
-- Check numeric arguments
IF $4 < 1 THEN
RAISE EXCEPTION 'argument ''position'' must be a number greater than 0';
END IF;
IF $5 < 0 THEN
RAISE EXCEPTION 'argument ''occurrence'' must be a positive number';
END IF;
-- Set the modifiers
IF $5 = 0 THEN
modifiers := oracle.translate_oracle_modifiers($6, true);
ELSE
modifiers := oracle.translate_oracle_modifiers($6, false);
END IF;
-- Check if the occurrence queried exceeds the number of occurrences
IF $5 > 1 THEN
v_nummatch := (SELECT count(*) FROM regexp_matches(substr($1, $4), $2, $6||'g'));
IF $5 > v_nummatch THEN
RETURN $1;
END IF;
-- Get the position of the occurrence we are looking for
v_pos := oracle.regexp_instr($1, $2, $4, $5, 0, $6, 1);
IF v_pos = 0 THEN
RETURN $1;
END IF;
END IF;
-- Get the substring before this position we will need to restore it
v_before := substr($1, 1, v_pos - 1);
-- Replace occurrence(s)
v_replaced_str := v_before || pg_catalog.regexp_replace(substr($1, v_pos), $2, $3, modifiers);
RETURN v_replaced_str;
END;
$$
LANGUAGE plpgsql;