| 20 | == utilisation des donnees de parcours == |
| 21 | En pratique nous n'aurons probablement besoin que du noeud le plus amont des parcours comme programmé ci dessus. Mais les scripts ci-dessous pourraient s'avérer utiles. |
| 22 | {{{ |
| 23 | |
| 24 | /* |
| 25 | regexp_split_to_array(string text, pattern text [, flags text ]) regexp_split_to_array('hello world', E'\\s+') {hello,world} |
| 26 | SELECT regexp_split_to_array('hello world', E'\\s+'); |
| 27 | SELECT regexp_split_to_array('hello.world', E'\\.+'); |
| 28 | */ |
| 29 | SELECT regexp_split_to_array(ltree2text(chemin),E'\\.+') from rht.rht_topology where chemin <@ '212340'; |
| 30 | |
| 31 | /* |
| 32 | http://www.pgsql.cz/index.php/PostgreSQL_SQL_Tricks#Array_to_table |
| 33 | SELECT (ARRAY[1,3,4,6,7])[idx.n] FROM generate_series(1,7) idx(n); |
| 34 | CREATE OR REPLACE FUNCTION unpack(anyarray) |
| 35 | RETURNS SETOF anyelement AS $$ |
| 36 | SELECT $1[i] |
| 37 | FROM generate_series(array_lower($1,1), |
| 38 | array_upper($1,1)) g(i); |
| 39 | $$ LANGUAGE sql STRICT IMMUTABLE; |
| 40 | */ |
| 41 | CREATE OR REPLACE FUNCTION unpack(anyarray) |
| 42 | RETURNS SETOF anyelement AS $$ |
| 43 | SELECT $1[i] |
| 44 | FROM generate_series(array_lower($1,1), |
| 45 | array_upper($1,1)) g(i); |
| 46 | $$ LANGUAGE sql STRICT IMMUTABLE; |
| 47 | -- pour convertir un vecteur en table |
| 48 | select unpack(vecteurchemin) from |
| 49 | (SELECT regexp_split_to_array(ltree2text(chemin),E'\\.+') as vecteurchemin from rht.rht_topology where chemin <@ '212340' limit 1) as sub |
| 50 | -- j'avais pas vu il y a déjà une fonction magique unest... |
| 51 | select cast(unnest(vecteurchemin) as integer) as chemin_id_drain from |
| 52 | (SELECT regexp_split_to_array(ltree2text(chemin),E'\\.+') as vecteurchemin from rht.rht_topology where chemin <@ '212340' limit 1) as sub |
| 53 | -- Fonction qui renvoit les identifiants uniques des tronçons en amont d'un ouvrage |
| 54 | |
| 55 | |
| 56 | CREATE OR REPLACE FUNCTION troncons(integer) |
| 57 | RETURNS SETOF integer AS $$ |
| 58 | select cast(unnest(vecteurchemin) as integer) as chemin_id_drain from |
| 59 | (SELECT regexp_split_to_array(ltree2text(chemin),E'\\.+') as vecteurchemin from rht.rht_topology where chemin <@ text2ltree(cast ($1 as text)) limit 1) as sub; |
| 60 | $$ LANGUAGE sql STRICT IMMUTABLE; |
| 61 | |
| 62 | SELECT troncons_amont(212340); -- OK |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | }}} |