-
Notifications
You must be signed in to change notification settings - Fork 4
/
04-functions.sql
86 lines (72 loc) · 1.47 KB
/
04-functions.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
83
84
85
86
CREATE OR REPLACE FUNCTION embargoed(embargo_date timestamp)
RETURNS boolean
AS $$
SELECT coalesce(embargo_date > now(), false)
$$
LANGUAGE sql STABLE;
CREATE OR REPLACE FUNCTION is_public(p project)
RETURNS boolean
AS $$ SELECT NOT embargoed(p.embargo_date) $$
LANGUAGE sql STABLE;
CREATE OR REPLACE FUNCTION is_public(s session)
RETURNS boolean
AS $$
BEGIN
IF (s.embargo_date IS NOT null) THEN
RETURN NOT embargoed(s.embargo_date);
END IF;
IF (s.project_id IS NOT null) THEN
RETURN is_public(p)
FROM project p
WHERE s.project_id = p.id;
END IF;
RETURN true;
END;
$$
LANGUAGE plpgsql STABLE;
CREATE OR REPLACE FUNCTION is_public(s sample)
RETURNS boolean
AS $$
BEGIN
IF (s.embargo_date IS NOT null) THEN
RETURN NOT embargoed(s.embargo_date);
END IF;
/* A sample gains its public/private status
primarily from whether it has any public
sessions. */
IF NOT EXISTS (
SELECT sample_id FROM session WHERE sample_id = s.id
) THEN
RETURN true;
END IF;
RETURN EXISTS(
SELECT sample_id
FROM session
WHERE sample_id = s.id
AND is_public(session)
);
END;
$$
LANGUAGE plpgsql STABLE;
CREATE OR REPLACE FUNCTION is_public(a analysis)
RETURNS boolean
AS $$
SELECT is_public(s)
FROM session s
WHERE s.id = a.session_id;
$$
LANGUAGE sql STABLE;
CREATE OR REPLACE FUNCTION is_public(d datum)
RETURNS boolean
AS $$
BEGIN
IF (d.analysis IS NOT null) THEN
RETURN is_public(a)
FROM analysis a
WHERE d.analysis = a.id;
ELSE
RETURN true;
END IF;
END;
$$
LANGUAGE plpgsql STABLE;