From 21fbb7e9976d5986dba0e9b7f3e104f8fa74c5e4 Mon Sep 17 00:00:00 2001 From: Jason Etheridge Date: Fri, 8 Jun 2018 15:51:25 -0400 Subject: [PATCH 1/1] strip_tag function Signed-off-by: Jason Etheridge --- sql/base/base.sql | 39 +++++++++++++++++++++++++++++++++++++++ 1 files changed, 39 insertions(+), 0 deletions(-) diff --git a/sql/base/base.sql b/sql/base/base.sql index 2741da9..15bb67a 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -3469,6 +3469,45 @@ BEGIN 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/(.........)./${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 $$ -- 1.7.2.5