X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=blobdiff_plain;f=sql%2Fbase%2Fbase.sql;h=16e6b3abfb72e45c767b733ede9776b00a30fdaa;hp=1d3507413078f4956a2a98554efb6a8899fc7a8e;hb=a220c56b038fc03e95ba7e5242931cb621cc6ee3;hpb=b1e4d9eaef945bc9ba808510aea183069a00cfdf diff --git a/sql/base/base.sql b/sql/base/base.sql index 1d35074..16e6b3a 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -1833,3 +1833,43 @@ The first argument is an array of tag/subfield specifiers, e.g., ARRAY['001', '2 The second argument is an array of text containing the values to plug into each field. If the value for a given field is NULL or the empty string, it is not inserted. $$; + +CREATE OR REPLACE FUNCTION migration_tools.set_indicator (TEXT, TEXT, INTEGER, CHAR(1)) RETURNS TEXT AS $func$ + +my ($marcxml, $tag, $pos, $value) = @_; + +use MARC::Record; +use MARC::File::XML (BinaryEncoding => 'UTF-8'); +use MARC::Charset; +use strict; + +MARC::Charset->assume_unicode(1); + +elog(ERROR, 'indicator position must be either 1 or 2') unless $pos =~ /^[12]$/; +elog(ERROR, 'MARC tag must be numeric') unless $tag =~ /^\d{3}$/; +elog(ERROR, 'MARC tag must not be control field') if $tag =~ /^00/; +elog(ERROR, 'Value must be exactly one character') unless $value =~ /^.$/; + +my $xml = $marcxml; +eval { + my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8'); + + foreach my $field ($marc->field($tag)) { + $field->update("ind$pos" => $value); + } + $xml = $marc->as_xml_record; + $xml =~ s/^<\?.+?\?>$//mo; + $xml =~ s/\n//sgo; + $xml =~ s/>\s+