From 809d9d65225124d237bd314734e72cdc4998437d Mon Sep 17 00:00:00 2001 From: gmc Date: Tue, 12 Apr 2011 16:53:04 +0000 Subject: [PATCH] add missing upgrade script Signed-off-by: Galen Charlton git-svn-id: svn://svn.open-ils.org/ILS/trunk@20044 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../Pg/upgrade/0509.schema.better_maintain_901.sql | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Open-ILS/src/sql/Pg/upgrade/0509.schema.better_maintain_901.sql diff --git a/Open-ILS/src/sql/Pg/upgrade/0509.schema.better_maintain_901.sql b/Open-ILS/src/sql/Pg/upgrade/0509.schema.better_maintain_901.sql new file mode 100644 index 000000000..6ae79e886 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0509.schema.better_maintain_901.sql @@ -0,0 +1,75 @@ +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0509'); -- gmc + +CREATE OR REPLACE FUNCTION evergreen.xml_escape(str TEXT) RETURNS text AS $$ + SELECT REPLACE(REPLACE(REPLACE($1, + '&', '&'), + '<', '<'), + '>', '>'); +$$ LANGUAGE SQL IMMUTABLE; + +CREATE OR REPLACE FUNCTION maintain_901 () RETURNS TRIGGER AS $func$ +DECLARE + use_id_for_tcn BOOLEAN; +BEGIN + -- Remove any existing 901 fields before we insert the authoritative one + NEW.marc := REGEXP_REPLACE(NEW.marc, E']*?tag="901".+?', '', 'g'); + + IF TG_TABLE_SCHEMA = 'biblio' THEN + -- Set TCN value to record ID? + SELECT enabled FROM config.global_flag INTO use_id_for_tcn + WHERE name = 'cat.bib.use_id_for_tcn'; + + IF use_id_for_tcn = 't' THEN + NEW.tcn_value := NEW.id; + END IF; + + NEW.marc := REGEXP_REPLACE( + NEW.marc, + E'()', + E'' || + '' || evergreen.xml_escape(NEW.tcn_value) || E'' || + '' || evergreen.xml_escape(NEW.tcn_source) || E'' || + '' || NEW.id || E'' || + '' || TG_TABLE_SCHEMA || E'' || + CASE WHEN NEW.owner IS NOT NULL THEN '' || NEW.owner || E'' ELSE '' END || + CASE WHEN NEW.share_depth IS NOT NULL THEN '' || NEW.share_depth || E'' ELSE '' END || + E'\\1' + ); + ELSIF TG_TABLE_SCHEMA = 'authority' THEN + NEW.marc := REGEXP_REPLACE( + NEW.marc, + E'()', + E'' || + '' || NEW.id || E'' || + '' || TG_TABLE_SCHEMA || E'' || + E'\\1' + ); + ELSIF TG_TABLE_SCHEMA = 'serial' THEN + NEW.marc := REGEXP_REPLACE( + NEW.marc, + E'()', + E'' || + '' || NEW.id || E'' || + '' || TG_TABLE_SCHEMA || E'' || + '' || NEW.owning_lib || E'' || + CASE WHEN NEW.record IS NOT NULL THEN '' || NEW.record || E'' ELSE '' END || + E'\\1' + ); + ELSE + NEW.marc := REGEXP_REPLACE( + NEW.marc, + E'()', + E'' || + '' || NEW.id || E'' || + '' || TG_TABLE_SCHEMA || E'' || + E'\\1' + ); + END IF; + + RETURN NEW; +END; +$func$ LANGUAGE PLPGSQL; + +COMMIT; -- 2.11.0