use separate init.sql that only calls other scripts

This commit is contained in:
Helmut Merz 2020-02-19 08:55:08 +01:00
parent 8956bdb35e
commit 6879a9254c
6 changed files with 58 additions and 37 deletions

View file

@ -1,4 +1,6 @@
-- create load functions
create or replace function get_value(tname text, vtext text) returns bigint as $$ create or replace function get_value(tname text, vtext text) returns bigint as $$
begin begin
case tname case tname

View file

@ -1,4 +1,6 @@
-- create show functions
create or replace function show_value(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); declare tname text := (select dtname from datatypes as dt where dt.id = type);
begin begin
@ -10,13 +12,18 @@ create or replace function show_value(tname text, id bigint) returns text as $$
begin begin
case tname case tname
when 'node' then when 'node' then
return ( return show_node(id);
select ns.prefix || ':' || n.name else
from nodes n return text(show_value.id);
join namespaces ns on ns.id = n.namespace
where n.id = show_value.id
);
else return text(show_value.id);
end case; end case;
end; end;
$$ language plpgsql; $$ 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;

View file

@ -1,32 +1,11 @@
-- create tables and views
\i tcreate.sql \i tcreate.sql
\i vtriples.sql \i vtriples.sql
-- create functions
\i fload.sql \i fload.sql
\i fshow.sql \i fshow.sql
select new_event() as event
\gset
-- load data -- load data
\i lbase.sql
select load_namespace('http://cyberconcepts.org/cco-common#', 'cco');
select load_namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf');
select load_namespace('http://www.w3.org/2000/01/rdf-schema#', 'rdfs');
select load_datatype('namespace', 'namespaces');
select load_datatype('node', 'nodes');
select load_datatype('datatype', 'datatypes');
select load_datatype('event', 'events');
select load_datatype('triple', 'triples');
select load_datatype('text', 'texts');
select load_datatype('int');
select load_datatype('decimal');
select load_datatype('timestamp');
-- basic triples: type assignments for
-- type -> Property; Property, Class -> Class
select load_triple('node', 'rdf:type', 'rdf:type', 'node', 'rdf:Property', :event);
select load_triple('node', 'rdf:Property', 'rdf:type', 'node', 'rdfs:Class', :event);
select load_triple('node', 'rdfs:Class', 'rdf:type', 'node', 'rdfs:Class', :event);

28
pgsql/lbase.sql Normal file
View file

@ -0,0 +1,28 @@
-- load base data
select new_event() as event
\gset
-- load data
select load_namespace('http://cyberconcepts.org/cco-common#', 'cco');
select load_namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf');
select load_namespace('http://www.w3.org/2000/01/rdf-schema#', 'rdfs');
select load_datatype('namespace', 'namespaces');
select load_datatype('node', 'nodes');
select load_datatype('datatype', 'datatypes');
select load_datatype('event', 'events');
select load_datatype('triple', 'triples');
select load_datatype('text', 'texts');
select load_datatype('int');
select load_datatype('decimal');
select load_datatype('timestamp');
-- basic triples: type assignments for
-- type -> Property; Property, Class -> Class
select load_triple('node', 'rdf:type', 'rdf:type', 'node', 'rdf:Property', :event);
select load_triple('node', 'rdf:Property', 'rdf:type', 'node', 'rdfs:Class', :event);
select load_triple('node', 'rdfs:Class', 'rdf:type', 'node', 'rdfs:Class', :event);

View file

@ -1,21 +1,23 @@
-- tables
-- create tables
CREATE TABLE namespaces ( CREATE TABLE namespaces (
id serial NOT NULL primary key, id serial NOT NULL primary key,
iri text NOT NULL, iri text NOT NULL unique,
prefix text prefix text unique
); );
CREATE TABLE datatypes ( CREATE TABLE datatypes (
id smallserial NOT NULL primary key, id smallserial NOT NULL primary key,
dtname text NOT NULL, dtname text NOT NULL unique,
tablename text tablename text
); );
CREATE TABLE nodes ( CREATE TABLE nodes (
id bigserial NOT NULL primary key, id bigserial NOT NULL primary key,
namespace bigint REFERENCES namespaces, namespace bigint REFERENCES namespaces,
name text name text,
unique (namespace, name)
); );
CREATE TABLE events ( CREATE TABLE events (
@ -37,7 +39,7 @@ CREATE TABLE triples (
CREATE TABLE texts ( CREATE TABLE texts (
id bigserial NOT NULL primary key, id bigserial NOT NULL primary key,
language bigint REFERENCES nodes, language bigint REFERENCES nodes,
text text NOT NULL text text NOT NULL unique
); );
-- set table owner -- set table owner
@ -66,3 +68,4 @@ CREATE INDEX idx_po ON triples USING btree (predicate, otype, ovalue);
CREATE INDEX idx_os ON triples USING btree (otype, ovalue, stype, svalue); CREATE INDEX idx_os ON triples USING btree (otype, ovalue, stype, svalue);
CREATE INDEX fki_language ON texts USING btree (language); CREATE INDEX fki_language ON texts USING btree (language);
CREATE INDEX idx_texts ON texts USING btree (text);

View file

@ -1,4 +1,6 @@
-- create triples views
create or replace view vtriples as create or replace view vtriples as
select triples.id, select triples.id,
stype, svalue, show_value(stype, svalue) as stext, stype, svalue, show_value(stype, svalue) as stext,