word: translations from _word_ref view coalescing parent values
[sheet.git] / tools / word.pg.sql
index 1bc5dffcc78e25f5dea4d0dee6164a6069f71ef9..7aacfa88d5cfb8f76fba04699e9ea87df8adc692 100644 (file)
@@ -48,6 +48,23 @@ CREATE OR REPLACE FUNCTION exportform(word) RETURNS text AS $$
        );
 $$ LANGUAGE SQL IMMUTABLE;
 
+CREATE OR REPLACE VIEW _word_ref AS
+       SELECT
+               r.form, r.alt, r.lang,
+               coalesce(r.cat,     w.cat    ) cat, --TODO translate w?
+               coalesce(r.ref,     r.id     ) "ref",
+               coalesce(r.prio,    w.prio   ) prio,
+               coalesce(r.source,  w.source ) source,
+               coalesce(r.thumb,   w.thumb  ) thumb,
+               coalesce(r.wptitle, w.wptitle) wptitle,
+               r.created, r.updated,
+               CASE WHEN r.source IS NULL THEN w.id ELSE r.id END id, -- image id
+               coalesce(r.cover,   w.cover  ) cover,
+               coalesce(r.grade,   w.grade  ) grade,
+               r.creator
+       FROM word r
+       LEFT JOIN word w ON w.id = r.ref;
+
 CREATE OR REPLACE VIEW _cat_words AS
        SELECT exportform(word.*) form, sub.*, word.lang, word.ref
        FROM word RIGHT JOIN (