From 3350409f04c822f7a4360662466e2abad5a519f9 Mon Sep 17 00:00:00 2001 From: lpofredc Date: Thu, 5 Sep 2024 12:28:51 +0200 Subject: [PATCH] precommit --- .pre-commit-config.yaml | 18 +- 07_functions.sql | 535 ++++++------ 08_upsert_observations.sql | 1693 +++++++++++++++++------------------- 3 files changed, 1092 insertions(+), 1154 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ec82e6f..9a12502 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,19 +12,13 @@ repos: - id: fix-byte-order-marker - id: fix-encoding-pragma args: [--remove] - - id: trailing-whitespace - args: [--markdown-linebreak-ext=md] - - repo: https://github.com/sqlfluff/sqlfluff - rev: 2.1.2 + # - id: trailing-whitespace + # args: [--markdown-linebreak-ext=md] + - repo: https://github.com/amureki/pre-commit-pgFormatter + rev: v1.3.0 # Use the sha / tag you want to point at hooks: - - id: sqlfluff-lint - args: - - "--dialect" - - "postgres" - - id: sqlfluff-fix - args: - - "--dialect" - - "postgres" + - id: pg_format + args: [-b,-f1,-C,-U2,-k,-g,-w80] ci: diff --git a/07_functions.sql b/07_functions.sql index d4f2cd8..938378d 100644 --- a/07_functions.sql +++ b/07_functions.sql @@ -2,380 +2,395 @@ FUNCTIONS --------- A collection of various helper fonctions -*/ - - + */ /* Function to get taxo group from visionature id_species */ - BEGIN; -DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_taxo_group_values_from_vn ( - _key TEXT, _site TEXT, _id INTEGER, OUT _result TEXT -); +DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_taxo_group_values_from_vn (_key + TEXT , _site TEXT , _id INTEGER , OUT _result TEXT); -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_taxo_group_values_from_vn( - _key TEXT, _site TEXT, _id INTEGER, OUT _result TEXT -) -RETURNS TEXT -LANGUAGE plpgsql -AS -$$ +CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_taxo_group_values_from_vn + (_key TEXT , _site TEXT , _id INTEGER , OUT _result TEXT) + RETURNS TEXT + LANGUAGE plpgsql + AS $$ BEGIN EXECUTE 'select item ->> $1 from src_vn_json.taxo_groups_json where taxo_groups_json.id = $3 and taxo_groups_json.site like $2 limit 1;' INTO _result - USING _key, _site, _id; + USING _key , _site , _id; END; $$; -COMMENT ON FUNCTION src_lpodatas.fct_c_get_taxo_group_values_from_vn( - _key TEXT, _site TEXT, _id INTEGER, OUT _result TEXT -) IS 'Function to get taxo group from visionature id_species'; +COMMENT ON FUNCTION src_lpodatas.fct_c_get_taxo_group_values_from_vn (_key TEXT + , _site TEXT , _id INTEGER , OUT _result TEXT) IS 'Function to get taxo group from visionature id_species'; /* Function to get taxref datas from VN id_sp */ -DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_taxref_values_from_vn ( - _field_name ANYELEMENT, _id_species INTEGER, OUT _result ANYELEMENT -); - -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_taxref_values_from_vn( - _field_name ANYELEMENT, _id_species INTEGER, OUT _result ANYELEMENT -) -RETURNS ANYELEMENT -LANGUAGE plpgsql -AS -$$ +DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_taxref_values_from_vn + (_field_name ANYELEMENT , _id_species INTEGER , OUT _result ANYELEMENT); + +CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_taxref_values_from_vn + (_field_name ANYELEMENT , _id_species INTEGER , OUT _result ANYELEMENT) + RETURNS ANYELEMENT + LANGUAGE plpgsql + AS $$ BEGIN - EXECUTE format( - 'SELECT taxref.%I from taxonomie.cor_c_vn_taxref join taxonomie.taxref on cor_c_vn_taxref.cd_nom = taxref.cd_nom where vn_id = $1 limit 1', - _field_name) INTO _result - USING _id_species; + EXECUTE format('SELECT taxref.%I from taxonomie.cor_c_vn_taxref join taxonomie.taxref on cor_c_vn_taxref.cd_nom = taxref.cd_nom where vn_id = $1 limit 1' , _field_name) INTO _result + USING _id_species; END; $$; -COMMENT ON FUNCTION src_lpodatas.fct_c_get_taxref_values_from_vn( - _field_name ANYELEMENT, _id_species INTEGER, OUT _result ANYELEMENT -) IS 'Function to get taxref datas from VN id_sp'; +COMMENT ON FUNCTION src_lpodatas.fct_c_get_taxref_values_from_vn (_field_name + ANYELEMENT , _id_species INTEGER , OUT _result ANYELEMENT) IS 'Function to get taxref datas from VN id_sp'; /* Function to get visionature species datas from VN id_sp */ -DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_species_values_from_vn ( - _key ANYELEMENT, _id_species INTEGER, OUT _result ANYELEMENT -); - -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_species_values_from_vn( - _key ANYELEMENT, _id_species INTEGER, OUT _result ANYELEMENT -) -RETURNS ANYELEMENT -LANGUAGE plpgsql -AS -$$ +DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_species_values_from_vn (_key + ANYELEMENT , _id_species INTEGER , OUT _result ANYELEMENT); + +CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_species_values_from_vn (_key + ANYELEMENT , _id_species INTEGER , OUT _result ANYELEMENT) + RETURNS ANYELEMENT + LANGUAGE plpgsql + AS $$ BEGIN EXECUTE 'select item ->> $1 from src_vn_json.species_json where species_json.id = $2 limit 1;' INTO _result - USING _key, _id_species; + USING _key , _id_species; END; $$; -COMMENT ON FUNCTION src_lpodatas.fct_c_get_species_values_from_vn( - _key ANYELEMENT, _id_species INTEGER, OUT _result ANYELEMENT -) IS 'Function to get visionature species datas from VN id_sp'; +COMMENT ON FUNCTION src_lpodatas.fct_c_get_species_values_from_vn (_key + ANYELEMENT , _id_species INTEGER , OUT _result ANYELEMENT) IS 'Function to get visionature species datas from VN id_sp'; /* Function to get observer full name from VisioNature observer universal id*/ -DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_observer_full_name_from_vn ( - _id_universal INTEGER, OUT _result TEXT -); - -CREATE FUNCTION src_lpodatas.fct_c_get_observer_full_name_from_vn( - _id_universal INTEGER, OUT _result TEXT -) -RETURNS TEXT -LANGUAGE plpgsql -AS -$$ +DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_observer_full_name_from_vn + (_id_universal INTEGER , OUT _result TEXT); + +CREATE FUNCTION src_lpodatas.fct_c_get_observer_full_name_from_vn + (_id_universal INTEGER , OUT _result TEXT) + RETURNS TEXT + LANGUAGE plpgsql + AS $$ BEGIN EXECUTE format('select concat(UPPER(item ->> ''name''), '' '', item ->> ''surname'') as text from src_vn_json.observers_json where observers_json.id_universal = $1 limit 1') INTO _result - USING _id_universal; + USING _id_universal; END; $$; - -COMMENT ON FUNCTION src_lpodatas.fct_c_get_observer_full_name_from_vn( - _id_universal INTEGER, OUT _result TEXT -) IS 'Function to get observer full name from VisioNature observer universal id'; +COMMENT ON FUNCTION src_lpodatas.fct_c_get_observer_full_name_from_vn + (_id_universal INTEGER , OUT _result TEXT) IS 'Function to get observer full name from VisioNature observer universal id'; /* Function to get entity name from VisioNature observer universal id */ -DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_entity_from_observer_site_uid ( - _uid INTEGER, _site TEXT, OUT _result TEXT -); - -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_entity_from_observer_site_uid( - _uid INTEGER, _site TEXT, OUT _result TEXT -) -RETURNS TEXT -LANGUAGE plpgsql -AS -$$ +DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_entity_from_observer_site_uid + (_uid INTEGER , _site TEXT , OUT _result TEXT); + +CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_entity_from_observer_site_uid + (_uid INTEGER , _site TEXT , OUT _result TEXT) + RETURNS TEXT + LANGUAGE plpgsql + AS $$ BEGIN - SELECT INTO _result CASE - WHEN ent.item ->> 'short_name' = '-' THEN - NULL - ELSE - ent.item ->> 'short_name' - END - FROM src_vn_json.observers_json usr - JOIN src_vn_json.entities_json ent ON (usr.site, - cast(usr.item ->> 'id_entity' AS INT)) = (ent.site, - ent.id) - WHERE usr.id_universal = _uid - AND usr.site = _site; + SELECT + INTO _result CASE WHEN ent.item ->> 'short_name' = '-' THEN + NULL + ELSE + ent.item ->> 'short_name' + END + FROM + src_vn_json.observers_json usr + JOIN src_vn_json.entities_json ent ON (usr.site + , cast(usr.item ->> 'id_entity' AS INT)) = (ent.site + , ent.id) + WHERE + usr.id_universal = _uid + AND usr.site = _site; END; $$; -COMMENT ON FUNCTION src_lpodatas.fct_c_get_entity_from_observer_site_uid( - _uid INTEGER, _site TEXT, OUT _result TEXT -) IS 'Function to get entity name from VisioNature observer universal id'; +COMMENT ON FUNCTION src_lpodatas.fct_c_get_entity_from_observer_site_uid (_uid + INTEGER , _site TEXT , OUT _result TEXT) IS 'Function to get entity name from VisioNature observer universal id'; /* Function to generate an array of behaviours from VisioNature datas */ -DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array ( - _behaviours JSONB, OUT _result TEXT [] -); - -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array( - _behaviours JSONB, OUT _result TEXT [] -) -RETURNS TEXT [] -LANGUAGE plpgsql -AS -$$ +DROP FUNCTION IF EXISTS + src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array (_behaviours + JSONB , OUT _result TEXT[]); + +CREATE OR REPLACE FUNCTION + src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array (_behaviours + JSONB , OUT _result TEXT[]) + RETURNS TEXT[] + LANGUAGE plpgsql + AS $$ DECLARE _array_id TEXT[]; BEGIN IF _behaviours IS NOT NULL THEN - SELECT array_agg(u.x)::TEXT[] - INTO _array_id - FROM (SELECT t.value ->> '@id' AS x - FROM jsonb_array_elements(_behaviours) AS t) AS u; - SELECT INTO _result array_agg(item ->> 'text') - FROM src_vn_json.field_details_json - WHERE id IN (SELECT unnest(_array_id)); + SELECT + array_agg(u.x)::TEXT[] INTO _array_id + FROM ( + SELECT + t.value ->> '@id' AS x + FROM + jsonb_array_elements(_behaviours) AS t) AS u; + SELECT + INTO _result array_agg(item ->> 'text') + FROM + src_vn_json.field_details_json + WHERE + id IN ( + SELECT + unnest(_array_id)); ELSE - SELECT NULL - INTO _result; + SELECT + NULL INTO _result; END IF; END; $$; -COMMENT ON FUNCTION src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array( - _behaviours JSONB, OUT _result TEXT [] -) IS 'Function to generate an array of behaviours from VisioNature datas'; +COMMENT ON FUNCTION src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array + (_behaviours JSONB , OUT _result TEXT[]) IS 'Function to generate an array of behaviours from VisioNature datas'; /* list visionature medias URL from medias details */ -DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array ( - _medias JSONB, OUT _result TEXT -); - -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array( - _medias JSONB, OUT _result TEXT -) -RETURNS TEXT -LANGUAGE plpgsql -AS -$$ +DROP FUNCTION IF EXISTS + src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array (_medias + JSONB , OUT _result TEXT); + +CREATE OR REPLACE FUNCTION + src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array (_medias + JSONB , OUT _result TEXT) + RETURNS TEXT + LANGUAGE plpgsql + AS $$ BEGIN IF _medias IS NOT NULL THEN - SELECT string_agg(u.x, ', ')::TEXT - INTO _result - FROM (SELECT concat(t.value ->> 'path', '/', t.value ->> 'filename') AS x - FROM jsonb_array_elements(_medias) AS t) AS u; + SELECT + string_agg(u.x , ', ')::TEXT INTO _result + FROM ( + SELECT + concat(t.value ->> 'path' , '/' , t.value ->> 'filename') AS x + FROM + jsonb_array_elements(_medias) AS t) AS u; ELSE - SELECT NULL - INTO _result; + SELECT + NULL INTO _result; END IF; END; $$; -COMMENT ON FUNCTION src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array( - _medias JSONB, OUT _result TEXT -) IS 'Function to list medias URL from VisioNature datas'; +COMMENT ON FUNCTION + src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array (_medias + JSONB , OUT _result TEXT) IS 'Function to list medias URL from VisioNature datas'; /* Function to get observation generated UUID */ /* NOTE: removed because uuid are now available in faune-france API */ - -DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_observation_uuid ( - _site CHARACTER VARYING, _id INTEGER -) CASCADE; - - -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_observation_uuid( - _site CHARACTER VARYING, _id INTEGER -) RETURNS UUID -LANGUAGE plpgsql -AS -$$ +DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_observation_uuid (_site + CHARACTER VARYING , _id INTEGER) CASCADE; + +CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_observation_uuid (_site + CHARACTER VARYING , _id INTEGER) + RETURNS UUID + LANGUAGE plpgsql + AS $$ DECLARE the_uuid UUID DEFAULT NULL; BEGIN - IF (SELECT exists(SELECT * - FROM information_schema.tables - WHERE table_schema = 'src_vn_json' - AND table_name = 'uuid_xref')) THEN - SELECT uuid INTO the_uuid FROM src_vn_json.uuid_xref WHERE site LIKE _site AND id = _id LIMIT 1; + IF ( + SELECT + EXISTS ( + SELECT + * + FROM + information_schema.tables + WHERE + table_schema = 'src_vn_json' AND table_name = 'uuid_xref')) THEN + SELECT + uuid INTO the_uuid + FROM + src_vn_json.uuid_xref + WHERE + site LIKE _site + AND id = _id + LIMIT 1; END IF; RETURN the_uuid; END; -$$ LANGUAGE plpgsql; - -COMMENT ON FUNCTION src_lpodatas.fct_c_get_observation_uuid() IS 'Function to get observation generated UUID'; +$$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION src_lpodatas.fct_c_get_observation_uuid () IS 'Function to get observation generated UUID'; -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_taxon_diffusion_level( - _cd_nom INT -) -RETURNS INT AS -$$ +CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_taxon_diffusion_level (_cd_nom INT) + RETURNS INT + AS $$ DECLARE the_nomenclature_id INT; BEGIN - SELECT id_nomenclature_diffusion_level - INTO the_nomenclature_id - FROM src_lpodatas.t_c_rules_diffusion_level - WHERE cd_nom = _cd_nom; + SELECT + id_nomenclature_diffusion_level INTO the_nomenclature_id + FROM + src_lpodatas.t_c_rules_diffusion_level + WHERE + cd_nom = _cd_nom; RETURN the_nomenclature_id; END; -$$ LANGUAGE plpgsql; +$$ +LANGUAGE plpgsql; -DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_committees_validation_status ( - _committees_validation JSONB -); +DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_committees_validation_status + (_committees_validation JSONB); -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_committees_validation_status( - _committees_validation JSONB -) -RETURNS TEXT [] AS -$$ +CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_committees_validation_status + (_committees_validation JSONB) + RETURNS TEXT[] + AS $$ DECLARE the_values TEXT[]; - the_rec RECORD; + the_rec RECORD; BEGIN - FOR the_rec IN (SELECT jsonb_object_keys(_committees_validation) AS key) + FOR the_rec IN ( + SELECT + jsonb_object_keys(_committees_validation) AS key) LOOP - SELECT array_append(the_values, _committees_validation ->> the_rec.key) - INTO the_values; + SELECT + array_append(the_values , _committees_validation ->> the_rec.key) INTO the_values; END LOOP; RETURN the_values; END; -$$ LANGUAGE plpgsql; +$$ +LANGUAGE plpgsql; -DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_committees_validation_is_accepted ( - _committees_validation JSONB -); +DROP FUNCTION IF EXISTS + src_lpodatas.fct_c_get_committees_validation_is_accepted + (_committees_validation JSONB); -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_committees_validation_is_accepted( - _committees_validation JSONB -) -RETURNS BOOLEAN AS -$$ +CREATE OR REPLACE FUNCTION + src_lpodatas.fct_c_get_committees_validation_is_accepted + (_committees_validation JSONB) + RETURNS BOOLEAN + AS $$ DECLARE is_accepted BOOLEAN; BEGIN - SELECT 'ACCEPTED' = ANY (src_lpodatas.fct_c_get_committees_validation_status(_committees_validation)) - INTO is_accepted; + SELECT + 'ACCEPTED' = ANY + (src_lpodatas.fct_c_get_committees_validation_status + (_committees_validation)) INTO is_accepted; RETURN is_accepted; END; -$$ LANGUAGE plpgsql; - -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_check_hidden_rules( - _cd_nom INT, _date_min TIMESTAMP, _raw_bird_breed_code INT -) -RETURNS BOOL AS $$ +LANGUAGE plpgsql; +CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_check_hidden_rules (_cd_nom INT , + _date_min TIMESTAMP , _raw_bird_breed_code INT) + RETURNS BOOL + AS $$ DECLARE the_bird_breed_code INT; the_has_hidden_rule BOOL; BEGIN - SELECT CASE - WHEN _raw_bird_breed_code = 99 THEN NULL - WHEN _raw_bird_breed_code = 30 THEN 2 - WHEN _raw_bird_breed_code = 40 THEN 4 - WHEN _raw_bird_breed_code = 50 THEN 11 - ELSE _raw_bird_breed_code - END - INTO the_bird_breed_code; - SELECT exists (SELECT * - FROM src_lpodatas.t_c_visionature_hidding_rules rule - WHERE rule.cd_nom = _cd_nom - AND (rule.all_time_restriction - OR ( - _date_min BETWEEN - make_date(extract(YEAR FROM now())::INT, - rule.restriction_start_month::INT, - rule.restriction_start_day::INT - ) AND - make_date( - extract(YEAR FROM now())::INT, - rule.restriction_end_month::INT, - rule.restriction_end_day::INT - ) - ) - AND (rule.restriction_atlas_min_code IS NULL) - OR (rule.restriction_atlas_min_code IS NOT NULL AND - (the_bird_breed_code IS NOT NULL AND - the_bird_breed_code >= rule.restriction_atlas_min_code)) - )) - INTO the_has_hidden_rule; + SELECT + CASE WHEN _raw_bird_breed_code = 99 THEN + NULL + WHEN _raw_bird_breed_code = 30 THEN + 2 + WHEN _raw_bird_breed_code = 40 THEN + 4 + WHEN _raw_bird_breed_code = 50 THEN + 11 + ELSE + _raw_bird_breed_code + END INTO the_bird_breed_code; + SELECT + EXISTS ( + SELECT + * + FROM + src_lpodatas.t_c_visionature_hidding_rules rule + WHERE + rule.cd_nom = _cd_nom + AND (rule.all_time_restriction + OR (_date_min BETWEEN make_date(extract(YEAR FROM + now())::INT , rule.restriction_start_month::INT , + rule.restriction_start_day::INT) + AND make_date(extract(YEAR FROM now())::INT , + rule.restriction_end_month::INT , + rule.restriction_end_day::INT)) + AND (rule.restriction_atlas_min_code IS NULL) + OR (rule.restriction_atlas_min_code IS NOT NULL + AND (the_bird_breed_code IS NOT NULL + AND the_bird_breed_code >= rule.restriction_atlas_min_code)))) INTO the_has_hidden_rule; RETURN the_has_hidden_rule; END; -$$ LANGUAGE plpgsql; +$$ +LANGUAGE plpgsql; -COMMENT ON FUNCTION src_lpodatas.fct_c_check_hidden_rules() IS 'Detect observations hidden by sensitivity rules'; +COMMENT ON FUNCTION src_lpodatas.fct_c_check_hidden_rules () IS 'Detect observations hidden by sensitivity rules'; -CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_diffusion_level( - _cd_nom INT, _date_min TIMESTAMP, - _raw_bird_breed_code INT, _item JSONB -) -RETURNS INT AS -$$ +CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_diffusion_level (_cd_nom INT + , _date_min TIMESTAMP , _raw_bird_breed_code INT , _item JSONB) + RETURNS INT + AS $$ DECLARE the_id_nomenclature_diffusion_level INT DEFAULT NULL; - the_hidden_by_rules BOOL DEFAULT FALSE; + the_hidden_by_rules BOOL DEFAULT FALSE; BEGIN /* Si la table des données de sensibilté visionature existe */ - IF (SELECT exists(SELECT - FROM information_schema.tables - WHERE table_schema = 'src_lpodatas' - AND table_name = 't_c_visionature_hidding_rules')) THEN + IF ( + SELECT + EXISTS ( + SELECT + FROM + information_schema.tables + WHERE + table_schema = 'src_lpodatas' AND table_name = 't_c_visionature_hidding_rules')) THEN /* Identifie si La donnée est cachée par une règle de sensibilité faune-france */ - SELECT src_lpodatas.fct_c_check_hidden_rules(_cd_nom, _date_min, _raw_bird_breed_code) INTO the_hidden_by_rules; + SELECT + src_lpodatas.fct_c_check_hidden_rules (_cd_nom , _date_min , + _raw_bird_breed_code) INTO the_hidden_by_rules; + + /* Si la donnée est automatiquement cachée par une règle de sensibilité, alors on la diffuse précisément */ --- IF the_hidden_by_rules THEN --- SELECT ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', '5') --- INTO the_id_nomenclature_diffusion_level; + -- IF the_hidden_by_rules THEN + -- SELECT + -- ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', + -- '5') + -- INTO the_id_nomenclature_diffusion_level; END IF; + + /* sinon, on fait le check courant */ - SELECT CASE - -- Taxons sensibles, règle dans la table src_lpodatas.t_c_rules_diffusion_level > diffusion au cas par cas - WHEN _cd_nom IN (SELECT cd_nom FROM src_lpodatas.t_c_rules_diffusion_level) - THEN src_lpodatas.fct_c_get_taxon_diffusion_level(_cd_nom) - -- Observation "cachée" automatiquement par les règles de sensibilités de visionature > diffusion précise - WHEN the_hidden_by_rules THEN ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', '5') - -- Observation a priori cachée par l'observateur - WHEN cast(_item #>> '{observers,0,hidden}' IS NOT NULL AS BOOL) - THEN ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', '2') - -- Observation "invalide" (> refused) ou en ffquestionnement (> question) - WHEN _item #>> '{observers,0,admin_hidden_type}' IN ('refused', 'question') THEN - ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', '4') - ELSE - ref_nomenclatures.get_id_nomenclature('NIV_PRECIS', '5') - END - INTO the_id_nomenclature_diffusion_level; + SELECT + CASE + -- Taxons sensibles, règle dans la table src_lpodatas.t_c_rules_diffusion_level > + -- diffusion au cas par cas + WHEN _cd_nom IN ( + SELECT + cd_nom + FROM + src_lpodatas.t_c_rules_diffusion_level) THEN + src_lpodatas.fct_c_get_taxon_diffusion_level (_cd_nom) + -- Observation "cachée" automatiquement par les règles de sensibilités de + -- visionature > diffusion précise + WHEN the_hidden_by_rules THEN + ref_nomenclatures.get_id_nomenclature ('NIV_PRECIS' , '5') + -- Observation a priori cachée par l'observateur + WHEN cast(_item #>> '{observers,0,hidden}' IS NOT NULL AS BOOL) THEN + ref_nomenclatures.get_id_nomenclature ('NIV_PRECIS' , '2') + -- Observation "invalide" (> refused) ou en ffquestionnement (> question) + WHEN _item #>> '{observers,0,admin_hidden_type}' IN ('refused' , 'question') THEN + ref_nomenclatures.get_id_nomenclature ('NIV_PRECIS' , '4') + ELSE + ref_nomenclatures.get_id_nomenclature ('NIV_PRECIS' , '5') + END INTO the_id_nomenclature_diffusion_level; RETURN the_id_nomenclature_diffusion_level; END; -$$ LANGUAGE plpgsql; +$$ +LANGUAGE plpgsql; -COMMENT ON FUNCTION src_lpodatas.fct_c_get_diffusion_level() IS 'Défini les règles de diffusion des données cachées'; +COMMENT ON FUNCTION src_lpodatas.fct_c_get_diffusion_level () IS 'Défini les règles de diffusion des données cachées'; COMMIT; diff --git a/08_upsert_observations.sql b/08_upsert_observations.sql index 34ad042..34e55b8 100644 --- a/08_upsert_observations.sql +++ b/08_upsert_observations.sql @@ -4,945 +4,874 @@ UPSERT_OBSERVATIONS Main trigger function to upsert or delete observations into GeoNature from VisioNature Data are then stored into gn_synthese.synthese and src_lpodatas.t_c_synthese_extended with id_synthese as common field. -*/ - + */ BEGIN; -DROP TRIGGER IF EXISTS tri_c_upsert_vn_observation_to_geonature ON src_vn_json.observations_json; +DROP TRIGGER IF EXISTS tri_c_upsert_vn_observation_to_geonature ON + src_vn_json.observations_json; -DROP FUNCTION IF EXISTS src_lpodatas.fct_tri_c_upsert_vn_observation_to_geonature () CASCADE; +DROP FUNCTION IF EXISTS + src_lpodatas.fct_tri_c_upsert_vn_observation_to_geonature () CASCADE; -CREATE OR REPLACE FUNCTION src_lpodatas.fct_tri_c_upsert_vn_observation_to_geonature() -RETURNS TRIGGER -LANGUAGE plpgsql -AS -$$ +CREATE OR REPLACE FUNCTION src_lpodatas.fct_tri_c_upsert_vn_observation_to_geonature () + RETURNS TRIGGER + LANGUAGE plpgsql + AS $$ DECLARE /* Log duration informations variables */ - start_ts TIMESTAMP; + start_ts TIMESTAMP; /* common_data */ - the_id_synthese INTEGER; + the_id_synthese INTEGER; /* gn_synthese.synthese data */ - the_unique_id_sinp UUID; - the_unique_id_sinp_grp UUID; - the_id_source INTEGER; - the_entity_source_pk_value VARCHAR; - the_id_dataset INTEGER; - the_id_nomenclature_geo_object_nature INTEGER; - the_id_nomenclature_grp_typ INTEGER; - the_id_nomenclature_obs_technique INTEGER; - the_id_nomenclature_bio_status INTEGER; - the_id_nomenclature_bio_condition INTEGER; - the_id_nomenclature_naturalness INTEGER; - the_id_nomenclature_exist_proof INTEGER; - the_id_nomenclature_valid_status INTEGER; - the_id_nomenclature_diffusion_level INTEGER; - the_id_nomenclature_life_stage INTEGER; - the_id_nomenclature_sex INTEGER; - the_id_nomenclature_obj_count INTEGER; - the_id_nomenclature_type_count INTEGER; - the_id_nomenclature_sensitivity INTEGER; - the_id_nomenclature_observation_status INTEGER; - the_id_nomenclature_blurring INTEGER; - the_id_nomenclature_source_status INTEGER; - the_id_nomenclature_info_geo_type INTEGER; - the_count_min INTEGER; - the_count_max INTEGER; - the_cd_nom INTEGER; + the_unique_id_sinp UUID; + the_unique_id_sinp_grp UUID; + the_id_source INTEGER; + the_entity_source_pk_value VARCHAR; + the_id_dataset INTEGER; + the_id_nomenclature_geo_object_nature INTEGER; + the_id_nomenclature_grp_typ INTEGER; + the_id_nomenclature_obs_technique INTEGER; + the_id_nomenclature_bio_status INTEGER; + the_id_nomenclature_bio_condition INTEGER; + the_id_nomenclature_naturalness INTEGER; + the_id_nomenclature_exist_proof INTEGER; + the_id_nomenclature_valid_status INTEGER; + the_id_nomenclature_diffusion_level INTEGER; + the_id_nomenclature_life_stage INTEGER; + the_id_nomenclature_sex INTEGER; + the_id_nomenclature_obj_count INTEGER; + the_id_nomenclature_type_count INTEGER; + the_id_nomenclature_sensitivity INTEGER; + the_id_nomenclature_observation_status INTEGER; + the_id_nomenclature_blurring INTEGER; + the_id_nomenclature_source_status INTEGER; + the_id_nomenclature_info_geo_type INTEGER; + the_count_min INTEGER; + the_count_max INTEGER; + the_cd_nom INTEGER; -- TODO Pour les non match, cd_nom de Animalia - the_nom_cite VARCHAR; - the_meta_v_taxref VARCHAR; - the_sample_number_proof VARCHAR; - the_digital_proof VARCHAR; - the_non_digital_proof VARCHAR; - the_altitude_min INTEGER; - the_altitude_max INTEGER; - _the_geom_4326 public.GEOMETRY(Geometry, 4326); - _the_geom_point public.GEOMETRY(Point, 4326); - _the_geom_local public.GEOMETRY(Geometry, :local_srid); - the_date_min TIMESTAMP; - the_date_max TIMESTAMP; - the_validation_comment TEXT; - the_observers TEXT; - the_observers_extended TEXT; - the_determiner TEXT; - the_id_digitiser INTEGER; + the_nom_cite VARCHAR; + the_meta_v_taxref VARCHAR; + the_sample_number_proof VARCHAR; + the_digital_proof VARCHAR; + the_non_digital_proof VARCHAR; + the_altitude_min INTEGER; + the_altitude_max INTEGER; + _the_geom_4326 public.geometry(Geometry , 4326); + _the_geom_point public.geometry(POINT , 4326); + _the_geom_local public.geometry(Geometry , :local_srid); + the_date_min TIMESTAMP; + the_date_max TIMESTAMP; + the_validation_comment TEXT; + the_observers TEXT; + the_observers_extended TEXT; + the_determiner TEXT; + the_id_digitiser INTEGER; the_id_nomenclature_determination_method INTEGER; - the_comments TEXT; - the_reference_biblio VARCHAR(255); - + the_comments TEXT; + the_reference_biblio VARCHAR(255); /* t_c_synthese_extended data */ --- the_observation_detail JSONB; - the_id_sp_source INTEGER; - the_taxo_group VARCHAR(50); - the_taxo_real BOOLEAN; + -- the_observation_detail JSONB; + the_id_sp_source INTEGER; + the_taxo_group VARCHAR(50); + the_taxo_real BOOLEAN; -- TODO plus tard; taxon vrai uniquement si rang IN ('ES';'SSES') - the_common_name VARCHAR(250); - the_pseudo_observer_uid VARCHAR(200); + the_common_name VARCHAR(250); + the_pseudo_observer_uid VARCHAR(200); -- TODO Renseigner avec la fonction actuelle - the_bird_breed_code INTEGER; - the_breed_status VARCHAR(20); - the_bat_breed_colo BOOLEAN; - the_bat_is_gite BOOLEAN; - the_bat_period VARCHAR(20); - the_estimation_code VARCHAR(50); - the_date_year INTEGER; - the_mortality BOOLEAN; - the_mortality_cause VARCHAR(250); - the_export_excluded BOOLEAN; - the_project_code VARCHAR(250); - the_juridical_person VARCHAR(100); - the_behaviour TEXT[]; - the_geo_accuracy VARCHAR(50); - the_details JSONB; - the_id_place INT; - the_place VARCHAR(250); - the_id_form VARCHAR(20); - the_is_valid BOOLEAN; - the_private_comment TEXT; - the_is_hidden BOOLEAN DEFAULT FALSE; + the_bird_breed_code INTEGER; + the_breed_status VARCHAR(20); + the_bat_breed_colo BOOLEAN; + the_bat_is_gite BOOLEAN; + the_bat_period VARCHAR(20); + the_estimation_code VARCHAR(50); + the_date_year INTEGER; + the_mortality BOOLEAN; + the_mortality_cause VARCHAR(250); + the_export_excluded BOOLEAN; + the_project_code VARCHAR(250); + the_juridical_person VARCHAR(100); + the_behaviour TEXT[]; + the_geo_accuracy VARCHAR(50); + the_details JSONB; + the_id_place INT; + the_place VARCHAR(250); + the_id_form VARCHAR(20); + the_is_valid BOOLEAN; + the_private_comment TEXT; + the_is_hidden BOOLEAN DEFAULT FALSE; BEGIN - SELECT clock_timestamp() INTO start_ts; - RAISE DEBUG '-- % -- START SCRIPT', start_ts; - RAISE DEBUG '-- % -- Populate variables > Beginning', start_ts; - SELECT cast(new.item #>> '{species,@id}' AS INTEGER) - INTO the_id_sp_source; + SELECT + clock_timestamp() INTO start_ts; + RAISE DEBUG '-- % -- START SCRIPT' , start_ts; + RAISE DEBUG '-- % -- Populate variables > Beginning' , start_ts; + SELECT + cast(NEW.item #>> '{species,@id}' AS INTEGER) INTO the_id_sp_source; + /* Partie gn_synthese.synthese */ - SELECT coalesce(cast(new.item #>> '{observers,0,uuid}' AS UUID), - src_lpodatas.fct_c_get_observation_uuid(new.site, new.id)) - INTO the_unique_id_sinp; - RAISE DEBUG 'UUID is %', the_unique_id_sinp; - SELECT uuid - INTO the_unique_id_sinp_grp - FROM src_vn_json.forms_json - WHERE (new.site, (new.item #>> '{observers,0,id_form}')::INT) = (forms_json.site, forms_json.id); - SELECT src_lpodatas.fct_c_upsert_or_get_source_from_visionature(new.site) - INTO the_id_source; - SELECT new.id::TEXT - INTO the_entity_source_pk_value; - SELECT public.st_setsrid(public.st_makepoint(cast(((new.item -> 'observers') -> 0) ->> 'coord_lon' AS FLOAT), - cast(((new.item -> 'observers') -> 0) ->> 'coord_lat' AS FLOAT)), - 4326) - INTO _the_geom_4326; - SELECT to_timestamp(cast(new.item #>> '{observers,0,timing,@timestamp}' AS DOUBLE PRECISION)) - INTO the_date_min; - SELECT the_date_min - INTO the_date_max; - SELECT cast(coalesce(src_lpodatas.fct_c_get_taxref_values_from_vn('cd_nom'::TEXT, - cast(new.item #>> '{species,@id}' AS INTEGER)), - gn_commons.get_default_parameter('visionature_default_cd_nom')) AS INTEGER) - INTO the_cd_nom; - SELECT cast(new.item #>> '{observers,0,atlas_code}' AS INTEGER) - INTO the_bird_breed_code; - IF /* MANAGE SYMPETRUM DATA */ - new.item #> '{observers,0}' ? 'project_code' - THEN - SELECT src_lpodatas.fct_c_get_or_insert_dataset_from_shortname(new.item #>> '{observers,0,project_code}', - 'visionature_default_dataset', - 'visionature_default_acquisition_framework') - INTO the_id_dataset; - ELSEIF (st_intersects(_the_geom_4326, (SELECT geom FROM ref_geo.mv_c_sympetrum_cover)) - AND new.item #>> '{species,taxonomy}' = '8') - THEN - SELECT src_lpodatas.fct_c_get_or_insert_dataset_from_shortname_with_af_id( - 'LPO_SYMPETRUM', - NULL, - src_lpodatas.fct_c_get_or_insert_basic_acquisition_framework( - 'LPO_SYMPETRUM', - 'CA LPO-SYMPETRUM', - now()::DATE) - ) - INTO the_id_dataset; + SELECT + coalesce(cast(NEW.item #>> '{observers,0,uuid}' AS UUID) , + src_lpodatas.fct_c_get_observation_uuid (NEW.site , NEW.id)) INTO + the_unique_id_sinp; + RAISE DEBUG 'UUID is %' , the_unique_id_sinp; + SELECT + uuid INTO the_unique_id_sinp_grp + FROM + src_vn_json.forms_json + WHERE (NEW.site , (NEW.item #>> '{observers,0,id_form}')::INT) = (forms_json.site + , forms_json.id); + SELECT + src_lpodatas.fct_c_upsert_or_get_source_from_visionature (NEW.site) INTO the_id_source; + SELECT + NEW.id::TEXT INTO the_entity_source_pk_value; + SELECT + src_lpodatas.fct_c_get_id_role_from_visionature_uid (NEW.item #>> + '{observers,0,@uid}' , TRUE) INTO the_id_digitiser; + SELECT + public.st_setsrid (public.st_makepoint (cast(((NEW.item -> + 'observers') -> 0) ->> 'coord_lon' AS FLOAT) , + cast(((NEW.item -> 'observers') -> 0) ->> 'coord_lat' AS + FLOAT)) , 4326) INTO _the_geom_4326; + SELECT + to_timestamp(cast(NEW.item #>> '{observers,0,timing,@timestamp}' AS DOUBLE PRECISION)) INTO the_date_min; + SELECT + the_date_min INTO the_date_max; + SELECT + cast(coalesce(src_lpodatas.fct_c_get_taxref_values_from_vn + ('cd_nom'::TEXT , cast(NEW.item #>> '{species,@id}' AS + INTEGER)) , gn_commons.get_default_parameter ('visionature_default_cd_nom')) AS + INTEGER) INTO the_cd_nom; + SELECT + cast(NEW.item #>> '{observers,0,atlas_code}' AS INTEGER) INTO the_bird_breed_code; + IF + /* if project_code > dataset from project code */ + NEW.item #> '{observers,0}' ? 'project_code' THEN + SELECT + src_lpodatas.fct_c_get_or_insert_dataset_from_shortname (NEW.item + #>> '{observers,0,project_code}' , 'visionature_default_dataset' , 'visionature_default_acquisition_framework') INTO + the_id_dataset; ELSE - SELECT coalesce(src_lpodatas.fct_c_get_dataset_from_observer_uid(new.item #>> '{observers,0,@uid}'), - src_lpodatas.fct_c_get_or_insert_dataset_from_shortname(NULL, - 'visionature_default_dataset', - 'visionature_default_acquisition_framework')) - INTO the_id_dataset; - END IF; - SELECT ref_nomenclatures.fct_c_get_synonyms_nomenclature('NAT_OBJ_GEO', new.item #>> '{observers,0,precision}') - INTO the_id_nomenclature_geo_object_nature; - SELECT gn_synthese.get_default_nomenclature_value('TYP_GRP'::CHARACTER VARYING) - INTO the_id_nomenclature_grp_typ; - -- SELECT gn_synthese.get_default_nomenclature_value('METH_OBS') INTO the_id_nomenclature_obs_meth; - -- ref_nomenclatures.fct_c_get_synonyms_nomenclature('METH_OBS', - -- new.item #>> '{observers,0,details,0,condition}') - SELECT ref_nomenclatures.get_id_nomenclature('METH_OBS', '21') - INTO the_id_nomenclature_obs_technique; - -- coalesce( - -- ref_nomenclatures.fct_c_get_synonyms_nomenclature('TECHNIQUE_OBS', - -- new.item #>> '{observers,0,details,0,condition}'), - -- ref_nomenclatures.fct_c_get_synonyms_nomenclature('TECHNIQUE_OBS', - -- new.item #>> '{observers,0,details,0,age}')) - -- SELECT gn_synthese.get_default_nomenclature_value('STATUT_BIO') INTO the_id_nomenclature_bio_status; - SELECT coalesce( - coalesce( - coalesce( - ref_nomenclatures.fct_c_get_synonyms_nomenclature('STATUT_BIO', - new.item #>> - '{observers,0,atlas_code}'), - CASE - WHEN src_lpodatas.fct_c_get_reproduction_status( - (new.item #>> '{species,taxonomy}')::INT, new.item) IN - ('Possible', 'Probable', 'Certain') - THEN ref_nomenclatures.get_id_nomenclature('STATUT_BIO', '3') END - ), ref_nomenclatures.fct_c_get_synonyms_nomenclature('STATUT_BIO', new.item #>> - '{observers,0,details,0,condition}')), - ref_nomenclatures.get_id_nomenclature('STATUT_BIO', '1')) - INTO the_id_nomenclature_bio_status; - SELECT CASE - WHEN new.item #> '{observers,0,extended_info}' ? 'mortality' THEN - ref_nomenclatures.get_id_nomenclature('ETA_BIO', '3') - ELSE ref_nomenclatures.get_id_nomenclature('ETA_BIO', '2') - END - INTO the_id_nomenclature_bio_condition; - SELECT ref_nomenclatures.get_id_nomenclature('NATURALITE', '0') - INTO the_id_nomenclature_naturalness; - SELECT CASE - WHEN new.item #> '{observers,0}' ? 'medias' THEN - ref_nomenclatures.get_id_nomenclature('PREUVE_EXIST', '1') - ELSE - ref_nomenclatures.get_id_nomenclature('PREUVE_EXIST', '2') - END - INTO the_id_nomenclature_exist_proof; - SELECT - -- When chr or chn accepted then certain, when admin_hidden then is douteux else probable. - CASE - WHEN src_lpodatas.fct_c_get_committees_validation_is_accepted(new.item #> '{observers,0,committees_validation}') - THEN - ref_nomenclatures.get_id_nomenclature('STATUT_VALID', '1') - WHEN cast(new.item #>> '{observers,0,admin_hidden}' AS BOOLEAN) OR -- TRUE si soumis à validation - NOT src_lpodatas.fct_c_get_committees_validation_is_accepted(new.item #> '{observers,0,committees_validation}') -- TRUE si - THEN - ref_nomenclatures.get_id_nomenclature('STATUT_VALID', '3') + SELECT + coalesce( /* if observer in organism > organism default dataset */ + src_lpodatas.fct_c_get_dataset_from_observer_uid (NEW.item #>> + '{observers,0,@uid}') , /* else if sympetrum data > dataset LPO_SYMPETRUM*/ CASE WHEN (st_intersects + (_the_geom_4326 , ( + SELECT + geom + FROM ref_geo.mv_c_sympetrum_cover)) + AND NEW.item #>> '{species,taxonomy}' = '8') THEN + src_lpodatas.fct_c_get_or_insert_dataset_from_shortname_wit + h_af_id ('LPO_SYMPETRUM' , NULL , + src_lpodatas.fct_c_get_or_insert_basic_acquisition_fram + ework ('LPO_SYMPETRUM' , 'CA LPO-SYMPETRUM' , + now()::DATE)) ELSE - ref_nomenclatures.get_id_nomenclature('STATUT_VALID', '2') - END - INTO the_id_nomenclature_valid_status; - SELECT src_lpodatas.fct_c_get_diffusion_level(the_cd_nom, the_date_min, - the_bird_breed_code, new.item) - INTO the_id_nomenclature_diffusion_level; - SELECT - -- coalesce(ref_nomenclatures.fct_c_get_synonyms_nomenclature('STADE_VIE', - -- new.item #>> '{observers,0,details,0,age}'), - -- gn_synthese.get_default_nomenclature_value('STADE_VIE')) - ref_nomenclatures.get_id_nomenclature('STADE_VIE', '0') - INTO the_id_nomenclature_life_stage; - SELECT ref_nomenclatures.get_id_nomenclature('SEXE', '0') - INTO the_id_nomenclature_sex; - SELECT ref_nomenclatures.get_id_nomenclature('OBJ_DENBR', 'IND') - INTO the_id_nomenclature_obj_count; - SELECT --- ref_nomenclatures.fct_c_get_synonyms_nomenclature('STADE_VIE', new.item #>> '{observers,0,estimation_code}') -ref_nomenclatures.get_id_nomenclature('TYP_DENBR', 'ind') - INTO the_id_nomenclature_type_count; - SELECT CASE - WHEN cast(new.item #>> '{observers,0,hidden}' AS BOOLEAN) THEN - ref_nomenclatures.get_id_nomenclature('SENSIBILITE', '2') - ELSE - ref_nomenclatures.get_id_nomenclature('SENSIBILITE', '0') - END - INTO the_id_nomenclature_sensitivity; - SELECT CASE - WHEN ((new.item #>> '{observers,0,count}' = '0' - AND new.item #>> '{observers,0,estimation_code}' LIKE 'EXACT_VALUE') OR - (new.item #>> '{observers,0,atlas_code}' = '99')) THEN - ref_nomenclatures.get_id_nomenclature('STATUT_OBS', 'No') - ELSE - ref_nomenclatures.get_id_nomenclature('STATUT_OBS', 'Pr') - END - INTO the_id_nomenclature_observation_status; - SELECT ref_nomenclatures.get_id_nomenclature('DEE_FLOU', 'NON') - INTO the_id_nomenclature_blurring; - SELECT ref_nomenclatures.get_id_nomenclature('STATUT_SOURCE', 'Te') - INTO the_id_nomenclature_source_status; - SELECT ref_nomenclatures.get_id_nomenclature('TYP_INF_GEO', '1') - --SELECT coalesce(ref_nomenclatures.fct_c_get_synonyms_nomenclature( - -- 'TYP_INF_GEO', - -- new.item #>> '{observers,0,precision}'), - --ref_nomenclatures.get_id_nomenclature('TYP_INF_GEO', '2'))s - INTO the_id_nomenclature_info_geo_type; - SELECT cast(new.item #>> '{observers,0,count}' AS INTEGER) - INTO the_count_min; - SELECT cast(new.item #>> '{observers,0,count}' AS INTEGER) - INTO the_count_max; + /* else default dataset */ + src_lpodatas.fct_c_get_or_insert_dataset_from_shortname + (NULL , 'visionature_default_dataset' , 'visionature_default_acquisition_framework') + END) INTO the_id_dataset; + END IF; + SELECT + ref_nomenclatures.fct_c_get_synonyms_nomenclature ('NAT_OBJ_GEO' , + NEW.item #>> '{observers,0,precision}') INTO + the_id_nomenclature_geo_object_nature; + SELECT + gn_synthese.get_default_nomenclature_value ('TYP_GRP'::CHARACTER + VARYING) INTO the_id_nomenclature_grp_typ; + -- SELECT gn_synthese.get_default_nomenclature_value('METH_OBS') INTO + -- the_id_nomenclature_obs_meth; + -- + -- ref_nomenclatures.fct_c_get_synonyms_nomenclature('METH_OBS', + -- new.item + -- #>> + -- '{observers,0,details,0,condition}') + SELECT + ref_nomenclatures.get_id_nomenclature ('METH_OBS' , + '21') INTO the_id_nomenclature_obs_technique; + -- coalesce( + -- + -- ref_nomenclatures.fct_c_get_synonyms_nomenclature('TECHNIQUE_OBS', + -- + -- new.item + -- #>> + -- '{observers,0,details,0,condition}'), + -- + -- ref_nomenclatures.fct_c_get_synonyms_nomenclature('TECHNIQUE_OBS', + -- + -- new.item + -- #>> + -- '{observers,0,details,0,age}')) + -- SELECT + -- gn_synthese.get_default_nomenclature_value('STATUT_BIO') + -- INTO + -- the_id_nomenclature_bio_status; + SELECT + coalesce(coalesce(coalesce(ref_nomenclatures.fct_c_get_synonyms_nomencl + ature ('STATUT_BIO' , NEW.item #>> '{observers,0,atlas_code}') , CASE WHEN + src_lpodatas.fct_c_get_reproduction_status ((NEW.item #>> + '{species,taxonomy}')::INT , NEW.item) IN ('Possible' , + 'Probable' , 'Certain') THEN + ref_nomenclatures.get_id_nomenclature ('STATUT_BIO' , '3') + END) , ref_nomenclatures.fct_c_get_synonyms_nomenclature + ('STATUT_BIO' , NEW.item #>> '{observers,0,details,0,condition}')) , + ref_nomenclatures.get_id_nomenclature ('STATUT_BIO' + , '1')) INTO + the_id_nomenclature_bio_status; + SELECT + CASE WHEN NEW.item #> '{observers,0,extended_info}' ? 'mortality' THEN + ref_nomenclatures.get_id_nomenclature ('ETA_BIO' , '3') + ELSE + ref_nomenclatures.get_id_nomenclature ('ETA_BIO' , '2') + END INTO the_id_nomenclature_bio_condition; + SELECT + ref_nomenclatures.get_id_nomenclature ('NATURALITE' , + '0') INTO the_id_nomenclature_naturalness; + SELECT + CASE WHEN NEW.item #> '{observers,0}' ? 'medias' THEN + ref_nomenclatures.get_id_nomenclature ('PREUVE_EXIST' , '1') + ELSE + ref_nomenclatures.get_id_nomenclature ('PREUVE_EXIST' , '2') + END INTO the_id_nomenclature_exist_proof; + SELECT + -- When chr or chn accepted then certain, when admin_hidden then is douteux else + -- probable. + CASE WHEN src_lpodatas.fct_c_get_committees_validation_is_accepted + (NEW.item #> '{observers,0,committees_validation}') THEN + ref_nomenclatures.get_id_nomenclature ('STATUT_VALID' , '1') + WHEN cast(NEW.item #>> '{observers,0,admin_hidden}' AS BOOLEAN) + OR -- TRUE si soumis à validation + NOT src_lpodatas.fct_c_get_committees_validation_is_accepted + (NEW.item #> '{observers,0,committees_validation}') -- TRUE si + THEN + ref_nomenclatures.get_id_nomenclature ('STATUT_VALID' , '3') + ELSE + ref_nomenclatures.get_id_nomenclature ('STATUT_VALID' , '2') + END INTO the_id_nomenclature_valid_status; + SELECT + src_lpodatas.fct_c_get_diffusion_level (the_cd_nom , the_date_min , + the_bird_breed_code , NEW.item) INTO + the_id_nomenclature_diffusion_level; + SELECT + -- + -- coalesce(ref_nomenclatures.fct_c_get_synonyms_nomenclature('STADE_VIE', + -- new.item + -- #>> '{observers,0,details,0,age}'), + -- gn_synthese.get_default_nomenclature_value('STADE_VIE')) + ref_nomenclatures.get_id_nomenclature ('STADE_VIE' , + '0') INTO the_id_nomenclature_life_stage; + SELECT + ref_nomenclatures.get_id_nomenclature ('SEXE' , + '0') INTO the_id_nomenclature_sex; + SELECT + ref_nomenclatures.get_id_nomenclature ('OBJ_DENBR' , + 'IND') INTO the_id_nomenclature_obj_count; + SELECT + -- ref_nomenclatures.fct_c_get_synonyms_nomenclature('STADE_VIE', + -- new.item #>> '{observers,0,estimation_code}') + ref_nomenclatures.get_id_nomenclature ('TYP_DENBR' , + 'ind') INTO the_id_nomenclature_type_count; + SELECT + CASE WHEN cast(NEW.item #>> '{observers,0,hidden}' AS BOOLEAN) THEN + ref_nomenclatures.get_id_nomenclature ('SENSIBILITE' , '2') + ELSE + ref_nomenclatures.get_id_nomenclature ('SENSIBILITE' , '0') + END INTO the_id_nomenclature_sensitivity; + SELECT + CASE WHEN ((NEW.item #>> '{observers,0,count}' = '0' + AND NEW.item #>> '{observers,0,estimation_code}' LIKE 'EXACT_VALUE') + OR (NEW.item #>> '{observers,0,atlas_code}' = '99')) THEN + ref_nomenclatures.get_id_nomenclature ('STATUT_OBS' , 'No') + ELSE + ref_nomenclatures.get_id_nomenclature ('STATUT_OBS' , 'Pr') + END INTO the_id_nomenclature_observation_status; + SELECT + ref_nomenclatures.get_id_nomenclature ('DEE_FLOU' , + 'NON') INTO the_id_nomenclature_blurring; + SELECT + ref_nomenclatures.get_id_nomenclature ('STATUT_SOURCE' , + 'Te') INTO the_id_nomenclature_source_status; + SELECT + ref_nomenclatures.get_id_nomenclature ('TYP_INF_GEO' , '1') + --SELECT coalesce(ref_nomenclatures.fct_c_get_synonyms_nomenclature( + -- 'TYP_INF_GEO', + -- new.item #>> '{observers,0,precision}'), + -- ref_nomenclatures.get_id_nomenclature('TYP_INF_GEO', + -- '2'))s + INTO the_id_nomenclature_info_geo_type; + SELECT + cast(NEW.item #>> '{observers,0,count}' AS INTEGER) INTO the_count_min; + SELECT + cast(NEW.item #>> '{observers,0,count}' AS INTEGER) INTO the_count_max; - SELECT src_lpodatas.fct_c_get_species_values_from_vn('latin_name'::TEXT, the_id_sp_source) - INTO the_nom_cite; - SELECT gn_commons.get_default_parameter('taxref_version', NULL) - INTO the_meta_v_taxref; - SELECT NULL - INTO the_sample_number_proof; - SELECT src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array(new.item #> '{observers,0,medias}') - INTO the_digital_proof; - SELECT NULL - INTO the_non_digital_proof; - SELECT cast(new.item #>> '{observers,0,altitude}' AS INTEGER) - INTO the_altitude_min; - SELECT cast(new.item #>> '{observers,0,altitude}' AS INTEGER) - INTO the_altitude_max; + SELECT + src_lpodatas.fct_c_get_species_values_from_vn ('latin_name'::TEXT , + the_id_sp_source) INTO the_nom_cite; + SELECT + gn_commons.get_default_parameter ('taxref_version' , NULL) INTO the_meta_v_taxref; + SELECT + NULL INTO the_sample_number_proof; + SELECT + src_lpodatas.fct_c_get_medias_url_from_visionature_medias_array + (NEW.item #> '{observers,0,medias}') INTO the_digital_proof; + SELECT + NULL INTO the_non_digital_proof; + SELECT + cast(NEW.item #>> '{observers,0,altitude}' AS INTEGER) INTO the_altitude_min; + SELECT + cast(NEW.item #>> '{observers,0,altitude}' AS INTEGER) INTO the_altitude_max; - SELECT _the_geom_4326 - INTO _the_geom_point; - SELECT public.st_transform(_the_geom_4326, (gn_commons.get_default_parameter('gn_local_srid'))::INT) - INTO _the_geom_local; + SELECT + _the_geom_4326 INTO _the_geom_point; + SELECT + public.st_transform (_the_geom_4326 , (gn_commons.get_default_parameter + ('gn_local_srid'))::INT) INTO _the_geom_local; - SELECT NULL - INTO the_validation_comment; - SELECT CASE - WHEN (new.item #> '{observers,0}') ? 'second_hand' AND (new.item #>> '{observers,0,second_hand}') = '1' - THEN NULL - ELSE src_lpodatas.fct_c_get_role_name_from_visionature_uid(new.item #>> '{observers,0,@uid}', TRUE) END - INTO the_observers; - SELECT CASE - WHEN (new.item #> '{observers,0}') ? 'second_hand' AND (new.item #>> '{observers,0,second_hand}') = '1' - THEN NULL - ELSE src_lpodatas.fct_c_get_role_name_from_visionature_uid(new.item #>> '{observers,0,@uid}', FALSE) END - INTO the_observers_extended; - SELECT the_observers INTO the_determiner; - SELECT src_lpodatas.fct_c_get_id_role_from_visionature_uid(new.item #>> '{observers,0,@uid}', TRUE) - INTO the_id_digitiser; - SELECT gn_synthese.get_default_nomenclature_value('METH_DETERMIN') - INTO the_id_nomenclature_determination_method; - -- COALESCE( - -- ref_nomenclatures.fct_c_get_synonyms_nomenclature('METH_DETERMIN', - -- new.item #>> '{observers,0,details,0,condition}'), - -- , gn_synthese.get_default_nomenclature_value('METH_DETERMIN')) - SELECT new.item #>> '{observers,0,comment}' - INTO the_comments; - SELECT src_lpodatas.fct_c_get_source_url(the_id_source, the_entity_source_pk_value) - INTO the_reference_biblio; + SELECT + NULL INTO the_validation_comment; + SELECT + CASE WHEN (NEW.item #> '{observers,0}') ? 'second_hand' + AND (NEW.item #>> '{observers,0,second_hand}') = '1' THEN + NULL + ELSE + src_lpodatas.fct_c_get_role_name_from_visionature_uid (NEW.item #>> + '{observers,0,@uid}' , TRUE) + END INTO the_observers; + SELECT + CASE WHEN (NEW.item #> '{observers,0}') ? 'second_hand' + AND (NEW.item #>> '{observers,0,second_hand}') = '1' THEN + NULL + ELSE + src_lpodatas.fct_c_get_role_name_from_visionature_uid (NEW.item #>> + '{observers,0,@uid}' , FALSE) + END INTO the_observers_extended; + SELECT + the_observers INTO the_determiner; + SELECT + gn_synthese.get_default_nomenclature_value ('METH_DETERMIN') INTO + the_id_nomenclature_determination_method; + -- COALESCE( + -- + -- ref_nomenclatures.fct_c_get_synonyms_nomenclature('METH_DETERMIN', + -- + -- new.item + -- #>> + -- '{observers,0,details,0,condition}'), + -- , + -- gn_synthese.get_default_nomenclature_value('METH_DETERMIN')) + SELECT + NEW.item #>> '{observers,0,comment}' INTO the_comments; + SELECT + src_lpodatas.fct_c_get_source_url (the_id_source , + the_entity_source_pk_value) INTO the_reference_biblio; -- SELECT --- to_timestamp(cast(new.item #>> '{observers,0,insert_date}' AS DOUBLE PRECISION)) --- INTO the_meta_create_date; --- SELECT --- to_timestamp(cast(new.item #>> '{observers,0,update_date}' AS DOUBLE PRECISION)) --- INTO the_meta_update_date; - + -- to_timestamp(cast(new.item #>> '{observers,0,insert_date}' AS DOUBLE + -- PRECISION)) + -- INTO the_meta_create_date; + -- SELECT + -- to_timestamp(cast(new.item #>> '{observers,0,update_date}' AS DOUBLE + -- PRECISION)) + -- INTO the_meta_update_date; /* Partie .t_c_synthese_extended */ - SELECT src_lpodatas.fct_c_get_taxo_group_values_from_vn('name', new.site, - cast(new.item #>> '{species,taxonomy}' AS INT)) - INTO the_taxo_group; - SELECT src_lpodatas.fct_c_get_taxref_values_from_vn('id_rang'::TEXT, - cast(new.item #>> '{species,@id}' AS INTEGER)) IN - ('ES', 'SSES') - INTO the_taxo_real; - SELECT CASE - WHEN src_lpodatas.fct_c_get_taxref_values_from_vn('nom_vern'::TEXT, the_id_sp_source) IS NOT NULL THEN - split_part(src_lpodatas.fct_c_get_taxref_values_from_vn('nom_vern'::TEXT, the_id_sp_source), ',', 1) - ELSE - src_lpodatas.fct_c_get_species_values_from_vn('french_name'::TEXT, the_id_sp_source) - END - INTO the_common_name; - SELECT encode(hmac(cast((new.item #>> '{observers,0,@uid}') AS TEXT), 'cyifoE!A5r', 'sha1'), 'hex') - INTO the_pseudo_observer_uid; + SELECT + src_lpodatas.fct_c_get_taxo_group_values_from_vn ('name' , + NEW.site , cast(NEW.item #>> '{species,taxonomy}' AS INT)) INTO + the_taxo_group; + SELECT + src_lpodatas.fct_c_get_taxref_values_from_vn ('id_rang'::TEXT , + cast(NEW.item #>> '{species,@id}' AS INTEGER)) IN + ('ES' , 'SSES') INTO the_taxo_real; + SELECT + CASE WHEN src_lpodatas.fct_c_get_taxref_values_from_vn + ('nom_vern'::TEXT , the_id_sp_source) IS NOT NULL THEN + split_part(src_lpodatas.fct_c_get_taxref_values_from_vn + ('nom_vern'::TEXT , the_id_sp_source) , ',' + , 1) + ELSE + src_lpodatas.fct_c_get_species_values_from_vn + ('french_name'::TEXT , the_id_sp_source) + END INTO the_common_name; + SELECT + encode(hmac(cast((NEW.item #>> '{observers,0,@uid}') AS TEXT) , + 'cyifoE!A5r' , 'sha1') , 'hex') INTO + the_pseudo_observer_uid; - SELECT CASE - WHEN (new.item #> '{observers,0}') ? 'atlas_code' THEN - ref_nomenclatures.get_nomenclature_label_by_cdnom_mnemonique('VN_ATLAS_CODE', - new.item #>> - '{observers,0,atlas_code}') - ELSE src_lpodatas.fct_c_get_reproduction_status( - (new.item #>> '{species,taxonomy}')::INT, new.item) - END - INTO the_breed_status; - SELECT NULL - INTO the_bat_breed_colo; - SELECT NULL - INTO the_bat_is_gite; - SELECT NULL - INTO the_bat_period; - SELECT new.item #>> '{observers,0,estimation_code}' - INTO the_estimation_code; - SELECT cast(extract(YEAR FROM to_timestamp(cast(new.item #>> '{date,@timestamp}' AS DOUBLE PRECISION))) AS INTEGER) - INTO the_date_year; - SELECT cast(((new.item #>> '{observers,0,extended_info,mortality}'::TEXT[]) IS NOT NULL) AS BOOLEAN) - INTO the_mortality; - SELECT new.item #>> '{observers,0,extended_info, mortality, death_cause2}' - INTO the_mortality_cause; - SELECT FALSE - INTO the_export_excluded; - SELECT new.item #>> '{observers,0,project_code}' - INTO the_project_code; - SELECT src_lpodatas.fct_c_get_entity_from_observer_site_uid(cast((new.item #>> '{observers,0,@uid}') AS INTEGER), - new.site) - INTO the_juridical_person; - SELECT src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array(new.item #> '{observers,0,behaviours}') - INTO the_behaviour; - SELECT new.item #>> '{observers,0,precision}' - INTO the_geo_accuracy; - SELECT new.item #> '{observers,0,details}' - INTO the_details; - SELECT cast(new.item #>> '{place,@id}' AS INTEGER) - INTO the_id_place; - SELECT new.item #>> '{place,name}' - INTO the_place; - SELECT new.id_form_universal - INTO the_id_form; - SELECT coalesce(NOT cast(new.item #>> '{observers,0,admin_hidden}' AS BOOLEAN), TRUE) - INTO the_is_valid; - SELECT new.item #>> '{observers,0,hidden_comment}' - INTO the_private_comment; - SELECT cast(new.item #>> '{observers,0,hidden}' IS NOT NULL AS BOOL) - INTO the_is_hidden; + SELECT + CASE WHEN (NEW.item #> '{observers,0}') ? 'atlas_code' THEN + ref_nomenclatures.get_nomenclature_label_by_cdnom_mnemonique + ('VN_ATLAS_CODE' , NEW.item #>> '{observers,0,atlas_code}') + ELSE + src_lpodatas.fct_c_get_reproduction_status ((NEW.item #>> + '{species,taxonomy}')::INT , NEW.item) + END INTO the_breed_status; + SELECT + NULL INTO the_bat_breed_colo; + SELECT + NULL INTO the_bat_is_gite; + SELECT + NULL INTO the_bat_period; + SELECT + NEW.item #>> '{observers,0,estimation_code}' INTO the_estimation_code; + SELECT + cast(extract(YEAR FROM to_timestamp(cast(NEW.item #>> '{date,@timestamp}' + AS DOUBLE PRECISION))) AS INTEGER) INTO the_date_year; + SELECT + cast(((NEW.item #>> '{observers,0,extended_info,mortality}'::TEXT[]) IS NOT NULL) AS BOOLEAN) + INTO the_mortality; + SELECT + NEW.item #>> '{observers,0,extended_info, mortality, death_cause2}' INTO the_mortality_cause; + SELECT + FALSE INTO the_export_excluded; + SELECT + NEW.item #>> '{observers,0,project_code}' INTO the_project_code; + SELECT + src_lpodatas.fct_c_get_entity_from_observer_site_uid (cast((NEW.item + #>> '{observers,0,@uid}') AS INTEGER) , NEW.site) INTO + the_juridical_person; + SELECT + src_lpodatas.fct_c_get_behaviours_texts_array_from_id_array (NEW.item + #> '{observers,0,behaviours}') INTO the_behaviour; + SELECT + NEW.item #>> '{observers,0,precision}' INTO the_geo_accuracy; + SELECT + NEW.item #> '{observers,0,details}' INTO the_details; + SELECT + cast(NEW.item #>> '{place,@id}' AS INTEGER) INTO the_id_place; + SELECT + NEW.item #>> '{place,name}' INTO the_place; + SELECT + NEW.id_form_universal INTO the_id_form; + SELECT + coalesce(NOT cast(NEW.item #>> '{observers,0,admin_hidden}' AS BOOLEAN) , TRUE) INTO the_is_valid; + SELECT + NEW.item #>> '{observers,0,hidden_comment}' INTO the_private_comment; + SELECT + cast(NEW.item #>> '{observers,0,hidden}' IS NOT NULL AS BOOL) INTO the_is_hidden; - RAISE DEBUG '-- % -- Populate variables > ENDING : duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); + RAISE DEBUG '-- % -- Populate variables > ENDING : duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); IF (tg_op = 'UPDATE') - -- DO UPDATE IF trigger action is an UPDATE + -- DO UPDATE IF trigger action is an UPDATE THEN - RAISE DEBUG 'Try update data % from site % with uuid %', new.id, new.site, the_unique_id_sinp; - RAISE DEBUG '-- % -- Update statement synthese when found > BEGINNING : total duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); + RAISE DEBUG 'Try update data % from site % with uuid %' , NEW.id , NEW.site , the_unique_id_sinp; + RAISE DEBUG '-- % -- Update statement synthese when found > BEGINNING : total duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); -- Updating data on gn_synthese.synthese when raw data is updated UPDATE gn_synthese.synthese - SET unique_id_sinp = coalesce(unique_id_sinp, the_unique_id_sinp) - , unique_id_sinp_grp = the_unique_id_sinp_grp - , id_source = the_id_source - , entity_source_pk_value = the_entity_source_pk_value - , id_dataset = the_id_dataset - , id_nomenclature_geo_object_nature = the_id_nomenclature_geo_object_nature - , id_nomenclature_grp_typ = the_id_nomenclature_grp_typ - - -- , id_nomenclature_obs_meth = the_id_nomenclature_obs_meth - , id_nomenclature_obs_technique = the_id_nomenclature_obs_technique - , id_nomenclature_bio_status = the_id_nomenclature_bio_status - , id_nomenclature_bio_condition = the_id_nomenclature_bio_condition - , id_nomenclature_naturalness = the_id_nomenclature_naturalness - , id_nomenclature_exist_proof = the_id_nomenclature_exist_proof - , id_nomenclature_valid_status = the_id_nomenclature_valid_status - , id_nomenclature_diffusion_level = the_id_nomenclature_diffusion_level - , id_nomenclature_life_stage = the_id_nomenclature_life_stage - , id_nomenclature_sex = the_id_nomenclature_sex - , id_nomenclature_obj_count = the_id_nomenclature_obj_count - , id_nomenclature_type_count = the_id_nomenclature_type_count - , id_nomenclature_sensitivity = the_id_nomenclature_sensitivity - , id_nomenclature_observation_status = the_id_nomenclature_observation_status - , id_nomenclature_blurring = the_id_nomenclature_blurring - , id_nomenclature_source_status = the_id_nomenclature_source_status - , id_nomenclature_info_geo_type = the_id_nomenclature_info_geo_type - , count_min = the_count_min - , count_max = the_count_max - , cd_nom = the_cd_nom - , nom_cite = the_nom_cite - , meta_v_taxref = the_meta_v_taxref - , sample_number_proof = the_sample_number_proof - , digital_proof = the_digital_proof - , non_digital_proof = the_non_digital_proof - , altitude_min = the_altitude_min - , altitude_max = the_altitude_max - , the_geom_4326 = _the_geom_4326 - , the_geom_point = _the_geom_point - , the_geom_local = _the_geom_local - , date_min = the_date_min - , date_max = the_date_max - , validation_comment = the_validation_comment - , observers = the_observers - , id_digitiser = the_id_digitiser - , id_nomenclature_determination_method = the_id_nomenclature_determination_method - , comment_description = the_comments - , reference_biblio = the_reference_biblio --- , meta_create_date = the_meta_create_date --- , meta_update_date = now() - , last_action = 'U' + SET + unique_id_sinp = coalesce(unique_id_sinp , the_unique_id_sinp) + , unique_id_sinp_grp = the_unique_id_sinp_grp + , id_source = the_id_source + , entity_source_pk_value = the_entity_source_pk_value + , id_dataset = the_id_dataset + , id_nomenclature_geo_object_nature = the_id_nomenclature_geo_object_nature + , id_nomenclature_grp_typ = the_id_nomenclature_grp_typ + -- , id_nomenclature_obs_meth = the_id_nomenclature_obs_meth + , id_nomenclature_obs_technique = the_id_nomenclature_obs_technique + , id_nomenclature_bio_status = the_id_nomenclature_bio_status + , id_nomenclature_bio_condition = the_id_nomenclature_bio_condition + , id_nomenclature_naturalness = the_id_nomenclature_naturalness + , id_nomenclature_exist_proof = the_id_nomenclature_exist_proof + , id_nomenclature_valid_status = the_id_nomenclature_valid_status + , id_nomenclature_diffusion_level = the_id_nomenclature_diffusion_level + , id_nomenclature_life_stage = the_id_nomenclature_life_stage + , id_nomenclature_sex = the_id_nomenclature_sex + , id_nomenclature_obj_count = the_id_nomenclature_obj_count + , id_nomenclature_type_count = the_id_nomenclature_type_count + , id_nomenclature_sensitivity = the_id_nomenclature_sensitivity + , id_nomenclature_observation_status = the_id_nomenclature_observation_status + , id_nomenclature_blurring = the_id_nomenclature_blurring + , id_nomenclature_source_status = the_id_nomenclature_source_status + , id_nomenclature_info_geo_type = the_id_nomenclature_info_geo_type + , count_min = the_count_min + , count_max = the_count_max + , cd_nom = the_cd_nom + , nom_cite = the_nom_cite + , meta_v_taxref = the_meta_v_taxref + , sample_number_proof = the_sample_number_proof + , digital_proof = the_digital_proof + , non_digital_proof = the_non_digital_proof + , altitude_min = the_altitude_min + , altitude_max = the_altitude_max + , the_geom_4326 = _the_geom_4326 + , the_geom_point = _the_geom_point + , the_geom_local = _the_geom_local + , date_min = the_date_min + , date_max = the_date_max + , validation_comment = the_validation_comment + , observers = the_observers + , id_digitiser = the_id_digitiser + , id_nomenclature_determination_method = the_id_nomenclature_determination_method + , comment_description = the_comments + , reference_biblio = the_reference_biblio + -- , meta_create_date = the_meta_create_date + -- , meta_update_date = now() + , last_action = 'U' WHERE (unique_id_sinp = the_unique_id_sinp) - OR (entity_source_pk_value, id_source) = (the_entity_source_pk_value, the_id_source) - RETURNING id_synthese INTO the_id_synthese; - RAISE DEBUG '-- % -- Update statement synthese when found > ENDING : total duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); + OR (entity_source_pk_value + , id_source) = (the_entity_source_pk_value + , the_id_source) + RETURNING + id_synthese INTO the_id_synthese; + RAISE DEBUG '-- % -- Update statement synthese when found > ENDING : total duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); IF NOT found THEN - RAISE DEBUG 'Data % from site % not found, proceed INSERT to synthese', new.id, new.site; - RAISE DEBUG '-- % -- Update statement synthese when NOT found > BEGINNING : total duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); - INSERT INTO gn_synthese.synthese ( unique_id_sinp - , unique_id_sinp_grp - , id_source - , entity_source_pk_value - , id_dataset - , id_nomenclature_geo_object_nature - , id_nomenclature_grp_typ - , + RAISE DEBUG 'Data % from site % not found, proceed INSERT to synthese' , NEW.id , NEW.site; + RAISE DEBUG '-- % -- Update statement synthese when NOT found > BEGINNING : total duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); + INSERT INTO gn_synthese.synthese (unique_id_sinp , + unique_id_sinp_grp , id_source , entity_source_pk_value , + id_dataset , id_nomenclature_geo_object_nature , + id_nomenclature_grp_typ , -- , id_nomenclature_obs_meth - id_nomenclature_obs_technique - , id_nomenclature_bio_status - , id_nomenclature_bio_condition - , id_nomenclature_naturalness - , id_nomenclature_exist_proof - , id_nomenclature_valid_status - , id_nomenclature_diffusion_level - , id_nomenclature_life_stage - , id_nomenclature_sex - , id_nomenclature_obj_count - , id_nomenclature_type_count - , id_nomenclature_sensitivity - , id_nomenclature_observation_status - , id_nomenclature_blurring - , id_nomenclature_source_status - , id_nomenclature_info_geo_type - , count_min - , count_max - , cd_nom - , nom_cite - , meta_v_taxref - , sample_number_proof - , digital_proof - , non_digital_proof - , altitude_min - , altitude_max - , the_geom_4326 - , the_geom_point - , the_geom_local - , date_min - , date_max - , validation_comment - , observers - , id_digitiser - , id_nomenclature_determination_method - , comment_description - , reference_biblio --- , meta_create_date --- , meta_update_date - , last_action) - VALUES ( the_unique_id_sinp - , the_unique_id_sinp_grp - , the_id_source - , the_entity_source_pk_value - , the_id_dataset - , the_id_nomenclature_geo_object_nature - , the_id_nomenclature_grp_typ - , - -- , the_id_nomenclature_obs_meth - the_id_nomenclature_obs_technique - , the_id_nomenclature_bio_status - , the_id_nomenclature_bio_condition - , the_id_nomenclature_naturalness - , the_id_nomenclature_exist_proof - , the_id_nomenclature_valid_status - , the_id_nomenclature_diffusion_level - , the_id_nomenclature_life_stage - , the_id_nomenclature_sex - , the_id_nomenclature_obj_count - , the_id_nomenclature_type_count - , the_id_nomenclature_sensitivity - , the_id_nomenclature_observation_status - , the_id_nomenclature_blurring - , the_id_nomenclature_source_status - , the_id_nomenclature_info_geo_type - , the_count_min - , the_count_max - , the_cd_nom - , the_nom_cite - , the_meta_v_taxref - , the_sample_number_proof - , the_digital_proof - , the_non_digital_proof - , the_altitude_min - , the_altitude_max - , _the_geom_4326 - , _the_geom_point - , _the_geom_local - , the_date_min - , the_date_max - , the_validation_comment - , the_observers - , the_id_digitiser - , the_id_nomenclature_determination_method - , the_comments - , the_reference_biblio --- , now() --- , the_meta_update_date - , 'U') - RETURNING id_synthese INTO the_id_synthese; - RAISE DEBUG '-- % -- Update statement synthese when NOT found > ENDING : total duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); + id_nomenclature_obs_technique , id_nomenclature_bio_status , + id_nomenclature_bio_condition , id_nomenclature_naturalness + , id_nomenclature_exist_proof , + id_nomenclature_valid_status , + id_nomenclature_diffusion_level , + id_nomenclature_life_stage , id_nomenclature_sex , + id_nomenclature_obj_count , id_nomenclature_type_count , + id_nomenclature_sensitivity , + id_nomenclature_observation_status , + id_nomenclature_blurring , id_nomenclature_source_status , + id_nomenclature_info_geo_type , count_min , count_max , + cd_nom , nom_cite , meta_v_taxref , sample_number_proof , + digital_proof , non_digital_proof , altitude_min , + altitude_max , the_geom_4326 , the_geom_point , + the_geom_local , date_min , date_max , validation_comment , + observers , id_digitiser , + id_nomenclature_determination_method , comment_description + , reference_biblio + -- , meta_create_date + -- , meta_update_date + , last_action) + VALUES (the_unique_id_sinp , the_unique_id_sinp_grp , + the_id_source , the_entity_source_pk_value , the_id_dataset + , the_id_nomenclature_geo_object_nature , + the_id_nomenclature_grp_typ , + -- , the_id_nomenclature_obs_meth + the_id_nomenclature_obs_technique , + the_id_nomenclature_bio_status , + the_id_nomenclature_bio_condition , + the_id_nomenclature_naturalness , + the_id_nomenclature_exist_proof , + the_id_nomenclature_valid_status , + the_id_nomenclature_diffusion_level , + the_id_nomenclature_life_stage , + the_id_nomenclature_sex , the_id_nomenclature_obj_count + , the_id_nomenclature_type_count , + the_id_nomenclature_sensitivity , + the_id_nomenclature_observation_status , + the_id_nomenclature_blurring , + the_id_nomenclature_source_status , + the_id_nomenclature_info_geo_type , the_count_min , + the_count_max , the_cd_nom , the_nom_cite , + the_meta_v_taxref , the_sample_number_proof , + the_digital_proof , the_non_digital_proof , + the_altitude_min , the_altitude_max , _the_geom_4326 , + _the_geom_point , _the_geom_local , the_date_min , + the_date_max , the_validation_comment , the_observers , + the_id_digitiser , + the_id_nomenclature_determination_method , the_comments + , the_reference_biblio + -- , now() + -- , the_meta_update_date + , 'U') + RETURNING + id_synthese INTO the_id_synthese; + RAISE DEBUG '-- % -- Update statement synthese when NOT found > ENDING : total duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); END IF; -- Updating extended datas when raw data is updated - RAISE DEBUG '-- % -- Update statement t_c_synthese_extended when found > BEGINNING : total duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); + RAISE DEBUG '-- % -- Update statement t_c_synthese_extended when found > BEGINNING : total duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); UPDATE src_lpodatas.t_c_synthese_extended - SET id_synthese = the_id_synthese - , id_sp_source = the_id_sp_source - , taxo_group = the_taxo_group - , taxo_real = the_taxo_real - , common_name = the_common_name - , pseudo_observer_uid = the_pseudo_observer_uid - , bird_breed_code = the_bird_breed_code - , breed_status = the_breed_status - , bat_breed_colo = the_bat_breed_colo - , bat_is_gite = the_bat_is_gite - , bat_period = the_bat_period - , estimation_code = the_estimation_code - , date_year = the_date_year - , mortality = the_mortality - , mortality_cause = the_mortality_cause - , export_excluded = the_export_excluded - , project_code = the_project_code - , juridical_person = the_juridical_person - , behaviour = the_behaviour - , geo_accuracy = the_geo_accuracy - , details = the_details - , id_place = the_id_place - , place = the_place - , id_form = the_id_form - , is_valid = the_is_valid - , private_comment = the_private_comment - , is_hidden = the_is_hidden - , observers = the_observers_extended - WHERE id_synthese = the_id_synthese; + SET + id_synthese = the_id_synthese + , id_sp_source = the_id_sp_source + , taxo_group = the_taxo_group + , taxo_real = the_taxo_real + , common_name = the_common_name + , pseudo_observer_uid = the_pseudo_observer_uid + , bird_breed_code = the_bird_breed_code + , breed_status = the_breed_status + , bat_breed_colo = the_bat_breed_colo + , bat_is_gite = the_bat_is_gite + , bat_period = the_bat_period + , estimation_code = the_estimation_code + , date_year = the_date_year + , mortality = the_mortality + , mortality_cause = the_mortality_cause + , export_excluded = the_export_excluded + , project_code = the_project_code + , juridical_person = the_juridical_person + , behaviour = the_behaviour + , geo_accuracy = the_geo_accuracy + , details = the_details + , id_place = the_id_place + , place = the_place + , id_form = the_id_form + , is_valid = the_is_valid + , private_comment = the_private_comment + , is_hidden = the_is_hidden + , observers = the_observers_extended + WHERE + id_synthese = the_id_synthese; IF NOT found THEN - RAISE DEBUG 'Data % from site % not found, proceed INSERT to synthese_extended', new.id, new.site; - RAISE DEBUG '-- % -- Update statement t_c_synthese_extended when NOT found > BEGINNING : total duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); - INSERT INTO src_lpodatas.t_c_synthese_extended ( id_synthese - , id_sp_source - , taxo_group - , taxo_real - , common_name - , pseudo_observer_uid - , bird_breed_code - , breed_status - , bat_breed_colo - , bat_is_gite - , bat_period - , estimation_code - , date_year - , mortality - , mortality_cause - , export_excluded - , project_code - , juridical_person - , behaviour - , geo_accuracy - , details - , id_place - , place - , id_form - , is_valid - , private_comment - , is_hidden - , observers) - VALUES ( the_id_synthese - , the_id_sp_source - , the_taxo_group - , the_taxo_real - , the_common_name - , the_pseudo_observer_uid - , the_bird_breed_code - , the_breed_status - , the_bat_breed_colo - , the_bat_is_gite - , the_bat_period - , the_estimation_code - , the_date_year - , the_mortality - , the_mortality_cause - , the_export_excluded - , the_project_code - , the_juridical_person - , the_behaviour - , the_geo_accuracy - , the_details - , the_id_place - , the_place - , the_id_form - , the_is_valid - , the_private_comment - , the_is_hidden - , the_observers_extended); + RAISE DEBUG 'Data % from site % not found, proceed INSERT to synthese_extended' , NEW.id , NEW.site; + RAISE DEBUG '-- % -- Update statement t_c_synthese_extended when NOT found > BEGINNING : total duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); + INSERT INTO src_lpodatas.t_c_synthese_extended (id_synthese , + id_sp_source , taxo_group , taxo_real , common_name , + pseudo_observer_uid , bird_breed_code , breed_status , + bat_breed_colo , bat_is_gite , bat_period , estimation_code , + date_year , mortality , mortality_cause , export_excluded , + project_code , juridical_person , behaviour , geo_accuracy , + details , id_place , place , id_form , is_valid , + private_comment , is_hidden , observers) + VALUES (the_id_synthese , the_id_sp_source , the_taxo_group , + the_taxo_real , the_common_name , the_pseudo_observer_uid , + the_bird_breed_code , the_breed_status , the_bat_breed_colo + , the_bat_is_gite , the_bat_period , the_estimation_code , + the_date_year , the_mortality , the_mortality_cause , + the_export_excluded , the_project_code , + the_juridical_person , the_behaviour , the_geo_accuracy , + the_details , the_id_place , the_place , the_id_form , + the_is_valid , the_private_comment , the_is_hidden , + the_observers_extended); END IF; - ELSEIF (tg_op = 'INSERT') THEN - RAISE DEBUG 'Try insert data % from site %', new.id, new.site; - RAISE DEBUG '-- % -- insert statement synthese when found > BEGINNING : total duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); - INSERT INTO gn_synthese.synthese ( unique_id_sinp - , unique_id_sinp_grp - , id_source - , entity_source_pk_value - , id_dataset - , id_nomenclature_geo_object_nature - , id_nomenclature_grp_typ - , + ELSEIF (tg_op = 'INSERT') THEN + RAISE DEBUG 'Try insert data % from site %' , NEW.id , NEW.site; + RAISE DEBUG '-- % -- insert statement synthese when found > BEGINNING : total duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); + INSERT INTO gn_synthese.synthese (unique_id_sinp , unique_id_sinp_grp , + id_source , entity_source_pk_value , id_dataset , + id_nomenclature_geo_object_nature , id_nomenclature_grp_typ , -- , id_nomenclature_obs_meth - id_nomenclature_obs_technique - , id_nomenclature_bio_status - , id_nomenclature_bio_condition - , id_nomenclature_naturalness - , id_nomenclature_exist_proof - , id_nomenclature_valid_status - , id_nomenclature_diffusion_level - , id_nomenclature_life_stage - , id_nomenclature_sex - , id_nomenclature_obj_count - , id_nomenclature_type_count - , id_nomenclature_sensitivity - , id_nomenclature_observation_status - , id_nomenclature_blurring - , id_nomenclature_source_status - , id_nomenclature_info_geo_type - , count_min - , count_max - , cd_nom - , nom_cite - , meta_v_taxref - , sample_number_proof - , digital_proof - , non_digital_proof - , altitude_min - , altitude_max - , the_geom_4326 - , the_geom_point - , the_geom_local - , date_min - , date_max - , validation_comment - , observers - , id_digitiser - , id_nomenclature_determination_method - , comment_description - , reference_biblio --- , meta_create_date --- , meta_update_date - , last_action) - VALUES ( the_unique_id_sinp - , the_unique_id_sinp_grp - , the_id_source - , the_entity_source_pk_value - , the_id_dataset - , the_id_nomenclature_geo_object_nature - , the_id_nomenclature_grp_typ - , - -- , the_id_nomenclature_obs_meth - the_id_nomenclature_obs_technique - , the_id_nomenclature_bio_status - , the_id_nomenclature_bio_condition - , the_id_nomenclature_naturalness - , the_id_nomenclature_exist_proof - , the_id_nomenclature_valid_status - , the_id_nomenclature_diffusion_level - , the_id_nomenclature_life_stage - , the_id_nomenclature_sex - , the_id_nomenclature_obj_count - , the_id_nomenclature_type_count - , the_id_nomenclature_sensitivity - , the_id_nomenclature_observation_status - , the_id_nomenclature_blurring - , the_id_nomenclature_source_status - , the_id_nomenclature_info_geo_type - , the_count_min - , the_count_max - , the_cd_nom - , the_nom_cite - , the_meta_v_taxref - , the_sample_number_proof - , the_digital_proof - , the_non_digital_proof - , the_altitude_min - , the_altitude_max - , _the_geom_4326 - , _the_geom_point - , _the_geom_local - , the_date_min - , the_date_max - , the_validation_comment - , the_observers - , the_id_digitiser - , the_id_nomenclature_determination_method - , the_comments - , the_reference_biblio --- , the_meta_create_date --- , the_meta_update_date - , 'U') + id_nomenclature_obs_technique , id_nomenclature_bio_status , + id_nomenclature_bio_condition , id_nomenclature_naturalness , + id_nomenclature_exist_proof , id_nomenclature_valid_status , + id_nomenclature_diffusion_level , id_nomenclature_life_stage , + id_nomenclature_sex , id_nomenclature_obj_count , + id_nomenclature_type_count , id_nomenclature_sensitivity , + id_nomenclature_observation_status , id_nomenclature_blurring , + id_nomenclature_source_status , id_nomenclature_info_geo_type , + count_min , count_max , cd_nom , nom_cite , meta_v_taxref , + sample_number_proof , digital_proof , non_digital_proof , + altitude_min , altitude_max , the_geom_4326 , the_geom_point , + the_geom_local , date_min , date_max , validation_comment , + observers , id_digitiser , id_nomenclature_determination_method + , comment_description , reference_biblio + -- , meta_create_date + -- , meta_update_date + , last_action) + VALUES (the_unique_id_sinp , the_unique_id_sinp_grp , the_id_source + , the_entity_source_pk_value , the_id_dataset , + the_id_nomenclature_geo_object_nature , + the_id_nomenclature_grp_typ , + -- , the_id_nomenclature_obs_meth + the_id_nomenclature_obs_technique , + the_id_nomenclature_bio_status , + the_id_nomenclature_bio_condition , + the_id_nomenclature_naturalness , + the_id_nomenclature_exist_proof , + the_id_nomenclature_valid_status , + the_id_nomenclature_diffusion_level , + the_id_nomenclature_life_stage , the_id_nomenclature_sex , + the_id_nomenclature_obj_count , + the_id_nomenclature_type_count , + the_id_nomenclature_sensitivity , + the_id_nomenclature_observation_status , + the_id_nomenclature_blurring , + the_id_nomenclature_source_status , + the_id_nomenclature_info_geo_type , the_count_min , + the_count_max , the_cd_nom , the_nom_cite , + the_meta_v_taxref , the_sample_number_proof , + the_digital_proof , the_non_digital_proof , + the_altitude_min , the_altitude_max , _the_geom_4326 , + _the_geom_point , _the_geom_local , the_date_min , + the_date_max , the_validation_comment , the_observers , + the_id_digitiser , the_id_nomenclature_determination_method + , the_comments , the_reference_biblio + -- , the_meta_create_date + -- , the_meta_update_date + , 'U') ON CONFLICT (unique_id_sinp) - DO UPDATE SET unique_id_sinp_grp = the_unique_id_sinp_grp - , id_source = the_id_source - , entity_source_pk_value = the_entity_source_pk_value - , id_dataset = the_id_dataset - , id_nomenclature_geo_object_nature = the_id_nomenclature_geo_object_nature - , id_nomenclature_grp_typ = the_id_nomenclature_grp_typ - , - -- , id_nomenclature_obs_meth = the_id_nomenclature_obs_meth - id_nomenclature_obs_technique = the_id_nomenclature_obs_technique - , id_nomenclature_bio_status = the_id_nomenclature_bio_status - , id_nomenclature_bio_condition = the_id_nomenclature_bio_condition - , id_nomenclature_naturalness = the_id_nomenclature_naturalness - , id_nomenclature_exist_proof = the_id_nomenclature_exist_proof - , id_nomenclature_valid_status = the_id_nomenclature_valid_status - , id_nomenclature_diffusion_level = the_id_nomenclature_diffusion_level - , id_nomenclature_life_stage = the_id_nomenclature_life_stage - , id_nomenclature_sex = the_id_nomenclature_sex - , id_nomenclature_obj_count = the_id_nomenclature_obj_count - , id_nomenclature_type_count = the_id_nomenclature_type_count - , id_nomenclature_sensitivity = the_id_nomenclature_sensitivity - , id_nomenclature_observation_status = the_id_nomenclature_observation_status - , id_nomenclature_blurring = the_id_nomenclature_blurring - , id_nomenclature_source_status = the_id_nomenclature_source_status - , id_nomenclature_info_geo_type = the_id_nomenclature_info_geo_type - , count_min = the_count_min - , count_max = the_count_max - , cd_nom = the_cd_nom - , nom_cite = the_nom_cite - , meta_v_taxref = the_meta_v_taxref - , sample_number_proof = the_sample_number_proof - , digital_proof = the_digital_proof - , non_digital_proof = the_non_digital_proof - , altitude_min = the_altitude_min - , altitude_max = the_altitude_max - , the_geom_4326 = _the_geom_4326 - , the_geom_point = _the_geom_point - , the_geom_local = _the_geom_local - , date_min = the_date_min - , date_max = the_date_max - , validation_comment = the_validation_comment - , observers = the_observers - , id_digitiser = the_id_digitiser - , id_nomenclature_determination_method = the_id_nomenclature_determination_method - , comment_description = the_comments - , reference_biblio = the_reference_biblio --- , meta_create_date = the_meta_create_date --- , meta_update_date = the_meta_update_date - , last_action = 'U' - RETURNING id_synthese INTO the_id_synthese; - RAISE DEBUG '-- % -- insert statement synthese when found > ENDING : total duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); - RAISE DEBUG '-- % -- insert statement t_c_synthese_extended when found > BEGINNING : total duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); - INSERT INTO src_lpodatas.t_c_synthese_extended ( id_synthese - , id_sp_source - , taxo_group - , taxo_real - , common_name - , pseudo_observer_uid - , bird_breed_code - , breed_status - , bat_breed_colo - , bat_is_gite - , bat_period - , estimation_code - , date_year - , mortality - , mortality_cause - , export_excluded - , project_code - , juridical_person - , behaviour - , geo_accuracy - , details - , id_place - , place - , id_form - , is_valid - , private_comment - , is_hidden - , observers) - VALUES ( the_id_synthese - , the_id_sp_source - , the_taxo_group - , the_taxo_real - , the_common_name - , the_pseudo_observer_uid - , the_bird_breed_code - , the_breed_status - , the_bat_breed_colo - , the_bat_is_gite - , the_bat_period - , the_estimation_code - , the_date_year - , the_mortality - , the_mortality_cause - , the_export_excluded - , the_project_code - , the_juridical_person - , the_behaviour - , the_geo_accuracy - , the_details - , the_id_place - , the_place - , the_id_form - , the_is_valid - , the_private_comment - , the_is_hidden - , the_observers_extended) + DO UPDATE SET + unique_id_sinp_grp = the_unique_id_sinp_grp , id_source = + the_id_source , entity_source_pk_value = + the_entity_source_pk_value , id_dataset = the_id_dataset , + id_nomenclature_geo_object_nature = + the_id_nomenclature_geo_object_nature , + id_nomenclature_grp_typ = the_id_nomenclature_grp_typ , + -- , id_nomenclature_obs_meth = the_id_nomenclature_obs_meth + id_nomenclature_obs_technique = + the_id_nomenclature_obs_technique , + id_nomenclature_bio_status = the_id_nomenclature_bio_status + , id_nomenclature_bio_condition = + the_id_nomenclature_bio_condition , + id_nomenclature_naturalness = + the_id_nomenclature_naturalness , + id_nomenclature_exist_proof = + the_id_nomenclature_exist_proof , + id_nomenclature_valid_status = + the_id_nomenclature_valid_status , + id_nomenclature_diffusion_level = + the_id_nomenclature_diffusion_level , + id_nomenclature_life_stage = the_id_nomenclature_life_stage + , id_nomenclature_sex = the_id_nomenclature_sex , + id_nomenclature_obj_count = the_id_nomenclature_obj_count , + id_nomenclature_type_count = the_id_nomenclature_type_count + , id_nomenclature_sensitivity = + the_id_nomenclature_sensitivity , + id_nomenclature_observation_status = + the_id_nomenclature_observation_status , + id_nomenclature_blurring = the_id_nomenclature_blurring , + id_nomenclature_source_status = + the_id_nomenclature_source_status , + id_nomenclature_info_geo_type = + the_id_nomenclature_info_geo_type , count_min = + the_count_min , count_max = the_count_max , cd_nom = + the_cd_nom , nom_cite = the_nom_cite , meta_v_taxref = + the_meta_v_taxref , sample_number_proof = + the_sample_number_proof , digital_proof = the_digital_proof + , non_digital_proof = the_non_digital_proof , altitude_min + = the_altitude_min , altitude_max = the_altitude_max , + the_geom_4326 = _the_geom_4326 , the_geom_point = + _the_geom_point , the_geom_local = _the_geom_local , + date_min = the_date_min , date_max = the_date_max , + validation_comment = the_validation_comment , observers = + the_observers , id_digitiser = the_id_digitiser , + id_nomenclature_determination_method = + the_id_nomenclature_determination_method , + comment_description = the_comments , reference_biblio = + the_reference_biblio + -- , meta_create_date = + -- the_meta_create_date + -- , meta_update_date = + -- the_meta_update_date + , last_action = 'U' + RETURNING + id_synthese INTO the_id_synthese; + RAISE DEBUG '-- % -- insert statement synthese when found > ENDING : total duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); + RAISE DEBUG '-- % -- insert statement t_c_synthese_extended when found > BEGINNING : total duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); + INSERT INTO src_lpodatas.t_c_synthese_extended (id_synthese , + id_sp_source , taxo_group , taxo_real , common_name , + pseudo_observer_uid , bird_breed_code , breed_status , + bat_breed_colo , bat_is_gite , bat_period , estimation_code , + date_year , mortality , mortality_cause , export_excluded , + project_code , juridical_person , behaviour , geo_accuracy , + details , id_place , place , id_form , is_valid , private_comment , + is_hidden , observers) + VALUES (the_id_synthese , the_id_sp_source , the_taxo_group , + the_taxo_real , the_common_name , the_pseudo_observer_uid , + the_bird_breed_code , the_breed_status , the_bat_breed_colo , + the_bat_is_gite , the_bat_period , the_estimation_code , + the_date_year , the_mortality , the_mortality_cause , + the_export_excluded , the_project_code , the_juridical_person , + the_behaviour , the_geo_accuracy , the_details , the_id_place , + the_place , the_id_form , the_is_valid , the_private_comment , + the_is_hidden , the_observers_extended) ON CONFLICT (id_synthese) - DO UPDATE SET id_synthese = the_id_synthese - , id_sp_source = the_id_sp_source - , taxo_group = the_taxo_group - , taxo_real = the_taxo_real - , common_name = the_common_name - , pseudo_observer_uid = the_pseudo_observer_uid - , bird_breed_code = the_bird_breed_code - , breed_status = the_breed_status - , bat_breed_colo = the_bat_breed_colo - , bat_is_gite = the_bat_is_gite - , bat_period = the_bat_period - , estimation_code = the_estimation_code - , date_year = the_date_year - , mortality = the_mortality - , mortality_cause = the_mortality_cause - , export_excluded = the_export_excluded - , project_code = the_project_code - , juridical_person = the_juridical_person - , behaviour = the_behaviour - , geo_accuracy = the_geo_accuracy - , details = the_details - , id_place = the_id_place - , place = the_place - , id_form = the_id_form - , is_valid = the_is_valid - , private_comment = the_private_comment - , is_hidden = the_is_hidden - , observers = the_observers_extended; - RAISE DEBUG '-- % -- insert statement t_c_synthese_extended when found > ENDING : total duration %', start_ts, (SELECT (clock_timestamp() - start_ts)); + DO UPDATE SET + id_synthese = the_id_synthese , id_sp_source = the_id_sp_source + , taxo_group = the_taxo_group , taxo_real = the_taxo_real , + common_name = the_common_name , pseudo_observer_uid = + the_pseudo_observer_uid , bird_breed_code = + the_bird_breed_code , breed_status = the_breed_status , + bat_breed_colo = the_bat_breed_colo , bat_is_gite = + the_bat_is_gite , bat_period = the_bat_period , + estimation_code = the_estimation_code , date_year = + the_date_year , mortality = the_mortality , mortality_cause + = the_mortality_cause , export_excluded = + the_export_excluded , project_code = the_project_code , + juridical_person = the_juridical_person , behaviour = + the_behaviour , geo_accuracy = the_geo_accuracy , details = + the_details , id_place = the_id_place , place = the_place , + id_form = the_id_form , is_valid = the_is_valid , + private_comment = the_private_comment , is_hidden = + the_is_hidden , observers = the_observers_extended; + RAISE DEBUG '-- % -- insert statement t_c_synthese_extended when found > ENDING : total duration %' , start_ts , ( + SELECT + (clock_timestamp() - start_ts)); - RAISE DEBUG 'NEW upsert data %', the_id_synthese; + RAISE DEBUG 'NEW upsert data %' , the_id_synthese; END IF; + + /* Debug block, to comment if not used */ -- INSERT INTO - -- tmp.debug_log_duration(site, id, uuid, duration, project_code, id_dataset) - -- VALUES - -- ( new.site - -- , new.id - -- , the_unique_id_sinp - -- , (clock_timestamp() - start_ts) - -- , new.item #> '{observers,0}' ? 'project_code' - -- , the_id_dataset); + -- tmp.debug_log_duration(site, id, uuid, duration, + -- project_code, + -- id_dataset) + -- VALUES + -- ( new.site + -- , new.id + -- , the_unique_id_sinp + -- , (clock_timestamp() - start_ts) + -- , new.item #> '{observers,0}' ? 'project_code' + -- , the_id_dataset); RETURN new; -END ; +END; $$; -COMMENT ON FUNCTION src_lpodatas.fct_tri_c_upsert_vn_observation_to_geonature() -IS 'Trigger function to upsert datas from VisioNature to synthese and custom child table'; +COMMENT ON FUNCTION src_lpodatas.fct_tri_c_upsert_vn_observation_to_geonature + () IS 'Trigger function to upsert datas from VisioNature to synthese and custom child table'; -DROP TRIGGER IF EXISTS fct_tri_c_upsert_vn_observation_to_geonature ON src_vn_json.observations_json; +DROP TRIGGER IF EXISTS fct_tri_c_upsert_vn_observation_to_geonature ON + src_vn_json.observations_json; CREATE TRIGGER fct_tri_c_upsert_vn_observation_to_geonature -AFTER INSERT OR UPDATE -ON src_vn_json.observations_json -FOR EACH ROW -EXECUTE PROCEDURE src_lpodatas.fct_tri_c_upsert_vn_observation_to_geonature(); + AFTER INSERT OR UPDATE ON src_vn_json.observations_json + FOR EACH ROW + EXECUTE PROCEDURE src_lpodatas.fct_tri_c_upsert_vn_observation_to_geonature (); -- TRUNCATE gn_synthese.synthese RESTART IDENTITY CASCADE; -DROP FUNCTION IF EXISTS src_lpodatas.fct_tri_c_delete_vn_observation_from_geonature () CASCADE; +DROP FUNCTION IF EXISTS + src_lpodatas.fct_tri_c_delete_vn_observation_from_geonature () CASCADE; -CREATE OR REPLACE FUNCTION src_lpodatas.fct_tri_c_delete_vn_observation_from_geonature() -RETURNS TRIGGER -LANGUAGE plpgsql -AS -$$ +CREATE OR REPLACE FUNCTION + src_lpodatas.fct_tri_c_delete_vn_observation_from_geonature () + RETURNS TRIGGER + LANGUAGE plpgsql + AS $$ DECLARE - the_id_synthese INT; + the_id_synthese INT; the_unique_id_sinp UUID; BEGIN - SELECT coalesce(cast(old.item #>> '{observers,0,uuid}' AS UUID), - src_lpodatas.fct_c_get_observation_uuid(old.site, old.id)) - INTO the_unique_id_sinp; - SELECT id_synthese - INTO the_id_synthese - FROM gn_synthese.synthese - WHERE unique_id_sinp = the_unique_id_sinp; - RAISE DEBUG ' Delete data with uuid %', the_unique_id_sinp; - DELETE - FROM src_lpodatas.t_c_synthese_extended + SELECT + coalesce(cast(OLD.item #>> '{observers,0,uuid}' AS UUID) , + src_lpodatas.fct_c_get_observation_uuid (OLD.site , OLD.id)) INTO + the_unique_id_sinp; + SELECT + id_synthese INTO the_id_synthese + FROM + gn_synthese.synthese + WHERE + unique_id_sinp = the_unique_id_sinp; + RAISE DEBUG ' Delete data with uuid %' , the_unique_id_sinp; + DELETE FROM src_lpodatas.t_c_synthese_extended WHERE t_c_synthese_extended.id_synthese = the_id_synthese; - DELETE - FROM gn_synthese.synthese + DELETE FROM gn_synthese.synthese WHERE synthese.id_synthese = the_id_synthese; IF NOT found THEN RETURN NULL; @@ -952,15 +881,15 @@ END; $$; -COMMENT ON FUNCTION src_lpodatas.fct_tri_c_delete_vn_observation_from_geonature() -IS 'Trigger function to delete datas from gnadm synthese and extended table when DELETE on VisioNature source datas'; +COMMENT ON FUNCTION src_lpodatas.fct_tri_c_delete_vn_observation_from_geonature + () IS 'Trigger function to delete datas from gnadm synthese and extended table when DELETE on VisioNature source datas'; -DROP TRIGGER IF EXISTS tri_c_delete_vn_observation_from_geonature ON src_vn_json.observations_json; +DROP TRIGGER IF EXISTS tri_c_delete_vn_observation_from_geonature ON + src_vn_json.observations_json; CREATE TRIGGER tri_c_delete_vn_observation_from_geonature -AFTER DELETE -ON src_vn_json.observations_json -FOR EACH ROW -EXECUTE PROCEDURE src_lpodatas.fct_tri_c_delete_vn_observation_from_geonature(); + AFTER DELETE ON src_vn_json.observations_json + FOR EACH ROW + EXECUTE PROCEDURE src_lpodatas.fct_tri_c_delete_vn_observation_from_geonature (); COMMIT;