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