-
Notifications
You must be signed in to change notification settings - Fork 2
/
06_sources.sql
82 lines (74 loc) · 1.93 KB
/
06_sources.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
SOURCES
-------
Create sources from sites, each VisioNature site become a source.
*/
BEGIN;
CREATE INDEX IF NOT EXISTS i_source_name ON gn_synthese.t_sources (name_source);
DROP FUNCTION IF EXISTS src_lpodatas.fct_c_upsert_or_get_source_from_visionature (
_source TEXT
);
CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_upsert_or_get_source_from_visionature(
_source TEXT
)
RETURNS INTEGER
AS
$$
DECLARE
thenewid INT;
BEGIN
IF (
SELECT
exists(
SELECT
1
FROM
gn_synthese.t_sources
WHERE
name_source LIKE _source)) THEN
SELECT
id_source
INTO thenewid
FROM
gn_synthese.t_sources
WHERE
name_source LIKE _source;
RAISE DEBUG 'Source named % already exists with id %', _source, thenewid;
ELSE
INSERT INTO gn_synthese.t_sources (name_source)
VALUES (_source)
RETURNING id_source INTO thenewid;
RAISE DEBUG 'Source named % inserted with id %', _source, thenewid;
RETURN thenewid;
END IF;
RETURN thenewid;
END
$$
LANGUAGE plpgsql;
COMMENT ON FUNCTION src_lpodatas.fct_c_upsert_or_get_source_from_visionature(
_source TEXT
) IS 'function to basically create new sources from VisioNature import';
DROP FUNCTION IF EXISTS src_lpodatas.fct_c_get_source_url (
_id_source INT, _id_data TEXT
);
CREATE OR REPLACE FUNCTION src_lpodatas.fct_c_get_source_url(
_id_source INT, _id_data TEXT DEFAULT NULL
)
RETURNS TEXT
AS
$$
DECLARE
the_url TEXT;
BEGIN
SELECT
CASE WHEN _id_data IS NOT NULL THEN concat(url_source, _id_data) ELSE the_url END
INTO the_url
FROM
gn_synthese.t_sources
WHERE
id_source = _id_source;
RETURN the_url;
END ;
$$
LANGUAGE plpgsql;
COMMIT;