24 | | -- fonction pour renvoyer les distances source |
| 26 | alter table rht.rht_topology add column noeudsource boolean default FALSE; |
| 27 | -- identification des noeuds source |
| 28 | update rht.rht_topology set noeudsource=TRUE where id_drain in ( |
| 29 | select id_drain from rht.rht_topology except ( |
| 30 | select rt2.id_drain from rht.rht_topology rt1 join rht.rht_topology rt2 on rt1.nextdownid=rt2.id_drain) |
| 31 | );-- 57533 |
| 32 | update rht.rht_topology set dmer=dmer+length/2;-- 114600 |
| 33 | alter table rht.rht_topology add column dsource numeric; |
| 34 | update rht.rht_topology set dsource=length/2 where noeudsource;-- 57533 |
| 35 | -- voir schéma pour explications |
| 36 | |
| 37 | |
31 | | nbligne:=1; |
32 | | DROP TABLE IF EXISTS rht.distance_mer; |
33 | | CREATE TABLE rht.distance_mer (id_drain integer,distance_mer numeric); |
34 | | FOR id_drains IN SELECT id_drain FROM rht.rht_topology ORDER BY id_drain LOOP |
35 | | -- pour vérification décommenter ensuite |
36 | | RAISE NOTICE 'distance_source for %', quote_ident(CAST(id_drains.id_drain as text)); |
37 | | EXECUTE 'INSERT INTO rht.distance_mer SELECT MAX(dmer) FROM rht.rht_topology WHERE id_drain IN ( |
38 | | SELECT CAST(ltree2text(subpath(chemin, -1, 1))as integer) FROM rht.rht_topology where chemin <@ text2ltree(cast ('|| |
39 | | id_drains.id_drain |
40 | | || 'as text)) )'; |
41 | | nbligne=nbligne+1; |
42 | | END LOOP; |
43 | | RETURN nbligne; |
| 46 | ligne:=1; |
| 47 | DROP TABLE IF EXISTS sousselection; |
| 48 | DROP sequence IF EXISTS seq; |
| 49 | CREATE sequence seq; |
| 50 | EXECUTE 'create temporary table sousselection as SELECT id_drain, nextval(''seq'') as num FROM (SELECT * FROM rht.rht_topology WHERE noeudsource=FALSE ORDER BY id_drain) as sub WHERE id_drain >='|| min || ' AND id_drain<='||max ; |
| 51 | SELECT COUNT(*) FROM sousselection INTO nbligne; |
| 52 | WHILE ligne <=nbligne LOOP |
| 53 | -- pour vérification décommenter ensuite |
| 54 | SELECT * from sousselection where num=ligne INTO id_drains; |
| 55 | RAISE NOTICE 'distance_source for %', quote_ident(CAST(id_drains.id_drain as text)); |
| 56 | EXECUTE 'INSERT INTO rht.distance_source SELECT '||id_drains.id_drain||', MAX(dmer+length/2) as dmer FROM rht.rht_topology WHERE id_drain IN ( |
| 57 | SELECT CAST(ltree2text(subpath(chemin, -1, 1))as integer) FROM rht.rht_topology where chemin ~ ''*.'|| |
| 58 | id_drains.id_drain |
| 59 | || '.*'' )'; |
| 60 | ligne=ligne+1; |
| 61 | END LOOP; |
| 62 | RETURN ligne; |
47 | | select rht.distance_source(); |
| 70 | -- pour essai pour voir ce qui se passe dans la requête |
| 71 | |
| 72 | SELECT 212340, MAX(dmer+length/2) as dmer FROM rht.rht_topology WHERE id_drain IN ( |
| 73 | SELECT CAST(ltree2text(subpath(chemin, -1, 1))as integer) FROM rht.rht_topology where chemin ~ '*.212340.*' ) |
| 74 | -- il faudra enlever la dmer de 212340 |
| 75 | |
| 76 | -- requête finale pour calculer les distances source (à lancer) |
| 77 | alter table rht.distance_source d add column distance source numeric; |
| 78 | update rht.distance_source set distance source = sub.distance_source from ( |
| 79 | select id_drain, distance-dmer as distance_source from rht.distance_source s joint rht.rht_topology t on s.id_drain=t.id_drain) as source |
| 80 | where sub.id_drain=d.id_drain; |