]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/3.0.3-3.0.4-upgrade-db.sql
Forward-port 3.0.4 upgrade script
[Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 3.0.3-3.0.4-upgrade-db.sql
1 --Upgrade Script for 3.0.3 to 3.0.4
2 \set eg_version '''3.0.4'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('3.0.4', :eg_version);
5
6 SELECT evergreen.upgrade_deps_block_check('1088', :eg_version);
7
8 CREATE OR REPLACE FUNCTION metabib.staged_browse(query text, fields integer[], context_org integer, context_locations integer[], staff boolean, browse_superpage_size integer, count_up_from_zero boolean, result_limit integer, next_pivot_pos integer)
9  RETURNS SETOF metabib.flat_browse_entry_appearance
10 AS $f$
11 DECLARE
12     curs                    REFCURSOR;
13     rec                     RECORD;
14     qpfts_query             TEXT;
15     aqpfts_query            TEXT;
16     afields                 INT[];
17     bfields                 INT[];
18     result_row              metabib.flat_browse_entry_appearance%ROWTYPE;
19     results_skipped         INT := 0;
20     row_counter             INT := 0;
21     row_number              INT;
22     slice_start             INT;
23     slice_end               INT;
24     full_end                INT;
25     all_records             BIGINT[];
26     all_brecords             BIGINT[];
27     all_arecords            BIGINT[];
28     superpage_of_records    BIGINT[];
29     superpage_size          INT;
30     c_tests                 TEXT := '';
31     b_tests                 TEXT := '';
32     c_orgs                  INT[];
33     unauthorized_entry      RECORD;
34 BEGIN
35     IF count_up_from_zero THEN
36         row_number := 0;
37     ELSE
38         row_number := -1;
39     END IF;
40
41     IF NOT staff THEN
42         SELECT x.c_attrs, x.b_attrs INTO c_tests, b_tests FROM asset.patron_default_visibility_mask() x;
43     END IF;
44
45     -- b_tests supplies its own query_int operator, c_tests does not
46     IF c_tests <> '' THEN c_tests := c_tests || '&'; END IF;
47
48     SELECT ARRAY_AGG(id) INTO c_orgs FROM actor.org_unit_descendants(context_org);
49
50     c_tests := c_tests || search.calculate_visibility_attribute_test('circ_lib',c_orgs)
51                || '&' || search.calculate_visibility_attribute_test('owning_lib',c_orgs);
52
53     PERFORM 1 FROM config.internal_flag WHERE enabled AND name = 'opac.located_uri.act_as_copy';
54     IF FOUND THEN
55         b_tests := b_tests || search.calculate_visibility_attribute_test(
56             'luri_org',
57             (SELECT ARRAY_AGG(id) FROM actor.org_unit_full_path(context_org) x)
58         );
59     ELSE
60         b_tests := b_tests || search.calculate_visibility_attribute_test(
61             'luri_org',
62             (SELECT ARRAY_AGG(id) FROM actor.org_unit_ancestors(context_org) x)
63         );
64     END IF;
65
66     IF context_locations THEN
67         IF c_tests <> '' THEN c_tests := c_tests || '&'; END IF;
68         c_tests := c_tests || search.calculate_visibility_attribute_test('location',context_locations);
69     END IF;
70
71     OPEN curs NO SCROLL FOR EXECUTE query;
72
73     LOOP
74         FETCH curs INTO rec;
75         IF NOT FOUND THEN
76             IF result_row.pivot_point IS NOT NULL THEN
77                 RETURN NEXT result_row;
78             END IF;
79             RETURN;
80         END IF;
81
82         --Is unauthorized?
83         SELECT INTO unauthorized_entry *
84         FROM metabib.browse_entry_simple_heading_map mbeshm
85         INNER JOIN authority.simple_heading ash ON ( mbeshm.simple_heading = ash.id )
86         INNER JOIN authority.control_set_authority_field acsaf ON ( acsaf.id = ash.atag )
87         JOIN authority.heading_field ahf ON (ahf.id = acsaf.heading_field)
88         WHERE mbeshm.entry = rec.id
89         AND   ahf.heading_purpose = 'variant';
90
91         -- Gather aggregate data based on the MBE row we're looking at now, authority axis
92         IF (unauthorized_entry.record IS NOT NULL) THEN
93             --unauthorized term belongs to an auth linked to a bib?
94             SELECT INTO all_arecords, result_row.sees, afields
95                     ARRAY_AGG(DISTINCT abl.bib),
96                     STRING_AGG(DISTINCT abl.authority::TEXT, $$,$$),
97                     ARRAY_AGG(DISTINCT map.metabib_field)
98             FROM authority.bib_linking abl
99             INNER JOIN authority.control_set_auth_field_metabib_field_map_refs map ON (
100                     map.authority_field = unauthorized_entry.atag
101                     AND map.metabib_field = ANY(fields)
102             )
103             WHERE abl.authority = unauthorized_entry.record;
104         ELSE
105             --do usual procedure
106             SELECT INTO all_arecords, result_row.sees, afields
107                     ARRAY_AGG(DISTINCT abl.bib), -- bibs to check for visibility
108                     STRING_AGG(DISTINCT aal.source::TEXT, $$,$$), -- authority record ids
109                     ARRAY_AGG(DISTINCT map.metabib_field) -- authority-tag-linked CMF rows
110
111             FROM  metabib.browse_entry_simple_heading_map mbeshm
112                     JOIN authority.simple_heading ash ON ( mbeshm.simple_heading = ash.id )
113                     JOIN authority.authority_linking aal ON ( ash.record = aal.source )
114                     JOIN authority.bib_linking abl ON ( aal.target = abl.authority )
115                     JOIN authority.control_set_auth_field_metabib_field_map_refs map ON (
116                         ash.atag = map.authority_field
117                         AND map.metabib_field = ANY(fields)
118                     )
119                     JOIN authority.control_set_authority_field acsaf ON (
120                         map.authority_field = acsaf.id
121                     )
122                     JOIN authority.heading_field ahf ON (ahf.id = acsaf.heading_field)
123               WHERE mbeshm.entry = rec.id
124               AND   ahf.heading_purpose = 'variant';
125
126         END IF;
127
128         -- Gather aggregate data based on the MBE row we're looking at now, bib axis
129         SELECT INTO all_brecords, result_row.authorities, bfields
130                 ARRAY_AGG(DISTINCT source),
131                 STRING_AGG(DISTINCT authority::TEXT, $$,$$),
132                 ARRAY_AGG(DISTINCT def)
133           FROM  metabib.browse_entry_def_map
134           WHERE entry = rec.id
135                 AND def = ANY(fields);
136
137         SELECT INTO result_row.fields STRING_AGG(DISTINCT x::TEXT, $$,$$) FROM UNNEST(afields || bfields) x;
138
139         result_row.sources := 0;
140         result_row.asources := 0;
141
142         -- Bib-linked vis checking
143         IF ARRAY_UPPER(all_brecords,1) IS NOT NULL THEN
144
145             SELECT  INTO result_row.sources COUNT(DISTINCT b.id)
146               FROM  biblio.record_entry b
147                     JOIN asset.copy_vis_attr_cache acvac ON (acvac.record = b.id)
148               WHERE b.id = ANY(all_brecords[1:browse_superpage_size])
149                     AND (
150                         acvac.vis_attr_vector @@ c_tests::query_int
151                         OR b.vis_attr_vector @@ b_tests::query_int
152                     );
153
154             result_row.accurate := TRUE;
155
156         END IF;
157
158         -- Authority-linked vis checking
159         IF ARRAY_UPPER(all_arecords,1) IS NOT NULL THEN
160
161             SELECT  INTO result_row.asources COUNT(DISTINCT b.id)
162               FROM  biblio.record_entry b
163                     JOIN asset.copy_vis_attr_cache acvac ON (acvac.record = b.id)
164               WHERE b.id = ANY(all_arecords[1:browse_superpage_size])
165                     AND (
166                         acvac.vis_attr_vector @@ c_tests::query_int
167                         OR b.vis_attr_vector @@ b_tests::query_int
168                     );
169
170             result_row.aaccurate := TRUE;
171
172         END IF;
173
174         IF result_row.sources > 0 OR result_row.asources > 0 THEN
175
176             -- The function that calls this function needs row_number in order
177             -- to correctly order results from two different runs of this
178             -- functions.
179             result_row.row_number := row_number;
180
181             -- Now, if row_counter is still less than limit, return a row.  If
182             -- not, but it is less than next_pivot_pos, continue on without
183             -- returning actual result rows until we find
184             -- that next pivot, and return it.
185
186             IF row_counter < result_limit THEN
187                 result_row.browse_entry := rec.id;
188                 result_row.value := rec.value;
189
190                 RETURN NEXT result_row;
191             ELSE
192                 result_row.browse_entry := NULL;
193                 result_row.authorities := NULL;
194                 result_row.fields := NULL;
195                 result_row.value := NULL;
196                 result_row.sources := NULL;
197                 result_row.sees := NULL;
198                 result_row.accurate := NULL;
199                 result_row.aaccurate := NULL;
200                 result_row.pivot_point := rec.id;
201
202                 IF row_counter >= next_pivot_pos THEN
203                     RETURN NEXT result_row;
204                     RETURN;
205                 END IF;
206             END IF;
207
208             IF count_up_from_zero THEN
209                 row_number := row_number + 1;
210             ELSE
211                 row_number := row_number - 1;
212             END IF;
213
214             -- row_counter is different from row_number.
215             -- It simply counts up from zero so that we know when
216             -- we've reached our limit.
217             row_counter := row_counter + 1;
218         END IF;
219     END LOOP;
220 END;
221 $f$ LANGUAGE plpgsql ROWS 10;
222
223
224 COMMIT;