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 73 71 6c 20 69 6d 6d 75 74 61 GUAGE sql immuta
0c20: 62 6c 65 20 53 54 52 49 43 54 0a 09 41 53 20 24 ble STRICT..AS $
0c30: 24 0a 73 65 6c 65 63 74 20 6d 61 72 6b 28 24 31 $.select mark($1
0c40: 2c 20 67 65 74 5f 74 61 67 28 61 72 72 61 79 5b , get_tag(array[
0c50: 24 32 5d 29 2c 20 4e 55 4c 4c 29 20 61 73 20 72 $2]), NULL) as r
0c60: 65 73 75 6c 74 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 esult;.$$;..-- t
0c70: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 his function add
0c80: 73 20 74 61 67 20 74 6f 20 64 6f 6d 61 69 6e 0a s tag to domain.
0c90: 43 52 45 41 54 45 20 6f 72 20 72 65 70 6c 61 63 CREATE or replac
0ca0: 65 20 46 55 4e 43 54 49 4f 4e 20 6d 61 72 6b 28 e FUNCTION mark(
0cb0: 64 6f 6d 61 69 6e 20 74 65 78 74 2c 20 6e 65 77 domain text, new
0cc0: 5f 74 61 67 20 74 65 78 74 29 20 52 45 54 55 52 _tag text) RETUR
0cd0: 4e 53 20 69 6e 74 65 67 65 72 0a 09 4c 41 4e 47 NS integer..LANG
0ce0: 55 41 47 45 20 73 71 6c 20 69 6d 6d 75 74 61 62 UAGE sql immutab
0cf0: 6c 65 20 53 54 52 49 43 54 0a 09 41 53 20 24 24 le STRICT..AS $$
0d00: 0a 73 65 6c 65 63 74 20 6d 61 72 6b 28 67 65 74 .select mark(get
0d10: 5f 73 69 74 65 28 24 31 29 2c 20 67 65 74 5f 74 _site($1), get_t
0d20: 61 67 28 61 72 72 61 79 5b 24 32 5d 29 2c 20 4e ag(array[$2]), N
0d30: 55 4c 4c 29 20 61 73 20 72 65 73 75 6c 74 3b 0a ULL) as result;.
0d40: 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 66 75 6e $$;..-- this fun
0d50: 63 74 69 6f 6e 20 73 65 74 73 20 74 61 67 73 20 ction sets tags
0d60: 66 6f 72 20 73 69 74 65 20 77 69 74 68 6f 75 74 for site without
0d70: 20 72 65 67 65 78 70 0a 43 52 45 41 54 45 20 6f regexp.CREATE o
0d80: 72 20 72 65 70 6c 61 63 65 20 46 55 4e 43 54 49 r replace FUNCTI
0d90: 4f 4e 20 73 65 74 28 6d 79 5f 69 64 5f 73 69 74 ON set(my_id_sit
0da0: 65 20 69 6e 74 65 67 65 72 2c 20 6d 79 5f 69 64 e integer, my_id
0db0: 5f 74 61 67 20 69 6e 74 65 67 65 72 29 20 52 45 _tag integer) RE
0dc0: 54 55 52 4e 53 20 69 6e 74 65 67 65 72 0a 09 4c TURNS integer..L
0dd0: 41 4e 47 55 41 47 45 20 73 71 6c 20 53 54 52 49 ANGUAGE sql STRI
0de0: 43 54 0a 09 41 53 20 24 24 0a 64 65 6c 65 74 65 CT..AS $$.delete
0df0: 20 66 72 6f 6d 20 75 72 6c 73 20 77 68 65 72 65 from urls where
0e00: 20 24 31 20 3d 20 69 64 5f 73 69 74 65 20 61 6e $1 = id_site an
0e10: 64 20 72 65 67 65 78 70 20 69 73 20 4e 55 4c 4c d regexp is NULL
0e20: 3b 0a 69 6e 73 65 72 74 20 69 6e 74 6f 20 75 72 ;.insert into ur
0e30: 6c 73 20 28 69 64 5f 73 69 74 65 2c 20 69 64 5f ls (id_site, id_
0e40: 74 61 67 29 20 76 61 6c 75 65 73 20 28 24 31 2c tag) values ($1,
0e50: 20 24 32 29 3b 0a 73 65 6c 65 63 74 20 24 31 3b $2);.select $1;
0e60: 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 66 75 .$$;..-- this fu
0e70: 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 61 67 73 nction sets tags
0e80: 20 66 6f 72 20 73 69 74 65 2f 72 65 67 65 78 70 for site/regexp
0e90: 20 70 61 69 72 0a 43 52 45 41 54 45 20 6f 72 20 pair.CREATE or
0ea0: 72 65 70 6c 61 63 65 20 46 55 4e 43 54 49 4f 4e replace FUNCTION
0eb0: 20 73 65 74 28 6d 79 5f 69 64 5f 73 69 74 65 20 set(my_id_site
0ec0: 69 6e 74 65 67 65 72 2c 20 6d 79 5f 69 64 5f 74 integer, my_id_t
0ed0: 61 67 20 69 6e 74 65 67 65 72 2c 20 6d 79 5f 72 ag integer, my_r
0ee0: 65 67 65 78 70 20 74 65 78 74 29 20 52 45 54 55 egexp text) RETU
0ef0: 52 4e 53 20 69 6e 74 65 67 65 72 0a 09 4c 41 4e RNS integer..LAN
0f00: 47 55 41 47 45 20 73 71 6c 20 53 54 52 49 43 54 GUAGE sql STRICT
0f10: 0a 09 41 53 20 24 24 0a 64 65 6c 65 74 65 20 66 ..AS $$.delete f
0f20: 72 6f 6d 20 75 72 6c 73 20 77 68 65 72 65 20 24 rom urls where $
0f30: 31 20 3d 20 69 64 5f 73 69 74 65 20 61 6e 64 20 1 = id_site and
0f40: 24 33 20 3d 20 72 65 67 65 78 70 3b 0a 69 6e 73 $3 = regexp;.ins
0f50: 65 72 74 20 69 6e 74 6f 20 75 72 6c 73 20 28 69 ert into urls (i
0f60: 64 5f 73 69 74 65 2c 20 69 64 5f 74 61 67 2c 20 d_site, id_tag,
0f70: 72 65 67 65 78 70 29 20 76 61 6c 75 65 73 20 28 regexp) values (
0f80: 24 31 2c 20 24 32 2c 20 24 33 29 3b 0a 73 65 6c $1, $2, $3);.sel
0f90: 65 63 74 20 24 31 3b 0a 24 24 3b 0a 0a 2d 2d 20 ect $1;.$$;..--
0fa0: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 74 this function st
0fb0: 6f 72 65 73 20 6e 65 77 20 64 61 74 61 20 66 6f ores new data fo
0fc0: 72 20 73 69 74 65 2f 72 65 67 65 78 70 20 70 61 r site/regexp pa
0fd0: 69 72 0a 63 72 65 61 74 65 20 6f 72 20 72 65 70 ir.create or rep
0fe0: 6c 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 73 65 lace function se
0ff0: 74 28 64 6f 6d 61 69 6e 20 74 65 78 74 2c 20 74 t(domain text, t
1000: 61 67 73 20 74 65 78 74 2c 20 72 65 67 65 78 70 ags text, regexp
1010: 20 74 65 78 74 29 20 72 65 74 75 72 6e 73 20 69 text) returns i
1020: 6e 74 65 67 65 72 0a 09 6c 61 6e 67 75 61 67 65 nteger..language
1030: 20 73 71 6c 20 69 6d 6d 75 74 61 62 6c 65 20 73 sql immutable s
1040: 74 72 69 63 74 0a 09 61 73 20 24 24 0a 73 65 6c trict..as $$.sel
1050: 65 63 74 20 73 65 74 28 67 65 74 5f 73 69 74 65 ect set(get_site
1060: 28 24 31 29 2c 20 67 65 74 5f 74 61 67 28 24 32 ($1), get_tag($2
1070: 3a 3a 74 65 78 74 5b 5d 29 2c 20 24 33 29 3b 0a ::text[]), $3);.
1080: 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 66 75 6e $$;..-- this fun
1090: 63 74 69 6f 6e 20 73 74 6f 72 65 73 20 6e 65 77 ction stores new
10a0: 20 64 61 74 61 20 66 6f 72 20 73 69 74 65 2f 72 data for site/r
10b0: 65 67 65 78 70 20 70 61 69 72 0a 63 72 65 61 74 egexp pair.creat
10c0: 65 20 6f 72 20 72 65 70 6c 61 63 65 20 66 75 6e e or replace fun
10d0: 63 74 69 6f 6e 20 73 65 74 28 64 6f 6d 61 69 6e ction set(domain
10e0: 20 74 65 78 74 2c 20 74 61 67 73 20 74 65 78 74 text, tags text
10f0: 29 20 72 65 74 75 72 6e 73 20 69 6e 74 65 67 65 ) returns intege
1100: 72 0a 09 6c 61 6e 67 75 61 67 65 20 73 71 6c 20 r..language sql
1110: 69 6d 6d 75 74 61 62 6c 65 20 73 74 72 69 63 74 immutable strict
1120: 0a 09 61 73 20 24 24 0a 73 65 6c 65 63 74 20 73 ..as $$.select s
1130: 65 74 28 67 65 74 5f 73 69 74 65 28 24 31 29 2c et(get_site($1),
1140: 20 67 65 74 5f 74 61 67 28 24 32 3a 3a 74 65 78 get_tag($2::tex
1150: 74 5b 5d 29 29 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 t[]));.$$;..-- t
1160: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
1170: 75 72 6e 73 20 69 64 20 6f 66 20 74 61 67 20 61 urns id of tag a
1180: 72 72 61 79 0a 63 72 65 61 74 65 20 6f 72 20 72 rray.create or r
1190: 65 70 6c 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 eplace function
11a0: 67 65 74 5f 74 61 67 28 6d 79 5f 74 61 67 20 74 get_tag(my_tag t
11b0: 65 78 74 5b 5d 29 20 72 65 74 75 72 6e 73 20 69 ext[]) returns i
11c0: 6e 74 65 67 65 72 0a 09 6c 61 6e 67 75 61 67 65 nteger..language
11d0: 20 70 6c 70 67 73 71 6c 20 73 74 72 69 63 74 0a plpgsql strict.
11e0: 09 61 73 20 24 24 0a 64 65 63 6c 61 72 65 0a 09 .as $$.declare..
11f0: 74 61 67 5f 69 64 20 69 6e 74 65 67 65 72 3b 0a tag_id integer;.
1200: 62 65 67 69 6e 0a 09 73 65 6c 65 63 74 20 69 64 begin..select id
1210: 5f 74 61 67 20 66 72 6f 6d 20 74 61 67 20 77 68 _tag from tag wh
1220: 65 72 65 20 75 73 6f 72 74 28 6d 79 5f 74 61 67 ere usort(my_tag
1230: 29 20 3d 20 74 61 67 20 69 6e 74 6f 20 74 61 67 ) = tag into tag
1240: 5f 69 64 3b 0a 09 69 66 20 6e 6f 74 20 66 6f 75 _id;..if not fou
1250: 6e 64 20 74 68 65 6e 0a 09 09 69 6e 73 65 72 74 nd then...insert
1260: 20 69 6e 74 6f 20 74 61 67 20 28 74 61 67 29 20 into tag (tag)
1270: 76 61 6c 75 65 73 20 28 75 73 6f 72 74 28 6d 79 values (usort(my
1280: 5f 74 61 67 29 29 3b 0a 09 09 73 65 6c 65 63 74 _tag));...select
1290: 20 69 64 5f 74 61 67 20 66 72 6f 6d 20 74 61 67 id_tag from tag
12a0: 20 77 68 65 72 65 20 75 73 6f 72 74 28 6d 79 5f where usort(my_
12b0: 74 61 67 29 20 3d 20 74 61 67 20 69 6e 74 6f 20 tag) = tag into
12c0: 74 61 67 5f 69 64 3b 0a 09 65 6e 64 20 69 66 3b tag_id;..end if;
12d0: 0a 09 72 65 74 75 72 6e 20 74 61 67 5f 69 64 3b ..return tag_id;
12e0: 0a 65 6e 64 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 61 .end;.$$;..-- ta
12f0: 62 6c 65 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 ble to hold all
1300: 72 75 6c 65 73 0a 43 52 45 41 54 45 20 54 41 42 rules.CREATE TAB
1310: 4c 45 20 72 75 6c 65 73 20 28 0a 09 6e 65 74 6d LE rules (..netm
1320: 61 73 6b 20 63 69 64 72 20 4e 4f 54 20 4e 55 4c ask cidr NOT NUL
1330: 4c 2c 0a 09 72 65 64 69 72 65 63 74 5f 75 72 6c L,..redirect_url
1340: 20 74 65 78 74 20 44 45 46 41 55 4c 54 20 27 61 text DEFAULT 'a
1350: 62 6f 75 74 3a 3a 62 6c 61 6e 6b 27 3a 3a 74 65 bout::blank'::te
1360: 78 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 66 72 xt NOT NULL,..fr
1370: 6f 6d 5f 77 65 65 6b 64 61 79 20 73 6d 61 6c 6c om_weekday small
1380: 69 6e 74 20 44 45 46 41 55 4c 54 20 30 20 4e 4f int DEFAULT 0 NO
1390: 54 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 77 65 65 6b T NULL,..to_week
13a0: 64 61 79 20 73 6d 61 6c 6c 69 6e 74 20 44 45 46 day smallint DEF
13b0: 41 55 4c 54 20 36 20 4e 4f 54 20 4e 55 4c 4c 2c AULT 6 NOT NULL,
13c0: 0a 09 66 72 6f 6d 5f 74 69 6d 65 20 74 69 6d 65 ..from_time time
13d0: 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f without time zo
13e0: 6e 65 20 44 45 46 41 55 4c 54 20 27 30 30 3a 30 ne DEFAULT '00:0
13f0: 30 3a 30 30 27 3a 3a 74 69 6d 65 20 77 69 74 68 0:00'::time with
1400: 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 4e 4f out time zone NO
1410: 54 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 74 69 6d 65 T NULL,..to_time
1420: 20 74 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 time without ti
1430: 6d 65 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 me zone DEFAULT
1440: 27 32 33 3a 35 39 3a 35 39 27 3a 3a 74 69 6d 65 '23:59:59'::time
1450: 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f without time zo
1460: 6e 65 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 69 64 ne NOT NULL,..id
1470: 5f 74 61 67 20 73 6d 61 6c 6c 69 6e 74 20 4e 4f _tag smallint NO
1480: 54 20 4e 55 4c 4c 0a 29 3b 0a 0a 41 4c 54 45 52 T NULL.);..ALTER
1490: 20 54 41 42 4c 45 20 4f 4e 4c 59 20 72 75 6c 65 TABLE ONLY rule
14a0: 73 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 49 4e s..ADD CONSTRAIN
14b0: 54 20 72 75 6c 65 73 5f 74 61 67 5f 66 20 46 4f T rules_tag_f FO
14c0: 52 45 49 47 4e 20 4b 45 59 20 28 69 64 5f 74 61 REIGN KEY (id_ta
14d0: 67 29 20 52 45 46 45 52 45 4e 43 45 53 20 74 61 g) REFERENCES ta
14e0: 67 28 69 64 5f 74 61 67 29 20 4d 41 54 43 48 20 g(id_tag) MATCH
14f0: 46 55 4c 4c 0a 09 4f 4e 20 55 50 44 41 54 45 20 FULL..ON UPDATE
1500: 52 45 53 54 52 49 43 54 20 4f 4e 20 44 45 4c 45 RESTRICT ON DELE
1510: 54 45 20 52 45 53 54 52 49 43 54 20 44 45 46 45 TE RESTRICT DEFE
1520: 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 RRABLE INITIALLY
1530: 20 44 45 46 45 52 52 45 44 3b 0a 0a 2d 2d 20 74 DEFERRED;..-- t
1540: 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 73 69 74 able to hold sit
1550: 65 20 61 72 72 61 79 73 0a 43 52 45 41 54 45 20 e arrays.CREATE
1560: 54 41 42 4c 45 20 73 69 74 65 20 28 0a 09 69 64 TABLE site (..id
1570: 5f 73 69 74 65 20 73 65 72 69 61 6c 2c 0a 09 73 _site serial,..s
1580: 69 74 65 20 74 65 78 74 5b 5d 20 4e 4f 54 20 4e ite text[] NOT N
1590: 55 4c 4c 0a 29 3b 0a 0a 41 4c 54 45 52 20 54 41 ULL.);..ALTER TA
15a0: 42 4c 45 20 4f 4e 4c 59 20 73 69 74 65 0a 09 41 BLE ONLY site..A
15b0: 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 20 73 69 DD CONSTRAINT si
15c0: 74 65 5f 70 6b 65 79 20 50 52 49 4d 41 52 59 20 te_pkey PRIMARY
15d0: 4b 45 59 20 28 69 64 5f 73 69 74 65 29 3b 0a 0a KEY (id_site);..
15e0: 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e CREATE UNIQUE IN
15f0: 44 45 58 20 73 69 74 65 5f 75 20 4f 4e 20 73 69 DEX site_u ON si
1600: 74 65 20 28 75 73 6f 72 74 28 73 69 74 65 29 29 te (usort(site))
1610: 3b 0a 0a 43 52 45 41 54 45 20 49 4e 44 45 58 20 ;..CREATE INDEX
1620: 73 69 74 65 5f 67 20 4f 4e 20 73 69 74 65 20 55 site_g ON site U
1630: 53 49 4e 47 20 67 69 6e 20 28 73 69 74 65 29 3b SING gin (site);
1640: 0a 0a 2d 2d 20 74 61 62 6c 65 20 74 6f 20 68 6f ..-- table to ho
1650: 6c 64 20 74 61 67 20 63 6f 6d 62 69 6e 61 74 69 ld tag combinati
1660: 6f 6e 73 0a 43 52 45 41 54 45 20 54 41 42 4c 45 ons.CREATE TABLE
1670: 20 74 61 67 20 28 0a 09 69 64 5f 74 61 67 20 73 tag (..id_tag s
1680: 65 72 69 61 6c 2c 0a 09 74 61 67 20 74 65 78 74 erial,..tag text
1690: 5b 5d 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a 0a [] NOT NULL.);..
16a0: 41 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 ALTER TABLE ONLY
16b0: 20 74 61 67 0a 09 41 44 44 20 43 4f 4e 53 54 52 tag..ADD CONSTR
16c0: 41 49 4e 54 20 74 61 67 5f 70 6b 65 79 20 50 52 AINT tag_pkey PR
16d0: 49 4d 41 52 59 20 4b 45 59 20 28 69 64 5f 74 61 IMARY KEY (id_ta
16e0: 67 29 3b 0a 0a 43 52 45 41 54 45 20 55 4e 49 51 g);..CREATE UNIQ
16f0: 55 45 20 49 4e 44 45 58 20 74 61 67 5f 75 20 4f UE INDEX tag_u O
1700: 4e 20 74 61 67 20 28 75 73 6f 72 74 28 74 61 67 N tag (usort(tag
1710: 29 29 3b 0a 0a 43 52 45 41 54 45 20 49 4e 44 45 ));..CREATE INDE
1720: 58 20 74 61 67 5f 67 20 4f 4e 20 74 61 67 20 55 X tag_g ON tag U
1730: 53 49 4e 47 20 67 69 6e 20 28 74 61 67 29 3b 0a SING gin (tag);.
1740: 0a 2d 2d 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c .-- table to hol
1750: 64 20 74 61 67 20 2d 20 73 69 74 65 20 6c 69 6e d tag - site lin
1760: 6b 73 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 ks.CREATE TABLE
1770: 75 72 6c 73 20 28 0a 09 64 61 74 65 5f 61 64 64 urls (..date_add
1780: 65 64 20 74 69 6d 65 73 74 61 6d 70 20 77 69 74 ed timestamp wit
1790: 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 44 hout time zone D
17a0: 45 46 41 55 4c 54 20 28 27 6e 6f 77 27 3a 3a 74 EFAULT ('now'::t
17b0: 65 78 74 29 3a 3a 74 69 6d 65 73 74 61 6d 70 28 ext)::timestamp(
17c0: 30 29 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 0) without time
17d0: 7a 6f 6e 65 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 zone NOT NULL,..
17e0: 69 64 5f 73 69 74 65 20 73 6d 61 6c 6c 69 6e 74 id_site smallint
17f0: 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 69 64 5f 74 NOT NULL,..id_t
1800: 61 67 20 73 6d 61 6c 6c 69 6e 74 20 4e 4f 54 20 ag smallint NOT
1810: 4e 55 4c 4c 2c 0a 09 72 65 67 65 78 70 20 74 65 NULL,..regexp te
1820: 78 74 0a 29 3b 0a 0a 43 52 45 41 54 45 20 55 4e xt.);..CREATE UN
1830: 49 51 55 45 20 49 4e 44 45 58 20 75 72 6c 73 5f IQUE INDEX urls_
1840: 70 6b 65 79 20 4f 4e 20 75 72 6c 73 20 55 53 49 pkey ON urls USI
1850: 4e 47 20 62 74 72 65 65 20 28 69 64 5f 73 69 74 NG btree (id_sit
1860: 65 2c 20 72 65 67 65 78 70 29 3b 0a 0a 43 52 45 e, regexp);..CRE
1870: 41 54 45 20 49 4e 44 45 58 20 75 72 6c 73 5f 69 ATE INDEX urls_i
1880: 64 5f 74 61 67 20 4f 4e 20 75 72 6c 73 20 55 53 d_tag ON urls US
1890: 49 4e 47 20 62 74 72 65 65 20 28 69 64 5f 74 61 ING btree (id_ta
18a0: 67 29 3b 0a 0a 41 4c 54 45 52 20 54 41 42 4c 45 g);..ALTER TABLE
18b0: 20 4f 4e 4c 59 20 75 72 6c 73 0a 09 41 44 44 20 ONLY urls..ADD
18c0: 43 4f 4e 53 54 52 41 49 4e 54 20 75 72 6c 73 5f CONSTRAINT urls_
18d0: 73 69 74 65 5f 66 20 46 4f 52 45 49 47 4e 20 4b site_f FOREIGN K
18e0: 45 59 20 28 69 64 5f 73 69 74 65 29 20 52 45 46 EY (id_site) REF
18f0: 45 52 45 4e 43 45 53 20 73 69 74 65 28 69 64 5f ERENCES site(id_
1900: 73 69 74 65 29 20 4d 41 54 43 48 20 46 55 4c 4c site) MATCH FULL
1910: 0a 09 4f 4e 20 55 50 44 41 54 45 20 52 45 53 54 ..ON UPDATE REST
1920: 52 49 43 54 20 4f 4e 20 44 45 4c 45 54 45 20 52 RICT ON DELETE R
1930: 45 53 54 52 49 43 54 20 44 45 46 45 52 52 41 42 ESTRICT DEFERRAB
1940: 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 LE INITIALLY DEF
1950: 45 52 52 45 44 3b 0a 0a 41 4c 54 45 52 20 54 41 ERRED;..ALTER TA
1960: 42 4c 45 20 4f 4e 4c 59 20 75 72 6c 73 0a 09 41 BLE ONLY urls..A
1970: 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 20 75 72 DD CONSTRAINT ur
1980: 6c 73 5f 74 61 67 5f 66 20 46 4f 52 45 49 47 4e ls_tag_f FOREIGN
1990: 20 4b 45 59 20 28 69 64 5f 74 61 67 29 20 52 45 KEY (id_tag) RE
19a0: 46 45 52 45 4e 43 45 53 20 74 61 67 28 69 64 5f FERENCES tag(id_
19b0: 74 61 67 29 20 4d 41 54 43 48 20 46 55 4c 4c 0a tag) MATCH FULL.
19c0: 09 4f 4e 20 55 50 44 41 54 45 20 52 45 53 54 52 .ON UPDATE RESTR
19d0: 49 43 54 20 4f 4e 20 44 45 4c 45 54 45 20 52 45 ICT ON DELETE RE
19e0: 53 54 52 49 43 54 20 44 45 46 45 52 52 41 42 4c STRICT DEFERRABL
19f0: 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 E INITIALLY DEFE
1a00: 52 52 45 44 3b 0a 0a 2d 2d 20 72 75 6c 65 20 74 RRED;..-- rule t
1a10: 6f 20 6a 6f 69 6e 20 61 6c 6c 20 74 61 62 6c 65 o join all table
1a20: 73 20 69 6e 74 6f 20 6f 6e 65 20 74 6f 20 73 69 s into one to si
1a30: 6d 70 6c 69 66 79 20 61 63 63 65 73 73 0a 2d 2d mplify access.--
1a40: 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 20 75 73 automaticall us
1a50: 65 73 20 63 75 72 72 65 6e 74 20 64 61 79 20 61 es current day a
1a60: 6e 64 20 74 69 6d 65 20 64 61 74 61 0a 43 52 45 nd time data.CRE
1a70: 41 54 45 20 56 49 45 57 20 73 69 74 65 5f 72 75 ATE VIEW site_ru
1a80: 6c 65 20 41 53 0a 53 45 4c 45 43 54 20 61 2e 72 le AS.SELECT a.r
1a90: 65 64 69 72 65 63 74 5f 75 72 6c 2c 20 61 2e 6e edirect_url, a.n
1aa0: 65 74 6d 61 73 6b 2c 20 62 2e 73 69 74 65 2c 20 etmask, b.site,
1ab0: 62 2e 72 65 67 65 78 70 0a 46 52 4f 4d 20 28 28 b.regexp.FROM ((
1ac0: 0a 09 53 45 4c 45 43 54 20 72 75 6c 65 73 2e 72 ..SELECT rules.r
1ad0: 65 64 69 72 65 63 74 5f 75 72 6c 2c 20 74 61 67 edirect_url, tag
1ae0: 2e 74 61 67 20 41 53 20 72 75 6c 65 5f 74 61 67 .tag AS rule_tag
1af0: 2c 20 72 75 6c 65 73 2e 6e 65 74 6d 61 73 6b 0a , rules.netmask.
1b00: 09 46 52 4f 4d 20 72 75 6c 65 73 20 4e 41 54 55 .FROM rules NATU
1b10: 52 41 4c 20 4a 4f 49 4e 20 74 61 67 0a 09 57 48 RAL JOIN tag..WH
1b20: 45 52 45 20 28 27 6e 6f 77 27 3a 3a 74 65 78 74 ERE ('now'::text
1b30: 29 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 74 20 )::time without
1b40: 74 69 6d 65 20 7a 6f 6e 65 20 3e 3d 20 72 75 6c time zone >= rul
1b50: 65 73 2e 66 72 6f 6d 5f 74 69 6d 65 0a 09 09 41 es.from_time...A
1b60: 4e 44 20 28 27 6e 6f 77 27 3a 3a 74 65 78 74 29 ND ('now'::text)
1b70: 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 74 20 74 ::time without t
1b80: 69 6d 65 20 7a 6f 6e 65 20 3c 3d 20 72 75 6c 65 ime zone <= rule
1b90: 73 2e 74 6f 5f 74 69 6d 65 0a 09 09 41 4e 44 20 s.to_time...AND
1ba0: 64 61 74 65 5f 70 61 72 74 28 27 64 6f 77 27 3a date_part('dow':
1bb0: 3a 74 65 78 74 2c 20 6e 6f 77 28 29 29 20 3e 3d :text, now()) >=
1bc0: 20 28 72 75 6c 65 73 2e 66 72 6f 6d 5f 77 65 65 (rules.from_wee
1bd0: 6b 64 61 79 29 3a 3a 64 6f 75 62 6c 65 20 70 72 kday)::double pr
1be0: 65 63 69 73 69 6f 6e 0a 09 09 41 4e 44 20 64 61 ecision...AND da
1bf0: 74 65 5f 70 61 72 74 28 27 64 6f 77 27 3a 3a 74 te_part('dow'::t
1c00: 65 78 74 2c 20 6e 6f 77 28 29 29 20 3c 3d 20 28 ext, now()) <= (
1c10: 72 75 6c 65 73 2e 74 6f 5f 77 65 65 6b 64 61 79 rules.to_weekday
1c20: 29 3a 3a 64 6f 75 62 6c 65 20 70 72 65 63 69 73 )::double precis
1c30: 69 6f 6e 0a 29 20 61 20 4a 4f 49 4e 20 28 0a 09 ion.) a JOIN (..
1c40: 53 45 4c 45 43 54 20 73 69 74 65 2e 73 69 74 65 SELECT site.site
1c50: 2c 20 74 61 67 2e 74 61 67 20 41 53 20 75 72 6c , tag.tag AS url
1c60: 5f 74 61 67 2c 20 72 65 67 65 78 70 0a 09 46 52 _tag, regexp..FR
1c70: 4f 4d 20 75 72 6c 73 20 4e 41 54 55 52 41 4c 20 OM urls NATURAL
1c80: 4a 4f 49 4e 20 74 61 67 20 4e 41 54 55 52 41 4c JOIN tag NATURAL
1c90: 20 4a 4f 49 4e 20 73 69 74 65 0a 29 20 62 20 4f JOIN site.) b O
1ca0: 4e 20 28 62 2e 75 72 6c 5f 74 61 67 20 26 26 20 N (b.url_tag &&
1cb0: 61 2e 72 75 6c 65 5f 74 61 67 29 29 3b 0a a.rule_tag));.