Artifact
83b3f67d5df288dabd54251b9ae293fa0b18eb53c3ea907428f51fd1c4cbd758:
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 61 64 64 73 20 74 61 67 20 74 6f tion adds tag to
0ff0: 20 64 6f 6d 61 69 6e 20 77 69 74 68 20 72 65 67 domain with reg
1000: 65 78 70 0a 43 52 45 41 54 45 20 6f 72 20 72 65 exp.CREATE or re
1010: 70 6c 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 6d place FUNCTION m
1020: 61 72 6b 28 64 6f 6d 61 69 6e 20 74 65 78 74 2c ark(domain text,
1030: 20 74 61 67 73 20 74 65 78 74 5b 5d 2c 20 72 65 tags text[], re
1040: 67 65 78 70 20 74 65 78 74 29 20 52 45 54 55 52 gexp text) RETUR
1050: 4e 53 20 69 6e 74 65 67 65 72 0a 09 4c 41 4e 47 NS integer..LANG
1060: 55 41 47 45 20 73 71 6c 20 69 6d 6d 75 74 61 62 UAGE sql immutab
1070: 6c 65 20 53 54 52 49 43 54 0a 09 41 53 20 24 24 le STRICT..AS $$
1080: 0a 73 65 6c 65 63 74 20 6d 61 72 6b 28 67 65 74 .select mark(get
1090: 5f 73 69 74 65 28 24 31 29 2c 20 67 65 74 5f 74 _site($1), get_t
10a0: 61 67 28 24 32 29 2c 20 24 33 29 20 61 73 20 72 ag($2), $3) as r
10b0: 65 73 75 6c 74 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 esult;.$$;..-- t
10c0: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 his function add
10d0: 73 20 74 61 67 20 74 6f 20 64 6f 6d 61 69 6e 20 s tag to domain
10e0: 77 69 74 68 20 72 65 67 65 78 70 0a 43 52 45 41 with regexp.CREA
10f0: 54 45 20 6f 72 20 72 65 70 6c 61 63 65 20 46 55 TE or replace FU
1100: 4e 43 54 49 4f 4e 20 6d 61 72 6b 28 64 6f 6d 61 NCTION mark(doma
1110: 69 6e 20 74 65 78 74 2c 20 74 61 67 73 20 74 65 in text, tags te
1120: 78 74 5b 5d 29 20 52 45 54 55 52 4e 53 20 69 6e xt[]) RETURNS in
1130: 74 65 67 65 72 0a 09 4c 41 4e 47 55 41 47 45 20 teger..LANGUAGE
1140: 73 71 6c 20 69 6d 6d 75 74 61 62 6c 65 20 53 54 sql immutable ST
1150: 52 49 43 54 0a 09 41 53 20 24 24 0a 73 65 6c 65 RICT..AS $$.sele
1160: 63 74 20 6d 61 72 6b 28 67 65 74 5f 73 69 74 65 ct mark(get_site
1170: 28 24 31 29 2c 20 67 65 74 5f 74 61 67 28 24 32 ($1), get_tag($2
1180: 29 2c 20 4e 55 4c 4c 29 20 61 73 20 72 65 73 75 ), NULL) as resu
1190: 6c 74 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 lt;.$$;..-- this
11a0: 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
11b0: 73 20 69 64 20 6f 66 20 74 61 67 20 61 72 72 61 s id of tag arra
11c0: 79 0a 63 72 65 61 74 65 20 6f 72 20 72 65 70 6c y.create or repl
11d0: 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 67 65 74 ace function get
11e0: 5f 74 61 67 28 6d 79 5f 74 61 67 20 74 65 78 74 _tag(my_tag text
11f0: 5b 5d 29 20 72 65 74 75 72 6e 73 20 69 6e 74 65 []) returns inte
1200: 67 65 72 0a 09 6c 61 6e 67 75 61 67 65 20 70 6c ger..language pl
1210: 70 67 73 71 6c 20 73 74 72 69 63 74 0a 09 61 73 pgsql strict..as
1220: 20 24 24 0a 64 65 63 6c 61 72 65 0a 09 74 61 67 $$.declare..tag
1230: 5f 69 64 20 69 6e 74 65 67 65 72 3b 0a 62 65 67 _id integer;.beg
1240: 69 6e 0a 09 73 65 6c 65 63 74 20 69 64 5f 74 61 in..select id_ta
1250: 67 20 66 72 6f 6d 20 74 61 67 20 77 68 65 72 65 g from tag where
1260: 20 75 73 6f 72 74 28 6d 79 5f 74 61 67 29 20 3d usort(my_tag) =
1270: 20 74 61 67 20 69 6e 74 6f 20 74 61 67 5f 69 64 tag into tag_id
1280: 3b 0a 09 69 66 20 6e 6f 74 20 66 6f 75 6e 64 20 ;..if not found
1290: 74 68 65 6e 0a 09 09 69 6e 73 65 72 74 20 69 6e then...insert in
12a0: 74 6f 20 74 61 67 20 28 74 61 67 29 20 76 61 6c to tag (tag) val
12b0: 75 65 73 20 28 75 73 6f 72 74 28 6d 79 5f 74 61 ues (usort(my_ta
12c0: 67 29 29 3b 0a 09 09 73 65 6c 65 63 74 20 69 64 g));...select id
12d0: 5f 74 61 67 20 66 72 6f 6d 20 74 61 67 20 77 68 _tag from tag wh
12e0: 65 72 65 20 75 73 6f 72 74 28 6d 79 5f 74 61 67 ere usort(my_tag
12f0: 29 20 3d 20 74 61 67 20 69 6e 74 6f 20 74 61 67 ) = tag into tag
1300: 5f 69 64 3b 0a 09 65 6e 64 20 69 66 3b 0a 09 72 _id;..end if;..r
1310: 65 74 75 72 6e 20 74 61 67 5f 69 64 3b 0a 65 6e eturn tag_id;.en
1320: 64 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 61 62 6c 65 d;.$$;..-- table
1330: 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 72 75 6c to hold all rul
1340: 65 73 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 es.CREATE TABLE
1350: 72 75 6c 65 73 20 28 0a 09 6e 65 74 6d 61 73 6b rules (..netmask
1360: 20 63 69 64 72 20 4e 4f 54 20 4e 55 4c 4c 2c 0a cidr NOT NULL,.
1370: 09 72 65 64 69 72 65 63 74 5f 75 72 6c 20 74 65 .redirect_url te
1380: 78 74 20 44 45 46 41 55 4c 54 20 27 61 62 6f 75 xt DEFAULT 'abou
1390: 74 3a 3a 62 6c 61 6e 6b 27 3a 3a 74 65 78 74 20 t::blank'::text
13a0: 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 66 72 6f 6d 5f NOT NULL,..from_
13b0: 77 65 65 6b 64 61 79 20 73 6d 61 6c 6c 69 6e 74 weekday smallint
13c0: 20 44 45 46 41 55 4c 54 20 30 20 4e 4f 54 20 4e DEFAULT 0 NOT N
13d0: 55 4c 4c 2c 0a 09 74 6f 5f 77 65 65 6b 64 61 79 ULL,..to_weekday
13e0: 20 73 6d 61 6c 6c 69 6e 74 20 44 45 46 41 55 4c smallint DEFAUL
13f0: 54 20 36 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 66 T 6 NOT NULL,..f
1400: 72 6f 6d 5f 74 69 6d 65 20 74 69 6d 65 20 77 69 rom_time time wi
1410: 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 thout time zone
1420: 44 45 46 41 55 4c 54 20 27 30 30 3a 30 30 3a 30 DEFAULT '00:00:0
1430: 30 27 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 74 0'::time without
1440: 20 74 69 6d 65 20 7a 6f 6e 65 20 4e 4f 54 20 4e time zone NOT N
1450: 55 4c 4c 2c 0a 09 74 6f 5f 74 69 6d 65 20 74 69 ULL,..to_time ti
1460: 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 me without time
1470: 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 27 32 33 zone DEFAULT '23
1480: 3a 35 39 3a 35 39 27 3a 3a 74 69 6d 65 20 77 69 :59:59'::time wi
1490: 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 thout time zone
14a0: 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 69 64 5f 74 61 NOT NULL,..id_ta
14b0: 67 20 73 6d 61 6c 6c 69 6e 74 20 4e 4f 54 20 4e g smallint NOT N
14c0: 55 4c 4c 0a 29 3b 0a 0a 41 4c 54 45 52 20 54 41 ULL.);..ALTER TA
14d0: 42 4c 45 20 4f 4e 4c 59 20 72 75 6c 65 73 0a 09 BLE ONLY rules..
14e0: 41 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 20 72 ADD CONSTRAINT r
14f0: 75 6c 65 73 5f 70 6b 65 79 20 50 52 49 4d 41 52 ules_pkey PRIMAR
1500: 59 20 4b 45 59 20 28 6e 65 74 6d 61 73 6b 29 3b Y KEY (netmask);
1510: 0a 0a 41 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e ..ALTER TABLE ON
1520: 4c 59 20 72 75 6c 65 73 0a 09 41 44 44 20 43 4f LY rules..ADD CO
1530: 4e 53 54 52 41 49 4e 54 20 72 75 6c 65 73 5f 74 NSTRAINT rules_t
1540: 61 67 5f 66 20 46 4f 52 45 49 47 4e 20 4b 45 59 ag_f FOREIGN KEY
1550: 20 28 69 64 5f 74 61 67 29 20 52 45 46 45 52 45 (id_tag) REFERE
1560: 4e 43 45 53 20 74 61 67 28 69 64 5f 74 61 67 29 NCES tag(id_tag)
1570: 20 4d 41 54 43 48 20 46 55 4c 4c 0a 09 4f 4e 20 MATCH FULL..ON
1580: 55 50 44 41 54 45 20 52 45 53 54 52 49 43 54 20 UPDATE RESTRICT
1590: 4f 4e 20 44 45 4c 45 54 45 20 52 45 53 54 52 49 ON DELETE RESTRI
15a0: 43 54 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e CT DEFERRABLE IN
15b0: 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 ITIALLY DEFERRED
15c0: 3b 0a 0a 2d 2d 20 74 61 62 6c 65 20 74 6f 20 68 ;..-- table to h
15d0: 6f 6c 64 20 73 69 74 65 20 61 72 72 61 79 73 0a old site arrays.
15e0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 69 74 CREATE TABLE sit
15f0: 65 20 28 0a 09 69 64 5f 73 69 74 65 20 73 65 72 e (..id_site ser
1600: 69 61 6c 2c 0a 09 73 69 74 65 20 74 65 78 74 5b ial,..site text[
1610: 5d 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a 0a 41 ] NOT NULL.);..A
1620: 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 LTER TABLE ONLY
1630: 73 69 74 65 0a 09 41 44 44 20 43 4f 4e 53 54 52 site..ADD CONSTR
1640: 41 49 4e 54 20 73 69 74 65 5f 70 6b 65 79 20 50 AINT site_pkey P
1650: 52 49 4d 41 52 59 20 4b 45 59 20 28 69 64 5f 73 RIMARY KEY (id_s
1660: 69 74 65 29 3b 0a 0a 43 52 45 41 54 45 20 55 4e ite);..CREATE UN
1670: 49 51 55 45 20 49 4e 44 45 58 20 73 69 74 65 5f IQUE INDEX site_
1680: 75 20 4f 4e 20 73 69 74 65 20 28 75 73 6f 72 74 u ON site (usort
1690: 28 73 69 74 65 29 29 3b 0a 0a 43 52 45 41 54 45 (site));..CREATE
16a0: 20 49 4e 44 45 58 20 73 69 74 65 5f 67 20 4f 4e INDEX site_g ON
16b0: 20 73 69 74 65 20 55 53 49 4e 47 20 67 69 6e 20 site USING gin
16c0: 28 73 69 74 65 29 3b 0a 0a 2d 2d 20 74 61 62 6c (site);..-- tabl
16d0: 65 20 74 6f 20 68 6f 6c 64 20 74 61 67 20 63 6f e to hold tag co
16e0: 6d 62 69 6e 61 74 69 6f 6e 73 0a 43 52 45 41 54 mbinations.CREAT
16f0: 45 20 54 41 42 4c 45 20 74 61 67 20 28 0a 09 69 E TABLE tag (..i
1700: 64 5f 74 61 67 20 73 65 72 69 61 6c 2c 0a 09 74 d_tag serial,..t
1710: 61 67 20 74 65 78 74 5b 5d 20 4e 4f 54 20 4e 55 ag text[] NOT NU
1720: 4c 4c 0a 29 3b 0a 0a 41 4c 54 45 52 20 54 41 42 LL.);..ALTER TAB
1730: 4c 45 20 4f 4e 4c 59 20 74 61 67 0a 09 41 44 44 LE ONLY tag..ADD
1740: 20 43 4f 4e 53 54 52 41 49 4e 54 20 74 61 67 5f CONSTRAINT tag_
1750: 70 6b 65 79 20 50 52 49 4d 41 52 59 20 4b 45 59 pkey PRIMARY KEY
1760: 20 28 69 64 5f 74 61 67 29 3b 0a 0a 43 52 45 41 (id_tag);..CREA
1770: 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 TE UNIQUE INDEX
1780: 74 61 67 5f 75 20 4f 4e 20 74 61 67 20 28 75 73 tag_u ON tag (us
1790: 6f 72 74 28 74 61 67 29 29 3b 0a 0a 43 52 45 41 ort(tag));..CREA
17a0: 54 45 20 49 4e 44 45 58 20 74 61 67 5f 67 20 4f TE INDEX tag_g O
17b0: 4e 20 74 61 67 20 55 53 49 4e 47 20 67 69 6e 20 N tag USING gin
17c0: 28 74 61 67 29 3b 0a 0a 2d 2d 20 74 61 62 6c 65 (tag);..-- table
17d0: 20 74 6f 20 68 6f 6c 64 20 74 61 67 20 2d 20 73 to hold tag - s
17e0: 69 74 65 20 6c 69 6e 6b 73 0a 43 52 45 41 54 45 ite links.CREATE
17f0: 20 54 41 42 4c 45 20 75 72 6c 73 20 28 0a 09 64 TABLE urls (..d
1800: 61 74 65 5f 61 64 64 65 64 20 74 69 6d 65 73 74 ate_added timest
1810: 61 6d 70 20 77 69 74 68 6f 75 74 20 74 69 6d 65 amp without time
1820: 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 28 27 zone DEFAULT ('
1830: 6e 6f 77 27 3a 3a 74 65 78 74 29 3a 3a 74 69 6d now'::text)::tim
1840: 65 73 74 61 6d 70 28 30 29 20 77 69 74 68 6f 75 estamp(0) withou
1850: 74 20 74 69 6d 65 20 7a 6f 6e 65 20 4e 4f 54 20 t time zone NOT
1860: 4e 55 4c 4c 2c 0a 09 69 64 5f 73 69 74 65 20 73 NULL,..id_site s
1870: 6d 61 6c 6c 69 6e 74 20 4e 4f 54 20 4e 55 4c 4c mallint NOT NULL
1880: 2c 0a 09 69 64 5f 74 61 67 20 73 6d 61 6c 6c 69 ,..id_tag smalli
1890: 6e 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 72 65 nt NOT NULL,..re
18a0: 67 65 78 70 20 74 65 78 74 0a 29 3b 0a 0a 43 52 gexp text.);..CR
18b0: 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 EATE UNIQUE INDE
18c0: 58 20 75 72 6c 73 5f 70 6b 65 79 20 4f 4e 20 75 X urls_pkey ON u
18d0: 72 6c 73 20 55 53 49 4e 47 20 62 74 72 65 65 20 rls USING btree
18e0: 28 69 64 5f 73 69 74 65 2c 20 72 65 67 65 78 70 (id_site, regexp
18f0: 29 3b 0a 0a 43 52 45 41 54 45 20 49 4e 44 45 58 );..CREATE INDEX
1900: 20 75 72 6c 73 5f 69 64 5f 74 61 67 20 4f 4e 20 urls_id_tag ON
1910: 75 72 6c 73 20 55 53 49 4e 47 20 62 74 72 65 65 urls USING btree
1920: 20 28 69 64 5f 74 61 67 29 3b 0a 0a 41 4c 54 45 (id_tag);..ALTE
1930: 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 75 72 6c R TABLE ONLY url
1940: 73 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 49 4e s..ADD CONSTRAIN
1950: 54 20 75 72 6c 73 5f 73 69 74 65 5f 66 20 46 4f T urls_site_f FO
1960: 52 45 49 47 4e 20 4b 45 59 20 28 69 64 5f 73 69 REIGN KEY (id_si
1970: 74 65 29 20 52 45 46 45 52 45 4e 43 45 53 20 73 te) REFERENCES s
1980: 69 74 65 28 69 64 5f 73 69 74 65 29 20 4d 41 54 ite(id_site) MAT
1990: 43 48 20 46 55 4c 4c 0a 09 4f 4e 20 55 50 44 41 CH FULL..ON UPDA
19a0: 54 45 20 52 45 53 54 52 49 43 54 20 4f 4e 20 44 TE RESTRICT ON D
19b0: 45 4c 45 54 45 20 52 45 53 54 52 49 43 54 20 44 ELETE RESTRICT D
19c0: 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 EFERRABLE INITIA
19d0: 4c 4c 59 20 44 45 46 45 52 52 45 44 3b 0a 0a 41 LLY DEFERRED;..A
19e0: 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 LTER TABLE ONLY
19f0: 75 72 6c 73 0a 09 41 44 44 20 43 4f 4e 53 54 52 urls..ADD CONSTR
1a00: 41 49 4e 54 20 75 72 6c 73 5f 74 61 67 5f 66 20 AINT urls_tag_f
1a10: 46 4f 52 45 49 47 4e 20 4b 45 59 20 28 69 64 5f FOREIGN KEY (id_
1a20: 74 61 67 29 20 52 45 46 45 52 45 4e 43 45 53 20 tag) REFERENCES
1a30: 74 61 67 28 69 64 5f 74 61 67 29 20 4d 41 54 43 tag(id_tag) MATC
1a40: 48 20 46 55 4c 4c 0a 09 4f 4e 20 55 50 44 41 54 H FULL..ON UPDAT
1a50: 45 20 52 45 53 54 52 49 43 54 20 4f 4e 20 44 45 E RESTRICT ON DE
1a60: 4c 45 54 45 20 52 45 53 54 52 49 43 54 20 44 45 LETE RESTRICT DE
1a70: 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c FERRABLE INITIAL
1a80: 4c 59 20 44 45 46 45 52 52 45 44 3b 0a 0a 2d 2d LY DEFERRED;..--
1a90: 20 72 75 6c 65 20 74 6f 20 6a 6f 69 6e 20 61 6c rule to join al
1aa0: 6c 20 74 61 62 6c 65 73 20 69 6e 74 6f 20 6f 6e l tables into on
1ab0: 65 20 74 6f 20 73 69 6d 70 6c 69 66 79 20 61 63 e to simplify ac
1ac0: 63 65 73 73 0a 2d 2d 20 61 75 74 6f 6d 61 74 69 cess.-- automati
1ad0: 63 61 6c 6c 20 75 73 65 73 20 63 75 72 72 65 6e call uses curren
1ae0: 74 20 64 61 79 20 61 6e 64 20 74 69 6d 65 20 64 t day and time d
1af0: 61 74 61 0a 43 52 45 41 54 45 20 56 49 45 57 20 ata.CREATE VIEW
1b00: 73 69 74 65 5f 72 75 6c 65 20 41 53 0a 53 45 4c site_rule AS.SEL
1b10: 45 43 54 20 61 2e 72 65 64 69 72 65 63 74 5f 75 ECT a.redirect_u
1b20: 72 6c 2c 20 61 2e 6e 65 74 6d 61 73 6b 2c 20 62 rl, a.netmask, b
1b30: 2e 73 69 74 65 2c 20 62 2e 72 65 67 65 78 70 0a .site, b.regexp.
1b40: 46 52 4f 4d 20 28 28 0a 09 53 45 4c 45 43 54 20 FROM ((..SELECT
1b50: 72 75 6c 65 73 2e 72 65 64 69 72 65 63 74 5f 75 rules.redirect_u
1b60: 72 6c 2c 20 74 61 67 2e 74 61 67 20 41 53 20 72 rl, tag.tag AS r
1b70: 75 6c 65 5f 74 61 67 2c 20 72 75 6c 65 73 2e 6e ule_tag, rules.n
1b80: 65 74 6d 61 73 6b 0a 09 46 52 4f 4d 20 72 75 6c etmask..FROM rul
1b90: 65 73 20 4e 41 54 55 52 41 4c 20 4a 4f 49 4e 20 es NATURAL JOIN
1ba0: 74 61 67 0a 09 57 48 45 52 45 20 28 27 6e 6f 77 tag..WHERE ('now
1bb0: 27 3a 3a 74 65 78 74 29 3a 3a 74 69 6d 65 20 77 '::text)::time w
1bc0: 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 ithout time zone
1bd0: 20 3e 3d 20 72 75 6c 65 73 2e 66 72 6f 6d 5f 74 >= rules.from_t
1be0: 69 6d 65 0a 09 09 41 4e 44 20 28 27 6e 6f 77 27 ime...AND ('now'
1bf0: 3a 3a 74 65 78 74 29 3a 3a 74 69 6d 65 20 77 69 ::text)::time wi
1c00: 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 thout time zone
1c10: 3c 3d 20 72 75 6c 65 73 2e 74 6f 5f 74 69 6d 65 <= rules.to_time
1c20: 0a 09 09 41 4e 44 20 64 61 74 65 5f 70 61 72 74 ...AND date_part
1c30: 28 27 64 6f 77 27 3a 3a 74 65 78 74 2c 20 6e 6f ('dow'::text, no
1c40: 77 28 29 29 20 3e 3d 20 28 72 75 6c 65 73 2e 66 w()) >= (rules.f
1c50: 72 6f 6d 5f 77 65 65 6b 64 61 79 29 3a 3a 64 6f rom_weekday)::do
1c60: 75 62 6c 65 20 70 72 65 63 69 73 69 6f 6e 0a 09 uble precision..
1c70: 09 41 4e 44 20 64 61 74 65 5f 70 61 72 74 28 27 .AND date_part('
1c80: 64 6f 77 27 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 dow'::text, now(
1c90: 29 29 20 3c 3d 20 28 72 75 6c 65 73 2e 74 6f 5f )) <= (rules.to_
1ca0: 77 65 65 6b 64 61 79 29 3a 3a 64 6f 75 62 6c 65 weekday)::double
1cb0: 20 70 72 65 63 69 73 69 6f 6e 0a 29 20 61 20 4a precision.) a J
1cc0: 4f 49 4e 20 28 0a 09 53 45 4c 45 43 54 20 73 69 OIN (..SELECT si
1cd0: 74 65 2e 73 69 74 65 2c 20 74 61 67 2e 74 61 67 te.site, tag.tag
1ce0: 20 41 53 20 75 72 6c 5f 74 61 67 2c 20 72 65 67 AS url_tag, reg
1cf0: 65 78 70 0a 09 46 52 4f 4d 20 75 72 6c 73 20 4e exp..FROM urls N
1d00: 41 54 55 52 41 4c 20 4a 4f 49 4e 20 74 61 67 20 ATURAL JOIN tag
1d10: 4e 41 54 55 52 41 4c 20 4a 4f 49 4e 20 73 69 74 NATURAL JOIN sit
1d20: 65 0a 29 20 62 20 4f 4e 20 28 62 2e 75 72 6c 5f e.) b ON (b.url_
1d30: 74 61 67 20 26 26 20 61 2e 72 75 6c 65 5f 74 61 tag && a.rule_ta
1d40: 67 29 29 3b 0a g));.