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