0000: 2d 2d 20 67 65 6e 65 72 61 6c 20 64 61 74 61 62 -- general datab
0010: 61 73 65 20 73 74 72 75 63 74 75 72 65 0a 2d 2d ase structure.--
0020: 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 61 table to hold a
0030: 6c 6c 20 72 75 6c 65 73 0a 43 52 45 41 54 45 20 ll rules.CREATE
0040: 54 41 42 4c 45 20 72 75 6c 65 73 20 28 0a 09 6e TABLE rules (..n
0050: 65 74 6d 61 73 6b 20 63 69 64 72 20 4e 4f 54 20 etmask cidr NOT
0060: 4e 55 4c 4c 2c 0a 09 72 65 64 69 72 65 63 74 5f NULL,..redirect_
0070: 75 72 6c 20 74 65 78 74 20 44 45 46 41 55 4c 54 url text DEFAULT
0080: 20 27 61 62 6f 75 74 3a 3a 62 6c 61 6e 6b 27 3a 'about::blank':
0090: 3a 74 65 78 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a :text NOT NULL,.
00a0: 09 66 72 6f 6d 5f 77 65 65 6b 64 61 79 20 73 6d .from_weekday sm
00b0: 61 6c 6c 69 6e 74 20 44 45 46 41 55 4c 54 20 30 allint DEFAULT 0
00c0: 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 77 NOT NULL,..to_w
00d0: 65 65 6b 64 61 79 20 73 6d 61 6c 6c 69 6e 74 20 eekday smallint
00e0: 44 45 46 41 55 4c 54 20 36 20 4e 4f 54 20 4e 55 DEFAULT 6 NOT NU
00f0: 4c 4c 2c 0a 09 66 72 6f 6d 5f 74 69 6d 65 20 74 LL,..from_time t
0100: 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65 ime without time
0110: 20 7a 6f 6e 65 20 44 45 46 41 55 4c 54 20 27 30 zone DEFAULT '0
0120: 30 3a 30 30 3a 30 30 27 3a 3a 74 69 6d 65 20 77 0:00:00'::time w
0130: 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e 65 ithout time zone
0140: 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 74 6f 5f 74 NOT NULL,..to_t
0150: 69 6d 65 20 74 69 6d 65 20 77 69 74 68 6f 75 74 ime time without
0160: 20 74 69 6d 65 20 7a 6f 6e 65 20 44 45 46 41 55 time zone DEFAU
0170: 4c 54 20 27 32 33 3a 35 39 3a 35 39 27 3a 3a 74 LT '23:59:59'::t
0180: 69 6d 65 20 77 69 74 68 6f 75 74 20 74 69 6d 65 ime without time
0190: 20 7a 6f 6e 65 20 4e 4f 54 20 4e 55 4c 4c 2c 0a zone NOT NULL,.
01a0: 09 69 64 5f 74 61 67 20 73 6d 61 6c 6c 69 6e 74 .id_tag smallint
01b0: 20 4e 4f 54 20 4e 55 4c 4c 0a 29 3b 0a 0a 41 4c NOT NULL.);..AL
01c0: 54 45 52 20 54 41 42 4c 45 20 4f 4e 4c 59 20 72 TER TABLE ONLY r
01d0: 75 6c 65 73 0a 09 41 44 44 20 43 4f 4e 53 54 52 ules..ADD CONSTR
01e0: 41 49 4e 54 20 72 75 6c 65 73 5f 74 61 67 5f 66 AINT rules_tag_f
01f0: 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 28 69 64 FOREIGN KEY (id
0200: 5f 74 61 67 29 20 52 45 46 45 52 45 4e 43 45 53 _tag) REFERENCES
0210: 20 74 61 67 28 69 64 5f 74 61 67 29 20 4d 41 54 tag(id_tag) MAT
0220: 43 48 20 46 55 4c 4c 0a 09 4f 4e 20 55 50 44 41 CH FULL..ON UPDA
0230: 54 45 20 52 45 53 54 52 49 43 54 20 4f 4e 20 44 TE RESTRICT ON D
0240: 45 4c 45 54 45 20 52 45 53 54 52 49 43 54 20 44 ELETE RESTRICT D
0250: 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 EFERRABLE INITIA
0260: 4c 4c 59 20 44 45 46 45 52 52 45 44 3b 0a 0a 2d LLY DEFERRED;..-
0270: 2d 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 - table to hold
0280: 73 69 74 65 20 61 72 72 61 79 73 0a 43 52 45 41 site arrays.CREA
0290: 54 45 20 54 41 42 4c 45 20 73 69 74 65 20 28 0a TE TABLE site (.
02a0: 09 69 64 5f 73 69 74 65 20 73 65 72 69 61 6c 2c .id_site serial,
02b0: 0a 09 73 69 74 65 20 74 65 78 74 5b 5d 20 4e 4f ..site text[] NO
02c0: 54 20 4e 55 4c 4c 0a 29 3b 0a 0a 41 4c 54 45 52 T NULL.);..ALTER
02d0: 20 54 41 42 4c 45 20 4f 4e 4c 59 20 73 69 74 65 TABLE ONLY site
02e0: 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 ..ADD CONSTRAINT
02f0: 20 73 69 74 65 5f 70 6b 65 79 20 50 52 49 4d 41 site_pkey PRIMA
0300: 52 59 20 4b 45 59 20 28 69 64 5f 73 69 74 65 29 RY KEY (id_site)
0310: 3b 0a 0a 43 52 45 41 54 45 20 55 4e 49 51 55 45 ;..CREATE UNIQUE
0320: 20 49 4e 44 45 58 20 73 69 74 65 5f 75 20 4f 4e INDEX site_u ON
0330: 20 73 69 74 65 20 28 75 73 6f 72 74 28 73 69 74 site (usort(sit
0340: 65 29 29 3b 0a 0a 43 52 45 41 54 45 20 49 4e 44 e));..CREATE IND
0350: 45 58 20 73 69 74 65 5f 67 20 4f 4e 20 73 69 74 EX site_g ON sit
0360: 65 20 55 53 49 4e 47 20 67 69 6e 20 28 73 69 74 e USING gin (sit
0370: 65 29 3b 0a 0a 2d 2d 20 74 61 62 6c 65 20 74 6f e);..-- table to
0380: 20 68 6f 6c 64 20 74 61 67 20 63 6f 6d 62 69 6e hold tag combin
0390: 61 74 69 6f 6e 73 0a 43 52 45 41 54 45 20 54 41 ations.CREATE TA
03a0: 42 4c 45 20 74 61 67 20 28 0a 09 69 64 5f 74 61 BLE tag (..id_ta
03b0: 67 20 73 65 72 69 61 6c 2c 0a 09 74 61 67 20 74 g serial,..tag t
03c0: 65 78 74 5b 5d 20 4e 4f 54 20 4e 55 4c 4c 0a 29 ext[] NOT NULL.)
03d0: 3b 0a 0a 41 4c 54 45 52 20 54 41 42 4c 45 20 4f ;..ALTER TABLE O
03e0: 4e 4c 59 20 74 61 67 0a 09 41 44 44 20 43 4f 4e NLY tag..ADD CON
03f0: 53 54 52 41 49 4e 54 20 74 61 67 5f 70 6b 65 79 STRAINT tag_pkey
0400: 20 50 52 49 4d 41 52 59 20 4b 45 59 20 28 69 64 PRIMARY KEY (id
0410: 5f 74 61 67 29 3b 0a 0a 43 52 45 41 54 45 20 55 _tag);..CREATE U
0420: 4e 49 51 55 45 20 49 4e 44 45 58 20 74 61 67 5f NIQUE INDEX tag_
0430: 75 20 4f 4e 20 74 61 67 20 28 75 73 6f 72 74 28 u ON tag (usort(
0440: 74 61 67 29 29 3b 0a 0a 43 52 45 41 54 45 20 49 tag));..CREATE I
0450: 4e 44 45 58 20 74 61 67 5f 67 20 4f 4e 20 74 61 NDEX tag_g ON ta
0460: 67 20 55 53 49 4e 47 20 67 69 6e 20 28 74 61 67 g USING gin (tag
0470: 29 3b 0a 0a 2d 2d 20 74 61 62 6c 65 20 74 6f 20 );..-- table to
0480: 68 6f 6c 64 20 74 61 67 20 2d 20 73 69 74 65 20 hold tag - site
0490: 6c 69 6e 6b 73 0a 43 52 45 41 54 45 20 54 41 42 links.CREATE TAB
04a0: 4c 45 20 75 72 6c 73 20 28 0a 09 64 61 74 65 5f LE urls (..date_
04b0: 61 64 64 65 64 20 74 69 6d 65 73 74 61 6d 70 20 added timestamp
04c0: 77 69 74 68 6f 75 74 20 74 69 6d 65 20 7a 6f 6e without time zon
04d0: 65 20 44 45 46 41 55 4c 54 20 28 27 6e 6f 77 27 e DEFAULT ('now'
04e0: 3a 3a 74 65 78 74 29 3a 3a 74 69 6d 65 73 74 61 ::text)::timesta
04f0: 6d 70 28 30 29 20 77 69 74 68 6f 75 74 20 74 69 mp(0) without ti
0500: 6d 65 20 7a 6f 6e 65 20 4e 4f 54 20 4e 55 4c 4c me zone NOT NULL
0510: 2c 0a 09 69 64 5f 73 69 74 65 20 73 6d 61 6c 6c ,..id_site small
0520: 69 6e 74 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 09 69 int NOT NULL,..i
0530: 64 5f 74 61 67 20 73 6d 61 6c 6c 69 6e 74 20 4e d_tag smallint N
0540: 4f 54 20 4e 55 4c 4c 2c 0a 09 72 65 67 65 78 70 OT NULL,..regexp
0550: 20 74 65 78 74 0a 29 3b 0a 0a 43 52 45 41 54 45 text.);..CREATE
0560: 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 75 72 UNIQUE INDEX ur
0570: 6c 73 5f 70 6b 65 79 20 4f 4e 20 75 72 6c 73 20 ls_pkey ON urls
0580: 55 53 49 4e 47 20 62 74 72 65 65 20 28 69 64 5f USING btree (id_
0590: 73 69 74 65 2c 20 72 65 67 65 78 70 29 3b 0a 0a site, regexp);..
05a0: 43 52 45 41 54 45 20 49 4e 44 45 58 20 75 72 6c CREATE INDEX url
05b0: 73 5f 69 64 5f 74 61 67 20 4f 4e 20 75 72 6c 73 s_id_tag ON urls
05c0: 20 55 53 49 4e 47 20 62 74 72 65 65 20 28 69 64 USING btree (id
05d0: 5f 74 61 67 29 3b 0a 0a 41 4c 54 45 52 20 54 41 _tag);..ALTER TA
05e0: 42 4c 45 20 4f 4e 4c 59 20 75 72 6c 73 0a 09 41 BLE ONLY urls..A
05f0: 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 20 75 72 DD CONSTRAINT ur
0600: 6c 73 5f 73 69 74 65 5f 66 20 46 4f 52 45 49 47 ls_site_f FOREIG
0610: 4e 20 4b 45 59 20 28 69 64 5f 73 69 74 65 29 20 N KEY (id_site)
0620: 52 45 46 45 52 45 4e 43 45 53 20 73 69 74 65 28 REFERENCES site(
0630: 69 64 5f 73 69 74 65 29 20 4d 41 54 43 48 20 46 id_site) MATCH F
0640: 55 4c 4c 0a 09 4f 4e 20 55 50 44 41 54 45 20 52 ULL..ON UPDATE R
0650: 45 53 54 52 49 43 54 20 4f 4e 20 44 45 4c 45 54 ESTRICT ON DELET
0660: 45 20 52 45 53 54 52 49 43 54 20 44 45 46 45 52 E RESTRICT DEFER
0670: 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 RABLE INITIALLY
0680: 44 45 46 45 52 52 45 44 3b 0a 0a 41 4c 54 45 52 DEFERRED;..ALTER
0690: 20 54 41 42 4c 45 20 4f 4e 4c 59 20 75 72 6c 73 TABLE ONLY urls
06a0: 0a 09 41 44 44 20 43 4f 4e 53 54 52 41 49 4e 54 ..ADD CONSTRAINT
06b0: 20 75 72 6c 73 5f 74 61 67 5f 66 20 46 4f 52 45 urls_tag_f FORE
06c0: 49 47 4e 20 4b 45 59 20 28 69 64 5f 74 61 67 29 IGN KEY (id_tag)
06d0: 20 52 45 46 45 52 45 4e 43 45 53 20 74 61 67 28 REFERENCES tag(
06e0: 69 64 5f 74 61 67 29 20 4d 41 54 43 48 20 46 55 id_tag) MATCH FU
06f0: 4c 4c 0a 09 4f 4e 20 55 50 44 41 54 45 20 52 45 LL..ON UPDATE RE
0700: 53 54 52 49 43 54 20 4f 4e 20 44 45 4c 45 54 45 STRICT ON DELETE
0710: 20 52 45 53 54 52 49 43 54 20 44 45 46 45 52 52 RESTRICT DEFERR
0720: 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 ABLE INITIALLY D
0730: 45 46 45 52 52 45 44 3b 0a 0a 2d 2d 20 72 75 6c EFERRED;..-- rul
0740: 65 20 74 6f 20 6a 6f 69 6e 20 61 6c 6c 20 74 61 e to join all ta
0750: 62 6c 65 73 20 69 6e 74 6f 20 6f 6e 65 20 74 6f bles into one to
0760: 20 73 69 6d 70 6c 69 66 79 20 61 63 63 65 73 73 simplify access
0770: 0a 2d 2d 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c .-- automaticall
0780: 20 75 73 65 73 20 63 75 72 72 65 6e 74 20 64 61 uses current da
0790: 79 20 61 6e 64 20 74 69 6d 65 20 64 61 74 61 0a y and time data.
07a0: 43 52 45 41 54 45 20 56 49 45 57 20 73 69 74 65 CREATE VIEW site
07b0: 5f 72 75 6c 65 20 41 53 0a 53 45 4c 45 43 54 20 _rule AS.SELECT
07c0: 61 2e 72 65 64 69 72 65 63 74 5f 75 72 6c 2c 20 a.redirect_url,
07d0: 61 2e 6e 65 74 6d 61 73 6b 2c 20 62 2e 73 69 74 a.netmask, b.sit
07e0: 65 2c 20 62 2e 72 65 67 65 78 70 0a 46 52 4f 4d e, b.regexp.FROM
07f0: 20 28 28 0a 09 53 45 4c 45 43 54 20 72 75 6c 65 ((..SELECT rule
0800: 73 2e 72 65 64 69 72 65 63 74 5f 75 72 6c 2c 20 s.redirect_url,
0810: 74 61 67 2e 74 61 67 20 41 53 20 72 75 6c 65 5f tag.tag AS rule_
0820: 74 61 67 2c 20 72 75 6c 65 73 2e 6e 65 74 6d 61 tag, rules.netma
0830: 73 6b 0a 09 46 52 4f 4d 20 72 75 6c 65 73 20 4e sk..FROM rules N
0840: 41 54 55 52 41 4c 20 4a 4f 49 4e 20 74 61 67 0a ATURAL JOIN tag.
0850: 09 57 48 45 52 45 20 28 27 6e 6f 77 27 3a 3a 74 .WHERE ('now'::t
0860: 65 78 74 29 3a 3a 74 69 6d 65 20 77 69 74 68 6f ext)::time witho
0870: 75 74 20 74 69 6d 65 20 7a 6f 6e 65 20 3e 3d 20 ut time zone >=
0880: 72 75 6c 65 73 2e 66 72 6f 6d 5f 74 69 6d 65 0a rules.from_time.
0890: 09 09 41 4e 44 20 28 27 6e 6f 77 27 3a 3a 74 65 ..AND ('now'::te
08a0: 78 74 29 3a 3a 74 69 6d 65 20 77 69 74 68 6f 75 xt)::time withou
08b0: 74 20 74 69 6d 65 20 7a 6f 6e 65 20 3c 3d 20 72 t time zone <= r
08c0: 75 6c 65 73 2e 74 6f 5f 74 69 6d 65 0a 09 09 41 ules.to_time...A
08d0: 4e 44 20 64 61 74 65 5f 70 61 72 74 28 27 64 6f ND date_part('do
08e0: 77 27 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 29 29 w'::text, now())
08f0: 20 3e 3d 20 28 72 75 6c 65 73 2e 66 72 6f 6d 5f >= (rules.from_
0900: 77 65 65 6b 64 61 79 29 3a 3a 64 6f 75 62 6c 65 weekday)::double
0910: 20 70 72 65 63 69 73 69 6f 6e 0a 09 09 41 4e 44 precision...AND
0920: 20 64 61 74 65 5f 70 61 72 74 28 27 64 6f 77 27 date_part('dow'
0930: 3a 3a 74 65 78 74 2c 20 6e 6f 77 28 29 29 20 3c ::text, now()) <
0940: 3d 20 28 72 75 6c 65 73 2e 74 6f 5f 77 65 65 6b = (rules.to_week
0950: 64 61 79 29 3a 3a 64 6f 75 62 6c 65 20 70 72 65 day)::double pre
0960: 63 69 73 69 6f 6e 0a 29 20 61 20 4a 4f 49 4e 20 cision.) a JOIN
0970: 28 0a 09 53 45 4c 45 43 54 20 73 69 74 65 2e 73 (..SELECT site.s
0980: 69 74 65 2c 20 74 61 67 2e 74 61 67 20 41 53 20 ite, tag.tag AS
0990: 75 72 6c 5f 74 61 67 2c 20 72 65 67 65 78 70 0a url_tag, regexp.
09a0: 09 46 52 4f 4d 20 75 72 6c 73 20 4e 41 54 55 52 .FROM urls NATUR
09b0: 41 4c 20 4a 4f 49 4e 20 74 61 67 20 4e 41 54 55 AL JOIN tag NATU
09c0: 52 41 4c 20 4a 4f 49 4e 20 73 69 74 65 0a 29 20 RAL JOIN site.)
09d0: 62 20 4f 4e 20 28 62 2e 75 72 6c 5f 74 61 67 20 b ON (b.url_tag
09e0: 26 26 20 61 2e 72 75 6c 65 5f 74 61 67 29 29 3b && a.rule_tag));
09f0: 0a 0a 43 52 45 41 54 45 20 50 52 4f 43 45 44 55 ..CREATE PROCEDU
0a00: 52 41 4c 20 4c 41 4e 47 55 41 47 45 20 70 6c 70 RAL LANGUAGE plp
0a10: 67 73 71 6c 3b 0a 0a 2d 2d 20 67 65 6e 65 72 61 gsql;..-- genera
0a20: 6c 20 61 72 72 61 79 20 73 6f 72 74 69 6e 67 20 l array sorting
0a30: 61 6e 64 20 64 6f 6d 61 69 6e 20 70 72 6f 63 65 and domain proce
0a40: 73 73 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 0a ssing functions.
0a50: 2d 2d 20 73 6f 72 74 73 20 61 72 72 61 79 0a 43 -- sorts array.C
0a60: 52 45 41 54 45 20 6f 72 20 72 65 70 6c 61 63 65 REATE or replace
0a70: 20 46 55 4e 43 54 49 4f 4e 20 73 6f 72 74 28 6f FUNCTION sort(o
0a80: 72 69 67 69 6e 61 6c 20 61 6e 79 61 72 72 61 79 riginal anyarray
0a90: 29 20 52 45 54 55 52 4e 53 20 61 6e 79 61 72 72 ) RETURNS anyarr
0aa0: 61 79 0a 09 4c 41 4e 47 55 41 47 45 20 73 71 6c ay..LANGUAGE sql
0ab0: 20 49 4d 4d 55 54 41 42 4c 45 20 53 54 52 49 43 IMMUTABLE STRIC
0ac0: 54 0a 09 41 53 20 24 5f 24 0a 73 65 6c 65 63 74 T..AS $_$.select
0ad0: 20 61 72 72 61 79 5f 61 67 67 28 69 74 65 6d 29 array_agg(item)
0ae0: 20 61 73 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 as result from
0af0: 28 73 65 6c 65 63 74 20 75 6e 6e 65 73 74 28 24 (select unnest($
0b00: 31 29 20 61 73 20 69 74 65 6d 20 6f 72 64 65 72 1) as item order
0b10: 20 62 79 20 69 74 65 6d 29 20 61 3b 0a 24 5f 24 by item) a;.$_$
0b20: 3b 0a 0a 2d 2d 20 73 6f 72 74 73 20 61 72 72 61 ;..-- sorts arra
0b30: 79 20 61 6e 64 20 72 65 6d 6f 76 65 73 20 64 75 y and removes du
0b40: 70 6c 69 63 61 74 65 73 0a 43 52 45 41 54 45 20 plicates.CREATE
0b50: 6f 72 20 72 65 70 6c 61 63 65 20 46 55 4e 43 54 or replace FUNCT
0b60: 49 4f 4e 20 75 73 6f 72 74 28 6f 72 69 67 69 6e ION usort(origin
0b70: 61 6c 20 61 6e 79 61 72 72 61 79 29 20 52 45 54 al anyarray) RET
0b80: 55 52 4e 53 20 61 6e 79 61 72 72 61 79 0a 09 4c URNS anyarray..L
0b90: 41 4e 47 55 41 47 45 20 73 71 6c 20 49 4d 4d 55 ANGUAGE sql IMMU
0ba0: 54 41 42 4c 45 20 53 54 52 49 43 54 0a 09 41 53 TABLE STRICT..AS
0bb0: 20 24 5f 24 0a 73 65 6c 65 63 74 20 61 72 72 61 $_$.select arra
0bc0: 79 5f 61 67 67 28 69 74 65 6d 29 20 61 73 20 72 y_agg(item) as r
0bd0: 65 73 75 6c 74 20 66 72 6f 6d 20 28 73 65 6c 65 esult from (sele
0be0: 63 74 20 64 69 73 74 69 6e 63 74 20 75 6e 6e 65 ct distinct unne
0bf0: 73 74 28 24 31 29 20 61 73 20 69 74 65 6d 20 6f st($1) as item o
0c00: 72 64 65 72 20 62 79 20 69 74 65 6d 29 20 61 3b rder by item) a;
0c10: 0a 24 5f 24 3b 0a 0a 2d 2d 20 74 72 61 6e 73 66 .$_$;..-- transf
0c20: 6f 72 6d 73 20 64 6f 6d 61 69 6e 20 69 6e 74 6f orms domain into
0c30: 20 6f 72 64 65 72 65 64 20 61 72 72 61 79 20 66 ordered array f
0c40: 6f 72 20 69 6e 64 65 78 69 6e 67 0a 43 52 45 41 or indexing.CREA
0c50: 54 45 20 6f 72 20 72 65 70 6c 61 63 65 20 46 55 TE or replace FU
0c60: 4e 43 54 49 4f 4e 20 74 72 69 70 64 6f 6d 61 69 NCTION tripdomai
0c70: 6e 28 75 72 6c 20 74 65 78 74 29 20 52 45 54 55 n(url text) RETU
0c80: 52 4e 53 20 74 65 78 74 5b 5d 0a 09 4c 41 4e 47 RNS text[]..LANG
0c90: 55 41 47 45 20 70 6c 70 67 73 71 6c 20 49 4d 4d UAGE plpgsql IMM
0ca0: 55 54 41 42 4c 45 20 53 54 52 49 43 54 0a 09 41 UTABLE STRICT..A
0cb0: 53 20 24 5f 24 0a 64 65 63 6c 61 72 65 0a 09 72 S $_$.declare..r
0cc0: 65 73 75 6c 74 20 74 65 78 74 5b 5d 3b 0a 09 73 esult text[];..s
0cd0: 70 6c 69 74 74 65 64 20 74 65 78 74 5b 5d 3b 0a plitted text[];.
0ce0: 09 78 20 69 6e 74 65 67 65 72 3b 0a 09 6c 65 6e .x integer;..len
0cf0: 67 74 68 20 69 6e 74 65 67 65 72 3b 0a 62 65 67 gth integer;.beg
0d00: 69 6e 0a 09 73 70 6c 69 74 74 65 64 20 3a 3d 20 in..splitted :=
0d10: 73 74 72 69 6e 67 5f 74 6f 5f 61 72 72 61 79 28 string_to_array(
0d20: 24 31 2c 20 27 2e 27 29 3b 0a 09 6c 65 6e 67 74 $1, '.');..lengt
0d30: 68 20 3a 3d 20 61 72 72 61 79 5f 6c 65 6e 67 74 h := array_lengt
0d40: 68 28 73 70 6c 69 74 74 65 64 2c 20 31 29 3b 0a h(splitted, 1);.
0d50: 09 78 20 3a 3d 20 31 3b 0a 09 6c 6f 6f 70 0a 09 .x := 1;..loop..
0d60: 09 65 78 69 74 20 77 68 65 6e 20 73 70 6c 69 74 .exit when split
0d70: 74 65 64 5b 78 5d 20 69 73 20 6e 75 6c 6c 3b 0a ted[x] is null;.
0d80: 09 09 72 65 73 75 6c 74 5b 78 5d 20 3a 3d 20 73 ..result[x] := s
0d90: 70 6c 69 74 74 65 64 5b 78 5d 20 7c 7c 20 27 3a plitted[x] || ':
0da0: 27 20 7c 7c 20 6c 65 6e 67 74 68 20 2d 20 78 3b ' || length - x;
0db0: 0a 09 09 78 20 3a 3d 20 78 20 2b 20 31 3b 0a 09 ...x := x + 1;..
0dc0: 65 6e 64 20 6c 6f 6f 70 3b 0a 09 72 65 74 75 72 end loop;..retur
0dd0: 6e 20 72 65 73 75 6c 74 3b 0a 65 6e 64 3b 24 5f n result;.end;$_
0de0: 24 3b 0a 0a 2d 2d 20 74 72 61 6e 73 66 6f 72 6d $;..-- transform
0df0: 73 20 6f 72 64 65 72 65 64 20 61 72 72 61 79 20 s ordered array
0e00: 69 6e 74 6f 20 64 6f 6d 61 69 6e 0a 63 72 65 61 into domain.crea
0e10: 74 65 20 6f 72 20 72 65 70 6c 61 63 65 20 66 75 te or replace fu
0e20: 6e 63 74 69 6f 6e 20 75 6e 74 72 69 70 28 73 69 nction untrip(si
0e30: 74 65 20 74 65 78 74 5b 5d 29 20 72 65 74 75 72 te text[]) retur
0e40: 6e 73 20 74 65 78 74 0a 09 6c 61 6e 67 75 61 67 ns text..languag
0e50: 65 20 70 6c 70 67 73 71 6c 20 69 6d 6d 75 74 61 e plpgsql immuta
0e60: 62 6c 65 20 73 74 72 69 63 74 0a 09 61 73 20 24 ble strict..as $
0e70: 5f 24 0a 64 65 63 6c 61 72 65 0a 09 78 20 69 6e _$.declare..x in
0e80: 74 65 67 65 72 3b 0a 09 73 70 6c 69 74 74 65 64 teger;..splitted
0e90: 20 74 65 78 74 5b 5d 3b 0a 09 70 61 69 72 20 74 text[];..pair t
0ea0: 65 78 74 5b 5d 3b 0a 62 65 67 69 6e 0a 09 78 20 ext[];.begin..x
0eb0: 3a 3d 20 61 72 72 61 79 5f 6c 65 6e 67 74 68 28 := array_length(
0ec0: 73 69 74 65 2c 20 31 29 3b 0a 09 6c 6f 6f 70 20 site, 1);..loop
0ed0: 0a 09 09 65 78 69 74 20 77 68 65 6e 20 73 69 74 ...exit when sit
0ee0: 65 5b 78 5d 20 69 73 20 6e 75 6c 6c 3b 0a 09 09 e[x] is null;...
0ef0: 70 61 69 72 20 3a 3d 20 73 74 72 69 6e 67 5f 74 pair := string_t
0f00: 6f 5f 61 72 72 61 79 28 73 69 74 65 5b 78 5d 2c o_array(site[x],
0f10: 20 27 3a 27 29 3b 0a 09 09 73 70 6c 69 74 74 65 ':');...splitte
0f20: 64 5b 30 20 2d 20 70 61 69 72 5b 32 5d 3a 3a 69 d[0 - pair[2]::i
0f30: 6e 74 65 67 65 72 5d 20 3a 3d 20 70 61 69 72 5b nteger] := pair[
0f40: 31 5d 3b 0a 09 09 78 20 3a 3d 20 78 20 2d 20 31 1];...x := x - 1
0f50: 3b 0a 09 65 6e 64 20 6c 6f 6f 70 3b 0a 09 72 65 ;..end loop;..re
0f60: 74 75 72 6e 20 61 72 72 61 79 5f 74 6f 5f 73 74 turn array_to_st
0f70: 72 69 6e 67 28 73 70 6c 69 74 74 65 64 2c 20 27 ring(splitted, '
0f80: 2e 27 29 3b 0a 65 6e 64 3b 0a 24 5f 24 3b 0a 0a .');.end;.$_$;..
0f90: 2d 2d 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 -- functions tha
0fa0: 74 20 77 6f 72 6b 73 20 77 69 74 68 20 74 61 62 t works with tab
0fb0: 6c 65 73 0a 2d 2d 20 74 68 69 73 20 66 75 6e 63 les.-- this func
0fc0: 74 69 6f 6e 73 20 72 65 74 75 72 6e 73 20 69 64 tions returns id
0fd0: 20 6f 66 20 73 69 74 65 0a 63 72 65 61 74 65 20 of site.create
0fe0: 6f 72 20 72 65 70 6c 61 63 65 20 66 75 6e 63 74 or replace funct
0ff0: 69 6f 6e 20 67 65 74 5f 73 69 74 65 28 6d 79 5f ion get_site(my_
1000: 73 69 74 65 20 74 65 78 74 5b 5d 29 20 72 65 74 site text[]) ret
1010: 75 72 6e 73 20 69 6e 74 65 67 65 72 0a 09 6c 61 urns integer..la
1020: 6e 67 75 61 67 65 20 70 6c 70 67 73 71 6c 20 73 nguage plpgsql s
1030: 74 72 69 63 74 0a 09 61 73 20 24 24 0a 64 65 63 trict..as $$.dec
1040: 6c 61 72 65 0a 09 73 69 74 65 5f 69 64 20 69 6e lare..site_id in
1050: 74 65 67 65 72 3b 0a 62 65 67 69 6e 0a 09 73 65 teger;.begin..se
1060: 6c 65 63 74 20 69 64 5f 73 69 74 65 20 66 72 6f lect id_site fro
1070: 6d 20 73 69 74 65 20 77 68 65 72 65 20 6d 79 5f m site where my_
1080: 73 69 74 65 20 3d 20 73 69 74 65 20 69 6e 74 6f site = site into
1090: 20 73 69 74 65 5f 69 64 3b 0a 09 69 66 20 6e 6f site_id;..if no
10a0: 74 20 66 6f 75 6e 64 20 74 68 65 6e 0a 09 09 69 t found then...i
10b0: 6e 73 65 72 74 20 69 6e 74 6f 20 73 69 74 65 20 nsert into site
10c0: 28 73 69 74 65 29 20 76 61 6c 75 65 73 20 28 6d (site) values (m
10d0: 79 5f 73 69 74 65 29 3b 0a 09 09 73 65 6c 65 63 y_site);...selec
10e0: 74 20 69 64 5f 73 69 74 65 20 66 72 6f 6d 20 73 t id_site from s
10f0: 69 74 65 20 77 68 65 72 65 20 6d 79 5f 73 69 74 ite where my_sit
1100: 65 20 3d 20 73 69 74 65 20 69 6e 74 6f 20 73 69 e = site into si
1110: 74 65 5f 69 64 3b 0a 09 65 6e 64 20 69 66 3b 0a te_id;..end if;.
1120: 09 72 65 74 75 72 6e 20 73 69 74 65 5f 69 64 3b .return site_id;
1130: 0a 65 6e 64 3b 0a 24 24 3b 0a 0a 63 72 65 61 74 .end;.$$;..creat
1140: 65 20 6f 72 20 72 65 70 6c 61 63 65 20 66 75 6e e or replace fun
1150: 63 74 69 6f 6e 20 67 65 74 5f 73 69 74 65 28 64 ction get_site(d
1160: 6f 6d 61 69 6e 20 74 65 78 74 29 20 72 65 74 75 omain text) retu
1170: 72 6e 73 20 69 6e 74 65 67 65 72 0a 09 6c 61 6e rns integer..lan
1180: 67 75 61 67 65 20 73 71 6c 20 69 6d 6d 75 74 61 guage sql immuta
1190: 62 6c 65 20 73 74 72 69 63 74 0a 09 61 73 20 24 ble strict..as $
11a0: 24 0a 73 65 6c 65 63 74 20 67 65 74 5f 73 69 74 $.select get_sit
11b0: 65 28 74 72 69 70 64 6f 6d 61 69 6e 28 24 31 29 e(tripdomain($1)
11c0: 29 20 61 73 20 72 65 73 75 6c 74 3b 0a 24 24 3b ) as result;.$$;
11d0: 0a 0a 2d 2d 20 74 68 69 73 20 66 75 6e 63 74 69 ..-- this functi
11e0: 6f 6e 20 69 6e 73 65 72 74 73 20 6f 72 20 75 70 on inserts or up
11f0: 64 61 74 65 73 20 72 65 63 6f 72 64 20 77 69 74 dates record wit
1200: 68 20 74 61 67 73 20 74 6f 20 73 69 74 65 20 62 h tags to site b
1210: 79 20 73 69 74 65 20 69 64 20 77 69 74 68 20 72 y site id with r
1220: 65 67 65 78 70 0a 43 52 45 41 54 45 20 6f 72 20 egexp.CREATE or
1230: 72 65 70 6c 61 63 65 20 46 55 4e 43 54 49 4f 4e replace FUNCTION
1240: 20 6d 61 72 6b 28 6d 79 5f 69 64 5f 73 69 74 65 mark(my_id_site
1250: 20 69 6e 74 65 67 65 72 2c 20 6d 79 5f 69 64 5f integer, my_id_
1260: 74 61 67 20 69 6e 74 65 67 65 72 2c 20 6d 79 5f tag integer, my_
1270: 72 65 67 65 78 70 20 74 65 78 74 29 20 52 45 54 regexp text) RET
1280: 55 52 4e 53 20 69 6e 74 65 67 65 72 0a 09 4c 41 URNS integer..LA
1290: 4e 47 55 41 47 45 20 70 6c 70 67 73 71 6c 20 53 NGUAGE plpgsql S
12a0: 54 52 49 43 54 0a 09 41 53 20 24 24 0a 64 65 63 TRICT..AS $$.dec
12b0: 6c 61 72 65 0a 09 2d 2d 20 6d 61 79 62 65 20 63 lare..-- maybe c
12c0: 68 65 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 61 heck should be a
12d0: 64 64 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 dded to make sur
12e0: 65 20 73 75 70 70 6c 69 65 64 20 73 69 74 65 20 e supplied site
12f0: 69 64 20 72 65 61 6c 6c 79 20 65 78 69 73 74 73 id really exists
1300: 0a 09 6d 79 5f 74 61 67 20 74 65 78 74 5b 5d 3b ..my_tag text[];
1310: 0a 62 65 67 69 6e 0a 09 2d 2d 20 73 65 6c 65 63 .begin..-- selec
1320: 74 69 6e 67 20 74 61 67 73 20 73 69 74 65 20 61 ting tags site a
1330: 6c 72 65 61 64 79 20 68 61 76 65 20 61 6e 64 20 lready have and
1340: 61 64 64 69 6e 67 20 6e 65 77 20 74 61 67 20 74 adding new tag t
1350: 6f 20 74 68 65 6d 0a 09 2d 2d 20 6e 6f 74 65 20 o them..-- note
1360: 74 68 61 74 20 74 61 67 73 20 73 68 6f 75 6c 64 that tags should
1370: 20 62 65 20 73 6f 72 74 65 64 20 74 6f 20 65 6c be sorted to el
1380: 69 6d 69 6e 61 74 65 20 70 65 72 6d 75 74 61 74 iminate permutat
1390: 69 6f 6e 73 0a 09 73 65 6c 65 63 74 20 63 6f 61 ions..select coa
13a0: 6c 65 73 63 65 28 74 61 67 2c 20 27 7b 7d 27 3a lesce(tag, '{}':
13b0: 3a 74 65 78 74 5b 5d 29 20 66 72 6f 6d 20 75 72 :text[]) from ur
13c0: 6c 73 20 6e 61 74 75 72 61 6c 20 6c 65 66 74 20 ls natural left
13d0: 6a 6f 69 6e 20 74 61 67 0a 09 09 77 68 65 72 65 join tag...where
13e0: 20 69 64 5f 73 69 74 65 20 3d 20 6d 79 5f 69 64 id_site = my_id
13f0: 5f 73 69 74 65 20 61 6e 64 20 72 65 67 65 78 70 _site and regexp
1400: 20 3d 20 6d 79 5f 72 65 67 65 78 70 20 69 6e 74 = my_regexp int
1410: 6f 20 6d 79 5f 74 61 67 3b 0a 09 69 66 20 6e 6f o my_tag;..if no
1420: 74 20 66 6f 75 6e 64 20 74 68 65 6e 0a 09 09 2d t found then...-
1430: 2d 20 6e 6f 20 72 65 63 6f 72 64 73 20 66 6f 75 - no records fou
1440: 6e 64 20 2d 20 63 72 65 61 74 69 6e 67 20 6e 65 nd - creating ne
1450: 77 20 74 61 67 0a 09 09 69 6e 73 65 72 74 20 69 w tag...insert i
1460: 6e 74 6f 20 75 72 6c 73 20 28 69 64 5f 73 69 74 nto urls (id_sit
1470: 65 2c 20 69 64 5f 74 61 67 2c 20 72 65 67 65 78 e, id_tag, regex
1480: 70 29 20 76 61 6c 75 65 73 20 28 6d 79 5f 69 64 p) values (my_id
1490: 5f 73 69 74 65 2c 20 6d 79 5f 69 64 5f 74 61 67 _site, my_id_tag
14a0: 2c 20 6d 79 5f 72 65 67 65 78 70 29 3b 0a 09 65 , my_regexp);..e
14b0: 6c 73 65 0a 09 09 2d 2d 20 6a 6f 69 6e 69 6e 67 lse...-- joining
14c0: 20 74 61 67 73 0a 09 09 73 65 6c 65 63 74 20 75 tags...select u
14d0: 73 6f 72 74 28 6d 79 5f 74 61 67 20 7c 7c 20 74 sort(my_tag || t
14e0: 61 67 29 20 66 72 6f 6d 20 74 61 67 20 77 68 65 ag) from tag whe
14f0: 72 65 20 69 64 5f 74 61 67 20 3d 20 6d 79 5f 69 re id_tag = my_i
1500: 64 5f 74 61 67 20 69 6e 74 6f 20 6d 79 5f 74 61 d_tag into my_ta
1510: 67 3b 0a 09 09 2d 2d 20 75 70 64 61 74 69 6e 67 g;...-- updating
1520: 20 65 78 69 73 74 69 6e 67 20 72 65 63 6f 72 64 existing record
1530: 0a 09 09 75 70 64 61 74 65 20 75 72 6c 73 20 73 ...update urls s
1540: 65 74 20 69 64 5f 74 61 67 20 3d 20 67 65 74 5f et id_tag = get_
1550: 74 61 67 28 6d 79 5f 74 61 67 29 0a 09 09 09 77 tag(my_tag)....w
1560: 68 65 72 65 20 69 64 5f 73 69 74 65 20 3d 20 6d here id_site = m
1570: 79 5f 69 64 5f 73 69 74 65 20 61 6e 64 20 72 65 y_id_site and re
1580: 67 65 78 70 20 3d 20 6d 79 5f 72 65 67 65 78 70 gexp = my_regexp
1590: 3b 0a 09 65 6e 64 20 69 66 3b 0a 09 72 65 74 75 ;..end if;..retu
15a0: 72 6e 20 6d 79 5f 69 64 5f 73 69 74 65 3b 0a 65 rn my_id_site;.e
15b0: 6e 64 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 nd;.$$;..-- this
15c0: 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 73 20 74 function adds t
15d0: 61 67 20 74 6f 20 73 69 74 65 20 62 79 20 73 69 ag to site by si
15e0: 74 65 20 69 64 0a 43 52 45 41 54 45 20 6f 72 20 te id.CREATE or
15f0: 72 65 70 6c 61 63 65 20 46 55 4e 43 54 49 4f 4e replace FUNCTION
1600: 20 6d 61 72 6b 28 6d 79 5f 69 64 5f 73 69 74 65 mark(my_id_site
1610: 20 69 6e 74 65 67 65 72 2c 20 6e 65 77 5f 74 61 integer, new_ta
1620: 67 20 74 65 78 74 29 20 52 45 54 55 52 4e 53 20 g text) RETURNS
1630: 69 6e 74 65 67 65 72 0a 09 4c 41 4e 47 55 41 47 integer..LANGUAG
1640: 45 20 73 71 6c 20 69 6d 6d 75 74 61 62 6c 65 20 E sql immutable
1650: 53 54 52 49 43 54 0a 09 41 53 20 24 24 0a 73 65 STRICT..AS $$.se
1660: 6c 65 63 74 20 6d 61 72 6b 28 24 31 2c 20 67 65 lect mark($1, ge
1670: 74 5f 74 61 67 28 61 72 72 61 79 5b 24 32 5d 29 t_tag(array[$2])
1680: 2c 20 4e 55 4c 4c 29 20 61 73 20 72 65 73 75 6c , NULL) as resul
1690: 74 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 t;.$$;..-- this
16a0: 66 75 6e 63 74 69 6f 6e 20 61 64 64 73 20 74 61 function adds ta
16b0: 67 20 74 6f 20 64 6f 6d 61 69 6e 0a 43 52 45 41 g to domain.CREA
16c0: 54 45 20 6f 72 20 72 65 70 6c 61 63 65 20 46 55 TE or replace FU
16d0: 4e 43 54 49 4f 4e 20 6d 61 72 6b 28 64 6f 6d 61 NCTION mark(doma
16e0: 69 6e 20 74 65 78 74 2c 20 6e 65 77 5f 74 61 67 in text, new_tag
16f0: 20 74 65 78 74 29 20 52 45 54 55 52 4e 53 20 69 text) RETURNS i
1700: 6e 74 65 67 65 72 0a 09 4c 41 4e 47 55 41 47 45 nteger..LANGUAGE
1710: 20 73 71 6c 20 69 6d 6d 75 74 61 62 6c 65 20 53 sql immutable S
1720: 54 52 49 43 54 0a 09 41 53 20 24 24 0a 73 65 6c TRICT..AS $$.sel
1730: 65 63 74 20 6d 61 72 6b 28 67 65 74 5f 73 69 74 ect mark(get_sit
1740: 65 28 24 31 29 2c 20 67 65 74 5f 74 61 67 28 61 e($1), get_tag(a
1750: 72 72 61 79 5b 24 32 5d 29 2c 20 4e 55 4c 4c 29 rray[$2]), NULL)
1760: 20 61 73 20 72 65 73 75 6c 74 3b 0a 24 24 3b 0a as result;.$$;.
1770: 0a 2d 2d 20 74 68 69 73 20 66 75 6e 63 74 69 6f .-- this functio
1780: 6e 20 73 65 74 73 20 74 61 67 73 20 66 6f 72 20 n sets tags for
1790: 73 69 74 65 20 77 69 74 68 6f 75 74 20 72 65 67 site without reg
17a0: 65 78 70 0a 43 52 45 41 54 45 20 6f 72 20 72 65 exp.CREATE or re
17b0: 70 6c 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 73 place FUNCTION s
17c0: 65 74 28 6d 79 5f 69 64 5f 73 69 74 65 20 69 6e et(my_id_site in
17d0: 74 65 67 65 72 2c 20 6d 79 5f 69 64 5f 74 61 67 teger, my_id_tag
17e0: 20 69 6e 74 65 67 65 72 29 20 52 45 54 55 52 4e integer) RETURN
17f0: 53 20 69 6e 74 65 67 65 72 0a 09 4c 41 4e 47 55 S integer..LANGU
1800: 41 47 45 20 73 71 6c 20 53 54 52 49 43 54 0a 09 AGE sql STRICT..
1810: 41 53 20 24 24 0a 64 65 6c 65 74 65 20 66 72 6f AS $$.delete fro
1820: 6d 20 75 72 6c 73 20 77 68 65 72 65 20 24 31 20 m urls where $1
1830: 3d 20 69 64 5f 73 69 74 65 20 61 6e 64 20 72 65 = id_site and re
1840: 67 65 78 70 20 69 73 20 4e 55 4c 4c 3b 0a 69 6e gexp is NULL;.in
1850: 73 65 72 74 20 69 6e 74 6f 20 75 72 6c 73 20 28 sert into urls (
1860: 69 64 5f 73 69 74 65 2c 20 69 64 5f 74 61 67 29 id_site, id_tag)
1870: 20 76 61 6c 75 65 73 20 28 24 31 2c 20 24 32 29 values ($1, $2)
1880: 3b 0a 73 65 6c 65 63 74 20 24 31 3b 0a 24 24 3b ;.select $1;.$$;
1890: 0a 0a 2d 2d 20 74 68 69 73 20 66 75 6e 63 74 69 ..-- this functi
18a0: 6f 6e 20 73 65 74 73 20 74 61 67 73 20 66 6f 72 on sets tags for
18b0: 20 73 69 74 65 2f 72 65 67 65 78 70 20 70 61 69 site/regexp pai
18c0: 72 0a 43 52 45 41 54 45 20 6f 72 20 72 65 70 6c r.CREATE or repl
18d0: 61 63 65 20 46 55 4e 43 54 49 4f 4e 20 73 65 74 ace FUNCTION set
18e0: 28 6d 79 5f 69 64 5f 73 69 74 65 20 69 6e 74 65 (my_id_site inte
18f0: 67 65 72 2c 20 6d 79 5f 69 64 5f 74 61 67 20 69 ger, my_id_tag i
1900: 6e 74 65 67 65 72 2c 20 6d 79 5f 72 65 67 65 78 nteger, my_regex
1910: 70 20 74 65 78 74 29 20 52 45 54 55 52 4e 53 20 p text) RETURNS
1920: 69 6e 74 65 67 65 72 0a 09 4c 41 4e 47 55 41 47 integer..LANGUAG
1930: 45 20 73 71 6c 20 53 54 52 49 43 54 0a 09 41 53 E sql STRICT..AS
1940: 20 24 24 0a 64 65 6c 65 74 65 20 66 72 6f 6d 20 $$.delete from
1950: 75 72 6c 73 20 77 68 65 72 65 20 24 31 20 3d 20 urls where $1 =
1960: 69 64 5f 73 69 74 65 20 61 6e 64 20 24 33 20 3d id_site and $3 =
1970: 20 72 65 67 65 78 70 3b 0a 69 6e 73 65 72 74 20 regexp;.insert
1980: 69 6e 74 6f 20 75 72 6c 73 20 28 69 64 5f 73 69 into urls (id_si
1990: 74 65 2c 20 69 64 5f 74 61 67 2c 20 72 65 67 65 te, id_tag, rege
19a0: 78 70 29 20 76 61 6c 75 65 73 20 28 24 31 2c 20 xp) values ($1,
19b0: 24 32 2c 20 24 33 29 3b 0a 73 65 6c 65 63 74 20 $2, $3);.select
19c0: 24 31 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 $1;.$$;..-- this
19d0: 20 66 75 6e 63 74 69 6f 6e 20 73 74 6f 72 65 73 function stores
19e0: 20 6e 65 77 20 64 61 74 61 20 66 6f 72 20 73 69 new data for si
19f0: 74 65 2f 72 65 67 65 78 70 20 70 61 69 72 0a 63 te/regexp pair.c
1a00: 72 65 61 74 65 20 6f 72 20 72 65 70 6c 61 63 65 reate or replace
1a10: 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 28 64 6f function set(do
1a20: 6d 61 69 6e 20 74 65 78 74 2c 20 74 61 67 73 20 main text, tags
1a30: 74 65 78 74 2c 20 72 65 67 65 78 70 20 74 65 78 text, regexp tex
1a40: 74 29 20 72 65 74 75 72 6e 73 20 69 6e 74 65 67 t) returns integ
1a50: 65 72 0a 09 6c 61 6e 67 75 61 67 65 20 73 71 6c er..language sql
1a60: 20 69 6d 6d 75 74 61 62 6c 65 20 73 74 72 69 63 immutable stric
1a70: 74 0a 09 61 73 20 24 24 0a 73 65 6c 65 63 74 20 t..as $$.select
1a80: 73 65 74 28 67 65 74 5f 73 69 74 65 28 24 31 29 set(get_site($1)
1a90: 2c 20 67 65 74 5f 74 61 67 28 24 32 3a 3a 74 65 , get_tag($2::te
1aa0: 78 74 5b 5d 29 2c 20 24 33 29 3b 0a 24 24 3b 0a xt[]), $3);.$$;.
1ab0: 0a 2d 2d 20 74 68 69 73 20 66 75 6e 63 74 69 6f .-- this functio
1ac0: 6e 20 73 74 6f 72 65 73 20 6e 65 77 20 64 61 74 n stores new dat
1ad0: 61 20 66 6f 72 20 73 69 74 65 2f 72 65 67 65 78 a for site/regex
1ae0: 70 20 70 61 69 72 0a 63 72 65 61 74 65 20 6f 72 p pair.create or
1af0: 20 72 65 70 6c 61 63 65 20 66 75 6e 63 74 69 6f replace functio
1b00: 6e 20 73 65 74 28 64 6f 6d 61 69 6e 20 74 65 78 n set(domain tex
1b10: 74 2c 20 74 61 67 73 20 74 65 78 74 29 20 72 65 t, tags text) re
1b20: 74 75 72 6e 73 20 69 6e 74 65 67 65 72 0a 09 6c turns integer..l
1b30: 61 6e 67 75 61 67 65 20 73 71 6c 20 69 6d 6d 75 anguage sql immu
1b40: 74 61 62 6c 65 20 73 74 72 69 63 74 0a 09 61 73 table strict..as
1b50: 20 24 24 0a 73 65 6c 65 63 74 20 73 65 74 28 67 $$.select set(g
1b60: 65 74 5f 73 69 74 65 28 24 31 29 2c 20 67 65 74 et_site($1), get
1b70: 5f 74 61 67 28 24 32 3a 3a 74 65 78 74 5b 5d 29 _tag($2::text[])
1b80: 29 3b 0a 24 24 3b 0a 0a 2d 2d 20 74 68 69 73 20 );.$$;..-- this
1b90: 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
1ba0: 20 69 64 20 6f 66 20 74 61 67 20 61 72 72 61 79 id of tag array
1bb0: 0a 63 72 65 61 74 65 20 6f 72 20 72 65 70 6c 61 .create or repla
1bc0: 63 65 20 66 75 6e 63 74 69 6f 6e 20 67 65 74 5f ce function get_
1bd0: 74 61 67 28 6d 79 5f 74 61 67 20 74 65 78 74 5b tag(my_tag text[
1be0: 5d 29 20 72 65 74 75 72 6e 73 20 69 6e 74 65 67 ]) returns integ
1bf0: 65 72 0a 09 6c 61 6e 67 75 61 67 65 20 70 6c 70 er..language plp
1c00: 67 73 71 6c 20 73 74 72 69 63 74 0a 09 61 73 20 gsql strict..as
1c10: 24 24 0a 64 65 63 6c 61 72 65 0a 09 74 61 67 5f $$.declare..tag_
1c20: 69 64 20 69 6e 74 65 67 65 72 3b 0a 62 65 67 69 id integer;.begi
1c30: 6e 0a 09 73 65 6c 65 63 74 20 69 64 5f 74 61 67 n..select id_tag
1c40: 20 66 72 6f 6d 20 74 61 67 20 77 68 65 72 65 20 from tag where
1c50: 75 73 6f 72 74 28 6d 79 5f 74 61 67 29 20 3d 20 usort(my_tag) =
1c60: 74 61 67 20 69 6e 74 6f 20 74 61 67 5f 69 64 3b tag into tag_id;
1c70: 0a 09 69 66 20 6e 6f 74 20 66 6f 75 6e 64 20 74 ..if not found t
1c80: 68 65 6e 0a 09 09 69 6e 73 65 72 74 20 69 6e 74 hen...insert int
1c90: 6f 20 74 61 67 20 28 74 61 67 29 20 76 61 6c 75 o tag (tag) valu
1ca0: 65 73 20 28 75 73 6f 72 74 28 6d 79 5f 74 61 67 es (usort(my_tag
1cb0: 29 29 3b 0a 09 09 73 65 6c 65 63 74 20 69 64 5f ));...select id_
1cc0: 74 61 67 20 66 72 6f 6d 20 74 61 67 20 77 68 65 tag from tag whe
1cd0: 72 65 20 75 73 6f 72 74 28 6d 79 5f 74 61 67 29 re usort(my_tag)
1ce0: 20 3d 20 74 61 67 20 69 6e 74 6f 20 74 61 67 5f = tag into tag_
1cf0: 69 64 3b 0a 09 65 6e 64 20 69 66 3b 0a 09 72 65 id;..end if;..re
1d00: 74 75 72 6e 20 74 61 67 5f 69 64 3b 0a 65 6e 64 turn tag_id;.end
1d10: 3b 0a 24 24 3b 0a ;.$$;.