Squid url redirector

Annotation For database.sql
anonymous

Annotation For database.sql

Origin for each line in database.sql from check-in f1bafd194a:

09a01deb52 2009-10-01    1: CREATE PROCEDURAL LANGUAGE plpgsql;
09a01deb52 2009-10-01    2: 
e0ecab03f9 2009-10-07    3: -- general array sorting functions
e0ecab03f9 2009-10-07    4: -- sorts array
67e762b39b 2009-10-14    5: CREATE or replace FUNCTION sort(original anyarray) RETURNS anyarray
e0ecab03f9 2009-10-07    6: 	LANGUAGE sql IMMUTABLE STRICT
e0ecab03f9 2009-10-07    7: 	AS $_$
e0ecab03f9 2009-10-07    8: select array_agg(item) as result from (select unnest($1) as item order by item) a;
e0ecab03f9 2009-10-07    9: $_$;
e0ecab03f9 2009-10-07   10: 
e0ecab03f9 2009-10-07   11: -- sorts array and removes duplicates
67e762b39b 2009-10-14   12: CREATE or replace FUNCTION usort(original anyarray) RETURNS anyarray
e0ecab03f9 2009-10-07   13: 	LANGUAGE sql IMMUTABLE STRICT
e0ecab03f9 2009-10-07   14: 	AS $_$
e0ecab03f9 2009-10-07   15: select array_agg(item) as result from (select distinct unnest($1) as item order by item) a;
e0ecab03f9 2009-10-07   16: $_$;
ddb0e69dd7 2009-10-02   17: 
ddb0e69dd7 2009-10-02   18: -- transforms domain into ordered array for indexing
67e762b39b 2009-10-14   19: CREATE or replace FUNCTION tripdomain(url text) RETURNS text[]
ddb0e69dd7 2009-10-02   20: 	LANGUAGE plpgsql IMMUTABLE STRICT
ddb0e69dd7 2009-10-02   21: 	AS $_$
ddb0e69dd7 2009-10-02   22: declare
ddb0e69dd7 2009-10-02   23: 	result text[];
ddb0e69dd7 2009-10-02   24: 	splitted text[];
ddb0e69dd7 2009-10-02   25: 	x integer;
ddb0e69dd7 2009-10-02   26: 	length integer;
ddb0e69dd7 2009-10-02   27: begin
ddb0e69dd7 2009-10-02   28: 	splitted := string_to_array($1, '.');
ddb0e69dd7 2009-10-02   29: 	length := array_length(splitted, 1);
ddb0e69dd7 2009-10-02   30: 	x := 1;
ddb0e69dd7 2009-10-02   31: 	loop
ddb0e69dd7 2009-10-02   32: 		exit when splitted[x] is null;
ddb0e69dd7 2009-10-02   33: 		result[x] := splitted[x] || ':' || length - x;
ddb0e69dd7 2009-10-02   34: 		x := x + 1;
ddb0e69dd7 2009-10-02   35: 	end loop;
ddb0e69dd7 2009-10-02   36: 	return result;
ddb0e69dd7 2009-10-02   37: end;$_$;
ddb0e69dd7 2009-10-02   38: 
ddb0e69dd7 2009-10-02   39: -- transforms ordered array into domain
67e762b39b 2009-10-14   40: create or replace function untrip(site text[]) returns text
ddb0e69dd7 2009-10-02   41: 	language plpgsql immutable strict
ddb0e69dd7 2009-10-02   42: 	as $_$
ddb0e69dd7 2009-10-02   43: declare
ddb0e69dd7 2009-10-02   44: 	x integer;
ddb0e69dd7 2009-10-02   45: 	splitted text[];
ddb0e69dd7 2009-10-02   46: 	pair text[];
ddb0e69dd7 2009-10-02   47: begin
ddb0e69dd7 2009-10-02   48: 	x := array_length(site, 1);
ddb0e69dd7 2009-10-02   49: 	loop 
ddb0e69dd7 2009-10-02   50: 		exit when site[x] is null;
ddb0e69dd7 2009-10-02   51: 		pair := string_to_array(site[x], ':');
ddb0e69dd7 2009-10-02   52: 		splitted[0 - pair[2]::integer] := pair[1];
ddb0e69dd7 2009-10-02   53: 		x := x - 1;
ddb0e69dd7 2009-10-02   54: 	end loop;
ddb0e69dd7 2009-10-02   55: 	return array_to_string(splitted, '.');
09a01deb52 2009-10-01   56: end;
09a01deb52 2009-10-01   57: $_$;
09a01deb52 2009-10-01   58: 
12c35e5674 2009-12-15   59: -- this functions returns id of site
12c35e5674 2009-12-15   60: create or replace function get_site(my_site text[]) returns integer
12c35e5674 2009-12-15   61: 	language plpgsql strict
12c35e5674 2009-12-15   62: 	as $$
12c35e5674 2009-12-15   63: declare
12c35e5674 2009-12-15   64: 	site_id integer;
12c35e5674 2009-12-15   65: begin
12c35e5674 2009-12-15   66: 	select id_site from site where my_site = site into site_id;
12c35e5674 2009-12-15   67: 	if not found then
12c35e5674 2009-12-15   68: 		insert into site (site) values (my_site);
12c35e5674 2009-12-15   69: 		select id_site from site where my_site = site into site_id;
12c35e5674 2009-12-15   70: 	end if;
12c35e5674 2009-12-15   71: 	return site_id;
12c35e5674 2009-12-15   72: end;
12c35e5674 2009-12-15   73: $$;
12c35e5674 2009-12-15   74: 
12c35e5674 2009-12-15   75: create or replace function get_site(domain text) returns integer
12c35e5674 2009-12-15   76: 	language sql immutable strict
12c35e5674 2009-12-15   77: 	as $$
12c35e5674 2009-12-15   78: select get_site(tripdomain($1)) as result;
12c35e5674 2009-12-15   79: $$;
12c35e5674 2009-12-15   80: 
f1bafd194a 2010-03-18   81: -- this function inserts or updates record with tags to site by site id with regexp
f1bafd194a 2010-03-18   82: CREATE or replace FUNCTION mark(my_id_site integer, my_id_tag integer, my_regexp text) RETURNS integer
f1bafd194a 2010-03-18   83: 	LANGUAGE plpgsql STRICT
f1bafd194a 2010-03-18   84: 	AS $$
f1bafd194a 2010-03-18   85: declare
f1bafd194a 2010-03-18   86: 	-- maybe check should be added to make sure supplied site id really exists
f1bafd194a 2010-03-18   87: 	my_tag text[];
f1bafd194a 2010-03-18   88: begin
f1bafd194a 2010-03-18   89: 	-- selecting tags site already have and adding new tag to them
f1bafd194a 2010-03-18   90: 	-- note that tags should be sorted to eliminate permutations
f1bafd194a 2010-03-18   91: 	select coalesce(tag, '{}'::text[]) from urls natural left join tag
f1bafd194a 2010-03-18   92: 		where id_site = my_id_site and regexp = my_regexp into my_tag;
f1bafd194a 2010-03-18   93: 	if not found then
f1bafd194a 2010-03-18   94: 		-- no records found - creating new tag
f1bafd194a 2010-03-18   95: 		insert into urls (id_site, id_tag, regexp) values (my_id_site, my_id_tag, my_regexp);
f1bafd194a 2010-03-18   96: 	else
f1bafd194a 2010-03-18   97: 		-- joining tags
f1bafd194a 2010-03-18   98: 		select usort(my_tag || tag) from tag where id_tag = my_id_tag into my_tag;
f1bafd194a 2010-03-18   99: 		-- updating existing record
f1bafd194a 2010-03-18  100: 		update urls set id_tag = get_tag(my_tag)
f1bafd194a 2010-03-18  101: 			where id_site = my_id_site and regexp = my_regexp;
f1bafd194a 2010-03-18  102: 	end if;
f1bafd194a 2010-03-18  103: 	return my_id_site;
f1bafd194a 2010-03-18  104: end;
f1bafd194a 2010-03-18  105: $$;
f1bafd194a 2010-03-18  106: 
12c35e5674 2009-12-15  107: -- this function adds tag to site by site id
12c35e5674 2009-12-15  108: CREATE or replace FUNCTION mark(my_id_site integer, new_tag text) RETURNS integer
12c35e5674 2009-12-15  109: 	LANGUAGE plpgsql STRICT
12c35e5674 2009-12-15  110: 	AS $$
12c35e5674 2009-12-15  111: declare
12c35e5674 2009-12-15  112: 	-- maybe check should be added to make sure supplied site id really exists
12c35e5674 2009-12-15  113: 	my_tag text[];
12c35e5674 2009-12-15  114: begin
12c35e5674 2009-12-15  115: 	-- selecting tags site already have and adding new tag to them
12c35e5674 2009-12-15  116: 	-- note that tags should be sorted to eliminate permutations
f1bafd194a 2010-03-18  117: 	select coalesce(tag, '{}'::text[]) from urls natural left join tag
f1bafd194a 2010-03-18  118: 		where id_site = my_id_site and regexp is null into my_tag;
12c35e5674 2009-12-15  119: 	if not found then
12c35e5674 2009-12-15  120: 		-- no records found - creating new tag
12c35e5674 2009-12-15  121: 		insert into urls (id_site, id_tag) values (my_id_site, get_tag(array[new_tag]));
12c35e5674 2009-12-15  122: 	else
12c35e5674 2009-12-15  123: 		-- joining tags
12c35e5674 2009-12-15  124: 		select usort(my_tag || array[new_tag]) into my_tag;
12c35e5674 2009-12-15  125: 		-- updating existing record
f1bafd194a 2010-03-18  126: 		update urls set id_tag = get_tag(my_tag) where id_site = my_id_site and regexp is null;
12c35e5674 2009-12-15  127: 	end if;
12c35e5674 2009-12-15  128: 	return my_id_site;
12c35e5674 2009-12-15  129: end;
12c35e5674 2009-12-15  130: $$;
12c35e5674 2009-12-15  131: 
12c35e5674 2009-12-15  132: -- this function adds tag to domain
12c35e5674 2009-12-15  133: CREATE or replace FUNCTION mark(domain text, new_tag text) RETURNS integer
12c35e5674 2009-12-15  134: 	LANGUAGE sql immutable STRICT
12c35e5674 2009-12-15  135: 	AS $$
12c35e5674 2009-12-15  136: select mark(get_site($1), $2) as result;
f1bafd194a 2010-03-18  137: $$;
f1bafd194a 2010-03-18  138: 
f1bafd194a 2010-03-18  139: -- this function adds tag to domain with regexp
f1bafd194a 2010-03-18  140: CREATE or replace FUNCTION mark(domain text, tags text[], regexp text) RETURNS integer
f1bafd194a 2010-03-18  141: 	LANGUAGE sql immutable STRICT
f1bafd194a 2010-03-18  142: 	AS $$
f1bafd194a 2010-03-18  143: select mark(get_site($1), get_tag($2), $3) as result;
f1bafd194a 2010-03-18  144: $$;
f1bafd194a 2010-03-18  145: 
f1bafd194a 2010-03-18  146: -- this function adds tag to domain with regexp
f1bafd194a 2010-03-18  147: CREATE or replace FUNCTION mark(domain text, tags text[]) RETURNS integer
f1bafd194a 2010-03-18  148: 	LANGUAGE sql immutable STRICT
f1bafd194a 2010-03-18  149: 	AS $$
f1bafd194a 2010-03-18  150: select mark(get_site($1), get_tag($2), NULL) as result;
12c35e5674 2009-12-15  151: $$;
12c35e5674 2009-12-15  152: 
12c35e5674 2009-12-15  153: -- this function returns id of tag array
12c35e5674 2009-12-15  154: create or replace function get_tag(my_tag text[]) returns integer
12c35e5674 2009-12-15  155: 	language plpgsql strict
12c35e5674 2009-12-15  156: 	as $$
12c35e5674 2009-12-15  157: declare
12c35e5674 2009-12-15  158: 	tag_id integer;
12c35e5674 2009-12-15  159: begin
12c35e5674 2009-12-15  160: 	select id_tag from tag where usort(my_tag) = tag into tag_id;
12c35e5674 2009-12-15  161: 	if not found then
12c35e5674 2009-12-15  162: 		insert into tag (tag) values (usort(my_tag));
12c35e5674 2009-12-15  163: 		select id_tag from tag where usort(my_tag) = tag into tag_id;
12c35e5674 2009-12-15  164: 	end if;
12c35e5674 2009-12-15  165: 	return tag_id;
12c35e5674 2009-12-15  166: end;
12c35e5674 2009-12-15  167: $$;
12c35e5674 2009-12-15  168: 
ddb0e69dd7 2009-10-02  169: -- table to hold all rules
ddb0e69dd7 2009-10-02  170: CREATE TABLE rules (
ddb0e69dd7 2009-10-02  171: 	netmask cidr NOT NULL,
ddb0e69dd7 2009-10-02  172: 	redirect_url text DEFAULT 'about::blank'::text NOT NULL,
ddb0e69dd7 2009-10-02  173: 	from_weekday smallint DEFAULT 0 NOT NULL,
ddb0e69dd7 2009-10-02  174: 	to_weekday smallint DEFAULT 6 NOT NULL,
ddb0e69dd7 2009-10-02  175: 	from_time time without time zone DEFAULT '00:00:00'::time without time zone NOT NULL,
ddb0e69dd7 2009-10-02  176: 	to_time time without time zone DEFAULT '23:59:59'::time without time zone NOT NULL,
ddb0e69dd7 2009-10-02  177: 	id_tag smallint NOT NULL
ddb0e69dd7 2009-10-02  178: );
ddb0e69dd7 2009-10-02  179: 
ddb0e69dd7 2009-10-02  180: ALTER TABLE ONLY rules
ddb0e69dd7 2009-10-02  181: 	ADD CONSTRAINT rules_pkey PRIMARY KEY (netmask);
f1bafd194a 2010-03-18  182: 
f1bafd194a 2010-03-18  183: ALTER TABLE ONLY rules
f1bafd194a 2010-03-18  184: 	ADD CONSTRAINT rules_tag_f FOREIGN KEY (id_tag) REFERENCES tag(id_tag) MATCH FULL
f1bafd194a 2010-03-18  185: 	ON UPDATE RESTRICT ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED;
33e72616c9 2009-10-09  186: 
ddb0e69dd7 2009-10-02  187: -- table to hold site arrays
ddb0e69dd7 2009-10-02  188: CREATE TABLE site (
ddb0e69dd7 2009-10-02  189: 	id_site serial,
4b22e25f24 2009-10-07  190: 	site text[] NOT NULL
ddb0e69dd7 2009-10-02  191: );
09a01deb52 2009-10-01  192: 
09a01deb52 2009-10-01  193: ALTER TABLE ONLY site
33e72616c9 2009-10-09  194: 	ADD CONSTRAINT site_pkey PRIMARY KEY (id_site);
ddb0e69dd7 2009-10-02  195: 
33e72616c9 2009-10-09  196: CREATE UNIQUE INDEX site_u ON site (usort(site));
ddb0e69dd7 2009-10-02  197: 
e0ecab03f9 2009-10-07  198: CREATE INDEX site_g ON site USING gin (site);
ddb0e69dd7 2009-10-02  199: 
ddb0e69dd7 2009-10-02  200: -- table to hold tag combinations
ddb0e69dd7 2009-10-02  201: CREATE TABLE tag (
ddb0e69dd7 2009-10-02  202: 	id_tag serial,
ddb0e69dd7 2009-10-02  203: 	tag text[] NOT NULL
ddb0e69dd7 2009-10-02  204: );
09a01deb52 2009-10-01  205: 
09a01deb52 2009-10-01  206: ALTER TABLE ONLY tag
33e72616c9 2009-10-09  207: 	ADD CONSTRAINT tag_pkey PRIMARY KEY (id_tag);
e0ecab03f9 2009-10-07  208: 
33e72616c9 2009-10-09  209: CREATE UNIQUE INDEX tag_u ON tag (usort(tag));
09a01deb52 2009-10-01  210: 
09a01deb52 2009-10-01  211: CREATE INDEX tag_g ON tag USING gin (tag);
09a01deb52 2009-10-01  212: 
ddb0e69dd7 2009-10-02  213: -- table to hold tag - site links
ddb0e69dd7 2009-10-02  214: CREATE TABLE urls (
ddb0e69dd7 2009-10-02  215: 	date_added timestamp without time zone DEFAULT ('now'::text)::timestamp(0) without time zone NOT NULL,
ddb0e69dd7 2009-10-02  216: 	id_site smallint NOT NULL,
7d9c268669 2009-10-02  217: 	id_tag smallint NOT NULL,
12c35e5674 2009-12-15  218: 	regexp text
ddb0e69dd7 2009-10-02  219: );
ddb0e69dd7 2009-10-02  220: 
f1bafd194a 2010-03-18  221: CREATE UNIQUE INDEX urls_pkey ON urls USING btree (id_site, regexp);
f1bafd194a 2010-03-18  222: 
f1bafd194a 2010-03-18  223: CREATE INDEX urls_id_tag ON urls USING btree (id_tag);
4b22e25f24 2009-10-07  224: 
f1bafd194a 2010-03-18  225: ALTER TABLE ONLY urls
f1bafd194a 2010-03-18  226: 	ADD CONSTRAINT urls_site_f FOREIGN KEY (id_site) REFERENCES site(id_site) MATCH FULL
f1bafd194a 2010-03-18  227: 	ON UPDATE RESTRICT ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED;
ddb0e69dd7 2009-10-02  228: 
f1bafd194a 2010-03-18  229: ALTER TABLE ONLY urls
f1bafd194a 2010-03-18  230: 	ADD CONSTRAINT urls_tag_f FOREIGN KEY (id_tag) REFERENCES tag(id_tag) MATCH FULL
f1bafd194a 2010-03-18  231: 	ON UPDATE RESTRICT ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED;
09a01deb52 2009-10-01  232: 
ddb0e69dd7 2009-10-02  233: -- rule to join all tables into one to simplify access
ddb0e69dd7 2009-10-02  234: -- automaticall uses current day and time data
ddb0e69dd7 2009-10-02  235: CREATE VIEW site_rule AS
7d9c268669 2009-10-02  236: SELECT a.redirect_url, a.netmask, b.site, b.regexp
ddb0e69dd7 2009-10-02  237: FROM ((
ddb0e69dd7 2009-10-02  238: 	SELECT rules.redirect_url, tag.tag AS rule_tag, rules.netmask
7d9c268669 2009-10-02  239: 	FROM rules NATURAL JOIN tag
7d9c268669 2009-10-02  240: 	WHERE ('now'::text)::time without time zone >= rules.from_time
7d9c268669 2009-10-02  241: 		AND ('now'::text)::time without time zone <= rules.to_time
7d9c268669 2009-10-02  242: 		AND date_part('dow'::text, now()) >= (rules.from_weekday)::double precision
7d9c268669 2009-10-02  243: 		AND date_part('dow'::text, now()) <= (rules.to_weekday)::double precision
ddb0e69dd7 2009-10-02  244: ) a JOIN (
7d9c268669 2009-10-02  245: 	SELECT site.site, tag.tag AS url_tag, regexp
7d9c268669 2009-10-02  246: 	FROM urls NATURAL JOIN tag NATURAL JOIN site
7d9c268669 2009-10-02  247: ) b ON (b.url_tag && a.rule_tag));