Skip to content

Commit f55d2a7

Browse files
committed
updated 3DCityDB delete script
1 parent bfddfc9 commit f55d2a7

File tree

1 file changed

+109
-0
lines changed
  • resources/3dcitydb/postgresql/sql-scripts/citydb-pkg

1 file changed

+109
-0
lines changed

resources/3dcitydb/postgresql/sql-scripts/citydb-pkg/delete.sql

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
* delete_address(pid_array bigint[], schema_name TEXT) RETURNS SETOF BIGINT
3737
* delete_address(pid bigint) RETURNS BIGINT
3838
* delete_address(pid bigint, schema_name TEXT) RETURNS BIGINT
39+
* terminate_feature(pid_array bigint[], metadata JSON DEFAULT '{}', cascade BOOLEAN DEFAULT TRUE) RETURNS SETOF BIGINT
40+
* terminate_feature(pid_array bigint[], schema_name TEXT, metadata JSON DEFAULT '{}', cascade BOOLEAN DEFAULT TRUE) RETURNS SETOF BIGINT
41+
* terminate_feature(pid bigint, metadata JSON DEFAULT '{}', cascade BOOLEAN DEFAULT TRUE) RETURNS BIGINT
42+
* terminate_feature(pid bigint, schema_name TEXT, metadata JSON DEFAULT '{}', cascade BOOLEAN DEFAULT TRUE) RETURNS BIGINT
3943
******************************************************************/
4044

4145
/******************************************************************
@@ -796,4 +800,109 @@ BEGIN
796800
RETURN citydb_pkg.del_address(ARRAY[pid]);
797801
END;
798802
$body$
803+
LANGUAGE plpgsql STRICT;
804+
805+
/******************************************************************
806+
* terminate feature based on an id array
807+
******************************************************************/
808+
CREATE OR REPLACE FUNCTION citydb_pkg.terminate_feature(pid_array bigint[], metadata JSON DEFAULT '{}', cascade BOOLEAN DEFAULT TRUE) RETURNS SETOF BIGINT AS
809+
$body$
810+
DECLARE
811+
terminated_ids bigint[] := '{}';
812+
child_feature_ids bigint[] := '{}';
813+
BEGIN
814+
WITH terminated_objects AS (
815+
UPDATE
816+
feature f
817+
SET
818+
termination_date = COALESCE((metadata->>'termination_date')::timestamp with time zone, now()),
819+
last_modification_date = COALESCE((metadata->>'last_modification_date')::timestamp with time zone, now()),
820+
reason_for_update = COALESCE(metadata->>'reason_for_update', f.reason_for_update),
821+
updating_person = COALESCE(metadata->>'updating_person', USER),
822+
lineage = COALESCE(metadata->>'lineage', f.lineage)
823+
FROM
824+
unnest($1) a(a_id)
825+
WHERE
826+
f.id = a.a_id
827+
RETURNING
828+
f.id
829+
)
830+
SELECT
831+
array_agg(id)
832+
INTO
833+
terminated_ids
834+
FROM
835+
terminated_objects;
836+
837+
if cascade THEN
838+
SELECT
839+
array_agg(val_feature_id)
840+
INTO
841+
child_feature_ids
842+
FROM
843+
property p, unnest(terminated_ids) a(a_id)
844+
WHERE
845+
p.feature_id = a.a_id AND val_relation_type = 1;
846+
847+
IF -1 = ALL(child_feature_ids) IS NOT NULL THEN
848+
PERFORM
849+
citydb_pkg.terminate_feature(array_agg(a.a_id), metadata, cascade)
850+
FROM
851+
(SELECT DISTINCT unnest(child_feature_ids) AS a_id) a
852+
WHERE NOT EXISTS
853+
(
854+
SELECT
855+
1
856+
FROM
857+
property p
858+
INNER JOIN
859+
feature f
860+
ON f.id = p.feature_id
861+
WHERE p.val_feature_id = a.a_id AND f.termination_date IS NULL AND p.val_relation_type = 1
862+
);
863+
END IF;
864+
END IF;
865+
866+
RETURN QUERY
867+
SELECT unnest(terminated_ids);
868+
END;
869+
$body$
870+
LANGUAGE plpgsql STRICT;
871+
872+
/******************************************************************
873+
* terminate features based on an id array and schema name
874+
******************************************************************/
875+
CREATE OR REPLACE FUNCTION citydb_pkg.terminate_feature(pid_array bigint[], schema_name TEXT, metadata JSON DEFAULT '{}', cascade BOOLEAN DEFAULT TRUE) RETURNS SETOF BIGINT AS
876+
$body$
877+
BEGIN
878+
EXECUTE format('set search_path to %I, public', schema_name);
879+
880+
RETURN QUERY
881+
SELECT citydb_pkg.terminate_feature(pid_array, metadata, cascade);
882+
END;
883+
$body$
884+
LANGUAGE plpgsql STRICT;
885+
886+
/******************************************************************
887+
* terminate a feature based on an id and schema name
888+
******************************************************************/
889+
CREATE OR REPLACE FUNCTION citydb_pkg.terminate_feature(pid bigint, metadata JSON DEFAULT '{}', cascade BOOLEAN DEFAULT TRUE) RETURNS BIGINT AS
890+
$body$
891+
BEGIN
892+
RETURN citydb_pkg.terminate_feature(ARRAY[pid], metadata, cascade);
893+
END;
894+
$body$
895+
LANGUAGE plpgsql STRICT;
896+
897+
/******************************************************************
898+
* terminate a feature based on an id and schema name
899+
******************************************************************/
900+
CREATE OR REPLACE FUNCTION citydb_pkg.terminate_feature(pid bigint, schema_name TEXT, metadata JSON DEFAULT '{}', cascade BOOLEAN DEFAULT TRUE) RETURNS BIGINT AS
901+
$body$
902+
BEGIN
903+
EXECUTE format('set search_path to %I, public', schema_name);
904+
905+
RETURN citydb_pkg.terminate_feature(ARRAY[pid], metadata, cascade);
906+
END;
907+
$body$
799908
LANGUAGE plpgsql STRICT;

0 commit comments

Comments
 (0)