Overview
| Comment: | reworked mark functions, now they can update data without deleting it |
|---|---|
| Downloads: | Tarball | ZIP archive | SQL archive |
| Timelines: | family | ancestors | descendants | both | master | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
2326f3bb9a9a00cd379159ec5aeada35 |
| User & Date: | c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 on 2009-10-08 21:51:22.000 |
| Other Links: | branch diff | manifest | tags |
Context
|
2009-10-09
| ||
| 06:20 | minor naming unification check-in: 33e72616c9 user: c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 tags: master, trunk | |
|
2009-10-08
| ||
| 21:51 | reworked mark functions, now they can update data without deleting it check-in: 2326f3bb9a user: c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 tags: master, trunk | |
|
2009-10-07
| ||
| 14:20 | * added array sorting functions; * indexes modified to check array uniqueness. check-in: e0ecab03f9 user: c.kworr@d4daf22a-8aaf-11de-a64d-234b64dd91b4 tags: master, trunk | |
Changes
Modified database.sql
from [c71bdf1f8e]
to [6f8e0e1cc8].
| ︙ | ︙ | |||
12 13 14 15 16 17 18 | CREATE FUNCTION usort(original anyarray) RETURNS anyarray LANGUAGE sql IMMUTABLE STRICT AS $_$ select array_agg(item) as result from (select distinct unnest($1) as item order by item) a; $_$; -- this function adds tag to domain | | | | | | | > < < | < < < < < < < < | | | | | | > > < > > > > > > > | | | | < < | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
CREATE FUNCTION usort(original anyarray) RETURNS anyarray
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select array_agg(item) as result from (select distinct unnest($1) as item order by item) a;
$_$;
-- this function adds tag to domain
CREATE or replace FUNCTION mark(domain text, new_tag text) RETURNS void
LANGUAGE plpgsql STRICT
AS $$
declare
my_site text[];
my_id_site smallint;
my_tag text[];
my_id_tag smallint;
begin
my_site := tripdomain(domain);
-- selecting site id from table or adding site to the table
select id_site from site where my_site = site into my_id_site;
if not found then
insert into site (site) values (my_site);
select id_site from site where my_site = site into my_id_site;
end if;
-- selecting tags site already have and adding new tag to them
-- note that tags should be sorted to eliminate permutations
select coalesce(tag, '{}'::text[]) from urls natural join tag where id_site = my_id_site into my_tag;
if not found then
-- no records found - creating new tag
insert into urls (id_site, id_tag) values (my_id_site, get_tag(array[new_tag]));
my_tag := array[new_tag];
else
-- joining tags
select usort(my_tag || array[new_tag]) into my_tag;
-- deleting old site specification
update urls set id_tag = get_tag(my_tag || array[new_tag]) where id_site = my_id_site;
end if;
end;
$$;
-- this function adds tag to site by site id
CREATE or replace FUNCTION mark(my_id_site integer, new_tag text) RETURNS void
LANGUAGE plpgsql STRICT
AS $$
declare
-- maybe check should be added to make sure supplied site id really exists
my_tag text[];
my_tag_id integer;
begin
-- selecting tags site already have and adding new tag to them
-- note that tags should be sorted to eliminate permutations
select coalesce(tag, '{}'::text[]) from urls natural left join tag where id_site = my_id_site into my_tag;
if not found then
-- no records found - creating new tag
insert into urls (id_site, id_tag) values (my_id_site, get_tag(array[new_tag]));
else
-- joining tags
select usort(my_tag || array[new_tag]) into my_tag;
-- updating existing record
update urls set id_tag = get_tag(my_tag || array[new_tag]) where id_site = my_id_site;
end if;
end;
$$;
-- this function returns id of tag array
create or replace function get_tag(my_tag text[]) returns integer
language plpgsql strict
as $$
declare
tag_id integer;
begin
select id_tag from tag where usort(my_tag) = tag into tag_id;
if not found then
insert into tag (tag) values (usort(my_tag));
select id_tag from tag where usort(my_tag) = tag into tag_id;
end if;
return tag_id;
end;
$$;
-- transforms domain into ordered array for indexing
CREATE FUNCTION tripdomain(url text) RETURNS text[]
LANGUAGE plpgsql IMMUTABLE STRICT
AS $_$
|
| ︙ | ︙ | |||
151 152 153 154 155 156 157 | -- table to hold site arrays CREATE TABLE site ( id_site serial, site text[] NOT NULL ); ALTER TABLE ONLY site | | | 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | -- table to hold site arrays CREATE TABLE site ( id_site serial, site text[] NOT NULL ); ALTER TABLE ONLY site ADD CONSTRAINT id_site PRIMARY KEY (id_site); CREATE UNIQUE INDEX site_s ON site (usort(site)); CREATE INDEX site_g ON site USING gin (site); -- table to hold tag combinations CREATE TABLE tag ( |
| ︙ | ︙ |