m_insert_tag for kmig
authorJason Etheridge <jason@equinoxinitiative.org>
Mon, 15 Jun 2020 15:53:00 +0000 (11:53 -0400)
committerJason Etheridge <jason@equinoxinitiative.org>
Mon, 15 Jun 2020 15:53:00 +0000 (11:53 -0400)
Signed-off-by: Jason Etheridge <jason@equinoxinitiative.org>

kmig.d/sql/init/030-marc_functions.sql

index 35df163..cb917f2 100644 (file)
@@ -75,3 +75,41 @@ CREATE FUNCTION
     END
 $
 DELIMITER ;
+
+-- Pass it the biblionumber, tag number, subfield, value and it'll add a MARC field to the end of the record accordingly
+-- Be sure to escape the value if needed
+-- Example: SELECT m_insert_tag(1,'909','a','foo');
+DROP FUNCTION IF EXISTS m_insert_tag;
+DELIMITER $
+CREATE FUNCTION
+   m_insert_tag(bnumber INTEGER, tag TEXT COLLATE utf8mb4_unicode_ci, subfield TEXT COLLATE utf8mb4_unicode_ci, value TEXT COLLATE utf8mb4_unicode_ci)
+   RETURNS BOOLEAN
+   DETERMINISTIC
+    BEGIN
+        DECLARE marcxml TEXT COLLATE utf8mb4_unicode_ci DEFAULT NULL;
+
+        SELECT metadata INTO marcxml FROM biblio_metadata WHERE biblionumber = bnumber;
+
+        IF NULLIF(marcxml,'') IS NULL THEN -- whaaa?
+            RETURN FALSE;
+        END IF;
+
+        SET marcxml = replace(marcxml,'</record>',
+            CONCAT(
+                 '  <datafield tag="'
+                ,tag
+                ,'">\n    <subfield code="'
+                ,subfield
+                ,'">'
+                ,value
+                ,'</subfield>\n  </datafield>\n'
+                ,'</record>'
+            )
+        );
+
+        UPDATE biblio_metadata SET metadata = marcxml where biblionumber = bnumber;
+        RETURN TRUE;
+
+    END
+$
+DELIMITER ;