-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_library.sql
892 lines (726 loc) · 21 KB
/
json_library.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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
/*
This is a home-built json extension library. Its something that was started from scratch and needs plenty of work. Some functions are experiments; some are placeholders.
*/
/*
*/
CREATE OR REPLACE FUNCTION jsonb_build(
IN key_in text
, IN value_in anyelement
, OUT json_out JSONB
)
AS
$$
BEGIN
json_out := jsonb_build_object(key_in,value_in);
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_build(TEXT, ANYELEMENT) IS 'Builds JSONB from TEXT and ANYELEMENT. This function is intended to be used by custom operator(s).';
CREATE OR REPLACE FUNCTION jsonb_build(
IN key_in text
, IN value_in text
, OUT json_out JSONB
)
AS
$$
BEGIN
json_out := jsonb_build_object(key_in,value_in);
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_build(TEXT, TEXT) IS 'Builds JSONB from TEXT and TEXT. This function is intended to be used by custom operator(s).';
CREATE OR REPLACE FUNCTION jsonb_build(
IN key_in TEXT
, IN value_in JSONB
, OUT json_out JSONB
)
AS
$$
BEGIN
json_out := jsonb_build_object(key_in,value_in);
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_build(TEXT, JSONB) IS 'Builds JSONB from TEXT and JSONB. This function is intended to be used by custom operator(s).';
DROP OPERATOR IF EXISTS +> (text, text);
CREATE OPERATOR +> (
PROCEDURE = jsonb_build,
LEFTARG = text,
RIGHTARG = text,
COMMUTATOR = +>
)
;
COMMENT ON OPERATOR +> (TEXT, TEXT) IS 'This is a shorthand operator that builds a JSONB object from the left key and right text.';
DROP OPERATOR IF EXISTS +> (text, jsonb);
CREATE OPERATOR +> (
PROCEDURE = jsonb_build,
LEFTARG = text,
RIGHTARG = jsonb,
COMMUTATOR = +>
)
;
COMMENT ON OPERATOR +> (TEXT, JSONB) IS 'This is a shorthand operator that builds a JSONB object from the left key and right JSONB.';
DROP OPERATOR IF EXISTS +> (text, anyelement); -- though this takes "anyelement", text and JSON types will first be matched with their dedicated functions for proper handling. Everything else gets this function.
CREATE OPERATOR +> (
PROCEDURE = jsonb_build,
LEFTARG = text,
RIGHTARG = anyelement,
COMMUTATOR = +>
)
;
COMMENT ON OPERATOR +> (TEXT, ANYELEMENT) IS 'This is a shorthand operator that builds a JSONB object from the left key and right value.';
/*
*/
CREATE OR REPLACE FUNCTION jsonb_as_numeric(
IN json_in JSONB
, IN key_in TEXT
, OUT json_out NUMERIC
)
AS
$$
BEGIN
json_out := (json_in->>key_in)::NUMERIC;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_as_numeric(JSONB, TEXT) IS 'Gets value from object cast as NUMERIC. This function is intended to be used by custom operator(s).';
DROP OPERATOR IF EXISTS ->## (jsonb, text);
CREATE OPERATOR ->## (
PROCEDURE = jsonb_as_numeric,
LEFTARG = jsonb,
RIGHTARG = text,
COMMUTATOR = ->##
)
;
COMMENT ON OPERATOR ->## (JSONB, TEXT) IS 'This is a shorthand operator that returns the numeric value from the left object per the key given by right operator.';
CREATE OR REPLACE FUNCTION jsonb_as_int(
IN json_in JSONB
, IN key_in TEXT
, OUT json_out INT
)
AS
$$
BEGIN
json_out := (json_in->>key_in)::INT;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_as_int(JSONB, TEXT) IS 'This is a shorthand operator that gets value from object cast as INT. This function is intended to be used by custom operator(s).';
DROP OPERATOR IF EXISTS -># (jsonb, text);
CREATE OPERATOR -># (
PROCEDURE = jsonb_as_int,
LEFTARG = jsonb,
RIGHTARG = text,
COMMUTATOR = ->#
)
;
COMMENT ON OPERATOR -># (JSONB, TEXT) IS 'This is a shorthand operator that returns the integer value from the left object per the key given by right operator.';
-- This has questionable functionality and need.
CREATE OR REPLACE FUNCTION jsonb_as_text_array(
IN json_in JSONB
, IN key_in TEXT
, OUT text_out TEXT[]
)
AS
$$
BEGIN
text_out := (json_in->key_in)::TEXT[];
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_as_text_array(JSONB, TEXT) IS 'This is a shorthand operator that gets value from object cast as TEXT[]. This function is intended to be used by custom operator(s).';
DROP OPERATOR IF EXISTS ->>& (jsonb, text);
CREATE OPERATOR ->>& (
PROCEDURE = jsonb_as_text_array,
LEFTARG = jsonb,
RIGHTARG = text
)
;
COMMENT ON OPERATOR ->>& (JSONB, TEXT) IS 'This is a shorthand operator that returns the text array from the left object per the key given by right operator.';
CREATE OR REPLACE FUNCTION jsonb_as_boolean(
IN json_in JSONB
, IN key_in TEXT
, OUT json_out BOOLEAN
)
AS
$$
BEGIN
json_out := (json_in->>key_in)::BOOLEAN;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_as_boolean(JSONB, TEXT) IS 'This is a shorthand operator that gets value from object cast as BOOLEAN. This function is intended to be used by custom operator(s).';
DROP OPERATOR IF EXISTS ->? (jsonb, text);
CREATE OPERATOR ->? (
PROCEDURE = jsonb_as_boolean,
LEFTARG = jsonb,
RIGHTARG = text,
COMMUTATOR = ->?
)
;
COMMENT ON OPERATOR ->? (JSONB, TEXT) IS 'This is a shorthand operator that returns the boolean value from the left object per the key given by right operator.';
CREATE OR REPLACE FUNCTION jsonb_as_date(
IN json_in JSONB
, IN key_in TEXT
, OUT json_out DATE
)
AS
$$
BEGIN
json_out := (json_in->>key_in)::date;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_as_date(JSONB, TEXT) IS 'This is a shorthand operator that gets value from object cast as DATE. This function is intended to be used by custom operator(s).';
DROP OPERATOR IF EXISTS ->@ (jsonb, text);
CREATE OPERATOR ->@ (
PROCEDURE = jsonb_as_date,
LEFTARG = jsonb,
RIGHTARG = text,
COMMUTATOR = ->@
)
;
COMMENT ON OPERATOR ->@ (JSONB, TEXT) IS 'This is a shorthand operator that returns the date from the left object per the key given by right operator.';
/*The native pgs function for json_agg() becomes invalid whenever a NULL or empty array is appended, which is buggy, so I made this patch.*/
CREATE OR REPLACE FUNCTION json_agg_statef (IN json_cur_in JSONB, IN json_next_in JSONB, OUT json_cur_out JSONB) AS
$$
BEGIN
json_cur_out := json_cur_in & json_next_in;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
DROP AGGREGATE IF EXISTS json_agg (JSONB);
CREATE AGGREGATE json_agg (JSONB)
(
SFUNC = json_agg_statef,
STYPE = JSONB
)
;
COMMENT ON AGGREGATE json_agg(JSONB) IS 'Replaces the factory-shipped version of this function with one that ignores NULLs and empty objects.';
/*
*/
CREATE OR REPLACE FUNCTION json_agg_array_statef (IN json_cur_in JSONB, IN json_next_in JSONB, OUT json_cur_out JSONB) AS
$$
BEGIN
CASE
WHEN json_cur_in IS NULL THEN
json_cur_out := json_next_in;
ELSE
json_cur_out := json_build_array(json_next_in, json_cur_in);
END CASE;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
DROP AGGREGATE IF EXISTS json_agg_array (JSONB);
CREATE AGGREGATE json_agg_array (JSONB)
(
SFUNC = json_agg_array_statef,
STYPE = JSONB
)
;
COMMENT ON FUNCTION json_agg_array(JSONB) IS 'Safely aggregates json using the standard json_build_array() function but ignoring NULL objects.';
/*
*/
CREATE OR REPLACE FUNCTION json_select_keys_if_exist( -- selects multiple keys
IN json_in JSONB
, IN key_list TEXT[]
, OUT json_out JSONB
)
AS
$$
DECLARE
key_name TEXT;
BEGIN
FOREACH key_name IN ARRAY key_list LOOP
IF json_in ? key_name THEN
json_out := json_out & (json_in # key_name);
END IF;
END LOOP;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION json_select_keys_if_exist(JSONB, TEXT[]) IS 'Returns the object for the given key name(s) text array from the given JSONB array. This function is intended to be used by custom operator(s).';
-- separate function than "json_select_key_if_exists()" because combining them requires a polymorphic input and that requires usage to explicitly cast variables which is a pain
DROP OPERATOR IF EXISTS #& (JSONB, TEXT[]);
CREATE OPERATOR #& (
PROCEDURE = json_select_keys_if_exist
, LEFTARG = JSONB
, RIGHTARG = TEXT[]
)
;
COMMENT ON OPERATOR #& (JSONB, TEXT[]) IS 'This is a shorthand operator that returns the top-level key-value pairs in left object which are mentioned in the right key-name text array.';
/*
*/
CREATE OR REPLACE FUNCTION json_select_key_if_exists( -- selects one key
IN json_in JSONB
, IN json_key TEXT
, OUT json_out JSONB
)
AS
$$
DECLARE
BEGIN
CASE
WHEN json_in ? json_key
THEN
json_out := (json_key +> (json_in #> array[json_key]));
RETURN;
ELSE
json_out := NULL;
RETURN;
END CASE;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION json_select_key_if_exists(JSONB, TEXT) IS 'Returns the object for the given key name from the given JSONB array. This function is intended to be used by custom operator(s).';
DROP OPERATOR IF EXISTS # (JSONB, text);
CREATE OPERATOR # (
PROCEDURE = json_select_key_if_exists
, LEFTARG = JSONB
, RIGHTARG = text
)
;
COMMENT ON OPERATOR # (JSONB, TEXT) IS 'This is a shorthand operator that returns the top-level key-value pair in left object of the right key-name text.';
/*
*/
-- This function differs from that of the pgs native || functionality in that if either constitiutent is NULL, then the result is the other constituent. This makes it easy to append without needing to know whether the appended object is NULL or not.
CREATE OR REPLACE FUNCTION json_append(
IN json_a JSONB
, IN json_b JSONB
, OUT json_out JSONB
)
AS $$
DECLARE
json_empty JSONB := '{}'::JSONB;
BEGIN
json_out := COALESCE(json_a,json_empty) || COALESCE(json_b,json_empty);
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION json_append(JSONB, JSONB) IS 'Replaces the factory-shipped version of this function with one that ignores NULLs and empty objects.';
DROP OPERATOR IF EXISTS & (JSONB, JSONB);
CREATE OPERATOR & (
PROCEDURE = json_append
, LEFTARG = JSONB
, RIGHTARG = JSONB
)
;
COMMENT ON OPERATOR & (JSONB, JSONB) IS 'This is a shorthand operator that appends the left JSONB object to the right JSONB object.';
/*
*/
CREATE OR REPLACE FUNCTION json_setrsert(IN JSONB, IN TEXT[], IN ANYELEMENT, IN BOOLEAN DEFAULT TRUE) RETURNS JSONB AS
$$
BEGIN
IF ($1 #> $2) IS NULL
THEN return jsonb_set(COALESCE($1,'{}'), $2[:(cardinality($2)-1)], to_jsonb(array[$3]), TRUE);
ELSE return jsonb_insert($1, $2, to_jsonb($3), $4);
END IF;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION json_setrsert(JSONB, TEXT[], ANYELEMENT, BOOLEAN) IS 'Inserts a value into the array of the first argument at the destination designated by the second argument. It creates the array if it does not exist already. This is a combination of the native functions jsonb_set and jsonb_insert';
/*
*/
CREATE OR REPLACE FUNCTION jsonb_table_insert(
IN target_schema TEXT
, IN target_table TEXT
, IN update_values JSONB
, IN flags JSONB DEFAULT '{}'
, OUT json_out JSONB
)
AS
$$
DECLARE
exec_string TEXT;
BEGIN
with
table_props as
(
select
column_name::text
, udt_name::text
, data_type::text
from information_schema.columns
where
table_name = target_table
and table_schema = target_schema
)
, insert_data as
(
select
key as column_name
, value as column_value
from jsonb_each_text(update_values)
)
, build as
(
select
string_agg(column_name, ', ') as column_name_list
, string_agg(quote_literal(column_value)::text || (CASE WHEN data_type = 'ARRAY' THEN '::JSONB'::text ELSE ''::TEXT END) || '::' || udt_name, ', ') as column_value_list
from insert_data
join table_props using (column_name) -- only the columns supplied in the JSON object will be pulled from the destination table
)
SELECT
'INSERT INTO ' || target_schema || '.' || target_table
|| ' (' || column_name_list || ')'
|| ' VALUES (' || column_value_list ||')'
|| ' RETURNING row_to_json(' || target_table || ')'
into exec_string
FROM build
;
IF flags->?'debug' THEN
json_out := 'sql'+> exec_string; RETURN;
END IF;
EXECUTE exec_string into json_out;
END;
$$
LANGUAGE PLPGSQL
VOLATILE
;
COMMENT ON FUNCTION jsonb_table_insert(TEXT, TEXT, JSONB, JSONB) IS 'Emulates a single-row table insert query using json where the supplied object keys correspond to the destination table column names. The first argument is the destination schema, the second argument is the destination table, the third argument is the json with values for columns (invalid column names are ignored) and the fourth argument is for diagnostic flags. The response is the newly added row.';
/*
*/
CREATE OR REPLACE FUNCTION jsonb_table_update(
IN target_schema TEXT
, IN target_table TEXT
, IN target_row JSONB
, IN update_values JSONB
, IN flags JSONB DEFAULT '{}'
, OUT json_out JSONB
)
AS
$$
DECLARE
exec_string TEXT;
BEGIN
with
table_props as
(
select
column_name::text
, udt_name::text
, data_type::text
from information_schema.columns
where
table_name = target_table
and table_schema = target_schema
)
, insert_data as
(
select
key as column_name
, value as column_value
from jsonb_each_text(update_values)
)
, build as
(
select
string_agg(
column_name || ' = ' || COALESCE(
quote_literal(column_value) || (CASE WHEN data_type = 'ARRAY' THEN '::JSONB'::text ELSE ''::TEXT END) || '::' || udt_name
, 'NULL' -- for when when the value is actually NULL
)
, ', '
) as data_list
from insert_data
join table_props using (column_name) -- only the columns supplied in the JSON object will be pulled from the destination table
)
, wheres as
(
select
string_agg(key || ' = ' || quote_literal(value), ' and ') as wheres
from jsonb_each_text(target_row)
)
SELECT
'UPDATE ' || target_schema || '.' || target_table || ' SET ' || data_list || ' WHERE ' || wheres || ' RETURNING row_to_json(' || target_table || ')'
into exec_string
FROM build, wheres
;
IF flags->?'debug' THEN
json_out := 'sql'+> exec_string; return;
END IF;
EXECUTE exec_string INTO json_out;
END;
$$
LANGUAGE PLPGSQL
VOLATILE
;
COMMENT ON FUNCTION jsonb_table_update(TEXT, TEXT, JSONB, JSONB, JSONB) IS 'Emulates a single-row table update query using json where the supplied object keys correspond to the destination table column names. The first argument is the destination schema, the second argument is the destination table, the third argument is the json object to specify the where constraints, the fourth argument is the json object with values for updated columns (invalid column names are ignored) and the fith argument is for diagnostic flags. The response is the newly added row.';
/*
*/
CREATE OR REPLACE FUNCTION "text"(IN json_in JSONB) returns TEXT as
$$
SELECT (json_in#>>'{}')::TEXT;
$$
LANGUAGE sql
IMMUTABLE;
COMMENT ON FUNCTION "text"(JSONB) IS 'System function used for casting';
CREATE CAST (JSONB AS TEXT) WITH FUNCTION "text"(JSONB) AS ASSIGNMENT;
CREATE OR REPLACE FUNCTION text_array(IN json_array JSONB) returns TEXT[] as
$$
SELECT ARRAY(SELECT jsonb_array_elements_text(json_array));
$$
LANGUAGE sql
IMMUTABLE;
COMMENT ON FUNCTION text_array(JSONB) IS 'System function used for casting';
CREATE CAST (JSONB AS text[]) WITH FUNCTION text_array(JSONB) AS ASSIGNMENT;
CREATE OR REPLACE FUNCTION "int"(IN json_in JSONB) returns INT as
$$
SELECT (json_in#>>'{}')::INT;
$$
LANGUAGE sql
IMMUTABLE;
COMMENT ON FUNCTION "int"(JSONB) IS 'System function used for casting';
CREATE CAST (JSONB AS int) WITH FUNCTION "int"(JSONB) AS ASSIGNMENT;
CREATE OR REPLACE FUNCTION int_array(IN json_array JSONB) returns INT[] as
$$
SELECT ARRAY(SELECT jsonb_array_elements_text(json_array)::int);
$$
LANGUAGE sql
IMMUTABLE;
COMMENT ON FUNCTION int_array(JSONB) IS 'System function used for casting';
CREATE CAST (JSONB AS int[]) WITH FUNCTION int_array(JSONB) AS ASSIGNMENT;
CREATE OR REPLACE FUNCTION int_array(IN json_in JSONB, IN text_in TEXT) returns INT[] as
$$
SELECT (json_in->text_in)::int[];
$$
LANGUAGE sql
IMMUTABLE;
COMMENT ON FUNCTION int_array(JSONB, TEXT) IS 'Gets value from object cast as INT[]. This function is intended to be used by custom operator(s).';
CREATE OPERATOR ->#& (
PROCEDURE = "int_array",
LEFTARG = JSONB,
RIGHTARG = TEXT,
COMMUTATOR = ->#&
)
;
COMMENT ON OPERATOR ->#& (JSONB, TEXT) IS 'Returns the integer array value from the left object per the key given by right operator.';
CREATE OR REPLACE FUNCTION "numeric"(IN json_in JSONB) returns NUMERIC as
$$
SELECT (json_in#>>'{}')::NUMERIC;
$$
LANGUAGE sql
IMMUTABLE;
COMMENT ON FUNCTION "numeric"(JSONB) IS 'System function used for casting';
CREATE CAST (JSONB AS NUMERIC) WITH FUNCTION "numeric"(JSONB) AS ASSIGNMENT;
/*
*/
DROP OPERATOR IF EXISTS ** (jsonb, none);
CREATE OPERATOR ** (
PROCEDURE = jsonb_pretty
, LEFTARG = jsonb
)
;
COMMENT ON OPERATOR ** (JSONB, NONE) IS 'This is a shorthand operator that returns the left object made pretty.';
/*
*/
CREATE OR REPLACE FUNCTION jsonb_array_append(
IN json_base JSONB
, IN json_wedge JSONB
, OUT json_out JSONB
) AS
$$
DECLARE
wedge TEXT;
comma TEXT := ',';
BEGIN
CASE
WHEN jsonb_typeof(json_wedge) = 'string'
THEN wedge := quote_ident(json_wedge::text);
WHEN json_wedge IS NULL OR json_wedge::text = '{}' OR json_wedge::text = '[]'
THEN
json_out := json_base;
return;
WHEN json_base IS NULL OR json_base::text = '{}' OR json_base::text = '[]'
THEN
comma := '';
wedge := json_wedge::text;
ELSE
wedge := json_wedge::text;
END CASE;
CASE jsonb_typeof(json_base)::text
WHEN 'array' THEN
json_out := (left(json_base::text,-1)+comma+wedge+']')::jsonb
return;
WHEN 'object' THEN
IF jsonb_typeof(json_wedge) = 'array'
THEN RAISE EXCEPTION 'Cannot append object and array types';
RETURN;
END IF;
json_out := (left(json_base::text,-1)+comma+right(json_wedge::text,-1))::jsonb
return;
ELSE
RAISE EXCEPTION 'Unusual case "%" not found.', jsonb_typeof(json_base)::text;
RETURN;
END CASE;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_array_append(JSONB, JSONB) IS 'Inteligently appends the second argument to the first depending on what datatype the second is.';
/*
*/
CREATE OR REPLACE FUNCTION jsonb_accepted_keys(
IN value_in JSONB
, IN keys TEXT[]
)
RETURNS BOOLEAN AS
$$
BEGIN
RETURN
(
SELECT
count(*)= 0
FROM jsonb_object_keys(value_in)
WHERE
NOT array[jsonb_object_keys] <@ keys
)
;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_accepted_keys(JSONB, TEXT[]) IS 'Returns false if any of the keys provided in the first JSON argument are not in the second TEXT[] argument (in otherwords: unaccepted).';
DROP OPERATOR IF EXISTS ?&! (JSONB, TEXT[]);
CREATE OPERATOR ?&! (
PROCEDURE = jsonb_accepted_keys,
LEFTARG = JSONB,
RIGHTARG = TEXT[]
)
;
COMMENT ON OPERATOR ?&! (JSONB, TEXT[]) IS 'This is a shorthand operator that returns FALSE if any of the keys provided in the left object are not specified in the left text array.';
/*
*/
CREATE OR REPLACE FUNCTION jsonb_keys_coexist(
IN value_in JSONB
, IN keys TEXT[]
)
RETURNS BOOLEAN AS
$$
BEGIN
RETURN
(
SELECT
count(*) > 1
FROM jsonb_object_keys(value_in)
WHERE
jsonb_object_keys <@ keys
)
;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_keys_coexist(JSONB, TEXT[]) IS 'This is a shorthand operator that Returns TRUE if *more than one* of the specified keys in the second argument exists at the top level of the first argument.';
/*
*/
CREATE OR REPLACE FUNCTION cast_to_jsonb(IN TEXT) RETURNS JSONB AS
$$
BEGIN
return $1::jsonb;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION cast_to_jsonb(TEXT) IS 'System function used for casting';
CREATE OPERATOR @ (
PROCEDURE = cast_to_jsonb,
RIGHTARG = TEXT
)
;
COMMENT ON OPERATOR @ (NONE, TEXT) IS 'This is a shorthand operator that casts the right text into JSONB.';
/*
*/
CREATE TYPE key_value AS
(
key TEXT
, value TEXT
)
;
CREATE OR REPLACE FUNCTION jsonb_each_casted(
IN json_in JSONB
) RETURNS SETOF key_value AS
$$
DECLARE
BEGIN
RETURN QUERY
select
key
, CASE
WHEN jsonb_typeof(jbe.value) = 'array' THEN replace(replace(jbet.value::text,'[','{'),']','}')
WHEN jsonb_typeof(jbe.value) = 'string' THEN jbet.value
WHEN jsonb_typeof(jbe.value) = 'null' THEN NULL
ELSE jbet.value
END
from jsonb_each(json_in) jbe
join jsonb_each_text(json_in) jbet using (key)
;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_each_casted(JSONB) IS 'System function used for casting.';
/*
*/
CREATE OR REPLACE FUNCTION jsonb_delta(
IN json_left JSONB
, IN json_right JSONB
, OUT json_out JSONB
) AS
$$
BEGIN
with
base as
(
select
key
, case when a.value != b.value then ('left'+>a.value)&('right'+>b.value) ELSE NULL END as changes
from jsonb_each(json_left) a
join jsonb_each(json_right) b using (key)
)
select
json_agg(key+>changes)
into json_out
from base
where
changes IS NOT NULL
;
END;
$$
LANGUAGE PLPGSQL
IMMUTABLE
;
COMMENT ON FUNCTION jsonb_delta(JSONB, JSONB) IS 'Computes a diff between the first and second JSONB arguments.';