]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0455.schema.acq-tweak-lineitem-attr-splitting.sql
LP#1661688: Add a link and other tweaks to alternate hold pickup feature
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0455.schema.acq-tweak-lineitem-attr-splitting.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0455'); -- gmc
4
5 CREATE OR REPLACE FUNCTION public.ingest_acq_marc ( ) RETURNS TRIGGER AS $function$
6 DECLARE
7         value           TEXT;
8         atype           TEXT;
9         prov            INT;
10         pos             INT;
11         adef            RECORD;
12         xpath_string    TEXT;
13 BEGIN
14         FOR adef IN SELECT *,tableoid FROM acq.lineitem_attr_definition LOOP
15
16                 SELECT relname::TEXT INTO atype FROM pg_class WHERE oid = adef.tableoid;
17
18                 IF (atype NOT IN ('lineitem_usr_attr_definition','lineitem_local_attr_definition')) THEN
19                         IF (atype = 'lineitem_provider_attr_definition') THEN
20                                 SELECT provider INTO prov FROM acq.lineitem_provider_attr_definition WHERE id = adef.id;
21                                 CONTINUE WHEN NEW.provider IS NULL OR prov <> NEW.provider;
22                         END IF;
23                         
24                         IF (atype = 'lineitem_provider_attr_definition') THEN
25                                 SELECT xpath INTO xpath_string FROM acq.lineitem_provider_attr_definition WHERE id = adef.id;
26                         ELSIF (atype = 'lineitem_marc_attr_definition') THEN
27                                 SELECT xpath INTO xpath_string FROM acq.lineitem_marc_attr_definition WHERE id = adef.id;
28                         ELSIF (atype = 'lineitem_generated_attr_definition') THEN
29                                 SELECT xpath INTO xpath_string FROM acq.lineitem_generated_attr_definition WHERE id = adef.id;
30                         END IF;
31
32             xpath_string := REGEXP_REPLACE(xpath_string,$re$//?text\(\)$$re$,'');
33
34             IF (adef.code = 'title' OR adef.code = 'author') THEN
35                 -- title and author should not be split
36                 -- FIXME: once oils_xpath can grok XPATH 2.0 functions, we can use
37                 -- string-join in the xpath and remove this special case
38                         SELECT extract_acq_marc_field(id, xpath_string, adef.remove) INTO value FROM acq.lineitem WHERE id = NEW.id;
39                         IF (value IS NOT NULL AND value <> '') THEN
40                                     INSERT INTO acq.lineitem_attr (lineitem, definition, attr_type, attr_name, attr_value)
41                                     VALUES (NEW.id, adef.id, atype, adef.code, value);
42                 END IF;
43             ELSE
44                 pos := 1;
45
46                 LOOP
47                             SELECT extract_acq_marc_field(id, xpath_string || '[' || pos || ']', adef.remove) INTO value FROM acq.lineitem WHERE id = NEW.id;
48
49                             IF (value IS NOT NULL AND value <> '') THEN
50                                     INSERT INTO acq.lineitem_attr (lineitem, definition, attr_type, attr_name, attr_value)
51                                             VALUES (NEW.id, adef.id, atype, adef.code, value);
52                     ELSE
53                         EXIT;
54                                 END IF;
55
56                     pos := pos + 1;
57                 END LOOP;
58             END IF;
59
60                 END IF;
61
62         END LOOP;
63
64         RETURN NULL;
65 END;
66 $function$ LANGUAGE PLPGSQL;
67
68 COMMIT;