BEGIN; INSERT INTO config.upgrade_log (version) VALUES ('0488'); -- dbs 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']*?\s*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'' || '' || NEW.tcn_value || E'' || '' || 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;