]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0819.schema.acn_dewey_normalizer.sql
LP#1248734: (follow-up) add new indexes to schema update script
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0819.schema.acn_dewey_normalizer.sql
1 -- Evergreen DB patch 0819.schema.acn_dewey_normalizer.sql
2 --
3 -- Fixes Dewey call number sorting (per LP# 1150939)
4 --
5 BEGIN;
6
7 -- check whether patch can be applied
8 SELECT evergreen.upgrade_deps_block_check('0819', :eg_version);
9
10 CREATE OR REPLACE FUNCTION asset.label_normalizer_dewey(TEXT) RETURNS TEXT AS $func$
11     # Derived from the Koha C4::ClassSortRoutine::Dewey module
12     # Copyright (C) 2007 LibLime
13     # Licensed under the GPL v2 or later
14
15     use strict;
16     use warnings;
17
18     my $init = uc(shift);
19     $init =~ s/^\s+//;
20     $init =~ s/\s+$//;
21     $init =~ s!/!!g;
22     $init =~ s/^([\p{IsAlpha}]+)/$1 /;
23     my @tokens = split /\.|\s+/, $init;
24     my $digit_group_count = 0;
25     my $first_digit_group_idx;
26     for (my $i = 0; $i <= $#tokens; $i++) {
27         if ($tokens[$i] =~ /^\d+$/) {
28             $digit_group_count++;
29             if ($digit_group_count == 1) {
30                 $first_digit_group_idx = $i;
31             }
32             if (2 == $digit_group_count) {
33                 $tokens[$i] = sprintf("%-15.15s", $tokens[$i]);
34                 $tokens[$i] =~ tr/ /0/;
35             }
36         }
37     }
38     # Pad the first digit_group if there was only one
39     if (1 == $digit_group_count) {
40         $tokens[$first_digit_group_idx] .= '_000000000000000'
41     }
42     my $key = join("_", @tokens);
43     $key =~ s/[^\p{IsAlnum}_]//g;
44
45     return $key;
46
47 $func$ LANGUAGE PLPERLU;
48
49 -- regenerate sort keys for any dewey call numbers
50 UPDATE asset.call_number SET id = id WHERE label_class = 2;
51
52 COMMIT;