From 582c569c90fef77c0460057dae2303edcc29a31d Mon Sep 17 00:00:00 2001 From: Jason Etheridge Date: Tue, 9 Jun 2020 15:35:29 -0400 Subject: [PATCH] koha function for updating the 003 tag Signed-off-by: Jason Etheridge --- kmig.d/sql/init/030-marc_functions.sql | 50 ++++++++++++++++++++++++++++++++ 1 files changed, 50 insertions(+), 0 deletions(-) diff --git a/kmig.d/sql/init/030-marc_functions.sql b/kmig.d/sql/init/030-marc_functions.sql index 08d5e8e..35df163 100644 --- a/kmig.d/sql/init/030-marc_functions.sql +++ b/kmig.d/sql/init/030-marc_functions.sql @@ -24,4 +24,54 @@ CREATE FUNCTION $ DELIMITER ; +-- pass it the biblionumber and the 003 value (MARC org code) and it'll set the 003 +-- to the passed value for the referenced bib +DROP FUNCTION IF EXISTS m_update_003; +DELIMITER $ +CREATE FUNCTION + m_update_003(bnumber INTEGER, marc_org_code TEXT COLLATE utf8mb4_unicode_ci) + RETURNS BOOLEAN + DETERMINISTIC + BEGIN + DECLARE ldr TEXT COLLATE utf8mb4_unicode_ci DEFAULT NULL; + DECLARE tag003_count INT DEFAULT NULL; + DECLARE marcxml TEXT COLLATE utf8mb4_unicode_ci DEFAULT NULL; + + SELECT metadata INTO marcxml FROM biblio_metadata WHERE biblionumber = bnumber; + SELECT ExtractValue(metadata,'//leader') INTO ldr FROM biblio_metadata WHERE biblionumber = bnumber; + SELECT ExtractValue(metadata,'count(//controlfield[@tag="003"])') INTO tag003_count FROM biblio_metadata WHERE biblionumber = bnumber; + + IF NULLIF(ldr,'') IS NULL THEN -- we need a leader + RETURN FALSE; + END IF; + IF tag003_count > 1 THEN -- we need one or zero of these; it shouldn't be repeatable + RETURN FALSE; + END IF; + + -- handle 003 (insert or edit) + IF tag003_count = 0 THEN + SET marcxml = UpdateXML( + marcxml, + '//leader', + CONCAT( + '',ldr,'\n', + ' ',marc_org_code,'' + ) + ); + ELSE + SET marcxml = UpdateXML( + marcxml, + '//controlfield[@tag="003"]', + CONCAT( + '',marc_org_code,'' + ) + ); + END IF; + + UPDATE biblio_metadata SET metadata = marcxml where biblionumber = bnumber; + RETURN TRUE; + + END +$ +DELIMITER ; -- 1.7.2.5