Skip to content

Commit

Permalink
SQL - calcul sensibilite et homogénéisation des scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
gildeluermoz committed Aug 8, 2017
1 parent 4b74976 commit 46904bf
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 45 deletions.
144 changes: 117 additions & 27 deletions data/core/complements_taxonomie.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
CREATE OR REPLACE FUNCTION taxonomie.check_is_inbibnoms(cdnom integer)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

SET search_path = taxonomie, pg_catalog;


----------------------
--MATERIALIZED VIEWS--
----------------------
--Vue materialisée permettant d'améliorer fortement les performances des contraintes check sur les champ filtres 'regne' et 'group2_inpn'
CREATE MATERIALIZED VIEW vm_regne AS (SELECT DISTINCT regne FROM taxref);
CREATE MATERIALIZED VIEW vm_group2_inpn AS (SELECT DISTINCT group2_inpn FROM taxref);


-------------
--FUNCTIONS--
-------------
CREATE OR REPLACE FUNCTION check_is_inbibnoms(cdnom integer)
RETURNS boolean AS
$BODY$
--fonction permettant de vérifier si un texte proposé correspond à un group2_inpn dans la table taxref
BEGIN
IF cdnom IN(SELECT cd_nom FROM taxonomie.bib_noms) THEN
IF cdnom IN(SELECT cd_nom FROM bib_noms) THEN
RETURN true;
ELSE
RETURN false;
Expand All @@ -13,17 +34,13 @@ $BODY$
LANGUAGE plpgsql IMMUTABLE
COST 100;

--Vue materialisée permettant d'améliorer fortement les performances des contraintes check sur les champ filtres 'regne' et 'group2_inpn'

CREATE MATERIALIZED VIEW taxonomie.vm_regne AS (SELECT DISTINCT regne FROM taxonomie.taxref);
CREATE MATERIALIZED VIEW taxonomie.vm_group2_inpn AS (SELECT DISTINCT group2_inpn FROM taxonomie.taxref);

CREATE OR REPLACE FUNCTION taxonomie.check_is_group2inpn(mygroup text)
CREATE OR REPLACE FUNCTION check_is_group2inpn(mygroup text)
RETURNS boolean AS
$BODY$
--fonction permettant de vérifier si un texte proposé correspond à un group2_inpn dans la table taxref
BEGIN
IF mygroup IN(SELECT group2_inpn FROM taxonomie.vm_group2_inpn) OR mygroup IS NULL THEN
IF mygroup IN(SELECT group2_inpn FROM vm_group2_inpn) OR mygroup IS NULL THEN
RETURN true;
ELSE
RETURN false;
Expand All @@ -34,12 +51,12 @@ $BODY$
COST 100;


CREATE OR REPLACE FUNCTION taxonomie.check_is_regne(myregne text)
CREATE OR REPLACE FUNCTION check_is_regne(myregne text)
RETURNS boolean AS
$BODY$
--fonction permettant de vérifier si un texte proposé correspond à un regne dans la table taxref
BEGIN
IF myregne IN(SELECT regne FROM taxonomie.vm_regne) OR myregne IS NULL THEN
IF myregne IN(SELECT regne FROM vm_regne) OR myregne IS NULL THEN
return true;
ELSE
RETURN false;
Expand All @@ -50,44 +67,117 @@ $BODY$
COST 100;


CREATE OR REPLACE FUNCTION taxonomie.find_group2inpn(id integer)
CREATE OR REPLACE FUNCTION find_group2inpn(id integer)
RETURNS text AS
$BODY$
--fonction permettant de renvoyer le group2_inpn d'un taxon à partir de son cd_nom
DECLARE group2 character varying(255);
BEGIN
SELECT INTO group2 group2_inpn FROM taxonomie.taxref WHERE cd_nom = id;
SELECT INTO group2 group2_inpn FROM taxref WHERE cd_nom = id;
return group2;
END;
$BODY$
LANGUAGE plpgsql IMMUTABLE
COST 100;

CREATE OR REPLACE FUNCTION calcul_sensibilite(
cdnom integer,
idnomenclature integer)
RETURNS integer AS
$BODY$
--fonction permettant de renvoyer l'id nomenclature correspondant à la sensibilité de l'observation
--USAGE : SELECT taxonomie.calcul_sensibilite(240,21);
DECLARE
idsensibilite integer;
BEGIN
SELECT max(id_nomenclature_niv_precis) INTO idsensibilite
FROM taxonomie.cor_taxref_sensibilite
WHERE cd_nom = cdnom
AND (id_nomenclature = idnomenclature OR id_nomenclature = 0);
IF idsensibilite IS NULL THEN
idsensibilite = 163;
END IF;
RETURN idsensibilite;
END;
$BODY$
LANGUAGE plpgsql IMMUTABLE
COST 100;



CREATE TABLE taxonomie.cor_taxref_nomenclature

----------
--TABLES--
----------
CREATE TABLE cor_taxref_nomenclature
(
id_nomenclature integer NOT NULL,
regne character varying(255) NOT NULL,
group2_inpn character varying(255) NOT NULL,
date_create timestamp without time zone DEFAULT now(),
date_update timestamp without time zone,
CONSTRAINT cor_taxref_nomenclature_pkey PRIMARY KEY (id_nomenclature, regne, group2_inpn),
CONSTRAINT cor_taxref_nomenclature_id_nomenclature_fkey FOREIGN KEY (id_nomenclature)
REFERENCES meta.t_nomenclatures (id_nomenclature) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE NO ACTION,
CONSTRAINT cor_taxref_nomenclature_isgroup2inpn_check CHECK (taxonomie.check_is_group2inpn(group2_inpn::text) OR group2_inpn::text = 'all'::text),
CONSTRAINT cor_taxref_nomenclature_isregne_check CHECK (taxonomie.check_is_regne(regne::text) OR regne::text = 'all'::text)
)
WITH (
OIDS=FALSE
date_update timestamp without time zone
);


CREATE TABLE cor_taxref_sensibilite
(
cd_nom integer NOT NULL,
id_nomenclature_niv_precis integer NOT NULL,
id_nomenclature integer NOT NULL,
duree_sensibilite integer NOT NULL,
territoire_sensibilite character varying(50),
date_create timestamp without time zone DEFAULT now(),
date_update timestamp without time zone
);


---------------
--PRIMARY KEY--
---------------
ALTER TABLE ONLY cor_taxref_nomenclature
ADD CONSTRAINT pk_cor_taxref_nomenclature PRIMARY KEY (id_nomenclature, regne, group2_inpn);

ALTER TABLE ONLY cor_taxref_sensibilite
ADD CONSTRAINT pk_cor_taxref_sensibilite PRIMARY KEY (cd_nom, id_nomenclature_niv_precis, id_nomenclature);


---------------
--FOREIGN KEY--
---------------
ALTER TABLE ONLY cor_taxref_nomenclature
ADD CONSTRAINT fk_cor_taxref_nomenclature_id_nomenclature FOREIGN KEY (id_nomenclature) REFERENCES meta.t_nomenclatures(id_nomenclature) ON UPDATE CASCADE;


ALTER TABLE ONLY cor_taxref_sensibilite
ADD CONSTRAINT fk_cor_taxref_sensibilite_cd_nom FOREIGN KEY (cd_nom) REFERENCES taxref(cd_nom) ON UPDATE CASCADE;

ALTER TABLE ONLY cor_taxref_sensibilite
ADD CONSTRAINT fk_cor_taxref_sensibilite_niv_precis FOREIGN KEY (id_nomenclature_niv_precis) REFERENCES meta.t_nomenclatures(id_nomenclature) ON UPDATE CASCADE;

ALTER TABLE ONLY cor_taxref_sensibilite
ADD CONSTRAINT fk_cor_taxref_sensibilite_id_nomenclature FOREIGN KEY (id_nomenclature) REFERENCES meta.t_nomenclatures(id_nomenclature) ON UPDATE CASCADE;


--------------
--CONSTRAINS--
--------------
ALTER TABLE ONLY cor_taxref_nomenclature
ADD CONSTRAINT check_cor_taxref_nomenclature_isgroup2inpn CHECK (check_is_group2inpn(group2_inpn::text) OR group2_inpn::text = 'all'::text);

ALTER TABLE ONLY cor_taxref_nomenclature
ADD CONSTRAINT check_cor_taxref_nomenclature_isregne CHECK (check_is_regne(regne::text) OR regne::text = 'all'::text);


ALTER TABLE ONLY cor_taxref_sensibilite
ADD CONSTRAINT check_cor_taxref_sensibilite_niv_precis CHECK (meta.check_type_nomenclature(id_nomenclature_niv_precis,5));


---------
--VIEWS--
---------

CREATE OR REPLACE VIEW meta.v_nomenclature_taxonomie AS
SELECT tn.id_type_nomenclature,
SELECT tn.id_type_nomenclature,
tn.libelle_type_nomenclature,
tn.definition_type_nomenclature,
ctn.regne,
Expand All @@ -98,8 +188,8 @@ CREATE OR REPLACE VIEW meta.v_nomenclature_taxonomie AS
n.definition_nomenclature,
n.id_parent,
n.hierarchie
FROM meta.t_nomenclatures n
JOIN meta.bib_types_nomenclatures tn ON tn.id_type_nomenclature = n.id_type_nomenclature
JOIN taxonomie.cor_taxref_nomenclature ctn ON ctn.id_nomenclature = n.id_nomenclature
FROM meta.t_nomenclatures n
JOIN meta.bib_types_nomenclatures tn ON tn.id_type_nomenclature = n.id_type_nomenclature
JOIN taxonomie.cor_taxref_nomenclature ctn ON ctn.id_nomenclature = n.id_nomenclature
WHERE n.id_parent <> 0
ORDER BY tn.id_type_nomenclature, ctn.regne, ctn.group2_inpn, n.id_nomenclature;
12 changes: 12 additions & 0 deletions data/core/data_nomenclatures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@ INSERT INTO cor_taxref_nomenclature VALUES (99, 'all', 'all', now(), NULL);
INSERT INTO cor_taxref_nomenclature VALUES (100, 'all', 'all', now(), NULL);
INSERT INTO cor_taxref_nomenclature VALUES (101, 'all', 'all', now(), NULL);


---------------------------
--Statut de validation--
---------------------------
Expand All @@ -1257,3 +1258,14 @@ INSERT INTO cor_taxref_nomenclature VALUES (347, 'all', 'all', now(), NULL);
INSERT INTO cor_taxref_nomenclature VALUES (348, 'all', 'all', now(), NULL);
INSERT INTO cor_taxref_nomenclature VALUES (349, 'all', 'all', now(), NULL);
INSERT INTO cor_taxref_nomenclature VALUES (350, 'all', 'all', now(), NULL);


---------------------------------------
--Niveau de précision de la diffusion--
---------------------------------------
INSERT INTO cor_taxref_nomenclature VALUES (158, 'all', 'all', now(), NULL);
INSERT INTO cor_taxref_nomenclature VALUES (159, 'all', 'all', now(), NULL);
INSERT INTO cor_taxref_nomenclature VALUES (160, 'all', 'all', now(), NULL);
INSERT INTO cor_taxref_nomenclature VALUES (161, 'all', 'all', now(), NULL);
INSERT INTO cor_taxref_nomenclature VALUES (162, 'all', 'all', now(), NULL);
INSERT INTO cor_taxref_nomenclature VALUES (163, 'all', 'all', now(), NULL);
Loading

3 comments on commit 46904bf

@camillemonchicourt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je pense que GN ne devrait rien rajouter dans le schéma taxonomie.

Celui-ci devrait etre exactement comme celui de TaxHub comme un potentiel schema fille.

Du coup le debut avec les VM et les fonctions je les basculerai dans TaxHub et pas juste dans GN.

Les cor entre taxonomie et nomenclatures je les basculerai dans meta de GN.

Et ce qui est lié a la sensibilité je le mettrai dans meta ou synthese ou un autre schema de GN genre core ou du genre.

@gildeluermoz
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mouais !
Il faut bien définir pourquoi le schéma taxonomie ne doit rien comporter d'autre que ce qui concerne la taxo INPN et TaxHub. Les tables actuellement ajoutées sont bien en lien avec la taxonomie, au moins autant que ce qui est dans taxonomie.medias ou en lien avec les attributs des taxons. Il s'agit de vocabulaires liés à la taxonomie ou de la sensibilité de tel ou tel taxon.
Si on doit faire une modif de cet ordre, ce n'est actuellement pas très complexe mais il faut définir une logique. On peut :

  • laisser les tables concernée dans le schéma taxonomie et les prefixer gn_*
  • créer un schéma nomenclature et basculer dedans tout ce qui concerne la nomenclature, y compris les vues qui regroupent les nomenclatures par type, actuellement, à tord probablement, dans le schéma contactfaune
  • créer un schéma taxogeonature ou taxonomiegeonature

Je penche pour la solution 2 car tout ce qui tourne autour de la nomenclature est un mécanisme complexe (tables, hiérarchie, vues, fonctions, contraintes) qu'il peut être utile d'isoler pour clarifier son usage.

@camillemonchicourt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK donc on part sur le principe que le schema taxonomie de GeoNature doit être le même que celui de TaxHub. Ça simplifiera la lisibilité et la maintenance.

Du coup à terme, on basculera la création des VM et des fonctions liées à la taxonomie (vm_regne, check_isinbibnoms....) dans TaxHub.

Pour la nomenclature on retient la proposition 2, avec la création d'un schéma nomenclature où on va y déplacer meta.t_nomenclature et meta.bib_types_nomenclatures) et on va aussi basculer cor_taxref_nomenclature ainsi que ce qui concerne la sensibilité.

Please # to comment.