From 6879a9254c37728119258fdd5f67625d434bbc00 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Wed, 19 Feb 2020 08:55:08 +0100 Subject: [PATCH] use separate init.sql that only calls other scripts --- pgsql/fload.sql | 2 ++ pgsql/fshow.sql | 21 ++++++++++++++------- pgsql/init.sql | 27 +++------------------------ pgsql/lbase.sql | 28 ++++++++++++++++++++++++++++ pgsql/tcreate.sql | 15 +++++++++------ pgsql/vtriples.sql | 2 ++ 6 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 pgsql/lbase.sql diff --git a/pgsql/fload.sql b/pgsql/fload.sql index e9120d5..ed914ae 100644 --- a/pgsql/fload.sql +++ b/pgsql/fload.sql @@ -1,4 +1,6 @@ +-- create load functions + create or replace function get_value(tname text, vtext text) returns bigint as $$ begin case tname diff --git a/pgsql/fshow.sql b/pgsql/fshow.sql index 8923641..f0a9f48 100644 --- a/pgsql/fshow.sql +++ b/pgsql/fshow.sql @@ -1,4 +1,6 @@ +-- 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 @@ -10,13 +12,18 @@ 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); + return show_node(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; + + diff --git a/pgsql/init.sql b/pgsql/init.sql index 35129e4..5c6b0e7 100644 --- a/pgsql/init.sql +++ b/pgsql/init.sql @@ -1,32 +1,11 @@ +-- create tables and views \i tcreate.sql \i vtriples.sql +-- create functions \i fload.sql \i fshow.sql -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); +\i lbase.sql diff --git a/pgsql/lbase.sql b/pgsql/lbase.sql new file mode 100644 index 0000000..74a5feb --- /dev/null +++ b/pgsql/lbase.sql @@ -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); diff --git a/pgsql/tcreate.sql b/pgsql/tcreate.sql index 7d80590..5427b09 100644 --- a/pgsql/tcreate.sql +++ b/pgsql/tcreate.sql @@ -1,21 +1,23 @@ --- tables + +-- create tables CREATE TABLE namespaces ( id serial NOT NULL primary key, - iri text NOT NULL, - prefix text + iri text NOT NULL unique, + prefix text unique ); CREATE TABLE datatypes ( id smallserial NOT NULL primary key, - dtname text NOT NULL, + dtname text NOT NULL unique, tablename text ); CREATE TABLE nodes ( id bigserial NOT NULL primary key, namespace bigint REFERENCES namespaces, - name text + name text, + unique (namespace, name) ); CREATE TABLE events ( @@ -37,7 +39,7 @@ CREATE TABLE triples ( CREATE TABLE texts ( id bigserial NOT NULL primary key, language bigint REFERENCES nodes, - text text NOT NULL + text text NOT NULL unique ); -- 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 fki_language ON texts USING btree (language); +CREATE INDEX idx_texts ON texts USING btree (text); diff --git a/pgsql/vtriples.sql b/pgsql/vtriples.sql index 92a5c57..72c5d75 100644 --- a/pgsql/vtriples.sql +++ b/pgsql/vtriples.sql @@ -1,4 +1,6 @@ +-- create triples views + create or replace view vtriples as select triples.id, stype, svalue, show_value(stype, svalue) as stext,