4 CREATE OR REPLACE FUNCTION extract_marc_field_set
5 (TEXT, BIGINT, TEXT, TEXT) RETURNS SETOF TEXT AS $$
13 FROM oils_xpath_table(
14 'id', 'marc', $1, $3, 'id = ' || $2)
15 AS t(id int, t text))x
17 IF $4 IS NOT NULL THEN
18 SELECT INTO output (SELECT regexp_replace(output, $4, '', 'g'));
24 $$ LANGUAGE PLPGSQL IMMUTABLE;
27 CREATE OR REPLACE FUNCTION
28 public.extract_acq_marc_field_set ( BIGINT, TEXT, TEXT)
29 RETURNS SETOF TEXT AS $$
30 SELECT extract_marc_field_set('acq.lineitem', $1, $2, $3);
34 CREATE OR REPLACE FUNCTION public.ingest_acq_marc ( ) RETURNS TRIGGER AS $function$
43 FOR adef IN SELECT *,tableoid FROM acq.lineitem_attr_definition LOOP
45 SELECT relname::TEXT INTO atype FROM pg_class WHERE oid = adef.tableoid;
47 IF (atype NOT IN ('lineitem_usr_attr_definition','lineitem_local_attr_definition')) THEN
48 IF (atype = 'lineitem_provider_attr_definition') THEN
49 SELECT provider INTO prov FROM acq.lineitem_provider_attr_definition WHERE id = adef.id;
50 CONTINUE WHEN NEW.provider IS NULL OR prov <> NEW.provider;
53 IF (atype = 'lineitem_provider_attr_definition') THEN
54 SELECT xpath INTO xpath_string FROM acq.lineitem_provider_attr_definition WHERE id = adef.id;
55 ELSIF (atype = 'lineitem_marc_attr_definition') THEN
56 SELECT xpath INTO xpath_string FROM acq.lineitem_marc_attr_definition WHERE id = adef.id;
57 ELSIF (atype = 'lineitem_generated_attr_definition') THEN
58 SELECT xpath INTO xpath_string FROM acq.lineitem_generated_attr_definition WHERE id = adef.id;
61 xpath_string := REGEXP_REPLACE(xpath_string,$re$//?text\(\)$$re$,'');
63 IF (adef.code = 'title' OR adef.code = 'author') THEN
64 -- title and author should not be split
65 -- FIXME: once oils_xpath can grok XPATH 2.0 functions, we can use
66 -- string-join in the xpath and remove this special case
67 SELECT extract_acq_marc_field(id, xpath_string, adef.remove) INTO value FROM acq.lineitem WHERE id = NEW.id;
68 IF (value IS NOT NULL AND value <> '') THEN
69 INSERT INTO acq.lineitem_attr (lineitem, definition, attr_type, attr_name, attr_value)
70 VALUES (NEW.id, adef.id, atype, adef.code, value);
75 -- each application of the regex may produce multiple values
77 SELECT * FROM extract_acq_marc_field_set(
78 NEW.id, xpath_string || '[' || pos || ']', adef.remove)
81 IF (value IS NOT NULL AND value <> '') THEN
82 INSERT INTO acq.lineitem_attr
83 (lineitem, definition, attr_type, attr_name, attr_value)
84 VALUES (NEW.id, adef.id, atype, adef.code, value);
102 $function$ LANGUAGE PLPGSQL;