-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintl.sql
More file actions
172 lines (149 loc) · 4.54 KB
/
intl.sql
File metadata and controls
172 lines (149 loc) · 4.54 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
-- FIDATA. Open-source system for analysis of financial and economic data
-- Copyright © 2013 Basil Peace
/*
This file is part of FIDATA.
FIDATA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FIDATA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FIDATA. If not, see <http://www.gnu.org/licenses/>.
*/
CREATE DOMAIN common.lang_id character(3);
CREATE TABLE langs (
id common.lang_id PRIMARY KEY, -- ISO 639-2/T code
CHECK (common.is_valid_alpha_code(id)),
part1_code character(2) -- ISO 639-1 code, if available
CHECK (common.is_valid_alpha_code(part1_code) IS DISTINCT FROM FALSE),
name character varying(32) NOT NULL
CHECK (char_length(name) > 0)
);
CREATE UNIQUE INDEX ON langs(part1_code);
CREATE UNIQUE INDEX ON langs(upper(name));
CREATE FUNCTION triggers.langs_set_codes_case() RETURNS trigger
LANGUAGE plpgsql VOLATILE
AS $$
BEGIN
NEW.id := lower(NEW.id);
NEW.part1_code := lower(NEW.part1_code);
RETURN NEW;
END
$$;
CREATE TRIGGER set_codes_case
BEFORE INSERT
ON langs
FOR EACH ROW
EXECUTE PROCEDURE triggers.langs_set_codes_case()
;
CREATE FUNCTION get_lang(lang_id common.lang_id, OUT res langs)
LANGUAGE plpgsql STABLE RETURNS NULL ON NULL INPUT
AS $$
BEGIN
SELECT * FROM langs WHERE id = lower(lang_id) INTO res;
END
$$;
CREATE FUNCTION get_lang_by_part1_code(lang_part1_code character(2), OUT res langs)
LANGUAGE plpgsql STABLE RETURNS NULL ON NULL INPUT
AS $$
BEGIN
SELECT * FROM langs WHERE part1_code = lower(lang_part1_code) INTO res;
END
$$;
CREATE FUNCTION get_lang_by_name(lang_name character varying(32), OUT res langs)
LANGUAGE plpgsql STABLE RETURNS NULL ON NULL INPUT
AS $$
BEGIN
SELECT * FROM langs WHERE upper(name) = upper(lang_name) INTO res;
END
$$;
CREATE DOMAIN common.script_id character(4);
CREATE TABLE scripts (
id common.script_id PRIMARY KEY
CHECK (common.is_valid_alpha_code(id)),
num_code character(3)
CHECK (common.is_valid_numerical_code(num_code) IS DISTINCT FROM FALSE),
name character varying(64) NOT NULL
CHECK (char_length(name) > 0)
);
CREATE UNIQUE INDEX ON scripts(num_code);
CREATE UNIQUE INDEX ON scripts(upper(name));
CREATE FUNCTION triggers.scripts_set_codes_case() RETURNS trigger
LANGUAGE plpgsql VOLATILE
AS $$
BEGIN
NEW.id := initcap(NEW.id);
RETURN NEW;
END
$$;
CREATE TRIGGER set_codes_case
BEFORE INSERT
ON scripts
FOR EACH ROW
EXECUTE PROCEDURE triggers.scripts_set_codes_case()
;
CREATE FUNCTION get_script(script_id common.script_id, OUT res scripts)
LANGUAGE plpgsql STABLE RETURNS NULL ON NULL INPUT
AS $$
BEGIN
SELECT * FROM scripts WHERE id = initcap(script_id) INTO res;
END
$$;
CREATE FUNCTION get_script_by_name(script_name character varying(64), OUT res scripts)
LANGUAGE plpgsql STABLE RETURNS NULL ON NULL INPUT
AS $$
BEGIN
SELECT * FROM scripts WHERE upper(name) = upper(script_name) INTO res;
END
$$;
CREATE FUNCTION is_valid_lang_script(lang_id common.lang_id, script_id common.script_id) RETURNS bool
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF lang_id IS NULL THEN
RETURN NULL;
ELSIF script_id IS NULL THEN
RETURN EXISTS(SELECT * FROM langs WHERE id = lower(lang_id));
ELSE
RETURN EXISTS(SELECT * FROM langs_scripts WHERE (lang = lower(lang_id)) AND (script = initcap(script_id)));
END IF;
END
$$;
CREATE TABLE langs_scripts (
lang common.lang_id NOT NULL
REFERENCES langs (id)
ON UPDATE CASCADE ON DELETE CASCADE,
script common.script_id NOT NULL
REFERENCES scripts (id)
ON UPDATE CASCADE ON DELETE CASCADE,
PRIMARY KEY (lang, script)
);
-- CREATE INDEX ON langs_scripts (lang);
CREATE INDEX ON langs_scripts (script);
CREATE FUNCTION triggers.langs_scripts_insert_only_absent() RETURNS trigger
LANGUAGE plpgsql VOLATILE
AS $$
BEGIN
NEW.lang := lower(NEW.lang);
NEW.script := initcap(NEW.script);
IF NOT EXISTS(SELECT * FROM langs_scripts where (lang = NEW.lang) AND (script = NEW.script)) THEN
RETURN NEW;
ELSE
RETURN NULL;
END IF;
END
$$;
CREATE TRIGGER insert_only_absent
BEFORE INSERT
ON langs_scripts
FOR EACH ROW
EXECUTE PROCEDURE triggers.langs_scripts_insert_only_absent()
;
GRANT
DELETE
ON TABLE langs_scripts
TO fidata
;