Artifact
952aeefe4bb72fdddc64dba3dfa28b7a8ad1b176c004be7f3e901bfeec9880e7:
0000: 2d 2d 20 67 65 6e 65 72 61 6c 20 61 72 72 61 79 -- general array
0010: 20 73 6f 72 74 69 6e 67 20 61 6e 64 20 64 6f 6d sorting and dom
0020: 61 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 66 ain processing f
0030: 75 6e 63 74 69 6f 6e 73 0a 2d 2d 20 73 6f 72 74 unctions.-- sort
0040: 73 20 61 72 72 61 79 0a 43 52 45 41 54 45 20 6f s array.CREATE o
0050: 72 20 72 65 70 6c 61 63 65 20 46 55 4e 43 54 49 r replace FUNCTI
0060: 4f 4e 20 73 6f 72 74 28 6f 72 69 67 69 6e 61 6c ON sort(original
0070: 20 61 6e 79 61 72 72 61 79 29 20 52 45 54 55 52 anyarray) RETUR
0080: 4e 53 20 61 6e 79 61 72 72 61 79 0a 09 4c 41 4e NS anyarray..LAN
0090: 47 55 41 47 45 20 73 71 6c 20 49 4d 4d 55 54 41 GUAGE sql IMMUTA
00a0: 42 4c 45 20 53 54 52 49 43 54 0a 09 41 53 20 24 BLE STRICT..AS $
00b0: 5f 24 0a 73 65 6c 65 63 74 20 61 72 72 61 79 5f _$.select array_
00c0: 61 67 67 28 69 74 65 6d 29 20 61 73 20 72 65 73 agg(item) as res
00d0: 75 6c 74 20 66 72 6f 6d 20 28 73 65 6c 65 63 74 ult from (select
00e0: 20 75 6e 6e 65 73 74 28 24 31 29 20 61 73 20 69 unnest($1) as i
00f0: 74 65 6d 20 6f 72 64 65 72 20 62 79 20 69 74 65 tem order by ite
0100: 6d 29 20 61 3b 0a 24 5f 24 3b 0a 0a 2d 2d 20 73 m) a;.$_$;..-- s
0110: 6f 72 74 73 20 61 72 72 61 79 20 61 6e 64 20 72 orts array and r
0120: 65 6d 6f 76 65 73 20 64 75 70 6c 69 63 61 74 65 emoves duplicate
0130: 73 0a 43 52 45 41 54 45 20 6f 72 20 72 65 70 6c s.CREATE or repl
0140: 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 75 73 6f ace FUNCTION uso
0150: 72 74 28 6f 72 69 67 69 6e 61 6c 20 61 6e 79 61 rt(original anya
0160: 72 72 61 79 29 20 52 45 54 55 52 4e 53 20 61 6e rray) RETURNS an
0170: 79 61 72 72 61 79 0a 09 4c 41 4e 47 55 41 47 45 yarray..LANGUAGE
0180: 20 73 71 6c 20 49 4d 4d 55 54 41 42 4c 45 20 53 sql IMMUTABLE S
0190: 54 52 49 43 54 0a 09 41 53 20 24 5f 24 0a 73 65 TRICT..AS $_$.se
01a0: 6c 65 63 74 20 61 72 72 61 79 5f 61 67 67 28 69 lect array_agg(i
01b0: 74 65 6d 29 20 61 73 20 72 65 73 75 6c 74 20 66 tem) as result f
01c0: 72 6f 6d 20 28 73 65 6c 65 63 74 20 64 69 73 74 rom (select dist
01d0: 69 6e 63 74 20 75 6e 6e 65 73 74 28 24 31 29 20 inct unnest($1)
01e0: 61 73 20 69 74 65 6d 20 6f 72 64 65 72 20 62 79 as item order by
01f0: 20 69 74 65 6d 29 20 61 3b 0a 24 5f 24 3b 0a 0a item) a;.$_$;..
0200: 2d 2d 20 67 65 6e 65 72 61 6c 20 64 61 74 61 62 -- general datab
0210: 61 73 65 20 73 74 72 75 63 74 75 72 65 0a 2d 2d ase structure.--
0220: 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 61 table to hold a
0230: 6c 6c 20 72 75 6c 65 73 0a 43 52 45 41 54 45 20 ll rules.CREATE
0240: 54 41 42 4c 45 20 72 75 6c 65 73 20 28 0a 09 6e TABLE rules (..n
0250: 65 74 6d 61 73 6b 20 63 69 64 72 20 4e 4f 54 20 etmask cidr NOT
0260: 4e 55 4c 4c 2c 0a 09 72 65 64 69 72 65 63 74 5f NULL,..redirect_
0270: 75 72 6c 20 74 65 78 74 20 44 45 46 41 55 4c 54 url text DEFAULT
0280: 20 27 61 62 6f 75 74 3a 3a 62 6c 61 6e 6b 27 3a 'about::blank':
0290: 3a 74 65 78 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a :text NOT NULL,.
02a0: 09 66 72 6f 6d 5f 77 65 65 6b 64 61 79 20 73 6d .from_weekday sm
02b0: 61 6c 6c 69 6e 74 20 44 45 46 41 55 4c 54 20 30 allint DEFAULT 0
02c0: 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 77 NOT NULL,..to_w
02d0: 65 65 6b 64 61 79 20 73 6d 61 6c 6c 69 6e 74 20 eekday smallint
02e0: 44 45 46 41 55 4c 54 20 36 20 4e 4f 54 20 4e 55 DEFAULT 6 NOT NU
02f0: 4c 4c 2c 0a 09 66 72 6f 6d 5f 74 69 6d 65 20 74 LL,..from_time t
0300: 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65 ime without time
0310: 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 27 30 zone DEFAULT '0
0320: 30 3a 30 30 3a 30 30 27 3a 3a 74 69 6d 65 20 77 0:00:00'::time w
0330: 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 ithout time zone
0340: 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 74 NOT NULL,..to_t
0350: 69 6d 65 20 74 69 6d 65 20 77 69 74 68 6f 75 74 ime time without
0360: 20 74 69 6d 65 20 7a 6f 6e 65 20 44 45 46 41 55 time zone DEFAU
0370: 4c 54 20 27 32 33 3a 35 39 3a 35 39 27 3a 3a 74 LT '23:59:59'::t
0380: 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65 ime without time
0390: 20 7a 6f 6e 65 20 4e 4f 54 20 4e 55 4c 4c 2c 0a zone NOT NULL,.
03a0: 09 74 61 67 20 74 65 78 74 5b 5d 20 4e 4f 54 20 .tag text[] NOT
03b0: 4e 55 4c 4c 0a 29 3b 0a 0a 2d 2d 20 74 61 62 6c NULL.);..-- tabl
03c0: 65 20 74 6f 20 68 6f 6c 64 20 74 61 67 20 2d 20 e to hold tag -
03d0: 73 69 74 65 20 6c 69 6e 6b 73 0a 43 52 45 41 54 site links.CREAT
03e0: 45 20 54 41 42 4c 45 20 75 72 6c 73 20 28 0a 09 E TABLE urls (..
03f0: 64 61 74 65 5f 61 64 64 65 64 20 74 69 6d 65 73 date_added times
0400: 74 61 6d 70 20 77 69 74 68 6f 75 74 20 74 69 6d tamp without tim
0410: 65 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 28 e zone DEFAULT (
0420: 27 6e 6f 77 27 3a 3a 74 65 78 74 29 3a 3a 74 69 'now'::text)::ti
0430: 6d 65 73 74 61 6d 70 28 30 29 20 77 69 74 68 6f mestamp(0) witho
0440: 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 4e 4f 54 ut time zone NOT
0450: 20 4e 55 4c 4c 2c 0a 09 73 69 74 65 20 74 65 78 NULL,..site tex
0460: 74 5b 5d 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 74 t[] NOT NULL,..t
0470: 61 67 20 74 65 78 74 5b 5d 20 4e 4f 54 20 4e 55 ag text[] NOT NU
0480: 4c 4c 2c 0a 09 72 65 67 65 78 70 20 74 65 78 74 LL,..regexp text
0490: 0a 29 3b 0a 0a 63 72 65 61 74 65 20 75 6e 69 71 .);..create uniq
04a0: 75 65 20 69 6e 64 65 78 20 75 72 6c 73 5f 72 73 ue index urls_rs
04b0: 74 20 6f 6e 20 75 72 6c 73 20 28 72 65 67 65 78 t on urls (regex
04c0: 70 2c 20 75 73 6f 72 74 28 73 69 74 65 29 2c 20 p, usort(site),
04d0: 75 73 6f 72 74 28 74 61 67 29 29 3b 0a 0a 2d 2d usort(tag));..--
04e0: 20 72 75 6c 65 20 74 6f 20 6a 6f 69 6e 20 61 6c rule to join al
04f0: 6c 20 74 61 62 6c 65 73 20 69 6e 74 6f 20 6f 6e l tables into on
0500: 65 20 74 6f 20 73 69 6d 70 6c 69 66 79 20 61 63 e to simplify ac
0510: 63 65 73 73 0a 2d 2d 20 61 75 74 6f 6d 61 74 69 cess.-- automati
0520: 63 61 6c 6c 20 75 73 65 73 20 63 75 72 72 65 6e call uses curren
0530: 74 20 64 61 79 20 61 6e 64 20 74 69 6d 65 20 64 t day and time d
0540: 61 74 61 0a 63 72 65 61 74 65 20 76 69 65 77 20 ata.create view
0550: 73 69 74 65 5f 72 75 6c 65 20 61 73 0a 73 65 6c site_rule as.sel
0560: 65 63 74 20 72 65 64 69 72 65 63 74 5f 75 72 6c ect redirect_url
0570: 2c 20 6e 65 74 6d 61 73 6b 2c 20 73 69 74 65 2c , netmask, site,
0580: 20 72 65 67 65 78 70 0a 66 72 6f 6d 20 72 75 6c regexp.from rul
0590: 65 73 20 6a 6f 69 6e 20 75 72 6c 73 0a 6f 6e 20 es join urls.on
05a0: 28 75 72 6c 73 2e 74 61 67 20 26 26 20 72 75 6c (urls.tag && rul
05b0: 65 73 2e 74 61 67 29 0a 77 68 65 72 65 20 28 27 es.tag).where ('
05c0: 6e 6f 77 27 3a 3a 74 65 78 74 29 3a 3a 74 69 6d now'::text)::tim
05d0: 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a e without time z
05e0: 6f 6e 65 20 3e 3d 20 66 72 6f 6d 5f 74 69 6d 65 one >= from_time
05f0: 0a 09 61 6e 64 20 28 27 6e 6f 77 27 3a 3a 74 65 ..and ('now'::te
0600: 78 74 29 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 xt)::time withou
0610: 74 20 74 69 6d 65 20 7a 6f 6e 65 20 3c 3d 20 74 t time zone <= t
0620: 6f 5f 74 69 6d 65 0a 09 61 6e 64 20 64 61 74 65 o_time..and date
0630: 5f 70 61 72 74 28 27 64 6f 77 27 3a 3a 74 65 78 _part('dow'::tex
0640: 74 2c 20 6e 6f 77 28 29 29 20 3e 3d 20 28 66 72 t, now()) >= (fr
0650: 6f 6d 5f 77 65 65 6b 64 61 79 29 3a 3a 64 6f 75 om_weekday)::dou
0660: 62 6c 65 20 70 72 65 63 69 73 69 6f 6e 0a 09 61 ble precision..a
0670: 6e 64 20 64 61 74 65 5f 70 61 72 74 28 27 64 6f nd date_part('do
0680: 77 27 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 29 29 w'::text, now())
0690: 20 3c 3d 20 28 74 6f 5f 77 65 65 6b 64 61 79 29 <= (to_weekday)
06a0: 3a 3a 64 6f 75 62 6c 65 20 70 72 65 63 69 73 69 ::double precisi
06b0: 6f 6e 3b 0a 0a 43 52 45 41 54 45 20 50 52 4f 43 on;..CREATE PROC
06c0: 45 44 55 52 41 4c 20 4c 41 4e 47 55 41 47 45 20 EDURAL LANGUAGE
06d0: 70 6c 70 67 73 71 6c 3b 0a 0a 2d 2d 20 74 72 61 plpgsql;..-- tra
06e0: 6e 73 66 6f 72 6d 73 20 64 6f 6d 61 69 6e 20 69 nsforms domain i
06f0: 6e 74 6f 20 6f 72 64 65 72 65 64 20 61 72 72 61 nto ordered arra
0700: 79 20 66 6f 72 20 69 6e 64 65 78 69 6e 67 0a 43 y for indexing.C
0710: 52 45 41 54 45 20 6f 72 20 72 65 70 6c 61 63 65 REATE or replace
0720: 20 46 55 4e 43 54 49 4f 4e 20 74 72 69 70 64 6f FUNCTION tripdo
0730: 6d 61 69 6e 28 75 72 6c 20 74 65 78 74 29 20 52 main(url text) R
0740: 45 54 55 52 4e 53 20 74 65 78 74 5b 5d 0a 09 4c ETURNS text[]..L
0750: 41 4e 47 55 41 47 45 20 70 6c 70 67 73 71 6c 20 ANGUAGE plpgsql
0760: 49 4d 4d 55 54 41 42 4c 45 20 53 54 52 49 43 54 IMMUTABLE STRICT
0770: 0a 09 41 53 20 24 5f 24 0a 64 65 63 6c 61 72 65 ..AS $_$.declare
0780: 0a 09 72 65 73 75 6c 74 20 74 65 78 74 5b 5d 3b ..result text[];
0790: 0a 09 73 70 6c 69 74 74 65 64 20 74 65 78 74 5b ..splitted text[
07a0: 5d 3b 0a 09 78 20 69 6e 74 65 67 65 72 3b 0a 09 ];..x integer;..
07b0: 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 3b 0a length integer;.
07c0: 62 65 67 69 6e 0a 09 73 70 6c 69 74 74 65 64 20 begin..splitted
07d0: 3a 3d 20 73 74 72 69 6e 67 5f 74 6f 5f 61 72 72 := string_to_arr
07e0: 61 79 28 24 31 2c 20 27 2e 27 29 3b 0a 09 6c 65 ay($1, '.');..le
07f0: 6e 67 74 68 20 3a 3d 20 61 72 72 61 79 5f 6c 65 ngth := array_le
0800: 6e 67 74 68 28 73 70 6c 69 74 74 65 64 2c 20 31 ngth(splitted, 1
0810: 29 3b 0a 09 78 20 3a 3d 20 31 3b 0a 09 6c 6f 6f );..x := 1;..loo
0820: 70 0a 09 09 65 78 69 74 20 77 68 65 6e 20 73 70 p...exit when sp
0830: 6c 69 74 74 65 64 5b 78 5d 20 69 73 20 6e 75 6c litted[x] is nul
0840: 6c 3b 0a 09 09 72 65 73 75 6c 74 5b 78 5d 20 3a l;...result[x] :
0850: 3d 20 73 70 6c 69 74 74 65 64 5b 78 5d 20 7c 7c = splitted[x] ||
0860: 20 27 3a 27 20 7c 7c 20 6c 65 6e 67 74 68 20 2d ':' || length -
0870: 20 78 3b 0a 09 09 78 20 3a 3d 20 78 20 2b 20 31 x;...x := x + 1
0880: 3b 0a 09 65 6e 64 20 6c 6f 6f 70 3b 0a 09 72 65 ;..end loop;..re
0890: 74 75 72 6e 20 72 65 73 75 6c 74 3b 0a 65 6e 64 turn result;.end
08a0: 3b 24 5f 24 3b 0a 0a 2d 2d 20 74 72 61 6e 73 66 ;$_$;..-- transf
08b0: 6f 72 6d 73 20 6f 72 64 65 72 65 64 20 61 72 72 orms ordered arr
08c0: 61 79 20 69 6e 74 6f 20 64 6f 6d 61 69 6e 0a 63 ay into domain.c
08d0: 72 65 61 74 65 20 6f 72 20 72 65 70 6c 61 63 65 reate or replace
08e0: 20 66 75 6e 63 74 69 6f 6e 20 75 6e 74 72 69 70 function untrip
08f0: 28 73 69 74 65 20 74 65 78 74 5b 5d 29 20 72 65 (site text[]) re
0900: 74 75 72 6e 73 20 74 65 78 74 0a 09 6c 61 6e 67 turns text..lang
0910: 75 61 67 65 20 70 6c 70 67 73 71 6c 20 69 6d 6d uage plpgsql imm
0920: 75 74 61 62 6c 65 20 73 74 72 69 63 74 0a 09 61 utable strict..a
0930: 73 20 24 5f 24 0a 64 65 63 6c 61 72 65 0a 09 78 s $_$.declare..x
0940: 20 69 6e 74 65 67 65 72 3b 0a 09 73 70 6c 69 74 integer;..split
0950: 74 65 64 20 74 65 78 74 5b 5d 3b 0a 09 70 61 69 ted text[];..pai
0960: 72 20 74 65 78 74 5b 5d 3b 0a 62 65 67 69 6e 0a r text[];.begin.
0970: 09 78 20 3a 3d 20 61 72 72 61 79 5f 6c 65 6e 67 .x := array_leng
0980: 74 68 28 73 69 74 65 2c 20 31 29 3b 0a 09 6c 6f th(site, 1);..lo
0990: 6f 70 20 0a 09 09 65 78 69 74 20 77 68 65 6e 20 op ...exit when
09a0: 73 69 74 65 5b 78 5d 20 69 73 20 6e 75 6c 6c 3b site[x] is null;
09b0: 0a 09 09 70 61 69 72 20 3a 3d 20 73 74 72 69 6e ...pair := strin
09c0: 67 5f 74 6f 5f 61 72 72 61 79 28 73 69 74 65 5b g_to_array(site[
09d0: 78 5d 2c 20 27 3a 27 29 3b 0a 09 09 73 70 6c 69 x], ':');...spli
09e0: 74 74 65 64 5b 30 20 2d 20 70 61 69 72 5b 32 5d tted[0 - pair[2]
09f0: 3a 3a 69 6e 74 65 67 65 72 5d 20 3a 3d 20 70 61 ::integer] := pa
0a00: 69 72 5b 31 5d 3b 0a 09 09 78 20 3a 3d 20 78 20 ir[1];...x := x
0a10: 2d 20 31 3b 0a 09 65 6e 64 20 6c 6f 6f 70 3b 0a - 1;..end loop;.
0a20: 09 72 65 74 75 72 6e 20 61 72 72 61 79 5f 74 6f .return array_to
0a30: 5f 73 74 72 69 6e 67 28 73 70 6c 69 74 74 65 64 _string(splitted
0a40: 2c 20 27 2e 27 29 3b 0a 65 6e 64 3b 0a 24 5f 24 , '.');.end;.$_$
0a50: 3b 0a ;.