word: two-letter language codes
[sheet.git] / tools / word.pg.sql
1 CREATE TABLE word (
2         form       text        NOT NULL,
3         alt        text[],
4         lang       text        NOT NULL DEFAULT 'en',
5         cat        integer              REFERENCES word (id),
6         ref        integer              REFERENCES word (id),
7         prio       smallint    NOT NULL DEFAULT '1',
8         grade      integer,
9         cover      boolean     NOT NULL DEFAULT FALSE,
10         source     text,
11         thumb      text[],
12         wptitle    text,
13         created    timestamptz          DEFAULT now(),
14         id         serial      NOT NULL PRIMARY KEY
15 );
16
17 COMMENT ON COLUMN word.form       IS 'preferred textual representation';
18 COMMENT ON COLUMN word.alt        IS 'alternate forms with equivalent meaning';
19 COMMENT ON COLUMN word.lang       IS 'ISO 639 language code matching wikipedia subdomain';
20 COMMENT ON COLUMN word.cat        IS 'hierarchical classification';
21 COMMENT ON COLUMN word.ref        IS 'reference to equivalent en translation';
22 COMMENT ON COLUMN word.prio       IS 'difficulty level or importance; lower values have precedence';
23 COMMENT ON COLUMN word.grade      IS 'ascending hierarchical order, preceding default alphabetical';
24 COMMENT ON COLUMN word.cover      IS 'highlight if selected';
25 COMMENT ON COLUMN word.source     IS 'URI of downloaded image';
26 COMMENT ON COLUMN word.thumb      IS 'ImageMagick convert options to create thumbnail from source image';
27 COMMENT ON COLUMN word.wptitle    IS 'reference Wikipedia article';
28
29 CREATE OR REPLACE FUNCTION exportform(word) RETURNS text AS $$
30         SELECT concat(
31                 $1.prio || CASE WHEN $1.cover THEN 'c' ELSE '' END || ':',
32                 array_to_string($1.form || $1.alt, '/')
33         );
34 $$ LANGUAGE SQL IMMUTABLE;
35
36 CREATE OR REPLACE VIEW _cat_words AS
37         SELECT exportform(word.*) form, sub.*, word.lang, word.ref
38         FROM word RIGHT JOIN (
39                 SELECT cat id, array_agg(exportform(word.*) ORDER BY grade, form) forms
40                         FROM word WHERE ref IS NULL GROUP BY cat
41         ) sub USING (id);