tww
.tww_app
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
import_manhole_quarantine_try_let_update()
Parameters
Name
Type
Mode
IN
Definition
DECLARE let_kind text; new_lets integer; old_lets integer; BEGIN let_kind := TG_ARGV[0]; -- count new lets IF let_kind='inlet' AND ( NEW.inlet_3_material IS NOT NULL OR NEW.inlet_3_depth_m IS NOT NULL OR NEW.inlet_3_clear_height IS NOT NULL ) OR let_kind='outlet' AND ( NEW.outlet_1_material IS NOT NULL OR NEW.outlet_1_depth_m IS NOT NULL OR NEW.outlet_1_clear_height IS NOT NULL ) THEN IF let_kind='inlet' AND ( NEW.inlet_4_material IS NOT NULL OR NEW.inlet_4_depth_m IS NOT NULL OR NEW.inlet_4_clear_height IS NOT NULL ) OR let_kind='outlet' AND ( NEW.outlet_2_material IS NOT NULL OR NEW.outlet_2_depth_m IS NOT NULL OR NEW.outlet_2_clear_height IS NOT NULL ) THEN new_lets = 2; -- it's possibly more, but at least > 1 ELSE new_lets = 1; END IF; ELSE new_lets = 0; END IF; -- count old lets old_lets = ( SELECT COUNT (*) FROM tww_od.reach re LEFT JOIN tww_od.reach_point rp ON let_kind='inlet' AND rp.obj_id = re.fk_reach_point_to OR let_kind='outlet' AND rp.obj_id = re.fk_reach_point_from LEFT JOIN tww_od.wastewater_networkelement wn ON wn.obj_id = rp.fk_wastewater_networkelement LEFT JOIN tww_app.vw_tww_wastewater_structure ws ON ws.obj_id = wn.fk_wastewater_structure WHERE ws.obj_id = NEW.obj_id ); -- handle inlets IF ( new_lets > 1 AND old_lets > 0 ) OR old_lets > 1 THEN -- request for update because new lets are bigger 1 (and old lets not 0 ) or old lets are bigger 1 RAISE NOTICE 'Impossible to assign %s - manual edit needed.', let_kind; ELSE IF new_lets = 0 AND old_lets > 0 THEN -- request for delete because no new lets but old lets RAISE NOTICE 'No new %s but old ones - manual delete needed.', let_kind; ELSIF new_lets > 0 AND old_lets = 0 THEN -- request for create because no old lets but new lets RAISE NOTICE 'No old %s but new ones - manual create needed.', let_kind; ELSE IF new_lets = 1 AND old_lets = 1 THEN IF let_kind='inlet' THEN -- update material and dimension on reach UPDATE tww_od.reach SET material = NEW.inlet_3_material, clear_height = NEW.inlet_3_clear_height WHERE obj_id = ( SELECT re.obj_id FROM tww_od.reach re LEFT JOIN tww_od.reach_point rp ON rp.obj_id = re.fk_reach_point_to LEFT JOIN tww_od.wastewater_networkelement wn ON wn.obj_id = rp.fk_wastewater_networkelement LEFT JOIN tww_app.vw_tww_wastewater_structure ws ON ws.obj_id = wn.fk_wastewater_structure WHERE ws.obj_id = NEW.obj_id ); -- update depth_m on reach_point UPDATE tww_od.reach_point SET level = NEW.co_level - NEW.inlet_3_depth_m WHERE obj_id = ( SELECT rp.obj_id FROM tww_od.reach re LEFT JOIN tww_od.reach_point rp ON rp.obj_id = re.fk_reach_point_to LEFT JOIN tww_od.wastewater_networkelement wn ON wn.obj_id = rp.fk_wastewater_networkelement LEFT JOIN tww_app.vw_tww_wastewater_structure ws ON ws.obj_id = wn.fk_wastewater_structure WHERE ws.obj_id = NEW.obj_id ); ELSE -- update material on reach UPDATE tww_od.reach SET material = NEW.outlet_1_material, clear_height = NEW.outlet_1_clear_height WHERE obj_id = ( SELECT re.obj_id FROM tww_od.reach re LEFT JOIN tww_od.reach_point rp ON rp.obj_id = re.fk_reach_point_from LEFT JOIN tww_od.wastewater_networkelement wn ON wn.obj_id = rp.fk_wastewater_networkelement LEFT JOIN tww_app.vw_tww_wastewater_structure ws ON ws.obj_id = wn.fk_wastewater_structure WHERE ws.obj_id = NEW.obj_id ); -- update depth_m on reach_point UPDATE tww_od.reach_point SET level = NEW.co_level - NEW.outlet_1_depth_m WHERE obj_id = ( SELECT rp.obj_id FROM tww_od.reach re LEFT JOIN tww_od.reach_point rp ON rp.obj_id = re.fk_reach_point_from LEFT JOIN tww_od.wastewater_networkelement wn ON wn.obj_id = rp.fk_wastewater_networkelement LEFT JOIN tww_app.vw_tww_wastewater_structure ws ON ws.obj_id = wn.fk_wastewater_structure WHERE ws.obj_id = NEW.obj_id ); END IF; RAISE NOTICE '%s updated', let_kind; ELSE -- do nothing RAISE NOTICE 'No %s - nothing to do', let_kind; END IF; IF let_kind='inlet' THEN -- set inlet okay UPDATE tww_od.import_manhole_quarantine SET inlet_okay = true WHERE quarantine_serial = NEW.quarantine_serial; ELSE -- set outlet okay UPDATE tww_od.import_manhole_quarantine SET outlet_okay = true WHERE quarantine_serial = NEW.quarantine_serial; END IF; END IF; END IF; RETURN NEW; -- catch EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'EXCEPTION: %', SQLERRM; RETURN NEW; END;