1 DROP SCHEMA IF EXISTS vandelay CASCADE;
5 CREATE OR REPLACE FUNCTION array_remove_item_by_value(inp ANYARRAY, el ANYELEMENT) RETURNS anyarray AS $$ SELECT ARRAY_ACCUM(x.e) FROM UNNEST( $1 ) x(e) WHERE x.e <> $2; $$ LANGUAGE SQL;
7 CREATE SCHEMA vandelay;
9 CREATE TABLE vandelay.match_set (
10 id SERIAL PRIMARY KEY,
12 owner INT NOT NULL REFERENCES actor.org_unit (id) ON DELETE CASCADE,
13 mtype TEXT NOT NULL DEFAULT 'biblio', -- 'biblio','authority','mfhd'?, others?
14 CONSTRAINT name_once_per_owner_mtype UNIQUE (name, owner, mtype)
17 -- Table to define match points, either FF via SVF or tag+subfield
18 CREATE TABLE vandelay.match_set_point (
19 id SERIAL PRIMARY KEY,
20 match_set INT NOT NULL REFERENCES vandelay.match_set (id),
21 svf TEXT REFERENCES config.record_attr_definition,
24 required BOOL NOT NULL DEFAULT TRUE,
25 quality INT NOT NULL DEFAULT 1, -- higher is better
26 CONSTRAINT vmsp_need_a_subfield_with_a_tag CHECK ((tag IS NOT NULL AND subfield IS NOT NULL) OR tag IS NULL),
27 CONSTRAINT vmsp_need_a_tag_or_a_ff CHECK ((tag IS NOT NULL AND svf IS NULL) OR (tag IS NULL AND svf IS NOT NULL))
29 CREATE UNIQUE INDEX vmsp_def_once_per_set ON vandelay.match_set_point (match_set, COALESCE(tag,''), COALESCE(subfield,''), COALESCE(svf,''));
31 CREATE TABLE vandelay.match_set_quality (
32 id SERIAL PRIMARY KEY,
33 match_set INT NOT NULL REFERENCES vandelay.match_set (id),
34 svf TEXT REFERENCES config.record_attr_definition,
38 quality INT NOT NULL DEFAULT 1, -- higher is better
39 CONSTRAINT vmsq_need_a_subfield_with_a_tag CHECK ((tag IS NOT NULL AND subfield IS NOT NULL) OR tag IS NULL),
40 CONSTRAINT vmsq_need_a_tag_or_a_ff CHECK ((tag IS NOT NULL AND svf IS NULL) OR (tag IS NULL AND svf IS NOT NULL))
42 CREATE UNIQUE INDEX vmsq_def_once_per_set ON vandelay.match_set_quality (match_set, COALESCE(tag,''), COALESCE(subfield,''), COALESCE(svf,''));
45 CREATE TABLE vandelay.queue (
46 id BIGSERIAL PRIMARY KEY,
47 owner INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
49 complete BOOL NOT NULL DEFAULT FALSE,
50 queue_type TEXT NOT NULL DEFAULT 'bib' CHECK (queue_type IN ('bib','authority')),
51 match_set INT REFERENCES vandelay.match_set (id) ON UPDATE CASCADE ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
52 CONSTRAINT vand_queue_name_once_per_owner_const UNIQUE (owner,name,queue_type)
55 CREATE TABLE vandelay.queued_record (
56 id BIGSERIAL PRIMARY KEY,
57 create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
58 import_time TIMESTAMP WITH TIME ZONE,
59 purpose TEXT NOT NULL DEFAULT 'import' CHECK (purpose IN ('import','overlay')),
61 quality INT NOT NULL DEFAULT 0
66 /* Bib stuff at the top */
67 ----------------------------------------------------
69 CREATE TABLE vandelay.bib_attr_definition (
70 id SERIAL PRIMARY KEY,
71 code TEXT UNIQUE NOT NULL,
74 remove TEXT NOT NULL DEFAULT ''
77 -- Each TEXT field (other than 'name') should hold an XPath predicate for pulling the data needed
78 -- DROP TABLE vandelay.import_item_attr_definition CASCADE;
79 CREATE TABLE vandelay.import_item_attr_definition (
80 id BIGSERIAL PRIMARY KEY,
81 owner INT NOT NULL REFERENCES actor.org_unit (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
84 keep BOOL NOT NULL DEFAULT FALSE,
104 priv_note_title TEXT,
106 CONSTRAINT vand_import_item_attr_def_idx UNIQUE (owner,name)
109 CREATE TABLE vandelay.import_error (
110 code TEXT PRIMARY KEY,
111 description TEXT NOT NULL -- i18n
114 CREATE TABLE vandelay.bib_queue (
115 queue_type TEXT NOT NULL DEFAULT 'bib' CHECK (queue_type = 'bib'),
116 item_attr_def BIGINT REFERENCES vandelay.import_item_attr_definition (id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
117 CONSTRAINT vand_bib_queue_name_once_per_owner_const UNIQUE (owner,name,queue_type)
118 ) INHERITS (vandelay.queue);
119 ALTER TABLE vandelay.bib_queue ADD PRIMARY KEY (id);
121 CREATE TABLE vandelay.queued_bib_record (
122 queue INT NOT NULL REFERENCES vandelay.bib_queue (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
123 bib_source INT REFERENCES config.bib_source (id) DEFERRABLE INITIALLY DEFERRED,
124 imported_as BIGINT REFERENCES biblio.record_entry (id) DEFERRABLE INITIALLY DEFERRED,
125 import_error TEXT REFERENCES vandelay.import_error (code) ON DELETE SET NULL ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
127 ) INHERITS (vandelay.queued_record);
128 ALTER TABLE vandelay.queued_bib_record ADD PRIMARY KEY (id);
129 CREATE INDEX queued_bib_record_queue_idx ON vandelay.queued_bib_record (queue);
131 CREATE TABLE vandelay.queued_bib_record_attr (
132 id BIGSERIAL PRIMARY KEY,
133 record BIGINT NOT NULL REFERENCES vandelay.queued_bib_record (id) DEFERRABLE INITIALLY DEFERRED,
134 field INT NOT NULL REFERENCES vandelay.bib_attr_definition (id) DEFERRABLE INITIALLY DEFERRED,
135 attr_value TEXT NOT NULL
137 CREATE INDEX queued_bib_record_attr_record_idx ON vandelay.queued_bib_record_attr (record);
139 CREATE TABLE vandelay.bib_match (
140 id BIGSERIAL PRIMARY KEY,
141 matched_set INT REFERENCES vandelay.match_set (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
142 queued_record BIGINT REFERENCES vandelay.queued_bib_record (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
143 eg_record BIGINT REFERENCES biblio.record_entry (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
144 quality INT NOT NULL DEFAULT 0
147 CREATE TABLE vandelay.import_item (
148 id BIGSERIAL PRIMARY KEY,
149 record BIGINT NOT NULL REFERENCES vandelay.queued_bib_record (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
150 definition BIGINT NOT NULL REFERENCES vandelay.import_item_attr_definition (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
151 import_error TEXT REFERENCES vandelay.import_error (code) ON DELETE SET NULL ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
161 deposit_amount NUMERIC(8,2),
174 CREATE TABLE vandelay.import_bib_trash_fields (
175 id BIGSERIAL PRIMARY KEY,
176 owner INT NOT NULL REFERENCES actor.org_unit (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
178 CONSTRAINT vand_import_bib_trash_fields_idx UNIQUE (owner,field)
181 CREATE TABLE vandelay.merge_profile (
182 id BIGSERIAL PRIMARY KEY,
183 owner INT NOT NULL REFERENCES actor.org_unit (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
189 CONSTRAINT vand_merge_prof_owner_name_idx UNIQUE (owner,name),
190 CONSTRAINT add_replace_strip_or_preserve CHECK ((preserve_spec IS NOT NULL OR replace_spec IS NOT NULL) OR (preserve_spec IS NULL AND replace_spec IS NULL))
193 CREATE OR REPLACE FUNCTION vandelay.marc21_record_type( marc TEXT ) RETURNS config.marc21_rec_type_map AS $func$
200 retval config.marc21_rec_type_map%ROWTYPE;
202 ldr := oils_xpath_string( '//*[local-name()="leader"]', marc );
204 IF ldr IS NULL OR ldr = '' THEN
205 SELECT * INTO retval FROM config.marc21_rec_type_map WHERE code = 'BKS';
209 SELECT * INTO tval_rec FROM config.marc21_ff_pos_map WHERE fixed_field = 'Type' LIMIT 1; -- They're all the same
210 SELECT * INTO bval_rec FROM config.marc21_ff_pos_map WHERE fixed_field = 'BLvl' LIMIT 1; -- They're all the same
213 tval := SUBSTRING( ldr, tval_rec.start_pos + 1, tval_rec.length );
214 bval := SUBSTRING( ldr, bval_rec.start_pos + 1, bval_rec.length );
216 -- RAISE NOTICE 'type %, blvl %, ldr %', tval, bval, ldr;
218 SELECT * INTO retval FROM config.marc21_rec_type_map WHERE type_val LIKE '%' || tval || '%' AND blvl_val LIKE '%' || bval || '%';
221 IF retval.code IS NULL THEN
222 SELECT * INTO retval FROM config.marc21_rec_type_map WHERE code = 'BKS';
227 $func$ LANGUAGE PLPGSQL;
229 CREATE OR REPLACE FUNCTION vandelay.marc21_extract_fixed_field( marc TEXT, ff TEXT ) RETURNS TEXT AS $func$
236 rtype := (vandelay.marc21_record_type( marc )).code;
237 FOR ff_pos IN SELECT * FROM config.marc21_ff_pos_map WHERE fixed_field = ff AND rec_type = rtype ORDER BY tag DESC LOOP
238 FOR tag_data IN SELECT value FROM UNNEST( oils_xpath( '//*[@tag="' || UPPER(ff_pos.tag) || '"]/text()', marc ) ) x(value) LOOP
239 val := SUBSTRING( tag_data.value, ff_pos.start_pos + 1, ff_pos.length );
242 val := REPEAT( ff_pos.default_val, ff_pos.length );
248 $func$ LANGUAGE PLPGSQL;
250 CREATE TYPE biblio.record_ff_map AS (record BIGINT, ff_name TEXT, ff_value TEXT);
251 CREATE OR REPLACE FUNCTION vandelay.marc21_extract_all_fixed_fields( marc TEXT ) RETURNS SETOF biblio.record_ff_map AS $func$
256 output biblio.record_ff_map%ROWTYPE;
258 rtype := (vandelay.marc21_record_type( marc )).code;
260 FOR ff_pos IN SELECT * FROM config.marc21_ff_pos_map WHERE rec_type = rtype ORDER BY tag DESC LOOP
261 output.ff_name := ff_pos.fixed_field;
262 output.ff_value := NULL;
264 FOR tag_data IN SELECT value FROM UNNEST( oils_xpath( '//*[@tag="' || UPPER(tag) || '"]/text()', marc ) ) x(value) LOOP
265 output.ff_value := SUBSTRING( tag_data.value, ff_pos.start_pos + 1, ff_pos.length );
266 IF output.ff_value IS NULL THEN output.ff_value := REPEAT( ff_pos.default_val, ff_pos.length ); END IF;
268 output.ff_value := NULL;
275 $func$ LANGUAGE PLPGSQL;
277 CREATE TYPE biblio.marc21_physical_characteristics AS ( id INT, record BIGINT, ptype TEXT, subfield INT, value INT );
278 CREATE OR REPLACE FUNCTION vandelay.marc21_physical_characteristics( marc TEXT) RETURNS SETOF biblio.marc21_physical_characteristics AS $func$
282 ptype config.marc21_physical_characteristic_type_map%ROWTYPE;
283 psf config.marc21_physical_characteristic_subfield_map%ROWTYPE;
284 pval config.marc21_physical_characteristic_value_map%ROWTYPE;
285 retval biblio.marc21_physical_characteristics%ROWTYPE;
288 _007 := oils_xpath_string( '//*[@tag="007"]', marc );
290 IF _007 IS NOT NULL AND _007 <> '' THEN
291 SELECT * INTO ptype FROM config.marc21_physical_characteristic_type_map WHERE ptype_key = SUBSTRING( _007, 1, 1 );
293 IF ptype.ptype_key IS NOT NULL THEN
294 FOR psf IN SELECT * FROM config.marc21_physical_characteristic_subfield_map WHERE ptype_key = ptype.ptype_key LOOP
295 SELECT * INTO pval FROM config.marc21_physical_characteristic_value_map WHERE ptype_subfield = psf.id AND value = SUBSTRING( _007, psf.start_pos + 1, psf.length );
297 IF pval.id IS NOT NULL THEN
300 retval.ptype := ptype.ptype_key;
301 retval.subfield := psf.id;
302 retval.value := pval.id;
312 $func$ LANGUAGE PLPGSQL;
314 CREATE TYPE vandelay.flat_marc AS ( tag CHAR(3), ind1 TEXT, ind2 TEXT, subfield TEXT, value TEXT );
315 CREATE OR REPLACE FUNCTION vandelay.flay_marc ( TEXT ) RETURNS SETOF vandelay.flat_marc AS $func$
318 use MARC::File::XML (BinaryEncoding => 'UTF-8');
321 my $r = MARC::Record->new_from_xml( $xml );
323 return_next( { tag => 'LDR', value => $r->leader } );
325 for my $f ( $r->fields ) {
326 if ($f->is_control_field) {
327 return_next({ tag => $f->tag, value => $f->data });
329 for my $s ($f->subfields) {
332 ind1 => $f->indicator(1),
333 ind2 => $f->indicator(2),
338 if ( $f->tag eq '245' and $s->[0] eq 'a' ) {
339 my $trim = $f->indicator(2) || 0;
342 ind1 => $f->indicator(1),
343 ind2 => $f->indicator(2),
345 value => substr( $s->[1], $trim )
354 $func$ LANGUAGE PLPERLU;
356 CREATE OR REPLACE FUNCTION vandelay.flatten_marc ( marc TEXT ) RETURNS SETOF vandelay.flat_marc AS $func$
358 output vandelay.flat_marc%ROWTYPE;
361 FOR field IN SELECT * FROM vandelay.flay_marc( marc ) LOOP
362 output.ind1 := field.ind1;
363 output.ind2 := field.ind2;
364 output.tag := field.tag;
365 output.subfield := field.subfield;
366 IF field.subfield IS NOT NULL AND field.tag NOT IN ('020','022','024') THEN -- exclude standard numbers and control fields
367 output.value := naco_normalize(field.value, field.subfield);
369 output.value := field.value;
372 CONTINUE WHEN output.value IS NULL;
377 $func$ LANGUAGE PLPGSQL;
379 CREATE OR REPLACE FUNCTION vandelay.extract_rec_attrs ( xml TEXT, attr_defs TEXT[]) RETURNS hstore AS $_$
381 transformed_xml TEXT;
384 xfrm config.xml_transform%ROWTYPE;
386 new_attrs HSTORE := ''::HSTORE;
387 attr_def config.record_attr_definition%ROWTYPE;
390 FOR attr_def IN SELECT * FROM config.record_attr_definition WHERE name IN (SELECT * FROM UNNEST(attr_defs)) ORDER BY format LOOP
392 IF attr_def.tag IS NOT NULL THEN -- tag (and optional subfield list) selection
393 SELECT ARRAY_TO_STRING(ARRAY_ACCUM(x.value), COALESCE(attr_def.joiner,' ')) INTO attr_value
394 FROM vandelay.flatten_marc(xml) AS x
395 WHERE x.tag LIKE attr_def.tag
397 WHEN attr_def.sf_list IS NOT NULL
398 THEN POSITION(x.subfield IN attr_def.sf_list) > 0
405 ELSIF attr_def.fixed_field IS NOT NULL THEN -- a named fixed field, see config.marc21_ff_pos_map.fixed_field
406 attr_value := vandelay.marc21_extract_fixed_field(xml, attr_def.fixed_field);
408 ELSIF attr_def.xpath IS NOT NULL THEN -- and xpath expression
410 SELECT INTO xfrm * FROM config.xml_transform WHERE name = attr_def.format;
412 -- See if we can skip the XSLT ... it's expensive
413 IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
414 -- Can't skip the transform
415 IF xfrm.xslt <> '---' THEN
416 transformed_xml := oils_xslt_process(xml,xfrm.xslt);
418 transformed_xml := xml;
421 prev_xfrm := xfrm.name;
424 IF xfrm.name IS NULL THEN
425 -- just grab the marcxml (empty) transform
426 SELECT INTO xfrm * FROM config.xml_transform WHERE xslt = '---' LIMIT 1;
427 prev_xfrm := xfrm.name;
430 attr_value := oils_xpath_string(attr_def.xpath, transformed_xml, COALESCE(attr_def.joiner,' '), ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]);
432 ELSIF attr_def.phys_char_sf IS NOT NULL THEN -- a named Physical Characteristic, see config.marc21_physical_characteristic_*_map
433 SELECT value::TEXT INTO attr_value
434 FROM vandelay.marc21_physical_characteristics(xml)
435 WHERE subfield = attr_def.phys_char_sf
436 LIMIT 1; -- Just in case ...
440 -- apply index normalizers to attr_value
442 SELECT n.func AS func,
443 n.param_count AS param_count,
445 FROM config.index_normalizer n
446 JOIN config.record_attr_index_norm_map m ON (m.norm = n.id)
447 WHERE attr = attr_def.name
449 EXECUTE 'SELECT ' || normalizer.func || '(' ||
450 quote_literal( attr_value ) ||
452 WHEN normalizer.param_count > 0
453 THEN ',' || REPLACE(REPLACE(BTRIM(normalizer.params,'[]'),E'\'',E'\\\''),E'"',E'\'')
460 -- Add the new value to the hstore
461 new_attrs := new_attrs || hstore( attr_def.name, attr_value );
467 $_$ LANGUAGE PLPGSQL;
469 CREATE OR REPLACE FUNCTION vandelay.extract_rec_attrs ( xml TEXT ) RETURNS hstore AS $_$
470 SELECT vandelay.extract_rec_attrs( $1, (SELECT ARRAY_ACCUM(name) FROM config.record_attr_definition));
473 CREATE OR REPLACE FUNCTION vandelay.match_bib_record ( ) RETURNS TRIGGER AS $func$
475 incoming_existing_id TEXT;
476 my_bib_queue vandelay.bib_queue%ROWTYPE;
477 my_match_set vandelay.match_set%ROWTYPE;
478 test vandelay.match_set_point%ROWTYPE;
479 potential_matches BIGINT[];
487 DELETE FROM vandelay.bib_match WHERE queued_record = NEW.id;
489 incoming_existing_id := oils_xpath_string('//*[@tag="901"]/*[@code="c"][1]',NEW.marc);
491 IF incoming_existing_id IS NOT NULL THEN
492 SELECT id INTO tmp_rec FROM biblio.record_entry WHERE id = exact_id;
493 IF tmp_rec IS NOT NULL THEN
494 INSERT INTO vandelay.bib_match (queued_record, eg_record, quality) VALUES ( NEW.id, exact_id, 9999);
499 SELECT * INTO my_bib_queue FROM vandelay.bib_queue WHERE id = NEW.queue;
502 -- whew ... here we go ...
503 FOR test IN SELECT * FROM vandelay.match_set_point WHERE match_set = my_bib_queue.match_set ORDER BY required DESC LOOP
504 IF test.tag IS NOT NULL THEN
505 FOR rvalue IN SELECT value FROM vandelay.flatten_marc( xml ) WHERE tag = test.tag AND subfield = test.subfield LOOP
506 SELECT ARRAY_ACCUM(DISTINCT record) INTO potential_matches FROM metabib.real_full_rec WHERE tag = test.tag AND subfield = test.subfield AND value = rvalue;
509 matches := potential_matches;
510 first_round := FALSE;
511 ELSIF test.required THEN
512 FOR tmp_rec IN SELECT * FROM UNNEST(matches) LOOP
513 IF tmp_rec NOT IN (SELECT * FROM UNNEST(potential_matches)) THEN
514 matches := array_remove_item_by_value(matches, tmp_rec);
515 potential_matches := array_remove_item_by_value(potential_matches, tmp_rec);
520 -- add the quality for this match
521 FOR tmp_rec IN SELECT * FROM UNNEST(potential_matches) LOOP
522 tmp_quality := COALESCE((quality_set -> tmp_rec::TEXT)::INT, 0);
523 quality_set := quality_set || hstore(tmp_rec::TEXT, (tmp_quality + test.quality)::TEXT);
528 rvalue := vandelay.vandelay.extract_rec_attrs(xml, ARRAY[test.svf]);
531 matches := potential_matches;
532 first_round := FALSE;
533 ELSIF test.required THEN
534 FOR tmp_rec IN SELECT * FROM UNNEST(matches) LOOP
535 IF tmp_rec NOT IN (SELECT * FROM UNNEST(potential_matches)) THEN
536 matches := array_remove_item_by_value(matches, tmp_rec);
537 potential_matches := array_remove_item_by_value(potential_matches, tmp_rec);
542 -- add the quality for this match
543 FOR tmp_rec IN SELECT * FROM UNNEST(potential_matches) LOOP
544 tmp_quality := COALESCE((quality_set -> tmp_rec::TEXT)::INT, 0);
545 quality_set := quality_set || hstore(tmp_rec::TEXT, (tmp_quality + test.quality)::TEXT);
551 FOR tmp_rec IN SELECT * FROM UNNEST(matches) LOOP
552 INSERT INTO vandelay.bib_match (matched_set, queued_record, eg_record, quality) VALUES (my_bib_queue.match_set, NEW.id, tmp_rec, (quality_set -> tmp_rec::TEXT));
557 $func$ LANGUAGE PLPGSQL;
559 CREATE OR REPLACE FUNCTION vandelay.incoming_record_quality ( xml TEXT, match_set_id INT ) RETURNS INT AS $_$
563 test vandelay.match_set_quality%ROWTYPE;
566 FOR test IN SELECT * FROM vandelay.match_set_quality WHERE match_set = match_set_id LOOP
567 IF test.tag IS NOT NULL THEN
568 FOR rvalue IN SELECT value FROM vandelay.flatten_marc( xml ) WHERE tag = test.tag AND subfield = test.subfield LOOP
569 IF test.value = rvalue THEN
570 out_q := out_q + test.quality;
574 IF test.value = vandelay.extract_rec_attrs(xml, ARRAY[test.svf]) THEN
575 out_q := out_q + test.quality;
582 $_$ LANGUAGE PLPGSQL;
584 CREATE TYPE vandelay.tcn_data AS (tcn TEXT, tcn_source TEXT, used BOOL);
585 CREATE OR REPLACE FUNCTION vandelay.find_bib_tcn_data ( xml TEXT ) RETURNS SETOF vandelay.tcn_data AS $_$
589 output vandelay.tcn_data%ROWTYPE;
593 eg_tcn := BTRIM((oils_xpath('//*[@tag="001"]/text()',xml))[1]);
594 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
596 eg_tcn_source := BTRIM((oils_xpath('//*[@tag="003"]/text()',xml))[1]);
597 IF eg_tcn_source IS NULL OR eg_tcn_source = '' THEN
598 eg_tcn_source := 'System Local';
601 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
604 output.used := FALSE;
609 output.tcn := eg_tcn;
610 output.tcn_source := eg_tcn_source;
616 eg_tcn := BTRIM((oils_xpath('//*[@tag="901"]/*[@code="a"]/text()',xml))[1]);
617 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
619 eg_tcn_source := BTRIM((oils_xpath('//*[@tag="901"]/*[@code="b"]/text()',xml))[1]);
620 IF eg_tcn_source IS NULL OR eg_tcn_source = '' THEN
621 eg_tcn_source := 'System Local';
624 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
627 output.used := FALSE;
632 output.tcn := eg_tcn;
633 output.tcn_source := eg_tcn_source;
639 eg_tcn := BTRIM((oils_xpath('//*[@tag="039"]/*[@code="a"]/text()',xml))[1]);
640 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
642 eg_tcn_source := BTRIM((oils_xpath('//*[@tag="039"]/*[@code="b"]/text()',xml))[1]);
643 IF eg_tcn_source IS NULL OR eg_tcn_source = '' THEN
644 eg_tcn_source := 'System Local';
647 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
650 output.used := FALSE;
655 output.tcn := eg_tcn;
656 output.tcn_source := eg_tcn_source;
662 eg_tcn := REGEXP_REPLACE((oils_xpath('//*[@tag="020"]/*[@code="a"]/text()',xml))[1], $re$^(\w+).*?$$re$, $re$\1$re$);
663 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
665 eg_tcn_source := 'ISBN';
667 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
670 output.used := FALSE;
675 output.tcn := eg_tcn;
676 output.tcn_source := eg_tcn_source;
682 eg_tcn := REGEXP_REPLACE((oils_xpath('//*[@tag="022"]/*[@code="a"]/text()',xml))[1], $re$^(\w+).*?$$re$, $re$\1$re$);
683 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
685 eg_tcn_source := 'ISSN';
687 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
690 output.used := FALSE;
695 output.tcn := eg_tcn;
696 output.tcn_source := eg_tcn_source;
702 eg_tcn := REGEXP_REPLACE((oils_xpath('//*[@tag="010"]/*[@code="a"]/text()',xml))[1], $re$^(\w+).*?$$re$, $re$\1$re$);
703 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
705 eg_tcn_source := 'LCCN';
707 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
710 output.used := FALSE;
715 output.tcn := eg_tcn;
716 output.tcn_source := eg_tcn_source;
722 eg_tcn := REGEXP_REPLACE((oils_xpath('//*[@tag="035"]/*[@code="a"]/text()',xml))[1], $re$^.*?(\w+)$$re$, $re$\1$re$);
723 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
725 eg_tcn_source := 'System Legacy';
727 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
730 output.used := FALSE;
735 output.tcn := eg_tcn;
736 output.tcn_source := eg_tcn_source;
743 $_$ LANGUAGE PLPGSQL;
745 CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT, field TEXT, force_add INT ) RETURNS TEXT AS $_$
748 use MARC::File::XML (BinaryEncoding => 'UTF-8');
752 MARC::Charset->assume_unicode(1);
754 my $target_xml = shift;
755 my $source_xml = shift;
756 my $field_spec = shift;
757 my $force_add = shift || 0;
759 my $target_r = MARC::Record->new_from_xml( $target_xml );
760 my $source_r = MARC::Record->new_from_xml( $source_xml );
762 return $target_xml unless ($target_r && $source_r);
764 my @field_list = split(',', $field_spec);
767 for my $f (@field_list) {
768 $f =~ s/^\s*//; $f =~ s/\s*$//;
769 if ($f =~ /^(.{3})(\w*)(?:\[([^]]*)\])?$/) {
775 $match =~ s/^\s*//; $match =~ s/\s*$//;
776 $fields{$field} = { sf => [ split('', $sf) ] };
778 my ($msf,$mre) = split('~', $match);
779 if (length($msf) > 0 and length($mre) > 0) {
780 $msf =~ s/^\s*//; $msf =~ s/\s*$//;
781 $mre =~ s/^\s*//; $mre =~ s/\s*$//;
782 $fields{$field}{match} = { sf => $msf, re => qr/$mre/ };
788 for my $f ( keys %fields) {
789 if ( @{$fields{$f}{sf}} ) {
790 for my $from_field ($source_r->field( $f )) {
791 my @tos = $target_r->field( $f );
793 next if (exists($fields{$f}{match}) and !$force_add);
794 my @new_fields = map { $_->clone } $source_r->field( $f );
795 $target_r->insert_fields_ordered( @new_fields );
797 for my $to_field (@tos) {
798 if (exists($fields{$f}{match})) {
799 next unless (grep { $_ =~ $fields{$f}{match}{re} } $to_field->subfield($fields{$f}{match}{sf}));
801 my @new_sf = map { ($_ => $from_field->subfield($_)) } @{$fields{$f}{sf}};
802 $to_field->add_subfields( @new_sf );
807 my @new_fields = map { $_->clone } $source_r->field( $f );
808 $target_r->insert_fields_ordered( @new_fields );
812 $target_xml = $target_r->as_xml_record;
813 $target_xml =~ s/^<\?.+?\?>$//mo;
814 $target_xml =~ s/\n//sgo;
815 $target_xml =~ s/>\s+</></sgo;
819 $_$ LANGUAGE PLPERLU;
821 CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
822 SELECT vandelay.add_field( $1, $2, $3, 0 );
825 CREATE OR REPLACE FUNCTION vandelay.strip_field ( xml TEXT, field TEXT ) RETURNS TEXT AS $_$
828 use MARC::File::XML (BinaryEncoding => 'UTF-8');
832 MARC::Charset->assume_unicode(1);
835 my $r = MARC::Record->new_from_xml( $xml );
837 return $xml unless ($r);
839 my $field_spec = shift;
840 my @field_list = split(',', $field_spec);
843 for my $f (@field_list) {
844 $f =~ s/^\s*//; $f =~ s/\s*$//;
845 if ($f =~ /^(.{3})(\w*)(?:\[([^]]*)\])?$/) {
851 $match =~ s/^\s*//; $match =~ s/\s*$//;
852 $fields{$field} = { sf => [ split('', $sf) ] };
854 my ($msf,$mre) = split('~', $match);
855 if (length($msf) > 0 and length($mre) > 0) {
856 $msf =~ s/^\s*//; $msf =~ s/\s*$//;
857 $mre =~ s/^\s*//; $mre =~ s/\s*$//;
858 $fields{$field}{match} = { sf => $msf, re => qr/$mre/ };
864 for my $f ( keys %fields) {
865 for my $to_field ($r->field( $f )) {
866 if (exists($fields{$f}{match})) {
867 next unless (grep { $_ =~ $fields{$f}{match}{re} } $to_field->subfield($fields{$f}{match}{sf}));
870 if ( @{$fields{$f}{sf}} ) {
871 $to_field->delete_subfield(code => $fields{$f}{sf});
873 $r->delete_field( $to_field );
878 $xml = $r->as_xml_record;
879 $xml =~ s/^<\?.+?\?>$//mo;
881 $xml =~ s/>\s+</></sgo;
885 $_$ LANGUAGE PLPERLU;
887 CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
894 parsed_target := vandelay.strip_field( target_xml, ''); -- this dance normalizes the format of the xml for the IF below
896 FOR curr_field IN SELECT UNNEST( STRING_TO_ARRAY(field, ',') ) LOOP -- naive split, but it's the same we use in the perl
898 xml_output := vandelay.strip_field( parsed_target, curr_field);
900 IF xml_output <> parsed_target AND curr_field ~ E'~' THEN
901 -- we removed something, and there was a regexp restriction in the curr_field definition, so proceed
902 xml_output := vandelay.add_field( xml_output, source_xml, curr_field, 1 );
903 ELSIF curr_field !~ E'~' THEN
904 -- No regexp restriction, add the curr_field
905 xml_output := vandelay.add_field( xml_output, source_xml, curr_field, 0 );
908 parsed_target := xml_output; -- in prep for any following loop iterations
914 $_$ LANGUAGE PLPGSQL;
916 CREATE OR REPLACE FUNCTION vandelay.merge_record_xml ( target_xml TEXT, source_xml TEXT, add_rule TEXT, replace_preserve_rule TEXT, strip_rule TEXT ) RETURNS TEXT AS $_$
917 SELECT vandelay.replace_field( vandelay.add_field( vandelay.strip_field( $1, $5) , $2, $3 ), $2, $4);
920 CREATE TYPE vandelay.compile_profile AS (add_rule TEXT, replace_rule TEXT, preserve_rule TEXT, strip_rule TEXT);
921 CREATE OR REPLACE FUNCTION vandelay.compile_profile ( incoming_xml TEXT ) RETURNS vandelay.compile_profile AS $_$
923 output vandelay.compile_profile%ROWTYPE;
924 profile vandelay.merge_profile%ROWTYPE;
926 profile_tmpl_owner TEXT;
928 strip_rule TEXT := '';
929 replace_rule TEXT := '';
930 preserve_rule TEXT := '';
934 profile_tmpl := (oils_xpath('//*[@tag="905"]/*[@code="t"]/text()',incoming_xml))[1];
935 profile_tmpl_owner := (oils_xpath('//*[@tag="905"]/*[@code="o"]/text()',incoming_xml))[1];
937 IF profile_tmpl IS NOT NULL AND profile_tmpl <> '' AND profile_tmpl_owner IS NOT NULL AND profile_tmpl_owner <> '' THEN
938 SELECT p.* INTO profile
939 FROM vandelay.merge_profile p
940 JOIN actor.org_unit u ON (u.id = p.owner)
941 WHERE p.name = profile_tmpl
942 AND u.shortname = profile_tmpl_owner;
944 IF profile.id IS NOT NULL THEN
945 add_rule := COALESCE(profile.add_spec,'');
946 strip_rule := COALESCE(profile.strip_spec,'');
947 replace_rule := COALESCE(profile.replace_spec,'');
948 preserve_rule := COALESCE(profile.preserve_spec,'');
952 add_rule := add_rule || ',' || COALESCE(ARRAY_TO_STRING(oils_xpath('//*[@tag="905"]/*[@code="a"]/text()',incoming_xml),','),'');
953 strip_rule := strip_rule || ',' || COALESCE(ARRAY_TO_STRING(oils_xpath('//*[@tag="905"]/*[@code="d"]/text()',incoming_xml),','),'');
954 replace_rule := replace_rule || ',' || COALESCE(ARRAY_TO_STRING(oils_xpath('//*[@tag="905"]/*[@code="r"]/text()',incoming_xml),','),'');
955 preserve_rule := preserve_rule || ',' || COALESCE(ARRAY_TO_STRING(oils_xpath('//*[@tag="905"]/*[@code="p"]/text()',incoming_xml),','),'');
957 output.add_rule := BTRIM(add_rule,',');
958 output.replace_rule := BTRIM(replace_rule,',');
959 output.strip_rule := BTRIM(strip_rule,',');
960 output.preserve_rule := BTRIM(preserve_rule,',');
964 $_$ LANGUAGE PLPGSQL;
966 CREATE OR REPLACE FUNCTION vandelay.template_overlay_bib_record ( v_marc TEXT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
968 merge_profile vandelay.merge_profile%ROWTYPE;
969 dyn_profile vandelay.compile_profile%ROWTYPE;
979 SELECT b.marc INTO eg_marc
980 FROM biblio.record_entry b
984 IF eg_marc IS NULL OR v_marc IS NULL THEN
985 -- RAISE NOTICE 'no marc for template or bib record';
989 dyn_profile := vandelay.compile_profile( v_marc );
991 IF merge_profile_id IS NOT NULL THEN
992 SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
994 dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
995 dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
996 dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
997 dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
1001 IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
1002 -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
1006 IF dyn_profile.replace_rule <> '' THEN
1007 source_marc = v_marc;
1008 target_marc = eg_marc;
1009 replace_rule = dyn_profile.replace_rule;
1011 source_marc = eg_marc;
1012 target_marc = v_marc;
1013 replace_rule = dyn_profile.preserve_rule;
1016 UPDATE biblio.record_entry
1017 SET marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
1021 -- RAISE NOTICE 'update of biblio.record_entry failed';
1028 $$ LANGUAGE PLPGSQL;
1030 CREATE OR REPLACE FUNCTION vandelay.merge_record_xml ( target_marc TEXT, template_marc TEXT ) RETURNS TEXT AS $$
1032 dyn_profile vandelay.compile_profile%ROWTYPE;
1040 IF target_marc IS NULL OR template_marc IS NULL THEN
1041 -- RAISE NOTICE 'no marc for target or template record';
1045 dyn_profile := vandelay.compile_profile( template_marc );
1047 IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
1048 -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
1052 IF dyn_profile.replace_rule <> '' THEN
1053 trgt_marc = target_marc;
1054 tmpl_marc = template_marc;
1055 replace_rule = dyn_profile.replace_rule;
1057 tmp_marc = target_marc;
1058 trgt_marc = template_marc;
1059 tmpl_marc = tmp_marc;
1060 replace_rule = dyn_profile.preserve_rule;
1063 RETURN vandelay.merge_record_xml( trgt_marc, tmpl_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule );
1066 $$ LANGUAGE PLPGSQL;
1068 CREATE OR REPLACE FUNCTION vandelay.template_overlay_bib_record ( v_marc TEXT, eg_id BIGINT) RETURNS BOOL AS $$
1069 SELECT vandelay.template_overlay_bib_record( $1, $2, NULL);
1072 CREATE OR REPLACE FUNCTION vandelay.overlay_bib_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
1074 merge_profile vandelay.merge_profile%ROWTYPE;
1075 dyn_profile vandelay.compile_profile%ROWTYPE;
1086 SELECT q.marc INTO v_marc
1087 FROM vandelay.queued_record q
1088 JOIN vandelay.bib_match m ON (m.queued_record = q.id AND q.id = import_id)
1091 IF v_marc IS NULL THEN
1092 -- RAISE NOTICE 'no marc for vandelay or bib record';
1096 IF vandelay.template_overlay_bib_record( v_marc, eg_id, merge_profile_id) THEN
1097 UPDATE vandelay.queued_bib_record
1098 SET imported_as = eg_id,
1100 WHERE id = import_id;
1102 editor_string := (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1];
1104 IF editor_string IS NOT NULL AND editor_string <> '' THEN
1105 SELECT usr INTO editor_id FROM actor.card WHERE barcode = editor_string;
1107 IF editor_id IS NULL THEN
1108 SELECT id INTO editor_id FROM actor.usr WHERE usrname = editor_string;
1111 IF editor_id IS NOT NULL THEN
1112 UPDATE biblio.record_entry SET editor = editor_id WHERE id = eg_id;
1119 -- RAISE NOTICE 'update of biblio.record_entry failed';
1124 $$ LANGUAGE PLPGSQL;
1126 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_bib_record ( import_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
1130 match_attr vandelay.bib_attr_definition%ROWTYPE;
1133 PERFORM * FROM vandelay.queued_bib_record WHERE import_time IS NOT NULL AND id = import_id;
1136 -- RAISE NOTICE 'already imported, cannot auto-overlay'
1140 SELECT COUNT(*) INTO match_count FROM vandelay.bib_match WHERE queued_record = import_id;
1142 IF match_count <> 1 THEN
1143 -- RAISE NOTICE 'not an exact match';
1147 SELECT d.* INTO match_attr
1148 FROM vandelay.bib_attr_definition d
1149 JOIN vandelay.queued_bib_record_attr a ON (a.field = d.id)
1150 JOIN vandelay.bib_match m ON (m.matched_attr = a.id)
1151 WHERE m.queued_record = import_id;
1153 IF NOT (match_attr.xpath ~ '@tag="901"' AND match_attr.xpath ~ '@code="c"') THEN
1154 -- RAISE NOTICE 'not a 901c match: %', match_attr.xpath;
1158 SELECT m.eg_record INTO eg_id
1159 FROM vandelay.bib_match m
1160 WHERE m.queued_record = import_id
1163 IF eg_id IS NULL THEN
1167 RETURN vandelay.overlay_bib_record( import_id, eg_id, merge_profile_id );
1169 $$ LANGUAGE PLPGSQL;
1171 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_bib_queue ( queue_id BIGINT, merge_profile_id INT ) RETURNS SETOF BIGINT AS $$
1173 queued_record vandelay.queued_bib_record%ROWTYPE;
1176 FOR queued_record IN SELECT * FROM vandelay.queued_bib_record WHERE queue = queue_id AND import_time IS NULL LOOP
1178 IF vandelay.auto_overlay_bib_record( queued_record.id, merge_profile_id ) THEN
1179 RETURN NEXT queued_record.id;
1187 $$ LANGUAGE PLPGSQL;
1189 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_bib_queue ( queue_id BIGINT ) RETURNS SETOF BIGINT AS $$
1190 SELECT * FROM vandelay.auto_overlay_bib_queue( $1, NULL );
1193 CREATE OR REPLACE FUNCTION vandelay.ingest_items ( import_id BIGINT, attr_def_id BIGINT ) RETURNS SETOF vandelay.import_item AS $$
1204 deposit_amount TEXT;
1217 tmp_attr_set RECORD;
1218 attr_set vandelay.import_item%ROWTYPE;
1224 SELECT * INTO attr_def FROM vandelay.import_item_attr_definition WHERE id = attr_def_id;
1228 attr_set.definition := attr_def.id;
1230 -- Build the combined XPath
1234 WHEN attr_def.owning_lib IS NULL THEN 'null()'
1235 WHEN LENGTH( attr_def.owning_lib ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.owning_lib || '"]'
1236 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.owning_lib
1241 WHEN attr_def.circ_lib IS NULL THEN 'null()'
1242 WHEN LENGTH( attr_def.circ_lib ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.circ_lib || '"]'
1243 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.circ_lib
1248 WHEN attr_def.call_number IS NULL THEN 'null()'
1249 WHEN LENGTH( attr_def.call_number ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.call_number || '"]'
1250 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.call_number
1255 WHEN attr_def.copy_number IS NULL THEN 'null()'
1256 WHEN LENGTH( attr_def.copy_number ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.copy_number || '"]'
1257 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.copy_number
1262 WHEN attr_def.status IS NULL THEN 'null()'
1263 WHEN LENGTH( attr_def.status ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.status || '"]'
1264 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.status
1269 WHEN attr_def.location IS NULL THEN 'null()'
1270 WHEN LENGTH( attr_def.location ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.location || '"]'
1271 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.location
1276 WHEN attr_def.circulate IS NULL THEN 'null()'
1277 WHEN LENGTH( attr_def.circulate ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.circulate || '"]'
1278 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.circulate
1283 WHEN attr_def.deposit IS NULL THEN 'null()'
1284 WHEN LENGTH( attr_def.deposit ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.deposit || '"]'
1285 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.deposit
1290 WHEN attr_def.deposit_amount IS NULL THEN 'null()'
1291 WHEN LENGTH( attr_def.deposit_amount ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.deposit_amount || '"]'
1292 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.deposit_amount
1297 WHEN attr_def.ref IS NULL THEN 'null()'
1298 WHEN LENGTH( attr_def.ref ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.ref || '"]'
1299 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.ref
1304 WHEN attr_def.holdable IS NULL THEN 'null()'
1305 WHEN LENGTH( attr_def.holdable ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.holdable || '"]'
1306 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.holdable
1311 WHEN attr_def.price IS NULL THEN 'null()'
1312 WHEN LENGTH( attr_def.price ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.price || '"]'
1313 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.price
1318 WHEN attr_def.barcode IS NULL THEN 'null()'
1319 WHEN LENGTH( attr_def.barcode ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.barcode || '"]'
1320 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.barcode
1325 WHEN attr_def.circ_modifier IS NULL THEN 'null()'
1326 WHEN LENGTH( attr_def.circ_modifier ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.circ_modifier || '"]'
1327 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.circ_modifier
1332 WHEN attr_def.circ_as_type IS NULL THEN 'null()'
1333 WHEN LENGTH( attr_def.circ_as_type ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.circ_as_type || '"]'
1334 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.circ_as_type
1339 WHEN attr_def.alert_message IS NULL THEN 'null()'
1340 WHEN LENGTH( attr_def.alert_message ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.alert_message || '"]'
1341 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.alert_message
1346 WHEN attr_def.opac_visible IS NULL THEN 'null()'
1347 WHEN LENGTH( attr_def.opac_visible ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.opac_visible || '"]'
1348 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.opac_visible
1353 WHEN attr_def.pub_note IS NULL THEN 'null()'
1354 WHEN LENGTH( attr_def.pub_note ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.pub_note || '"]'
1355 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.pub_note
1359 WHEN attr_def.priv_note IS NULL THEN 'null()'
1360 WHEN LENGTH( attr_def.priv_note ) = 1 THEN '//*[@tag="' || attr_def.tag || '"]/*[@code="' || attr_def.priv_note || '"]'
1361 ELSE '//*[@tag="' || attr_def.tag || '"]/*' || attr_def.priv_note
1366 owning_lib || '|' ||
1368 call_number || '|' ||
1369 copy_number || '|' ||
1374 deposit_amount || '|' ||
1379 circ_modifier || '|' ||
1380 circ_as_type || '|' ||
1381 alert_message || '|' ||
1386 -- RAISE NOTICE 'XPath: %', xpath;
1390 FROM oils_xpath_table( 'id', 'marc', 'vandelay.queued_bib_record', xpath, 'id = ' || import_id )
1391 AS t( id INT, ol TEXT, clib TEXT, cn TEXT, cnum TEXT, cs TEXT, cl TEXT, circ TEXT,
1392 dep TEXT, dep_amount TEXT, r TEXT, hold TEXT, pr TEXT, bc TEXT, circ_mod TEXT,
1393 circ_as TEXT, amessage TEXT, note TEXT, pnote TEXT, opac_vis TEXT )
1396 tmp_attr_set.pr = REGEXP_REPLACE(tmp_attr_set.pr, E'[^0-9\\.]', '', 'g');
1397 tmp_attr_set.dep_amount = REGEXP_REPLACE(tmp_attr_set.dep_amount, E'[^0-9\\.]', '', 'g');
1399 tmp_attr_set.pr := NULLIF( tmp_attr_set.pr, '' );
1400 tmp_attr_set.dep_amount := NULLIF( tmp_attr_set.dep_amount, '' );
1402 SELECT id INTO attr_set.owning_lib FROM actor.org_unit WHERE shortname = UPPER(tmp_attr_set.ol); -- INT
1403 SELECT id INTO attr_set.circ_lib FROM actor.org_unit WHERE shortname = UPPER(tmp_attr_set.clib); -- INT
1404 SELECT id INTO attr_set.status FROM config.copy_status WHERE LOWER(name) = LOWER(tmp_attr_set.cs); -- INT
1406 SELECT id INTO attr_set.location
1407 FROM asset.copy_location
1408 WHERE LOWER(name) = LOWER(tmp_attr_set.cl)
1409 AND asset.copy_location.owning_lib = COALESCE(attr_set.owning_lib, attr_set.circ_lib); -- INT
1411 attr_set.circulate :=
1412 LOWER( SUBSTRING( tmp_attr_set.circ, 1, 1)) IN ('t','y','1')
1413 OR LOWER(tmp_attr_set.circ) = 'circulating'; -- BOOL
1416 LOWER( SUBSTRING( tmp_attr_set.dep, 1, 1 ) ) IN ('t','y','1')
1417 OR LOWER(tmp_attr_set.dep) = 'deposit'; -- BOOL
1419 attr_set.holdable :=
1420 LOWER( SUBSTRING( tmp_attr_set.hold, 1, 1 ) ) IN ('t','y','1')
1421 OR LOWER(tmp_attr_set.hold) = 'holdable'; -- BOOL
1423 attr_set.opac_visible :=
1424 LOWER( SUBSTRING( tmp_attr_set.opac_vis, 1, 1 ) ) IN ('t','y','1')
1425 OR LOWER(tmp_attr_set.opac_vis) = 'visible'; -- BOOL
1428 LOWER( SUBSTRING( tmp_attr_set.r, 1, 1 ) ) IN ('t','y','1')
1429 OR LOWER(tmp_attr_set.r) = 'reference'; -- BOOL
1431 attr_set.copy_number := tmp_attr_set.cnum::INT; -- INT,
1432 attr_set.deposit_amount := tmp_attr_set.dep_amount::NUMERIC(6,2); -- NUMERIC(6,2),
1433 attr_set.price := tmp_attr_set.pr::NUMERIC(8,2); -- NUMERIC(8,2),
1435 attr_set.call_number := tmp_attr_set.cn; -- TEXT
1436 attr_set.barcode := tmp_attr_set.bc; -- TEXT,
1437 attr_set.circ_modifier := tmp_attr_set.circ_mod; -- TEXT,
1438 attr_set.circ_as_type := tmp_attr_set.circ_as; -- TEXT,
1439 attr_set.alert_message := tmp_attr_set.amessage; -- TEXT,
1440 attr_set.pub_note := tmp_attr_set.note; -- TEXT,
1441 attr_set.priv_note := tmp_attr_set.pnote; -- TEXT,
1442 attr_set.alert_message := tmp_attr_set.amessage; -- TEXT,
1444 RETURN NEXT attr_set;
1453 $$ LANGUAGE PLPGSQL;
1456 CREATE OR REPLACE FUNCTION vandelay.ingest_bib_marc ( ) RETURNS TRIGGER AS $$
1462 FOR adef IN SELECT * FROM vandelay.bib_attr_definition LOOP
1464 SELECT extract_marc_field('vandelay.queued_bib_record', id, adef.xpath, adef.remove) INTO value FROM vandelay.queued_bib_record WHERE id = NEW.id;
1465 IF (value IS NOT NULL AND value <> '') THEN
1466 INSERT INTO vandelay.queued_bib_record_attr (record, field, attr_value) VALUES (NEW.id, adef.id, value);
1473 $$ LANGUAGE PLPGSQL;
1475 CREATE OR REPLACE FUNCTION vandelay.ingest_bib_items ( ) RETURNS TRIGGER AS $func$
1478 item_data vandelay.import_item%ROWTYPE;
1481 SELECT item_attr_def INTO attr_def FROM vandelay.bib_queue WHERE id = NEW.queue;
1483 FOR item_data IN SELECT * FROM vandelay.ingest_items( NEW.id::BIGINT, attr_def ) LOOP
1484 INSERT INTO vandelay.import_item (
1508 item_data.definition,
1509 item_data.owning_lib,
1511 item_data.call_number,
1512 item_data.copy_number,
1515 item_data.circulate,
1517 item_data.deposit_amount,
1522 item_data.circ_modifier,
1523 item_data.circ_as_type,
1524 item_data.alert_message,
1526 item_data.priv_note,
1527 item_data.opac_visible
1533 $func$ LANGUAGE PLPGSQL;
1535 CREATE OR REPLACE FUNCTION vandelay.match_bib_record ( ) RETURNS TRIGGER AS $func$
1544 DELETE FROM vandelay.bib_match WHERE queued_record = NEW.id;
1546 SELECT * INTO attr_def FROM vandelay.bib_attr_definition WHERE xpath = '//*[@tag="901"]/*[@code="c"]' ORDER BY id LIMIT 1;
1548 IF attr_def IS NOT NULL AND attr_def.id IS NOT NULL THEN
1549 id_value := extract_marc_field('vandelay.queued_bib_record', NEW.id, attr_def.xpath, attr_def.remove);
1551 IF id_value IS NOT NULL AND id_value <> '' AND id_value ~ $r$^\d+$$r$ THEN
1552 SELECT id INTO exact_id FROM biblio.record_entry WHERE id = id_value::BIGINT AND NOT deleted;
1553 SELECT * INTO attr FROM vandelay.queued_bib_record_attr WHERE record = NEW.id and field = attr_def.id LIMIT 1;
1554 IF exact_id IS NOT NULL THEN
1555 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('id', attr.id, NEW.id, exact_id);
1560 IF exact_id IS NULL THEN
1561 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
1563 -- All numbers? check for an id match
1564 IF (attr.attr_value ~ $r$^\d+$$r$) THEN
1565 FOR eg_rec IN SELECT * FROM biblio.record_entry WHERE id = attr.attr_value::BIGINT AND deleted IS FALSE LOOP
1566 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('id', attr.id, NEW.id, eg_rec.id);
1570 -- Looks like an ISBN? check for an isbn match
1571 IF (attr.attr_value ~* $r$^[0-9x]+$$r$ AND character_length(attr.attr_value) IN (10,13)) THEN
1572 FOR eg_rec IN EXECUTE $$SELECT * FROM metabib.full_rec fr WHERE fr.value LIKE evergreen.lowercase('$$ || attr.attr_value || $$%') AND fr.tag = '020' AND fr.subfield = 'a'$$ LOOP
1573 PERFORM id FROM biblio.record_entry WHERE id = eg_rec.record AND deleted IS FALSE;
1575 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('isbn', attr.id, NEW.id, eg_rec.record);
1579 -- subcheck for isbn-as-tcn
1580 FOR eg_rec IN SELECT * FROM biblio.record_entry WHERE tcn_value = 'i' || attr.attr_value AND deleted IS FALSE LOOP
1581 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('tcn_value', attr.id, NEW.id, eg_rec.id);
1585 -- check for an OCLC tcn_value match
1586 IF (attr.attr_value ~ $r$^o\d+$$r$) THEN
1587 FOR eg_rec IN SELECT * FROM biblio.record_entry WHERE tcn_value = regexp_replace(attr.attr_value,'^o','ocm') AND deleted IS FALSE LOOP
1588 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('tcn_value', attr.id, NEW.id, eg_rec.id);
1592 -- check for a direct tcn_value match
1593 FOR eg_rec IN SELECT * FROM biblio.record_entry WHERE tcn_value = attr.attr_value AND deleted IS FALSE LOOP
1594 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('tcn_value', attr.id, NEW.id, eg_rec.id);
1597 -- check for a direct item barcode match
1600 FROM biblio.record_entry b
1601 JOIN asset.call_number cn ON (cn.record = b.id)
1602 JOIN asset.copy cp ON (cp.call_number = cn.id)
1603 WHERE cp.barcode = attr.attr_value AND cp.deleted IS FALSE
1605 INSERT INTO vandelay.bib_match (field_type, matched_attr, queued_record, eg_record) VALUES ('id', attr.id, NEW.id, eg_rec.id);
1613 $func$ LANGUAGE PLPGSQL;
1615 CREATE OR REPLACE FUNCTION vandelay.cleanup_bib_marc ( ) RETURNS TRIGGER AS $$
1617 DELETE FROM vandelay.queued_bib_record_attr WHERE record = OLD.id;
1618 DELETE FROM vandelay.import_item WHERE record = OLD.id;
1620 IF TG_OP = 'UPDATE' THEN
1625 $$ LANGUAGE PLPGSQL;
1627 CREATE TRIGGER cleanup_bib_trigger
1628 BEFORE UPDATE OR DELETE ON vandelay.queued_bib_record
1629 FOR EACH ROW EXECUTE PROCEDURE vandelay.cleanup_bib_marc();
1631 CREATE TRIGGER ingest_bib_trigger
1632 AFTER INSERT OR UPDATE ON vandelay.queued_bib_record
1633 FOR EACH ROW EXECUTE PROCEDURE vandelay.ingest_bib_marc();
1635 CREATE TRIGGER ingest_item_trigger
1636 AFTER INSERT OR UPDATE ON vandelay.queued_bib_record
1637 FOR EACH ROW EXECUTE PROCEDURE vandelay.ingest_bib_items();
1639 CREATE TRIGGER zz_match_bibs_trigger
1640 AFTER INSERT OR UPDATE ON vandelay.queued_bib_record
1641 FOR EACH ROW EXECUTE PROCEDURE vandelay.match_bib_record();
1644 /* Authority stuff down here */
1645 ---------------------------------------
1646 CREATE TABLE vandelay.authority_attr_definition (
1647 id SERIAL PRIMARY KEY,
1648 code TEXT UNIQUE NOT NULL,
1650 xpath TEXT NOT NULL,
1651 remove TEXT NOT NULL DEFAULT ''
1654 CREATE TABLE vandelay.authority_queue (
1655 queue_type TEXT NOT NULL DEFAULT 'authority' CHECK (queue_type = 'authority'),
1656 CONSTRAINT vand_authority_queue_name_once_per_owner_const UNIQUE (owner,name,queue_type)
1657 ) INHERITS (vandelay.queue);
1658 ALTER TABLE vandelay.authority_queue ADD PRIMARY KEY (id);
1660 CREATE TABLE vandelay.queued_authority_record (
1661 queue INT NOT NULL REFERENCES vandelay.authority_queue (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
1662 imported_as INT REFERENCES authority.record_entry (id) DEFERRABLE INITIALLY DEFERRED,
1663 import_error TEXT REFERENCES vandelay.import_error (code) ON DELETE SET NULL ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
1665 ) INHERITS (vandelay.queued_record);
1666 ALTER TABLE vandelay.queued_authority_record ADD PRIMARY KEY (id);
1667 CREATE INDEX queued_authority_record_queue_idx ON vandelay.queued_authority_record (queue);
1669 CREATE TABLE vandelay.queued_authority_record_attr (
1670 id BIGSERIAL PRIMARY KEY,
1671 record BIGINT NOT NULL REFERENCES vandelay.queued_authority_record (id) DEFERRABLE INITIALLY DEFERRED,
1672 field INT NOT NULL REFERENCES vandelay.authority_attr_definition (id) DEFERRABLE INITIALLY DEFERRED,
1673 attr_value TEXT NOT NULL
1675 CREATE INDEX queued_authority_record_attr_record_idx ON vandelay.queued_authority_record_attr (record);
1677 CREATE TABLE vandelay.authority_match (
1678 id BIGSERIAL PRIMARY KEY,
1679 matched_set INT REFERENCES vandelay.match_set (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
1680 queued_record BIGINT REFERENCES vandelay.queued_authority_record (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
1681 eg_record BIGINT REFERENCES authority.record_entry (id) DEFERRABLE INITIALLY DEFERRED,
1682 quality INT NOT NULL DEFAULT 0
1685 CREATE OR REPLACE FUNCTION vandelay.ingest_authority_marc ( ) RETURNS TRIGGER AS $$
1691 FOR adef IN SELECT * FROM vandelay.authority_attr_definition LOOP
1693 SELECT extract_marc_field('vandelay.queued_authority_record', id, adef.xpath, adef.remove) INTO value FROM vandelay.queued_authority_record WHERE id = NEW.id;
1694 IF (value IS NOT NULL AND value <> '') THEN
1695 INSERT INTO vandelay.queued_authority_record_attr (record, field, attr_value) VALUES (NEW.id, adef.id, value);
1702 $$ LANGUAGE PLPGSQL;
1704 CREATE OR REPLACE FUNCTION vandelay.cleanup_authority_marc ( ) RETURNS TRIGGER AS $$
1706 DELETE FROM vandelay.queued_authority_record_attr WHERE record = OLD.id;
1707 IF TG_OP = 'UPDATE' THEN
1712 $$ LANGUAGE PLPGSQL;
1714 CREATE TRIGGER cleanup_authority_trigger
1715 BEFORE UPDATE OR DELETE ON vandelay.queued_authority_record
1716 FOR EACH ROW EXECUTE PROCEDURE vandelay.cleanup_authority_marc();
1718 CREATE TRIGGER ingest_authority_trigger
1719 AFTER INSERT OR UPDATE ON vandelay.queued_authority_record
1720 FOR EACH ROW EXECUTE PROCEDURE vandelay.ingest_authority_marc();
1722 CREATE OR REPLACE FUNCTION vandelay.overlay_authority_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
1724 merge_profile vandelay.merge_profile%ROWTYPE;
1725 dyn_profile vandelay.compile_profile%ROWTYPE;
1734 SELECT b.marc INTO eg_marc
1735 FROM authority.record_entry b
1736 JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id)
1739 SELECT q.marc INTO v_marc
1740 FROM vandelay.queued_record q
1741 JOIN vandelay.authority_match m ON (m.queued_record = q.id AND q.id = import_id)
1744 IF eg_marc IS NULL OR v_marc IS NULL THEN
1745 -- RAISE NOTICE 'no marc for vandelay or authority record';
1749 dyn_profile := vandelay.compile_profile( v_marc );
1751 IF merge_profile_id IS NOT NULL THEN
1752 SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
1754 dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
1755 dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
1756 dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
1757 dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
1761 IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
1762 -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
1766 IF dyn_profile.replace_rule <> '' THEN
1767 source_marc = v_marc;
1768 target_marc = eg_marc;
1769 replace_rule = dyn_profile.replace_rule;
1771 source_marc = eg_marc;
1772 target_marc = v_marc;
1773 replace_rule = dyn_profile.preserve_rule;
1776 UPDATE authority.record_entry
1777 SET marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
1781 UPDATE vandelay.queued_authority_record
1782 SET imported_as = eg_id,
1784 WHERE id = import_id;
1788 -- RAISE NOTICE 'update of authority.record_entry failed';
1793 $$ LANGUAGE PLPGSQL;
1795 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_authority_record ( import_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
1800 SELECT COUNT(*) INTO match_count FROM vandelay.authority_match WHERE queued_record = import_id;
1802 IF match_count <> 1 THEN
1803 -- RAISE NOTICE 'not an exact match';
1807 SELECT m.eg_record INTO eg_id
1808 FROM vandelay.authority_match m
1809 WHERE m.queued_record = import_id
1812 IF eg_id IS NULL THEN
1816 RETURN vandelay.overlay_authority_record( import_id, eg_id, merge_profile_id );
1818 $$ LANGUAGE PLPGSQL;
1820 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_authority_queue ( queue_id BIGINT, merge_profile_id INT ) RETURNS SETOF BIGINT AS $$
1822 queued_record vandelay.queued_authority_record%ROWTYPE;
1825 FOR queued_record IN SELECT * FROM vandelay.queued_authority_record WHERE queue = queue_id AND import_time IS NULL LOOP
1827 IF vandelay.auto_overlay_authority_record( queued_record.id, merge_profile_id ) THEN
1828 RETURN NEXT queued_record.id;
1836 $$ LANGUAGE PLPGSQL;
1838 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_authority_queue ( queue_id BIGINT ) RETURNS SETOF BIGINT AS $$
1839 SELECT * FROM vandelay.auto_overlay_authority_queue( $1, NULL );
1843 -- Vandelay (for importing and exporting records) 012.schema.vandelay.sql
1844 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (1, 'title', oils_i18n_gettext(1, 'vqbrad', 'Title of work', 'description'),'//*[@tag="245"]/*[contains("abcmnopr",@code)]');
1845 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (2, 'author', oils_i18n_gettext(1, 'vqbrad', 'Author of work', 'description'),'//*[@tag="100" or @tag="110" or @tag="113"]/*[contains("ad",@code)]');
1846 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (3, 'language', oils_i18n_gettext(3, 'vqbrad', 'Language of work', 'description'),'//*[@tag="240"]/*[@code="l"][1]');
1847 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (4, 'pagination', oils_i18n_gettext(4, 'vqbrad', 'Pagination', 'description'),'//*[@tag="300"]/*[@code="a"][1]');
1848 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident, remove ) VALUES (5, 'isbn',oils_i18n_gettext(5, 'vqbrad', 'ISBN', 'description'),'//*[@tag="020"]/*[@code="a"]', TRUE, $r$(?:-|\s.+$)$r$);
1849 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident, remove ) VALUES (6, 'issn',oils_i18n_gettext(6, 'vqbrad', 'ISSN', 'description'),'//*[@tag="022"]/*[@code="a"]', TRUE, $r$(?:-|\s.+$)$r$);
1850 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (7, 'price',oils_i18n_gettext(7, 'vqbrad', 'Price', 'description'),'//*[@tag="020" or @tag="022"]/*[@code="c"][1]');
1851 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident ) VALUES (8, 'rec_identifier',oils_i18n_gettext(8, 'vqbrad', 'Accession Number', 'description'),'//*[@tag="001"]', TRUE);
1852 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident ) VALUES (9, 'eg_tcn',oils_i18n_gettext(9, 'vqbrad', 'TCN Value', 'description'),'//*[@tag="901"]/*[@code="a"]', TRUE);
1853 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident ) VALUES (10, 'eg_tcn_source',oils_i18n_gettext(10, 'vqbrad', 'TCN Source', 'description'),'//*[@tag="901"]/*[@code="b"]', TRUE);
1854 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident ) VALUES (11, 'eg_identifier',oils_i18n_gettext(11, 'vqbrad', 'Internal ID', 'description'),'//*[@tag="901"]/*[@code="c"]', TRUE);
1855 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (12, 'publisher',oils_i18n_gettext(12, 'vqbrad', 'Publisher', 'description'),'//*[@tag="260"]/*[@code="b"][1]');
1856 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, remove ) VALUES (13, 'pubdate',oils_i18n_gettext(13, 'vqbrad', 'Publication Date', 'description'),'//*[@tag="260"]/*[@code="c"][1]',$r$\D$r$);
1857 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (14, 'edition',oils_i18n_gettext(14, 'vqbrad', 'Edition', 'description'),'//*[@tag="250"]/*[@code="a"][1]');
1859 --INSERT INTO vandelay.import_item_attr_definition (
1860 -- owner, name, tag, owning_lib, circ_lib, location,
1861 -- call_number, circ_modifier, barcode, price, copy_number,
1862 -- circulate, ref, holdable, opac_visible, status
1865 -- 'Evergreen 852 export format',
1867 -- '[@code = "b"][1]',
1868 -- '[@code = "b"][2]',
1875 -- '[@code = "x" and text() = "circulating"]',
1876 -- '[@code = "x" and text() = "reference"]',
1877 -- '[@code = "x" and text() = "holdable"]',
1878 -- '[@code = "x" and text() = "visible"]',
1882 --INSERT INTO vandelay.import_item_attr_definition (
1895 -- 'Unicorn Import format -- 999',
1906 --INSERT INTO vandelay.authority_attr_definition ( code, description, xpath, ident ) VALUES ('rec_identifier','Identifier','//*[@tag="001"]', TRUE);