Artifact
dcb8126c9c19862a190e5cf1c241b7abe6a163c586160bfe757619d22f3a02ee:
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 43 52 45 41 54 45 20 46 55 4e 43 ql;..CREATE FUNC
0030: 54 49 4f 4e 20 6d 61 72 6b 28 64 6f 6d 61 69 6e TION mark(domain
0040: 20 74 65 78 74 2c 20 6e 65 77 5f 74 61 67 20 74 text, new_tag t
0050: 65 78 74 29 20 52 45 54 55 52 4e 53 20 76 6f 69 ext) RETURNS voi
0060: 64 0a 20 20 20 20 4c 41 4e 47 55 41 47 45 20 70 d. LANGUAGE p
0070: 6c 70 67 73 71 6c 20 53 54 52 49 43 54 0a 20 20 lpgsql STRICT.
0080: 20 20 41 53 20 24 24 0a 64 65 63 6c 61 72 65 0a AS $$.declare.
0090: 20 6d 79 5f 73 69 74 65 20 74 65 78 74 5b 5d 3b my_site text[];
00a0: 0a 20 6d 79 5f 73 69 74 65 5f 69 64 20 73 6d 61 . my_site_id sma
00b0: 6c 6c 69 6e 74 3b 0a 20 6d 79 5f 74 61 67 20 74 llint;. my_tag t
00c0: 65 78 74 5b 5d 3b 0a 20 6d 79 5f 74 61 67 5f 69 ext[];. my_tag_i
00d0: 64 20 73 6d 61 6c 6c 69 6e 74 3b 0a 62 65 67 69 d smallint;.begi
00e0: 6e 0a 20 6d 79 5f 73 69 74 65 20 3a 3d 20 74 72 n. my_site := tr
00f0: 69 70 64 6f 6d 61 69 6e 28 64 6f 6d 61 69 6e 29 ipdomain(domain)
0100: 3b 0a 0a 20 73 65 6c 65 63 74 20 69 64 5f 73 69 ;.. select id_si
0110: 74 65 20 66 72 6f 6d 20 73 69 74 65 20 77 68 65 te from site whe
0120: 72 65 20 6d 79 5f 73 69 74 65 20 3d 20 73 69 74 re my_site = sit
0130: 65 20 69 6e 74 6f 20 6d 79 5f 73 69 74 65 5f 69 e into my_site_i
0140: 64 3b 0a 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 d;. if not found
0150: 20 74 68 65 6e 0a 20 20 69 6e 73 65 72 74 20 69 then. insert i
0160: 6e 74 6f 20 73 69 74 65 20 28 73 69 74 65 29 20 nto site (site)
0170: 76 61 6c 75 65 73 20 28 6d 79 5f 73 69 74 65 29 values (my_site)
0180: 3b 0a 20 20 73 65 6c 65 63 74 20 69 64 5f 73 69 ;. select id_si
0190: 74 65 20 66 72 6f 6d 20 73 69 74 65 20 77 68 65 te from site whe
01a0: 72 65 20 6d 79 5f 73 69 74 65 20 3d 20 73 69 74 re my_site = sit
01b0: 65 20 69 6e 74 6f 20 6d 79 5f 73 69 74 65 5f 69 e into my_site_i
01c0: 64 3b 0a 20 65 6e 64 20 69 66 3b 0a 0a 20 73 65 d;. end if;.. se
01d0: 6c 65 63 74 20 74 61 67 20 66 72 6f 6d 20 75 72 lect tag from ur
01e0: 6c 73 20 6e 61 74 75 72 61 6c 20 6a 6f 69 6e 20 ls natural join
01f0: 74 61 67 20 77 68 65 72 65 20 69 64 5f 73 69 74 tag where id_sit
0200: 65 20 3d 20 6d 79 5f 73 69 74 65 5f 69 64 20 69 e = my_site_id i
0210: 6e 74 6f 20 6d 79 5f 74 61 67 3b 0a 20 69 66 20 nto my_tag;. if
0220: 6e 6f 74 20 66 6f 75 6e 64 20 74 68 65 6e 0a 20 not found then.
0230: 20 6d 79 5f 74 61 67 20 3a 3d 20 61 72 72 61 79 my_tag := array
0240: 5b 6e 65 77 5f 74 61 67 5d 3b 0a 20 65 6c 73 65 [new_tag];. else
0250: 0a 20 20 73 65 6c 65 63 74 20 61 72 72 61 79 5f . select array_
0260: 61 67 67 28 74 61 67 29 20 66 72 6f 6d 20 28 73 agg(tag) from (s
0270: 65 6c 65 63 74 20 64 69 73 74 69 6e 63 74 20 75 elect distinct u
0280: 6e 6e 65 73 74 28 6d 79 5f 74 61 67 20 7c 7c 20 nnest(my_tag ||
0290: 61 72 72 61 79 5b 6e 65 77 5f 74 61 67 5d 29 20 array[new_tag])
02a0: 61 73 20 74 61 67 20 6f 72 64 65 72 20 62 79 20 as tag order by
02b0: 74 61 67 20 61 73 63 29 20 61 20 69 6e 74 6f 20 tag asc) a into
02c0: 0a 6d 79 5f 74 61 67 3b 0a 20 20 64 65 6c 65 74 .my_tag;. delet
02d0: 65 20 66 72 6f 6d 20 75 72 6c 73 20 77 68 65 72 e from urls wher
02e0: 65 20 69 64 5f 73 69 74 65 20 3d 20 6d 79 5f 73 e id_site = my_s
02f0: 69 74 65 5f 69 64 3b 0a 20 65 6e 64 20 69 66 3b ite_id;. end if;
0300: 0a 0a 20 73 65 6c 65 63 74 20 69 64 5f 74 61 67 .. select id_tag
0310: 20 66 72 6f 6d 20 74 61 67 20 77 68 65 72 65 20 from tag where
0320: 6d 79 5f 74 61 67 20 3d 20 74 61 67 20 69 6e 74 my_tag = tag int
0330: 6f 20 6d 79 5f 74 61 67 5f 69 64 3b 0a 20 69 66 o my_tag_id;. if
0340: 20 6e 6f 74 20 66 6f 75 6e 64 20 74 68 65 6e 0a not found then.
0350: 20 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 74 61 insert into ta
0360: 67 20 28 74 61 67 29 20 76 61 6c 75 65 73 28 6d g (tag) values(m
0370: 79 5f 74 61 67 29 3b 0a 20 20 73 65 6c 65 63 74 y_tag);. select
0380: 20 69 64 5f 74 61 67 20 66 72 6f 6d 20 74 61 67 id_tag from tag
0390: 20 77 68 65 72 65 20 6d 79 5f 74 61 67 20 3d 20 where my_tag =
03a0: 74 61 67 20 69 6e 74 6f 20 6d 79 5f 74 61 67 5f tag into my_tag_
03b0: 69 64 3b 0a 20 65 6e 64 20 69 66 3b 0a 0a 20 69 id;. end if;.. i
03c0: 6e 73 65 72 74 20 69 6e 74 6f 20 75 72 6c 73 20 nsert into urls
03d0: 28 69 64 5f 73 69 74 65 2c 20 69 64 5f 74 61 67 (id_site, id_tag
03e0: 29 20 76 61 6c 75 65 73 20 28 6d 79 5f 73 69 74 ) values (my_sit
03f0: 65 5f 69 64 2c 20 6d 79 5f 74 61 67 5f 69 64 29 e_id, my_tag_id)
0400: 3b 0a 65 6e 64 3b 24 24 3b 0a 0a 43 52 45 41 54 ;.end;$$;..CREAT
0410: 45 20 46 55 4e 43 54 49 4f 4e 20 6d 61 72 6b 28 E FUNCTION mark(
0420: 6d 79 5f 73 69 74 65 5f 69 64 20 73 6d 61 6c 6c my_site_id small
0430: 69 6e 74 2c 20 6e 65 77 5f 74 61 67 20 74 65 78 int, new_tag tex
0440: 74 29 20 52 45 54 55 52 4e 53 20 76 6f 69 64 0a t) RETURNS void.
0450: 20 20 20 20 4c 41 4e 47 55 41 47 45 20 70 6c 70 LANGUAGE plp
0460: 67 73 71 6c 20 53 54 52 49 43 54 0a 20 20 20 20 gsql STRICT.
0470: 41 53 20 24 24 0a 64 65 63 6c 61 72 65 0a 20 6d AS $$.declare. m
0480: 79 5f 74 61 67 20 74 65 78 74 5b 5d 3b 0a 20 6d y_tag text[];. m
0490: 79 5f 74 61 67 5f 69 64 20 73 6d 61 6c 6c 69 6e y_tag_id smallin
04a0: 74 3b 0a 62 65 67 69 6e 0a 0a 20 73 65 6c 65 63 t;.begin.. selec
04b0: 74 20 74 61 67 20 66 72 6f 6d 20 75 72 6c 73 20 t tag from urls
04c0: 6e 61 74 75 72 61 6c 20 6a 6f 69 6e 20 74 61 67 natural join tag
04d0: 20 77 68 65 72 65 20 69 64 5f 73 69 74 65 20 3d where id_site =
04e0: 20 6d 79 5f 73 69 74 65 5f 69 64 20 69 6e 74 6f my_site_id into
04f0: 20 6d 79 5f 74 61 67 3b 0a 20 69 66 20 6e 6f 74 my_tag;. if not
0500: 20 66 6f 75 6e 64 20 74 68 65 6e 0a 20 20 6d 79 found then. my
0510: 5f 74 61 67 20 3a 3d 20 61 72 72 61 79 5b 6e 65 _tag := array[ne
0520: 77 5f 74 61 67 5d 3b 0a 20 65 6c 73 65 0a 20 20 w_tag];. else.
0530: 73 65 6c 65 63 74 20 61 72 72 61 79 5f 61 67 67 select array_agg
0540: 28 74 61 67 29 20 66 72 6f 6d 20 28 73 65 6c 65 (tag) from (sele
0550: 63 74 20 64 69 73 74 69 6e 63 74 20 75 6e 6e 65 ct distinct unne
0560: 73 74 28 6d 79 5f 74 61 67 20 7c 7c 20 61 72 72 st(my_tag || arr
0570: 61 79 5b 6e 65 77 5f 74 61 67 5d 29 20 61 73 20 ay[new_tag]) as
0580: 74 61 67 20 6f 72 64 65 72 20 62 79 20 74 61 67 tag order by tag
0590: 20 61 73 63 29 20 61 20 69 6e 74 6f 20 0a 6d 79 asc) a into .my
05a0: 5f 74 61 67 3b 0a 20 20 64 65 6c 65 74 65 20 66 _tag;. delete f
05b0: 72 6f 6d 20 75 72 6c 73 20 77 68 65 72 65 20 69 rom urls where i
05c0: 64 5f 73 69 74 65 20 3d 20 6d 79 5f 73 69 74 65 d_site = my_site
05d0: 5f 69 64 3b 0a 20 65 6e 64 20 69 66 3b 0a 0a 20 _id;. end if;..
05e0: 73 65 6c 65 63 74 20 69 64 5f 74 61 67 20 66 72 select id_tag fr
05f0: 6f 6d 20 74 61 67 20 77 68 65 72 65 20 6d 79 5f om tag where my_
0600: 74 61 67 20 3d 20 74 61 67 20 69 6e 74 6f 20 6d tag = tag into m
0610: 79 5f 74 61 67 5f 69 64 3b 0a 20 69 66 20 6e 6f y_tag_id;. if no
0620: 74 20 66 6f 75 6e 64 20 74 68 65 6e 0a 20 20 69 t found then. i
0630: 6e 73 65 72 74 20 69 6e 74 6f 20 74 61 67 20 28 nsert into tag (
0640: 74 61 67 29 20 76 61 6c 75 65 73 28 6d 79 5f 74 tag) values(my_t
0650: 61 67 29 3b 0a 20 20 73 65 6c 65 63 74 20 69 64 ag);. select id
0660: 5f 74 61 67 20 66 72 6f 6d 20 74 61 67 20 77 68 _tag from tag wh
0670: 65 72 65 20 6d 79 5f 74 61 67 20 3d 20 74 61 67 ere my_tag = tag
0680: 20 69 6e 74 6f 20 6d 79 5f 74 61 67 5f 69 64 3b into my_tag_id;
0690: 0a 20 65 6e 64 20 69 66 3b 0a 0a 20 69 6e 73 65 . end if;.. inse
06a0: 72 74 20 69 6e 74 6f 20 75 72 6c 73 20 28 69 64 rt into urls (id
06b0: 5f 73 69 74 65 2c 20 69 64 5f 74 61 67 29 20 76 _site, id_tag) v
06c0: 61 6c 75 65 73 20 28 6d 79 5f 73 69 74 65 5f 69 alues (my_site_i
06d0: 64 2c 20 6d 79 5f 74 61 67 5f 69 64 29 3b 0a 65 d, my_tag_id);.e
06e0: 6e 64 3b 24 24 3b 0a 0a 43 52 45 41 54 45 20 46 nd;$$;..CREATE F
06f0: 55 4e 43 54 49 4f 4e 20 74 72 69 70 64 6f 6d 61 UNCTION tripdoma
0700: 69 6e 28 75 72 6c 20 74 65 78 74 29 20 52 45 54 in(url text) RET
0710: 55 52 4e 53 20 74 65 78 74 5b 5d 0a 20 20 20 20 URNS text[].
0720: 4c 41 4e 47 55 41 47 45 20 70 6c 70 67 73 71 6c LANGUAGE plpgsql
0730: 20 49 4d 4d 55 54 41 42 4c 45 20 53 54 52 49 43 IMMUTABLE STRIC
0740: 54 0a 20 20 20 20 41 53 20 24 5f 24 0a 64 65 63 T. AS $_$.dec
0750: 6c 61 72 65 0a 20 20 72 65 73 75 6c 74 20 76 61 lare. result va
0760: 72 63 68 61 72 5b 5d 3b 0a 20 20 73 70 6c 69 74 rchar[];. split
0770: 74 65 64 20 76 61 72 63 68 61 72 5b 5d 3b 0a 20 ted varchar[];.
0780: 20 72 65 76 65 72 73 65 64 20 76 61 72 63 68 61 reversed varcha
0790: 72 5b 5d 3b 0a 20 20 78 20 69 6e 74 65 67 65 72 r[];. x integer
07a0: 3b 0a 62 65 67 69 6e 0a 20 20 78 20 3a 3d 20 31 ;.begin. x := 1
07b0: 3b 0a 20 20 73 70 6c 69 74 74 65 64 20 3a 3d 20 ;. splitted :=
07c0: 73 74 72 69 6e 67 5f 74 6f 5f 61 72 72 61 79 28 string_to_array(
07d0: 24 31 2c 20 27 2e 27 29 3b 0a 20 20 6c 6f 6f 70 $1, '.');. loop
07e0: 0a 20 20 20 20 65 78 69 74 20 77 68 65 6e 20 73 . exit when s
07f0: 70 6c 69 74 74 65 64 5b 78 5d 20 69 73 20 6e 75 plitted[x] is nu
0800: 6c 6c 3b 0a 20 20 20 20 69 66 20 6c 65 6e 67 74 ll;. if lengt
0810: 68 28 73 70 6c 69 74 74 65 64 5b 78 5d 29 20 3e h(splitted[x]) >
0820: 20 30 20 74 68 65 6e 0a 20 20 20 20 20 20 72 65 0 then. re
0830: 76 65 72 73 65 64 20 3a 3d 20 73 70 6c 69 74 74 versed := splitt
0840: 65 64 5b 78 5d 20 7c 7c 20 72 65 76 65 72 73 65 ed[x] || reverse
0850: 64 3b 0a 20 20 20 20 65 6e 64 20 69 66 3b 0a 20 d;. end if;.
0860: 20 20 20 78 20 3a 3d 20 78 20 2b 20 31 3b 0a 20 x := x + 1;.
0870: 20 65 6e 64 20 6c 6f 6f 70 3b 0a 20 20 78 20 3a end loop;. x :
0880: 3d 20 31 3b 0a 20 20 6c 6f 6f 70 0a 20 20 20 20 = 1;. loop.
0890: 65 78 69 74 20 77 68 65 6e 20 72 65 76 65 72 73 exit when revers
08a0: 65 64 5b 78 5d 20 69 73 20 6e 75 6c 6c 3b 0a 20 ed[x] is null;.
08b0: 20 20 20 72 65 73 75 6c 74 5b 78 5d 20 3a 3d 20 result[x] :=
08c0: 72 65 76 65 72 73 65 64 5b 78 5d 20 7c 7c 20 27 reversed[x] || '
08d0: 3a 27 20 7c 7c 20 78 3b 0a 20 20 20 20 78 20 3a :' || x;. x :
08e0: 3d 20 78 20 2b 20 31 3b 0a 20 20 65 6e 64 20 6c = x + 1;. end l
08f0: 6f 6f 70 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 oop;. return re
0900: 73 75 6c 74 3b 0a 65 6e 64 3b 0a 24 5f 24 3b 0a sult;.end;.$_$;.
0910: 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 72 75 .CREATE TABLE ru
0920: 6c 65 73 20 28 0a 20 20 20 20 6e 65 74 6d 61 73 les (. netmas
0930: 6b 20 63 69 64 72 20 4e 4f 54 20 4e 55 4c 4c 2c k cidr NOT NULL,
0940: 0a 20 20 20 20 72 65 64 69 72 65 63 74 5f 75 72 . redirect_ur
0950: 6c 20 74 65 78 74 20 44 45 46 41 55 4c 54 20 27 l text DEFAULT '
0960: 61 62 6f 75 74 3a 3a 62 6c 61 6e 6b 27 3a 3a 74 about::blank'::t
0970: 65 78 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 20 20 ext NOT NULL,.
0980: 20 20 66 72 6f 6d 5f 77 65 65 6b 64 61 79 20 73 from_weekday s
0990: 6d 61 6c 6c 69 6e 74 20 44 45 46 41 55 4c 54 20 mallint DEFAULT
09a0: 30 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 20 20 20 20 0 NOT NULL,.
09b0: 74 6f 5f 77 65 65 6b 64 61 79 20 73 6d 61 6c 6c to_weekday small
09c0: 69 6e 74 20 44 45 46 41 55 4c 54 20 36 20 4e 4f int DEFAULT 6 NO
09d0: 54 20 4e 55 4c 4c 2c 0a 20 20 20 20 66 72 6f 6d T NULL,. from
09e0: 5f 74 69 6d 65 20 74 69 6d 65 20 77 69 74 68 6f _time time witho
09f0: 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 44 45 46 ut time zone DEF
0a00: 41 55 4c 54 20 27 30 30 3a 30 30 3a 30 30 27 3a AULT '00:00:00':
0a10: 3a 74 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 :time without ti
0a20: 6d 65 20 7a 6f 6e 65 20 4e 4f 54 20 4e 55 4c 4c me zone NOT NULL
0a30: 2c 0a 20 20 20 20 74 6f 5f 74 69 6d 65 20 74 69 ,. to_time ti
0a40: 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 me without time
0a50: 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 27 32 33 zone DEFAULT '23
0a60: 3a 35 39 3a 35 39 27 3a 3a 74 69 6d 65 20 77 69 :59:59'::time wi
0a70: 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 thout time zone
0a80: 4e 4f 54 20 4e 55 4c 4c 2c 0a 20 20 20 20 69 64 NOT NULL,. id
0a90: 5f 74 61 67 20 73 6d 61 6c 6c 69 6e 74 20 4e 4f _tag smallint NO
0aa0: 54 20 4e 55 4c 4c 0a 29 3b 0a 0a 0a 43 52 45 41 T NULL.);...CREA
0ab0: 54 45 20 54 41 42 4c 45 20 73 69 74 65 20 28 0a TE TABLE site (.
0ac0: 20 20 20 20 69 64 5f 73 69 74 65 20 73 65 72 69 id_site seri
0ad0: 61 6c 2c 0a 20 20 20 20 73 69 74 65 20 74 65 78 al,. site tex
0ae0: 74 5b 5d 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a t[] NOT NULL.);.
0af0: 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 61 .CREATE TABLE ta
0b00: 67 20 28 0a 20 20 20 20 69 64 5f 74 61 67 20 73 g (. id_tag s
0b10: 65 72 69 61 6c 2c 0a 20 20 20 20 74 61 67 20 74 erial,. tag t
0b20: 65 78 74 5b 5d 20 4e 4f 54 20 4e 55 4c 4c 0a 29 ext[] NOT NULL.)
0b30: 3b 0a 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 ;..CREATE TABLE
0b40: 75 72 6c 73 20 28 0a 20 20 20 20 64 61 74 65 5f urls (. date_
0b50: 61 64 64 65 64 20 74 69 6d 65 73 74 61 6d 70 20 added timestamp
0b60: 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e without time zon
0b70: 65 20 44 45 46 41 55 4c 54 20 28 27 6e 6f 77 27 e DEFAULT ('now'
0b80: 3a 3a 74 65 78 74 29 3a 3a 74 69 6d 65 73 74 61 ::text)::timesta
0b90: 6d 70 28 30 29 20 77 69 74 68 6f 75 74 20 74 69 mp(0) without ti
0ba0: 6d 65 20 7a 6f 6e 65 20 4e 4f 54 20 4e 55 4c 4c me zone NOT NULL
0bb0: 2c 0a 20 20 20 20 69 64 5f 73 69 74 65 20 73 6d ,. id_site sm
0bc0: 61 6c 6c 69 6e 74 20 4e 4f 54 20 4e 55 4c 4c 2c allint NOT NULL,
0bd0: 0a 20 20 20 20 69 64 5f 74 61 67 20 73 6d 61 6c . id_tag smal
0be0: 6c 69 6e 74 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b lint NOT NULL.);
0bf0: 0a 0a 43 52 45 41 54 45 20 56 49 45 57 20 73 69 ..CREATE VIEW si
0c00: 74 65 5f 72 75 6c 65 20 41 53 0a 20 20 20 20 53 te_rule AS. S
0c10: 45 4c 45 43 54 20 61 2e 72 65 64 69 72 65 63 74 ELECT a.redirect
0c20: 5f 75 72 6c 2c 20 61 2e 6e 65 74 6d 61 73 6b 2c _url, a.netmask,
0c30: 20 62 2e 73 69 74 65 20 46 52 4f 4d 20 28 28 53 b.site FROM ((S
0c40: 45 4c 45 43 54 20 72 75 6c 65 73 2e 72 65 64 69 ELECT rules.redi
0c50: 72 65 63 74 5f 75 72 6c 2c 20 74 61 67 2e 74 61 rect_url, tag.ta
0c60: 67 20 41 53 20 72 75 6c 65 5f 74 61 67 2c 20 72 g AS rule_tag, r
0c70: 75 6c 65 73 2e 6e 65 74 6d 61 73 6b 20 46 52 4f ules.netmask FRO
0c80: 4d 20 28 72 75 6c 65 73 20 4e 41 54 55 52 41 4c M (rules NATURAL
0c90: 20 4a 4f 49 4e 20 74 61 67 29 20 57 48 45 52 45 JOIN tag) WHERE
0ca0: 20 28 28 28 28 28 27 6e 6f 77 27 3a 3a 74 65 78 ((((('now'::tex
0cb0: 74 29 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 74 t)::time without
0cc0: 20 74 69 6d 65 20 7a 6f 6e 65 20 3e 3d 20 72 75 time zone >= ru
0cd0: 6c 65 73 2e 66 72 6f 6d 5f 74 69 6d 65 29 20 41 les.from_time) A
0ce0: 4e 44 20 28 28 27 6e 6f 77 27 3a 3a 74 65 78 74 ND (('now'::text
0cf0: 29 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 74 20 )::time without
0d00: 74 69 6d 65 20 7a 6f 6e 65 20 3c 3d 20 72 75 6c time zone <= rul
0d10: 65 73 2e 74 6f 5f 74 69 6d 65 29 29 20 41 4e 44 es.to_time)) AND
0d20: 20 28 64 61 74 65 5f 70 61 72 74 28 27 64 6f 77 (date_part('dow
0d30: 27 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 29 29 20 '::text, now())
0d40: 3e 3d 20 28 72 75 6c 65 73 2e 66 72 6f 6d 5f 77 >= (rules.from_w
0d50: 65 65 6b 64 61 79 29 3a 3a 64 6f 75 62 6c 65 20 eekday)::double
0d60: 70 72 65 63 69 73 69 6f 6e 29 29 20 41 4e 44 20 precision)) AND
0d70: 28 64 61 74 65 5f 70 61 72 74 28 27 64 6f 77 27 (date_part('dow'
0d80: 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 29 29 20 3c ::text, now()) <
0d90: 3d 20 28 72 75 6c 65 73 2e 74 6f 5f 77 65 65 6b = (rules.to_week
0da0: 64 61 79 29 3a 3a 64 6f 75 62 6c 65 20 70 72 65 day)::double pre
0db0: 63 69 73 69 6f 6e 29 29 29 20 61 20 4a 4f 49 4e cision))) a JOIN
0dc0: 20 28 53 45 4c 45 43 54 20 73 69 74 65 2e 73 69 (SELECT site.si
0dd0: 74 65 2c 20 74 61 67 2e 74 61 67 20 41 53 20 75 te, tag.tag AS u
0de0: 72 6c 5f 74 61 67 20 46 52 4f 4d 20 28 28 75 72 rl_tag FROM ((ur
0df0: 6c 73 20 4e 41 54 55 52 41 4c 20 4a 4f 49 4e 20 ls NATURAL JOIN
0e00: 74 61 67 29 20 4e 41 54 55 52 41 4c 20 4a 4f 49 tag) NATURAL JOI
0e10: 4e 20 73 69 74 65 29 29 20 62 20 4f 4e 20 28 28 N site)) b ON ((
0e20: 62 2e 75 72 6c 5f 74 61 67 20 26 26 20 61 2e 72 b.url_tag && a.r
0e30: 75 6c 65 5f 74 61 67 29 29 29 3b 0a 0a 41 4c 54 ule_tag)));..ALT
0e40: 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 72 75 ER TABLE ONLY ru
0e50: 6c 65 73 0a 20 20 20 20 41 44 44 20 43 4f 4e 53 les. ADD CONS
0e60: 54 52 41 49 4e 54 20 72 75 6c 65 73 5f 70 6b 65 TRAINT rules_pke
0e70: 79 20 50 52 49 4d 41 52 59 20 4b 45 59 20 28 6e y PRIMARY KEY (n
0e80: 65 74 6d 61 73 6b 29 3b 0a 0a 41 4c 54 45 52 20 etmask);..ALTER
0e90: 54 41 42 4c 45 20 4f 4e 4c 59 20 73 69 74 65 0a TABLE ONLY site.
0ea0: 20 20 20 20 41 44 44 20 43 4f 4e 53 54 52 41 49 ADD CONSTRAI
0eb0: 4e 54 20 73 69 74 65 5f 69 64 20 50 52 49 4d 41 NT site_id PRIMA
0ec0: 52 59 20 4b 45 59 20 28 69 64 5f 73 69 74 65 29 RY KEY (id_site)
0ed0: 3b 0a 0a 41 4c 54 45 52 20 54 41 42 4c 45 20 4f ;..ALTER TABLE O
0ee0: 4e 4c 59 20 74 61 67 0a 20 20 20 20 41 44 44 20 NLY tag. ADD
0ef0: 43 4f 4e 53 54 52 41 49 4e 54 20 74 61 67 5f 69 CONSTRAINT tag_i
0f00: 64 20 50 52 49 4d 41 52 59 20 4b 45 59 20 28 69 d PRIMARY KEY (i
0f10: 64 5f 74 61 67 29 3b 0a 0a 41 4c 54 45 52 20 54 d_tag);..ALTER T
0f20: 41 42 4c 45 20 4f 4e 4c 59 20 75 72 6c 73 0a 20 ABLE ONLY urls.
0f30: 20 20 20 41 44 44 20 43 4f 4e 53 54 52 41 49 4e ADD CONSTRAIN
0f40: 54 20 75 72 6c 73 5f 70 6b 65 79 20 50 52 49 4d T urls_pkey PRIM
0f50: 41 52 59 20 4b 45 59 20 28 64 61 74 65 5f 61 64 ARY KEY (date_ad
0f60: 64 65 64 29 3b 0a 0a 43 52 45 41 54 45 20 55 4e ded);..CREATE UN
0f70: 49 51 55 45 20 49 4e 44 45 58 20 73 69 74 65 5f IQUE INDEX site_
0f80: 73 20 4f 4e 20 73 69 74 65 20 55 53 49 4e 47 20 s ON site USING
0f90: 62 74 72 65 65 20 28 73 69 74 65 29 3b 0a 0a 43 btree (site);..C
0fa0: 52 45 41 54 45 20 49 4e 44 45 58 20 73 69 74 65 REATE INDEX site
0fb0: 5f 73 67 20 4f 4e 20 73 69 74 65 20 55 53 49 4e _sg ON site USIN
0fc0: 47 20 67 69 6e 20 28 73 69 74 65 29 3b 0a 0a 43 G gin (site);..C
0fd0: 52 45 41 54 45 20 49 4e 44 45 58 20 74 61 67 5f REATE INDEX tag_
0fe0: 67 20 4f 4e 20 74 61 67 20 55 53 49 4e 47 20 67 g ON tag USING g
0ff0: 69 6e 20 28 74 61 67 29 3b 0a 0a 43 52 45 41 54 in (tag);..CREAT
1000: 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 74 E UNIQUE INDEX t
1010: 61 67 5f 73 20 4f 4e 20 74 61 67 20 55 53 49 4e ag_s ON tag USIN
1020: 47 20 62 74 72 65 65 20 28 74 61 67 29 3b 0a 0a G btree (tag);..
1030: 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e CREATE UNIQUE IN
1040: 44 45 58 20 75 72 6c 73 5f 69 64 5f 73 69 74 65 DEX urls_id_site
1050: 20 4f 4e 20 75 72 6c 73 20 55 53 49 4e 47 20 62 ON urls USING b
1060: 74 72 65 65 20 28 69 64 5f 73 69 74 65 29 3b 0a tree (id_site);.