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