2 CREATE OR REPLACE FUNCTION metabib.reingest_metabib_field_entries( bib_id BIGINT, skip_facet BOOL DEFAULT FALSE, skip_browse BOOL DEFAULT FALSE, skip_search BOOL DEFAULT FALSE ) RETURNS VOID AS $func$
5 ind_data metabib.field_entry_template%ROWTYPE;
6 mbe_row metabib.browse_entry%ROWTYPE;
9 PERFORM * FROM config.internal_flag WHERE name = 'ingest.assume_inserts_only' AND enabled;
11 IF NOT skip_search THEN
12 FOR fclass IN SELECT * FROM config.metabib_class LOOP
13 -- RAISE NOTICE 'Emptying out %', fclass.name;
14 EXECUTE $$DELETE FROM metabib.$$ || fclass.name || $$_field_entry WHERE source = $$ || bib_id;
17 IF NOT skip_facet THEN
18 DELETE FROM metabib.facet_entry WHERE source = bib_id;
20 IF NOT skip_browse THEN
21 DELETE FROM metabib.browse_entry_def_map WHERE source = bib_id;
25 FOR ind_data IN SELECT * FROM biblio.extract_metabib_field_entry( bib_id ) LOOP
26 IF ind_data.field < 0 THEN
27 ind_data.field = -1 * ind_data.field;
30 IF ind_data.facet_field AND NOT skip_facet THEN
31 INSERT INTO metabib.facet_entry (field, source, value)
32 VALUES (ind_data.field, ind_data.source, ind_data.value);
35 IF ind_data.browse_field AND NOT skip_browse THEN
36 -- A caveat about this SELECT: this should take care of replacing
37 -- old mbe rows when data changes, but not if normalization (by
38 -- which I mean specifically the output of
39 -- evergreen.oils_tsearch2()) changes. It may or may not be
40 -- expensive to add a comparison of index_vector to index_vector
41 -- to the WHERE clause below.
42 SELECT INTO mbe_row * FROM metabib.browse_entry WHERE value = ind_data.value;
46 INSERT INTO metabib.browse_entry (value) VALUES
47 (metabib.browse_normalize(ind_data.value, ind_data.field));
48 mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
51 INSERT INTO metabib.browse_entry_def_map (entry, def, source)
52 VALUES (mbe_id, ind_data.field, ind_data.source);
55 IF ind_data.search_field AND NOT skip_search THEN
57 INSERT INTO metabib.$$ || ind_data.field_class || $$_field_entry (field, source, value)
59 quote_literal(ind_data.field) || $$, $$ ||
60 quote_literal(ind_data.source) || $$, $$ ||
61 quote_literal(ind_data.value) ||
69 $func$ LANGUAGE PLPGSQL;
72 DROP FUNCTION IF EXISTS metabib.reingest_metabib_field_entries(BIGINT);