1 DROP SCHEMA vandelay CASCADE;
5 CREATE SCHEMA vandelay;
7 CREATE TABLE vandelay.queue (
8 id BIGSERIAL PRIMARY KEY,
9 owner INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
11 complete BOOL NOT NULL DEFAULT FALSE,
12 queue_type TEXT NOT NULL DEFAULT 'bib' CHECK (queue_type IN ('bib','authority')),
13 CONSTRAINT vand_queue_name_once_per_owner_const UNIQUE (owner,name,queue_type)
16 CREATE TABLE vandelay.queued_record (
17 id BIGSERIAL PRIMARY KEY,
18 create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
19 import_time TIMESTAMP WITH TIME ZONE,
20 purpose TEXT NOT NULL DEFAULT 'import' CHECK (purpose IN ('import','overlay')),
26 /* Bib stuff at the top */
27 ----------------------------------------------------
29 CREATE TABLE vandelay.bib_attr_definition (
30 id SERIAL PRIMARY KEY,
31 code TEXT UNIQUE NOT NULL,
34 remove TEXT NOT NULL DEFAULT '',
35 ident BOOL NOT NULL DEFAULT FALSE
38 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath ) VALUES ('title','Title of work','//*[@tag="245"]/*[contains("abcmnopr",@code)]');
39 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath ) VALUES ('author','Author of work','//*[@tag="100" or @tag="110" or @tag="113"]/*[contains("ad",@code)]');
40 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath ) VALUES ('language','Lanuage of work','//*[@tag="240"]/*[@code="l"][1]');
41 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath ) VALUES ('pagination','Pagination','//*[@tag="300"]/*[@code="a"][1]');
42 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath, ident, remove ) VALUES ('isbn','ISBN','//*[@tag="020"]/*[@code="a"]', TRUE, $r$(?:-|\s.+$)$r$);
43 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath, ident, remove ) VALUES ('issn','ISSN','//*[@tag="022"]/*[@code="a"]', TRUE, $r$(?:-|\s.+$)$r$);
44 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath ) VALUES ('price','Price','//*[@tag="020" or @tag="022"]/*[@code="c"][1]');
45 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath, ident ) VALUES ('rec_identifier','Accession Number','//*[@tag="001"]', TRUE);
46 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath, ident ) VALUES ('eg_tcn','TCN Value','//*[@tag="901"]/*[@code="a"]', TRUE);
47 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath, ident ) VALUES ('eg_tcn_source','TCN Source','//*[@tag="901"]/*[@code="b"]', TRUE);
48 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath, ident ) VALUES ('eg_identifier','Internal ID','//*[@tag="901"]/*[@code="c"]', TRUE);
49 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath ) VALUES ('publisher','Publisher','//*[@tag="260"]/*[@code="b"][1]');
50 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath, remove ) VALUES ('pubdate','Publication Date','//*[@tag="260"]/*[@code="c"][1]',$r$\D$r$);
51 INSERT INTO vandelay.bib_attr_definition ( code, description, xpath ) VALUES ('edition','Edition','//*[@tag="250"]/*[@code="a"][1]');
54 -- Each TEXT field (other than 'name') should hold an XPath predicate for pulling the data needed
55 -- DROP TABLE vandelay.import_item_attr_definition CASCADE;
56 CREATE TABLE vandelay.import_item_attr_definition (
57 id BIGSERIAL PRIMARY KEY,
58 owner INT NOT NULL REFERENCES actor.org_unit (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
61 keep BOOL NOT NULL DEFAULT FALSE,
83 CONSTRAINT vand_import_item_attr_def_idx UNIQUE (owner,name)
86 INSERT INTO vandelay.import_item_attr_definition (
87 owner, name, tag, owning_lib, circ_lib, location,
88 call_number, circ_modifier, barcode, price, copy_number,
89 circulate, ref, holdable, opac_visible, status
92 'Evergreen 852 export format',
102 '[@code = "x" and text() = "ncirculating"]',
103 '[@code = "x" and text() = "reference"]',
104 '[@code = "x" and text() = "holdable"]',
105 '[@code = "x" and text() = "visible"]',
109 INSERT INTO vandelay.import_item_attr_definition (
122 'Unicorn Import format -- 999',
133 CREATE TABLE vandelay.bib_queue (
134 queue_type TEXT NOT NULL DEFAULT 'bib' CHECK (queue_type = 'bib'),
135 item_attr_def TEXT REFERENCES vandelay.import_item_attr_definition (id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
136 CONSTRAINT vand_bib_queue_name_once_per_owner_const UNIQUE (owner,name,queue_type)
137 ) INHERITS (vandelay.queue);
138 ALTER TABLE vandelay.bib_queue ADD PRIMARY KEY (id);
140 CREATE TABLE vandelay.queued_bib_record (
141 queue INT NOT NULL REFERENCES vandelay.bib_queue (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
142 bib_source INT REFERENCES config.bib_source (id) DEFERRABLE INITIALLY DEFERRED,
143 imported_as INT REFERENCES biblio.record_entry (id) DEFERRABLE INITIALLY DEFERRED
144 ) INHERITS (vandelay.queued_record);
145 ALTER TABLE vandelay.queued_bib_record ADD PRIMARY KEY (id);
147 CREATE TABLE vandelay.queued_bib_record_attr (
148 id BIGSERIAL PRIMARY KEY,
149 record BIGINT NOT NULL REFERENCES vandelay.queued_bib_record (id) DEFERRABLE INITIALLY DEFERRED,
150 field INT NOT NULL REFERENCES vandelay.bib_attr_definition (id) DEFERRABLE INITIALLY DEFERRED,
151 attr_value TEXT NOT NULL
154 CREATE TABLE vandelay.bib_match (
155 id BIGSERIAL PRIMARY KEY,
156 field_type TEXT NOT NULL CHECK (field_type in ('isbn','tcn_value','id')),
157 matched_attr INT REFERENCES vandelay.queued_bib_record_attr (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
158 queued_record BIGINT REFERENCES vandelay.queued_bib_record (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
159 eg_record BIGINT REFERENCES biblio.record_entry (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
162 -- DROP TABLE vandelay.import_item CASCADE;
163 CREATE TABLE vandelay.import_item (
164 id BIGSERIAL PRIMARY KEY,
165 record BIGINT NOT NULL REFERENCES vandelay.queued_bib_record (id) ON DELETE CASCADE,
166 definition BIGINT NOT NULL REFERENCES vandelay.import_item_attr_definition (id) ON DELETE CASCADE,
175 deposit_amount NUMERIC(8,2),
188 CREATE TABLE vandelay.import_bib_trash_fields (
189 id BIGSERIAL PRIMARY KEY,
190 owner INT NOT NULL REFERENCES actor.org_unit (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
192 CONSTRAINT vand_import_bib_trash_fields_idx UNIQUE (owner,field)
195 CREATE OR REPLACE FUNCTION vandelay.strip_field ( xml TEXT, field TEXT ) RETURNS TEXT AS $_$
201 my $field_spec = shift;
203 my $r = MARC::Record->new_from_xml( $xml );
204 $r->delete_field( $_ ) for ( $r->field( $field_spec ) );
206 $xml = $r->as_xml_record;
207 $xml =~ s/^<\?.+?\?>$//mo;
209 $xml =~ s/>\s+</></sgo;
213 $_$ LANGUAGE PLPERLU;
216 CREATE OR REPLACE FUNCTION vandelay.ingest_items ( import_id BIGINT, attr_def_id BIGINT ) RETURNS SETOF vandelay.import_item AS $$
241 attr_set vandelay.import_item%ROWTYPE;
247 SELECT * INTO attr_def FROM vandelay.import_item_attr_definition WHERE id = attr_def_id;
251 attr_set.definition := attr_def.id;
253 -- Build the combined XPath
257 WHEN attr_def.owning_lib IS NULL THEN 'null()'
258 WHEN LENGTH( attr_def.owning_lib ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.owning_lib || '"]'
259 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.owning_lib
264 WHEN attr_def.circ_lib IS NULL THEN 'null()'
265 WHEN LENGTH( attr_def.circ_lib ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.circ_lib || '"]'
266 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.circ_lib
271 WHEN attr_def.call_number IS NULL THEN 'null()'
272 WHEN LENGTH( attr_def.call_number ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.call_number || '"]'
273 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.call_number
278 WHEN attr_def.copy_number IS NULL THEN 'null()'
279 WHEN LENGTH( attr_def.copy_number ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.copy_number || '"]'
280 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.copy_number
285 WHEN attr_def.status IS NULL THEN 'null()'
286 WHEN LENGTH( attr_def.status ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.status || '"]'
287 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.status
292 WHEN attr_def.location IS NULL THEN 'null()'
293 WHEN LENGTH( attr_def.location ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.location || '"]'
294 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.location
299 WHEN attr_def.circulate IS NULL THEN 'null()'
300 WHEN LENGTH( attr_def.circulate ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.circulate || '"]'
301 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.circulate
306 WHEN attr_def.deposit IS NULL THEN 'null()'
307 WHEN LENGTH( attr_def.deposit ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.deposit || '"]'
308 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.deposit
313 WHEN attr_def.deposit_amount IS NULL THEN 'null()'
314 WHEN LENGTH( attr_def.deposit_amount ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.deposit_amount || '"]'
315 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.deposit_amount
320 WHEN attr_def.ref IS NULL THEN 'null()'
321 WHEN LENGTH( attr_def.ref ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.ref || '"]'
322 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.ref
327 WHEN attr_def.holdable IS NULL THEN 'null()'
328 WHEN LENGTH( attr_def.holdable ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.holdable || '"]'
329 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.holdable
334 WHEN attr_def.price IS NULL THEN 'null()'
335 WHEN LENGTH( attr_def.price ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.price || '"]'
336 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.price
341 WHEN attr_def.barcode IS NULL THEN 'null()'
342 WHEN LENGTH( attr_def.barcode ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.barcode || '"]'
343 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.barcode
348 WHEN attr_def.circ_modifier IS NULL THEN 'null()'
349 WHEN LENGTH( attr_def.circ_modifier ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.circ_modifier || '"]'
350 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.circ_modifier
355 WHEN attr_def.circ_as_type IS NULL THEN 'null()'
356 WHEN LENGTH( attr_def.circ_as_type ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.circ_as_type || '"]'
357 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.circ_as_type
362 WHEN attr_def.alert_message IS NULL THEN 'null()'
363 WHEN LENGTH( attr_def.alert_message ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.alert_message || '"]'
364 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.alert_message
369 WHEN attr_def.opac_visible IS NULL THEN 'null()'
370 WHEN LENGTH( attr_def.opac_visible ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.opac_visible || '"]'
371 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.opac_visible
376 WHEN attr_def.pub_note IS NULL THEN 'null()'
377 WHEN LENGTH( attr_def.pub_note ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.pub_note || '"]'
378 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.pub_note
382 WHEN attr_def.priv_note IS NULL THEN 'null()'
383 WHEN LENGTH( attr_def.priv_note ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.priv_note || '"]'
384 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.priv_note
391 call_number || '|' ||
392 copy_number || '|' ||
397 deposit_amount || '|' ||
402 circ_modifier || '|' ||
403 circ_as_type || '|' ||
404 alert_message || '|' ||
409 -- RAISE NOTICE 'XPath: %', xpath;
413 FROM xpath_table( 'id', 'marc', 'vandelay.queued_bib_record', xpath, 'id = ' || import_id )
414 AS t( id BIGINT, ol TEXT, clib TEXT, cn TEXT, cnum TEXT, cs TEXT, cl TEXT, circ TEXT,
415 dep TEXT, dep_amount TEXT, r TEXT, hold TEXT, pr TEXT, bc TEXT, circ_mod TEXT,
416 circ_as TEXT, amessage TEXT, note TEXT, pnote TEXT, opac_vis TEXT )
419 tmp_attr_set.pr = REGEXP_REPLACE(tmp_attr_set.pr, E'[^0-9\\.]', '', 'g');
420 tmp_attr_set.dep_amount = REGEXP_REPLACE(tmp_attr_set.dep_amount, E'[^0-9\\.]', '', 'g');
422 tmp_attr_set.pr := NULLIF( tmp_attr_set.pr, '' );
423 tmp_attr_set.dep_amount := NULLIF( tmp_attr_set.dep_amount, '' );
425 SELECT id INTO attr_set.owning_lib FROM actor.org_unit WHERE shortname = UPPER(tmp_attr_set.ol); -- INT
426 SELECT id INTO attr_set.circ_lib FROM actor.org_unit WHERE shortname = UPPER(tmp_attr_set.clib); -- INT
427 SELECT id INTO attr_set.status FROM config.copy_status WHERE LOWER(name) = LOWER(tmp_attr_set.cs); -- INT
429 SELECT id INTO attr_set.location
430 FROM asset.copy_location
431 WHERE LOWER(name) = LOWER(tmp_attr_set.cl)
432 AND owning_lib = COALESCE(attr_set.owning_lib, attr_set.circ_lib); -- INT
434 attr_set.circulate :=
435 LOWER( SUBSTRING( tmp_attr_set.circ, 1, 1)) IN ('t','y','1')
436 OR LOWER(tmp_attr_set.circ) = 'circulating'; -- BOOL
439 LOWER( SUBSTRING( tmp_attr_set.dep, 1, 1 ) ) IN ('t','y','1')
440 OR LOWER(tmp_attr_set.dep) = 'deposit'; -- BOOL
443 LOWER( SUBSTRING( tmp_attr_set.hold, 1, 1 ) ) IN ('t','y','1')
444 OR LOWER(tmp_attr_set.hold) = 'holdable'; -- BOOL
446 attr_set.opac_visible :=
447 LOWER( SUBSTRING( tmp_attr_set.opac_vis, 1, 1 ) ) IN ('t','y','1')
448 OR LOWER(tmp_attr_set.opac_vis) = 'visible'; -- BOOL
451 LOWER( SUBSTRING( tmp_attr_set.r, 1, 1 ) ) IN ('t','y','1')
452 OR LOWER(tmp_attr_set.r) = 'reference'; -- BOOL
454 attr_set.copy_number := tmp_attr_set.cnum::INT; -- INT,
455 attr_set.deposit_amount := tmp_attr_set.dep_amount::NUMERIC(6,2); -- NUMERIC(6,2),
456 attr_set.price := tmp_attr_set.pr::NUMERIC(8,2); -- NUMERIC(8,2),
458 attr_set.call_number := tmp_attr_set.cn; -- TEXT
459 attr_set.barcode := tmp_attr_set.bc; -- TEXT,
460 attr_set.circ_modifier := tmp_attr_set.circ_mod; -- TEXT,
461 attr_set.circ_as_type := tmp_attr_set.circ_as; -- TEXT,
462 attr_set.alert_message := tmp_attr_set.amessage; -- TEXT,
463 attr_set.pub_note := tmp_attr_set.note; -- TEXT,
464 attr_set.priv_note := tmp_attr_set.pnote; -- TEXT,
465 attr_set.alert_message := tmp_attr_set.amessage; -- TEXT,
467 RETURN NEXT attr_set;
477 CREATE OR REPLACE FUNCTION vandelay.ingest_bib_marc ( ) RETURNS TRIGGER AS $$
483 FOR adef IN SELECT * FROM vandelay.bib_attr_definition LOOP
485 SELECT extract_marc_field('vandelay.queued_bib_record', id, adef.xpath, adef.remove) INTO value FROM vandelay.queued_bib_record WHERE id = NEW.id;
486 IF (value IS NOT NULL AND value <> '') THEN
487 INSERT INTO vandelay.queued_bib_record_attr (record, field, attr_value) VALUES (NEW.id, adef.id, value);
496 CREATE OR REPLACE FUNCTION vandelay.ingest_bib_items ( ) RETURNS TRIGGER AS $func$
500 item_data vandelay.import_item%ROWTYPE;
503 SELECT * INTO queue_rec FROM vandelay.bib_queue WHERE id = NEW.queue;
505 FOR item_rule IN SELECT r.* FROM actor.org_unit_ancestors( queue_rec.owner ) o JOIN vandelay.import_item_attr_definition r ON ( r.owner = o.id ) LOOP
506 FOR item_data IN SELECT * FROM vandelay.ingest_items( NEW.id::BIGINT, item_rule.id::BIGINT ) LOOP
507 INSERT INTO vandelay.import_item (
531 item_data.definition,
532 item_data.owning_lib,
534 item_data.call_number,
535 item_data.copy_number,
540 item_data.deposit_amount,
545 item_data.circ_modifier,
546 item_data.circ_as_type,
547 item_data.alert_message,
550 item_data.opac_visible
557 $func$ LANGUAGE PLPGSQL;
559 CREATE OR REPLACE FUNCTION vandelay.match_bib_record ( ) RETURNS TRIGGER AS $func$
564 FOR attr IN SELECT a.* FROM vandelay.queued_bib_record_attr a JOIN vandelay.bib_attr_definition d ON (d.id = a.field) WHERE record = NEW.id AND d.ident IS TRUE LOOP
566 -- All numbers? check for an id match
567 IF (attr.attr_value ~ $r$^\d+$$r$) THEN
568 FOR eg_rec IN SELECT * FROM biblio.record_entry WHERE id = attr.attr_value::BIGINT AND deleted IS FALSE LOOP
569 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('id', attr.id, NEW.id, eg_rec.id);
573 -- Looks like an ISBN? check for an isbn match
574 IF (attr.attr_value ~* $r$^[0-9x]+$$r$ AND character_length(attr.attr_value) IN (10,13)) THEN
575 FOR eg_rec IN EXECUTE $$SELECT * FROM metabib.full_rec fr WHERE fr.value LIKE LOWER('$$ || attr.attr_value || $$%') AND fr.tag = '020' AND fr.subfield = 'a'$$ LOOP
576 PERFORM id FROM biblio.record_entry WHERE id = eg_rec.record AND deleted IS FALSE;
578 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('isbn', attr.id, NEW.id, eg_rec.record);
582 -- subcheck for isbn-as-tcn
583 FOR eg_rec IN SELECT * FROM biblio.record_entry WHERE tcn_value = 'i' || attr.attr_value AND deleted IS FALSE LOOP
584 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('tcn_value', attr.id, NEW.id, eg_rec.id);
588 -- check for an OCLC tcn_value match
589 IF (attr.attr_value ~ $r$^o\d+$$r$) THEN
590 FOR eg_rec IN SELECT * FROM biblio.record_entry WHERE tcn_value = regexp_replace(attr.attr_value,'^o','ocm') AND deleted IS FALSE LOOP
591 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('tcn_value', attr.id, NEW.id, eg_rec.id);
595 -- check for a direct tcn_value match
596 FOR eg_rec IN SELECT * FROM biblio.record_entry WHERE tcn_value = attr.attr_value AND deleted IS FALSE LOOP
597 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('tcn_value', attr.id, NEW.id, eg_rec.id);
604 $func$ LANGUAGE PLPGSQL;
606 CREATE OR REPLACE FUNCTION vandelay.cleanup_bib_marc ( ) RETURNS TRIGGER AS $$
608 DELETE FROM vandelay.queued_bib_record_attr WHERE record = OLD.id;
609 DELETE FROM vandelay.import_item WHERE record = OLD.id;
611 IF TG_OP = 'UPDATE' THEN
618 CREATE TRIGGER cleanup_bib_trigger
619 BEFORE UPDATE OR DELETE ON vandelay.queued_bib_record
620 FOR EACH ROW EXECUTE PROCEDURE vandelay.cleanup_bib_marc();
622 CREATE TRIGGER ingest_bib_trigger
623 AFTER INSERT OR UPDATE ON vandelay.queued_bib_record
624 FOR EACH ROW EXECUTE PROCEDURE vandelay.ingest_bib_marc();
626 CREATE TRIGGER ingest_item_trigger
627 AFTER INSERT OR UPDATE ON vandelay.queued_bib_record
628 FOR EACH ROW EXECUTE PROCEDURE vandelay.ingest_bib_items();
630 CREATE TRIGGER zz_match_bibs_trigger
631 AFTER INSERT OR UPDATE ON vandelay.queued_bib_record
632 FOR EACH ROW EXECUTE PROCEDURE vandelay.match_bib_record();
635 /* Authority stuff down here */
636 ---------------------------------------
637 CREATE TABLE vandelay.authority_attr_definition (
638 id SERIAL PRIMARY KEY,
639 code TEXT UNIQUE NOT NULL,
642 remove TEXT NOT NULL DEFAULT '',
643 ident BOOL NOT NULL DEFAULT FALSE
645 INSERT INTO vandelay.authority_attr_definition ( code, description, xpath, ident ) VALUES ('rec_identifier','Identifier','//*[@tag="001"]', TRUE);
647 CREATE TABLE vandelay.authority_queue (
648 queue_type TEXT NOT NULL DEFAULT 'authority' CHECK (queue_type = 'authority'),
649 CONSTRAINT vand_authority_queue_name_once_per_owner_const UNIQUE (owner,name,queue_type)
650 ) INHERITS (vandelay.queue);
651 ALTER TABLE vandelay.authority_queue ADD PRIMARY KEY (id);
653 CREATE TABLE vandelay.queued_authority_record (
654 queue INT NOT NULL REFERENCES vandelay.authority_queue (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
655 imported_as INT REFERENCES authority.record_entry (id) DEFERRABLE INITIALLY DEFERRED
656 ) INHERITS (vandelay.queued_record);
657 ALTER TABLE vandelay.queued_authority_record ADD PRIMARY KEY (id);
659 CREATE TABLE vandelay.queued_authority_record_attr (
660 id BIGSERIAL PRIMARY KEY,
661 record BIGINT NOT NULL REFERENCES vandelay.queued_authority_record (id) DEFERRABLE INITIALLY DEFERRED,
662 field INT NOT NULL REFERENCES vandelay.authority_attr_definition (id) DEFERRABLE INITIALLY DEFERRED,
663 attr_value TEXT NOT NULL
666 CREATE TABLE vandelay.authority_match (
667 id BIGSERIAL PRIMARY KEY,
668 matched_attr INT REFERENCES vandelay.queued_authority_record_attr (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
669 queued_record BIGINT REFERENCES vandelay.queued_authority_record (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
670 eg_record BIGINT REFERENCES authority.record_entry (id) DEFERRABLE INITIALLY DEFERRED
673 CREATE OR REPLACE FUNCTION vandelay.ingest_authority_marc ( ) RETURNS TRIGGER AS $$
679 FOR adef IN SELECT * FROM vandelay.authority_attr_definition LOOP
681 SELECT extract_marc_field('vandelay.queued_authority_record', id, adef.xpath, adef.remove) INTO value FROM vandelay.queued_authority_record WHERE id = NEW.id;
682 IF (value IS NOT NULL AND value <> '') THEN
683 INSERT INTO vandelay.queued_authority_record_attr (record, field, attr_value) VALUES (NEW.id, adef.id, value);
692 CREATE OR REPLACE FUNCTION vandelay.cleanup_authority_marc ( ) RETURNS TRIGGER AS $$
694 DELETE FROM vandelay.queued_authority_record_attr WHERE record = OLD.id;
695 IF TG_OP = 'UPDATE' THEN
702 CREATE TRIGGER cleanup_authority_trigger
703 BEFORE UPDATE OR DELETE ON vandelay.queued_authority_record
704 FOR EACH ROW EXECUTE PROCEDURE vandelay.cleanup_authority_marc();
706 CREATE TRIGGER ingest_authority_trigger
707 AFTER INSERT OR UPDATE ON vandelay.queued_authority_record
708 FOR EACH ROW EXECUTE PROCEDURE vandelay.ingest_authority_marc();