diff --git a/examples/cities/pgsql/init_cities.sql b/examples/cities/pgsql/init_cities.sql index 0f034e2..f65524a 100644 --- a/examples/cities/pgsql/init_cities.sql +++ b/examples/cities/pgsql/init_cities.sql @@ -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, diff --git a/pgsql/fload.sql b/pgsql/fload.sql new file mode 100644 index 0000000..08dc5d4 --- /dev/null +++ b/pgsql/fload.sql @@ -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; diff --git a/pgsql/fshow.sql b/pgsql/fshow.sql index 224192c..8923641 100644 --- a/pgsql/fshow.sql +++ b/pgsql/fshow.sql @@ -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; diff --git a/pgsql/init.sql b/pgsql/init.sql index 72faa4c..ff0e654 100644 --- a/pgsql/init.sql +++ b/pgsql/init.sql @@ -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 diff --git a/pgsql/vtriples.sql b/pgsql/vtriples.sql index 7b872b1..92a5c57 100644 --- a/pgsql/vtriples.sql +++ b/pgsql/vtriples.sql @@ -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'