@nsshunt/stsdatamanagement 1.18.196 → 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 +210 -36
- package/package.json +1 -1
package/db-scripts/builddb.sql
CHANGED
|
@@ -1092,51 +1092,225 @@ CREATE OR REPLACE FUNCTION public.create_stsentity_latest(
|
|
|
1092
1092
|
_dbactionuser character varying,
|
|
1093
1093
|
_resname character varying,
|
|
1094
1094
|
_entname character varying,
|
|
1095
|
-
_entvalue character varying
|
|
1096
|
-
|
|
1097
|
-
|
|
1095
|
+
_entvalue character varying
|
|
1096
|
+
)
|
|
1097
|
+
RETURNS SETOF stsentity
|
|
1098
|
+
LANGUAGE 'plpgsql'
|
|
1099
|
+
COST 100
|
|
1100
|
+
VOLATILE
|
|
1101
|
+
ROWS 1000
|
|
1098
1102
|
|
|
1099
|
-
COST 100
|
|
1100
|
-
VOLATILE
|
|
1101
|
-
ROWS 1000
|
|
1102
|
-
|
|
1103
1103
|
AS $BODY$
|
|
1104
|
-
|
|
1105
|
-
|
|
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.
|
|
1106
1119
|
__inserted_oid bigint;
|
|
1107
|
-
|
|
1120
|
+
|
|
1121
|
+
-- Parsed JSON.
|
|
1122
|
+
__entvalue_jsonb jsonb;
|
|
1123
|
+
|
|
1108
1124
|
BEGIN
|
|
1109
|
-
begin
|
|
1110
|
-
_entvalue_jsonb := _entvalue::jsonb;
|
|
1111
|
-
exception
|
|
1112
|
-
when others then
|
|
1113
|
-
raise exception 'Invalid JSON string: %', _entvalue;
|
|
1114
|
-
end;
|
|
1115
1125
|
|
|
1116
|
-
--
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1126
|
+
-- -------------------------------------------------------------------------
|
|
1127
|
+
-- Validate / parse entity JSON
|
|
1128
|
+
-- -------------------------------------------------------------------------
|
|
1129
|
+
|
|
1130
|
+
BEGIN
|
|
1131
|
+
__entvalue_jsonb := _entvalue::jsonb;
|
|
1132
|
+
|
|
1133
|
+
EXCEPTION
|
|
1134
|
+
WHEN others THEN
|
|
1135
|
+
RAISE EXCEPTION
|
|
1136
|
+
'Invalid JSON string: %',
|
|
1137
|
+
_entvalue;
|
|
1138
|
+
END;
|
|
1139
|
+
|
|
1140
|
+
-- -------------------------------------------------------------------------
|
|
1141
|
+
-- Resolve the current, non-deleted resource.
|
|
1142
|
+
-- -------------------------------------------------------------------------
|
|
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;
|
|
1131
1303
|
|
|
1132
|
-
return query
|
|
1133
|
-
select * from stsentity where oid = __inserted_oid;
|
|
1134
|
-
END IF;
|
|
1135
1304
|
END;
|
|
1136
1305
|
$BODY$;
|
|
1137
1306
|
|
|
1138
|
-
ALTER FUNCTION public.create_stsentity_latest(
|
|
1139
|
-
|
|
1307
|
+
ALTER FUNCTION public.create_stsentity_latest(
|
|
1308
|
+
character varying,
|
|
1309
|
+
character varying,
|
|
1310
|
+
character varying,
|
|
1311
|
+
character varying
|
|
1312
|
+
)
|
|
1313
|
+
OWNER TO postgres;
|
|
1140
1314
|
|
|
1141
1315
|
|
|
1142
1316
|
|