]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0519.schema.extract_all_fixed_fields_from_leader.sql
Add default Vandelay match set to schema
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0519.schema.extract_all_fixed_fields_from_leader.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0519'); -- miker
4
5 CREATE OR REPLACE FUNCTION vandelay.marc21_extract_all_fixed_fields( marc TEXT ) RETURNS SETOF biblio.record_ff_map AS $func$
6 DECLARE
7     tag_data    TEXT;
8     rtype       TEXT;
9     ff_pos      RECORD;
10     output      biblio.record_ff_map%ROWTYPE;
11 BEGIN
12     rtype := (vandelay.marc21_record_type( marc )).code;
13
14     FOR ff_pos IN SELECT * FROM config.marc21_ff_pos_map WHERE rec_type = rtype ORDER BY tag DESC LOOP
15         output.ff_name  := ff_pos.fixed_field;
16         output.ff_value := NULL;
17
18         IF ff_pos.tag = 'ldr' THEN
19             output.ff_value := oils_xpath_string('//*[local-name()="leader"]', marc);
20             IF output.ff_value IS NOT NULL THEN
21                 output.ff_value := SUBSTRING( output.ff_value, ff_pos.start_pos + 1, ff_pos.length );
22                 RETURN NEXT output;
23                 output.ff_value := NULL;
24             END IF;
25         ELSE
26             FOR tag_data IN SELECT value FROM UNNEST( oils_xpath( '//*[@tag="' || UPPER(ff_pos.tag) || '"]/text()', marc ) ) x(value) LOOP
27                 output.ff_value := SUBSTRING( tag_data, ff_pos.start_pos + 1, ff_pos.length );
28                 IF output.ff_value IS NULL THEN output.ff_value := REPEAT( ff_pos.default_val, ff_pos.length ); END IF;
29                 RETURN NEXT output;
30                 output.ff_value := NULL;
31             END LOOP;
32         END IF;
33     
34     END LOOP;
35
36     RETURN;
37 END;
38 $func$ LANGUAGE PLPGSQL;
39
40 COMMIT;
41