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 69 6e 73 65 72 74 73 20 6f nction inserts o
07c0: 72 20 75 70 64 61 74 65 73 20 72 65 63 6f 72 64 r updates record
07d0: 20 77 69 74 68 20 74 61 67 73 20 74 6f 20 73 69 with tags to si
07e0: 74 65 20 62 79 20 73 69 74 65 20 69 64 20 77 69 te by site id wi
07f0: 74 68 20 72 65 67 65 78 70 0a 43 52 45 41 54 45 th regexp.CREATE
0800: 20 6f 72 20 72 65 70 6c 61 63 65 20 46 55 4e 43 or replace FUNC
0810: 54 49 4f 4e 20 6d 61 72 6b 28 6d 79 5f 69 64 5f TION mark(my_id_
0820: 73 69 74 65 20 69 6e 74 65 67 65 72 2c 20 6d 79 site integer, my
0830: 5f 69 64 5f 74 61 67 20 69 6e 74 65 67 65 72 2c _id_tag integer,
0840: 20 6d 79 5f 72 65 67 65 78 70 20 74 65 78 74 29 my_regexp text)
0850: 20 52 45 54 55 52 4e 53 20 69 6e 74 65 67 65 72 RETURNS integer
0860: 0a 09 4c 41 4e 47 55 41 47 45 20 70 6c 70 67 73 ..LANGUAGE plpgs
0870: 71 6c 20 53 54 52 49 43 54 0a 09 41 53 20 24 24 ql STRICT..AS $$
0880: 0a 64 65 63 6c 61 72 65 0a 09 2d 2d 20 6d 61 79 .declare..-- may
0890: 62 65 20 63 68 65 63 6b 20 73 68 6f 75 6c 64 20 be check should
08a0: 62 65 20 61 64 64 65 64 20 74 6f 20 6d 61 6b 65 be added to make
08b0: 20 73 75 72 65 20 73 75 70 70 6c 69 65 64 20 73 sure supplied s
08c0: 69 74 65 20 69 64 20 72 65 61 6c 6c 79 20 65 78 ite id really ex
08d0: 69 73 74 73 0a 09 6d 79 5f 74 61 67 20 74 65 78 ists..my_tag tex
08e0: 74 5b 5d 3b 0a 62 65 67 69 6e 0a 09 2d 2d 20 73 t[];.begin..-- s
08f0: 65 6c 65 63 74 69 6e 67 20 74 61 67 73 20 73 69 electing tags si
0900: 74 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 te already have
0910: 61 6e 64 20 61 64 64 69 6e 67 20 6e 65 77 20 74 and adding new t
0920: 61 67 20 74 6f 20 74 68 65 6d 0a 09 2d 2d 20 6e ag to them..-- n
0930: 6f 74 65 20 74 68 61 74 20 74 61 67 73 20 73 68 ote that tags sh
0940: 6f 75 6c 64 20 62 65 20 73 6f 72 74 65 64 20 74 ould be sorted t
0950: 6f 20 65 6c 69 6d 69 6e 61 74 65 20 70 65 72 6d o eliminate perm
0960: 75 74 61 74 69 6f 6e 73 0a 09 73 65 6c 65 63 74 utations..select
0970: 20 63 6f 61 6c 65 73 63 65 28 74 61 67 2c 20 27 coalesce(tag, '
0980: 7b 7d 27 3a 3a 74 65 78 74 5b 5d 29 20 66 72 6f {}'::text[]) fro
0990: 6d 20 75 72 6c 73 20 6e 61 74 75 72 61 6c 20 6c m urls natural l
09a0: 65 66 74 20 6a 6f 69 6e 20 74 61 67 0a 09 09 77 eft join tag...w
09b0: 68 65 72 65 20 69 64 5f 73 69 74 65 20 3d 20 6d here id_site = m
09c0: 79 5f 69 64 5f 73 69 74 65 20 61 6e 64 20 72 65 y_id_site and re
09d0: 67 65 78 70 20 3d 20 6d 79 5f 72 65 67 65 78 70 gexp = my_regexp
09e0: 20 69 6e 74 6f 20 6d 79 5f 74 61 67 3b 0a 09 69 into my_tag;..i
09f0: 66 20 6e 6f 74 20 66 6f 75 6e 64 20 74 68 65 6e f not found then
0a00: 0a 09 09 2d 2d 20 6e 6f 20 72 65 63 6f 72 64 73 ...-- no records
0a10: 20 66 6f 75 6e 64 20 2d 20 63 72 65 61 74 69 6e found - creatin
0a20: 67 20 6e 65 77 20 74 61 67 0a 09 09 69 6e 73 65 g new tag...inse
0a30: 72 74 20 69 6e 74 6f 20 75 72 6c 73 20 28 69 64 rt into urls (id
0a40: 5f 73 69 74 65 2c 20 69 64 5f 74 61 67 2c 20 72 _site, id_tag, r
0a50: 65 67 65 78 70 29 20 76 61 6c 75 65 73 20 28 6d egexp) values (m
0a60: 79 5f 69 64 5f 73 69 74 65 2c 20 6d 79 5f 69 64 y_id_site, my_id
0a70: 5f 74 61 67 2c 20 6d 79 5f 72 65 67 65 78 70 29 _tag, my_regexp)
0a80: 3b 0a 09 65 6c 73 65 0a 09 09 2d 2d 20 6a 6f 69 ;..else...-- joi
0a90: 6e 69 6e 67 20 74 61 67 73 0a 09 09 73 65 6c 65 ning tags...sele
0aa0: 63 74 20 75 73 6f 72 74 28 6d 79 5f 74 61 67 20 ct usort(my_tag
0ab0: 7c 7c 20 74 61 67 29 20 66 72 6f 6d 20 74 61 67 || tag) from tag
0ac0: 20 77 68 65 72 65 20 69 64 5f 74 61 67 20 3d 20 where id_tag =
0ad0: 6d 79 5f 69 64 5f 74 61 67 20 69 6e 74 6f 20 6d my_id_tag into m
0ae0: 79 5f 74 61 67 3b 0a 09 09 2d 2d 20 75 70 64 61 y_tag;...-- upda
0af0: 74 69 6e 67 20 65 78 69 73 74 69 6e 67 20 72 65 ting existing re
0b00: 63 6f 72 64 0a 09 09 75 70 64 61 74 65 20 75 72 cord...update ur
0b10: 6c 73 20 73 65 74 20 69 64 5f 74 61 67 20 3d 20 ls set id_tag =
0b20: 67 65 74 5f 74 61 67 28 6d 79 5f 74 61 67 29 0a get_tag(my_tag).
0b30: 09 09 09 77 68 65 72 65 20 69 64 5f 73 69 74 65 ...where id_site
0b40: 20 3d 20 6d 79 5f 69 64 5f 73 69 74 65 20 61 6e = my_id_site an
0b50: 64 20 72 65 67 65 78 70 20 3d 20 6d 79 5f 72 65 d regexp = my_re
0b60: 67 65 78 70 3b 0a 09 65 6e 64 20 69 66 3b 0a 09 gexp;..end if;..
0b70: 72 65 74 75 72 6e 20 6d 79 5f 69 64 5f 73 69 74 return my_id_sit
0b80: 65 3b 0a 65 6e 64 3b 0a 24 24 3b 0a 0a 2d 2d 20 e;.end;.$$;..--
0b90: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 this function ad
0ba0: 64 73 20 74 61 67 20 74 6f 20 73 69 74 65 20 62 ds tag to site b
0bb0: 79 20 73 69 74 65 20 69 64 0a 43 52 45 41 54 45 y site id.CREATE
0bc0: 20 6f 72 20 72 65 70 6c 61 63 65 20 46 55 4e 43 or replace FUNC
0bd0: 54 49 4f 4e 20 6d 61 72 6b 28 6d 79 5f 69 64 5f TION mark(my_id_
0be0: 73 69 74 65 20 69 6e 74 65 67 65 72 2c 20 6e 65 site integer, ne
0bf0: 77 5f 74 61 67 20 74 65 78 74 29 20 52 45 54 55 w_tag text) RETU
0c00: 52 4e 53 20 69 6e 74 65 67 65 72 0a 09 4c 41 4e RNS integer..LAN
0c10: 47 55 41 47 45 20 70 6c 70 67 73 71 6c 20 53 54 GUAGE plpgsql ST
0c20: 52 49 43 54 0a 09 41 53 20 24 24 0a 64 65 63 6c RICT..AS $$.decl
0c30: 61 72 65 0a 09 2d 2d 20 6d 61 79 62 65 20 63 68 are..-- maybe ch
0c40: 65 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 61 64 eck should be ad
0c50: 64 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 ded to make sure
0c60: 20 73 75 70 70 6c 69 65 64 20 73 69 74 65 20 69 supplied site i
0c70: 64 20 72 65 61 6c 6c 79 20 65 78 69 73 74 73 0a d really exists.
0c80: 09 6d 79 5f 74 61 67 20 74 65 78 74 5b 5d 3b 0a .my_tag text[];.
0c90: 62 65 67 69 6e 0a 09 2d 2d 20 73 65 6c 65 63 74 begin..-- select
0ca0: 69 6e 67 20 74 61 67 73 20 73 69 74 65 20 61 6c ing tags site al
0cb0: 72 65 61 64 79 20 68 61 76 65 20 61 6e 64 20 61 ready have and a
0cc0: 64 64 69 6e 67 20 6e 65 77 20 74 61 67 20 74 6f dding new tag to
0cd0: 20 74 68 65 6d 0a 09 2d 2d 20 6e 6f 74 65 20 74 them..-- note t
0ce0: 68 61 74 20 74 61 67 73 20 73 68 6f 75 6c 64 20 hat tags should
0cf0: 62 65 20 73 6f 72 74 65 64 20 74 6f 20 65 6c 69 be sorted to eli
0d00: 6d 69 6e 61 74 65 20 70 65 72 6d 75 74 61 74 69 minate permutati
0d10: 6f 6e 73 0a 09 73 65 6c 65 63 74 20 63 6f 61 6c ons..select coal
0d20: 65 73 63 65 28 74 61 67 2c 20 27 7b 7d 27 3a 3a esce(tag, '{}'::
0d30: 74 65 78 74 5b 5d 29 20 66 72 6f 6d 20 75 72 6c text[]) from url
0d40: 73 20 6e 61 74 75 72 61 6c 20 6c 65 66 74 20 6a s natural left j
0d50: 6f 69 6e 20 74 61 67 0a 09 09 77 68 65 72 65 20 oin tag...where
0d60: 69 64 5f 73 69 74 65 20 3d 20 6d 79 5f 69 64 5f id_site = my_id_
0d70: 73 69 74 65 20 61 6e 64 20 72 65 67 65 78 70 20 site and regexp
0d80: 69 73 20 6e 75 6c 6c 20 69 6e 74 6f 20 6d 79 5f is null into my_
0d90: 74 61 67 3b 0a 09 69 66 20 6e 6f 74 20 66 6f 75 tag;..if not fou
0da0: 6e 64 20 74 68 65 6e 0a 09 09 2d 2d 20 6e 6f 20 nd then...-- no
0db0: 72 65 63 6f 72 64 73 20 66 6f 75 6e 64 20 2d 20 records found -
0dc0: 63 72 65 61 74 69 6e 67 20 6e 65 77 20 74 61 67 creating new tag
0dd0: 0a 09 09 69 6e 73 65 72 74 20 69 6e 74 6f 20 75 ...insert into u
0de0: 72 6c 73 20 28 69 64 5f 73 69 74 65 2c 20 69 64 rls (id_site, id
0df0: 5f 74 61 67 29 20 76 61 6c 75 65 73 20 28 6d 79 _tag) values (my
0e00: 5f 69 64 5f 73 69 74 65 2c 20 67 65 74 5f 74 61 _id_site, get_ta
0e10: 67 28 61 72 72 61 79 5b 6e 65 77 5f 74 61 67 5d g(array[new_tag]
0e20: 29 29 3b 0a 09 65 6c 73 65 0a 09 09 2d 2d 20 6a ));..else...-- j
0e30: 6f 69 6e 69 6e 67 20 74 61 67 73 0a 09 09 73 65 oining tags...se
0e40: 6c 65 63 74 20 75 73 6f 72 74 28 6d 79 5f 74 61 lect usort(my_ta
0e50: 67 20 7c 7c 20 61 72 72 61 79 5b 6e 65 77 5f 74 g || array[new_t
0e60: 61 67 5d 29 20 69 6e 74 6f 20 6d 79 5f 74 61 67 ag]) into my_tag
0e70: 3b 0a 09 09 2d 2d 20 75 70 64 61 74 69 6e 67 20 ;...-- updating
0e80: 65 78 69 73 74 69 6e 67 20 72 65 63 6f 72 64 0a existing record.
0e90: 09 09 75 70 64 61 74 65 20 75 72 6c 73 20 73 65 ..update urls se
0ea0: 74 20 69 64 5f 74 61 67 20 3d 20 67 65 74 5f 74 t id_tag = get_t
0eb0: 61 67 28 6d 79 5f 74 61 67 29 20 77 68 65 72 65 ag(my_tag) where
0ec0: 20 69 64 5f 73 69 74 65 20 3d 20 6d 79 5f 69 64 id_site = my_id
0ed0: 5f 73 69 74 65 20 61 6e 64 20 72 65 67 65 78 70 _site and regexp
0ee0: 20 69 73 20 6e 75 6c 6c 3b 0a 09 65 6e 64 20 69 is null;..end i
0ef0: 66 3b 0a 09 72 65 74 75 72 6e 20 6d 79 5f 69 64 f;..return my_id
0f00: 5f 73 69 74 65 3b 0a 65 6e 64 3b 0a 24 24 3b 0a _site;.end;.$$;.
0f10: 0a 2d 2d 20 74 68 69 73 20 66 75 6e 63 74 69 6f .-- this functio
0f20: 6e 20 61 64 64 73 20 74 61 67 20 74 6f 20 64 6f n adds tag to do
0f30: 6d 61 69 6e 0a 43 52 45 41 54 45 20 6f 72 20 72 main.CREATE or r
0f40: 65 70 6c 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 eplace FUNCTION
0f50: 6d 61 72 6b 28 64 6f 6d 61 69 6e 20 74 65 78 74 mark(domain text
0f60: 2c 20 6e 65 77 5f 74 61 67 20 74 65 78 74 29 20 , new_tag text)
0f70: 52 45 54 55 52 4e 53 20 69 6e 74 65 67 65 72 0a RETURNS integer.
0f80: 09 4c 41 4e 47 55 41 47 45 20 73 71 6c 20 69 6d .LANGUAGE sql im
0f90: 6d 75 74 61 62 6c 65 20 53 54 52 49 43 54 0a 09 mutable STRICT..
0fa0: 41 53 20 24 24 0a 73 65 6c 65 63 74 20 6d 61 72 AS $$.select mar
0fb0: 6b 28 67 65 74 5f 73 69 74 65 28 24 31 29 2c 20 k(get_site($1),
0fc0: 24 32 29 20 61 73 20 72 65 73 75 6c 74 3b 0a 24 $2) as result;.$
0fd0: 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 66 75 6e 63 $;..-- this func
0fe0: 74 69 6f 6e 20 73 65 74 73 20 74 61 67 73 20 66 tion sets tags f
0ff0: 6f 72 20 73 69 74 65 2f 72 65 67 65 78 70 20 70 or site/regexp p
1000: 61 69 72 0a 43 52 45 41 54 45 20 6f 72 20 72 65 air.CREATE or re
1010: 70 6c 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 73 place FUNCTION s
1020: 65 74 28 6d 79 5f 69 64 5f 73 69 74 65 20 69 6e et(my_id_site in
1030: 74 65 67 65 72 2c 20 6d 79 5f 69 64 5f 74 61 67 teger, my_id_tag
1040: 20 69 6e 74 65 67 65 72 29 20 52 45 54 55 52 4e integer) RETURN
1050: 53 20 69 6e 74 65 67 65 72 0a 09 4c 41 4e 47 55 S integer..LANGU
1060: 41 47 45 20 73 71 6c 20 53 54 52 49 43 54 0a 09 AGE sql STRICT..
1070: 41 53 20 24 24 0a 64 65 6c 65 74 65 20 66 72 6f AS $$.delete fro
1080: 6d 20 75 72 6c 73 20 77 68 65 72 65 20 24 31 20 m urls where $1
1090: 3d 20 69 64 5f 73 69 74 65 20 61 6e 64 20 72 65 = id_site and re
10a0: 67 65 78 70 20 69 73 20 6e 75 6c 6c 3b 0a 69 6e gexp is null;.in
10b0: 73 65 72 74 20 69 6e 74 6f 20 75 72 6c 73 20 28 sert into urls (
10c0: 69 64 5f 73 69 74 65 2c 20 69 64 5f 74 61 67 29 id_site, id_tag)
10d0: 20 76 61 6c 75 65 73 20 28 24 31 2c 20 24 32 29 values ($1, $2)
10e0: 3b 0a 73 65 6c 65 63 74 20 24 31 3b 0a 24 24 3b ;.select $1;.$$;
10f0: 0a 0a 2d 2d 20 74 68 69 73 20 66 75 6e 63 74 69 ..-- this functi
1100: 6f 6e 20 73 65 74 73 20 74 61 67 73 20 66 6f 72 on sets tags for
1110: 20 73 69 74 65 2f 72 65 67 65 78 70 20 70 61 69 site/regexp pai
1120: 72 0a 43 52 45 41 54 45 20 6f 72 20 72 65 70 6c r.CREATE or repl
1130: 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 73 65 74 ace FUNCTION set
1140: 28 6d 79 5f 69 64 5f 73 69 74 65 20 69 6e 74 65 (my_id_site inte
1150: 67 65 72 2c 20 6d 79 5f 69 64 5f 74 61 67 20 69 ger, my_id_tag i
1160: 6e 74 65 67 65 72 2c 20 6d 79 5f 72 65 67 65 78 nteger, my_regex
1170: 70 20 74 65 78 74 29 20 52 45 54 55 52 4e 53 20 p text) RETURNS
1180: 69 6e 74 65 67 65 72 0a 09 4c 41 4e 47 55 41 47 integer..LANGUAG
1190: 45 20 73 71 6c 20 53 54 52 49 43 54 0a 09 41 53 E sql STRICT..AS
11a0: 20 24 24 0a 64 65 6c 65 74 65 20 66 72 6f 6d 20 $$.delete from
11b0: 75 72 6c 73 20 77 68 65 72 65 20 24 31 20 3d 20 urls where $1 =
11c0: 69 64 5f 73 69 74 65 20 61 6e 64 20 24 33 20 3d id_site and $3 =
11d0: 20 72 65 67 65 78 70 3b 0a 69 6e 73 65 72 74 20 regexp;.insert
11e0: 69 6e 74 6f 20 75 72 6c 73 20 28 69 64 5f 73 69 into urls (id_si
11f0: 74 65 2c 20 69 64 5f 74 61 67 2c 20 72 65 67 65 te, id_tag, rege
1200: 78 70 29 20 76 61 6c 75 65 73 20 28 24 31 2c 20 xp) values ($1,
1210: 24 32 2c 20 24 33 29 3b 0a 73 65 6c 65 63 74 20 $2, $3);.select
1220: 24 31 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 $1;.$$;..-- this
1230: 20 66 75 6e 63 74 69 6f 6e 20 73 74 6f 72 65 73 function stores
1240: 20 6e 65 77 20 64 61 74 61 20 66 6f 72 20 73 69 new data for si
1250: 74 65 2f 72 65 67 65 78 70 20 70 61 69 72 0a 63 te/regexp pair.c
1260: 72 65 61 74 65 20 6f 72 20 72 65 70 6c 61 63 65 reate or replace
1270: 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 28 64 6f function set(do
1280: 6d 61 69 6e 20 74 65 78 74 2c 20 74 61 67 73 20 main text, tags
1290: 74 65 78 74 2c 20 72 65 67 65 78 70 20 74 65 78 text, regexp tex
12a0: 74 29 20 72 65 74 75 72 6e 73 20 69 6e 74 65 67 t) returns integ
12b0: 65 72 0a 09 6c 61 6e 67 75 61 67 65 20 73 71 6c er..language sql
12c0: 20 69 6d 6d 75 74 61 62 6c 65 20 73 74 72 69 63 immutable stric
12d0: 74 0a 09 61 73 20 24 24 0a 73 65 6c 65 63 74 20 t..as $$.select
12e0: 73 65 74 28 67 65 74 5f 73 69 74 65 28 24 31 29 set(get_site($1)
12f0: 2c 20 67 65 74 5f 74 61 67 28 24 32 3a 3a 74 65 , get_tag($2::te
1300: 78 74 5b 5d 29 2c 20 24 33 29 3b 0a 24 24 3b 0a xt[]), $3);.$$;.
1310: 0a 2d 2d 20 74 68 69 73 20 66 75 6e 63 74 69 6f .-- this functio
1320: 6e 20 73 74 6f 72 65 73 20 6e 65 77 20 64 61 74 n stores new dat
1330: 61 20 66 6f 72 20 73 69 74 65 2f 72 65 67 65 78 a for site/regex
1340: 70 20 70 61 69 72 0a 63 72 65 61 74 65 20 6f 72 p pair.create or
1350: 20 72 65 70 6c 61 63 65 20 66 75 6e 63 74 69 6f replace functio
1360: 6e 20 73 65 74 28 64 6f 6d 61 69 6e 20 74 65 78 n set(domain tex
1370: 74 2c 20 74 61 67 73 20 74 65 78 74 29 20 72 65 t, tags text) re
1380: 74 75 72 6e 73 20 69 6e 74 65 67 65 72 0a 09 6c turns integer..l
1390: 61 6e 67 75 61 67 65 20 73 71 6c 20 69 6d 6d 75 anguage sql immu
13a0: 74 61 62 6c 65 20 73 74 72 69 63 74 0a 09 61 73 table strict..as
13b0: 20 24 24 0a 73 65 6c 65 63 74 20 73 65 74 28 67 $$.select set(g
13c0: 65 74 5f 73 69 74 65 28 24 31 29 2c 20 67 65 74 et_site($1), get
13d0: 5f 74 61 67 28 24 32 3a 3a 74 65 78 74 5b 5d 29 _tag($2::text[])
13e0: 29 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 );.$$;..-- this
13f0: 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
1400: 20 69 64 20 6f 66 20 74 61 67 20 61 72 72 61 79 id of tag array
1410: 0a 63 72 65 61 74 65 20 6f 72 20 72 65 70 6c 61 .create or repla
1420: 63 65 20 66 75 6e 63 74 69 6f 6e 20 67 65 74 5f ce function get_
1430: 74 61 67 28 6d 79 5f 74 61 67 20 74 65 78 74 5b tag(my_tag text[
1440: 5d 29 20 72 65 74 75 72 6e 73 20 69 6e 74 65 67 ]) returns integ
1450: 65 72 0a 09 6c 61 6e 67 75 61 67 65 20 70 6c 70 er..language plp
1460: 67 73 71 6c 20 73 74 72 69 63 74 0a 09 61 73 20 gsql strict..as
1470: 24 24 0a 64 65 63 6c 61 72 65 0a 09 74 61 67 5f $$.declare..tag_
1480: 69 64 20 69 6e 74 65 67 65 72 3b 0a 62 65 67 69 id integer;.begi
1490: 6e 0a 09 73 65 6c 65 63 74 20 69 64 5f 74 61 67 n..select id_tag
14a0: 20 66 72 6f 6d 20 74 61 67 20 77 68 65 72 65 20 from tag where
14b0: 75 73 6f 72 74 28 6d 79 5f 74 61 67 29 20 3d 20 usort(my_tag) =
14c0: 74 61 67 20 69 6e 74 6f 20 74 61 67 5f 69 64 3b tag into tag_id;
14d0: 0a 09 69 66 20 6e 6f 74 20 66 6f 75 6e 64 20 74 ..if not found t
14e0: 68 65 6e 0a 09 09 69 6e 73 65 72 74 20 69 6e 74 hen...insert int
14f0: 6f 20 74 61 67 20 28 74 61 67 29 20 76 61 6c 75 o tag (tag) valu
1500: 65 73 20 28 75 73 6f 72 74 28 6d 79 5f 74 61 67 es (usort(my_tag
1510: 29 29 3b 0a 09 09 73 65 6c 65 63 74 20 69 64 5f ));...select id_
1520: 74 61 67 20 66 72 6f 6d 20 74 61 67 20 77 68 65 tag from tag whe
1530: 72 65 20 75 73 6f 72 74 28 6d 79 5f 74 61 67 29 re usort(my_tag)
1540: 20 3d 20 74 61 67 20 69 6e 74 6f 20 74 61 67 5f = tag into tag_
1550: 69 64 3b 0a 09 65 6e 64 20 69 66 3b 0a 09 72 65 id;..end if;..re
1560: 74 75 72 6e 20 74 61 67 5f 69 64 3b 0a 65 6e 64 turn tag_id;.end
1570: 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 61 62 6c 65 20 ;.$$;..-- table
1580: 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 72 75 6c 65 to hold all rule
1590: 73 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 72 s.CREATE TABLE r
15a0: 75 6c 65 73 20 28 0a 09 6e 65 74 6d 61 73 6b 20 ules (..netmask
15b0: 63 69 64 72 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 cidr NOT NULL,..
15c0: 72 65 64 69 72 65 63 74 5f 75 72 6c 20 74 65 78 redirect_url tex
15d0: 74 20 44 45 46 41 55 4c 54 20 27 61 62 6f 75 74 t DEFAULT 'about
15e0: 3a 3a 62 6c 61 6e 6b 27 3a 3a 74 65 78 74 20 4e ::blank'::text N
15f0: 4f 54 20 4e 55 4c 4c 2c 0a 09 66 72 6f 6d 5f 77 OT NULL,..from_w
1600: 65 65 6b 64 61 79 20 73 6d 61 6c 6c 69 6e 74 20 eekday smallint
1610: 44 45 46 41 55 4c 54 20 30 20 4e 4f 54 20 4e 55 DEFAULT 0 NOT NU
1620: 4c 4c 2c 0a 09 74 6f 5f 77 65 65 6b 64 61 79 20 LL,..to_weekday
1630: 73 6d 61 6c 6c 69 6e 74 20 44 45 46 41 55 4c 54 smallint DEFAULT
1640: 20 36 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 66 72 6 NOT NULL,..fr
1650: 6f 6d 5f 74 69 6d 65 20 74 69 6d 65 20 77 69 74 om_time time wit
1660: 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 44 hout time zone D
1670: 45 46 41 55 4c 54 20 27 30 30 3a 30 30 3a 30 30 EFAULT '00:00:00
1680: 27 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 74 20 '::time without
1690: 74 69 6d 65 20 7a 6f 6e 65 20 4e 4f 54 20 4e 55 time zone NOT NU
16a0: 4c 4c 2c 0a 09 74 6f 5f 74 69 6d 65 20 74 69 6d LL,..to_time tim
16b0: 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a e without time z
16c0: 6f 6e 65 20 44 45 46 41 55 4c 54 20 27 32 33 3a one DEFAULT '23:
16d0: 35 39 3a 35 39 27 3a 3a 74 69 6d 65 20 77 69 74 59:59'::time wit
16e0: 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 4e hout time zone N
16f0: 4f 54 20 4e 55 4c 4c 2c 0a 09 69 64 5f 74 61 67 OT NULL,..id_tag
1700: 20 73 6d 61 6c 6c 69 6e 74 20 4e 4f 54 20 4e 55 smallint NOT NU
1710: 4c 4c 0a 29 3b 0a 0a 41 4c 54 45 52 20 54 41 42 LL.);..ALTER TAB
1720: 4c 45 20 4f 4e 4c 59 20 72 75 6c 65 73 0a 09 41 LE ONLY rules..A
1730: 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 20 72 75 DD CONSTRAINT ru
1740: 6c 65 73 5f 70 6b 65 79 20 50 52 49 4d 41 52 59 les_pkey PRIMARY
1750: 20 4b 45 59 20 28 6e 65 74 6d 61 73 6b 29 3b 0a KEY (netmask);.
1760: 0a 41 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c .ALTER TABLE ONL
1770: 59 20 72 75 6c 65 73 0a 09 41 44 44 20 43 4f 4e Y rules..ADD CON
1780: 53 54 52 41 49 4e 54 20 72 75 6c 65 73 5f 74 61 STRAINT rules_ta
1790: 67 5f 66 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 g_f FOREIGN KEY
17a0: 28 69 64 5f 74 61 67 29 20 52 45 46 45 52 45 4e (id_tag) REFEREN
17b0: 43 45 53 20 74 61 67 28 69 64 5f 74 61 67 29 20 CES tag(id_tag)
17c0: 4d 41 54 43 48 20 46 55 4c 4c 0a 09 4f 4e 20 55 MATCH FULL..ON U
17d0: 50 44 41 54 45 20 52 45 53 54 52 49 43 54 20 4f PDATE RESTRICT O
17e0: 4e 20 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 N DELETE RESTRIC
17f0: 54 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 T DEFERRABLE INI
1800: 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 3b TIALLY DEFERRED;
1810: 0a 0a 2d 2d 20 74 61 62 6c 65 20 74 6f 20 68 6f ..-- table to ho
1820: 6c 64 20 73 69 74 65 20 61 72 72 61 79 73 0a 43 ld site arrays.C
1830: 52 45 41 54 45 20 54 41 42 4c 45 20 73 69 74 65 REATE TABLE site
1840: 20 28 0a 09 69 64 5f 73 69 74 65 20 73 65 72 69 (..id_site seri
1850: 61 6c 2c 0a 09 73 69 74 65 20 74 65 78 74 5b 5d al,..site text[]
1860: 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a 0a 41 4c NOT NULL.);..AL
1870: 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 73 TER TABLE ONLY s
1880: 69 74 65 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 ite..ADD CONSTRA
1890: 49 4e 54 20 73 69 74 65 5f 70 6b 65 79 20 50 52 INT site_pkey PR
18a0: 49 4d 41 52 59 20 4b 45 59 20 28 69 64 5f 73 69 IMARY KEY (id_si
18b0: 74 65 29 3b 0a 0a 43 52 45 41 54 45 20 55 4e 49 te);..CREATE UNI
18c0: 51 55 45 20 49 4e 44 45 58 20 73 69 74 65 5f 75 QUE INDEX site_u
18d0: 20 4f 4e 20 73 69 74 65 20 28 75 73 6f 72 74 28 ON site (usort(
18e0: 73 69 74 65 29 29 3b 0a 0a 43 52 45 41 54 45 20 site));..CREATE
18f0: 49 4e 44 45 58 20 73 69 74 65 5f 67 20 4f 4e 20 INDEX site_g ON
1900: 73 69 74 65 20 55 53 49 4e 47 20 67 69 6e 20 28 site USING gin (
1910: 73 69 74 65 29 3b 0a 0a 2d 2d 20 74 61 62 6c 65 site);..-- table
1920: 20 74 6f 20 68 6f 6c 64 20 74 61 67 20 63 6f 6d to hold tag com
1930: 62 69 6e 61 74 69 6f 6e 73 0a 43 52 45 41 54 45 binations.CREATE
1940: 20 54 41 42 4c 45 20 74 61 67 20 28 0a 09 69 64 TABLE tag (..id
1950: 5f 74 61 67 20 73 65 72 69 61 6c 2c 0a 09 74 61 _tag serial,..ta
1960: 67 20 74 65 78 74 5b 5d 20 4e 4f 54 20 4e 55 4c g text[] NOT NUL
1970: 4c 0a 29 3b 0a 0a 41 4c 54 45 52 20 54 41 42 4c L.);..ALTER TABL
1980: 45 20 4f 4e 4c 59 20 74 61 67 0a 09 41 44 44 20 E ONLY tag..ADD
1990: 43 4f 4e 53 54 52 41 49 4e 54 20 74 61 67 5f 70 CONSTRAINT tag_p
19a0: 6b 65 79 20 50 52 49 4d 41 52 59 20 4b 45 59 20 key PRIMARY KEY
19b0: 28 69 64 5f 74 61 67 29 3b 0a 0a 43 52 45 41 54 (id_tag);..CREAT
19c0: 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 74 E UNIQUE INDEX t
19d0: 61 67 5f 75 20 4f 4e 20 74 61 67 20 28 75 73 6f ag_u ON tag (uso
19e0: 72 74 28 74 61 67 29 29 3b 0a 0a 43 52 45 41 54 rt(tag));..CREAT
19f0: 45 20 49 4e 44 45 58 20 74 61 67 5f 67 20 4f 4e E INDEX tag_g ON
1a00: 20 74 61 67 20 55 53 49 4e 47 20 67 69 6e 20 28 tag USING gin (
1a10: 74 61 67 29 3b 0a 0a 2d 2d 20 74 61 62 6c 65 20 tag);..-- table
1a20: 74 6f 20 68 6f 6c 64 20 74 61 67 20 2d 20 73 69 to hold tag - si
1a30: 74 65 20 6c 69 6e 6b 73 0a 43 52 45 41 54 45 20 te links.CREATE
1a40: 54 41 42 4c 45 20 75 72 6c 73 20 28 0a 09 64 61 TABLE urls (..da
1a50: 74 65 5f 61 64 64 65 64 20 74 69 6d 65 73 74 61 te_added timesta
1a60: 6d 70 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 mp without time
1a70: 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 28 27 6e zone DEFAULT ('n
1a80: 6f 77 27 3a 3a 74 65 78 74 29 3a 3a 74 69 6d 65 ow'::text)::time
1a90: 73 74 61 6d 70 28 30 29 20 77 69 74 68 6f 75 74 stamp(0) without
1aa0: 20 74 69 6d 65 20 7a 6f 6e 65 20 4e 4f 54 20 4e time zone NOT N
1ab0: 55 4c 4c 2c 0a 09 69 64 5f 73 69 74 65 20 73 6d ULL,..id_site sm
1ac0: 61 6c 6c 69 6e 74 20 4e 4f 54 20 4e 55 4c 4c 2c allint NOT NULL,
1ad0: 0a 09 69 64 5f 74 61 67 20 73 6d 61 6c 6c 69 6e ..id_tag smallin
1ae0: 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 72 65 67 t NOT NULL,..reg
1af0: 65 78 70 20 74 65 78 74 0a 29 3b 0a 0a 43 52 45 exp text.);..CRE
1b00: 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58 ATE UNIQUE INDEX
1b10: 20 75 72 6c 73 5f 70 6b 65 79 20 4f 4e 20 75 72 urls_pkey ON ur
1b20: 6c 73 20 55 53 49 4e 47 20 62 74 72 65 65 20 28 ls USING btree (
1b30: 69 64 5f 73 69 74 65 2c 20 72 65 67 65 78 70 29 id_site, regexp)
1b40: 3b 0a 0a 43 52 45 41 54 45 20 49 4e 44 45 58 20 ;..CREATE INDEX
1b50: 75 72 6c 73 5f 69 64 5f 74 61 67 20 4f 4e 20 75 urls_id_tag ON u
1b60: 72 6c 73 20 55 53 49 4e 47 20 62 74 72 65 65 20 rls USING btree
1b70: 28 69 64 5f 74 61 67 29 3b 0a 0a 41 4c 54 45 52 (id_tag);..ALTER
1b80: 20 54 41 42 4c 45 20 4f 4e 4c 59 20 75 72 6c 73 TABLE ONLY urls
1b90: 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 ..ADD CONSTRAINT
1ba0: 20 75 72 6c 73 5f 73 69 74 65 5f 66 20 46 4f 52 urls_site_f FOR
1bb0: 45 49 47 4e 20 4b 45 59 20 28 69 64 5f 73 69 74 EIGN KEY (id_sit
1bc0: 65 29 20 52 45 46 45 52 45 4e 43 45 53 20 73 69 e) REFERENCES si
1bd0: 74 65 28 69 64 5f 73 69 74 65 29 20 4d 41 54 43 te(id_site) MATC
1be0: 48 20 46 55 4c 4c 0a 09 4f 4e 20 55 50 44 41 54 H FULL..ON UPDAT
1bf0: 45 20 52 45 53 54 52 49 43 54 20 4f 4e 20 44 45 E RESTRICT ON DE
1c00: 4c 45 54 45 20 52 45 53 54 52 49 43 54 20 44 45 LETE RESTRICT DE
1c10: 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c FERRABLE INITIAL
1c20: 4c 59 20 44 45 46 45 52 52 45 44 3b 0a 0a 41 4c LY DEFERRED;..AL
1c30: 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 75 TER TABLE ONLY u
1c40: 72 6c 73 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 rls..ADD CONSTRA
1c50: 49 4e 54 20 75 72 6c 73 5f 74 61 67 5f 66 20 46 INT urls_tag_f F
1c60: 4f 52 45 49 47 4e 20 4b 45 59 20 28 69 64 5f 74 OREIGN KEY (id_t
1c70: 61 67 29 20 52 45 46 45 52 45 4e 43 45 53 20 74 ag) REFERENCES t
1c80: 61 67 28 69 64 5f 74 61 67 29 20 4d 41 54 43 48 ag(id_tag) MATCH
1c90: 20 46 55 4c 4c 0a 09 4f 4e 20 55 50 44 41 54 45 FULL..ON UPDATE
1ca0: 20 52 45 53 54 52 49 43 54 20 4f 4e 20 44 45 4c RESTRICT ON DEL
1cb0: 45 54 45 20 52 45 53 54 52 49 43 54 20 44 45 46 ETE RESTRICT DEF
1cc0: 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c ERRABLE INITIALL
1cd0: 59 20 44 45 46 45 52 52 45 44 3b 0a 0a 2d 2d 20 Y DEFERRED;..--
1ce0: 72 75 6c 65 20 74 6f 20 6a 6f 69 6e 20 61 6c 6c rule to join all
1cf0: 20 74 61 62 6c 65 73 20 69 6e 74 6f 20 6f 6e 65 tables into one
1d00: 20 74 6f 20 73 69 6d 70 6c 69 66 79 20 61 63 63 to simplify acc
1d10: 65 73 73 0a 2d 2d 20 61 75 74 6f 6d 61 74 69 63 ess.-- automatic
1d20: 61 6c 6c 20 75 73 65 73 20 63 75 72 72 65 6e 74 all uses current
1d30: 20 64 61 79 20 61 6e 64 20 74 69 6d 65 20 64 61 day and time da
1d40: 74 61 0a 43 52 45 41 54 45 20 56 49 45 57 20 73 ta.CREATE VIEW s
1d50: 69 74 65 5f 72 75 6c 65 20 41 53 0a 53 45 4c 45 ite_rule AS.SELE
1d60: 43 54 20 61 2e 72 65 64 69 72 65 63 74 5f 75 72 CT a.redirect_ur
1d70: 6c 2c 20 61 2e 6e 65 74 6d 61 73 6b 2c 20 62 2e l, a.netmask, b.
1d80: 73 69 74 65 2c 20 62 2e 72 65 67 65 78 70 0a 46 site, b.regexp.F
1d90: 52 4f 4d 20 28 28 0a 09 53 45 4c 45 43 54 20 72 ROM ((..SELECT r
1da0: 75 6c 65 73 2e 72 65 64 69 72 65 63 74 5f 75 72 ules.redirect_ur
1db0: 6c 2c 20 74 61 67 2e 74 61 67 20 41 53 20 72 75 l, tag.tag AS ru
1dc0: 6c 65 5f 74 61 67 2c 20 72 75 6c 65 73 2e 6e 65 le_tag, rules.ne
1dd0: 74 6d 61 73 6b 0a 09 46 52 4f 4d 20 72 75 6c 65 tmask..FROM rule
1de0: 73 20 4e 41 54 55 52 41 4c 20 4a 4f 49 4e 20 74 s NATURAL JOIN t
1df0: 61 67 0a 09 57 48 45 52 45 20 28 27 6e 6f 77 27 ag..WHERE ('now'
1e00: 3a 3a 74 65 78 74 29 3a 3a 74 69 6d 65 20 77 69 ::text)::time wi
1e10: 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 thout time zone
1e20: 3e 3d 20 72 75 6c 65 73 2e 66 72 6f 6d 5f 74 69 >= rules.from_ti
1e30: 6d 65 0a 09 09 41 4e 44 20 28 27 6e 6f 77 27 3a me...AND ('now':
1e40: 3a 74 65 78 74 29 3a 3a 74 69 6d 65 20 77 69 74 :text)::time wit
1e50: 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 3c hout time zone <
1e60: 3d 20 72 75 6c 65 73 2e 74 6f 5f 74 69 6d 65 0a = rules.to_time.
1e70: 09 09 41 4e 44 20 64 61 74 65 5f 70 61 72 74 28 ..AND date_part(
1e80: 27 64 6f 77 27 3a 3a 74 65 78 74 2c 20 6e 6f 77 'dow'::text, now
1e90: 28 29 29 20 3e 3d 20 28 72 75 6c 65 73 2e 66 72 ()) >= (rules.fr
1ea0: 6f 6d 5f 77 65 65 6b 64 61 79 29 3a 3a 64 6f 75 om_weekday)::dou
1eb0: 62 6c 65 20 70 72 65 63 69 73 69 6f 6e 0a 09 09 ble precision...
1ec0: 41 4e 44 20 64 61 74 65 5f 70 61 72 74 28 27 64 AND date_part('d
1ed0: 6f 77 27 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 29 ow'::text, now()
1ee0: 29 20 3c 3d 20 28 72 75 6c 65 73 2e 74 6f 5f 77 ) <= (rules.to_w
1ef0: 65 65 6b 64 61 79 29 3a 3a 64 6f 75 62 6c 65 20 eekday)::double
1f00: 70 72 65 63 69 73 69 6f 6e 0a 29 20 61 20 4a 4f precision.) a JO
1f10: 49 4e 20 28 0a 09 53 45 4c 45 43 54 20 73 69 74 IN (..SELECT sit
1f20: 65 2e 73 69 74 65 2c 20 74 61 67 2e 74 61 67 20 e.site, tag.tag
1f30: 41 53 20 75 72 6c 5f 74 61 67 2c 20 72 65 67 65 AS url_tag, rege
1f40: 78 70 0a 09 46 52 4f 4d 20 75 72 6c 73 20 4e 41 xp..FROM urls NA
1f50: 54 55 52 41 4c 20 4a 4f 49 4e 20 74 61 67 20 4e TURAL JOIN tag N
1f60: 41 54 55 52 41 4c 20 4a 4f 49 4e 20 73 69 74 65 ATURAL JOIN site
1f70: 0a 29 20 62 20 4f 4e 20 28 62 2e 75 72 6c 5f 74 .) b ON (b.url_t
1f80: 61 67 20 26 26 20 61 2e 72 75 6c 65 5f 74 61 67 ag && a.rule_tag
1f90: 29 29 3b 0a ));.