Skip to content

Commit

Permalink
fix(ftrg): check first if addfield table exists before do action, and…
Browse files Browse the repository at this point in the history
… remove trailing whitespaces
  • Loading branch information
danimarinBG committed Aug 22, 2024
1 parent ede26c4 commit 6371e06
Show file tree
Hide file tree
Showing 7 changed files with 462 additions and 383 deletions.
38 changes: 25 additions & 13 deletions ud/ftrg/ud_gw_trg_edit_arc.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ v_seq_name text;
v_seq_code text;
v_code_prefix text;
v_arc_id text;
v_childtable_name text;

BEGIN
EXECUTE 'SET search_path TO '||quote_literal(TG_TABLE_SCHEMA)||', public';
Expand Down Expand Up @@ -516,9 +517,12 @@ BEGIN
USING NEW
INTO v_new_value_param;

IF v_new_value_param IS NOT NULL THEN
EXECUTE 'INSERT INTO man_arc_'||lower(v_customfeature)||' (arc_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')'
USING NEW.arc_id, v_new_value_param;
v_childtable_name := 'man_arc_' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
IF v_new_value_param IS NOT NULL THEN
EXECUTE 'INSERT INTO '||v_childtable_name||' (arc_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')'
USING NEW.arc_id, v_new_value_param;
END IF;
END IF;
END LOOP;
END IF;
Expand Down Expand Up @@ -730,15 +734,20 @@ BEGIN
USING OLD
INTO v_old_value_param;

IF (v_new_value_param IS NOT NULL AND v_old_value_param!=v_new_value_param) OR (v_new_value_param IS NOT NULL AND v_old_value_param IS NULL) THEN
EXECUTE 'INSERT INTO man_arc_'||lower(v_customfeature)||' (arc_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')
ON CONFLICT (arc_id)
DO UPDATE SET '||v_addfields.param_name||'=$2::'||v_addfields.datatype_id||' WHERE man_arc_'||lower(v_customfeature)||'.arc_id=$1'
USING NEW.arc_id, v_new_value_param;

ELSIF v_new_value_param IS NULL AND v_old_value_param IS NOT NULL THEN
EXECUTE 'UPDATE man_arc_'||lower(v_customfeature)||' SET '||v_addfields.param_name||' = null WHERE man_arc_'||lower(v_customfeature)||'.arc_id=$1'
USING NEW.arc_id;

v_childtable_name := 'man_arc_' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
IF (v_new_value_param IS NOT NULL AND v_old_value_param!=v_new_value_param) OR (v_new_value_param IS NOT NULL AND v_old_value_param IS NULL) THEN
EXECUTE 'INSERT INTO '||v_childtable_name||' (arc_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')
ON CONFLICT (arc_id)
DO UPDATE SET '||v_addfields.param_name||'=$2::'||v_addfields.datatype_id||' WHERE '||v_childtable_name||'.arc_id=$1'
USING NEW.arc_id, v_new_value_param;

ELSIF v_new_value_param IS NULL AND v_old_value_param IS NOT NULL THEN
EXECUTE 'UPDATE '||v_childtable_name||' SET '||v_addfields.param_name||' = null WHERE '||v_childtable_name||'.arc_id=$1'
USING NEW.arc_id;
END IF;
END IF;
END LOOP;
END IF;
Expand Down Expand Up @@ -770,8 +779,11 @@ BEGIN
-- Delete childtable addfields (after or before deletion of arc, doesn't matter)
v_customfeature = old.arc_type;
v_arc_id = old.arc_id;

EXECUTE 'DELETE FROM man_arc_'||lower(v_customfeature)||' WHERE arc_id = '||quote_literal(v_arc_id)||'';

v_childtable_name := 'man_arc_' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
EXECUTE 'DELETE FROM '||v_childtable_name||' WHERE arc_id = '||quote_literal(v_arc_id)||'';
END IF;

RETURN NULL;

Expand Down
40 changes: 26 additions & 14 deletions ud/ftrg/ud_gw_trg_edit_connec.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ v_seq_name text;
v_seq_code text;
v_code_prefix text;
v_connec_id text;
v_childtable_name text;

BEGIN

Expand Down Expand Up @@ -546,10 +547,14 @@ BEGIN
USING NEW
INTO v_new_value_param;

IF v_new_value_param IS NOT NULL THEN
EXECUTE 'INSERT INTO man_connec_'||lower(v_customfeature)||' (connec_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')'
USING NEW.connec_id, v_new_value_param;
v_childtable_name := 'man_connec' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
IF v_new_value_param IS NOT NULL THEN
EXECUTE 'INSERT INTO '||v_childtable_name||' (connec_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')'
USING NEW.connec_id, v_new_value_param;
END IF;
END IF;

END LOOP;
END IF;

Expand Down Expand Up @@ -778,15 +783,18 @@ BEGIN
USING OLD
INTO v_old_value_param;

IF (v_new_value_param IS NOT NULL AND v_old_value_param!=v_new_value_param) OR (v_new_value_param IS NOT NULL AND v_old_value_param IS NULL) THEN
EXECUTE 'INSERT INTO man_connec_'||lower(v_customfeature)||' (connec_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')
ON CONFLICT (connec_id)
DO UPDATE SET '||v_addfields.param_name||'=$2::'||v_addfields.datatype_id||' WHERE man_connec_'||lower(v_customfeature)||'.connec_id=$1'
USING NEW.connec_id, v_new_value_param;

ELSIF v_new_value_param IS NULL AND v_old_value_param IS NOT NULL THEN
EXECUTE 'UPDATE man_connec_'||lower(v_customfeature)||' SET '||v_addfields.param_name||' = null WHERE man_connec_'||lower(v_customfeature)||'.connec_id=$1'
USING NEW.connec_id;
v_childtable_name := 'man_connec' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
IF (v_new_value_param IS NOT NULL AND v_old_value_param!=v_new_value_param) OR (v_new_value_param IS NOT NULL AND v_old_value_param IS NULL) THEN
EXECUTE 'INSERT INTO '||v_childtable_name||' (connec_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')
ON CONFLICT (connec_id)
DO UPDATE SET '||v_addfields.param_name||'=$2::'||v_addfields.datatype_id||' WHERE '||v_childtable_name||'.connec_id=$1'
USING NEW.connec_id, v_new_value_param;

ELSIF v_new_value_param IS NULL AND v_old_value_param IS NOT NULL THEN
EXECUTE 'UPDATE '||v_childtable_name||' SET '||v_addfields.param_name||' = null WHERE '||v_childtable_name||'.connec_id=$1'
USING NEW.connec_id;
END IF;
END IF;
END LOOP;
END IF;
Expand Down Expand Up @@ -814,8 +822,12 @@ BEGIN
-- Delete childtable addfields (after or before deletion of connec, doesn't matter)
v_customfeature = old.connec_type;
v_connec_id = old.connec_id;

EXECUTE 'DELETE FROM man_connec_'||lower(v_customfeature)||' WHERE connec_id = '||quote_literal(v_connec_id)||'';


v_childtable_name := 'man_connec' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
EXECUTE 'DELETE FROM '||v_childtable_name||' WHERE connec_id = '||quote_literal(v_connec_id)||'';
END IF;

-- delete links
FOR v_record_link IN SELECT * FROM link WHERE feature_type='CONNEC' AND feature_id=OLD.connec_id
Expand Down
38 changes: 25 additions & 13 deletions ud/ftrg/ud_gw_trg_edit_gully.sql
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ v_seq_name text;
v_seq_code text;
v_code_prefix text;
v_gully_id text;
v_childtable_name text;

BEGIN

Expand Down Expand Up @@ -643,9 +644,12 @@ BEGIN
USING NEW
INTO v_new_value_param;

IF v_new_value_param IS NOT NULL THEN
EXECUTE 'INSERT INTO man_gully_'||lower(v_customfeature)||' (gully_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')'
USING NEW.gully_id, v_new_value_param;
v_childtable_name := 'man_gully_' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
IF v_new_value_param IS NOT NULL THEN
EXECUTE 'INSERT INTO '||v_childtable_name||' (gully_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')'
USING NEW.gully_id, v_new_value_param;
END IF;
END IF;
END LOOP;
END IF;
Expand Down Expand Up @@ -942,15 +946,20 @@ BEGIN
USING OLD
INTO v_old_value_param;

IF (v_new_value_param IS NOT NULL AND v_old_value_param!=v_new_value_param) OR (v_new_value_param IS NOT NULL AND v_old_value_param IS NULL) THEN
EXECUTE 'INSERT INTO man_gully_'||lower(v_customfeature)||' (gully_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')
ON CONFLICT (gully_id)
DO UPDATE SET '||v_addfields.param_name||'=$2::'||v_addfields.datatype_id||' WHERE man_gully_'||lower(v_customfeature)||'.gully_id=$1'
USING NEW.gully_id, v_new_value_param;

ELSIF v_new_value_param IS NULL AND v_old_value_param IS NOT NULL THEN
EXECUTE 'UPDATE man_gully_'||lower(v_customfeature)||' SET '||v_addfields.param_name||' = null WHERE man_gully_'||lower(v_customfeature)||'.gully_id=$1'
USING NEW.gully_id;

v_childtable_name := 'man_gully_' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
IF (v_new_value_param IS NOT NULL AND v_old_value_param!=v_new_value_param) OR (v_new_value_param IS NOT NULL AND v_old_value_param IS NULL) THEN
EXECUTE 'INSERT INTO '||v_childtable_name||' (gully_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')
ON CONFLICT (gully_id)
DO UPDATE SET '||v_addfields.param_name||'=$2::'||v_addfields.datatype_id||' WHERE '||v_childtable_name||'.gully_id=$1'
USING NEW.gully_id, v_new_value_param;

ELSIF v_new_value_param IS NULL AND v_old_value_param IS NOT NULL THEN
EXECUTE 'UPDATE '||v_childtable_name||' SET '||v_addfields.param_name||' = null WHERE '||v_childtable_name||'.gully_id=$1'
USING NEW.gully_id;
END IF;
END IF;
END LOOP;
END IF;
Expand Down Expand Up @@ -978,8 +987,11 @@ BEGIN
-- Delete childtable addfields (after or before deletion of gully, doesn't matter)
v_customfeature = old.gully_type;
v_gully_id = old.gully_id;

EXECUTE 'DELETE FROM man_gully_'||lower(v_customfeature)||' WHERE gully_id = '||quote_literal(v_gully_id)||'';

v_childtable_name := 'man_gully_' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
EXECUTE 'DELETE FROM '||v_childtable_name||' WHERE gully_id = '||quote_literal(v_gully_id)||'';
END IF;

-- delete links
FOR v_record_link IN SELECT * FROM link WHERE feature_type='GULLY' AND feature_id=OLD.gully_id
Expand Down
39 changes: 25 additions & 14 deletions ud/ftrg/ud_gw_trg_edit_node.sql
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ v_input json;
v_auto_sander boolean;
v_node_id text;

v_childtable_name text;

BEGIN

EXECUTE 'SET search_path TO '||quote_literal(TG_TABLE_SCHEMA)||', public';
Expand Down Expand Up @@ -693,9 +695,12 @@ BEGIN
USING NEW
INTO v_new_value_param;

IF v_new_value_param IS NOT NULL THEN
EXECUTE 'INSERT INTO man_node_'||lower(v_customfeature)||' (node_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')'
USING NEW.node_id, v_new_value_param;
v_childtable_name := 'man_node' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
IF v_new_value_param IS NOT NULL THEN
EXECUTE 'INSERT INTO '||v_childtable_name||' (node_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')'
USING NEW.node_id, v_new_value_param;
END IF;
END IF;
END LOOP;
END IF;
Expand Down Expand Up @@ -966,15 +971,18 @@ BEGIN
USING OLD
INTO v_old_value_param;

IF (v_new_value_param IS NOT NULL AND v_old_value_param!=v_new_value_param) OR (v_new_value_param IS NOT NULL AND v_old_value_param IS NULL) THEN
EXECUTE 'INSERT INTO man_node_'||lower(v_customfeature)||' (node_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')
ON CONFLICT (node_id)
DO UPDATE SET '||v_addfields.param_name||'=$2::'||v_addfields.datatype_id||' WHERE man_node_'||lower(v_customfeature)||'.node_id=$1'
USING NEW.node_id, v_new_value_param;

ELSIF v_new_value_param IS NULL AND v_old_value_param IS NOT NULL THEN
EXECUTE 'UPDATE man_node_'||lower(v_customfeature)||' SET '||v_addfields.param_name||' = null WHERE man_node_'||lower(v_customfeature)||'.node_id=$1'
USING NEW.node_id;
v_childtable_name := 'man_node' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
IF (v_new_value_param IS NOT NULL AND v_old_value_param!=v_new_value_param) OR (v_new_value_param IS NOT NULL AND v_old_value_param IS NULL) THEN
EXECUTE 'INSERT INTO '||v_childtable_name||' (node_id, '||v_addfields.param_name||') VALUES ($1, $2::'||v_addfields.datatype_id||')
ON CONFLICT (node_id)
DO UPDATE SET '||v_addfields.param_name||'=$2::'||v_addfields.datatype_id||' WHERE '||v_childtable_name||'.node_id=$1'
USING NEW.node_id, v_new_value_param;

ELSIF v_new_value_param IS NULL AND v_old_value_param IS NOT NULL THEN
EXECUTE 'UPDATE '||v_childtable_name||' SET '||v_addfields.param_name||' = null WHERE '||v_childtable_name||'.node_id=$1'
USING NEW.node_id;
END IF;
END IF;
END LOOP;
END IF;
Expand Down Expand Up @@ -1005,8 +1013,11 @@ BEGIN
-- Delete childtable addfields (after or before deletion of node, doesn't matter)
v_customfeature = old.node_type;
v_node_id = old.node_id;

EXECUTE 'DELETE FROM man_node_'||lower(v_customfeature)||' WHERE node_id = '||quote_literal(v_node_id)||'';

v_childtable_name := 'man_node' || lower(v_customfeature);
IF (SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = TG_TABLE_SCHEMA AND table_name = v_childtable_name)) IS TRUE THEN
EXECUTE 'DELETE FROM '||v_childtable_name||' WHERE node_id = '||quote_literal(v_node_id)||'';
END IF;

RETURN NULL;
END IF;
Expand Down
Loading

0 comments on commit 6371e06

Please sign in to comment.