Artifact
0101122ebbf02233af4f21802d4aaf1ee905c538b7a3635f144734ffdf98a3b1:
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 68 69 73 20 66 75 6e 63 74 69 6f 6e 73 - this functions
0220: 20 72 65 74 75 72 6e 73 20 69 64 20 6f 66 20 73 returns id of s
0230: 69 74 65 0a 63 72 65 61 74 65 20 6f 72 20 72 65 ite.create or re
0240: 70 6c 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 67 place function g
0250: 65 74 5f 73 69 74 65 28 6d 79 5f 73 69 74 65 20 et_site(my_site
0260: 74 65 78 74 5b 5d 29 20 72 65 74 75 72 6e 73 20 text[]) returns
0270: 69 6e 74 65 67 65 72 0a 09 6c 61 6e 67 75 61 67 integer..languag
0280: 65 20 70 6c 70 67 73 71 6c 20 73 74 72 69 63 74 e plpgsql strict
0290: 0a 09 61 73 20 24 24 0a 64 65 63 6c 61 72 65 0a ..as $$.declare.
02a0: 09 73 69 74 65 5f 69 64 20 69 6e 74 65 67 65 72 .site_id integer
02b0: 3b 0a 62 65 67 69 6e 0a 09 73 65 6c 65 63 74 20 ;.begin..select
02c0: 69 64 5f 73 69 74 65 20 66 72 6f 6d 20 73 69 74 id_site from sit
02d0: 65 20 77 68 65 72 65 20 6d 79 5f 73 69 74 65 20 e where my_site
02e0: 3d 20 73 69 74 65 20 69 6e 74 6f 20 73 69 74 65 = site into site
02f0: 5f 69 64 3b 0a 09 69 66 20 6e 6f 74 20 66 6f 75 _id;..if not fou
0300: 6e 64 20 74 68 65 6e 0a 09 09 69 6e 73 65 72 74 nd then...insert
0310: 20 69 6e 74 6f 20 73 69 74 65 20 28 73 69 74 65 into site (site
0320: 29 20 76 61 6c 75 65 73 20 28 6d 79 5f 73 69 74 ) values (my_sit
0330: 65 29 3b 0a 09 09 73 65 6c 65 63 74 20 69 64 5f e);...select id_
0340: 73 69 74 65 20 66 72 6f 6d 20 73 69 74 65 20 77 site from site w
0350: 68 65 72 65 20 6d 79 5f 73 69 74 65 20 3d 20 73 here my_site = s
0360: 69 74 65 20 69 6e 74 6f 20 73 69 74 65 5f 69 64 ite into site_id
0370: 3b 0a 09 65 6e 64 20 69 66 3b 0a 09 72 65 74 75 ;..end if;..retu
0380: 72 6e 20 73 69 74 65 5f 69 64 3b 0a 65 6e 64 3b rn site_id;.end;
0390: 0a 24 24 3b 0a 0a 63 72 65 61 74 65 20 6f 72 20 .$$;..create or
03a0: 72 65 70 6c 61 63 65 20 66 75 6e 63 74 69 6f 6e replace function
03b0: 20 67 65 74 5f 73 69 74 65 28 64 6f 6d 61 69 6e get_site(domain
03c0: 20 74 65 78 74 29 20 72 65 74 75 72 6e 73 20 69 text) returns i
03d0: 6e 74 65 67 65 72 0a 09 6c 61 6e 67 75 61 67 65 nteger..language
03e0: 20 73 71 6c 20 69 6d 6d 75 74 61 62 6c 65 20 73 sql immutable s
03f0: 74 72 69 63 74 0a 09 61 73 20 24 24 0a 73 65 6c trict..as $$.sel
0400: 65 63 74 20 67 65 74 5f 73 69 74 65 28 74 72 69 ect get_site(tri
0410: 70 64 6f 6d 61 69 6e 28 24 31 29 29 20 61 73 20 pdomain($1)) as
0420: 72 65 73 75 6c 74 3b 0a 24 24 3b 0a 0a 2d 2d 20 result;.$$;..--
0430: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 this function ad
0440: 64 73 20 74 61 67 20 74 6f 20 64 6f 6d 61 69 6e ds tag to domain
0450: 0a 43 52 45 41 54 45 20 6f 72 20 72 65 70 6c 61 .CREATE or repla
0460: 63 65 20 46 55 4e 43 54 49 4f 4e 20 6d 61 72 6b ce FUNCTION mark
0470: 28 64 6f 6d 61 69 6e 20 74 65 78 74 2c 20 6e 65 (domain text, ne
0480: 77 5f 74 61 67 20 74 65 78 74 29 20 52 45 54 55 w_tag text) RETU
0490: 52 4e 53 20 76 6f 69 64 0a 09 4c 41 4e 47 55 41 RNS void..LANGUA
04a0: 47 45 20 73 71 6c 20 69 6d 6d 75 74 61 62 6c 65 GE sql immutable
04b0: 20 53 54 52 49 43 54 0a 09 41 53 20 24 24 0a 73 STRICT..AS $$.s
04c0: 65 6c 65 63 74 20 6d 61 72 6b 28 67 65 74 5f 73 elect mark(get_s
04d0: 69 74 65 28 24 31 29 2c 20 24 32 29 20 61 73 20 ite($1), $2) as
04e0: 72 65 73 75 6c 74 3b 0a 24 24 3b 0a 0a 2d 2d 20 result;.$$;..--
04f0: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64 this function ad
0500: 64 73 20 74 61 67 20 74 6f 20 73 69 74 65 20 62 ds tag to site b
0510: 79 20 73 69 74 65 20 69 64 0a 43 52 45 41 54 45 y site id.CREATE
0520: 20 6f 72 20 72 65 70 6c 61 63 65 20 46 55 4e 43 or replace FUNC
0530: 54 49 4f 4e 20 6d 61 72 6b 28 6d 79 5f 69 64 5f TION mark(my_id_
0540: 73 69 74 65 20 69 6e 74 65 67 65 72 2c 20 6e 65 site integer, ne
0550: 77 5f 74 61 67 20 74 65 78 74 29 20 52 45 54 55 w_tag text) RETU
0560: 52 4e 53 20 76 6f 69 64 0a 09 4c 41 4e 47 55 41 RNS void..LANGUA
0570: 47 45 20 70 6c 70 67 73 71 6c 20 53 54 52 49 43 GE plpgsql STRIC
0580: 54 0a 09 41 53 20 24 24 0a 64 65 63 6c 61 72 65 T..AS $$.declare
0590: 0a 09 2d 2d 20 6d 61 79 62 65 20 63 68 65 63 6b ..-- maybe check
05a0: 20 73 68 6f 75 6c 64 20 62 65 20 61 64 64 65 64 should be added
05b0: 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 73 75 to make sure su
05c0: 70 70 6c 69 65 64 20 73 69 74 65 20 69 64 20 72 pplied site id r
05d0: 65 61 6c 6c 79 20 65 78 69 73 74 73 0a 09 6d 79 eally exists..my
05e0: 5f 74 61 67 20 74 65 78 74 5b 5d 3b 0a 09 6d 79 _tag text[];..my
05f0: 5f 74 61 67 5f 69 64 20 69 6e 74 65 67 65 72 3b _tag_id integer;
0600: 0a 62 65 67 69 6e 0a 09 2d 2d 20 73 65 6c 65 63 .begin..-- selec
0610: 74 69 6e 67 20 74 61 67 73 20 73 69 74 65 20 61 ting tags site a
0620: 6c 72 65 61 64 79 20 68 61 76 65 20 61 6e 64 20 lready have and
0630: 61 64 64 69 6e 67 20 6e 65 77 20 74 61 67 20 74 adding new tag t
0640: 6f 20 74 68 65 6d 0a 09 2d 2d 20 6e 6f 74 65 20 o them..-- note
0650: 74 68 61 74 20 74 61 67 73 20 73 68 6f 75 6c 64 that tags should
0660: 20 62 65 20 73 6f 72 74 65 64 20 74 6f 20 65 6c be sorted to el
0670: 69 6d 69 6e 61 74 65 20 70 65 72 6d 75 74 61 74 iminate permutat
0680: 69 6f 6e 73 0a 09 73 65 6c 65 63 74 20 63 6f 61 ions..select coa
0690: 6c 65 73 63 65 28 74 61 67 2c 20 27 7b 7d 27 3a lesce(tag, '{}':
06a0: 3a 74 65 78 74 5b 5d 29 20 66 72 6f 6d 20 75 72 :text[]) from ur
06b0: 6c 73 20 6e 61 74 75 72 61 6c 20 6c 65 66 74 20 ls natural left
06c0: 6a 6f 69 6e 20 74 61 67 20 77 68 65 72 65 20 69 join tag where i
06d0: 64 5f 73 69 74 65 20 3d 20 6d 79 5f 69 64 5f 73 d_site = my_id_s
06e0: 69 74 65 20 69 6e 74 6f 20 6d 79 5f 74 61 67 3b ite into my_tag;
06f0: 0a 09 69 66 20 6e 6f 74 20 66 6f 75 6e 64 20 74 ..if not found t
0700: 68 65 6e 0a 09 09 2d 2d 20 6e 6f 20 72 65 63 6f hen...-- no reco
0710: 72 64 73 20 66 6f 75 6e 64 20 2d 20 63 72 65 61 rds found - crea
0720: 74 69 6e 67 20 6e 65 77 20 74 61 67 0a 09 09 69 ting new tag...i
0730: 6e 73 65 72 74 20 69 6e 74 6f 20 75 72 6c 73 20 nsert into urls
0740: 28 69 64 5f 73 69 74 65 2c 20 69 64 5f 74 61 67 (id_site, id_tag
0750: 29 20 76 61 6c 75 65 73 20 28 6d 79 5f 69 64 5f ) values (my_id_
0760: 73 69 74 65 2c 20 67 65 74 5f 74 61 67 28 61 72 site, get_tag(ar
0770: 72 61 79 5b 6e 65 77 5f 74 61 67 5d 29 29 3b 0a ray[new_tag]));.
0780: 09 65 6c 73 65 0a 09 09 2d 2d 20 6a 6f 69 6e 69 .else...-- joini
0790: 6e 67 20 74 61 67 73 0a 09 09 73 65 6c 65 63 74 ng tags...select
07a0: 20 75 73 6f 72 74 28 6d 79 5f 74 61 67 20 7c 7c usort(my_tag ||
07b0: 20 61 72 72 61 79 5b 6e 65 77 5f 74 61 67 5d 29 array[new_tag])
07c0: 20 69 6e 74 6f 20 6d 79 5f 74 61 67 3b 0a 09 09 into my_tag;...
07d0: 2d 2d 20 75 70 64 61 74 69 6e 67 20 65 78 69 73 -- updating exis
07e0: 74 69 6e 67 20 72 65 63 6f 72 64 0a 09 09 75 70 ting record...up
07f0: 64 61 74 65 20 75 72 6c 73 20 73 65 74 20 69 64 date urls set id
0800: 5f 74 61 67 20 3d 20 67 65 74 5f 74 61 67 28 6d _tag = get_tag(m
0810: 79 5f 74 61 67 20 7c 7c 20 61 72 72 61 79 5b 6e y_tag || array[n
0820: 65 77 5f 74 61 67 5d 29 20 77 68 65 72 65 20 69 ew_tag]) where i
0830: 64 5f 73 69 74 65 20 3d 20 6d 79 5f 69 64 5f 73 d_site = my_id_s
0840: 69 74 65 3b 0a 09 65 6e 64 20 69 66 3b 0a 65 6e ite;..end if;.en
0850: 64 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 d;.$$;..-- this
0860: 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
0870: 20 69 64 20 6f 66 20 74 61 67 20 61 72 72 61 79 id of tag array
0880: 0a 63 72 65 61 74 65 20 6f 72 20 72 65 70 6c 61 .create or repla
0890: 63 65 20 66 75 6e 63 74 69 6f 6e 20 67 65 74 5f ce function get_
08a0: 74 61 67 28 6d 79 5f 74 61 67 20 74 65 78 74 5b tag(my_tag text[
08b0: 5d 29 20 72 65 74 75 72 6e 73 20 69 6e 74 65 67 ]) returns integ
08c0: 65 72 0a 09 6c 61 6e 67 75 61 67 65 20 70 6c 70 er..language plp
08d0: 67 73 71 6c 20 73 74 72 69 63 74 0a 09 61 73 20 gsql strict..as
08e0: 24 24 0a 64 65 63 6c 61 72 65 0a 09 74 61 67 5f $$.declare..tag_
08f0: 69 64 20 69 6e 74 65 67 65 72 3b 0a 62 65 67 69 id integer;.begi
0900: 6e 0a 09 73 65 6c 65 63 74 20 69 64 5f 74 61 67 n..select id_tag
0910: 20 66 72 6f 6d 20 74 61 67 20 77 68 65 72 65 20 from tag where
0920: 75 73 6f 72 74 28 6d 79 5f 74 61 67 29 20 3d 20 usort(my_tag) =
0930: 74 61 67 20 69 6e 74 6f 20 74 61 67 5f 69 64 3b tag into tag_id;
0940: 0a 09 69 66 20 6e 6f 74 20 66 6f 75 6e 64 20 74 ..if not found t
0950: 68 65 6e 0a 09 09 69 6e 73 65 72 74 20 69 6e 74 hen...insert int
0960: 6f 20 74 61 67 20 28 74 61 67 29 20 76 61 6c 75 o tag (tag) valu
0970: 65 73 20 28 75 73 6f 72 74 28 6d 79 5f 74 61 67 es (usort(my_tag
0980: 29 29 3b 0a 09 09 73 65 6c 65 63 74 20 69 64 5f ));...select id_
0990: 74 61 67 20 66 72 6f 6d 20 74 61 67 20 77 68 65 tag from tag whe
09a0: 72 65 20 75 73 6f 72 74 28 6d 79 5f 74 61 67 29 re usort(my_tag)
09b0: 20 3d 20 74 61 67 20 69 6e 74 6f 20 74 61 67 5f = tag into tag_
09c0: 69 64 3b 0a 09 65 6e 64 20 69 66 3b 0a 09 72 65 id;..end if;..re
09d0: 74 75 72 6e 20 74 61 67 5f 69 64 3b 0a 65 6e 64 turn tag_id;.end
09e0: 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 72 61 6e 73 66 ;.$$;..-- transf
09f0: 6f 72 6d 73 20 64 6f 6d 61 69 6e 20 69 6e 74 6f orms domain into
0a00: 20 6f 72 64 65 72 65 64 20 61 72 72 61 79 20 66 ordered array f
0a10: 6f 72 20 69 6e 64 65 78 69 6e 67 0a 43 52 45 41 or indexing.CREA
0a20: 54 45 20 6f 72 20 72 65 70 6c 61 63 65 20 46 55 TE or replace FU
0a30: 4e 43 54 49 4f 4e 20 74 72 69 70 64 6f 6d 61 69 NCTION tripdomai
0a40: 6e 28 75 72 6c 20 74 65 78 74 29 20 52 45 54 55 n(url text) RETU
0a50: 52 4e 53 20 74 65 78 74 5b 5d 0a 09 4c 41 4e 47 RNS text[]..LANG
0a60: 55 41 47 45 20 70 6c 70 67 73 71 6c 20 49 4d 4d UAGE plpgsql IMM
0a70: 55 54 41 42 4c 45 20 53 54 52 49 43 54 0a 09 41 UTABLE STRICT..A
0a80: 53 20 24 5f 24 0a 64 65 63 6c 61 72 65 0a 09 72 S $_$.declare..r
0a90: 65 73 75 6c 74 20 74 65 78 74 5b 5d 3b 0a 09 73 esult text[];..s
0aa0: 70 6c 69 74 74 65 64 20 74 65 78 74 5b 5d 3b 0a plitted text[];.
0ab0: 09 78 20 69 6e 74 65 67 65 72 3b 0a 09 6c 65 6e .x integer;..len
0ac0: 67 74 68 20 69 6e 74 65 67 65 72 3b 0a 62 65 67 gth integer;.beg
0ad0: 69 6e 0a 09 73 70 6c 69 74 74 65 64 20 3a 3d 20 in..splitted :=
0ae0: 73 74 72 69 6e 67 5f 74 6f 5f 61 72 72 61 79 28 string_to_array(
0af0: 24 31 2c 20 27 2e 27 29 3b 0a 09 6c 65 6e 67 74 $1, '.');..lengt
0b00: 68 20 3a 3d 20 61 72 72 61 79 5f 6c 65 6e 67 74 h := array_lengt
0b10: 68 28 73 70 6c 69 74 74 65 64 2c 20 31 29 3b 0a h(splitted, 1);.
0b20: 09 78 20 3a 3d 20 31 3b 0a 09 6c 6f 6f 70 0a 09 .x := 1;..loop..
0b30: 09 65 78 69 74 20 77 68 65 6e 20 73 70 6c 69 74 .exit when split
0b40: 74 65 64 5b 78 5d 20 69 73 20 6e 75 6c 6c 3b 0a ted[x] is null;.
0b50: 09 09 72 65 73 75 6c 74 5b 78 5d 20 3a 3d 20 73 ..result[x] := s
0b60: 70 6c 69 74 74 65 64 5b 78 5d 20 7c 7c 20 27 3a plitted[x] || ':
0b70: 27 20 7c 7c 20 6c 65 6e 67 74 68 20 2d 20 78 3b ' || length - x;
0b80: 0a 09 09 78 20 3a 3d 20 78 20 2b 20 31 3b 0a 09 ...x := x + 1;..
0b90: 65 6e 64 20 6c 6f 6f 70 3b 0a 09 72 65 74 75 72 end loop;..retur
0ba0: 6e 20 72 65 73 75 6c 74 3b 0a 65 6e 64 3b 24 5f n result;.end;$_
0bb0: 24 3b 0a 0a 2d 2d 20 74 72 61 6e 73 66 6f 72 6d $;..-- transform
0bc0: 73 20 6f 72 64 65 72 65 64 20 61 72 72 61 79 20 s ordered array
0bd0: 69 6e 74 6f 20 64 6f 6d 61 69 6e 0a 63 72 65 61 into domain.crea
0be0: 74 65 20 6f 72 20 72 65 70 6c 61 63 65 20 66 75 te or replace fu
0bf0: 6e 63 74 69 6f 6e 20 75 6e 74 72 69 70 28 73 69 nction untrip(si
0c00: 74 65 20 74 65 78 74 5b 5d 29 20 72 65 74 75 72 te text[]) retur
0c10: 6e 73 20 74 65 78 74 0a 09 6c 61 6e 67 75 61 67 ns text..languag
0c20: 65 20 70 6c 70 67 73 71 6c 20 69 6d 6d 75 74 61 e plpgsql immuta
0c30: 62 6c 65 20 73 74 72 69 63 74 0a 09 61 73 20 24 ble strict..as $
0c40: 5f 24 0a 64 65 63 6c 61 72 65 0a 09 78 20 69 6e _$.declare..x in
0c50: 74 65 67 65 72 3b 0a 09 73 70 6c 69 74 74 65 64 teger;..splitted
0c60: 20 74 65 78 74 5b 5d 3b 0a 09 70 61 69 72 20 74 text[];..pair t
0c70: 65 78 74 5b 5d 3b 0a 62 65 67 69 6e 0a 09 78 20 ext[];.begin..x
0c80: 3a 3d 20 61 72 72 61 79 5f 6c 65 6e 67 74 68 28 := array_length(
0c90: 73 69 74 65 2c 20 31 29 3b 0a 09 6c 6f 6f 70 20 site, 1);..loop
0ca0: 0a 09 09 65 78 69 74 20 77 68 65 6e 20 73 69 74 ...exit when sit
0cb0: 65 5b 78 5d 20 69 73 20 6e 75 6c 6c 3b 0a 09 09 e[x] is null;...
0cc0: 70 61 69 72 20 3a 3d 20 73 74 72 69 6e 67 5f 74 pair := string_t
0cd0: 6f 5f 61 72 72 61 79 28 73 69 74 65 5b 78 5d 2c o_array(site[x],
0ce0: 20 27 3a 27 29 3b 0a 09 09 73 70 6c 69 74 74 65 ':');...splitte
0cf0: 64 5b 30 20 2d 20 70 61 69 72 5b 32 5d 3a 3a 69 d[0 - pair[2]::i
0d00: 6e 74 65 67 65 72 5d 20 3a 3d 20 70 61 69 72 5b nteger] := pair[
0d10: 31 5d 3b 0a 09 09 78 20 3a 3d 20 78 20 2d 20 31 1];...x := x - 1
0d20: 3b 0a 09 65 6e 64 20 6c 6f 6f 70 3b 0a 09 72 65 ;..end loop;..re
0d30: 74 75 72 6e 20 61 72 72 61 79 5f 74 6f 5f 73 74 turn array_to_st
0d40: 72 69 6e 67 28 73 70 6c 69 74 74 65 64 2c 20 27 ring(splitted, '
0d50: 2e 27 29 3b 0a 65 6e 64 3b 0a 24 5f 24 3b 0a 0a .');.end;.$_$;..
0d60: 2d 2d 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 -- table to hold
0d70: 20 61 6c 6c 20 72 75 6c 65 73 0a 43 52 45 41 54 all rules.CREAT
0d80: 45 20 54 41 42 4c 45 20 72 75 6c 65 73 20 28 0a E TABLE rules (.
0d90: 09 6e 65 74 6d 61 73 6b 20 63 69 64 72 20 4e 4f .netmask cidr NO
0da0: 54 20 4e 55 4c 4c 2c 0a 09 72 65 64 69 72 65 63 T NULL,..redirec
0db0: 74 5f 75 72 6c 20 74 65 78 74 20 44 45 46 41 55 t_url text DEFAU
0dc0: 4c 54 20 27 61 62 6f 75 74 3a 3a 62 6c 61 6e 6b LT 'about::blank
0dd0: 27 3a 3a 74 65 78 74 20 4e 4f 54 20 4e 55 4c 4c '::text NOT NULL
0de0: 2c 0a 09 66 72 6f 6d 5f 77 65 65 6b 64 61 79 20 ,..from_weekday
0df0: 73 6d 61 6c 6c 69 6e 74 20 44 45 46 41 55 4c 54 smallint DEFAULT
0e00: 20 30 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 74 6f 0 NOT NULL,..to
0e10: 5f 77 65 65 6b 64 61 79 20 73 6d 61 6c 6c 69 6e _weekday smallin
0e20: 74 20 44 45 46 41 55 4c 54 20 36 20 4e 4f 54 20 t DEFAULT 6 NOT
0e30: 4e 55 4c 4c 2c 0a 09 66 72 6f 6d 5f 74 69 6d 65 NULL,..from_time
0e40: 20 74 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 time without ti
0e50: 6d 65 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 me zone DEFAULT
0e60: 27 30 30 3a 30 30 3a 30 30 27 3a 3a 74 69 6d 65 '00:00:00'::time
0e70: 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f without time zo
0e80: 6e 65 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 74 6f ne NOT NULL,..to
0e90: 5f 74 69 6d 65 20 74 69 6d 65 20 77 69 74 68 6f _time time witho
0ea0: 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 44 45 46 ut time zone DEF
0eb0: 41 55 4c 54 20 27 32 33 3a 35 39 3a 35 39 27 3a AULT '23:59:59':
0ec0: 3a 74 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 :time without ti
0ed0: 6d 65 20 7a 6f 6e 65 20 4e 4f 54 20 4e 55 4c 4c me zone NOT NULL
0ee0: 2c 0a 09 69 64 5f 74 61 67 20 73 6d 61 6c 6c 69 ,..id_tag smalli
0ef0: 6e 74 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a 0a nt NOT NULL.);..
0f00: 41 4c 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 ALTER TABLE ONLY
0f10: 20 72 75 6c 65 73 0a 09 41 44 44 20 43 4f 4e 53 rules..ADD CONS
0f20: 54 52 41 49 4e 54 20 72 75 6c 65 73 5f 70 6b 65 TRAINT rules_pke
0f30: 79 20 50 52 49 4d 41 52 59 20 4b 45 59 20 28 6e y PRIMARY KEY (n
0f40: 65 74 6d 61 73 6b 29 3b 0a 0a 2d 2d 20 74 61 62 etmask);..-- tab
0f50: 6c 65 20 74 6f 20 68 6f 6c 64 20 73 69 74 65 20 le to hold site
0f60: 61 72 72 61 79 73 0a 43 52 45 41 54 45 20 54 41 arrays.CREATE TA
0f70: 42 4c 45 20 73 69 74 65 20 28 0a 09 69 64 5f 73 BLE site (..id_s
0f80: 69 74 65 20 73 65 72 69 61 6c 2c 0a 09 73 69 74 ite serial,..sit
0f90: 65 20 74 65 78 74 5b 5d 20 4e 4f 54 20 4e 55 4c e text[] NOT NUL
0fa0: 4c 0a 29 3b 0a 0a 41 4c 54 45 52 20 54 41 42 4c L.);..ALTER TABL
0fb0: 45 20 4f 4e 4c 59 20 73 69 74 65 0a 09 41 44 44 E ONLY site..ADD
0fc0: 20 43 4f 4e 53 54 52 41 49 4e 54 20 73 69 74 65 CONSTRAINT site
0fd0: 5f 70 6b 65 79 20 50 52 49 4d 41 52 59 20 4b 45 _pkey PRIMARY KE
0fe0: 59 20 28 69 64 5f 73 69 74 65 29 3b 0a 0a 43 52 Y (id_site);..CR
0ff0: 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 EATE UNIQUE INDE
1000: 58 20 73 69 74 65 5f 75 20 4f 4e 20 73 69 74 65 X site_u ON site
1010: 20 28 75 73 6f 72 74 28 73 69 74 65 29 29 3b 0a (usort(site));.
1020: 0a 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 69 .CREATE INDEX si
1030: 74 65 5f 67 20 4f 4e 20 73 69 74 65 20 55 53 49 te_g ON site USI
1040: 4e 47 20 67 69 6e 20 28 73 69 74 65 29 3b 0a 0a NG gin (site);..
1050: 2d 2d 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 -- table to hold
1060: 20 74 61 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e tag combination
1070: 73 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 s.CREATE TABLE t
1080: 61 67 20 28 0a 09 69 64 5f 74 61 67 20 73 65 72 ag (..id_tag ser
1090: 69 61 6c 2c 0a 09 74 61 67 20 74 65 78 74 5b 5d ial,..tag text[]
10a0: 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a 0a 41 4c NOT NULL.);..AL
10b0: 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 74 TER TABLE ONLY t
10c0: 61 67 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 49 ag..ADD CONSTRAI
10d0: 4e 54 20 74 61 67 5f 70 6b 65 79 20 50 52 49 4d NT tag_pkey PRIM
10e0: 41 52 59 20 4b 45 59 20 28 69 64 5f 74 61 67 29 ARY KEY (id_tag)
10f0: 3b 0a 0a 43 52 45 41 54 45 20 55 4e 49 51 55 45 ;..CREATE UNIQUE
1100: 20 49 4e 44 45 58 20 74 61 67 5f 75 20 4f 4e 20 INDEX tag_u ON
1110: 74 61 67 20 28 75 73 6f 72 74 28 74 61 67 29 29 tag (usort(tag))
1120: 3b 0a 0a 43 52 45 41 54 45 20 49 4e 44 45 58 20 ;..CREATE INDEX
1130: 74 61 67 5f 67 20 4f 4e 20 74 61 67 20 55 53 49 tag_g ON tag USI
1140: 4e 47 20 67 69 6e 20 28 74 61 67 29 3b 0a 0a 2d NG gin (tag);..-
1150: 2d 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 - table to hold
1160: 74 61 67 20 2d 20 73 69 74 65 20 6c 69 6e 6b 73 tag - site links
1170: 0a 43 52 45 41 54 45 20 54 41 42 4c 45 20 75 72 .CREATE TABLE ur
1180: 6c 73 20 28 0a 09 64 61 74 65 5f 61 64 64 65 64 ls (..date_added
1190: 20 74 69 6d 65 73 74 61 6d 70 20 77 69 74 68 6f timestamp witho
11a0: 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 44 45 46 ut time zone DEF
11b0: 41 55 4c 54 20 28 27 6e 6f 77 27 3a 3a 74 65 78 AULT ('now'::tex
11c0: 74 29 3a 3a 74 69 6d 65 73 74 61 6d 70 28 30 29 t)::timestamp(0)
11d0: 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f without time zo
11e0: 6e 65 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 69 64 ne NOT NULL,..id
11f0: 5f 73 69 74 65 20 73 6d 61 6c 6c 69 6e 74 20 4e _site smallint N
1200: 4f 54 20 4e 55 4c 4c 2c 0a 09 69 64 5f 74 61 67 OT NULL,..id_tag
1210: 20 73 6d 61 6c 6c 69 6e 74 20 4e 4f 54 20 4e 55 smallint NOT NU
1220: 4c 4c 2c 0a 09 72 65 67 65 78 20 74 65 78 74 0a LL,..regex text.
1230: 29 3b 0a 0a 41 4c 54 45 52 20 54 41 42 4c 45 20 );..ALTER TABLE
1240: 4f 4e 4c 59 20 75 72 6c 73 0a 09 41 44 44 20 43 ONLY urls..ADD C
1250: 4f 4e 53 54 52 41 49 4e 54 20 75 72 6c 73 5f 70 ONSTRAINT urls_p
1260: 6b 65 79 20 50 52 49 4d 41 52 59 20 4b 45 59 20 key PRIMARY KEY
1270: 28 64 61 74 65 5f 61 64 64 65 64 29 3b 0a 0a 43 (date_added);..C
1280: 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 REATE UNIQUE IND
1290: 45 58 20 75 72 6c 73 5f 69 64 5f 73 69 74 65 20 EX urls_id_site
12a0: 4f 4e 20 75 72 6c 73 20 55 53 49 4e 47 20 62 74 ON urls USING bt
12b0: 72 65 65 20 28 69 64 5f 73 69 74 65 29 3b 0a 0a ree (id_site);..
12c0: 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e CREATE UNIQUE IN
12d0: 44 45 58 20 75 72 6c 73 5f 69 64 5f 74 61 67 20 DEX urls_id_tag
12e0: 4f 4e 20 75 72 6c 73 20 55 53 49 4e 47 20 62 74 ON urls USING bt
12f0: 72 65 65 20 28 69 64 5f 74 61 67 29 3b 0a 0a 2d ree (id_tag);..-
1300: 2d 20 72 75 6c 65 20 74 6f 20 6a 6f 69 6e 20 61 - rule to join a
1310: 6c 6c 20 74 61 62 6c 65 73 20 69 6e 74 6f 20 6f ll tables into o
1320: 6e 65 20 74 6f 20 73 69 6d 70 6c 69 66 79 20 61 ne to simplify a
1330: 63 63 65 73 73 0a 2d 2d 20 61 75 74 6f 6d 61 74 ccess.-- automat
1340: 69 63 61 6c 6c 20 75 73 65 73 20 63 75 72 72 65 icall uses curre
1350: 6e 74 20 64 61 79 20 61 6e 64 20 74 69 6d 65 20 nt day and time
1360: 64 61 74 61 0a 43 52 45 41 54 45 20 56 49 45 57 data.CREATE VIEW
1370: 20 73 69 74 65 5f 72 75 6c 65 20 41 53 0a 53 45 site_rule AS.SE
1380: 4c 45 43 54 20 61 2e 72 65 64 69 72 65 63 74 5f LECT a.redirect_
1390: 75 72 6c 2c 20 61 2e 6e 65 74 6d 61 73 6b 2c 20 url, a.netmask,
13a0: 62 2e 73 69 74 65 2c 20 62 2e 72 65 67 65 78 70 b.site, b.regexp
13b0: 0a 46 52 4f 4d 20 28 28 0a 09 53 45 4c 45 43 54 .FROM ((..SELECT
13c0: 20 72 75 6c 65 73 2e 72 65 64 69 72 65 63 74 5f rules.redirect_
13d0: 75 72 6c 2c 20 74 61 67 2e 74 61 67 20 41 53 20 url, tag.tag AS
13e0: 72 75 6c 65 5f 74 61 67 2c 20 72 75 6c 65 73 2e rule_tag, rules.
13f0: 6e 65 74 6d 61 73 6b 0a 09 46 52 4f 4d 20 72 75 netmask..FROM ru
1400: 6c 65 73 20 4e 41 54 55 52 41 4c 20 4a 4f 49 4e les NATURAL JOIN
1410: 20 74 61 67 0a 09 57 48 45 52 45 20 28 27 6e 6f tag..WHERE ('no
1420: 77 27 3a 3a 74 65 78 74 29 3a 3a 74 69 6d 65 20 w'::text)::time
1430: 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e without time zon
1440: 65 20 3e 3d 20 72 75 6c 65 73 2e 66 72 6f 6d 5f e >= rules.from_
1450: 74 69 6d 65 0a 09 09 41 4e 44 20 28 27 6e 6f 77 time...AND ('now
1460: 27 3a 3a 74 65 78 74 29 3a 3a 74 69 6d 65 20 77 '::text)::time w
1470: 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 ithout time zone
1480: 20 3c 3d 20 72 75 6c 65 73 2e 74 6f 5f 74 69 6d <= rules.to_tim
1490: 65 0a 09 09 41 4e 44 20 64 61 74 65 5f 70 61 72 e...AND date_par
14a0: 74 28 27 64 6f 77 27 3a 3a 74 65 78 74 2c 20 6e t('dow'::text, n
14b0: 6f 77 28 29 29 20 3e 3d 20 28 72 75 6c 65 73 2e ow()) >= (rules.
14c0: 66 72 6f 6d 5f 77 65 65 6b 64 61 79 29 3a 3a 64 from_weekday)::d
14d0: 6f 75 62 6c 65 20 70 72 65 63 69 73 69 6f 6e 0a ouble precision.
14e0: 09 09 41 4e 44 20 64 61 74 65 5f 70 61 72 74 28 ..AND date_part(
14f0: 27 64 6f 77 27 3a 3a 74 65 78 74 2c 20 6e 6f 77 'dow'::text, now
1500: 28 29 29 20 3c 3d 20 28 72 75 6c 65 73 2e 74 6f ()) <= (rules.to
1510: 5f 77 65 65 6b 64 61 79 29 3a 3a 64 6f 75 62 6c _weekday)::doubl
1520: 65 20 70 72 65 63 69 73 69 6f 6e 0a 29 20 61 20 e precision.) a
1530: 4a 4f 49 4e 20 28 0a 09 53 45 4c 45 43 54 20 73 JOIN (..SELECT s
1540: 69 74 65 2e 73 69 74 65 2c 20 74 61 67 2e 74 61 ite.site, tag.ta
1550: 67 20 41 53 20 75 72 6c 5f 74 61 67 2c 20 72 65 g AS url_tag, re
1560: 67 65 78 70 0a 09 46 52 4f 4d 20 75 72 6c 73 20 gexp..FROM urls
1570: 4e 41 54 55 52 41 4c 20 4a 4f 49 4e 20 74 61 67 NATURAL JOIN tag
1580: 20 4e 41 54 55 52 41 4c 20 4a 4f 49 4e 20 73 69 NATURAL JOIN si
1590: 74 65 0a 29 20 62 20 4f 4e 20 28 62 2e 75 72 6c te.) b ON (b.url
15a0: 5f 74 61 67 20 26 26 20 61 2e 72 75 6c 65 5f 74 _tag && a.rule_t
15b0: 61 67 29 29 3b 0a ag));.