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 20 77 69 74 68 6f 75 74 20 or site without
1000: 72 65 67 65 78 70 0a 43 52 45 41 54 45 20 6f 72 regexp.CREATE or
1010: 20 72 65 70 6c 61 63 65 20 46 55 4e 43 54 49 4f replace FUNCTIO
1020: 4e 20 73 65 74 28 6d 79 5f 69 64 5f 73 69 74 65 N set(my_id_site
1030: 20 69 6e 74 65 67 65 72 2c 20 6d 79 5f 69 64 5f integer, my_id_
1040: 74 61 67 20 69 6e 74 65 67 65 72 29 20 52 45 54 tag integer) RET
1050: 55 52 4e 53 20 69 6e 74 65 67 65 72 0a 09 4c 41 URNS integer..LA
1060: 4e 47 55 41 47 45 20 73 71 6c 20 53 54 52 49 43 NGUAGE sql STRIC
1070: 54 0a 09 41 53 20 24 24 0a 64 65 6c 65 74 65 20 T..AS $$.delete
1080: 66 72 6f 6d 20 75 72 6c 73 20 77 68 65 72 65 20 from urls where
1090: 24 31 20 3d 20 69 64 5f 73 69 74 65 20 61 6e 64 $1 = id_site and
10a0: 20 72 65 67 65 78 70 20 69 73 20 4e 55 4c 4c 3b regexp is NULL;
10b0: 0a 69 6e 73 65 72 74 20 69 6e 74 6f 20 75 72 6c .insert into url
10c0: 73 20 28 69 64 5f 73 69 74 65 2c 20 69 64 5f 74 s (id_site, id_t
10d0: 61 67 29 20 76 61 6c 75 65 73 20 28 24 31 2c 20 ag) values ($1,
10e0: 24 32 29 3b 0a 73 65 6c 65 63 74 20 24 31 3b 0a $2);.select $1;.
10f0: 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 66 75 6e $$;..-- this fun
1100: 63 74 69 6f 6e 20 73 65 74 73 20 74 61 67 73 20 ction sets tags
1110: 66 6f 72 20 73 69 74 65 2f 72 65 67 65 78 70 20 for site/regexp
1120: 70 61 69 72 0a 43 52 45 41 54 45 20 6f 72 20 72 pair.CREATE or r
1130: 65 70 6c 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 eplace FUNCTION
1140: 73 65 74 28 6d 79 5f 69 64 5f 73 69 74 65 20 69 set(my_id_site i
1150: 6e 74 65 67 65 72 2c 20 6d 79 5f 69 64 5f 74 61 nteger, my_id_ta
1160: 67 20 69 6e 74 65 67 65 72 2c 20 6d 79 5f 72 65 g integer, my_re
1170: 67 65 78 70 20 74 65 78 74 29 20 52 45 54 55 52 gexp text) RETUR
1180: 4e 53 20 69 6e 74 65 67 65 72 0a 09 4c 41 4e 47 NS integer..LANG
1190: 55 41 47 45 20 73 71 6c 20 53 54 52 49 43 54 0a UAGE sql STRICT.
11a0: 09 41 53 20 24 24 0a 64 65 6c 65 74 65 20 66 72 .AS $$.delete fr
11b0: 6f 6d 20 75 72 6c 73 20 77 68 65 72 65 20 24 31 om urls where $1
11c0: 20 3d 20 69 64 5f 73 69 74 65 20 61 6e 64 20 24 = id_site and $
11d0: 33 20 3d 20 72 65 67 65 78 70 3b 0a 69 6e 73 65 3 = regexp;.inse
11e0: 72 74 20 69 6e 74 6f 20 75 72 6c 73 20 28 69 64 rt into urls (id
11f0: 5f 73 69 74 65 2c 20 69 64 5f 74 61 67 2c 20 72 _site, id_tag, r
1200: 65 67 65 78 70 29 20 76 61 6c 75 65 73 20 28 24 egexp) values ($
1210: 31 2c 20 24 32 2c 20 24 33 29 3b 0a 73 65 6c 65 1, $2, $3);.sele
1220: 63 74 20 24 31 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 ct $1;.$$;..-- t
1230: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 74 6f his function sto
1240: 72 65 73 20 6e 65 77 20 64 61 74 61 20 66 6f 72 res new data for
1250: 20 73 69 74 65 2f 72 65 67 65 78 70 20 70 61 69 site/regexp pai
1260: 72 0a 63 72 65 61 74 65 20 6f 72 20 72 65 70 6c r.create or repl
1270: 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 ace function set
1280: 28 64 6f 6d 61 69 6e 20 74 65 78 74 2c 20 74 61 (domain text, ta
1290: 67 73 20 74 65 78 74 2c 20 72 65 67 65 78 70 20 gs text, regexp
12a0: 74 65 78 74 29 20 72 65 74 75 72 6e 73 20 69 6e text) returns in
12b0: 74 65 67 65 72 0a 09 6c 61 6e 67 75 61 67 65 20 teger..language
12c0: 73 71 6c 20 69 6d 6d 75 74 61 62 6c 65 20 73 74 sql immutable st
12d0: 72 69 63 74 0a 09 61 73 20 24 24 0a 73 65 6c 65 rict..as $$.sele
12e0: 63 74 20 73 65 74 28 67 65 74 5f 73 69 74 65 28 ct set(get_site(
12f0: 24 31 29 2c 20 67 65 74 5f 74 61 67 28 24 32 3a $1), get_tag($2:
1300: 3a 74 65 78 74 5b 5d 29 2c 20 24 33 29 3b 0a 24 :text[]), $3);.$
1310: 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 66 75 6e 63 $;..-- this func
1320: 74 69 6f 6e 20 73 74 6f 72 65 73 20 6e 65 77 20 tion stores new
1330: 64 61 74 61 20 66 6f 72 20 73 69 74 65 2f 72 65 data for site/re
1340: 67 65 78 70 20 70 61 69 72 0a 63 72 65 61 74 65 gexp pair.create
1350: 20 6f 72 20 72 65 70 6c 61 63 65 20 66 75 6e 63 or replace func
1360: 74 69 6f 6e 20 73 65 74 28 64 6f 6d 61 69 6e 20 tion set(domain
1370: 74 65 78 74 2c 20 74 61 67 73 20 74 65 78 74 29 text, tags text)
1380: 20 72 65 74 75 72 6e 73 20 69 6e 74 65 67 65 72 returns integer
1390: 0a 09 6c 61 6e 67 75 61 67 65 20 73 71 6c 20 69 ..language sql i
13a0: 6d 6d 75 74 61 62 6c 65 20 73 74 72 69 63 74 0a mmutable strict.
13b0: 09 61 73 20 24 24 0a 73 65 6c 65 63 74 20 73 65 .as $$.select se
13c0: 74 28 67 65 74 5f 73 69 74 65 28 24 31 29 2c 20 t(get_site($1),
13d0: 67 65 74 5f 74 61 67 28 24 32 3a 3a 74 65 78 74 get_tag($2::text
13e0: 5b 5d 29 29 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 []));.$$;..-- th
13f0: 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
1400: 72 6e 73 20 69 64 20 6f 66 20 74 61 67 20 61 72 rns id of tag ar
1410: 72 61 79 0a 63 72 65 61 74 65 20 6f 72 20 72 65 ray.create or re
1420: 70 6c 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 67 place function g
1430: 65 74 5f 74 61 67 28 6d 79 5f 74 61 67 20 74 65 et_tag(my_tag te
1440: 78 74 5b 5d 29 20 72 65 74 75 72 6e 73 20 69 6e xt[]) returns in
1450: 74 65 67 65 72 0a 09 6c 61 6e 67 75 61 67 65 20 teger..language
1460: 70 6c 70 67 73 71 6c 20 73 74 72 69 63 74 0a 09 plpgsql strict..
1470: 61 73 20 24 24 0a 64 65 63 6c 61 72 65 0a 09 74 as $$.declare..t
1480: 61 67 5f 69 64 20 69 6e 74 65 67 65 72 3b 0a 62 ag_id integer;.b
1490: 65 67 69 6e 0a 09 73 65 6c 65 63 74 20 69 64 5f egin..select id_
14a0: 74 61 67 20 66 72 6f 6d 20 74 61 67 20 77 68 65 tag from tag whe
14b0: 72 65 20 75 73 6f 72 74 28 6d 79 5f 74 61 67 29 re usort(my_tag)
14c0: 20 3d 20 74 61 67 20 69 6e 74 6f 20 74 61 67 5f = tag into tag_
14d0: 69 64 3b 0a 09 69 66 20 6e 6f 74 20 66 6f 75 6e id;..if not foun
14e0: 64 20 74 68 65 6e 0a 09 09 69 6e 73 65 72 74 20 d then...insert
14f0: 69 6e 74 6f 20 74 61 67 20 28 74 61 67 29 20 76 into tag (tag) v
1500: 61 6c 75 65 73 20 28 75 73 6f 72 74 28 6d 79 5f alues (usort(my_
1510: 74 61 67 29 29 3b 0a 09 09 73 65 6c 65 63 74 20 tag));...select
1520: 69 64 5f 74 61 67 20 66 72 6f 6d 20 74 61 67 20 id_tag from tag
1530: 77 68 65 72 65 20 75 73 6f 72 74 28 6d 79 5f 74 where usort(my_t
1540: 61 67 29 20 3d 20 74 61 67 20 69 6e 74 6f 20 74 ag) = tag into t
1550: 61 67 5f 69 64 3b 0a 09 65 6e 64 20 69 66 3b 0a ag_id;..end if;.
1560: 09 72 65 74 75 72 6e 20 74 61 67 5f 69 64 3b 0a .return tag_id;.
1570: 65 6e 64 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 61 62 end;.$$;..-- tab
1580: 6c 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 72 le to hold all r
1590: 75 6c 65 73 0a 43 52 45 41 54 45 20 54 41 42 4c ules.CREATE TABL
15a0: 45 20 72 75 6c 65 73 20 28 0a 09 6e 65 74 6d 61 E rules (..netma
15b0: 73 6b 20 63 69 64 72 20 4e 4f 54 20 4e 55 4c 4c sk cidr NOT NULL
15c0: 2c 0a 09 72 65 64 69 72 65 63 74 5f 75 72 6c 20 ,..redirect_url
15d0: 74 65 78 74 20 44 45 46 41 55 4c 54 20 27 61 62 text DEFAULT 'ab
15e0: 6f 75 74 3a 3a 62 6c 61 6e 6b 27 3a 3a 74 65 78 out::blank'::tex
15f0: 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 66 72 6f t NOT NULL,..fro
1600: 6d 5f 77 65 65 6b 64 61 79 20 73 6d 61 6c 6c 69 m_weekday smalli
1610: 6e 74 20 44 45 46 41 55 4c 54 20 30 20 4e 4f 54 nt DEFAULT 0 NOT
1620: 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 77 65 65 6b 64 NULL,..to_weekd
1630: 61 79 20 73 6d 61 6c 6c 69 6e 74 20 44 45 46 41 ay smallint DEFA
1640: 55 4c 54 20 36 20 4e 4f 54 20 4e 55 4c 4c 2c 0a ULT 6 NOT NULL,.
1650: 09 66 72 6f 6d 5f 74 69 6d 65 20 74 69 6d 65 20 .from_time time
1660: 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e without time zon
1670: 65 20 44 45 46 41 55 4c 54 20 27 30 30 3a 30 30 e DEFAULT '00:00
1680: 3a 30 30 27 3a 3a 74 69 6d 65 20 77 69 74 68 6f :00'::time witho
1690: 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 4e 4f 54 ut time zone NOT
16a0: 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 74 69 6d 65 20 NULL,..to_time
16b0: 74 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d time without tim
16c0: 65 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 27 e zone DEFAULT '
16d0: 32 33 3a 35 39 3a 35 39 27 3a 3a 74 69 6d 65 20 23:59:59'::time
16e0: 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e without time zon
16f0: 65 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 69 64 5f e NOT NULL,..id_
1700: 74 61 67 20 73 6d 61 6c 6c 69 6e 74 20 4e 4f 54 tag smallint NOT
1710: 20 4e 55 4c 4c 0a 29 3b 0a 0a 41 4c 54 45 52 20 NULL.);..ALTER
1720: 54 41 42 4c 45 20 4f 4e 4c 59 20 72 75 6c 65 73 TABLE ONLY rules
1730: 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 ..ADD CONSTRAINT
1740: 20 72 75 6c 65 73 5f 70 6b 65 79 20 50 52 49 4d rules_pkey PRIM
1750: 41 52 59 20 4b 45 59 20 28 6e 65 74 6d 61 73 6b ARY KEY (netmask
1760: 29 3b 0a 0a 41 4c 54 45 52 20 54 41 42 4c 45 20 );..ALTER TABLE
1770: 4f 4e 4c 59 20 72 75 6c 65 73 0a 09 41 44 44 20 ONLY rules..ADD
1780: 43 4f 4e 53 54 52 41 49 4e 54 20 72 75 6c 65 73 CONSTRAINT rules
1790: 5f 74 61 67 5f 66 20 46 4f 52 45 49 47 4e 20 4b _tag_f FOREIGN K
17a0: 45 59 20 28 69 64 5f 74 61 67 29 20 52 45 46 45 EY (id_tag) REFE
17b0: 52 45 4e 43 45 53 20 74 61 67 28 69 64 5f 74 61 RENCES tag(id_ta
17c0: 67 29 20 4d 41 54 43 48 20 46 55 4c 4c 0a 09 4f g) MATCH FULL..O
17d0: 4e 20 55 50 44 41 54 45 20 52 45 53 54 52 49 43 N UPDATE RESTRIC
17e0: 54 20 4f 4e 20 44 45 4c 45 54 45 20 52 45 53 54 T ON DELETE REST
17f0: 52 49 43 54 20 44 45 46 45 52 52 41 42 4c 45 20 RICT DEFERRABLE
1800: 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 INITIALLY DEFERR
1810: 45 44 3b 0a 0a 2d 2d 20 74 61 62 6c 65 20 74 6f ED;..-- table to
1820: 20 68 6f 6c 64 20 73 69 74 65 20 61 72 72 61 79 hold site array
1830: 73 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 s.CREATE TABLE s
1840: 69 74 65 20 28 0a 09 69 64 5f 73 69 74 65 20 73 ite (..id_site s
1850: 65 72 69 61 6c 2c 0a 09 73 69 74 65 20 74 65 78 erial,..site tex
1860: 74 5b 5d 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a t[] NOT NULL.);.
1870: 0a 41 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c .ALTER TABLE ONL
1880: 59 20 73 69 74 65 0a 09 41 44 44 20 43 4f 4e 53 Y site..ADD CONS
1890: 54 52 41 49 4e 54 20 73 69 74 65 5f 70 6b 65 79 TRAINT site_pkey
18a0: 20 50 52 49 4d 41 52 59 20 4b 45 59 20 28 69 64 PRIMARY KEY (id
18b0: 5f 73 69 74 65 29 3b 0a 0a 43 52 45 41 54 45 20 _site);..CREATE
18c0: 55 4e 49 51 55 45 20 49 4e 44 45 58 20 73 69 74 UNIQUE INDEX sit
18d0: 65 5f 75 20 4f 4e 20 73 69 74 65 20 28 75 73 6f e_u ON site (uso
18e0: 72 74 28 73 69 74 65 29 29 3b 0a 0a 43 52 45 41 rt(site));..CREA
18f0: 54 45 20 49 4e 44 45 58 20 73 69 74 65 5f 67 20 TE INDEX site_g
1900: 4f 4e 20 73 69 74 65 20 55 53 49 4e 47 20 67 69 ON site USING gi
1910: 6e 20 28 73 69 74 65 29 3b 0a 0a 2d 2d 20 74 61 n (site);..-- ta
1920: 62 6c 65 20 74 6f 20 68 6f 6c 64 20 74 61 67 20 ble to hold tag
1930: 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 0a 43 52 45 combinations.CRE
1940: 41 54 45 20 54 41 42 4c 45 20 74 61 67 20 28 0a ATE TABLE tag (.
1950: 09 69 64 5f 74 61 67 20 73 65 72 69 61 6c 2c 0a .id_tag serial,.
1960: 09 74 61 67 20 74 65 78 74 5b 5d 20 4e 4f 54 20 .tag text[] NOT
1970: 4e 55 4c 4c 0a 29 3b 0a 0a 41 4c 54 45 52 20 54 NULL.);..ALTER T
1980: 41 42 4c 45 20 4f 4e 4c 59 20 74 61 67 0a 09 41 ABLE ONLY tag..A
1990: 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 20 74 61 DD CONSTRAINT ta
19a0: 67 5f 70 6b 65 79 20 50 52 49 4d 41 52 59 20 4b g_pkey PRIMARY K
19b0: 45 59 20 28 69 64 5f 74 61 67 29 3b 0a 0a 43 52 EY (id_tag);..CR
19c0: 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 EATE UNIQUE INDE
19d0: 58 20 74 61 67 5f 75 20 4f 4e 20 74 61 67 20 28 X tag_u ON tag (
19e0: 75 73 6f 72 74 28 74 61 67 29 29 3b 0a 0a 43 52 usort(tag));..CR
19f0: 45 41 54 45 20 49 4e 44 45 58 20 74 61 67 5f 67 EATE INDEX tag_g
1a00: 20 4f 4e 20 74 61 67 20 55 53 49 4e 47 20 67 69 ON tag USING gi
1a10: 6e 20 28 74 61 67 29 3b 0a 0a 2d 2d 20 74 61 62 n (tag);..-- tab
1a20: 6c 65 20 74 6f 20 68 6f 6c 64 20 74 61 67 20 2d le to hold tag -
1a30: 20 73 69 74 65 20 6c 69 6e 6b 73 0a 43 52 45 41 site links.CREA
1a40: 54 45 20 54 41 42 4c 45 20 75 72 6c 73 20 28 0a TE TABLE urls (.
1a50: 09 64 61 74 65 5f 61 64 64 65 64 20 74 69 6d 65 .date_added time
1a60: 73 74 61 6d 70 20 77 69 74 68 6f 75 74 20 74 69 stamp without ti
1a70: 6d 65 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 me zone DEFAULT
1a80: 28 27 6e 6f 77 27 3a 3a 74 65 78 74 29 3a 3a 74 ('now'::text)::t
1a90: 69 6d 65 73 74 61 6d 70 28 30 29 20 77 69 74 68 imestamp(0) with
1aa0: 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 4e 4f out time zone NO
1ab0: 54 20 4e 55 4c 4c 2c 0a 09 69 64 5f 73 69 74 65 T NULL,..id_site
1ac0: 20 73 6d 61 6c 6c 69 6e 74 20 4e 4f 54 20 4e 55 smallint NOT NU
1ad0: 4c 4c 2c 0a 09 69 64 5f 74 61 67 20 73 6d 61 6c LL,..id_tag smal
1ae0: 6c 69 6e 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 lint NOT NULL,..
1af0: 72 65 67 65 78 70 20 74 65 78 74 0a 29 3b 0a 0a regexp text.);..
1b00: 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e CREATE UNIQUE IN
1b10: 44 45 58 20 75 72 6c 73 5f 70 6b 65 79 20 4f 4e DEX urls_pkey ON
1b20: 20 75 72 6c 73 20 55 53 49 4e 47 20 62 74 72 65 urls USING btre
1b30: 65 20 28 69 64 5f 73 69 74 65 2c 20 72 65 67 65 e (id_site, rege
1b40: 78 70 29 3b 0a 0a 43 52 45 41 54 45 20 49 4e 44 xp);..CREATE IND
1b50: 45 58 20 75 72 6c 73 5f 69 64 5f 74 61 67 20 4f EX urls_id_tag O
1b60: 4e 20 75 72 6c 73 20 55 53 49 4e 47 20 62 74 72 N urls USING btr
1b70: 65 65 20 28 69 64 5f 74 61 67 29 3b 0a 0a 41 4c ee (id_tag);..AL
1b80: 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 75 TER TABLE ONLY u
1b90: 72 6c 73 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 rls..ADD CONSTRA
1ba0: 49 4e 54 20 75 72 6c 73 5f 73 69 74 65 5f 66 20 INT urls_site_f
1bb0: 46 4f 52 45 49 47 4e 20 4b 45 59 20 28 69 64 5f FOREIGN KEY (id_
1bc0: 73 69 74 65 29 20 52 45 46 45 52 45 4e 43 45 53 site) REFERENCES
1bd0: 20 73 69 74 65 28 69 64 5f 73 69 74 65 29 20 4d site(id_site) M
1be0: 41 54 43 48 20 46 55 4c 4c 0a 09 4f 4e 20 55 50 ATCH FULL..ON UP
1bf0: 44 41 54 45 20 52 45 53 54 52 49 43 54 20 4f 4e DATE RESTRICT ON
1c00: 20 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 54 DELETE RESTRICT
1c10: 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 DEFERRABLE INIT
1c20: 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 3b 0a IALLY DEFERRED;.
1c30: 0a 41 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c .ALTER TABLE ONL
1c40: 59 20 75 72 6c 73 0a 09 41 44 44 20 43 4f 4e 53 Y urls..ADD CONS
1c50: 54 52 41 49 4e 54 20 75 72 6c 73 5f 74 61 67 5f TRAINT urls_tag_
1c60: 66 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 28 69 f FOREIGN KEY (i
1c70: 64 5f 74 61 67 29 20 52 45 46 45 52 45 4e 43 45 d_tag) REFERENCE
1c80: 53 20 74 61 67 28 69 64 5f 74 61 67 29 20 4d 41 S tag(id_tag) MA
1c90: 54 43 48 20 46 55 4c 4c 0a 09 4f 4e 20 55 50 44 TCH FULL..ON UPD
1ca0: 41 54 45 20 52 45 53 54 52 49 43 54 20 4f 4e 20 ATE RESTRICT ON
1cb0: 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 54 20 DELETE RESTRICT
1cc0: 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 DEFERRABLE INITI
1cd0: 41 4c 4c 59 20 44 45 46 45 52 52 45 44 3b 0a 0a ALLY DEFERRED;..
1ce0: 2d 2d 20 72 75 6c 65 20 74 6f 20 6a 6f 69 6e 20 -- rule to join
1cf0: 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 74 6f 20 all tables into
1d00: 6f 6e 65 20 74 6f 20 73 69 6d 70 6c 69 66 79 20 one to simplify
1d10: 61 63 63 65 73 73 0a 2d 2d 20 61 75 74 6f 6d 61 access.-- automa
1d20: 74 69 63 61 6c 6c 20 75 73 65 73 20 63 75 72 72 ticall uses curr
1d30: 65 6e 74 20 64 61 79 20 61 6e 64 20 74 69 6d 65 ent day and time
1d40: 20 64 61 74 61 0a 43 52 45 41 54 45 20 56 49 45 data.CREATE VIE
1d50: 57 20 73 69 74 65 5f 72 75 6c 65 20 41 53 0a 53 W site_rule AS.S
1d60: 45 4c 45 43 54 20 61 2e 72 65 64 69 72 65 63 74 ELECT a.redirect
1d70: 5f 75 72 6c 2c 20 61 2e 6e 65 74 6d 61 73 6b 2c _url, a.netmask,
1d80: 20 62 2e 73 69 74 65 2c 20 62 2e 72 65 67 65 78 b.site, b.regex
1d90: 70 0a 46 52 4f 4d 20 28 28 0a 09 53 45 4c 45 43 p.FROM ((..SELEC
1da0: 54 20 72 75 6c 65 73 2e 72 65 64 69 72 65 63 74 T rules.redirect
1db0: 5f 75 72 6c 2c 20 74 61 67 2e 74 61 67 20 41 53 _url, tag.tag AS
1dc0: 20 72 75 6c 65 5f 74 61 67 2c 20 72 75 6c 65 73 rule_tag, rules
1dd0: 2e 6e 65 74 6d 61 73 6b 0a 09 46 52 4f 4d 20 72 .netmask..FROM r
1de0: 75 6c 65 73 20 4e 41 54 55 52 41 4c 20 4a 4f 49 ules NATURAL JOI
1df0: 4e 20 74 61 67 0a 09 57 48 45 52 45 20 28 27 6e N tag..WHERE ('n
1e00: 6f 77 27 3a 3a 74 65 78 74 29 3a 3a 74 69 6d 65 ow'::text)::time
1e10: 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f without time zo
1e20: 6e 65 20 3e 3d 20 72 75 6c 65 73 2e 66 72 6f 6d ne >= rules.from
1e30: 5f 74 69 6d 65 0a 09 09 41 4e 44 20 28 27 6e 6f _time...AND ('no
1e40: 77 27 3a 3a 74 65 78 74 29 3a 3a 74 69 6d 65 20 w'::text)::time
1e50: 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e without time zon
1e60: 65 20 3c 3d 20 72 75 6c 65 73 2e 74 6f 5f 74 69 e <= rules.to_ti
1e70: 6d 65 0a 09 09 41 4e 44 20 64 61 74 65 5f 70 61 me...AND date_pa
1e80: 72 74 28 27 64 6f 77 27 3a 3a 74 65 78 74 2c 20 rt('dow'::text,
1e90: 6e 6f 77 28 29 29 20 3e 3d 20 28 72 75 6c 65 73 now()) >= (rules
1ea0: 2e 66 72 6f 6d 5f 77 65 65 6b 64 61 79 29 3a 3a .from_weekday)::
1eb0: 64 6f 75 62 6c 65 20 70 72 65 63 69 73 69 6f 6e double precision
1ec0: 0a 09 09 41 4e 44 20 64 61 74 65 5f 70 61 72 74 ...AND date_part
1ed0: 28 27 64 6f 77 27 3a 3a 74 65 78 74 2c 20 6e 6f ('dow'::text, no
1ee0: 77 28 29 29 20 3c 3d 20 28 72 75 6c 65 73 2e 74 w()) <= (rules.t
1ef0: 6f 5f 77 65 65 6b 64 61 79 29 3a 3a 64 6f 75 62 o_weekday)::doub
1f00: 6c 65 20 70 72 65 63 69 73 69 6f 6e 0a 29 20 61 le precision.) a
1f10: 20 4a 4f 49 4e 20 28 0a 09 53 45 4c 45 43 54 20 JOIN (..SELECT
1f20: 73 69 74 65 2e 73 69 74 65 2c 20 74 61 67 2e 74 site.site, tag.t
1f30: 61 67 20 41 53 20 75 72 6c 5f 74 61 67 2c 20 72 ag AS url_tag, r
1f40: 65 67 65 78 70 0a 09 46 52 4f 4d 20 75 72 6c 73 egexp..FROM urls
1f50: 20 4e 41 54 55 52 41 4c 20 4a 4f 49 4e 20 74 61 NATURAL JOIN ta
1f60: 67 20 4e 41 54 55 52 41 4c 20 4a 4f 49 4e 20 73 g NATURAL JOIN s
1f70: 69 74 65 0a 29 20 62 20 4f 4e 20 28 62 2e 75 72 ite.) b ON (b.ur
1f80: 6c 5f 74 61 67 20 26 26 20 61 2e 72 75 6c 65 5f l_tag && a.rule_
1f90: 74 61 67 29 29 3b 0a tag));.