@nsshunt/stsdatamanagement 1.18.195 → 1.18.198
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/db-scripts/builddb.sql +463 -118
- package/package.json +1 -1
package/db-scripts/builddb.sql
CHANGED
|
@@ -443,6 +443,7 @@ ALTER TABLE public.stsresource
|
|
|
443
443
|
|
|
444
444
|
-- DROP INDEX public.stsresource_resname_vnum;
|
|
445
445
|
|
|
446
|
+
-- Enforce resource version uniqueness.
|
|
446
447
|
CREATE UNIQUE INDEX stsresource_resname_vnum
|
|
447
448
|
ON public.stsresource USING btree
|
|
448
449
|
(resname COLLATE pg_catalog."default" ASC NULLS LAST, vnum ASC NULLS LAST)
|
|
@@ -452,12 +453,14 @@ CREATE UNIQUE INDEX stsresource_resname_vnum
|
|
|
452
453
|
|
|
453
454
|
-- DROP INDEX public.stsresource_resname_validto;
|
|
454
455
|
|
|
456
|
+
-- General current-version lookup.
|
|
457
|
+
-- Includes current deleted records.
|
|
455
458
|
CREATE INDEX stsresource_resname_validto
|
|
456
459
|
ON public.stsresource USING btree
|
|
457
460
|
(resname COLLATE pg_catalog."default" ASC NULLS LAST, validto DESC NULLS LAST)
|
|
458
461
|
TABLESPACE pg_default;
|
|
459
462
|
|
|
460
|
-
|
|
463
|
+
/*
|
|
461
464
|
-- DROP INDEX public.stsresource_given;
|
|
462
465
|
|
|
463
466
|
CREATE INDEX stsresource_given
|
|
@@ -471,6 +474,15 @@ CREATE INDEX stsresource_family
|
|
|
471
474
|
ON public.stsresource USING btree
|
|
472
475
|
(family COLLATE pg_catalog."default" ASC)
|
|
473
476
|
WHERE family IS NOT NULL;
|
|
477
|
+
*/
|
|
478
|
+
|
|
479
|
+
-- DROP INDEX public.idx_stsresource_resname_current;
|
|
480
|
+
|
|
481
|
+
-- Normal application reads and cursor-based pagination.
|
|
482
|
+
CREATE INDEX idx_stsresource_resname_current
|
|
483
|
+
ON public.stsresource (resname, oid)
|
|
484
|
+
WHERE validto IS NULL
|
|
485
|
+
AND dbaction != 3;
|
|
474
486
|
|
|
475
487
|
-- FUNCTION: public.create_stsresource(character varying, character varying, character varying)
|
|
476
488
|
|
|
@@ -564,7 +576,7 @@ ALTER FUNCTION public.create_stsresource(character varying, character varying, c
|
|
|
564
576
|
CREATE OR REPLACE FUNCTION public.update_stsresource(
|
|
565
577
|
_dbactionuser character varying,
|
|
566
578
|
_resname character varying,
|
|
567
|
-
_resvnum
|
|
579
|
+
_resvnum bigint,
|
|
568
580
|
_resdesc character varying)
|
|
569
581
|
RETURNS SETOF stsresource
|
|
570
582
|
LANGUAGE 'plpgsql'
|
|
@@ -616,13 +628,10 @@ BEGIN
|
|
|
616
628
|
END;
|
|
617
629
|
$BODY$;
|
|
618
630
|
|
|
619
|
-
ALTER FUNCTION public.update_stsresource(character varying, character varying,
|
|
631
|
+
ALTER FUNCTION public.update_stsresource(character varying, character varying, bigint, character varying)
|
|
620
632
|
OWNER TO postgres;
|
|
621
633
|
|
|
622
634
|
|
|
623
|
-
-- FUNCTION: public.update_stsresource(character varying, character varying, integer, character varying)
|
|
624
|
-
|
|
625
|
-
-- DROP FUNCTION public.update_stsresource(character varying, character varying, integer, character varying);
|
|
626
635
|
|
|
627
636
|
CREATE OR REPLACE FUNCTION public.update_stsresource_latest(
|
|
628
637
|
_dbactionuser character varying,
|
|
@@ -640,7 +649,7 @@ declare
|
|
|
640
649
|
_now timestamp without time zone := now();
|
|
641
650
|
resoid bigint;
|
|
642
651
|
newresoid stsresource.oid%TYPE;
|
|
643
|
-
_resvnum
|
|
652
|
+
_resvnum bigint;
|
|
644
653
|
_resdesc_jsonb JSONB;
|
|
645
654
|
_given character varying(64) COLLATE pg_catalog."default";
|
|
646
655
|
_family character varying(64) COLLATE pg_catalog."default";
|
|
@@ -691,14 +700,11 @@ ALTER FUNCTION public.update_stsresource_latest(character varying, character var
|
|
|
691
700
|
|
|
692
701
|
|
|
693
702
|
|
|
694
|
-
-- FUNCTION: public.delete_stsresource(character varying, character varying, integer)
|
|
695
|
-
|
|
696
|
-
-- DROP FUNCTION public.delete_stsresource(character varying, character varying, integer);
|
|
697
703
|
|
|
698
704
|
CREATE OR REPLACE FUNCTION public.delete_stsresource(
|
|
699
705
|
_dbactionuser character varying,
|
|
700
706
|
_resname character varying,
|
|
701
|
-
_resvnum
|
|
707
|
+
_resvnum bigint)
|
|
702
708
|
RETURNS SETOF stsresource
|
|
703
709
|
LANGUAGE 'plpgsql'
|
|
704
710
|
|
|
@@ -738,7 +744,7 @@ BEGIN
|
|
|
738
744
|
END;
|
|
739
745
|
$BODY$;
|
|
740
746
|
|
|
741
|
-
ALTER FUNCTION public.delete_stsresource(character varying, character varying,
|
|
747
|
+
ALTER FUNCTION public.delete_stsresource(character varying, character varying, bigint)
|
|
742
748
|
OWNER TO postgres;
|
|
743
749
|
|
|
744
750
|
|
|
@@ -758,7 +764,7 @@ declare
|
|
|
758
764
|
_now timestamp without time zone := now();
|
|
759
765
|
resoid bigint;
|
|
760
766
|
newresoid stsresource.oid%TYPE;
|
|
761
|
-
_resvnum
|
|
767
|
+
_resvnum bigint;
|
|
762
768
|
BEGIN
|
|
763
769
|
begin
|
|
764
770
|
_resvnum := (select vnum from stsresource r where r.resname = _resname and r.validto is null and r.dbaction != 3);
|
|
@@ -798,8 +804,6 @@ ALTER FUNCTION public.delete_stsresource_latest(character varying, character var
|
|
|
798
804
|
|
|
799
805
|
|
|
800
806
|
|
|
801
|
-
|
|
802
|
-
|
|
803
807
|
-- Table: public.stsentity
|
|
804
808
|
|
|
805
809
|
-- DROP TABLE public.stsentity;
|
|
@@ -831,141 +835,491 @@ ALTER TABLE public.stsentity
|
|
|
831
835
|
|
|
832
836
|
-- DROP INDEX public.stsentity_resourceoid_entname_vnum;
|
|
833
837
|
|
|
838
|
+
-- Enforce entity version uniqueness.
|
|
834
839
|
CREATE UNIQUE INDEX stsentity_resourceoid_entname_vnum
|
|
835
840
|
ON public.stsentity USING btree
|
|
836
|
-
|
|
841
|
+
(resourceoid ASC NULLS LAST, entname COLLATE pg_catalog."default" ASC NULLS LAST, vnum ASC NULLS LAST)
|
|
837
842
|
TABLESPACE pg_default;
|
|
838
843
|
|
|
839
|
-
--
|
|
840
|
-
|
|
841
|
-
-- DROP INDEX public.stsentity_resourceoid_entname_validto;
|
|
842
|
-
|
|
844
|
+
-- General current/version lookup.
|
|
843
845
|
CREATE INDEX stsentity_resourceoid_entname_validto
|
|
844
846
|
ON public.stsentity USING btree
|
|
845
847
|
(resourceoid ASC NULLS LAST, entname COLLATE pg_catalog."default" ASC NULLS LAST, validto DESC NULLS LAST)
|
|
846
848
|
TABLESPACE pg_default;
|
|
847
849
|
|
|
850
|
+
-- Normal application reads and cursor-based pagination.
|
|
851
|
+
CREATE UNIQUE INDEX idx_stsentity_resourceoid_entname_current
|
|
852
|
+
ON public.stsentity
|
|
853
|
+
(resourceoid, entname)
|
|
854
|
+
WHERE validto IS NULL;
|
|
848
855
|
|
|
849
|
-
-- FUNCTION: public.create_stsentity(character varying, character varying, integer, character varying, character varying)
|
|
850
|
-
|
|
851
|
-
-- DROP FUNCTION public.create_stsentity(character varying, character varying, integer, character varying, character varying);
|
|
852
856
|
|
|
853
857
|
CREATE OR REPLACE FUNCTION public.create_stsentity(
|
|
854
858
|
_dbactionuser character varying,
|
|
855
859
|
_resname character varying,
|
|
856
|
-
_resvnum
|
|
860
|
+
_resvnum bigint,
|
|
857
861
|
_entname character varying,
|
|
858
|
-
_entvalue character varying
|
|
859
|
-
|
|
860
|
-
|
|
862
|
+
_entvalue character varying
|
|
863
|
+
)
|
|
864
|
+
RETURNS SETOF stsentity
|
|
865
|
+
LANGUAGE 'plpgsql'
|
|
866
|
+
COST 100
|
|
867
|
+
VOLATILE
|
|
868
|
+
ROWS 1000
|
|
861
869
|
|
|
862
|
-
COST 100
|
|
863
|
-
VOLATILE
|
|
864
|
-
ROWS 1000
|
|
865
|
-
|
|
866
870
|
AS $BODY$
|
|
867
|
-
|
|
868
|
-
|
|
871
|
+
DECLARE
|
|
872
|
+
__now timestamp without time zone := now();
|
|
873
|
+
|
|
874
|
+
-- Current resource.
|
|
875
|
+
__resoid bigint;
|
|
876
|
+
|
|
877
|
+
-- Current entity, if one exists.
|
|
878
|
+
__entoid bigint;
|
|
879
|
+
__entvnum bigint;
|
|
880
|
+
__entdbaction smallint;
|
|
881
|
+
|
|
882
|
+
-- Version to assign to the newly created entity.
|
|
883
|
+
__newvnum bigint;
|
|
884
|
+
|
|
885
|
+
-- Newly inserted entity.
|
|
869
886
|
__inserted_oid bigint;
|
|
870
|
-
|
|
887
|
+
|
|
888
|
+
-- Parsed entity JSON.
|
|
889
|
+
__entvalue_jsonb jsonb;
|
|
890
|
+
|
|
871
891
|
BEGIN
|
|
872
|
-
begin
|
|
873
|
-
_entvalue_jsonb := _entvalue::jsonb;
|
|
874
|
-
exception
|
|
875
|
-
when others then
|
|
876
|
-
raise exception 'Invalid JSON string: %', _entvalue;
|
|
877
|
-
end;
|
|
878
892
|
|
|
879
|
-
--
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
893
|
+
-- -------------------------------------------------------------------------
|
|
894
|
+
-- Validate / parse entity JSON
|
|
895
|
+
-- -------------------------------------------------------------------------
|
|
896
|
+
|
|
897
|
+
BEGIN
|
|
898
|
+
__entvalue_jsonb := _entvalue::jsonb;
|
|
899
|
+
|
|
900
|
+
EXCEPTION
|
|
901
|
+
WHEN others THEN
|
|
902
|
+
RAISE EXCEPTION
|
|
903
|
+
'Invalid JSON string: %',
|
|
904
|
+
_entvalue;
|
|
905
|
+
END;
|
|
906
|
+
|
|
907
|
+
-- -------------------------------------------------------------------------
|
|
908
|
+
-- Resolve the requested resource.
|
|
909
|
+
--
|
|
910
|
+
-- The supplied resource version must still be the current, non-deleted
|
|
911
|
+
-- version of the resource.
|
|
912
|
+
-- -------------------------------------------------------------------------
|
|
913
|
+
|
|
914
|
+
BEGIN
|
|
915
|
+
SELECT
|
|
916
|
+
r.oid
|
|
917
|
+
INTO STRICT
|
|
918
|
+
__resoid
|
|
919
|
+
FROM public.stsresource r
|
|
920
|
+
WHERE r.resname = _resname
|
|
921
|
+
AND r.vnum = _resvnum
|
|
922
|
+
AND r.validto IS NULL
|
|
923
|
+
AND r.dbaction != 3;
|
|
924
|
+
|
|
925
|
+
EXCEPTION
|
|
926
|
+
|
|
927
|
+
WHEN no_data_found THEN
|
|
928
|
+
RAISE
|
|
929
|
+
'Resource % version % not found, not current, or deleted',
|
|
930
|
+
_resname,
|
|
931
|
+
_resvnum
|
|
932
|
+
USING ERRCODE = 'no_data_found';
|
|
933
|
+
|
|
934
|
+
WHEN too_many_rows THEN
|
|
935
|
+
RAISE
|
|
936
|
+
'Too many current stsresource records for resource % version %',
|
|
937
|
+
_resname,
|
|
938
|
+
_resvnum
|
|
939
|
+
USING ERRCODE = 'cardinality_violation';
|
|
940
|
+
|
|
941
|
+
END;
|
|
942
|
+
|
|
943
|
+
-- -------------------------------------------------------------------------
|
|
944
|
+
-- Find the current entity.
|
|
945
|
+
--
|
|
946
|
+
-- IMPORTANT:
|
|
947
|
+
--
|
|
948
|
+
-- Do NOT exclude dbaction = 3 here.
|
|
949
|
+
--
|
|
950
|
+
-- A logically deleted entity is still the current entity version while
|
|
951
|
+
-- validto IS NULL. If it exists, it must be closed before the entity can
|
|
952
|
+
-- be created again.
|
|
953
|
+
-- -------------------------------------------------------------------------
|
|
954
|
+
|
|
955
|
+
BEGIN
|
|
956
|
+
SELECT
|
|
957
|
+
e.oid,
|
|
958
|
+
e.vnum,
|
|
959
|
+
e.dbaction
|
|
960
|
+
INTO STRICT
|
|
961
|
+
__entoid,
|
|
962
|
+
__entvnum,
|
|
963
|
+
__entdbaction
|
|
964
|
+
FROM public.stsentity e
|
|
965
|
+
WHERE e.resourceoid = __resoid
|
|
966
|
+
AND e.entname = _entname
|
|
967
|
+
AND e.validto IS NULL;
|
|
968
|
+
|
|
969
|
+
EXCEPTION
|
|
970
|
+
|
|
971
|
+
WHEN no_data_found THEN
|
|
972
|
+
__entoid := NULL;
|
|
973
|
+
__entvnum := NULL;
|
|
974
|
+
__entdbaction := NULL;
|
|
975
|
+
|
|
976
|
+
WHEN too_many_rows THEN
|
|
977
|
+
RAISE
|
|
978
|
+
'Too many current stsentity records for entity % within resource %',
|
|
979
|
+
_entname,
|
|
980
|
+
_resname
|
|
981
|
+
USING ERRCODE = 'cardinality_violation';
|
|
982
|
+
|
|
983
|
+
END;
|
|
984
|
+
|
|
985
|
+
-- -------------------------------------------------------------------------
|
|
986
|
+
-- Current entity exists
|
|
987
|
+
-- -------------------------------------------------------------------------
|
|
988
|
+
|
|
989
|
+
IF __entoid IS NOT NULL THEN
|
|
990
|
+
|
|
991
|
+
-- ---------------------------------------------------------------------
|
|
992
|
+
-- Current entity is active.
|
|
993
|
+
--
|
|
994
|
+
-- CREATE is not allowed because the entity already exists.
|
|
995
|
+
-- ---------------------------------------------------------------------
|
|
996
|
+
|
|
997
|
+
IF __entdbaction != 3 THEN
|
|
998
|
+
|
|
999
|
+
RAISE
|
|
1000
|
+
'Duplicate entname % within stsresource %',
|
|
1001
|
+
_entname,
|
|
1002
|
+
_resname
|
|
1003
|
+
USING ERRCODE = 'unique_violation';
|
|
1004
|
+
|
|
1005
|
+
END IF;
|
|
1006
|
+
|
|
1007
|
+
-- ---------------------------------------------------------------------
|
|
1008
|
+
-- Current entity is logically deleted.
|
|
1009
|
+
--
|
|
1010
|
+
-- Close the deleted version and create the next version.
|
|
1011
|
+
-- ---------------------------------------------------------------------
|
|
1012
|
+
|
|
1013
|
+
UPDATE public.stsentity
|
|
1014
|
+
SET validto = __now
|
|
1015
|
+
WHERE oid = __entoid;
|
|
1016
|
+
|
|
1017
|
+
__newvnum := __entvnum + 1;
|
|
1018
|
+
|
|
1019
|
+
ELSE
|
|
1020
|
+
|
|
1021
|
+
-- ---------------------------------------------------------------------
|
|
1022
|
+
-- No current entity exists.
|
|
1023
|
+
--
|
|
1024
|
+
-- Normally this means the entity has never existed and MAX(vnum)
|
|
1025
|
+
-- will be NULL, resulting in version 1.
|
|
1026
|
+
--
|
|
1027
|
+
-- Using MAX(vnum) also protects against an abnormal historical state
|
|
1028
|
+
-- where previous versions exist but none has validto IS NULL.
|
|
1029
|
+
-- ---------------------------------------------------------------------
|
|
1030
|
+
|
|
1031
|
+
SELECT
|
|
1032
|
+
COALESCE(MAX(e.vnum), 0) + 1
|
|
1033
|
+
INTO
|
|
1034
|
+
__newvnum
|
|
1035
|
+
FROM public.stsentity e
|
|
1036
|
+
WHERE e.resourceoid = __resoid
|
|
1037
|
+
AND e.entname = _entname;
|
|
1038
|
+
|
|
1039
|
+
END IF;
|
|
1040
|
+
|
|
1041
|
+
-- -------------------------------------------------------------------------
|
|
1042
|
+
-- Create new active entity version
|
|
1043
|
+
-- -------------------------------------------------------------------------
|
|
1044
|
+
|
|
1045
|
+
INSERT INTO public.stsentity(
|
|
1046
|
+
resourceoid,
|
|
1047
|
+
entname,
|
|
1048
|
+
entvalue,
|
|
1049
|
+
entvalue_jsonb,
|
|
1050
|
+
vnum,
|
|
1051
|
+
dbaction,
|
|
1052
|
+
dbactionuser,
|
|
1053
|
+
validfrom
|
|
1054
|
+
)
|
|
1055
|
+
VALUES (
|
|
1056
|
+
__resoid,
|
|
1057
|
+
_entname,
|
|
1058
|
+
_entvalue,
|
|
1059
|
+
__entvalue_jsonb,
|
|
1060
|
+
__newvnum,
|
|
1061
|
+
1,
|
|
1062
|
+
_dbactionuser,
|
|
1063
|
+
__now
|
|
1064
|
+
)
|
|
1065
|
+
RETURNING oid
|
|
1066
|
+
INTO __inserted_oid;
|
|
1067
|
+
|
|
1068
|
+
-- -------------------------------------------------------------------------
|
|
1069
|
+
-- Return newly created entity
|
|
1070
|
+
-- -------------------------------------------------------------------------
|
|
1071
|
+
|
|
1072
|
+
RETURN QUERY
|
|
1073
|
+
SELECT *
|
|
1074
|
+
FROM public.stsentity
|
|
1075
|
+
WHERE oid = __inserted_oid;
|
|
894
1076
|
|
|
895
|
-
return query
|
|
896
|
-
select * from stsentity where oid = __inserted_oid;
|
|
897
|
-
END IF;
|
|
898
1077
|
END;
|
|
899
1078
|
$BODY$;
|
|
900
1079
|
|
|
901
|
-
ALTER FUNCTION public.create_stsentity(character varying, character varying, integer, character varying, character varying)
|
|
902
|
-
OWNER TO postgres;
|
|
903
|
-
|
|
904
1080
|
|
|
1081
|
+
ALTER FUNCTION public.create_stsentity(
|
|
1082
|
+
character varying,
|
|
1083
|
+
character varying,
|
|
1084
|
+
bigint,
|
|
1085
|
+
character varying,
|
|
1086
|
+
character varying
|
|
1087
|
+
)
|
|
1088
|
+
OWNER TO postgres;
|
|
905
1089
|
|
|
906
1090
|
|
|
907
1091
|
CREATE OR REPLACE FUNCTION public.create_stsentity_latest(
|
|
908
1092
|
_dbactionuser character varying,
|
|
909
1093
|
_resname character varying,
|
|
910
1094
|
_entname character varying,
|
|
911
|
-
_entvalue character varying
|
|
912
|
-
|
|
913
|
-
|
|
1095
|
+
_entvalue character varying
|
|
1096
|
+
)
|
|
1097
|
+
RETURNS SETOF stsentity
|
|
1098
|
+
LANGUAGE 'plpgsql'
|
|
1099
|
+
COST 100
|
|
1100
|
+
VOLATILE
|
|
1101
|
+
ROWS 1000
|
|
914
1102
|
|
|
915
|
-
COST 100
|
|
916
|
-
VOLATILE
|
|
917
|
-
ROWS 1000
|
|
918
|
-
|
|
919
1103
|
AS $BODY$
|
|
920
|
-
|
|
921
|
-
|
|
1104
|
+
DECLARE
|
|
1105
|
+
__now timestamp without time zone := now();
|
|
1106
|
+
|
|
1107
|
+
-- Current resource.
|
|
1108
|
+
__resoid bigint;
|
|
1109
|
+
|
|
1110
|
+
-- Current entity, if one exists.
|
|
1111
|
+
__entoid bigint;
|
|
1112
|
+
__entvnum bigint;
|
|
1113
|
+
__entdbaction smallint;
|
|
1114
|
+
|
|
1115
|
+
-- Version for the newly created entity.
|
|
1116
|
+
__newvnum bigint;
|
|
1117
|
+
|
|
1118
|
+
-- Newly inserted entity.
|
|
922
1119
|
__inserted_oid bigint;
|
|
923
|
-
|
|
1120
|
+
|
|
1121
|
+
-- Parsed JSON.
|
|
1122
|
+
__entvalue_jsonb jsonb;
|
|
1123
|
+
|
|
924
1124
|
BEGIN
|
|
925
|
-
begin
|
|
926
|
-
_entvalue_jsonb := _entvalue::jsonb;
|
|
927
|
-
exception
|
|
928
|
-
when others then
|
|
929
|
-
raise exception 'Invalid JSON string: %', _entvalue;
|
|
930
|
-
end;
|
|
931
1125
|
|
|
932
|
-
--
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
-- Exception Codes: https://www.postgresql.org/docs/10/errcodes-appendix.html
|
|
936
|
-
RAISE 'resname and version not found or not current version (not deleted) within stsresource %', _resname USING ERRCODE = 'no_data_found';
|
|
937
|
-
end if;
|
|
938
|
-
if (select count(*) from stsentity e
|
|
939
|
-
where e.resourceoid = resoid and e.entname = _entname and e.validto is null and e.dbaction != 3) > 0 then
|
|
940
|
-
-- Exception Codes: https://www.postgresql.org/docs/10/errcodes-appendix.html
|
|
941
|
-
-- RAISE EXCEPTION 'resname already exists within stsresource %', now();
|
|
942
|
-
RAISE 'Duplicate entname within stsresource: %', _resname USING ERRCODE = 'unique_violation';
|
|
943
|
-
else
|
|
944
|
-
insert into stsentity(resourceoid, entname, entvalue, entvalue_jsonb, vnum, dbaction, dbactionuser, validfrom)
|
|
945
|
-
values (resoid, _entname, _entvalue, _entvalue_jsonb, 1, 1, _dbactionuser, now())
|
|
946
|
-
returning oid into __inserted_oid;
|
|
1126
|
+
-- -------------------------------------------------------------------------
|
|
1127
|
+
-- Validate / parse entity JSON
|
|
1128
|
+
-- -------------------------------------------------------------------------
|
|
947
1129
|
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
END IF;
|
|
951
|
-
END;
|
|
952
|
-
$BODY$;
|
|
1130
|
+
BEGIN
|
|
1131
|
+
__entvalue_jsonb := _entvalue::jsonb;
|
|
953
1132
|
|
|
954
|
-
|
|
955
|
-
|
|
1133
|
+
EXCEPTION
|
|
1134
|
+
WHEN others THEN
|
|
1135
|
+
RAISE EXCEPTION
|
|
1136
|
+
'Invalid JSON string: %',
|
|
1137
|
+
_entvalue;
|
|
1138
|
+
END;
|
|
956
1139
|
|
|
1140
|
+
-- -------------------------------------------------------------------------
|
|
1141
|
+
-- Resolve the current, non-deleted resource.
|
|
1142
|
+
-- -------------------------------------------------------------------------
|
|
957
1143
|
|
|
1144
|
+
BEGIN
|
|
1145
|
+
SELECT
|
|
1146
|
+
r.oid
|
|
1147
|
+
INTO STRICT
|
|
1148
|
+
__resoid
|
|
1149
|
+
FROM public.stsresource r
|
|
1150
|
+
WHERE r.resname = _resname
|
|
1151
|
+
AND r.validto IS NULL
|
|
1152
|
+
AND r.dbaction != 3;
|
|
1153
|
+
|
|
1154
|
+
EXCEPTION
|
|
1155
|
+
|
|
1156
|
+
WHEN no_data_found THEN
|
|
1157
|
+
RAISE
|
|
1158
|
+
'Resource % not found, not current, or deleted',
|
|
1159
|
+
_resname
|
|
1160
|
+
USING ERRCODE = 'no_data_found';
|
|
1161
|
+
|
|
1162
|
+
WHEN too_many_rows THEN
|
|
1163
|
+
RAISE
|
|
1164
|
+
'Too many current stsresource records for resource %',
|
|
1165
|
+
_resname
|
|
1166
|
+
USING ERRCODE = 'cardinality_violation';
|
|
1167
|
+
|
|
1168
|
+
END;
|
|
1169
|
+
|
|
1170
|
+
-- -------------------------------------------------------------------------
|
|
1171
|
+
-- Find the current entity.
|
|
1172
|
+
--
|
|
1173
|
+
-- IMPORTANT:
|
|
1174
|
+
--
|
|
1175
|
+
-- Do not exclude dbaction = 3.
|
|
1176
|
+
--
|
|
1177
|
+
-- A logically deleted entity is still the latest/current version while
|
|
1178
|
+
-- validto IS NULL. If found, it must be closed before the entity can be
|
|
1179
|
+
-- created again.
|
|
1180
|
+
-- -------------------------------------------------------------------------
|
|
1181
|
+
|
|
1182
|
+
BEGIN
|
|
1183
|
+
SELECT
|
|
1184
|
+
e.oid,
|
|
1185
|
+
e.vnum,
|
|
1186
|
+
e.dbaction
|
|
1187
|
+
INTO STRICT
|
|
1188
|
+
__entoid,
|
|
1189
|
+
__entvnum,
|
|
1190
|
+
__entdbaction
|
|
1191
|
+
FROM public.stsentity e
|
|
1192
|
+
WHERE e.resourceoid = __resoid
|
|
1193
|
+
AND e.entname = _entname
|
|
1194
|
+
AND e.validto IS NULL;
|
|
1195
|
+
|
|
1196
|
+
EXCEPTION
|
|
1197
|
+
|
|
1198
|
+
WHEN no_data_found THEN
|
|
1199
|
+
__entoid := NULL;
|
|
1200
|
+
__entvnum := NULL;
|
|
1201
|
+
__entdbaction := NULL;
|
|
1202
|
+
|
|
1203
|
+
WHEN too_many_rows THEN
|
|
1204
|
+
RAISE
|
|
1205
|
+
'Too many current stsentity records for entity % within resource %',
|
|
1206
|
+
_entname,
|
|
1207
|
+
_resname
|
|
1208
|
+
USING ERRCODE = 'cardinality_violation';
|
|
1209
|
+
|
|
1210
|
+
END;
|
|
1211
|
+
|
|
1212
|
+
-- -------------------------------------------------------------------------
|
|
1213
|
+
-- Current entity exists.
|
|
1214
|
+
-- -------------------------------------------------------------------------
|
|
1215
|
+
|
|
1216
|
+
IF __entoid IS NOT NULL THEN
|
|
1217
|
+
|
|
1218
|
+
-- ---------------------------------------------------------------------
|
|
1219
|
+
-- Current entity is active.
|
|
1220
|
+
--
|
|
1221
|
+
-- CREATE is not allowed.
|
|
1222
|
+
-- ---------------------------------------------------------------------
|
|
1223
|
+
|
|
1224
|
+
IF __entdbaction != 3 THEN
|
|
1225
|
+
|
|
1226
|
+
RAISE
|
|
1227
|
+
'Duplicate entname % within stsresource %',
|
|
1228
|
+
_entname,
|
|
1229
|
+
_resname
|
|
1230
|
+
USING ERRCODE = 'unique_violation';
|
|
1231
|
+
|
|
1232
|
+
END IF;
|
|
1233
|
+
|
|
1234
|
+
-- ---------------------------------------------------------------------
|
|
1235
|
+
-- Current entity is logically deleted.
|
|
1236
|
+
--
|
|
1237
|
+
-- Close the deleted version, then create the next version.
|
|
1238
|
+
-- ---------------------------------------------------------------------
|
|
1239
|
+
|
|
1240
|
+
UPDATE public.stsentity
|
|
1241
|
+
SET validto = __now
|
|
1242
|
+
WHERE oid = __entoid;
|
|
1243
|
+
|
|
1244
|
+
__newvnum := __entvnum + 1;
|
|
1245
|
+
|
|
1246
|
+
ELSE
|
|
1247
|
+
|
|
1248
|
+
-- ---------------------------------------------------------------------
|
|
1249
|
+
-- No current entity exists.
|
|
1250
|
+
--
|
|
1251
|
+
-- Normally this means the entity has never existed, so MAX(vnum)
|
|
1252
|
+
-- will be NULL and the new version becomes 1.
|
|
1253
|
+
--
|
|
1254
|
+
-- Using MAX(vnum) also protects against historical data where versions
|
|
1255
|
+
-- exist but there is unexpectedly no current validto IS NULL row.
|
|
1256
|
+
-- ---------------------------------------------------------------------
|
|
1257
|
+
|
|
1258
|
+
SELECT
|
|
1259
|
+
COALESCE(MAX(e.vnum), 0) + 1
|
|
1260
|
+
INTO
|
|
1261
|
+
__newvnum
|
|
1262
|
+
FROM public.stsentity e
|
|
1263
|
+
WHERE e.resourceoid = __resoid
|
|
1264
|
+
AND e.entname = _entname;
|
|
1265
|
+
|
|
1266
|
+
END IF;
|
|
1267
|
+
|
|
1268
|
+
-- -------------------------------------------------------------------------
|
|
1269
|
+
-- Create new active entity version.
|
|
1270
|
+
-- -------------------------------------------------------------------------
|
|
1271
|
+
|
|
1272
|
+
INSERT INTO public.stsentity(
|
|
1273
|
+
resourceoid,
|
|
1274
|
+
entname,
|
|
1275
|
+
entvalue,
|
|
1276
|
+
entvalue_jsonb,
|
|
1277
|
+
vnum,
|
|
1278
|
+
dbaction,
|
|
1279
|
+
dbactionuser,
|
|
1280
|
+
validfrom
|
|
1281
|
+
)
|
|
1282
|
+
VALUES (
|
|
1283
|
+
__resoid,
|
|
1284
|
+
_entname,
|
|
1285
|
+
_entvalue,
|
|
1286
|
+
__entvalue_jsonb,
|
|
1287
|
+
__newvnum,
|
|
1288
|
+
1,
|
|
1289
|
+
_dbactionuser,
|
|
1290
|
+
__now
|
|
1291
|
+
)
|
|
1292
|
+
RETURNING oid
|
|
1293
|
+
INTO __inserted_oid;
|
|
1294
|
+
|
|
1295
|
+
-- -------------------------------------------------------------------------
|
|
1296
|
+
-- Return newly created entity.
|
|
1297
|
+
-- -------------------------------------------------------------------------
|
|
1298
|
+
|
|
1299
|
+
RETURN QUERY
|
|
1300
|
+
SELECT *
|
|
1301
|
+
FROM public.stsentity
|
|
1302
|
+
WHERE oid = __inserted_oid;
|
|
1303
|
+
|
|
1304
|
+
END;
|
|
1305
|
+
$BODY$;
|
|
1306
|
+
|
|
1307
|
+
ALTER FUNCTION public.create_stsentity_latest(
|
|
1308
|
+
character varying,
|
|
1309
|
+
character varying,
|
|
1310
|
+
character varying,
|
|
1311
|
+
character varying
|
|
1312
|
+
)
|
|
1313
|
+
OWNER TO postgres;
|
|
958
1314
|
|
|
959
|
-
-- FUNCTION: public.update_stsentity(character varying, character varying, integer, character varying, integer, character varying)
|
|
960
1315
|
|
|
961
|
-
-- DROP FUNCTION public.update_stsentity(character varying, character varying, integer, character varying, integer, character varying);
|
|
962
1316
|
|
|
963
1317
|
CREATE OR REPLACE FUNCTION public.update_stsentity(
|
|
964
1318
|
_dbactionuser character varying,
|
|
965
1319
|
_resname character varying,
|
|
966
|
-
_resvnum
|
|
1320
|
+
_resvnum bigint,
|
|
967
1321
|
_entname character varying,
|
|
968
|
-
_entvnum
|
|
1322
|
+
_entvnum bigint,
|
|
969
1323
|
_entvalue character varying)
|
|
970
1324
|
RETURNS SETOF stsentity
|
|
971
1325
|
LANGUAGE 'plpgsql'
|
|
@@ -1025,13 +1379,11 @@ BEGIN
|
|
|
1025
1379
|
END;
|
|
1026
1380
|
$BODY$;
|
|
1027
1381
|
|
|
1028
|
-
ALTER FUNCTION public.update_stsentity(character varying, character varying,
|
|
1382
|
+
ALTER FUNCTION public.update_stsentity(character varying, character varying, bigint, character varying, bigint, character varying)
|
|
1029
1383
|
OWNER TO postgres;
|
|
1030
1384
|
|
|
1031
1385
|
|
|
1032
1386
|
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
1387
|
CREATE OR REPLACE FUNCTION public.update_stsentity_latest(
|
|
1036
1388
|
_dbactionuser character varying,
|
|
1037
1389
|
_resname character varying,
|
|
@@ -1050,7 +1402,7 @@ declare
|
|
|
1050
1402
|
resoid bigint;
|
|
1051
1403
|
entoid bigint;
|
|
1052
1404
|
__inserted_oid bigint;
|
|
1053
|
-
_entvnum
|
|
1405
|
+
_entvnum bigint;
|
|
1054
1406
|
_entvalue_jsonb JSONB;
|
|
1055
1407
|
BEGIN
|
|
1056
1408
|
begin
|
|
@@ -1107,16 +1459,12 @@ ALTER FUNCTION public.update_stsentity_latest(character varying, character varyi
|
|
|
1107
1459
|
OWNER TO postgres;
|
|
1108
1460
|
|
|
1109
1461
|
|
|
1110
|
-
-- FUNCTION: public.delete_stsentity(character varying, character varying, integer, character varying, integer)
|
|
1111
|
-
|
|
1112
|
-
-- DROP FUNCTION public.delete_stsentity(character varying, character varying, integer, character varying, integer);
|
|
1113
|
-
|
|
1114
1462
|
CREATE OR REPLACE FUNCTION public.delete_stsentity(
|
|
1115
1463
|
_dbactionuser character varying,
|
|
1116
1464
|
_resname character varying,
|
|
1117
|
-
_resvnum
|
|
1465
|
+
_resvnum bigint,
|
|
1118
1466
|
_entname character varying,
|
|
1119
|
-
_entvnum
|
|
1467
|
+
_entvnum bigint)
|
|
1120
1468
|
RETURNS SETOF stsentity
|
|
1121
1469
|
LANGUAGE 'plpgsql'
|
|
1122
1470
|
|
|
@@ -1168,14 +1516,11 @@ BEGIN
|
|
|
1168
1516
|
END;
|
|
1169
1517
|
$BODY$;
|
|
1170
1518
|
|
|
1171
|
-
ALTER FUNCTION public.delete_stsentity(character varying, character varying,
|
|
1519
|
+
ALTER FUNCTION public.delete_stsentity(character varying, character varying, bigint, character varying, bigint)
|
|
1172
1520
|
OWNER TO postgres;
|
|
1173
1521
|
|
|
1174
1522
|
|
|
1175
1523
|
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
1524
|
CREATE OR REPLACE FUNCTION public.delete_stsentity_latest(
|
|
1180
1525
|
_dbactionuser character varying,
|
|
1181
1526
|
_resname character varying,
|
|
@@ -1193,7 +1538,7 @@ declare
|
|
|
1193
1538
|
resoid bigint;
|
|
1194
1539
|
entoid bigint;
|
|
1195
1540
|
__inserted_oid bigint;
|
|
1196
|
-
_entvnum
|
|
1541
|
+
_entvnum bigint;
|
|
1197
1542
|
BEGIN
|
|
1198
1543
|
BEGIN
|
|
1199
1544
|
-- Ensure we get the current non-deleted resource.
|