-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvrsn.sql
More file actions
8097 lines (6315 loc) · 251 KB
/
Copy pathvrsn.sql
File metadata and controls
8097 lines (6315 loc) · 251 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
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Disable check on function's body
SET check_function_bodies = off;
--
-- PostgreSQL database dump
--
-- Dumped from database version 15.7 (Ubuntu 15.7-0ubuntu0.23.10.1)
-- Dumped by pg_dump version 15.7 (Ubuntu 15.7-0ubuntu0.23.10.1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: vrsn; Type: SCHEMA; Schema: -; Owner: -
--
CREATE SCHEMA vrsn;
--
-- Name: bitemporal_record; Type: TYPE; Schema: vrsn; Owner: -
--
CREATE TYPE vrsn.bitemporal_record AS (
user_ts_range tstzrange,
db_ts_range tstzrange,
audit_record jsonb
);
--
-- Name: TYPE bitemporal_record; Type: COMMENT; Schema: vrsn; Owner: -
--
COMMENT ON TYPE vrsn.bitemporal_record IS 'type to manage bitemporal information and audit record';
--
-- Name: boolean_true_domain; Type: DOMAIN; Schema: vrsn; Owner: -
--
CREATE DOMAIN vrsn.boolean_true_domain AS boolean NOT NULL DEFAULT true;
--
-- Name: bt_audit_record; Type: DOMAIN; Schema: vrsn; Owner: -
--
CREATE DOMAIN vrsn.bt_audit_record AS jsonb NOT NULL;
--
-- Name: bt_db_ts_range; Type: DOMAIN; Schema: vrsn; Owner: -
--
CREATE DOMAIN vrsn.bt_db_ts_range AS tstzrange NOT NULL;
--
-- Name: bt_user_ts_range; Type: DOMAIN; Schema: vrsn; Owner: -
--
CREATE DOMAIN vrsn.bt_user_ts_range AS tstzrange NOT NULL;
--
-- Name: cached_attribute; Type: DOMAIN; Schema: vrsn; Owner: -
--
CREATE DOMAIN vrsn.cached_attribute AS jsonb;
--
-- Name: entity_fullname_type; Type: TYPE; Schema: vrsn; Owner: -
--
CREATE TYPE vrsn.entity_fullname_type AS (
schema_name text,
table_name text
);
--
-- Name: __entity_fullname_type__validate(vrsn.entity_fullname_type); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__entity_fullname_type__validate(st vrsn.entity_fullname_type) RETURNS boolean
LANGUAGE sql IMMUTABLE PARALLEL SAFE
BEGIN ATOMIC
SELECT (((st).schema_name ~ '^[a-zA-Z_][a-zA-Z0-9_]*$'::text) AND ((st).table_name ~ '^[a-zA-Z_][a-zA-Z0-9_]*$'::text));
END;
--
-- Name: entity_fullname_dmn; Type: DOMAIN; Schema: vrsn; Owner: -
--
CREATE DOMAIN vrsn.entity_fullname_dmn AS vrsn.entity_fullname_type
CONSTRAINT entity_fullname_dmn_check CHECK (vrsn.__entity_fullname_type__validate(VALUE));
--
-- Name: historice_entity_behaviour; Type: TYPE; Schema: vrsn; Owner: -
--
CREATE TYPE vrsn.historice_entity_behaviour AS ENUM (
'always',
'never',
'on_main_fields'
);
--
-- Name: TYPE historice_entity_behaviour; Type: COMMENT; Schema: vrsn; Owner: -
--
COMMENT ON TYPE vrsn.historice_entity_behaviour IS 'define the behaviour';
--
-- Name: table_field_details; Type: TYPE; Schema: vrsn; Owner: -
--
CREATE TYPE vrsn.table_field_details AS (
field_name text,
data_type text,
default_value text,
is_nullable boolean,
is_pk boolean,
pk_order integer,
table_order integer,
generation_type text,
complete_definition text
);
--
-- Name: tar_state_variables; Type: TYPE; Schema: vrsn; Owner: -
--
CREATE TYPE vrsn.tar_state_variables AS (
mitigate_conflicts boolean,
ignore_unchanged_values boolean,
versioning_active boolean,
allow_full_deactivation_by_past_close_ts boolean,
action_close boolean,
action_new boolean,
action_mod boolean,
deactivate_all boolean,
found_changed_value boolean,
past_time boolean,
near_past_time boolean,
on_dup_key_update boolean,
on_dup_key_exit boolean,
ignore_null_on_update boolean,
lock_main_table boolean,
enable_version_only_on_main_change boolean,
enable_history_attributes boolean,
enable_attribute_to_fields_replacement boolean,
is_ready boolean,
trace_call_stack boolean,
tar_changelog boolean
);
--
-- Name: __entity_fullname_type__to_hstore(vrsn.entity_fullname_type); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__entity_fullname_type__to_hstore(st vrsn.entity_fullname_type) RETURNS extensions.hstore
LANGUAGE sql IMMUTABLE
AS $$
SELECT CASE
WHEN st IS NULL THEN NULL::hstore
ELSE hstore(ARRAY['schema_name', 'table_name'],
ARRAY[COALESCE(st.schema_name, 'public'), COALESCE(st.table_name, '')])
END;
$$;
--
-- Name: __entity_fullname_type__to_json(vrsn.entity_fullname_type); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__entity_fullname_type__to_json(st vrsn.entity_fullname_type) RETURNS json
LANGUAGE sql IMMUTABLE
AS $$
SELECT CASE
WHEN st IS NULL THEN NULL::json
ELSE json_build_object(
'schema_name', COALESCE(st.schema_name, 'public'),
'table_name', COALESCE(st.table_name, '')
)
END;
$$;
--
-- Name: __entity_fullname_type__to_jsonb(vrsn.entity_fullname_type); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__entity_fullname_type__to_jsonb(st vrsn.entity_fullname_type) RETURNS jsonb
LANGUAGE sql IMMUTABLE
AS $$
SELECT CASE
WHEN st IS NULL THEN NULL::jsonb
ELSE jsonb_build_object(
'schema_name', COALESCE(st.schema_name, 'public'),
'table_name', COALESCE(st.table_name, '')
)
END;
$$;
--
-- Name: __entity_fullname_type__to_string(vrsn.entity_fullname_type); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__entity_fullname_type__to_string(st vrsn.entity_fullname_type) RETURNS text
LANGUAGE sql IMMUTABLE
AS $$
SELECT CASE
WHEN st IS NULL THEN NULL
WHEN st.schema_name IS NULL AND st.table_name IS NULL THEN NULL
ELSE COALESCE(st.schema_name, 'public') || '.' || COALESCE(st.table_name, '')
END;
$$;
--
-- Name: __entity_fullname_type__from_hstore(extensions.hstore); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__entity_fullname_type__from_hstore(hs extensions.hstore) RETURNS vrsn.entity_fullname_type
LANGUAGE plpgsql
AS $$
DECLARE
result vrsn.entity_fullname_type;
BEGIN
IF hs IS NULL THEN
RETURN NULL;
END IF;
result.schema_name := COALESCE(hs->'schema_name', hs->'schema', 'public');
result.table_name := COALESCE(hs->'table_name', hs->'table');
IF result.table_name IS NULL THEN
RAISE EXCEPTION 'Missing table_name in hstore';
END IF;
RETURN result;
END;
$$;
--
-- Name: __entity_fullname_type__from_json(json); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__entity_fullname_type__from_json(js json) RETURNS vrsn.entity_fullname_type
LANGUAGE plpgsql
AS $$
DECLARE
result vrsn.entity_fullname_type;
BEGIN
IF js IS NULL THEN
RETURN NULL;
END IF;
result.schema_name := COALESCE(js->>'schema_name', js->>'schema', 'public');
result.table_name := COALESCE(js->>'table_name', js->>'table');
IF result.table_name IS NULL THEN
RAISE EXCEPTION 'Missing table_name in JSON';
END IF;
RETURN result;
END;
$$;
--
-- Name: __entity_fullname_type__from_jsonb(jsonb); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__entity_fullname_type__from_jsonb(js jsonb) RETURNS vrsn.entity_fullname_type
LANGUAGE plpgsql
AS $$
DECLARE
result vrsn.entity_fullname_type;
BEGIN
IF js IS NULL THEN
RETURN NULL;
END IF;
result.schema_name := COALESCE(js->>'schema_name', js->>'schema', 'public');
result.table_name := COALESCE(js->>'table_name', js->>'table');
IF result.table_name IS NULL THEN
RAISE EXCEPTION 'Missing table_name in JSONB';
END IF;
RETURN result;
END;
$$;
--
-- Name: __entity_fullname_type__from_string(text); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__entity_fullname_type__from_string(input_string text) RETURNS vrsn.entity_fullname_type
LANGUAGE plpgsql
AS $$
DECLARE
result vrsn.entity_fullname_type;
parts text[];
BEGIN
IF input_string IS NULL THEN
RETURN NULL;
END IF;
-- Dividi la stringa per il punto
parts := string_to_array(input_string, '.');
IF array_length(parts, 1) = 2 THEN
result.schema_name := parts[1];
result.table_name := parts[2];
ELSIF array_length(parts, 1) = 1 THEN
-- Se non c'è schema, assume 'public'
result.schema_name := 'public';
result.table_name := parts[1];
ELSE
RAISE EXCEPTION 'Invalid schema.table format: %', input_string;
END IF;
RETURN result;
END;
$$;
--
-- Name: __bitemporal_entity__build_ddl(jsonb); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__bitemporal_entity__build_ddl(p_conf jsonb) RETURNS text
LANGUAGE plpgsql
AS $$
declare
/*
IN p_conf jsonb)
RETURNS text
*/
v_sqlStr text;
v_conf jsonb;
v_key text;
v_jb jsonb;
v_i integer;
v_valid boolean;
v_errors text[];
begin
--------------------------------------------------------------------
-- Fase 0: Verifico input
--
select is_valid, errors into v_valid, v_errors
from common.jsonb_schema__validate ( p_conf
, vrsn.parameters__get('bitemporal_entity','json_schema')
)
;
if not v_valid then
raise exception e'Json not valid:\n%', v_errors;
end if;
--------------------------------------------------------------------
-- Recupera parametro vuoto
v_conf=vrsn.parameters__get('bitemporal_entity','inner_conf');
--------------------------------------------------------------------
-- Fase 1: Applica p_conf a v_conf usando common.jsonb_recursive_left_merge.
-- Questo riempie la struttura di default di v_conf con i valori non-null forniti in p_conf.
v_conf := common.jsonb_recursive_left_merge(v_conf, p_conf, true);
--------------------------------------------------------------------
-- Completa i parametri
v_conf=vrsn.__bitemporal_entity__complete_conf_param(v_conf);
--------------------------------------------------------------------
-- Recupero la strutura dei campi
v_conf['structure']=vrsn.jsonb_table_structure__build(
(v_conf->'current_table'->>'table_name')
, (v_conf->'current_table'->>'schema_name')
);
--------------------------------------------------------------------
-- Verifico se eredita dalla tabella bitemporale
SELECT count(*) into v_i
from vrsn.__get_table_inheritance_ancestors(
(v_conf->'current_table'->>'schema_name')
, (v_conf->'current_table'->>'table_name')
)
WHERE ancestor_schema='vrsn'
and ancestor_table='bitemporal_parent_table'
;
if v_i>0 then
v_conf['version']=to_jsonb(2::int);
else
v_conf['version']=to_jsonb(1::int);
end if;
--------------------------------------------------------------------
-- Recupero la strutura
for v_key, v_jb in
select key, value
from jsonb_each(v_conf->'structure')
loop
if (v_jb->>'pk')::boolean then
v_conf['current_pk'][ (v_jb->>'pk_order')::int -1] = to_jsonb(v_key);
end if;
case v_jb->>'type'
when 'vrsn.bitemporal_record' then
v_conf['bt_info_name']= to_jsonb(v_key);
v_conf['history_pk'][0]=to_jsonb(v_key);
v_conf['bitemporal_fields']=v_conf->'bitemporal_fields' || to_jsonb(v_key);
when 'vrsn.bt_user_ts_range' then
if v_key='user_ts_range' then
v_conf['history_pk'][0]=to_jsonb(v_key);
v_conf['bitemporal_fields']=v_conf->'bitemporal_fields' || to_jsonb(v_key);
end if;
when 'vrsn.bt_db_ts_range' then
if v_key='db_ts_range' then
v_conf['history_pk'][1]=to_jsonb(v_key);
v_conf['bitemporal_fields']=v_conf->'bitemporal_fields' || to_jsonb(v_key);
end if;
when 'vrsn.bt_audit_record' then
if v_key='_audit_record' then
v_conf['bitemporal_fields']=v_conf->'bitemporal_fields' || to_jsonb(v_key);
end if;
else
null;
end case;
end loop;
v_conf['history_pk']=v_conf['current_pk'] || v_conf['history_pk'];
--raise notice e'\n%', jsonb_pretty(v_conf);
v_sqlStr= vrsn.__bitemporal_entity__get_ddl_complete(v_conf);
--------------------------------------------------------------------
-- Manage attribute handling
if v_conf->>'historice_entity' <> 'never' and (v_conf->>'enable_history_attributes')::boolean then
v_sqlStr=v_sqlStr
|| vrsn.__bitemporal_entity__get_ddl_complete_attribute(v_conf);
end if;
return v_sqlStr;
end;
$$;
--
-- Name: __bitemporal_entity__change(text, text, text, vrsn.historice_entity_behaviour, boolean, text, text, boolean, boolean, boolean); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__bitemporal_entity__change(p_entity_schema text, p_entity_name text, p_modify_user_id text, p_historice_entity vrsn.historice_entity_behaviour DEFAULT NULL::vrsn.historice_entity_behaviour, p_enable_history_attributes boolean DEFAULT NULL::boolean, p_main_fields_list text DEFAULT NULL::text, p_cached_fields_list text DEFAULT NULL::text, p_mitigate_conflicts boolean DEFAULT NULL::boolean, p_ignore_unchanged_values boolean DEFAULT NULL::boolean, p_enable_attribute_to_fields_replacement boolean DEFAULT NULL::boolean) RETURNS void
LANGUAGE plpgsql
AS $_$
declare
/*
p_entity_schema text,
p_entity_name text,
p_modify_user_id text,
p_historice_entity vrsn.historice_entity_behaviour DEFAULT null,
p_enable_history_attributes boolean DEFAULT null,
p_main_fields_list text DEFAULT NULL::text,
p_cached_fields_list text DEFAULT NULL::text
p_mitigate_conflicts boolean default null,
p_ignore_unchanged_values boolean default null,
p_enable_attribute_to_fields_replacement boolean default null
RETURNS void
*/
v_entity_full_name vrsn.entity_fullname_dmn;
affected_rows INTEGER;
row_lock boolean =false;
v_table_full_name text;
begin
--------------------------------------------------------------------
-- set entity name
v_entity_full_name.schema_name = p_entity_schema;
v_entity_full_name.table_name = p_entity_name;
--------------------------------------------------------------------
-- Try to obtain an advisory locks (semaphore) for
--
perform vrsn.__lock__get_advsory(
'vrsn.trigger_activation_record_base'
, v_entity_full_name::text
);
perform vrsn.__lock__get_advsory(
'vrsn.def_entity_behavior'
, v_entity_full_name::text
);
--------------------------------------------------------------------
-- Try to obtain row lock
-- and check if there is record for entity
begin
v_table_full_name='vrsn.def_entity_behavior';
select true into strict row_lock
from vrsn.def_entity_behavior
WHERE entity_full_name = v_entity_full_name
for update NOWAIT;
v_table_full_name='vrsn.trigger_activation_record_base';
select true into row_lock
from only vrsn.trigger_activation_record_base
WHERE entity_full_name = v_entity_full_name
for update NOWAIT;
EXCEPTION
WHEN lock_not_available THEN
raise lock_not_available
using message=format('Exlusive row lock unavailable for entity in %1$s in %2$s'
, v_entity_full_name
, v_table_full_name);
WHEN no_data_found THEN
raise no_data_found
using message=format('No record for %1$s in %2$s'
, v_entity_full_name
, v_table_full_name);
end;
--------------------------------------------------------------------
-- update def entity behaviour record
UPDATE vrsn.def_entity_behavior
SET
historice_entity
= COALESCE(p_historice_entity, historice_entity),
enable_history_attributes
= COALESCE(p_enable_history_attributes, enable_history_attributes),
main_fields_list
= COALESCE(p_main_fields_list, main_fields_list),
cached_fields_list
= COALESCE(p_cached_fields_list, cached_fields_list),
mitigate_conflicts
= COALESCE(p_mitigate_conflicts, mitigate_conflicts),
ignore_unchanged_values
= COALESCE(p_ignore_unchanged_values, ignore_unchanged_values),
enable_attribute_to_fields_replacement
= COALESCE(p_enable_attribute_to_fields_replacement, enable_attribute_to_fields_replacement),
modify_user_id
= p_modify_user_id
WHERE entity_full_name = v_entity_full_name;
GET DIAGNOSTICS affected_rows = ROW_COUNT;
delete
from only vrsn.trigger_activation_record_base
WHERE entity_full_name = v_entity_full_name;
if affected_rows =0 then
raise exception 'Entity <%> doesn''t exist,', v_entity_full_name::text;
end if;
end;
$_$;
--
-- Name: __bitemporal_entity__complete_conf_param(jsonb); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__bitemporal_entity__complete_conf_param(p_conf jsonb) RETURNS jsonb
LANGUAGE plpgsql
AS $$
declare
/*
IN p_conf jsonb
RETURNS jsonb
*/
begin
--raise notice '1 %',jsonb_pretty(p_conf);
--------------------------------------------------------------------
-- Recupera lo schema se assente
p_conf['current_table']['schema_name'] = to_jsonb(coalesce (
(p_conf->'current_table'->>'schema_name')
,(p_conf->'entity'->>'schema_name')
,(p_conf->'current_view'->>'schema_name')
));
p_conf['current_view']['schema_name'] = to_jsonb(coalesce (
(p_conf->'current_view'->>'schema_name')
,(p_conf->'entity'->>'schema_name')
,(p_conf->'current_table'->>'schema_name')
));
p_conf['entity']['schema_name'] = to_jsonb(coalesce (
(p_conf->'entity'->>'schema_name')
,(p_conf->'current_table'->>'schema_name')
));
p_conf['history_table']['schema_name'] = to_jsonb(coalesce (
(p_conf->'history_table'->>'schema_name')
,(p_conf->'current_table'->>'schema_name')
));
p_conf['attribute_entity']['schema_name'] = to_jsonb(coalesce (
(p_conf->'attribute_entity'->>'schema_name')
,(p_conf->'current_table'->>'schema_name')
));
--------------------------------------------------------------------
-- Recupera il nome della tabella
--raise notice '2%',jsonb_pretty(p_conf);
p_conf['current_table']['table_name'] = to_jsonb(coalesce (
(p_conf->'current_table'->>'table_name')
, vrsn.__bitemporal_entity__get_current_table_name(
(p_conf->'current_view'->>'table_name')
)
, (p_conf->'entity'->>'table_name') || '_current'
));
p_conf['entity']['table_name'] = to_jsonb(coalesce (
(p_conf->'entity'->>'table_name')
, vrsn.__bitemporal_entity__get_entity_name (
(p_conf->'current_table'->>'table_name')
)
));
p_conf['current_view']['table_name'] = to_jsonb(coalesce (
(p_conf->'current_view'->>'table_name')
, vrsn.__bitemporal_entity__get_view_name (
(p_conf->'current_table'->>'table_name')
)
));
p_conf['history_table']['table_name'] = to_jsonb(coalesce (
(p_conf->'history_table'->>'table_name')
, vrsn.__bitemporal_entity__get_history_table_name(
(p_conf->'current_table'->>'table_name')
)
));
p_conf['attribute_entity']['table_name'] = to_jsonb(coalesce (
(p_conf->'attribute_entity'->>'table_name')
, vrsn.__bitemporal_entity__get_attribute_entity_name(
(p_conf->'current_table'->>'table_name')
)
));
--raise notice '3%',jsonb_pretty(p_conf);
--------------------------------------------------------------------
-- Controlli di coerenza
if ((p_conf->'current_view'->>'schema_name') is null
or (p_conf->'current_view'->>'table_name') is null)
then
raise exception 'Missing information: You must provide at least "current_view.table_name" or "current_table.table_name".';
elsif (p_conf->'current_view'->>'schema_name') = (p_conf->'current_table'->>'schema_name')
and (p_conf->'current_view'->>'table_name') = (p_conf->'current_table'->>'table_name')
then
raise exception e'View and Table must be distinguishable.\n%',jsonb_pretty(p_conf);
end if;
if not (p_conf->>'enable_history_attributes')::boolean then
p_conf['attribute_entity']['schema_name']=to_jsonb(null::text);
p_conf['attribute_entity']['table_name']=to_jsonb(null::text);
end if;
return p_conf;
end;
$$;
--
-- Name: FUNCTION __bitemporal_entity__complete_conf_param(p_conf jsonb); Type: COMMENT; Schema: vrsn; Owner: -
--
COMMENT ON FUNCTION vrsn.__bitemporal_entity__complete_conf_param(p_conf jsonb) IS 'Complete missing value of conf parameter';
--
-- Name: __bitemporal_entity__get_attribute_entity_name(text); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__bitemporal_entity__get_attribute_entity_name(object_name text) RETURNS text
LANGUAGE sql IMMUTABLE PARALLEL SAFE
BEGIN ATOMIC
SELECT regexp_replace(object_name, '(\_current|\_history|\_view|\_entity)?$'::text, '_attribute'::text) AS entity_name;
END;
--
-- Name: FUNCTION __bitemporal_entity__get_attribute_entity_name(object_name text); Type: COMMENT; Schema: vrsn; Owner: -
--
COMMENT ON FUNCTION vrsn.__bitemporal_entity__get_attribute_entity_name(object_name text) IS 'return name for the attribute entity starting from a bitemporal table or view';
--
-- Name: __bitemporal_entity__get_attribute_table_name(text); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__bitemporal_entity__get_attribute_table_name(table_name text) RETURNS text
LANGUAGE sql IMMUTABLE PARALLEL SAFE
BEGIN ATOMIC
SELECT replace(table_name, '_current'::text, '_attribute_current'::text) AS tname;
END;
--
-- Name: FUNCTION __bitemporal_entity__get_attribute_table_name(table_name text); Type: COMMENT; Schema: vrsn; Owner: -
--
COMMENT ON FUNCTION vrsn.__bitemporal_entity__get_attribute_table_name(table_name text) IS 'return name for the attribute table';
--
-- Name: __bitemporal_entity__get_current_table_name(text); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__bitemporal_entity__get_current_table_name(object_name text) RETURNS text
LANGUAGE sql IMMUTABLE PARALLEL SAFE
BEGIN ATOMIC
SELECT regexp_replace(object_name, '(\_current|\_history|\_view|\_entity)?$'::text, '_current'::text) AS entity_name;
END;
--
-- Name: FUNCTION __bitemporal_entity__get_current_table_name(object_name text); Type: COMMENT; Schema: vrsn; Owner: -
--
COMMENT ON FUNCTION vrsn.__bitemporal_entity__get_current_table_name(object_name text) IS 'return name for the current table starting from a bitemporal table or view';
--
-- Name: __bitemporal_entity__get_ddl_attribute_table(jsonb); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__bitemporal_entity__get_ddl_attribute_table(p_conf jsonb) RETURNS text
LANGUAGE plpgsql
AS $_$
DECLARE
/*
IN p_conf jsonb)
RETURNS text
*/
v_pk_fields text;
v_fields_definition text;
v_ddl text;
v_sql_v1 constant text=$$
-- Create Attribute Table
create table if not exists %1$I.%2$I (
::bt_info:: vrsn.bitemporal_record NOT NULL
, %3$s
, constraint %2$I_pk primary key (%4$s)
);
$$;
v_sql_v2 constant text=$$
create table if not exists %1$I.%2$I (
%3$s
, constraint %2$I_pk primary key (%4$s)
) INHERITS (vrsn.bitemporal_parent_table) ;
$$;
BEGIN
---------------------------------------------------------------------
-- compute fields definiton
-- and pk composition
with fields_list as (
select field_name, pk_order, table_order, complete_definition
from vrsn.table__get_fields_details(
(p_conf->'parent_table'->>'schema_name')
, (p_conf->'parent_table'->>'table_name')
)
where is_pk
union all
select field_name, pk_order+100, table_order+100, complete_definition
from vrsn.table__get_fields_details('vrsn'
, 'bitemporal_parent_attribute_table')
), pk_fields as (
select string_agg( field_name
, ', ' order by pk_order
) as pk_text
from fields_list
where pk_order is not null
), fields_def as (
select string_agg( complete_definition
, e'\n\t\t,\t' order by table_order
) as fields_definition
from fields_list
)
select a.pk_text, b.fields_definition
INTO v_pk_fields, v_fields_definition
from pk_fields a, fields_def b;
raise notice '%', v_fields_definition;
---------------------------------------------------------------------
-- Determine the type of table creazione
if (p_conf->>'version')::integer= 2 then
v_ddl= v_sql_v2;
else
v_ddl=replace(v_sql_v1, '::bt_info::', (p_conf->>'bt_info_name') );
end if;
---------------------------------------------------------------------
-- substitute parameters
v_ddl =e'\n\n'||format(v_ddl
, (p_conf->'current_table'->>'schema_name')
, (p_conf->'current_table'->>'table_name')
, v_fields_definition
, v_pk_fields
);
RETURN v_ddl;
END;
$_$;
--
-- Name: __bitemporal_entity__get_ddl_complete(jsonb); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__bitemporal_entity__get_ddl_complete(p_conf jsonb) RETURNS text
LANGUAGE plpgsql
AS $_$
declare
v_sqlStr text;
t_username text;
begin
--------------------------------------------------------------------
-- get/set user name text
t_username =coalesce(
vrsn.parameters__get_value('tar', 'params.t_username') #>>'{}'
, 'modify_user_id'
);
--------------------------------------------------------------------
-- get Idex for historical search
-- for current table
v_sqlStr = e'\n\n' ||
vrsn.__bitemporal_entity__get_ddl_tsrange_idx(
p_conf, 'current_table'
);
--------------------------------------------------------------------
-- Get ddl for history table
v_sqlStr = v_sqlStr || e'\n\n' || vrsn.__bitemporal_entity__get_ddl_history_table(p_conf);
--------------------------------------------------------------------
-- get Idex for historical search
-- for history table
v_sqlStr = v_sqlStr ||e'\n\n' ||
vrsn.__bitemporal_entity__get_ddl_tsrange_idx(
p_conf, 'history_table'
);
--------------------------------------------------------------------
-- Get ddl for history table
v_sqlStr = v_sqlStr || e'\n\n' || vrsn.__bitemporal_entity__get_ddl_view(p_conf);
--------------------------------------------------------------------
-- Insert def entity behaviour record
v_sqlStr = v_sqlStr ||
format($$
-- Register data into vrsn.def_entity_behavior
INSERT INTO vrsn.def_entity_behavior(
entity_full_name.schema_name
, entity_full_name.table_name
, current_view_full_name.schema_name
, current_view_full_name.table_name
, current_table_full_name.schema_name
, current_table_full_name.table_name
, history_table_full_name.schema_name
, history_table_full_name.table_name
, attribute_entity_full_name.schema_name
, attribute_entity_full_name.table_name
, historice_entity, enable_history_attributes
, main_fields_list, cached_fields_list
, enable_attribute_to_fields_replacement
, tar_version, %17$I
, action_hints)
VALUES( %1$s, %2$s
, %3$s, %4$s
, %5$s, %6$s
, %7$s, %8$s
, %9$s, %10$s
, %11$s, %12$s
, %13$s, %14$s
, %15$s
, %16$s, 'process:vrsn.register'
, '{"onDupKey":"update"}'::jsonb);$$
, quote_nullable((p_conf->'entity'->>'schema_name'))
, quote_nullable((p_conf->'entity'->>'table_name'))
, quote_nullable((p_conf->'current_view'->>'schema_name'))
, quote_nullable((p_conf->'current_view'->>'table_name'))
, quote_nullable((p_conf->'current_table'->>'schema_name'))
, quote_nullable((p_conf->'current_table'->>'table_name'))
, quote_nullable((p_conf->'history_table'->>'schema_name'))
, quote_nullable((p_conf->'history_table'->>'table_name'))
, quote_nullable((p_conf->'attribute_entity'->>'schema_name'))
, quote_nullable((p_conf->'attribute_entity'->>'table_name'))
, quote_nullable(p_conf->>'historice_entity')
, p_conf->>'enable_history_attributes'
, quote_nullable(p_conf->>'main_fields_list')
, quote_nullable(p_conf->>'cached_fields_list')
, p_conf->>'enable_attribute_to_fields_replacement'
, quote_nullable('1') --tar_version
, t_username
);
return v_sqlStr;
end;
$_$;
--
-- Name: __bitemporal_entity__get_ddl_complete_attribute(jsonb); Type: FUNCTION; Schema: vrsn; Owner: -
--
CREATE FUNCTION vrsn.__bitemporal_entity__get_ddl_complete_attribute(p_conf jsonb) RETURNS text
LANGUAGE plpgsql
AS $_$
declare
v_sqlStr text;
v_conf jsonb;
v_key text;
v_jb jsonb;
v_i integer;
begin