-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sql
More file actions
514 lines (460 loc) · 24.8 KB
/
install.sql
File metadata and controls
514 lines (460 loc) · 24.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
---
--- GENERATED BY SQL-BUILDER
--- PLEASE DO NOT EDIT DIRECTLY
--- CREATED AT Wed, 08 Mar 2023 19:59:12 +0100
---
---
--- FROM: plpgsql-json-to-html.sql ---
---
---
--- IMPLEMENTATION
---
drop function if exists html_tag;
create or replace function html_tag (name text, attr json, children json) returns text as
$$
DECLARE
html text;
_tag_name text;
_k text;
_v text;
_child json;
_child_text text;
_void_elements text[] = ARRAY['!doctype', 'meta', 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'param', 'source', 'track', 'wbr'];
begin
_tag_name := lower(trim('"' FROM name::text));
html := '<' || _tag_name;
for _k, _v in
select * from json_each_text(attr)
loop
_k := lower(trim('"' FROM _k::text));
_v := lower(trim('"' FROM _v::text));
if length(_v) > 0 then
html := html || ' ' || _k || '="' || _v || '"';
else
html := html || ' ' || _k;
end if;
-- raise notice 'tag attr: k %, v %', _k, _v;
end loop;
if json_array_length(children) > 0 or
array_length(array_positions(_void_elements, _tag_name), 1) IS NULL -- test if _tag_name is NOT contained in
then
html := html || '>';
for _child in
select * from json_array_elements(children)
loop
if json_typeof(_child) = 'object' then
html := html || (select html_tag(cast(_child->'t' as text), _child->'a', _child->'c'));
elseif json_typeof(_child) = 'string' then
_child_text := trim('"' FROM _child::text);
html := html || _child_text;
end if;
end loop;
html := html || '</' || _tag_name || '>';
else
html := html || '>';
end if;
-- raise notice 'tag html: %', html;
return html;
end;
$$ language plpgsql;
drop function if exists json_to_html_pg;
create or replace function json_to_html_pg (a json) returns text as
$$
declare
html text;
tag_json json;
begin
for tag_json in
select * from json_array_elements(a)
loop
html := coalesce(html, '') || (select html_tag(cast(tag_json->'t' as text), tag_json->'a', tag_json->'c'));
end loop;
raise notice 'html: %', html;
return html;
end;
$$ language plpgsql;
---
--- FROM: python3u-json-to-html.sql ---
---
create extension plpython3u;
drop function if exists json_to_html;
create or replace function json_to_html (a json)
returns text
as $$
void_elements = ["!doctype", "meta", "area", "base", "br", "col", "embed", "hr", "img", "input", "keygen", "link", "param", "source", "track", "wbr"]
def parse_attr(attr):
import json
return json.loads(attr)
def tag(__name, *args, **kwargs):
g = "<"
g += __name.lower()
#plpy.info(__name.lower())
for k, v in kwargs.items():
if len(v) > 0:
g += " {}=\"{}\"".format(k.lower(), v.lower())
else:
g += " {}".format(k.lower())
g += ">"
if len(args) > 0 or __name not in void_elements:
for el in args:
if type(el) is dict:
g += tag(el['t'], *el['c'], **el['a'])
else:
g += el
g += "</{}>".format(__name.lower())
return g
attr = parse_attr(a)
#plpy.info(attr, type(attr))
r = ""
for el in attr:
r += tag(el['t'], *el['c'], **el['a'])
return r
$$ language plpython3u;
---
--- FROM: table-to-html.sql ---
---
drop function if exists table_to_html;
create or replace function table_to_html(statement text, title text, standalone bool default true, stylesheet text default 'https://unpkg.com/@picocss/pico@1.*/css/pico.classless.min.css') returns text as
$$
DECLARE
_table_json json;
_table_output json;
_table_head json;
_table_body json;
_table_el json;
_table_el_k text;
_table_el_v text;
_table_tr json;
begin
execute 'select json_agg(t) from (' || statement || ') t'
into _table_json;
_table_body := json_build_array();
_table_head := json_build_array();
-- thead
for _table_el_k, _table_el_v in select *
from json_each_text(
json_array_element(_table_json, 0)
)
loop
_table_head := _table_head::jsonb || json_build_array(
json_build_object(
't', 'th',
'a', json_build_object(),
'c', json_build_array(_table_el_k)
)
)::jsonb;
end loop;
-- tbody
for _table_el in select * from json_array_elements(_table_json)
loop
_table_tr := json_build_array();
-- reset
-- loop through k,v pairs
for _table_el_k, _table_el_v in select * from json_each_text(_table_el)
loop
_table_tr := _table_tr::jsonb || json_build_array(
json_build_object(
't', 'td',
'a', json_build_object(),
'c', json_build_array(_table_el_v)
)
)::jsonb;
end loop;
-- raise notice 'tr %', _table_tr::text;
-- create table bodyj
_table_body := _table_body::jsonb || json_build_array(json_build_object(
't', 'tr',
'a', json_build_object(),
'c', _table_tr
))::jsonb;
end loop;
_table_output := json_build_array(
json_build_object(
't', 'table',
'a', json_build_object(),
'c', json_build_array(
json_build_object(
't', 'thead',
'a', json_build_object(),
'c', json_build_array(
json_build_object(
't', 'tr',
'a', json_build_object(),
'c', _table_head
)
)
),
json_build_object(
't', 'tbody',
'a', json_build_object(),
'c', _table_body
)
)
)
);
if standalone then
-- put table into fully compliant html file (vscode template)
_table_output := json_build_array(
json_build_object('t', '!doctype', 'a', json_build_object('html', ''), 'c', json_build_array()),
json_build_object('t', 'html', 'a', json_build_object('lang', 'en'), 'c',
json_build_array(json_build_object('t', 'head', 'a', json_build_object(), 'c',
json_build_array(json_build_object('t', 'meta',
'a',
json_build_object('charset', 'UTF-8'),
'c',
json_build_array()),
json_build_object('t', 'meta',
'a',
json_build_object(
'http-equiv',
'X-UA-Compatible',
'content',
'IE=edge'),
'c',
json_build_array()),
json_build_object(
't', 'link',
'a', json_build_object(
'rel', 'stylesheet',
'href', stylesheet
),
'c', json_build_array()
),
json_build_object('t', 'meta',
'a',
json_build_object(
'name',
'viewport',
'content',
'width=device-width, initial-scale=1.0'),
'c',
json_build_array()),
json_build_object('t', 'title',
'a',
json_build_object(),
'c',
json_build_array(title)))),
json_build_object('t', 'body', 'a', json_build_object(), 'c',
json_build_array(
json_build_object(
't', 'main',
'a', json_build_object(
'style', 'overflow: auto;'
),
'c', json_build_array(
json_build_object('t', 'h1', 'a',
json_build_object(),
'c',
json_build_array(title))
, json_build_object(
't', 'div',
'a', json_build_object(),
'c', _table_output
)
)
)
)))));
end if;
return json_to_html(_table_output);
end;
$$ language plpgsql;
---
--- FROM: table-to-html-pg.sql ---
---
drop function if exists table_to_html_pg;
create or replace function table_to_html_pg(statement text, title text, standalone bool default true, stylesheet text default 'https://unpkg.com/@picocss/pico@1.*/css/pico.classless.min.css') returns text as
$$
DECLARE
_table_json json;
_table_output json;
_table_head json;
_table_body json;
_table_el json;
_table_el_k text;
_table_el_v text;
_table_tr json;
begin
execute 'select json_agg(t) from (' || statement || ') t'
into _table_json;
_table_body := json_build_array();
_table_head := json_build_array();
-- thead
for _table_el_k, _table_el_v in select *
from json_each_text(
json_array_element(_table_json, 0)
)
loop
_table_head := _table_head::jsonb || json_build_array(
json_build_object(
't', 'th',
'a', json_build_object(),
'c', json_build_array(_table_el_k)
)
)::jsonb;
end loop;
-- tbody
for _table_el in select * from json_array_elements(_table_json)
loop
_table_tr := json_build_array();
-- reset
-- loop through k,v pairs
for _table_el_k, _table_el_v in select * from json_each_text(_table_el)
loop
_table_tr := _table_tr::jsonb || json_build_array(
json_build_object(
't', 'td',
'a', json_build_object(),
'c', json_build_array(_table_el_v)
)
)::jsonb;
end loop;
-- raise notice 'tr %', _table_tr::text;
-- create table bodyj
_table_body := _table_body::jsonb || json_build_array(json_build_object(
't', 'tr',
'a', json_build_object(),
'c', _table_tr
))::jsonb;
end loop;
_table_output := json_build_array(
json_build_object(
't', 'table',
'a', json_build_object(),
'c', json_build_array(
json_build_object(
't', 'thead',
'a', json_build_object(),
'c', json_build_array(
json_build_object(
't', 'tr',
'a', json_build_object(),
'c', _table_head
)
)
),
json_build_object(
't', 'tbody',
'a', json_build_object(),
'c', _table_body
)
)
)
);
if standalone then
-- put table into fully compliant html file (vscode template)
_table_output := json_build_array(
json_build_object('t', '!doctype', 'a', json_build_object('html', ''), 'c', json_build_array()),
json_build_object('t', 'html', 'a', json_build_object('lang', 'en'), 'c',
json_build_array(json_build_object('t', 'head', 'a', json_build_object(), 'c',
json_build_array(json_build_object('t', 'meta',
'a',
json_build_object('charset', 'UTF-8'),
'c',
json_build_array()),
json_build_object('t', 'meta',
'a',
json_build_object(
'http-equiv',
'X-UA-Compatible',
'content',
'IE=edge'),
'c',
json_build_array()),
json_build_object(
't', 'link',
'a', json_build_object(
'rel', 'stylesheet',
'href', stylesheet
),
'c', json_build_array()
),
json_build_object('t', 'meta',
'a',
json_build_object(
'name',
'viewport',
'content',
'width=device-width, initial-scale=1.0'),
'c',
json_build_array()),
json_build_object('t', 'title',
'a',
json_build_object(),
'c',
json_build_array(title)))),
json_build_object('t', 'body', 'a', json_build_object(), 'c',
json_build_array(
json_build_object(
't', 'main',
'a', json_build_object(
'style', 'overflow: auto;'
),
'c', json_build_array(
json_build_object('t', 'h1', 'a',
json_build_object(),
'c',
json_build_array(title))
, json_build_object(
't', 'div',
'a', json_build_object(),
'c', _table_output
)
)
)
)))));
end if;
return json_to_html_pg(_table_output);
end;
$$ language plpgsql;
---
--- FROM: html-caching.sql ---
---
drop table if exists html_cache;
create table html_cache (
hash text NOT NULL,
content text NOT NULL,
created_at timestamp NOT NULL,
primary key (hash)
);
drop function if exists add_to_html_cache;
create or replace function add_to_html_cache(_q text, _content text) returns bool as
$$
declare
_hash text;
begin
-- how do i know when result-set is new??
_hash := md5( _q )::text;
insert into html_cache (hash, content, created_at) values (_hash, _content, NOW()) ON CONFLICT (hash) DO UPDATE
SET created_at = NOW(),
content = EXCLUDED.content;
return true;
end
$$ language plpgsql;
-- gets the html for a table from cache || create it
-- cache is stale after 1 hour
drop function if exists get_from_cache_or_compute;
create or replace function get_from_cache_or_compute(_q text, _name text) returns text as
$$
declare
_content text;
_exists bool;
_is_recent bool;
_hash text;
begin
_hash := md5( _q )::text;
select exists(select 1 from html_cache where hash = _hash) into _exists;
if _exists then
-- test if was created less than one hour ago
select age(now(), created_at) < age(now() + interval '1 hours', now()) from html_cache where hash = _hash into _is_recent;
else
_is_recent := false;
end if;
if _exists and _is_recent then
select content from html_cache where hash = _hash into _content;
else
select table_to_html(_q, _name) into _content;
perform add_to_html_cache(_q, _content);
end if;
return _content;
end
$$ language plpgsql;