Project

General

Profile

1
DROP VIEW organizations_view;
2
DROP VIEW organizations_simple_view;
3

    
4
DROP TABLE IF EXISTS user_countries;
5
DROP TABLE IF EXISTS users;
6
DROP TABLE IF EXISTS other_ids;
7
DROP TABLE IF EXISTS other_names;
8
DROP TABLE IF EXISTS acronyms;
9
DROP TABLE IF EXISTS relationships;
10
DROP TABLE IF EXISTS types;
11
DROP TABLE IF EXISTS urls;
12
DROP TABLE IF EXISTS openaire_simrels;
13
DROP TABLE IF EXISTS organizations;
14
DROP TABLE IF EXISTS org_types;
15
DROP TABLE IF EXISTS id_types;
16
DROP TABLE IF EXISTS languages;
17
DROP TABLE IF EXISTS countries;
18

    
19

    
20
CREATE TABLE org_types (val text PRIMARY KEY);
21
INSERT INTO org_types VALUES ('Archive'), ('Company'), ('Education'), ('Facility'), ('Government'), ('Healthcare'), ('Nonprofit'), ('Other');
22

    
23
CREATE TABLE id_types (val text PRIMARY KEY);
24
INSERT INTO id_types VALUES ('CNRS'), ('FundRef'), ('HESA'), ('ISNI'), ('OrgRef'), ('UCAS'), ('UKPRN'), ('Wikidata'), ('grid.ac');
25

    
26
CREATE TABLE languages (val text PRIMARY KEY);
27
INSERT INTO languages VALUES ('UNKNOWN'), ('aa'), ('af'), ('am'), ('ar'), ('as'), ('az'), ('ba'), ('be'), ('bg'), ('bn'), ('br'), ('bs'), ('ca'), ('ch'), ('co'), ('cs'), ('cy'), ('da'),
28
			('de'), ('dv'), ('dz'), ('el'), ('en'), ('eo'), ('es'), ('et'), ('eu'), ('fa'), ('fi'), ('fr'), ('fy'), ('ga'), ('gd'), ('gl'), ('gu'), ('he'), ('hi'), ('hr'), ('hu'), ('hy'), ('id'), ('is'),
29
			('it'), ('iu'), ('ja'), ('jv'), ('ka'), ('kk'), ('kl'), ('km'), ('kn'), ('ko'), ('ku'), ('ky'), ('la'), ('lb'), ('lo'), ('lt'), ('lv'), ('mg'), ('mi'), ('mk'), ('ml'), ('mn'), ('mr'), ('ms'),
30
			('mt'), ('my'), ('nb'), ('ne'), ('nl'), ('nn'), ('no'), ('oc'), ('or'), ('pa'), ('pl'), ('ps'), ('pt'), ('rm'), ('ro'), ('ru'), ('rw'), ('sa'), ('sd'), ('si'), ('sk'), ('sl'), ('so'), ('sq'),
31
			('sr'), ('sv'), ('sw'), ('ta'), ('te'), ('tg'), ('th'), ('tk'), ('tl'), ('tr'), ('tt'), ('ug'), ('uk'), ('ur'), ('uz'), ('vi'), ('xh'), ('yo'), ('zh'), ('zu');
32

    
33
CREATE TABLE countries (val text PRIMARY KEY);
34
INSERT INTO countries VALUES ('UNKNOWN'), ('AD'), ('AE'), ('AF'), ('AG'), ('AL'), ('AM'), ('AO'), ('AR'), ('AT'), ('AU'), ('AW'), ('AX'), ('AZ'), ('BA'), ('BB'), ('BD'), ('BE'), ('BF'), ('BG'), ('BH'), ('BI'),
35
			('BJ'), ('BM'), ('BN'), ('BO'), ('BQ'), ('BR'), ('BS'), ('BT'), ('BW'), ('BY'), ('BZ'), ('CA'), ('CD'), ('CF'), ('CG'), ('CH'), ('CI'), ('CL'), ('CM'), ('CN'), ('CO'), ('CR'),
36
			('CU'), ('CV'), ('CW'), ('CY'), ('CZ'), ('DE'), ('DJ'), ('DK'), ('DM'), ('DO'), ('DZ'), ('EC'), ('EE'), ('EG'), ('EH'), ('ER'), ('ES'), ('ET'), ('FI'), ('FJ'), ('FM'), ('FO'),
37
			('FR'), ('GA'), ('GB'), ('GD'), ('GE'), ('GF'), ('GH'), ('GI'), ('GL'), ('GM'), ('GN'), ('GP'), ('GQ'), ('GR'), ('GT'), ('GW'), ('GY'), ('HN'), ('HR'), ('HT'), ('HU'), ('ID'),
38
			('IE'), ('IL'), ('IM'), ('IN'), ('IQ'), ('IR'), ('IS'), ('IT'), ('JE'), ('JM'), ('JO'), ('JP'), ('KE'), ('KG'), ('KH'), ('KN'), ('KP'), ('KR'), ('KW'), ('KY'), ('KZ'), ('LA'),
39
			('LB'), ('LC'), ('LI'), ('LK'), ('LR'), ('LS'), ('LT'), ('LU'), ('LV'), ('LY'), ('MA'), ('MC'), ('MD'), ('ME'), ('MG'), ('MK'), ('ML'), ('MM'), ('MN'), ('MO'), ('MQ'), ('MR'),
40
			('MS'), ('MT'), ('MU'), ('MV'), ('MW'), ('MX'), ('MY'), ('MZ'), ('NA'), ('NC'), ('NE'), ('NG'), ('NI'), ('NL'), ('NO'), ('NP'), ('NU'), ('NZ'), ('OM'), ('PA'), ('PE'), ('PF'),
41
			('PG'), ('PH'), ('PK'), ('PL'), ('PS'), ('PT'), ('PW'), ('PY'), ('QA'), ('RE'), ('RO'), ('RS'), ('RU'), ('RW'), ('SA'), ('SB'), ('SC'), ('SD'), ('SE'), ('SG'), ('SI'), ('SJ'),
42
			('SK'), ('SL'), ('SM'), ('SN'), ('SO'), ('SR'), ('SS'), ('ST'), ('SV'), ('SX'), ('SY'), ('SZ'), ('TC'), ('TD'), ('TG'), ('TH'), ('TJ'), ('TL'), ('TM'), ('TN'), ('TO'), ('TR'),
43
			('TT'), ('TV'), ('TW'), ('TZ'), ('UA'), ('UG'), ('US'), ('UY'), ('UZ'), ('VA'), ('VC'), ('VE'), ('VG'), ('VN'), ('WS'), ('XK'), ('YE'), ('ZA'), ('ZM'), ('ZW');
44

    
45
CREATE TABLE users (
46
	email     text PRIMARY KEY,
47
	password  text NOT NULL,
48
	valid     boolean default true,
49
	superuser boolean default false
50
);
51

    
52
CREATE TABLE user_countries (
53
	email     text REFERENCES users(email),
54
	country   text  REFERENCES countries(val),
55
	PRIMARY KEY(email, country)	
56
);
57

    
58
CREATE TABLE organizations (
59
    id                text PRIMARY KEY,
60
    name              text,
61
    lat               double precision,
62
	lng               double precision,
63
	city              text,
64
	country           text REFERENCES countries(val),
65
	created_by        text,
66
	creation_date     timestamp with time zone DEFAULT now(),
67
	modified_by       text,
68
	modification_date timestamp with time zone DEFAULT now()
69
);
70

    
71
CREATE TABLE other_ids (
72
	id      text REFERENCES organizations(id),
73
	otherid text,
74
	type    text REFERENCES id_types(val),
75
	PRIMARY KEY (id, otherid, type)
76
);
77

    
78
CREATE TABLE other_names (
79
	id    text REFERENCES organizations(id),
80
	name  text,
81
	lang  text REFERENCES languages(val),
82
	PRIMARY KEY (id, name, lang)
83
);
84

    
85
CREATE TABLE acronyms (
86
	id text,
87
	acronym text,
88
	PRIMARY KEY (id, acronym)
89
);
90

    
91
CREATE TABLE relationships (
92
	id1     text REFERENCES organizations(id),
93
	reltype text,
94
	id2     text REFERENCES organizations(id),
95
    PRIMARY KEY (id1, reltype, id2)
96
);
97

    
98
CREATE TABLE types (
99
	id   text REFERENCES organizations(id),
100
	type text REFERENCES org_types(val),
101
	PRIMARY KEY (id, type)
102
);
103

    
104
CREATE TABLE urls (
105
	id  text REFERENCES organizations(id),
106
	url text,
107
	PRIMARY KEY (id, url)
108
);
109

    
110
CREATE TABLE openaire_simrels (
111
	local_id         text REFERENCES organizations(id),
112
	oa_id            text,
113
	oa_original_id   text,
114
	oa_name          text,
115
	oa_acronym       text,
116
	oa_country       text,
117
	oa_url           text,
118
	oa_collectedfrom text,
119
	reltype          text  NOT NULL DEFAULT 'suggested',
120
	PRIMARY KEY (local_id, oa_id)
121
);
122

    
123
CREATE VIEW organizations_view AS SELECT
124
    org.id,
125
    org.name,
126
    org.lat,
127
	org.lng,
128
	org.city,
129
	org.country,
130
	org.created_by,
131
	org.creation_date,
132
	org.modified_by,
133
	org.modification_date,
134
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('id', oid.otherid, 'type', oid.type)) FILTER (WHERE oid.otherid IS NOT NULL), '[]') AS other_ids,
135
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('name', n.name, 'lang', n.lang))      FILTER (WHERE n.name      IS NOT NULL), '[]') AS other_names,
136
	COALESCE(jsonb_agg(DISTINCT a.acronym)                                               FILTER (WHERE a.acronym   IS NOT NULL), '[]') AS acronyms,	
137
	COALESCE(jsonb_agg(DISTINCT t.type::text)                                            FILTER (WHERE t.type      IS NOT NULL), '[]') AS types,
138
	COALESCE(jsonb_agg(DISTINCT u.url)                                                   FILTER (WHERE u.url       IS NOT NULL), '[]') AS urls
139
FROM
140
    organizations org
141
    LEFT OUTER JOIN other_ids oid    ON (org.id = oid.id)
142
    LEFT OUTER JOIN other_names n    ON (org.id = n.id)
143
    LEFT OUTER JOIN acronyms a       ON (org.id = a.id)
144
    LEFT OUTER JOIN types t          ON (org.id = t.id)
145
    LEFT OUTER JOIN urls u           ON (org.id = u.id)
146
GROUP BY
147
    org.id,
148
    org.name,
149
    org.lat,
150
	org.lng,
151
	org.city,
152
	org.country,
153
	org.created_by,
154
	org.creation_date,
155
	org.modified_by,
156
	org.modification_date;
157
	
158
CREATE VIEW organizations_simple_view AS SELECT
159
    org.id,
160
    org.name,
161
	org.city,
162
	org.country,
163
	array_agg(DISTINCT a.acronym)    FILTER (WHERE a.acronym IS NOT NULL) AS acronyms,	
164
	array_agg(DISTINCT t.type::text) FILTER (WHERE t.type    IS NOT NULL) AS types
165
FROM
166
    organizations org
167
    LEFT OUTER JOIN acronyms a       ON (org.id = a.id)
168
    LEFT OUTER JOIN types t          ON (org.id = t.id)
169
GROUP BY
170
    org.id,
171
    org.name,
172
	org.city,
173
	org.country;
174
	
(3-3/3)