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 (
rectype int,
ctype int,
rectype int not null,
ctype int not null,
regiokey text not null primary key,
ckey text not null,
name text,
name text not null,
area decimal,
inhabitants int,
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);
begin
case tname
when 'node' then
return (
select ns.prefix || ':' || n.name
from nodes n
join namespaces ns on ns.id = n.namespace
where n.id = show.id
);
else return tname;
end case;
return show_value(tname, id);
end;
$$ language plpgsql;
create or replace function show_value(tname text, id bigint) returns text as $$
begin
case tname
when 'node' then
return (
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;
$$ language plpgsql;

View file

@ -64,7 +64,7 @@ INSERT INTO datatypes (dtname, tablename) VALUES
('triple', 'triples'), -- 5
('text', 'texts'); -- 6
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
(2, 'type'), -- 1

View file

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