return $barcode . $checkdigit;
$$ LANGUAGE PLPERLU STRICT STABLE;
+-- add_mod16_checkdigit
+-- $barcode source barcode
+--
+-- https://www.activebarcode.com/codes/checkdigit/modulo16.html
+
+CREATE OR REPLACE FUNCTION migration_tools.add_mod16_checkdigit (TEXT) RETURNS TEXT AS $$
+ my $barcode = shift;
+
+ my @digits = split //, $barcode;
+ my $total = 0;
+ foreach $digit (@digits) {
+ if ($digit =~ /[0-9]/) { $total += $digit;
+ } elsif ($digit eq '-') { $total += 10;
+ } elsif ($digit eq '$') { $total += 11;
+ } elsif ($digit eq ':') { $total += 12;
+ } elsif ($digit eq '/') { $total += 13;
+ } elsif ($digit eq '.') { $total += 14;
+ } elsif ($digit eq '+') { $total += 15;
+ } elsif ($digit eq 'A') { $total += 16;
+ } elsif ($digit eq 'B') { $total += 17;
+ } elsif ($digit eq 'C') { $total += 18;
+ } elsif ($digit eq 'D') { $total += 19;
+ } else { die "invalid digit <$digit>";
+ }
+ }
+ my $remainder = $total % 16;
+ my $difference = 16 - $remainder;
+ my $checkdigit;
+ if ($difference < 10) { $checkdigit = $difference;
+ } elsif ($difference == 10) { $checkdigit = '-';
+ } elsif ($difference == 11) { $checkdigit = '$';
+ } elsif ($difference == 12) { $checkdigit = ':';
+ } elsif ($difference == 13) { $checkdigit = '/';
+ } elsif ($difference == 14) { $checkdigit = '.';
+ } elsif ($difference == 15) { $checkdigit = '+';
+ } else { die "error calculating checkdigit";
+ }
+
+ return $barcode . $checkdigit;
+$$ LANGUAGE PLPERLU STRICT STABLE;
+
CREATE OR REPLACE FUNCTION migration_tools.attempt_phone (TEXT,TEXT) RETURNS TEXT AS $$
DECLARE
phone TEXT := $1;
END
$$ LANGUAGE plpgsql;
+-- yet another subfield 9 function, this one only adds the $9 if the ind1 = 1 or 4 and ind2 = 0 or 1
+DROP FUNCTION IF EXISTS migration_tools.strict_add_sf9(TEXT,TEXT);
+CREATE OR REPLACE FUNCTION migration_tools.strict_add_sf9(marc TEXT, new_9 TEXT)
+ RETURNS TEXT
+ LANGUAGE plperlu
+AS $function$
+use strict;
+use warnings;
+
+use MARC::Record;
+use MARC::File::XML (BinaryEncoding => 'utf8');
+
+binmode(STDERR, ':bytes');
+binmode(STDOUT, ':utf8');
+binmode(STDERR, ':utf8');
+
+my $marc_xml = shift;
+my $new_9_to_set = shift;
+
+$marc_xml =~ s/(<leader>.........)./${1}a/;
+
+eval {
+ $marc_xml = MARC::Record->new_from_xml($marc_xml);
+};
+if ($@) {
+ #elog("could not parse $bibid: $@\n");
+ import MARC::File::XML (BinaryEncoding => 'utf8');
+ return $marc_xml;
+}
+
+my @uris = $marc_xml->field('856');
+return $marc_xml->as_xml_record() unless @uris;
+
+foreach my $field (@uris) {
+ my $ind1 = $field->indicator('1');
+ if (!defined $ind1) { next; }
+ if ($ind1 ne '1' && $ind1 ne '4') { next; }
+ my $ind2 = $field->indicator('2');
+ if (!defined $ind2) { next; }
+ if ($ind2 ne '0' && $ind2 ne '1') { next; }
+ $field->add_subfields( '9' => $new_9_to_set );
+}
+
+return $marc_xml->as_xml_record();
+
+$function$;
+
+-- yet another subfield 9 function, this one only adds the $9 and forces
+-- ind1 = 4 if not already ind1 = 1 or 4 and ind2 = 0 if not already ind2 = 0 or 1
+DROP FUNCTION IF EXISTS migration_tools.force_add_sf9(TEXT,TEXT);
+CREATE OR REPLACE FUNCTION migration_tools.force_add_sf9(marc TEXT, new_9 TEXT)
+ RETURNS TEXT
+ LANGUAGE plperlu
+AS $function$
+use strict;
+use warnings;
+
+use MARC::Record;
+use MARC::File::XML (BinaryEncoding => 'utf8');
+
+binmode(STDERR, ':bytes');
+binmode(STDOUT, ':utf8');
+binmode(STDERR, ':utf8');
+
+my $marc_xml = shift;
+my $new_9_to_set = shift;
+
+$marc_xml =~ s/(<leader>.........)./${1}a/;
+
+eval {
+ $marc_xml = MARC::Record->new_from_xml($marc_xml);
+};
+if ($@) {
+ #elog("could not parse $bibid: $@\n");
+ import MARC::File::XML (BinaryEncoding => 'utf8');
+ return $marc_xml;
+}
+
+my @uris = $marc_xml->field('856');
+return $marc_xml->as_xml_record() unless @uris;
+
+foreach my $field (@uris) {
+ my $ind1 = $field->indicator('1');
+ if (!defined $ind1) { next; }
+ if ($ind1 ne '1' && $ind1 ne '4') { $field->set_indicator(1,'4'); }
+ my $ind2 = $field->indicator('2');
+ if (!defined $ind2) { next; }
+ if ($ind2 ne '0' && $ind2 ne '1') { $field->set_indicator(2,'0'); }
+ $field->add_subfields( '9' => $new_9_to_set );
+}
+
+return $marc_xml->as_xml_record();
+
+$function$;
+
-- alternate adding subfield 9 function in that it adds them to existing tags where the 856$u matches a correct value only
DROP FUNCTION IF EXISTS migration_tools.add_sf9(TEXT,TEXT,TEXT);
CREATE OR REPLACE FUNCTION migration_tools.add_sf9(marc TEXT, partial_u TEXT, new_9 TEXT)
END;
$BODY$ LANGUAGE plpgsql;
+-- strip marc tag
+DROP FUNCTION IF EXISTS migration_tools.strip_tag(TEXT,TEXT);
+CREATE OR REPLACE FUNCTION migration_tools.strip_tag(marc TEXT, tag TEXT)
+ RETURNS TEXT
+ LANGUAGE plperlu
+AS $function$
+use strict;
+use warnings;
+
+use MARC::Record;
+use MARC::File::XML (BinaryEncoding => 'utf8');
+
+binmode(STDERR, ':bytes');
+binmode(STDOUT, ':utf8');
+binmode(STDERR, ':utf8');
+
+my $marc_xml = shift;
+my $tag = shift;
+
+$marc_xml =~ s/(<leader>.........)./${1}a/;
+
+eval {
+ $marc_xml = MARC::Record->new_from_xml($marc_xml);
+};
+if ($@) {
+ #elog("could not parse $bibid: $@\n");
+ import MARC::File::XML (BinaryEncoding => 'utf8');
+ return $marc_xml;
+}
+
+my @fields = $marc_xml->field($tag);
+return $marc_xml->as_xml_record() unless @fields;
+
+$marc_xml->delete_fields(@fields);
+
+return $marc_xml->as_xml_record();
+
+$function$;
+
-- convenience function for linking to the item staging table
CREATE OR REPLACE FUNCTION migration_tools.handle_item_barcode (TEXT,TEXT,TEXT,TEXT,BOOLEAN) RETURNS VOID AS $$
END;
$$ LANGUAGE PLPGSQL STRICT VOLATILE;
+
+DROP FUNCTION IF EXISTS migration_tools.btrim_lcolumns(TEXT,TEXT);
+CREATE OR REPLACE FUNCTION migration_tools.btrim_lcolumns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN
+ LANGUAGE plpgsql
+AS $function$
+DECLARE
+ c_name TEXT;
+BEGIN
+
+ FOR c_name IN SELECT column_name FROM information_schema.columns WHERE
+ table_name = t_name
+ AND table_schema = s_name
+ AND (data_type='text' OR data_type='character varying')
+ AND column_name like 'l_%'
+ LOOP
+ EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = BTRIM(' || c_name || ')');
+ END LOOP;
+
+ RETURN TRUE;
+END
+$function$;
+
+DROP FUNCTION IF EXISTS migration_tools.null_empty_lcolumns(TEXT,TEXT);
+CREATE OR REPLACE FUNCTION migration_tools.null_empty_lcolumns(s_name TEXT, t_name TEXT) RETURNS BOOLEAN
+ LANGUAGE plpgsql
+AS $function$
+DECLARE
+ c_name TEXT;
+BEGIN
+
+ FOR c_name IN SELECT column_name FROM information_schema.columns WHERE
+ table_name = t_name
+ AND table_schema = s_name
+ AND (data_type='text' OR data_type='character varying')
+ AND column_name like 'l_%'
+ LOOP
+ EXECUTE FORMAT('UPDATE ' || s_name || '.' || t_name || ' SET ' || c_name || ' = NULL WHERE ' || c_name || ' = '''' ');
+ END LOOP;
+
+ RETURN TRUE;
+END
+$function$;
+