Artifact
8d08e9c8acfd3c066f82ea55a4eb19c819d5ba355961c5993baeb037072cae8e:
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 76 61 72 63 68 61 72 my_site varchar
00a0: 5b 5d 3b 0a 20 6d 79 5f 73 69 74 65 5f 69 64 20 [];. my_site_id
00b0: 73 6d 61 6c 6c 69 6e 74 3b 0a 20 6d 79 5f 74 61 smallint;. my_ta
00c0: 67 20 74 65 78 74 5b 5d 3b 0a 20 6d 79 5f 74 61 g text[];. my_ta
00d0: 67 5f 69 64 20 73 6d 61 6c 6c 69 6e 74 3b 0a 62 g_id smallint;.b
00e0: 65 67 69 6e 0a 20 6d 79 5f 73 69 74 65 20 3a 3d egin. my_site :=
00f0: 20 74 72 69 70 64 6f 6d 61 69 6e 28 64 6f 6d 61 tripdomain(doma
0100: 69 6e 29 3b 0a 0a 20 73 65 6c 65 63 74 20 69 64 in);.. select id
0110: 5f 73 69 74 65 20 66 72 6f 6d 20 73 69 74 65 20 _site from site
0120: 77 68 65 72 65 20 6d 79 5f 73 69 74 65 20 3d 20 where my_site =
0130: 73 69 74 65 20 69 6e 74 6f 20 6d 79 5f 73 69 74 site into my_sit
0140: 65 5f 69 64 3b 0a 20 69 66 20 6e 6f 74 20 66 6f e_id;. if not fo
0150: 75 6e 64 20 74 68 65 6e 0a 20 20 69 6e 73 65 72 und then. inser
0160: 74 20 69 6e 74 6f 20 73 69 74 65 20 28 73 69 74 t into site (sit
0170: 65 29 20 76 61 6c 75 65 73 20 28 6d 79 5f 73 69 e) values (my_si
0180: 74 65 29 3b 0a 20 20 73 65 6c 65 63 74 20 69 64 te);. select id
0190: 5f 73 69 74 65 20 66 72 6f 6d 20 73 69 74 65 20 _site from site
01a0: 77 68 65 72 65 20 6d 79 5f 73 69 74 65 20 3d 20 where my_site =
01b0: 73 69 74 65 20 69 6e 74 6f 20 6d 79 5f 73 69 74 site into my_sit
01c0: 65 5f 69 64 3b 0a 20 65 6e 64 20 69 66 3b 0a 0a e_id;. end if;..
01d0: 20 73 65 6c 65 63 74 20 74 61 67 20 66 72 6f 6d select tag from
01e0: 20 75 72 6c 73 20 6e 61 74 75 72 61 6c 20 6a 6f urls natural jo
01f0: 69 6e 20 74 61 67 20 77 68 65 72 65 20 69 64 5f in tag where id_
0200: 73 69 74 65 20 3d 20 6d 79 5f 73 69 74 65 5f 69 site = my_site_i
0210: 64 20 69 6e 74 6f 20 6d 79 5f 74 61 67 3b 0a 20 d into my_tag;.
0220: 69 66 20 6e 6f 74 20 66 6f 75 6e 64 20 74 68 65 if not found the
0230: 6e 0a 20 20 6d 79 5f 74 61 67 20 3a 3d 20 6e 65 n. my_tag := ne
0240: 77 5f 74 61 67 3b 0a 20 65 6c 73 65 0a 20 20 73 w_tag;. else. s
0250: 65 6c 65 63 74 20 61 72 72 61 79 5f 61 67 67 28 elect array_agg(
0260: 74 61 67 29 20 66 72 6f 6d 20 28 73 65 6c 65 63 tag) from (selec
0270: 74 20 64 69 73 74 69 6e 63 74 20 75 6e 6e 65 73 t distinct unnes
0280: 74 28 6d 79 5f 74 61 67 20 7c 7c 20 61 72 72 61 t(my_tag || arra
0290: 79 5b 6e 65 77 5f 74 61 67 5d 29 20 61 73 20 74 y[new_tag]) as t
02a0: 61 67 20 6f 72 64 65 72 20 62 79 20 74 61 67 20 ag order by tag
02b0: 61 73 63 29 20 61 20 69 6e 74 6f 20 0a 6d 79 5f asc) a into .my_
02c0: 74 61 67 3b 0a 20 20 64 65 6c 65 74 65 20 66 72 tag;. delete fr
02d0: 6f 6d 20 75 72 6c 73 20 77 68 65 72 65 20 69 64 om urls where id
02e0: 5f 73 69 74 65 20 3d 20 6d 79 5f 73 69 74 65 5f _site = my_site_
02f0: 69 64 3b 0a 20 65 6e 64 20 69 66 3b 0a 0a 20 73 id;. end if;.. s
0300: 65 6c 65 63 74 20 69 64 5f 74 61 67 20 66 72 6f elect id_tag fro
0310: 6d 20 74 61 67 20 77 68 65 72 65 20 6d 79 5f 74 m tag where my_t
0320: 61 67 20 3d 20 74 61 67 20 69 6e 74 6f 20 6d 79 ag = tag into my
0330: 5f 74 61 67 5f 69 64 3b 0a 20 69 66 20 6e 6f 74 _tag_id;. if not
0340: 20 66 6f 75 6e 64 20 74 68 65 6e 0a 20 20 69 6e found then. in
0350: 73 65 72 74 20 69 6e 74 6f 20 74 61 67 20 28 74 sert into tag (t
0360: 61 67 29 20 76 61 6c 75 65 73 28 6d 79 5f 74 61 ag) values(my_ta
0370: 67 29 3b 0a 20 20 73 65 6c 65 63 74 20 69 64 5f g);. select id_
0380: 74 61 67 20 66 72 6f 6d 20 74 61 67 20 77 68 65 tag from tag whe
0390: 72 65 20 6d 79 5f 74 61 67 20 3d 20 74 61 67 20 re my_tag = tag
03a0: 69 6e 74 6f 20 6d 79 5f 74 61 67 5f 69 64 3b 0a into my_tag_id;.
03b0: 20 65 6e 64 20 69 66 3b 0a 0a 20 69 6e 73 65 72 end if;.. inser
03c0: 74 20 69 6e 74 6f 20 75 72 6c 73 20 28 69 64 5f t into urls (id_
03d0: 73 69 74 65 2c 20 69 64 5f 74 61 67 29 20 76 61 site, id_tag) va
03e0: 6c 75 65 73 20 28 6d 79 5f 73 69 74 65 5f 69 64 lues (my_site_id
03f0: 2c 20 6d 79 5f 74 61 67 5f 69 64 29 3b 0a 65 6e , my_tag_id);.en
0400: 64 3b 24 24 3b 0a 0a 43 52 45 41 54 45 20 46 55 d;$$;..CREATE FU
0410: 4e 43 54 49 4f 4e 20 6d 61 72 6b 28 6d 79 5f 73 NCTION mark(my_s
0420: 69 74 65 5f 69 64 20 73 6d 61 6c 6c 69 6e 74 2c ite_id smallint,
0430: 20 6e 65 77 5f 74 61 67 20 74 65 78 74 29 20 52 new_tag text) R
0440: 45 54 55 52 4e 53 20 76 6f 69 64 0a 20 20 20 20 ETURNS void.
0450: 4c 41 4e 47 55 41 47 45 20 70 6c 70 67 73 71 6c LANGUAGE plpgsql
0460: 20 53 54 52 49 43 54 0a 20 20 20 20 41 53 20 24 STRICT. AS $
0470: 24 0a 64 65 63 6c 61 72 65 0a 20 6d 79 5f 74 61 $.declare. my_ta
0480: 67 20 74 65 78 74 5b 5d 3b 0a 20 6d 79 5f 74 61 g text[];. my_ta
0490: 67 5f 69 64 20 73 6d 61 6c 6c 69 6e 74 3b 0a 62 g_id smallint;.b
04a0: 65 67 69 6e 0a 0a 20 73 65 6c 65 63 74 20 74 61 egin.. select ta
04b0: 67 20 66 72 6f 6d 20 75 72 6c 73 20 6e 61 74 75 g from urls natu
04c0: 72 61 6c 20 6a 6f 69 6e 20 74 61 67 20 77 68 65 ral join tag whe
04d0: 72 65 20 69 64 5f 73 69 74 65 20 3d 20 6d 79 5f re id_site = my_
04e0: 73 69 74 65 5f 69 64 20 69 6e 74 6f 20 6d 79 5f site_id into my_
04f0: 74 61 67 3b 0a 20 69 66 20 6e 6f 74 20 66 6f 75 tag;. if not fou
0500: 6e 64 20 74 68 65 6e 0a 20 20 6d 79 5f 74 61 67 nd then. my_tag
0510: 20 3a 3d 20 6e 65 77 5f 74 61 67 3b 0a 20 65 6c := new_tag;. el
0520: 73 65 0a 20 20 73 65 6c 65 63 74 20 61 72 72 61 se. select arra
0530: 79 5f 61 67 67 28 74 61 67 29 20 66 72 6f 6d 20 y_agg(tag) from
0540: 28 73 65 6c 65 63 74 20 64 69 73 74 69 6e 63 74 (select distinct
0550: 20 75 6e 6e 65 73 74 28 6d 79 5f 74 61 67 20 7c unnest(my_tag |
0560: 7c 20 61 72 72 61 79 5b 6e 65 77 5f 74 61 67 5d | array[new_tag]
0570: 29 20 61 73 20 74 61 67 20 6f 72 64 65 72 20 62 ) as tag order b
0580: 79 20 74 61 67 20 61 73 63 29 20 61 20 69 6e 74 y tag asc) a int
0590: 6f 20 0a 6d 79 5f 74 61 67 3b 0a 20 20 64 65 6c o .my_tag;. del
05a0: 65 74 65 20 66 72 6f 6d 20 75 72 6c 73 20 77 68 ete from urls wh
05b0: 65 72 65 20 69 64 5f 73 69 74 65 20 3d 20 6d 79 ere id_site = my
05c0: 5f 73 69 74 65 5f 69 64 3b 0a 20 65 6e 64 20 69 _site_id;. end i
05d0: 66 3b 0a 0a 20 73 65 6c 65 63 74 20 69 64 5f 74 f;.. select id_t
05e0: 61 67 20 66 72 6f 6d 20 74 61 67 20 77 68 65 72 ag from tag wher
05f0: 65 20 6d 79 5f 74 61 67 20 3d 20 74 61 67 20 69 e my_tag = tag i
0600: 6e 74 6f 20 6d 79 5f 74 61 67 5f 69 64 3b 0a 20 nto my_tag_id;.
0610: 69 66 20 6e 6f 74 20 66 6f 75 6e 64 20 74 68 65 if not found the
0620: 6e 0a 20 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 n. insert into
0630: 74 61 67 20 28 74 61 67 29 20 76 61 6c 75 65 73 tag (tag) values
0640: 28 6d 79 5f 74 61 67 29 3b 0a 20 20 73 65 6c 65 (my_tag);. sele
0650: 63 74 20 69 64 5f 74 61 67 20 66 72 6f 6d 20 74 ct id_tag from t
0660: 61 67 20 77 68 65 72 65 20 6d 79 5f 74 61 67 20 ag where my_tag
0670: 3d 20 74 61 67 20 69 6e 74 6f 20 6d 79 5f 74 61 = tag into my_ta
0680: 67 5f 69 64 3b 0a 20 65 6e 64 20 69 66 3b 0a 0a g_id;. end if;..
0690: 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 75 72 6c insert into url
06a0: 73 20 28 69 64 5f 73 69 74 65 2c 20 69 64 5f 74 s (id_site, id_t
06b0: 61 67 29 20 76 61 6c 75 65 73 20 28 6d 79 5f 73 ag) values (my_s
06c0: 69 74 65 5f 69 64 2c 20 6d 79 5f 74 61 67 5f 69 ite_id, my_tag_i
06d0: 64 29 3b 0a 65 6e 64 3b 24 24 3b 0a 0a 43 52 45 d);.end;$$;..CRE
06e0: 41 54 45 20 46 55 4e 43 54 49 4f 4e 20 74 72 69 ATE FUNCTION tri
06f0: 70 64 6f 6d 61 69 6e 28 75 72 6c 20 63 68 61 72 pdomain(url char
0700: 61 63 74 65 72 20 76 61 72 79 69 6e 67 29 20 52 acter varying) R
0710: 45 54 55 52 4e 53 20 63 68 61 72 61 63 74 65 72 ETURNS character
0720: 20 76 61 72 79 69 6e 67 5b 5d 0a 20 20 20 20 4c varying[]. L
0730: 41 4e 47 55 41 47 45 20 70 6c 70 67 73 71 6c 20 ANGUAGE plpgsql
0740: 49 4d 4d 55 54 41 42 4c 45 20 53 54 52 49 43 54 IMMUTABLE STRICT
0750: 0a 20 20 20 20 41 53 20 24 5f 24 0a 64 65 63 6c . AS $_$.decl
0760: 61 72 65 0a 20 20 72 65 73 75 6c 74 20 76 61 72 are. result var
0770: 63 68 61 72 5b 5d 3b 0a 20 20 73 70 6c 69 74 74 char[];. splitt
0780: 65 64 20 76 61 72 63 68 61 72 5b 5d 3b 0a 20 20 ed varchar[];.
0790: 72 65 76 65 72 73 65 64 20 76 61 72 63 68 61 72 reversed varchar
07a0: 5b 5d 3b 0a 20 20 78 20 69 6e 74 65 67 65 72 3b [];. x integer;
07b0: 0a 62 65 67 69 6e 0a 20 20 78 20 3a 3d 20 31 3b .begin. x := 1;
07c0: 0a 20 20 73 70 6c 69 74 74 65 64 20 3a 3d 20 73 . splitted := s
07d0: 74 72 69 6e 67 5f 74 6f 5f 61 72 72 61 79 28 24 tring_to_array($
07e0: 31 2c 20 27 2e 27 29 3b 0a 20 20 6c 6f 6f 70 0a 1, '.');. loop.
07f0: 20 20 20 20 65 78 69 74 20 77 68 65 6e 20 73 70 exit when sp
0800: 6c 69 74 74 65 64 5b 78 5d 20 69 73 20 6e 75 6c litted[x] is nul
0810: 6c 3b 0a 20 20 20 20 69 66 20 6c 65 6e 67 74 68 l;. if length
0820: 28 73 70 6c 69 74 74 65 64 5b 78 5d 29 20 3e 20 (splitted[x]) >
0830: 30 20 74 68 65 6e 0a 20 20 20 20 20 20 72 65 76 0 then. rev
0840: 65 72 73 65 64 20 3a 3d 20 73 70 6c 69 74 74 65 ersed := splitte
0850: 64 5b 78 5d 20 7c 7c 20 72 65 76 65 72 73 65 64 d[x] || reversed
0860: 3b 0a 20 20 20 20 65 6e 64 20 69 66 3b 0a 20 20 ;. end if;.
0870: 20 20 78 20 3a 3d 20 78 20 2b 20 31 3b 0a 20 20 x := x + 1;.
0880: 65 6e 64 20 6c 6f 6f 70 3b 0a 20 20 78 20 3a 3d end loop;. x :=
0890: 20 31 3b 0a 20 20 6c 6f 6f 70 0a 20 20 20 20 65 1;. loop. e
08a0: 78 69 74 20 77 68 65 6e 20 72 65 76 65 72 73 65 xit when reverse
08b0: 64 5b 78 5d 20 69 73 20 6e 75 6c 6c 3b 0a 20 20 d[x] is null;.
08c0: 20 20 72 65 73 75 6c 74 5b 78 5d 20 3a 3d 20 72 result[x] := r
08d0: 65 76 65 72 73 65 64 5b 78 5d 20 7c 7c 20 27 3a eversed[x] || ':
08e0: 27 20 7c 7c 20 78 3b 0a 20 20 20 20 78 20 3a 3d ' || x;. x :=
08f0: 20 78 20 2b 20 31 3b 0a 20 20 65 6e 64 20 6c 6f x + 1;. end lo
0900: 6f 70 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 op;. return res
0910: 75 6c 74 3b 0a 65 6e 64 3b 0a 24 5f 24 3b 0a 0a ult;.end;.$_$;..
0920: 43 52 45 41 54 45 20 54 41 42 4c 45 20 72 75 6c CREATE TABLE rul
0930: 65 73 20 28 0a 20 20 20 20 6e 65 74 6d 61 73 6b es (. netmask
0940: 20 63 69 64 72 20 4e 4f 54 20 4e 55 4c 4c 2c 0a cidr NOT NULL,.
0950: 20 20 20 20 72 65 64 69 72 65 63 74 5f 75 72 6c redirect_url
0960: 20 74 65 78 74 20 44 45 46 41 55 4c 54 20 27 61 text DEFAULT 'a
0970: 62 6f 75 74 3a 3a 62 6c 61 6e 6b 27 3a 3a 74 65 bout::blank'::te
0980: 78 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 20 20 20 xt NOT NULL,.
0990: 20 66 72 6f 6d 5f 77 65 65 6b 64 61 79 20 73 6d from_weekday sm
09a0: 61 6c 6c 69 6e 74 20 44 45 46 41 55 4c 54 20 30 allint DEFAULT 0
09b0: 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 20 20 20 20 74 NOT NULL,. t
09c0: 6f 5f 77 65 65 6b 64 61 79 20 73 6d 61 6c 6c 69 o_weekday smalli
09d0: 6e 74 20 44 45 46 41 55 4c 54 20 36 20 4e 4f 54 nt DEFAULT 6 NOT
09e0: 20 4e 55 4c 4c 2c 0a 20 20 20 20 66 72 6f 6d 5f NULL,. from_
09f0: 74 69 6d 65 20 74 69 6d 65 20 77 69 74 68 6f 75 time time withou
0a00: 74 20 74 69 6d 65 20 7a 6f 6e 65 20 44 45 46 41 t time zone DEFA
0a10: 55 4c 54 20 27 30 30 3a 30 30 3a 30 30 27 3a 3a ULT '00:00:00'::
0a20: 74 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d time without tim
0a30: 65 20 7a 6f 6e 65 20 4e 4f 54 20 4e 55 4c 4c 2c e zone NOT NULL,
0a40: 0a 20 20 20 20 74 6f 5f 74 69 6d 65 20 74 69 6d . to_time tim
0a50: 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a e without time z
0a60: 6f 6e 65 20 44 45 46 41 55 4c 54 20 27 32 33 3a one DEFAULT '23:
0a70: 35 39 3a 35 39 27 3a 3a 74 69 6d 65 20 77 69 74 59:59'::time wit
0a80: 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 4e hout time zone N
0a90: 4f 54 20 4e 55 4c 4c 2c 0a 20 20 20 20 69 64 5f OT NULL,. id_
0aa0: 74 61 67 20 73 6d 61 6c 6c 69 6e 74 20 4e 4f 54 tag smallint NOT
0ab0: 20 4e 55 4c 4c 0a 29 3b 0a 0a 0a 43 52 45 41 54 NULL.);...CREAT
0ac0: 45 20 54 41 42 4c 45 20 73 69 74 65 20 28 0a 20 E TABLE site (.
0ad0: 20 20 20 69 64 5f 73 69 74 65 20 73 65 72 69 61 id_site seria
0ae0: 6c 2c 0a 20 20 20 20 73 69 74 65 20 74 65 78 74 l,. site text
0af0: 5b 5d 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a 0a [] NOT NULL.);..
0b00: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 61 67 CREATE TABLE tag
0b10: 20 28 0a 20 20 20 20 69 64 5f 74 61 67 20 73 65 (. id_tag se
0b20: 72 69 61 6c 2c 0a 20 20 20 20 74 61 67 20 74 65 rial,. tag te
0b30: 78 74 5b 5d 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b xt[] NOT NULL.);
0b40: 0a 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 75 ..CREATE TABLE u
0b50: 72 6c 73 20 28 0a 20 20 20 20 64 61 74 65 5f 61 rls (. date_a
0b60: 64 64 65 64 20 74 69 6d 65 73 74 61 6d 70 20 77 dded timestamp w
0b70: 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 ithout time zone
0b80: 20 44 45 46 41 55 4c 54 20 28 27 6e 6f 77 27 3a DEFAULT ('now':
0b90: 3a 74 65 78 74 29 3a 3a 74 69 6d 65 73 74 61 6d :text)::timestam
0ba0: 70 28 30 29 20 77 69 74 68 6f 75 74 20 74 69 6d p(0) without tim
0bb0: 65 20 7a 6f 6e 65 20 4e 4f 54 20 4e 55 4c 4c 2c e zone NOT NULL,
0bc0: 0a 20 20 20 20 69 64 5f 73 69 74 65 20 73 6d 61 . id_site sma
0bd0: 6c 6c 69 6e 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a llint NOT NULL,.
0be0: 20 20 20 20 69 64 5f 74 61 67 20 73 6d 61 6c 6c id_tag small
0bf0: 69 6e 74 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a int NOT NULL.);.
0c00: 0a 43 52 45 41 54 45 20 56 49 45 57 20 73 69 74 .CREATE VIEW sit
0c10: 65 5f 72 75 6c 65 20 41 53 0a 20 20 20 20 53 45 e_rule AS. SE
0c20: 4c 45 43 54 20 61 2e 72 65 64 69 72 65 63 74 5f LECT a.redirect_
0c30: 75 72 6c 2c 20 61 2e 6e 65 74 6d 61 73 6b 2c 20 url, a.netmask,
0c40: 62 2e 73 69 74 65 20 46 52 4f 4d 20 28 28 53 45 b.site FROM ((SE
0c50: 4c 45 43 54 20 72 75 6c 65 73 2e 72 65 64 69 72 LECT rules.redir
0c60: 65 63 74 5f 75 72 6c 2c 20 74 61 67 2e 74 61 67 ect_url, tag.tag
0c70: 20 41 53 20 72 75 6c 65 5f 74 61 67 2c 20 72 75 AS rule_tag, ru
0c80: 6c 65 73 2e 6e 65 74 6d 61 73 6b 20 46 52 4f 4d les.netmask FROM
0c90: 20 28 72 75 6c 65 73 20 4e 41 54 55 52 41 4c 20 (rules NATURAL
0ca0: 4a 4f 49 4e 20 74 61 67 29 20 57 48 45 52 45 20 JOIN tag) WHERE
0cb0: 28 28 28 28 28 27 6e 6f 77 27 3a 3a 74 65 78 74 ((((('now'::text
0cc0: 29 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 74 20 )::time without
0cd0: 74 69 6d 65 20 7a 6f 6e 65 20 3e 3d 20 72 75 6c time zone >= rul
0ce0: 65 73 2e 66 72 6f 6d 5f 74 69 6d 65 29 20 41 4e es.from_time) AN
0cf0: 44 20 28 28 27 6e 6f 77 27 3a 3a 74 65 78 74 29 D (('now'::text)
0d00: 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 74 20 74 ::time without t
0d10: 69 6d 65 20 7a 6f 6e 65 20 3c 3d 20 72 75 6c 65 ime zone <= rule
0d20: 73 2e 74 6f 5f 74 69 6d 65 29 29 20 41 4e 44 20 s.to_time)) AND
0d30: 28 64 61 74 65 5f 70 61 72 74 28 27 64 6f 77 27 (date_part('dow'
0d40: 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 29 29 20 3e ::text, now()) >
0d50: 3d 20 28 72 75 6c 65 73 2e 66 72 6f 6d 5f 77 65 = (rules.from_we
0d60: 65 6b 64 61 79 29 3a 3a 64 6f 75 62 6c 65 20 70 ekday)::double p
0d70: 72 65 63 69 73 69 6f 6e 29 29 20 41 4e 44 20 28 recision)) AND (
0d80: 64 61 74 65 5f 70 61 72 74 28 27 64 6f 77 27 3a date_part('dow':
0d90: 3a 74 65 78 74 2c 20 6e 6f 77 28 29 29 20 3c 3d :text, now()) <=
0da0: 20 28 72 75 6c 65 73 2e 74 6f 5f 77 65 65 6b 64 (rules.to_weekd
0db0: 61 79 29 3a 3a 64 6f 75 62 6c 65 20 70 72 65 63 ay)::double prec
0dc0: 69 73 69 6f 6e 29 29 29 20 61 20 4a 4f 49 4e 20 ision))) a JOIN
0dd0: 28 53 45 4c 45 43 54 20 73 69 74 65 2e 73 69 74 (SELECT site.sit
0de0: 65 2c 20 74 61 67 2e 74 61 67 20 41 53 20 75 72 e, tag.tag AS ur
0df0: 6c 5f 74 61 67 20 46 52 4f 4d 20 28 28 75 72 6c l_tag FROM ((url
0e00: 73 20 4e 41 54 55 52 41 4c 20 4a 4f 49 4e 20 74 s NATURAL JOIN t
0e10: 61 67 29 20 4e 41 54 55 52 41 4c 20 4a 4f 49 4e ag) NATURAL JOIN
0e20: 20 73 69 74 65 29 29 20 62 20 4f 4e 20 28 28 62 site)) b ON ((b
0e30: 2e 75 72 6c 5f 74 61 67 20 26 26 20 61 2e 72 75 .url_tag && a.ru
0e40: 6c 65 5f 74 61 67 29 29 29 3b 0a 0a 41 4c 54 45 le_tag)));..ALTE
0e50: 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 72 75 6c R TABLE ONLY rul
0e60: 65 73 0a 20 20 20 20 41 44 44 20 43 4f 4e 53 54 es. ADD CONST
0e70: 52 41 49 4e 54 20 72 75 6c 65 73 5f 70 6b 65 79 RAINT rules_pkey
0e80: 20 50 52 49 4d 41 52 59 20 4b 45 59 20 28 6e 65 PRIMARY KEY (ne
0e90: 74 6d 61 73 6b 29 3b 0a 0a 41 4c 54 45 52 20 54 tmask);..ALTER T
0ea0: 41 42 4c 45 20 4f 4e 4c 59 20 73 69 74 65 0a 20 ABLE ONLY site.
0eb0: 20 20 20 41 44 44 20 43 4f 4e 53 54 52 41 49 4e ADD CONSTRAIN
0ec0: 54 20 73 69 74 65 5f 69 64 20 50 52 49 4d 41 52 T site_id PRIMAR
0ed0: 59 20 4b 45 59 20 28 69 64 5f 73 69 74 65 29 3b Y KEY (id_site);
0ee0: 0a 0a 41 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e ..ALTER TABLE ON
0ef0: 4c 59 20 74 61 67 0a 20 20 20 20 41 44 44 20 43 LY tag. ADD C
0f00: 4f 4e 53 54 52 41 49 4e 54 20 74 61 67 5f 69 64 ONSTRAINT tag_id
0f10: 20 50 52 49 4d 41 52 59 20 4b 45 59 20 28 69 64 PRIMARY KEY (id
0f20: 5f 74 61 67 29 3b 0a 0a 41 4c 54 45 52 20 54 41 _tag);..ALTER TA
0f30: 42 4c 45 20 4f 4e 4c 59 20 75 72 6c 73 0a 20 20 BLE ONLY urls.
0f40: 20 20 41 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 ADD CONSTRAINT
0f50: 20 75 72 6c 73 5f 70 6b 65 79 20 50 52 49 4d 41 urls_pkey PRIMA
0f60: 52 59 20 4b 45 59 20 28 64 61 74 65 5f 61 64 64 RY KEY (date_add
0f70: 65 64 29 3b 0a 0a 43 52 45 41 54 45 20 55 4e 49 ed);..CREATE UNI
0f80: 51 55 45 20 49 4e 44 45 58 20 73 69 74 65 5f 73 QUE INDEX site_s
0f90: 20 4f 4e 20 73 69 74 65 20 55 53 49 4e 47 20 62 ON site USING b
0fa0: 74 72 65 65 20 28 73 69 74 65 29 3b 0a 0a 43 52 tree (site);..CR
0fb0: 45 41 54 45 20 49 4e 44 45 58 20 73 69 74 65 5f EATE INDEX site_
0fc0: 73 67 20 4f 4e 20 73 69 74 65 20 55 53 49 4e 47 sg ON site USING
0fd0: 20 67 69 6e 20 28 73 69 74 65 29 3b 0a 0a 43 52 gin (site);..CR
0fe0: 45 41 54 45 20 49 4e 44 45 58 20 74 61 67 5f 67 EATE INDEX tag_g
0ff0: 20 4f 4e 20 74 61 67 20 55 53 49 4e 47 20 67 69 ON tag USING gi
1000: 6e 20 28 74 61 67 29 3b 0a 0a 43 52 45 41 54 45 n (tag);..CREATE
1010: 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 74 61 UNIQUE INDEX ta
1020: 67 5f 73 20 4f 4e 20 74 61 67 20 55 53 49 4e 47 g_s ON tag USING
1030: 20 62 74 72 65 65 20 28 74 61 67 29 3b 0a 0a 43 btree (tag);..C
1040: 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 REATE UNIQUE IND
1050: 45 58 20 75 72 6c 73 5f 69 64 5f 73 69 74 65 20 EX urls_id_site
1060: 4f 4e 20 75 72 6c 73 20 55 53 49 4e 47 20 62 74 ON urls USING bt
1070: 72 65 65 20 28 69 64 5f 73 69 74 65 29 3b 0a ree (id_site);.