improve function definitions; provide new function get_value()

This commit is contained in:
Helmut Merz 2020-02-18 15:32:48 +01:00
parent f4c0bd42a3
commit 6283f0246b
5 changed files with 59 additions and 18 deletions

View file

@ -1,10 +1,10 @@
create table cities ( create table cities (
rectype int, rectype int not null,
ctype int, ctype int not null,
regiokey text not null primary key, regiokey text not null primary key,
ckey text not null, ckey text not null,
name text, name text not null,
area decimal, area decimal,
inhabitants int, inhabitants int,
zipcode text, zipcode text,

35
pgsql/fload.sql Normal file
View file

@ -0,0 +1,35 @@
create or replace function get_value(tname text, vtext text) returns bigint as $$
begin
case tname
when 'node' then
return get_node(vtext);
when 'int' then
return to_number(vtext, '99999999');
else
return 0;
end case;
end;
$$ language plpgsql;
create or replace function get_node(vtext text) returns bigint as $$
declare
pfx text;
vname text;
value bigint;
begin
pfx := split_part(vtext, ':', 1);
vname := split_part(vtext, ':', 2);
select nodes.id into value from nodes, namespaces ns
where ns.prefix = pfx
and nodes.namespace = ns.id
and nodes.name = vname;
if value is null then
insert into nodes (namespace, name)
(select ns.id, vname from namespaces ns where ns.prefix = pfx)
returning nodes.id into value;
end if;
return value;
end;
$$ language plpgsql;

View file

@ -1,16 +1,22 @@
create or replace function show(type smallint, id bigint) returns text as $$ create or replace function show_value(type smallint, id bigint) returns text as $$
declare tname text := (select dtname from datatypes as dt where dt.id = type); declare tname text := (select dtname from datatypes as dt where dt.id = type);
begin begin
case tname return show_value(tname, id);
when 'node' then end;
return ( $$ language plpgsql;
select ns.prefix || ':' || n.name
from nodes n create or replace function show_value(tname text, id bigint) returns text as $$
join namespaces ns on ns.id = n.namespace begin
where n.id = show.id case tname
); when 'node' then
else return tname; return (
end case; select ns.prefix || ':' || n.name
from nodes n
join namespaces ns on ns.id = n.namespace
where n.id = show_value.id
);
else return text(show_value.id);
end case;
end; end;
$$ language plpgsql; $$ language plpgsql;

View file

@ -64,7 +64,7 @@ INSERT INTO datatypes (dtname, tablename) VALUES
('triple', 'triples'), -- 5 ('triple', 'triples'), -- 5
('text', 'texts'); -- 6 ('text', 'texts'); -- 6
INSERT INTO datatypes (dtname) VALUES INSERT INTO datatypes (dtname) VALUES
('int'), ('float'), ('decimal'), ('timestamp'); -- 7, 8, 9, 10 ('int'), ('decimal'), ('timestamp'); -- 7, 8, 9
INSERT INTO nodes (namespace, name) VALUES INSERT INTO nodes (namespace, name) VALUES
(2, 'type'), -- 1 (2, 'type'), -- 1

View file

@ -1,9 +1,9 @@
create or replace view vtriples as create or replace view vtriples as
select triples.id, select triples.id,
stype, svalue, show(stype, svalue) as stext, stype, svalue, show_value(stype, svalue) as stext,
predicate, show(dt.id, predicate) as ptext, predicate, show_value(dt.id, predicate) as ptext,
otype, ovalue, show(otype, ovalue) as otext, otype, ovalue, show_value(otype, ovalue) as otext,
events.tstamp as ts events.tstamp as ts
from triples, datatypes dt, events from triples, datatypes dt, events
where dtname = 'node' where dtname = 'node'