
-- create functions for loading cities data as triples

create or replace function load_cities() returns int as $$
declare 
  count int := 0;
  event bigint := new_event();
  city record;
  key text;
begin
  for city in
    select name, regiokey, ckey, inhabitants from cities
      where rectype = '60'
      --limit 1
  loop
    key := 'xcd:' || city.regiokey;
    insert into vtriples (stname, stext, ptext, otname, otext, creation) values 
      ('node', key, 'rdf:type', 'node', 'xc:City', event),
      ('node', key, 'rdfs:label', 'text', city.name, event),
      ('node', key, 'xc:regiokey', 'text', city.regiokey, event),
      ('node', key, 'xc:ckey', 'text', city.ckey, event),
      ('node', key, 'xc:population', 'int', text(city.inhabitants), event);
    count := count + 1;
  end loop;
  return count;
end;
$$ language plpgsql;

