]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/011.schema.authority.sql
Fix two bugs:
[working/Evergreen.git] / Open-ILS / src / sql / Pg / 011.schema.authority.sql
1 /*
2  * Copyright (C) 2004-2008  Georgia Public Library Service
3  * Copyright (C) 2008  Equinox Software, Inc.
4  * Copyright (C) 2010  Laurentian University
5  * Mike Rylander <miker@esilibrary.com> 
6  * Dan Scott <dscott@laurentian.ca>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 DROP SCHEMA IF EXISTS authority CASCADE;
21
22 BEGIN;
23 CREATE SCHEMA authority;
24
25 CREATE TABLE authority.record_entry (
26         id              BIGSERIAL       PRIMARY KEY,
27         creator         INT             NOT NULL DEFAULT 1,
28         editor          INT             NOT NULL DEFAULT 1,
29         create_date     TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT now(),
30         edit_date       TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT now(),
31         active          BOOL            NOT NULL DEFAULT TRUE,
32         deleted         BOOL            NOT NULL DEFAULT FALSE,
33         source          INT,
34         marc            TEXT            NOT NULL,
35         last_xact_id    TEXT            NOT NULL,
36         owner           INT
37 );
38 CREATE INDEX authority_record_entry_creator_idx ON authority.record_entry ( creator );
39 CREATE INDEX authority_record_entry_editor_idx ON authority.record_entry ( editor );
40 CREATE TRIGGER a_marcxml_is_well_formed BEFORE INSERT OR UPDATE ON authority.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.check_marcxml_well_formed();
41 CREATE TRIGGER b_maintain_901 BEFORE INSERT OR UPDATE ON authority.record_entry FOR EACH ROW EXECUTE PROCEDURE maintain_901();
42 CREATE TRIGGER c_maintain_control_numbers BEFORE INSERT OR UPDATE ON authority.record_entry FOR EACH ROW EXECUTE PROCEDURE maintain_control_numbers();
43 CREATE RULE protect_authority_rec_delete AS ON DELETE TO authority.record_entry DO INSTEAD (UPDATE authority.record_entry SET deleted = TRUE WHERE OLD.id = authority.record_entry.id);
44
45 CREATE TABLE authority.bib_linking (
46     id          BIGSERIAL   PRIMARY KEY,
47     bib         BIGINT      NOT NULL REFERENCES biblio.record_entry (id),
48     authority   BIGINT      NOT NULL REFERENCES authority.record_entry (id)
49 );
50 CREATE INDEX authority_bl_bib_idx ON authority.bib_linking ( bib );
51 CREATE UNIQUE INDEX authority_bl_bib_authority_once_idx ON authority.bib_linking ( authority, bib );
52
53 CREATE TABLE authority.record_note (
54         id              BIGSERIAL       PRIMARY KEY,
55         record          BIGINT          NOT NULL REFERENCES authority.record_entry (id) DEFERRABLE INITIALLY DEFERRED,
56         value           TEXT            NOT NULL,
57         creator         INT             NOT NULL DEFAULT 1,
58         editor          INT             NOT NULL DEFAULT 1,
59         create_date     TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT now(),
60         edit_date       TIMESTAMP WITH TIME ZONE        NOT NULL DEFAULT now()
61 );
62 CREATE INDEX authority_record_note_record_idx ON authority.record_note ( record );
63 CREATE INDEX authority_record_note_creator_idx ON authority.record_note ( creator );
64 CREATE INDEX authority_record_note_editor_idx ON authority.record_note ( editor );
65
66 CREATE TABLE authority.rec_descriptor (
67         id              BIGSERIAL PRIMARY KEY,
68         record          BIGINT,
69         record_status   TEXT,
70         char_encoding   TEXT
71 );
72 CREATE INDEX authority_rec_descriptor_record_idx ON authority.rec_descriptor (record);
73
74 CREATE TABLE authority.full_rec (
75         id              BIGSERIAL       PRIMARY KEY,
76         record          BIGINT          NOT NULL,
77         tag             CHAR(3)         NOT NULL,
78         ind1            TEXT,
79         ind2            TEXT,
80         subfield        TEXT,
81         value           TEXT            NOT NULL,
82         index_vector    tsvector        NOT NULL
83 );
84 CREATE INDEX authority_full_rec_record_idx ON authority.full_rec (record);
85 CREATE INDEX authority_full_rec_tag_subfield_idx ON authority.full_rec (tag, subfield);
86 CREATE INDEX authority_full_rec_tag_part_idx ON authority.full_rec (SUBSTRING(tag FROM 2));
87 CREATE TRIGGER authority_full_rec_fti_trigger
88         BEFORE UPDATE OR INSERT ON authority.full_rec
89         FOR EACH ROW EXECUTE PROCEDURE tsearch2(index_vector, value);
90
91 CREATE INDEX authority_full_rec_index_vector_idx ON authority.full_rec USING GIST (index_vector);
92 /* Enable LIKE to use an index for database clusters with locales other than C or POSIX */
93 CREATE INDEX authority_full_rec_value_tpo_index ON authority.full_rec (value text_pattern_ops);
94
95 CREATE OR REPLACE VIEW authority.tracing_links AS
96         SELECT  main.record AS record,
97                 main.id AS main_id,
98                 main.tag AS main_tag,
99                 main.value AS main_value,
100                 substr(link.value,1,1) AS relationship,
101                 substr(link.value,2,1) AS use_restriction,
102                 substr(link.value,3,1) AS deprecation,
103                 substr(link.value,4,1) AS display_restriction,
104                 link_value.id AS link_id,
105                 link_value.tag AS link_tag,
106                 link_value.value AS link_value
107           FROM  authority.full_rec main
108                 JOIN authority.full_rec link
109                         ON (    link.record = main.record
110                                 AND link.tag in ((main.tag::int + 400)::text, (main.tag::int + 300)::text)
111                                 AND link.subfield = 'w' )
112                 JOIN authority.full_rec link_value
113                         ON (    link_value.record = main.record
114                                 AND link_value.tag = link.tag
115                                 AND link_value.subfield = 'a' )
116           WHERE main.tag IN ('100','110','111','130','150','151','155','180','181','182','185')
117                 AND main.subfield = 'a';
118
119 -- Function to generate an ephemeral overlay template from an authority record
120 CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( TEXT, BIGINT ) RETURNS TEXT AS $func$
121
122     use MARC::Record;
123     use MARC::File::XML (BinaryEncoding => 'UTF-8');
124
125     my $xml = shift;
126     my $r = MARC::Record->new_from_xml( $xml );
127
128     return undef unless ($r);
129
130     my $id = shift() || $r->subfield( '901' => 'c' );
131     $id =~ s/^\s*(?:\([^)]+\))?\s*(.+)\s*?$/$1/;
132     return undef unless ($id); # We need an ID!
133
134     my $tmpl = MARC::Record->new();
135     $tmpl->encoding( 'UTF-8' );
136
137     my @rule_fields;
138     for my $field ( $r->field( '1..' ) ) { # Get main entry fields from the authority record
139
140         my $tag = $field->tag;
141         my $i1 = $field->indicator(1);
142         my $i2 = $field->indicator(2);
143         my $sf = join '', map { $_->[0] } $field->subfields;
144         my @data = map { @$_ } $field->subfields;
145
146         my @replace_them;
147
148         # Map the authority field to bib fields it can control.
149         if ($tag >= 100 and $tag <= 111) {       # names
150             @replace_them = map { $tag + $_ } (0, 300, 500, 600, 700);
151         } elsif ($tag eq '130') {                # uniform title
152             @replace_them = qw/130 240 440 730 830/;
153         } elsif ($tag >= 150 and $tag <= 155) {  # subjects
154             @replace_them = ($tag + 500);
155         } elsif ($tag >= 180 and $tag <= 185) {  # floating subdivisions
156             @replace_them = qw/100 400 600 700 800 110 410 610 710 810 111 411 611 711 811 130 240 440 730 830 650 651 655/;
157         } else {
158             next;
159         }
160
161         # Dummy up the bib-side data
162         $tmpl->append_fields(
163             map {
164                 MARC::Field->new( $_, $i1, $i2, @data )
165             } @replace_them
166         );
167
168         # Construct some 'replace' rules
169         push @rule_fields, map { $_ . $sf . '[0~\)' .$id . '$]' } @replace_them;
170     }
171
172     # Insert the replace rules into the template
173     $tmpl->append_fields(
174         MARC::Field->new( '905' => ' ' => ' ' => 'r' => join(',', @rule_fields ) )
175     );
176
177     $xml = $tmpl->as_xml_record;
178     $xml =~ s/^<\?.+?\?>$//mo;
179     $xml =~ s/\n//sgo;
180     $xml =~ s/>\s+</></sgo;
181
182     return $xml;
183
184 $func$ LANGUAGE PLPERLU;
185
186 CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( BIGINT ) RETURNS TEXT AS $func$
187     SELECT authority.generate_overlay_template( marc, id ) FROM authority.record_entry WHERE id = $1;
188 $func$ LANGUAGE SQL;
189
190 CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( TEXT ) RETURNS TEXT AS $func$
191     SELECT authority.generate_overlay_template( $1, NULL );
192 $func$ LANGUAGE SQL;
193
194 CREATE OR REPLACE FUNCTION authority.merge_records ( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
195 DECLARE
196     moved_objects INT := 0;
197     bib_id        INT := 0;
198     bib_rec       biblio.record_entry%ROWTYPE;
199     auth_link     authority.bib_linking%ROWTYPE;
200     ingest_same   boolean;
201 BEGIN
202
203     -- 1. Update all bib records with the ID from target_record in their $0
204     FOR bib_rec IN SELECT bre.* FROM biblio.record_entry bre 
205       INNER JOIN authority.bib_linking abl ON abl.bib = bre.id
206       WHERE abl.authority = source_record LOOP
207
208         UPDATE biblio.record_entry
209           SET marc = REGEXP_REPLACE(marc, 
210             E'(<subfield\\s+code="0"\\s*>[^<]*?\\))' || source_record || '<',
211             E'\\1' || target_record || '<', 'g')
212           WHERE id = bib_rec.id;
213
214           moved_objects := moved_objects + 1;
215     END LOOP;
216
217     -- 2. Grab the current value of reingest on same MARC flag
218     SELECT enabled INTO ingest_same
219       FROM config.internal_flag
220       WHERE name = 'ingest.reingest.force_on_same_marc'
221     ;
222
223     -- 3. Temporarily set reingest on same to TRUE
224     UPDATE config.internal_flag
225       SET enabled = TRUE
226       WHERE name = 'ingest.reingest.force_on_same_marc'
227     ;
228
229     -- 4. Make a harmless update to target_record to trigger auto-update
230     --    in linked bibliographic records
231     UPDATE authority.record_entry
232       SET deleted = FALSE
233       WHERE id = source_record;
234
235     -- 5. "Delete" source_record
236     DELETE FROM authority.record_entry
237       WHERE id = source_record;
238
239     -- 6. Set "reingest on same MARC" flag back to initial value
240     UPDATE config.internal_flag
241       SET enabled = ingest_same
242       WHERE name = 'ingest.reingest.force_on_same_marc'
243     ;
244
245     RETURN moved_objects;
246 END;
247 $func$ LANGUAGE plpgsql;
248
249 COMMIT;