
-- create show functions

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
  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 show_node(id);
    when 'text' then
      return show_text(id);
    else 
      return text(show_value.id);
  end case;
end;
$$ language plpgsql;


create or replace function show_node(nid bigint) returns text as $$
  select ns.prefix || ':' || n.name from nodes n
    join namespaces ns on ns.id = n.namespace
    where n.id = nid;
$$ language sql;


create or replace function show_text(tid bigint) returns text as $$
  select text from texts
    where texts.id = tid;
$$ language sql;


create or replace function show_type(tid smallint) returns text as $$
  select dtname from datatypes dt
    where dt.id = tid;
$$ language sql stable;
