|
| 1 | +\echo Use "CREATE EXTENSION evr" to load this file. \quit |
| 2 | + |
| 3 | +create type evr_array_item as ( |
| 4 | + n NUMERIC, |
| 5 | + s TEXT |
| 6 | +); |
| 7 | + |
| 8 | +create type evr_t as ( |
| 9 | + epoch INT, |
| 10 | + version evr_array_item[], |
| 11 | + release evr_array_item[] |
| 12 | +); |
| 13 | + |
| 14 | +CREATE FUNCTION evr_trigger() RETURNS trigger AS $$ |
| 15 | + BEGIN |
| 16 | + NEW.evr = (select ROW(coalesce(NEW.epoch::numeric,0), |
| 17 | + rpmver_array(coalesce(NEW.version,'empty'))::evr_array_item[], |
| 18 | + rpmver_array(coalesce(NEW.release,'empty'))::evr_array_item[])::evr_t); |
| 19 | + RETURN NEW; |
| 20 | + END; |
| 21 | +$$ language 'plpgsql'; |
| 22 | + |
| 23 | +create or replace FUNCTION empty(t TEXT) |
| 24 | + RETURNS BOOLEAN as $$ |
| 25 | + BEGIN |
| 26 | + return t ~ '^[[:space:]]*$'; |
| 27 | + END; |
| 28 | +$$ language 'plpgsql'; |
| 29 | + |
| 30 | +create or replace FUNCTION isalpha(ch CHAR) |
| 31 | + RETURNS BOOLEAN as $$ |
| 32 | + BEGIN |
| 33 | + if ascii(ch) between ascii('a') and ascii('z') or |
| 34 | + ascii(ch) between ascii('A') and ascii('Z') |
| 35 | + then |
| 36 | + return TRUE; |
| 37 | + end if; |
| 38 | + return FALSE; |
| 39 | + END; |
| 40 | +$$ language 'plpgsql'; |
| 41 | + |
| 42 | +create or replace FUNCTION isalphanum(ch CHAR) |
| 43 | + RETURNS BOOLEAN as $$ |
| 44 | + BEGIN |
| 45 | + if ascii(ch) between ascii('a') and ascii('z') or |
| 46 | + ascii(ch) between ascii('A') and ascii('Z') or |
| 47 | + ascii(ch) between ascii('0') and ascii('9') |
| 48 | + then |
| 49 | + return TRUE; |
| 50 | + end if; |
| 51 | + return FALSE; |
| 52 | + END; |
| 53 | +$$ language 'plpgsql'; |
| 54 | + |
| 55 | +create or replace function isdigit(ch CHAR) |
| 56 | + RETURNS BOOLEAN as $$ |
| 57 | + BEGIN |
| 58 | + if ascii(ch) between ascii('0') and ascii('9') |
| 59 | + then |
| 60 | + return TRUE; |
| 61 | + end if; |
| 62 | + return FALSE; |
| 63 | + END ; |
| 64 | +$$ language 'plpgsql'; |
| 65 | + |
| 66 | +create or replace FUNCTION rpmver_array (string1 IN VARCHAR) |
| 67 | + RETURNS evr_array_item[] as $$ |
| 68 | + declare |
| 69 | + str1 VARCHAR := string1; |
| 70 | + digits VARCHAR(10) := '0123456789'; |
| 71 | + lc_alpha VARCHAR(27) := 'abcdefghijklmnopqrstuvwxyz'; |
| 72 | + uc_alpha VARCHAR(27) := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 73 | + alpha VARCHAR(54) := lc_alpha || uc_alpha; |
| 74 | + one VARCHAR; |
| 75 | + isnum BOOLEAN; |
| 76 | + ver_array evr_array_item[] := ARRAY[]::evr_array_item[]; |
| 77 | + BEGIN |
| 78 | + if str1 is NULL |
| 79 | + then |
| 80 | + RAISE EXCEPTION 'VALUE_ERROR.'; |
| 81 | + end if; |
| 82 | + |
| 83 | + one := str1; |
| 84 | + <<segment_loop>> |
| 85 | + while one <> '' |
| 86 | + loop |
| 87 | + declare |
| 88 | + segm1 VARCHAR; |
| 89 | + segm1_n NUMERIC := 0; |
| 90 | + begin |
| 91 | + -- Throw out all non-alphanum characters |
| 92 | + while one <> '' and not isalphanum(one) |
| 93 | + loop |
| 94 | + one := substr(one, 2); |
| 95 | + end loop; |
| 96 | + str1 := one; |
| 97 | + if str1 <> '' and isdigit(str1) |
| 98 | + then |
| 99 | + str1 := ltrim(str1, digits); |
| 100 | + isnum := true; |
| 101 | + else |
| 102 | + str1 := ltrim(str1, alpha); |
| 103 | + isnum := false; |
| 104 | + end if; |
| 105 | + if str1 <> '' |
| 106 | + then segm1 := substr(one, 1, length(one) - length(str1)); |
| 107 | + else segm1 := one; |
| 108 | + end if; |
| 109 | + |
| 110 | + if segm1 = '' then return ver_array; end if; /* arbitrary */ |
| 111 | + if isnum |
| 112 | + then |
| 113 | + segm1 := ltrim(segm1, '0'); |
| 114 | + if segm1 <> '' then segm1_n := segm1::numeric; end if; |
| 115 | + segm1 := NULL; |
| 116 | + else |
| 117 | + end if; |
| 118 | + ver_array := array_append(ver_array, (segm1_n, segm1)::evr_array_item); |
| 119 | + one := str1; |
| 120 | + end; |
| 121 | + end loop segment_loop; |
| 122 | + |
| 123 | + return ver_array; |
| 124 | + END ; |
| 125 | +$$ language 'plpgsql'; |
0 commit comments