3 SELECT evergreen.upgrade_deps_block_check('1076', :eg_version); -- miker/gmcharlt
5 CREATE OR REPLACE FUNCTION asset.cache_copy_visibility () RETURNS TRIGGER as $func$
7 ocn asset.call_number%ROWTYPE;
8 ncn asset.call_number%ROWTYPE;
12 IF TG_TABLE_NAME = 'peer_bib_copy_map' THEN -- Only needs ON INSERT OR DELETE, so handle separately
13 IF TG_OP = 'INSERT' THEN
14 INSERT INTO asset.copy_vis_attr_cache (record, target_copy, vis_attr_vector) VALUES (
17 asset.calculate_copy_visibility_attribute_set(NEW.target_copy)
21 ELSIF TG_OP = 'DELETE' THEN
22 DELETE FROM asset.copy_vis_attr_cache
23 WHERE record = NEW.peer_record AND target_copy = NEW.target_copy;
29 IF TG_OP = 'INSERT' THEN -- Handles ON INSERT. ON UPDATE is below.
30 IF TG_TABLE_NAME IN ('copy', 'unit') THEN
31 SELECT * INTO ncn FROM asset.call_number cn WHERE id = NEW.call_number;
32 INSERT INTO asset.copy_vis_attr_cache (record, target_copy, vis_attr_vector) VALUES (
35 asset.calculate_copy_visibility_attribute_set(NEW.id)
37 ELSIF TG_TABLE_NAME = 'record_entry' THEN
38 NEW.vis_attr_vector := biblio.calculate_bib_visibility_attribute_set(NEW.id);
44 -- handle items first, since with circulation activity
45 -- their statuses change frequently
46 IF TG_TABLE_NAME IN ('copy', 'unit') THEN -- This handles ON UPDATE OR DELETE. ON INSERT above
48 IF TG_OP = 'DELETE' THEN -- Shouldn't get here, normally
49 DELETE FROM asset.copy_vis_attr_cache WHERE target_copy = OLD.id;
53 SELECT * INTO ncn FROM asset.call_number cn WHERE id = NEW.call_number;
55 IF OLD.deleted <> NEW.deleted THEN
57 DELETE FROM asset.copy_vis_attr_cache WHERE target_copy = OLD.id;
59 INSERT INTO asset.copy_vis_attr_cache (record, target_copy, vis_attr_vector) VALUES (
62 asset.calculate_copy_visibility_attribute_set(NEW.id)
67 ELSIF OLD.call_number <> NEW.call_number THEN
68 SELECT * INTO ocn FROM asset.call_number cn WHERE id = OLD.call_number;
70 IF ncn.record <> ocn.record THEN
71 UPDATE biblio.record_entry
72 SET vis_attr_vector = biblio.calculate_bib_visibility_attribute_set(ncn.record)
73 WHERE id = ocn.record;
75 -- We have to use a record-specific WHERE clause
76 -- to avoid modifying the entries for peer-bib copies.
77 UPDATE asset.copy_vis_attr_cache
78 SET target_copy = NEW.id,
80 WHERE target_copy = OLD.id
81 AND record = ocn.record;
85 IF OLD.location <> NEW.location OR
86 OLD.status <> NEW.status OR
87 OLD.opac_visible <> NEW.opac_visible OR
88 OLD.circ_lib <> NEW.circ_lib
90 -- Any of these could change visibility, but
91 -- we'll save some queries and not try to calculate
92 -- the change directly. We want to update peer-bib
93 -- entries in this case, unlike above.
94 UPDATE asset.copy_vis_attr_cache
95 SET target_copy = NEW.id,
96 vis_attr_vector = asset.calculate_copy_visibility_attribute_set(NEW.id)
97 WHERE target_copy = OLD.id;
101 ELSIF TG_TABLE_NAME = 'call_number' THEN -- Only ON UPDATE. Copy handler will deal with ON INSERT OR DELETE.
103 IF OLD.record <> NEW.record THEN
104 IF NEW.label = '##URI##' THEN
105 UPDATE biblio.record_entry
106 SET vis_attr_vector = biblio.calculate_bib_visibility_attribute_set(OLD.record)
107 WHERE id = OLD.record;
109 UPDATE biblio.record_entry
110 SET vis_attr_vector = biblio.calculate_bib_visibility_attribute_set(NEW.record)
111 WHERE id = NEW.record;
114 UPDATE asset.copy_vis_attr_cache
115 SET record = NEW.record,
116 vis_attr_vector = asset.calculate_copy_visibility_attribute_set(target_copy)
117 WHERE target_copy IN (SELECT id FROM asset.copy WHERE call_number = NEW.id)
118 AND record = OLD.record;
120 ELSIF OLD.owning_lib <> NEW.owning_lib THEN
121 UPDATE asset.copy_vis_attr_cache
122 SET vis_attr_vector = asset.calculate_copy_visibility_attribute_set(target_copy)
123 WHERE target_copy IN (SELECT id FROM asset.copy WHERE call_number = NEW.id)
124 AND record = NEW.record;
126 IF NEW.label = '##URI##' THEN
127 UPDATE biblio.record_entry
128 SET vis_attr_vector = biblio.calculate_bib_visibility_attribute_set(OLD.record)
129 WHERE id = OLD.record;
133 ELSIF TG_TABLE_NAME = 'record_entry' THEN -- Only handles ON UPDATE OR DELETE
135 IF TG_OP = 'DELETE' THEN -- Shouldn't get here, normally
136 DELETE FROM asset.copy_vis_attr_cache WHERE record = OLD.id;
138 ELSIF OLD.source <> NEW.source THEN
139 NEW.vis_attr_vector := biblio.calculate_bib_visibility_attribute_set(NEW.id);
146 $func$ LANGUAGE PLPGSQL;