Artifact
0f042d67c0cb1eb508161a21ef10bdead5edc4565002f8474f48fbef53b4dc91:
0000: 43 52 45 41 54 45 20 50 52 4f 43 45 44 55 52 41 CREATE PROCEDURA
0010: 4c 20 4c 41 4e 47 55 41 47 45 20 70 6c 70 67 73 L LANGUAGE plpgs
0020: 71 6c 3b 0a 0a 2d 2d 20 67 65 6e 65 72 61 6c 20 ql;..-- general
0030: 61 72 72 61 79 20 73 6f 72 74 69 6e 67 20 66 75 array sorting fu
0040: 6e 63 74 69 6f 6e 73 0a 2d 2d 20 73 6f 72 74 73 nctions.-- sorts
0050: 20 61 72 72 61 79 0a 43 52 45 41 54 45 20 6f 72 array.CREATE or
0060: 20 72 65 70 6c 61 63 65 20 46 55 4e 43 54 49 4f replace FUNCTIO
0070: 4e 20 73 6f 72 74 28 6f 72 69 67 69 6e 61 6c 20 N sort(original
0080: 61 6e 79 61 72 72 61 79 29 20 52 45 54 55 52 4e anyarray) RETURN
0090: 53 20 61 6e 79 61 72 72 61 79 0a 09 4c 41 4e 47 S anyarray..LANG
00a0: 55 41 47 45 20 73 71 6c 20 49 4d 4d 55 54 41 42 UAGE sql IMMUTAB
00b0: 4c 45 20 53 54 52 49 43 54 0a 09 41 53 20 24 5f LE STRICT..AS $_
00c0: 24 0a 73 65 6c 65 63 74 20 61 72 72 61 79 5f 61 $.select array_a
00d0: 67 67 28 69 74 65 6d 29 20 61 73 20 72 65 73 75 gg(item) as resu
00e0: 6c 74 20 66 72 6f 6d 20 28 73 65 6c 65 63 74 20 lt from (select
00f0: 75 6e 6e 65 73 74 28 24 31 29 20 61 73 20 69 74 unnest($1) as it
0100: 65 6d 20 6f 72 64 65 72 20 62 79 20 69 74 65 6d em order by item
0110: 29 20 61 3b 0a 24 5f 24 3b 0a 0a 2d 2d 20 73 6f ) a;.$_$;..-- so
0120: 72 74 73 20 61 72 72 61 79 20 61 6e 64 20 72 65 rts array and re
0130: 6d 6f 76 65 73 20 64 75 70 6c 69 63 61 74 65 73 moves duplicates
0140: 0a 43 52 45 41 54 45 20 6f 72 20 72 65 70 6c 61 .CREATE or repla
0150: 63 65 20 46 55 4e 43 54 49 4f 4e 20 75 73 6f 72 ce FUNCTION usor
0160: 74 28 6f 72 69 67 69 6e 61 6c 20 61 6e 79 61 72 t(original anyar
0170: 72 61 79 29 20 52 45 54 55 52 4e 53 20 61 6e 79 ray) RETURNS any
0180: 61 72 72 61 79 0a 09 4c 41 4e 47 55 41 47 45 20 array..LANGUAGE
0190: 73 71 6c 20 49 4d 4d 55 54 41 42 4c 45 20 53 54 sql IMMUTABLE ST
01a0: 52 49 43 54 0a 09 41 53 20 24 5f 24 0a 73 65 6c RICT..AS $_$.sel
01b0: 65 63 74 20 61 72 72 61 79 5f 61 67 67 28 69 74 ect array_agg(it
01c0: 65 6d 29 20 61 73 20 72 65 73 75 6c 74 20 66 72 em) as result fr
01d0: 6f 6d 20 28 73 65 6c 65 63 74 20 64 69 73 74 69 om (select disti
01e0: 6e 63 74 20 75 6e 6e 65 73 74 28 24 31 29 20 61 nct unnest($1) a
01f0: 73 20 69 74 65 6d 20 6f 72 64 65 72 20 62 79 20 s item order by
0200: 69 74 65 6d 29 20 61 3b 0a 24 5f 24 3b 0a 0a 2d item) a;.$_$;..-
0210: 2d 20 74 72 61 6e 73 66 6f 72 6d 73 20 64 6f 6d - transforms dom
0220: 61 69 6e 20 69 6e 74 6f 20 6f 72 64 65 72 65 64 ain into ordered
0230: 20 61 72 72 61 79 20 66 6f 72 20 69 6e 64 65 78 array for index
0240: 69 6e 67 0a 43 52 45 41 54 45 20 6f 72 20 72 65 ing.CREATE or re
0250: 70 6c 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 74 place FUNCTION t
0260: 72 69 70 64 6f 6d 61 69 6e 28 75 72 6c 20 74 65 ripdomain(url te
0270: 78 74 29 20 52 45 54 55 52 4e 53 20 74 65 78 74 xt) RETURNS text
0280: 5b 5d 0a 09 4c 41 4e 47 55 41 47 45 20 70 6c 70 []..LANGUAGE plp
0290: 67 73 71 6c 20 49 4d 4d 55 54 41 42 4c 45 20 53 gsql IMMUTABLE S
02a0: 54 52 49 43 54 0a 09 41 53 20 24 5f 24 0a 64 65 TRICT..AS $_$.de
02b0: 63 6c 61 72 65 0a 09 72 65 73 75 6c 74 20 74 65 clare..result te
02c0: 78 74 5b 5d 3b 0a 09 73 70 6c 69 74 74 65 64 20 xt[];..splitted
02d0: 74 65 78 74 5b 5d 3b 0a 09 78 20 69 6e 74 65 67 text[];..x integ
02e0: 65 72 3b 0a 09 6c 65 6e 67 74 68 20 69 6e 74 65 er;..length inte
02f0: 67 65 72 3b 0a 62 65 67 69 6e 0a 09 73 70 6c 69 ger;.begin..spli
0300: 74 74 65 64 20 3a 3d 20 73 74 72 69 6e 67 5f 74 tted := string_t
0310: 6f 5f 61 72 72 61 79 28 24 31 2c 20 27 2e 27 29 o_array($1, '.')
0320: 3b 0a 09 6c 65 6e 67 74 68 20 3a 3d 20 61 72 72 ;..length := arr
0330: 61 79 5f 6c 65 6e 67 74 68 28 73 70 6c 69 74 74 ay_length(splitt
0340: 65 64 2c 20 31 29 3b 0a 09 78 20 3a 3d 20 31 3b ed, 1);..x := 1;
0350: 0a 09 6c 6f 6f 70 0a 09 09 65 78 69 74 20 77 68 ..loop...exit wh
0360: 65 6e 20 73 70 6c 69 74 74 65 64 5b 78 5d 20 69 en splitted[x] i
0370: 73 20 6e 75 6c 6c 3b 0a 09 09 72 65 73 75 6c 74 s null;...result
0380: 5b 78 5d 20 3a 3d 20 73 70 6c 69 74 74 65 64 5b [x] := splitted[
0390: 78 5d 20 7c 7c 20 27 3a 27 20 7c 7c 20 6c 65 6e x] || ':' || len
03a0: 67 74 68 20 2d 20 78 3b 0a 09 09 78 20 3a 3d 20 gth - x;...x :=
03b0: 78 20 2b 20 31 3b 0a 09 65 6e 64 20 6c 6f 6f 70 x + 1;..end loop
03c0: 3b 0a 09 72 65 74 75 72 6e 20 72 65 73 75 6c 74 ;..return result
03d0: 3b 0a 65 6e 64 3b 24 5f 24 3b 0a 0a 2d 2d 20 74 ;.end;$_$;..-- t
03e0: 72 61 6e 73 66 6f 72 6d 73 20 6f 72 64 65 72 65 ransforms ordere
03f0: 64 20 61 72 72 61 79 20 69 6e 74 6f 20 64 6f 6d d array into dom
0400: 61 69 6e 0a 63 72 65 61 74 65 20 6f 72 20 72 65 ain.create or re
0410: 70 6c 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 75 place function u
0420: 6e 74 72 69 70 28 73 69 74 65 20 74 65 78 74 5b ntrip(site text[
0430: 5d 29 20 72 65 74 75 72 6e 73 20 74 65 78 74 0a ]) returns text.
0440: 09 6c 61 6e 67 75 61 67 65 20 70 6c 70 67 73 71 .language plpgsq
0450: 6c 20 69 6d 6d 75 74 61 62 6c 65 20 73 74 72 69 l immutable stri
0460: 63 74 0a 09 61 73 20 24 5f 24 0a 64 65 63 6c 61 ct..as $_$.decla
0470: 72 65 0a 09 78 20 69 6e 74 65 67 65 72 3b 0a 09 re..x integer;..
0480: 73 70 6c 69 74 74 65 64 20 74 65 78 74 5b 5d 3b splitted text[];
0490: 0a 09 70 61 69 72 20 74 65 78 74 5b 5d 3b 0a 62 ..pair text[];.b
04a0: 65 67 69 6e 0a 09 78 20 3a 3d 20 61 72 72 61 79 egin..x := array
04b0: 5f 6c 65 6e 67 74 68 28 73 69 74 65 2c 20 31 29 _length(site, 1)
04c0: 3b 0a 09 6c 6f 6f 70 20 0a 09 09 65 78 69 74 20 ;..loop ...exit
04d0: 77 68 65 6e 20 73 69 74 65 5b 78 5d 20 69 73 20 when site[x] is
04e0: 6e 75 6c 6c 3b 0a 09 09 70 61 69 72 20 3a 3d 20 null;...pair :=
04f0: 73 74 72 69 6e 67 5f 74 6f 5f 61 72 72 61 79 28 string_to_array(
0500: 73 69 74 65 5b 78 5d 2c 20 27 3a 27 29 3b 0a 09 site[x], ':');..
0510: 09 73 70 6c 69 74 74 65 64 5b 30 20 2d 20 70 61 .splitted[0 - pa
0520: 69 72 5b 32 5d 3a 3a 69 6e 74 65 67 65 72 5d 20 ir[2]::integer]
0530: 3a 3d 20 70 61 69 72 5b 31 5d 3b 0a 09 09 78 20 := pair[1];...x
0540: 3a 3d 20 78 20 2d 20 31 3b 0a 09 65 6e 64 20 6c := x - 1;..end l
0550: 6f 6f 70 3b 0a 09 72 65 74 75 72 6e 20 61 72 72 oop;..return arr
0560: 61 79 5f 74 6f 5f 73 74 72 69 6e 67 28 73 70 6c ay_to_string(spl
0570: 69 74 74 65 64 2c 20 27 2e 27 29 3b 0a 65 6e 64 itted, '.');.end
0580: 3b 0a 24 5f 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 ;.$_$;..-- this
0590: 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e functions return
05a0: 73 20 69 64 20 6f 66 20 73 69 74 65 0a 63 72 65 s id of site.cre
05b0: 61 74 65 20 6f 72 20 72 65 70 6c 61 63 65 20 66 ate or replace f
05c0: 75 6e 63 74 69 6f 6e 20 67 65 74 5f 73 69 74 65 unction get_site
05d0: 28 6d 79 5f 73 69 74 65 20 74 65 78 74 5b 5d 29 (my_site text[])
05e0: 20 72 65 74 75 72 6e 73 20 69 6e 74 65 67 65 72 returns integer
05f0: 0a 09 6c 61 6e 67 75 61 67 65 20 70 6c 70 67 73 ..language plpgs
0600: 71 6c 20 73 74 72 69 63 74 0a 09 61 73 20 24 24 ql strict..as $$
0610: 0a 64 65 63 6c 61 72 65 0a 09 73 69 74 65 5f 69 .declare..site_i
0620: 64 20 69 6e 74 65 67 65 72 3b 0a 62 65 67 69 6e d integer;.begin
0630: 0a 09 73 65 6c 65 63 74 20 69 64 5f 73 69 74 65 ..select id_site
0640: 20 66 72 6f 6d 20 73 69 74 65 20 77 68 65 72 65 from site where
0650: 20 6d 79 5f 73 69 74 65 20 3d 20 73 69 74 65 20 my_site = site
0660: 69 6e 74 6f 20 73 69 74 65 5f 69 64 3b 0a 09 69 into site_id;..i
0670: 66 20 6e 6f 74 20 66 6f 75 6e 64 20 74 68 65 6e f not found then
0680: 0a 09 09 69 6e 73 65 72 74 20 69 6e 74 6f 20 73 ...insert into s
0690: 69 74 65 20 28 73 69 74 65 29 20 76 61 6c 75 65 ite (site) value
06a0: 73 20 28 6d 79 5f 73 69 74 65 29 3b 0a 09 09 73 s (my_site);...s
06b0: 65 6c 65 63 74 20 69 64 5f 73 69 74 65 20 66 72 elect id_site fr
06c0: 6f 6d 20 73 69 74 65 20 77 68 65 72 65 20 6d 79 om site where my
06d0: 5f 73 69 74 65 20 3d 20 73 69 74 65 20 69 6e 74 _site = site int
06e0: 6f 20 73 69 74 65 5f 69 64 3b 0a 09 65 6e 64 20 o site_id;..end
06f0: 69 66 3b 0a 09 72 65 74 75 72 6e 20 73 69 74 65 if;..return site
0700: 5f 69 64 3b 0a 65 6e 64 3b 0a 24 24 3b 0a 0a 63 _id;.end;.$$;..c
0710: 72 65 61 74 65 20 6f 72 20 72 65 70 6c 61 63 65 reate or replace
0720: 20 66 75 6e 63 74 69 6f 6e 20 67 65 74 5f 73 69 function get_si
0730: 74 65 28 64 6f 6d 61 69 6e 20 74 65 78 74 29 20 te(domain text)
0740: 72 65 74 75 72 6e 73 20 69 6e 74 65 67 65 72 0a returns integer.
0750: 09 6c 61 6e 67 75 61 67 65 20 73 71 6c 20 69 6d .language sql im
0760: 6d 75 74 61 62 6c 65 20 73 74 72 69 63 74 0a 09 mutable strict..
0770: 61 73 20 24 24 0a 73 65 6c 65 63 74 20 67 65 74 as $$.select get
0780: 5f 73 69 74 65 28 74 72 69 70 64 6f 6d 61 69 6e _site(tripdomain
0790: 28 24 31 29 29 20 61 73 20 72 65 73 75 6c 74 3b ($1)) as result;
07a0: 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 66 75 .$$;..-- this fu
07b0: 6e 63 74 69 6f 6e 20 61 64 64 73 20 74 61 67 20 nction adds tag
07c0: 74 6f 20 73 69 74 65 20 62 79 20 73 69 74 65 20 to site by site
07d0: 69 64 0a 43 52 45 41 54 45 20 6f 72 20 72 65 70 id.CREATE or rep
07e0: 6c 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 6d 61 lace FUNCTION ma
07f0: 72 6b 28 6d 79 5f 69 64 5f 73 69 74 65 20 69 6e rk(my_id_site in
0800: 74 65 67 65 72 2c 20 6e 65 77 5f 74 61 67 20 74 teger, new_tag t
0810: 65 78 74 29 20 52 45 54 55 52 4e 53 20 69 6e 74 ext) RETURNS int
0820: 65 67 65 72 0a 09 4c 41 4e 47 55 41 47 45 20 70 eger..LANGUAGE p
0830: 6c 70 67 73 71 6c 20 53 54 52 49 43 54 0a 09 41 lpgsql STRICT..A
0840: 53 20 24 24 0a 64 65 63 6c 61 72 65 0a 09 2d 2d S $$.declare..--
0850: 20 6d 61 79 62 65 20 63 68 65 63 6b 20 73 68 6f maybe check sho
0860: 75 6c 64 20 62 65 20 61 64 64 65 64 20 74 6f 20 uld be added to
0870: 6d 61 6b 65 20 73 75 72 65 20 73 75 70 70 6c 69 make sure suppli
0880: 65 64 20 73 69 74 65 20 69 64 20 72 65 61 6c 6c ed site id reall
0890: 79 20 65 78 69 73 74 73 0a 09 6d 79 5f 74 61 67 y exists..my_tag
08a0: 20 74 65 78 74 5b 5d 3b 0a 09 6d 79 5f 74 61 67 text[];..my_tag
08b0: 5f 69 64 20 69 6e 74 65 67 65 72 3b 0a 62 65 67 _id integer;.beg
08c0: 69 6e 0a 09 2d 2d 20 73 65 6c 65 63 74 69 6e 67 in..-- selecting
08d0: 20 74 61 67 73 20 73 69 74 65 20 61 6c 72 65 61 tags site alrea
08e0: 64 79 20 68 61 76 65 20 61 6e 64 20 61 64 64 69 dy have and addi
08f0: 6e 67 20 6e 65 77 20 74 61 67 20 74 6f 20 74 68 ng new tag to th
0900: 65 6d 0a 09 2d 2d 20 6e 6f 74 65 20 74 68 61 74 em..-- note that
0910: 20 74 61 67 73 20 73 68 6f 75 6c 64 20 62 65 20 tags should be
0920: 73 6f 72 74 65 64 20 74 6f 20 65 6c 69 6d 69 6e sorted to elimin
0930: 61 74 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 73 ate permutations
0940: 0a 09 73 65 6c 65 63 74 20 63 6f 61 6c 65 73 63 ..select coalesc
0950: 65 28 74 61 67 2c 20 27 7b 7d 27 3a 3a 74 65 78 e(tag, '{}'::tex
0960: 74 5b 5d 29 20 66 72 6f 6d 20 75 72 6c 73 20 6e t[]) from urls n
0970: 61 74 75 72 61 6c 20 6c 65 66 74 20 6a 6f 69 6e atural left join
0980: 20 74 61 67 20 77 68 65 72 65 20 69 64 5f 73 69 tag where id_si
0990: 74 65 20 3d 20 6d 79 5f 69 64 5f 73 69 74 65 20 te = my_id_site
09a0: 69 6e 74 6f 20 6d 79 5f 74 61 67 3b 0a 09 69 66 into my_tag;..if
09b0: 20 6e 6f 74 20 66 6f 75 6e 64 20 74 68 65 6e 0a not found then.
09c0: 09 09 2d 2d 20 6e 6f 20 72 65 63 6f 72 64 73 20 ..-- no records
09d0: 66 6f 75 6e 64 20 2d 20 63 72 65 61 74 69 6e 67 found - creating
09e0: 20 6e 65 77 20 74 61 67 0a 09 09 69 6e 73 65 72 new tag...inser
09f0: 74 20 69 6e 74 6f 20 75 72 6c 73 20 28 69 64 5f t into urls (id_
0a00: 73 69 74 65 2c 20 69 64 5f 74 61 67 29 20 76 61 site, id_tag) va
0a10: 6c 75 65 73 20 28 6d 79 5f 69 64 5f 73 69 74 65 lues (my_id_site
0a20: 2c 20 67 65 74 5f 74 61 67 28 61 72 72 61 79 5b , get_tag(array[
0a30: 6e 65 77 5f 74 61 67 5d 29 29 3b 0a 09 65 6c 73 new_tag]));..els
0a40: 65 0a 09 09 2d 2d 20 6a 6f 69 6e 69 6e 67 20 74 e...-- joining t
0a50: 61 67 73 0a 09 09 73 65 6c 65 63 74 20 75 73 6f ags...select uso
0a60: 72 74 28 6d 79 5f 74 61 67 20 7c 7c 20 61 72 72 rt(my_tag || arr
0a70: 61 79 5b 6e 65 77 5f 74 61 67 5d 29 20 69 6e 74 ay[new_tag]) int
0a80: 6f 20 6d 79 5f 74 61 67 3b 0a 09 09 2d 2d 20 75 o my_tag;...-- u
0a90: 70 64 61 74 69 6e 67 20 65 78 69 73 74 69 6e 67 pdating existing
0aa0: 20 72 65 63 6f 72 64 0a 09 09 75 70 64 61 74 65 record...update
0ab0: 20 75 72 6c 73 20 73 65 74 20 69 64 5f 74 61 67 urls set id_tag
0ac0: 20 3d 20 67 65 74 5f 74 61 67 28 6d 79 5f 74 61 = get_tag(my_ta
0ad0: 67 20 7c 7c 20 61 72 72 61 79 5b 6e 65 77 5f 74 g || array[new_t
0ae0: 61 67 5d 29 20 77 68 65 72 65 20 69 64 5f 73 69 ag]) where id_si
0af0: 74 65 20 3d 20 6d 79 5f 69 64 5f 73 69 74 65 3b te = my_id_site;
0b00: 0a 09 65 6e 64 20 69 66 3b 0a 09 72 65 74 75 72 ..end if;..retur
0b10: 6e 20 6d 79 5f 69 64 5f 73 69 74 65 3b 0a 65 6e n my_id_site;.en
0b20: 64 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 d;.$$;..-- this
0b30: 66 75 6e 63 74 69 6f 6e 20 61 64 64 73 20 74 61 function adds ta
0b40: 67 20 74 6f 20 64 6f 6d 61 69 6e 0a 43 52 45 41 g to domain.CREA
0b50: 54 45 20 6f 72 20 72 65 70 6c 61 63 65 20 46 55 TE or replace FU
0b60: 4e 43 54 49 4f 4e 20 6d 61 72 6b 28 64 6f 6d 61 NCTION mark(doma
0b70: 69 6e 20 74 65 78 74 2c 20 6e 65 77 5f 74 61 67 in text, new_tag
0b80: 20 74 65 78 74 29 20 52 45 54 55 52 4e 53 20 69 text) RETURNS i
0b90: 6e 74 65 67 65 72 0a 09 4c 41 4e 47 55 41 47 45 nteger..LANGUAGE
0ba0: 20 73 71 6c 20 69 6d 6d 75 74 61 62 6c 65 20 53 sql immutable S
0bb0: 54 52 49 43 54 0a 09 41 53 20 24 24 0a 73 65 6c TRICT..AS $$.sel
0bc0: 65 63 74 20 6d 61 72 6b 28 67 65 74 5f 73 69 74 ect mark(get_sit
0bd0: 65 28 24 31 29 2c 20 24 32 29 20 61 73 20 72 65 e($1), $2) as re
0be0: 73 75 6c 74 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 sult;.$$;..-- th
0bf0: 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
0c00: 72 6e 73 20 69 64 20 6f 66 20 74 61 67 20 61 72 rns id of tag ar
0c10: 72 61 79 0a 63 72 65 61 74 65 20 6f 72 20 72 65 ray.create or re
0c20: 70 6c 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 67 place function g
0c30: 65 74 5f 74 61 67 28 6d 79 5f 74 61 67 20 74 65 et_tag(my_tag te
0c40: 78 74 5b 5d 29 20 72 65 74 75 72 6e 73 20 69 6e xt[]) returns in
0c50: 74 65 67 65 72 0a 09 6c 61 6e 67 75 61 67 65 20 teger..language
0c60: 70 6c 70 67 73 71 6c 20 73 74 72 69 63 74 0a 09 plpgsql strict..
0c70: 61 73 20 24 24 0a 64 65 63 6c 61 72 65 0a 09 74 as $$.declare..t
0c80: 61 67 5f 69 64 20 69 6e 74 65 67 65 72 3b 0a 62 ag_id integer;.b
0c90: 65 67 69 6e 0a 09 73 65 6c 65 63 74 20 69 64 5f egin..select id_
0ca0: 74 61 67 20 66 72 6f 6d 20 74 61 67 20 77 68 65 tag from tag whe
0cb0: 72 65 20 75 73 6f 72 74 28 6d 79 5f 74 61 67 29 re usort(my_tag)
0cc0: 20 3d 20 74 61 67 20 69 6e 74 6f 20 74 61 67 5f = tag into tag_
0cd0: 69 64 3b 0a 09 69 66 20 6e 6f 74 20 66 6f 75 6e id;..if not foun
0ce0: 64 20 74 68 65 6e 0a 09 09 69 6e 73 65 72 74 20 d then...insert
0cf0: 69 6e 74 6f 20 74 61 67 20 28 74 61 67 29 20 76 into tag (tag) v
0d00: 61 6c 75 65 73 20 28 75 73 6f 72 74 28 6d 79 5f alues (usort(my_
0d10: 74 61 67 29 29 3b 0a 09 09 73 65 6c 65 63 74 20 tag));...select
0d20: 69 64 5f 74 61 67 20 66 72 6f 6d 20 74 61 67 20 id_tag from tag
0d30: 77 68 65 72 65 20 75 73 6f 72 74 28 6d 79 5f 74 where usort(my_t
0d40: 61 67 29 20 3d 20 74 61 67 20 69 6e 74 6f 20 74 ag) = tag into t
0d50: 61 67 5f 69 64 3b 0a 09 65 6e 64 20 69 66 3b 0a ag_id;..end if;.
0d60: 09 72 65 74 75 72 6e 20 74 61 67 5f 69 64 3b 0a .return tag_id;.
0d70: 65 6e 64 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 61 62 end;.$$;..-- tab
0d80: 6c 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 72 le to hold all r
0d90: 75 6c 65 73 0a 43 52 45 41 54 45 20 54 41 42 4c ules.CREATE TABL
0da0: 45 20 72 75 6c 65 73 20 28 0a 09 6e 65 74 6d 61 E rules (..netma
0db0: 73 6b 20 63 69 64 72 20 4e 4f 54 20 4e 55 4c 4c sk cidr NOT NULL
0dc0: 2c 0a 09 72 65 64 69 72 65 63 74 5f 75 72 6c 20 ,..redirect_url
0dd0: 74 65 78 74 20 44 45 46 41 55 4c 54 20 27 61 62 text DEFAULT 'ab
0de0: 6f 75 74 3a 3a 62 6c 61 6e 6b 27 3a 3a 74 65 78 out::blank'::tex
0df0: 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 66 72 6f t NOT NULL,..fro
0e00: 6d 5f 77 65 65 6b 64 61 79 20 73 6d 61 6c 6c 69 m_weekday smalli
0e10: 6e 74 20 44 45 46 41 55 4c 54 20 30 20 4e 4f 54 nt DEFAULT 0 NOT
0e20: 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 77 65 65 6b 64 NULL,..to_weekd
0e30: 61 79 20 73 6d 61 6c 6c 69 6e 74 20 44 45 46 41 ay smallint DEFA
0e40: 55 4c 54 20 36 20 4e 4f 54 20 4e 55 4c 4c 2c 0a ULT 6 NOT NULL,.
0e50: 09 66 72 6f 6d 5f 74 69 6d 65 20 74 69 6d 65 20 .from_time time
0e60: 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e without time zon
0e70: 65 20 44 45 46 41 55 4c 54 20 27 30 30 3a 30 30 e DEFAULT '00:00
0e80: 3a 30 30 27 3a 3a 74 69 6d 65 20 77 69 74 68 6f :00'::time witho
0e90: 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 4e 4f 54 ut time zone NOT
0ea0: 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 74 69 6d 65 20 NULL,..to_time
0eb0: 74 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d time without tim
0ec0: 65 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 27 e zone DEFAULT '
0ed0: 32 33 3a 35 39 3a 35 39 27 3a 3a 74 69 6d 65 20 23:59:59'::time
0ee0: 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e without time zon
0ef0: 65 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 69 64 5f e NOT NULL,..id_
0f00: 74 61 67 20 73 6d 61 6c 6c 69 6e 74 20 4e 4f 54 tag smallint NOT
0f10: 20 4e 55 4c 4c 0a 29 3b 0a 0a 41 4c 54 45 52 20 NULL.);..ALTER
0f20: 54 41 42 4c 45 20 4f 4e 4c 59 20 72 75 6c 65 73 TABLE ONLY rules
0f30: 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 ..ADD CONSTRAINT
0f40: 20 72 75 6c 65 73 5f 70 6b 65 79 20 50 52 49 4d rules_pkey PRIM
0f50: 41 52 59 20 4b 45 59 20 28 6e 65 74 6d 61 73 6b ARY KEY (netmask
0f60: 29 3b 0a 0a 2d 2d 20 74 61 62 6c 65 20 74 6f 20 );..-- table to
0f70: 68 6f 6c 64 20 73 69 74 65 20 61 72 72 61 79 73 hold site arrays
0f80: 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 69 .CREATE TABLE si
0f90: 74 65 20 28 0a 09 69 64 5f 73 69 74 65 20 73 65 te (..id_site se
0fa0: 72 69 61 6c 2c 0a 09 73 69 74 65 20 74 65 78 74 rial,..site text
0fb0: 5b 5d 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a 0a [] NOT NULL.);..
0fc0: 41 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 ALTER TABLE ONLY
0fd0: 20 73 69 74 65 0a 09 41 44 44 20 43 4f 4e 53 54 site..ADD CONST
0fe0: 52 41 49 4e 54 20 73 69 74 65 5f 70 6b 65 79 20 RAINT site_pkey
0ff0: 50 52 49 4d 41 52 59 20 4b 45 59 20 28 69 64 5f PRIMARY KEY (id_
1000: 73 69 74 65 29 3b 0a 0a 43 52 45 41 54 45 20 55 site);..CREATE U
1010: 4e 49 51 55 45 20 49 4e 44 45 58 20 73 69 74 65 NIQUE INDEX site
1020: 5f 75 20 4f 4e 20 73 69 74 65 20 28 75 73 6f 72 _u ON site (usor
1030: 74 28 73 69 74 65 29 29 3b 0a 0a 43 52 45 41 54 t(site));..CREAT
1040: 45 20 49 4e 44 45 58 20 73 69 74 65 5f 67 20 4f E INDEX site_g O
1050: 4e 20 73 69 74 65 20 55 53 49 4e 47 20 67 69 6e N site USING gin
1060: 20 28 73 69 74 65 29 3b 0a 0a 2d 2d 20 74 61 62 (site);..-- tab
1070: 6c 65 20 74 6f 20 68 6f 6c 64 20 74 61 67 20 63 le to hold tag c
1080: 6f 6d 62 69 6e 61 74 69 6f 6e 73 0a 43 52 45 41 ombinations.CREA
1090: 54 45 20 54 41 42 4c 45 20 74 61 67 20 28 0a 09 TE TABLE tag (..
10a0: 69 64 5f 74 61 67 20 73 65 72 69 61 6c 2c 0a 09 id_tag serial,..
10b0: 74 61 67 20 74 65 78 74 5b 5d 20 4e 4f 54 20 4e tag text[] NOT N
10c0: 55 4c 4c 0a 29 3b 0a 0a 41 4c 54 45 52 20 54 41 ULL.);..ALTER TA
10d0: 42 4c 45 20 4f 4e 4c 59 20 74 61 67 0a 09 41 44 BLE ONLY tag..AD
10e0: 44 20 43 4f 4e 53 54 52 41 49 4e 54 20 74 61 67 D CONSTRAINT tag
10f0: 5f 70 6b 65 79 20 50 52 49 4d 41 52 59 20 4b 45 _pkey PRIMARY KE
1100: 59 20 28 69 64 5f 74 61 67 29 3b 0a 0a 43 52 45 Y (id_tag);..CRE
1110: 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58 ATE UNIQUE INDEX
1120: 20 74 61 67 5f 75 20 4f 4e 20 74 61 67 20 28 75 tag_u ON tag (u
1130: 73 6f 72 74 28 74 61 67 29 29 3b 0a 0a 43 52 45 sort(tag));..CRE
1140: 41 54 45 20 49 4e 44 45 58 20 74 61 67 5f 67 20 ATE INDEX tag_g
1150: 4f 4e 20 74 61 67 20 55 53 49 4e 47 20 67 69 6e ON tag USING gin
1160: 20 28 74 61 67 29 3b 0a 0a 2d 2d 20 74 61 62 6c (tag);..-- tabl
1170: 65 20 74 6f 20 68 6f 6c 64 20 74 61 67 20 2d 20 e to hold tag -
1180: 73 69 74 65 20 6c 69 6e 6b 73 0a 43 52 45 41 54 site links.CREAT
1190: 45 20 54 41 42 4c 45 20 75 72 6c 73 20 28 0a 09 E TABLE urls (..
11a0: 64 61 74 65 5f 61 64 64 65 64 20 74 69 6d 65 73 date_added times
11b0: 74 61 6d 70 20 77 69 74 68 6f 75 74 20 74 69 6d tamp without tim
11c0: 65 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 28 e zone DEFAULT (
11d0: 27 6e 6f 77 27 3a 3a 74 65 78 74 29 3a 3a 74 69 'now'::text)::ti
11e0: 6d 65 73 74 61 6d 70 28 30 29 20 77 69 74 68 6f mestamp(0) witho
11f0: 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 4e 4f 54 ut time zone NOT
1200: 20 4e 55 4c 4c 2c 0a 09 69 64 5f 73 69 74 65 20 NULL,..id_site
1210: 73 6d 61 6c 6c 69 6e 74 20 4e 4f 54 20 4e 55 4c smallint NOT NUL
1220: 4c 2c 0a 09 69 64 5f 74 61 67 20 73 6d 61 6c 6c L,..id_tag small
1230: 69 6e 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 72 int NOT NULL,..r
1240: 65 67 65 78 70 20 74 65 78 74 0a 29 3b 0a 0a 41 egexp text.);..A
1250: 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 LTER TABLE ONLY
1260: 75 72 6c 73 0a 09 41 44 44 20 43 4f 4e 53 54 52 urls..ADD CONSTR
1270: 41 49 4e 54 20 75 72 6c 73 5f 70 6b 65 79 20 50 AINT urls_pkey P
1280: 52 49 4d 41 52 59 20 4b 45 59 20 28 64 61 74 65 RIMARY KEY (date
1290: 5f 61 64 64 65 64 29 3b 0a 0a 43 52 45 41 54 45 _added);..CREATE
12a0: 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 75 72 UNIQUE INDEX ur
12b0: 6c 73 5f 69 64 5f 73 69 74 65 20 4f 4e 20 75 72 ls_id_site ON ur
12c0: 6c 73 20 55 53 49 4e 47 20 62 74 72 65 65 20 28 ls USING btree (
12d0: 69 64 5f 73 69 74 65 29 3b 0a 0a 43 52 45 41 54 id_site);..CREAT
12e0: 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 75 E UNIQUE INDEX u
12f0: 72 6c 73 5f 69 64 5f 74 61 67 20 4f 4e 20 75 72 rls_id_tag ON ur
1300: 6c 73 20 55 53 49 4e 47 20 62 74 72 65 65 20 28 ls USING btree (
1310: 69 64 5f 74 61 67 29 3b 0a 0a 2d 2d 20 72 75 6c id_tag);..-- rul
1320: 65 20 74 6f 20 6a 6f 69 6e 20 61 6c 6c 20 74 61 e to join all ta
1330: 62 6c 65 73 20 69 6e 74 6f 20 6f 6e 65 20 74 6f bles into one to
1340: 20 73 69 6d 70 6c 69 66 79 20 61 63 63 65 73 73 simplify access
1350: 0a 2d 2d 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c .-- automaticall
1360: 20 75 73 65 73 20 63 75 72 72 65 6e 74 20 64 61 uses current da
1370: 79 20 61 6e 64 20 74 69 6d 65 20 64 61 74 61 0a y and time data.
1380: 43 52 45 41 54 45 20 56 49 45 57 20 73 69 74 65 CREATE VIEW site
1390: 5f 72 75 6c 65 20 41 53 0a 53 45 4c 45 43 54 20 _rule AS.SELECT
13a0: 61 2e 72 65 64 69 72 65 63 74 5f 75 72 6c 2c 20 a.redirect_url,
13b0: 61 2e 6e 65 74 6d 61 73 6b 2c 20 62 2e 73 69 74 a.netmask, b.sit
13c0: 65 2c 20 62 2e 72 65 67 65 78 70 0a 46 52 4f 4d e, b.regexp.FROM
13d0: 20 28 28 0a 09 53 45 4c 45 43 54 20 72 75 6c 65 ((..SELECT rule
13e0: 73 2e 72 65 64 69 72 65 63 74 5f 75 72 6c 2c 20 s.redirect_url,
13f0: 74 61 67 2e 74 61 67 20 41 53 20 72 75 6c 65 5f tag.tag AS rule_
1400: 74 61 67 2c 20 72 75 6c 65 73 2e 6e 65 74 6d 61 tag, rules.netma
1410: 73 6b 0a 09 46 52 4f 4d 20 72 75 6c 65 73 20 4e sk..FROM rules N
1420: 41 54 55 52 41 4c 20 4a 4f 49 4e 20 74 61 67 0a ATURAL JOIN tag.
1430: 09 57 48 45 52 45 20 28 27 6e 6f 77 27 3a 3a 74 .WHERE ('now'::t
1440: 65 78 74 29 3a 3a 74 69 6d 65 20 77 69 74 68 6f ext)::time witho
1450: 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 3e 3d 20 ut time zone >=
1460: 72 75 6c 65 73 2e 66 72 6f 6d 5f 74 69 6d 65 0a rules.from_time.
1470: 09 09 41 4e 44 20 28 27 6e 6f 77 27 3a 3a 74 65 ..AND ('now'::te
1480: 78 74 29 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 xt)::time withou
1490: 74 20 74 69 6d 65 20 7a 6f 6e 65 20 3c 3d 20 72 t time zone <= r
14a0: 75 6c 65 73 2e 74 6f 5f 74 69 6d 65 0a 09 09 41 ules.to_time...A
14b0: 4e 44 20 64 61 74 65 5f 70 61 72 74 28 27 64 6f ND date_part('do
14c0: 77 27 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 29 29 w'::text, now())
14d0: 20 3e 3d 20 28 72 75 6c 65 73 2e 66 72 6f 6d 5f >= (rules.from_
14e0: 77 65 65 6b 64 61 79 29 3a 3a 64 6f 75 62 6c 65 weekday)::double
14f0: 20 70 72 65 63 69 73 69 6f 6e 0a 09 09 41 4e 44 precision...AND
1500: 20 64 61 74 65 5f 70 61 72 74 28 27 64 6f 77 27 date_part('dow'
1510: 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 29 29 20 3c ::text, now()) <
1520: 3d 20 28 72 75 6c 65 73 2e 74 6f 5f 77 65 65 6b = (rules.to_week
1530: 64 61 79 29 3a 3a 64 6f 75 62 6c 65 20 70 72 65 day)::double pre
1540: 63 69 73 69 6f 6e 0a 29 20 61 20 4a 4f 49 4e 20 cision.) a JOIN
1550: 28 0a 09 53 45 4c 45 43 54 20 73 69 74 65 2e 73 (..SELECT site.s
1560: 69 74 65 2c 20 74 61 67 2e 74 61 67 20 41 53 20 ite, tag.tag AS
1570: 75 72 6c 5f 74 61 67 2c 20 72 65 67 65 78 70 0a url_tag, regexp.
1580: 09 46 52 4f 4d 20 75 72 6c 73 20 4e 41 54 55 52 .FROM urls NATUR
1590: 41 4c 20 4a 4f 49 4e 20 74 61 67 20 4e 41 54 55 AL JOIN tag NATU
15a0: 52 41 4c 20 4a 4f 49 4e 20 73 69 74 65 0a 29 20 RAL JOIN site.)
15b0: 62 20 4f 4e 20 28 62 2e 75 72 6c 5f 74 61 67 20 b ON (b.url_tag
15c0: 26 26 20 61 2e 72 75 6c 65 5f 74 61 67 29 29 3b && a.rule_tag));
15d0: 0a .