|
| 1 | +CREATE TYPE fortune_t AS (id int, message text); |
| 2 | + |
| 3 | +create or replace function fortune_template(f fortune_t) returns text as $$ |
| 4 | + SELECT format('<tr><td>%s</td><td>%s</td></tr>', $1.id, regexp_replace($1.message, '<', '<','g')); |
| 5 | +$$ language sql volatile; |
| 6 | + |
| 7 | +create or replace function fortunes_template(fortunes fortune_t[]) returns text as $$ |
| 8 | +WITH header AS ( |
| 9 | + SELECT 0 as id,'<!DOCTYPE html> |
| 10 | +<html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>' as html |
| 11 | +), footer AS ( |
| 12 | + SELECT 2,'</table></body></html>' as html |
| 13 | +), fortunes AS ( |
| 14 | + SELECT unnest as fortune from unnest($1) |
| 15 | +), additional AS ( |
| 16 | + SELECT (-1, 'Additional fortune added at request time.')::fortune_t as f |
| 17 | +), all_fortunes AS ( |
| 18 | + SELECT * from (SELECT * FROM fortunes UNION ALL SELECT * from additional) p ORDER BY (fortune).message |
| 19 | +), fortunes_html AS ( |
| 20 | + SELECT 1,string_agg(fortune_template(fortune), '') from all_fortunes |
| 21 | +), html AS ( |
| 22 | + SELECT * FROM header UNION SELECT * FROM fortunes_html UNION SELECT * from footer ORDER BY id |
| 23 | +) |
| 24 | +SELECT string_agg(html,'') from html; |
| 25 | +$$ language sql volatile; |
| 26 | + |
| 27 | +create or replace function "fortunes.html"() returns bytea as $$ |
| 28 | +DECLARE |
| 29 | + fortunes fortune_t[]; |
| 30 | +BEGIN |
| 31 | + SET LOCAL "response.headers" = '[{"Content-Type": "text/html"}]'; |
| 32 | + SELECT array_agg(CAST((id,message) AS fortune_t)) FROM "Fortunes" INTO fortunes; |
| 33 | + RETURN convert_to(fortunes_template(fortunes), 'UTF8'); |
| 34 | +END |
| 35 | +$$ language plpgsql volatile; |
0 commit comments