]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0473.schema.vandelay-replace_field.sql
LP#1772955: Only include xacts with balance in summary
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0473.schema.vandelay-replace_field.sql
1
2 BEGIN;
3
4 INSERT INTO config.upgrade_log (version) VALUES ('0473'); -- miker
5
6 CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
7 DECLARE
8     xml_output TEXT;
9     parsed_target TEXT;
10     curr_field TEXT;
11 BEGIN
12
13     parsed_target := vandelay.strip_field( target_xml, ''); -- this dance normalizes the format of the xml for the IF below
14
15     FOR curr_field IN SELECT UNNEST( STRING_TO_ARRAY(field, ',') ) LOOP -- naive split, but it's the same we use in the perl
16
17         xml_output := vandelay.strip_field( parsed_target, curr_field);
18
19         IF xml_output <> parsed_target  AND curr_field ~ E'~' THEN
20             -- we removed something, and there was a regexp restriction in the curr_field definition, so proceed
21             xml_output := vandelay.add_field( xml_output, source_xml, curr_field, 1 );
22         ELSIF curr_field !~ E'~' THEN
23             -- No regexp restriction, add the curr_field
24             xml_output := vandelay.add_field( xml_output, source_xml, curr_field, 0 );
25         END IF;
26
27         parsed_target := xml_output; -- in prep for any following loop iterations
28
29     END LOOP;
30
31     RETURN xml_output;
32 END;
33 $_$ LANGUAGE PLPGSQL;
34
35 COMMIT;