
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;
