-
Notifications
You must be signed in to change notification settings - Fork 0
/
gutenberg_public.sql
3289 lines (2276 loc) · 85 KB
/
gutenberg_public.sql
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- PostgreSQL database dump
--
-- Dumped from database version 10.23
-- Dumped by pg_dump version 10.23
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: gtrgm; Type: SHELL TYPE; Schema: public; Owner: postgres
--
CREATE TYPE public.gtrgm;
--
-- Name: gtrgm_in(cstring); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gtrgm_in(cstring) RETURNS public.gtrgm
LANGUAGE c STRICT
AS '$libdir/pg_trgm', 'gtrgm_in';
ALTER FUNCTION public.gtrgm_in(cstring) OWNER TO postgres;
--
-- Name: gtrgm_out(public.gtrgm); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gtrgm_out(public.gtrgm) RETURNS cstring
LANGUAGE c STRICT
AS '$libdir/pg_trgm', 'gtrgm_out';
ALTER FUNCTION public.gtrgm_out(public.gtrgm) OWNER TO postgres;
--
-- Name: gtrgm; Type: TYPE; Schema: public; Owner: postgres
--
CREATE TYPE public.gtrgm (
INTERNALLENGTH = variable,
INPUT = public.gtrgm_in,
OUTPUT = public.gtrgm_out,
ALIGNMENT = int4,
STORAGE = plain
);
ALTER TYPE public.gtrgm OWNER TO postgres;
SET default_tablespace = '';
SET default_with_oids = true;
--
-- Name: attributes; Type: TABLE; Schema: public; Owner: gutenberg
--
CREATE TABLE public.attributes (
pk integer DEFAULT nextval(('public.attributes_pk_seq'::text)::regclass) NOT NULL,
fk_books integer NOT NULL,
fk_attriblist integer NOT NULL,
fk_langs character varying(10),
text text NOT NULL,
nonfiling integer DEFAULT 0 NOT NULL,
indicators character varying(2) DEFAULT ' '::character varying,
tsvec tsvector,
CONSTRAINT attributes_text CHECK ((text <> ''::text))
);
ALTER TABLE public.attributes OWNER TO gutenberg;
--
-- Name: _attributes_tsvec(public.attributes); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public._attributes_tsvec(r public.attributes) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- creates tsvector from attributes record
BEGIN
IF r.fk_attriblist IN (240, 245, 246, 440, 505) THEN
-- titles
RETURN setweight (px ('t', r.text), 'B');
ELSIF r.fk_attriblist NOT IN (500, 508, 520, 540, 901, 902, 903, 904, 905) THEN
-- other attributes we want to search
RETURN to_tsvector ('pg_catalog.english', r.text);
END IF;
RETURN NULL;
END;
$$;
ALTER FUNCTION public._attributes_tsvec(r public.attributes) OWNER TO gutenberg;
--
-- Name: _attributes_tsvec_update(public.attributes); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public._attributes_tsvec_update(r public.attributes) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- creates tsvector from attributes record
DECLARE
tsv TSVECTOR := NULL;
BEGIN
IF r.fk_attriblist IN (240, 245, 246, 440, 505) THEN
-- title
tsv := setweight (to_tsvector (
'pg_catalog.english', pf2 ('tx', r.text)), 'B');
ELSIF r.fk_attriblist NOT IN (500, 508, 520, 540, 901, 902, 903, 904, 905) THEN
-- other attributes we want to search
tsv := setweight (to_tsvector ('pg_catalog.english', r.text), 'C');
END IF;
RETURN tsv;
END;
$$;
ALTER FUNCTION public._attributes_tsvec_update(r public.attributes) OWNER TO gutenberg;
--
-- Name: authors; Type: TABLE; Schema: public; Owner: gutenberg
--
CREATE TABLE public.authors (
pk integer DEFAULT nextval(('public.authors_pk_seq'::text)::regclass) NOT NULL,
author character varying(240) NOT NULL,
born_floor integer,
died_floor integer,
born_ceil integer,
died_ceil integer,
note text,
downloads integer DEFAULT 0 NOT NULL,
release_date date DEFAULT '1970-01-01'::date NOT NULL,
tsvec tsvector,
CONSTRAINT authors_author CHECK (((author)::text <> (''::character varying)::text))
);
ALTER TABLE public.authors OWNER TO gutenberg;
--
-- Name: _authors_tsvec(public.authors); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public._authors_tsvec(r public.authors) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- creates tsvector from author and alias records
DECLARE
tsv TSVECTOR;
r2 RECORD;
BEGIN
-- authors
tsv := setweight (px ('a', r.author), 'A');
tsv := tsv || to_tsvector ('pg_catalog.simple',
COALESCE ('' || r.born_floor, '') || COALESCE (' ' || r.died_floor, ''));
-- aliases
FOR r2 IN SELECT "alias" FROM aliases WHERE aliases.fk_authors = r.pk LOOP
tsv := tsv || setweight (px ('a', r2.alias), 'A');
END LOOP;
RETURN tsv;
END;
$$;
ALTER FUNCTION public._authors_tsvec(r public.authors) OWNER TO gutenberg;
--
-- Name: _authors_tsvec_update(public.authors); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public._authors_tsvec_update(r public.authors) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- after row trigger for alias table
-- creates full tsvector from author and alias records
DECLARE
tsv TSVECTOR;
r2 RECORD;
BEGIN
-- authors
tsv := to_tsvector ('pg_catalog.simple', pf ('a.', r.author));
tsv := tsv || to_tsvector ('pg_catalog.simple', r.author);
tsv := tsv || to_tsvector ('pg_catalog.english', r.author);
IF r.born_floor IS NOT NULL THEN
tsv := tsv || to_tsvector ('pg_catalog.simple', CAST (r.born_floor AS text));
END IF;
IF r.died_floor IS NOT NULL THEN
tsv := tsv || to_tsvector ('pg_catalog.simple', CAST (r.died_floor AS text));
END IF;
-- aliases
FOR r2 IN SELECT "alias" FROM aliases
WHERE aliases.fk_authors = r.pk
LOOP
tsv := tsv || to_tsvector ('pg_catalog.simple', pf ('a.', r2.alias));
tsv := tsv || to_tsvector ('pg_catalog.simple', r2.alias);
tsv := tsv || to_tsvector ('pg_catalog.english', r2.alias);
END LOOP;
RETURN tsv;
END;
$$;
ALTER FUNCTION public._authors_tsvec_update(r public.authors) OWNER TO gutenberg;
--
-- Name: books; Type: TABLE; Schema: public; Owner: gutenberg
--
CREATE TABLE public.books (
pk integer NOT NULL,
copyrighted integer DEFAULT 0 NOT NULL,
updatemode integer DEFAULT 0 NOT NULL,
release_date date DEFAULT ('now'::text)::date NOT NULL,
filemask character varying(240),
gutindex text,
downloads integer DEFAULT 0 NOT NULL,
title text,
tsvec tsvector,
nonfiling integer DEFAULT 0 NOT NULL
);
ALTER TABLE public.books OWNER TO gutenberg;
--
-- Name: _books_title(public.books); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public._books_title(r public.books) RETURNS record
LANGUAGE plpgsql
AS $$
-- helper
-- calculates book title from attributes table
DECLARE
r2 RECORD;
BEGIN
FOR r2 IN SELECT attributes.text, attributes.nonfiling
FROM attributes
WHERE attributes.fk_books = r.pk
AND attributes.fk_attriblist = ANY (ARRAY[240, 245, 246])
ORDER BY
CASE WHEN attributes.fk_attriblist = 245
THEN 1
ELSE attributes.fk_attriblist
END
LIMIT 1 LOOP
RETURN r2;
END LOOP;
r2.text := NULL;
r2.nonfiling := 0;
RETURN r2;
END;
$$;
ALTER FUNCTION public._books_title(r public.books) OWNER TO gutenberg;
--
-- Name: _books_tsvec(public.books); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public._books_tsvec(r public.books) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- creates tsvector for books from authors, title, subjects, bookshelves, ...
DECLARE
tsv TSVECTOR;
r2 RECORD;
BEGIN
-- ebook no.
tsv := setweight (to_tsvector ('pg_catalog.simple', pf2 ('no.', '' || r.pk)), 'A');
-- authors
FOR r2 IN SELECT tsvec FROM authors, mn_books_authors AS mn
WHERE authors.pk = mn.fk_authors AND mn.fk_books = r.pk
AND tsvec IS NOT NULL LOOP
tsv := tsv || r2.tsvec;
END LOOP;
-- attributes (title, etc.)
FOR r2 IN SELECT tsvec FROM attributes WHERE tsvec IS NOT NULL
AND fk_attriblist < 900 AND attributes.fk_books = r.pk LOOP
tsv := tsv || r2.tsvec;
END LOOP;
-- subjects
FOR r2 IN SELECT tsvec FROM subjects, mn_books_subjects AS mn
WHERE subjects.pk = mn.fk_subjects AND mn.fk_books = r.pk
AND tsvec IS NOT NULL LOOP
tsv := tsv || r2.tsvec;
END LOOP;
-- bookshelves
FOR r2 IN SELECT tsvec FROM bookshelves, mn_books_bookshelves AS mn
WHERE bookshelves.pk = mn.fk_bookshelves AND mn.fk_books = r.pk
AND tsvec IS NOT NULL LOOP
tsv := tsv || r2.tsvec;
END LOOP;
-- categories
FOR r2 IN SELECT category FROM categories, mn_books_categories AS mn
WHERE categories.pk = mn.fk_categories AND mn.fk_books = r.pk LOOP
tsv := tsv || to_tsvector ('pg_catalog.simple', pf ('cat0', r2.category));
END LOOP;
-- loccs
FOR r2 IN SELECT locc, fk_loccs FROM loccs, mn_books_loccs AS mn
WHERE loccs.pk = mn.fk_loccs AND mn.fk_books = r.pk LOOP
tsv := tsv || to_tsvector ('pg_catalog.english', pf ('lcnx', r2.locc));
tsv := tsv || to_tsvector ('pg_catalog.simple', 'lcc0' || r2.fk_loccs);
END LOOP;
-- languages
FOR r2 IN SELECT lang, fk_langs FROM langs, mn_books_langs AS mn
WHERE langs.pk = mn.fk_langs AND mn.fk_books = r.pk LOOP
tsv := tsv || to_tsvector (
'pg_catalog.simple', pf ('l0', r2.lang || ' ' || r2.fk_langs));
END LOOP;
-- filetypes
SELECT array_to_string (array_agg ('y0' || fk_filetypes), ' ') AS a INTO r2
FROM (SELECT DISTINCT fk_filetypes FROM files
WHERE diskstatus = 0 AND files.fk_books = r.pk) AS foo;
tsv := tsv || to_tsvector ('pg_catalog.simple', replace (r2.a, '-8', ''));
RETURN tsv;
END;
$$;
ALTER FUNCTION public._books_tsvec(r public.books) OWNER TO gutenberg;
SET default_with_oids = false;
--
-- Name: bookshelves; Type: TABLE; Schema: public; Owner: gutenberg
--
CREATE TABLE public.bookshelves (
pk integer NOT NULL,
bookshelf text NOT NULL,
downloads integer DEFAULT 0 NOT NULL,
release_date date DEFAULT '1970-01-01'::date NOT NULL,
tsvec tsvector,
CONSTRAINT bookshelves_bookshelf_check CHECK ((bookshelf <> ''::text))
);
ALTER TABLE public.bookshelves OWNER TO gutenberg;
--
-- Name: _bookshelves_tsvec(public.bookshelves); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public._bookshelves_tsvec(r public.bookshelves) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- creates tsvector from bookshelves record
BEGIN
RETURN px ('bs', r.bookshelf);
END;
$$;
ALTER FUNCTION public._bookshelves_tsvec(r public.bookshelves) OWNER TO gutenberg;
SET default_with_oids = true;
--
-- Name: subjects; Type: TABLE; Schema: public; Owner: gutenberg
--
CREATE TABLE public.subjects (
pk integer DEFAULT nextval(('public.subjects_pk_seq'::text)::regclass) NOT NULL,
subject character varying(240) NOT NULL,
downloads integer DEFAULT 0 NOT NULL,
release_date date DEFAULT '1970-01-01'::date NOT NULL,
tsvec tsvector,
CONSTRAINT subjects_subject CHECK (((subject)::text <> (''::character varying)::text))
);
ALTER TABLE public.subjects OWNER TO gutenberg;
--
-- Name: _subjects_tsvec(public.subjects); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public._subjects_tsvec(r public.subjects) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- creates tsvector from subjects record
BEGIN
RETURN setweight (px ('s', r.subject), 'C');
END;
$$;
ALTER FUNCTION public._subjects_tsvec(r public.subjects) OWNER TO gutenberg;
--
-- Name: attributes_tsvec(integer); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.attributes_tsvec(fk_attributes integer) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- for manual updates of attributes.tsvec
DECLARE
r attributes;
BEGIN
SELECT * INTO STRICT r FROM attributes WHERE attributes.pk = fk_attributes;
RETURN _attributes_tsvec (r);
END;
$$;
ALTER FUNCTION public.attributes_tsvec(fk_attributes integer) OWNER TO gutenberg;
--
-- Name: attributes_tsvec_triggerfunc(); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.attributes_tsvec_triggerfunc() RETURNS trigger
LANGUAGE plpgsql
AS $$
-- before row trigger for attributes table
BEGIN
IF TG_TABLE_NAME = 'attributes' THEN
NEW.tsvec := _attributes_tsvec (NEW);
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.attributes_tsvec_triggerfunc() OWNER TO gutenberg;
--
-- Name: attributes_tsvec_update(integer); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.attributes_tsvec_update(fk_attributes integer) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- for manual updates of attributes.tsvec
DECLARE
r attributes;
BEGIN
IF fk_attributes IS NULL THEN
RETURN NULL;
END IF;
SELECT * INTO r FROM attributes WHERE attributes.pk = fk_attributes;
RETURN _attributes_tsvec_update (r);
END;
$$;
ALTER FUNCTION public.attributes_tsvec_update(fk_attributes integer) OWNER TO gutenberg;
--
-- Name: authors_tsvec(integer); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.authors_tsvec(fk_authors integer) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- for manual updates of authors.tsvec
DECLARE
r authors;
BEGIN
SELECT * INTO STRICT r FROM authors WHERE authors.pk = fk_authors;
RETURN _authors_tsvec (r);
END;
$$;
ALTER FUNCTION public.authors_tsvec(fk_authors integer) OWNER TO gutenberg;
--
-- Name: authors_tsvec_alias_triggerfunc(); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.authors_tsvec_alias_triggerfunc() RETURNS trigger
LANGUAGE plpgsql
AS $$
-- after row trigger for aliases table
BEGIN
IF (TG_OP = 'DELETE') THEN
UPDATE authors SET tsvec = NULL WHERE authors.pk = OLD.fk_authors;
RETURN OLD;
ELSE
UPDATE authors SET tsvec = NULL WHERE authors.pk = NEW.fk_authors;
RETURN NEW;
END IF;
END;
$$;
ALTER FUNCTION public.authors_tsvec_alias_triggerfunc() OWNER TO gutenberg;
--
-- Name: authors_tsvec_triggerfunc(); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.authors_tsvec_triggerfunc() RETURNS trigger
LANGUAGE plpgsql
AS $$
-- before row trigger for authors table
BEGIN
IF TG_TABLE_NAME = 'authors' THEN
NEW.tsvec := _authors_tsvec (NEW);
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.authors_tsvec_triggerfunc() OWNER TO gutenberg;
--
-- Name: authors_tsvec_update(integer); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.authors_tsvec_update(fk_authors integer) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- for manual updates of authors.tsvec
-- creates tsvector for authors table from authors and aliases
DECLARE
r authors;
BEGIN
IF fk_authors IS NULL THEN
RETURN NULL;
END IF;
SELECT * INTO r FROM authors WHERE authors.pk = fk_authors;
RETURN _authors_tsvec_update (r);
END;
$$;
ALTER FUNCTION public.authors_tsvec_update(fk_authors integer) OWNER TO gutenberg;
--
-- Name: books_title_triggerfunc(); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.books_title_triggerfunc() RETURNS trigger
LANGUAGE plpgsql
AS $$
-- after row trigger for attributes table
-- updates books.title whenever attributes change
BEGIN
IF (TG_OP = 'DELETE') THEN
UPDATE books SET title = NULL WHERE books.pk = OLD.fk_books;
RETURN OLD;
ELSE
UPDATE books SET title = NULL WHERE books.pk = NEW.fk_books;
RETURN NEW;
END IF;
END;
$$;
ALTER FUNCTION public.books_title_triggerfunc() OWNER TO gutenberg;
--
-- Name: books_title_update(integer); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.books_title_update(_fk_books integer) RETURNS text
LANGUAGE sql
AS $_$
-- helper
-- calculates book title from attributes table
SELECT ltrim (substring (attributes.text FROM attributes.nonfiling))
FROM attributes
WHERE attributes.fk_books = $1
AND attributes.fk_attriblist = ANY (ARRAY[240, 245, 246])
-- take 245 first, the others if there is no 245 for this book.
ORDER BY
CASE WHEN attributes.fk_attriblist = 245
THEN 1
ELSE attributes.fk_attriblist
END
LIMIT 1;
$_$;
ALTER FUNCTION public.books_title_update(_fk_books integer) OWNER TO gutenberg;
--
-- Name: books_tsvec_1n_triggerfunc(); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.books_tsvec_1n_triggerfunc() RETURNS trigger
LANGUAGE plpgsql
AS $_$
-- after row trigger for authors, subjects, bookshelves tables
DECLARE
r RECORD;
tablename CONSTANT TEXT := TG_TABLE_NAME;
BEGIN
FOR r IN EXECUTE 'SELECT fk_books FROM mn_books_'
|| quote_ident (tablename)
|| ' AS mn WHERE mn.fk_'
|| quote_ident (tablename)
|| ' = $1' USING NEW.pk
LOOP
UPDATE books SET tsvec = NULL WHERE books.pk = r.fk_books;
END LOOP;
RETURN NULL;
END;
$_$;
ALTER FUNCTION public.books_tsvec_1n_triggerfunc() OWNER TO gutenberg;
--
-- Name: books_tsvec_mn_triggerfunc(); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.books_tsvec_mn_triggerfunc() RETURNS trigger
LANGUAGE plpgsql
AS $$
-- after row trigger for mn_books_* tables
-- table must have fk_books field
BEGIN
IF (TG_OP = 'DELETE') THEN
UPDATE books SET tsvec = NULL WHERE books.pk = OLD.fk_books;
RETURN OLD;
ELSE
UPDATE books SET tsvec = NULL WHERE books.pk = NEW.fk_books;
RETURN NEW;
END IF;
END;
$$;
ALTER FUNCTION public.books_tsvec_mn_triggerfunc() OWNER TO gutenberg;
--
-- Name: books_tsvec_triggerfunc(); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.books_tsvec_triggerfunc() RETURNS trigger
LANGUAGE plpgsql
AS $$
-- before row trigger for books table
DECLARE
r record;
BEGIN
NEW.tsvec := _books_tsvec (NEW);
r := _books_title (NEW);
NEW.title := r.text;
NEW.nonfiling = r.nonfiling;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.books_tsvec_triggerfunc() OWNER TO gutenberg;
--
-- Name: books_tsvec_update(integer); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.books_tsvec_update(fk_books integer) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- for manual updates and helper function for triggers
-- creates tsvector for books from authors, title, subjects, bookshelves, ...
DECLARE
r RECORD;
tsv TSVECTOR;
BEGIN
IF fk_books IS NULL THEN
RETURN NULL;
END IF;
-- ebook no.
tsv := setweight (to_tsvector ('pg_catalog.simple', pf2 ('no.', '' || fk_books)), 'A');
-- authors
FOR r IN SELECT tsvec FROM authors, mn_books_authors AS mn
WHERE authors.pk = mn.fk_authors AND mn.fk_books = fk_books
AND tsvec IS NOT NULL LOOP
tsv := tsv || setweight (r.tsvec, 'A');
END LOOP;
-- title
FOR r IN SELECT text FROM attributes
WHERE fk_attriblist IN (240, 245, 246, 440, 505)
AND attributes.fk_books = fk_books LOOP
tsv := tsv || setweight (to_tsvector (
'pg_catalog.english', pf2 ('tx', r.text)), 'B');
END LOOP;
-- other attributes
FOR r IN SELECT text FROM attributes
WHERE fk_attriblist < 900 AND attributes.fk_books = fk_books LOOP
tsv := tsv || setweight (to_tsvector ('pg_catalog.english', r.text), 'C');
END LOOP;
-- subjects
FOR r IN SELECT subject FROM subjects, mn_books_subjects AS mn
WHERE subjects.pk = mn.fk_subjects AND mn.fk_books = fk_books LOOP
tsv := tsv || setweight (to_tsvector (
'pg_catalog.english', pf2 ('sx', r.subject)), 'C');
END LOOP;
-- bookshelves
FOR r IN SELECT bookshelf FROM bookshelves, mn_books_bookshelves AS mn
WHERE bookshelves.pk = mn.fk_bookshelves AND mn.fk_books = fk_books LOOP
tsv := tsv || setweight (to_tsvector (
'pg_catalog.english', pf2 ('bsx', r.bookshelf)), 'D');
END LOOP;
-- categories
FOR r IN SELECT category FROM categories, mn_books_categories AS mn
WHERE categories.pk = mn.fk_categories AND mn.fk_books = fk_books LOOP
tsv := tsv || to_tsvector ('pg_catalog.english', pf ('catx', r.category));
END LOOP;
-- loccs
FOR r IN SELECT locc, fk_loccs FROM loccs, mn_books_loccs AS mn
WHERE loccs.pk = mn.fk_loccs AND mn.fk_books = fk_books LOOP
tsv := tsv || to_tsvector ('pg_catalog.english', pf ('lcx', r.locc));
tsv := tsv || to_tsvector ('pg_catalog.english', 'lc.' || r.fk_loccs);
END LOOP;
-- languages
FOR r IN SELECT lang, fk_langs FROM langs, mn_books_langs AS mn
WHERE langs.pk = mn.fk_langs AND mn.fk_books = fk_books LOOP
tsv := tsv || to_tsvector (
'pg_catalog.simple', pf ('l.', r.lang || ' ' || r.fk_langs));
END LOOP;
-- filetypes
SELECT array_agg (fk_filetypes) AS a INTO r
FROM (SELECT DISTINCT fk_filetypes FROM files
WHERE diskstatus = 0 AND files.fk_books = fk_books) AS foo;
tsv := tsv || to_tsvector ('pg_catalog.simple', 'y.' || array_to_string (r.a, ' y.'));
RETURN tsv;
END;
$$;
ALTER FUNCTION public.books_tsvec_update(fk_books integer) OWNER TO gutenberg;
--
-- Name: bookshelves_tsvec_triggerfunc(); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.bookshelves_tsvec_triggerfunc() RETURNS trigger
LANGUAGE plpgsql
AS $$
-- before row trigger for bookshelves table
BEGIN
IF TG_TABLE_NAME = 'bookshelves' THEN
NEW.tsvec := _bookshelves_tsvec (NEW);
END IF;
RETURN NEW;
END;
$$;
ALTER FUNCTION public.bookshelves_tsvec_triggerfunc() OWNER TO gutenberg;
--
-- Name: db_to_csv(text); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.db_to_csv(path text) RETURNS void
LANGUAGE plpgsql
AS $$
declare
tables RECORD;
statement TEXT;
begin
FOR tables IN
SELECT (table_schema || '.' || table_name) AS schema_table
FROM information_schema.tables t INNER JOIN information_schema.schemata s
ON s.schema_name = t.table_schema
WHERE t.table_schema NOT IN ('pg_catalog', 'information_schema', 'configuration')
AND t.table_type NOT IN ('VIEW')
ORDER BY schema_table
LOOP
statement := 'COPY ' || tables.schema_table || ' TO ''' || path || '/' || tables.schema_table || '.csv' ||''' DELIMITER '';'' CSV HEADER';
EXECUTE statement;
END LOOP;
return;
end;
$$;
ALTER FUNCTION public.db_to_csv(path text) OWNER TO gutenberg;
--
-- Name: filing(text, integer); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.filing(title text, nonfiling integer) RETURNS text
LANGUAGE plpgsql IMMUTABLE
AS $$
-- function to sort titles in filing order
BEGIN
RETURN ltrim (substring (title FROM nonfiling + 1));
END;
$$;
ALTER FUNCTION public.filing(title text, nonfiling integer) OWNER TO gutenberg;
--
-- Name: gin_extract_trgm(text, internal); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gin_extract_trgm(text, internal) RETURNS internal
LANGUAGE c IMMUTABLE STRICT
AS '$libdir/pg_trgm', 'gin_extract_trgm';
ALTER FUNCTION public.gin_extract_trgm(text, internal) OWNER TO postgres;
--
-- Name: gin_extract_trgm(text, internal, smallint, internal, internal); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gin_extract_trgm(text, internal, smallint, internal, internal) RETURNS internal
LANGUAGE c IMMUTABLE STRICT
AS '$libdir/pg_trgm', 'gin_extract_trgm';
ALTER FUNCTION public.gin_extract_trgm(text, internal, smallint, internal, internal) OWNER TO postgres;
--
-- Name: gin_trgm_consistent(internal, smallint, text, integer, internal, internal); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gin_trgm_consistent(internal, smallint, text, integer, internal, internal) RETURNS boolean
LANGUAGE c IMMUTABLE STRICT
AS '$libdir/pg_trgm', 'gin_trgm_consistent';
ALTER FUNCTION public.gin_trgm_consistent(internal, smallint, text, integer, internal, internal) OWNER TO postgres;
--
-- Name: gtrgm_compress(internal); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gtrgm_compress(internal) RETURNS internal
LANGUAGE c IMMUTABLE STRICT
AS '$libdir/pg_trgm', 'gtrgm_compress';
ALTER FUNCTION public.gtrgm_compress(internal) OWNER TO postgres;
--
-- Name: gtrgm_consistent(internal, text, integer, oid, internal); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gtrgm_consistent(internal, text, integer, oid, internal) RETURNS boolean
LANGUAGE c IMMUTABLE STRICT
AS '$libdir/pg_trgm', 'gtrgm_consistent';
ALTER FUNCTION public.gtrgm_consistent(internal, text, integer, oid, internal) OWNER TO postgres;
--
-- Name: gtrgm_decompress(internal); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gtrgm_decompress(internal) RETURNS internal
LANGUAGE c IMMUTABLE STRICT
AS '$libdir/pg_trgm', 'gtrgm_decompress';
ALTER FUNCTION public.gtrgm_decompress(internal) OWNER TO postgres;
--
-- Name: gtrgm_penalty(internal, internal, internal); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gtrgm_penalty(internal, internal, internal) RETURNS internal
LANGUAGE c IMMUTABLE STRICT
AS '$libdir/pg_trgm', 'gtrgm_penalty';
ALTER FUNCTION public.gtrgm_penalty(internal, internal, internal) OWNER TO postgres;
--
-- Name: gtrgm_picksplit(internal, internal); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gtrgm_picksplit(internal, internal) RETURNS internal
LANGUAGE c IMMUTABLE STRICT
AS '$libdir/pg_trgm', 'gtrgm_picksplit';
ALTER FUNCTION public.gtrgm_picksplit(internal, internal) OWNER TO postgres;
--
-- Name: gtrgm_same(public.gtrgm, public.gtrgm, internal); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gtrgm_same(public.gtrgm, public.gtrgm, internal) RETURNS internal
LANGUAGE c IMMUTABLE STRICT
AS '$libdir/pg_trgm', 'gtrgm_same';
ALTER FUNCTION public.gtrgm_same(public.gtrgm, public.gtrgm, internal) OWNER TO postgres;
--
-- Name: gtrgm_union(bytea, internal); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.gtrgm_union(bytea, internal) RETURNS integer[]
LANGUAGE c IMMUTABLE STRICT
AS '$libdir/pg_trgm', 'gtrgm_union';
ALTER FUNCTION public.gtrgm_union(bytea, internal) OWNER TO postgres;
--
-- Name: pf(text, text); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.pf(p text, t text) RETURNS text
LANGUAGE plpgsql
AS $$
-- returns string with all words prefixed by p
BEGIN
RETURN regexp_replace (t, E'\\m', p, 'g');
END;
$$;
ALTER FUNCTION public.pf(p text, t text) OWNER TO gutenberg;
--
-- Name: pf2(text, text); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.pf2(p text, t text) RETURNS text
LANGUAGE plpgsql
AS $$
-- returns original string plus string with all words prefixed by p
BEGIN
RETURN t || ' ' || regexp_replace (t, E'\\m', p, 'g');
END;
$$;
ALTER FUNCTION public.pf2(p text, t text) OWNER TO gutenberg;
--
-- Name: px(text, text); Type: FUNCTION; Schema: public; Owner: gutenberg
--
CREATE FUNCTION public.px(p text, t text) RETURNS tsvector
LANGUAGE plpgsql
AS $$
-- returns original string plus string with all words prefixed by p. and px
DECLARE
r RECORD;
s1 TEXT := '';
s2 TEXT := '';
-- regex to fix '1921-1968' which would otherwise be parsed as '1921', '-1968'
t2 TEXT := regexp_replace (t, E'([\\d])-([\\d])', E'\\1_\\2');