From 8956bdb35e69dcba034bbc554ac985c15c2374f4 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Wed, 19 Feb 2020 07:56:54 +0100 Subject: [PATCH] function improvements; init.sql as central initilization script --- pgsql/fload.sql | 66 ++++++++++++++++++++++++++++++++----- pgsql/import.sql | 25 -------------- pgsql/init.sql | 84 ++++++++++++++--------------------------------- pgsql/tcreate.sql | 68 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+), 93 deletions(-) delete mode 100644 pgsql/import.sql create mode 100644 pgsql/tcreate.sql diff --git a/pgsql/fload.sql b/pgsql/fload.sql index de71c2c..e9120d5 100644 --- a/pgsql/fload.sql +++ b/pgsql/fload.sql @@ -13,11 +13,10 @@ end; $$ language plpgsql; -create or replace function get_node(vtext text) returns bigint as $$ +create or replace function get_node(vtext text, out value bigint) as $$ declare pfx text; vname text; - value bigint; begin pfx := split_part(vtext, ':', 1); vname := split_part(vtext, ':', 2); @@ -30,7 +29,6 @@ begin (select ns.id, vname from namespaces ns where ns.prefix = pfx) returning nodes.id into value; end if; - return value; end; $$ language plpgsql; @@ -44,13 +42,21 @@ declare pred bigint; ot smallint; ov bigint; - trid bigint; begin - select dt.id into st from datatypes dt where dtname = stname; - select dt.id into ot from datatypes dt where dtname = otname; + st := get_datatype(stname); + ot := get_datatype(otname); sv := get_value(stname, stext); pred := get_value('node', ptext); ov := get_value(otname, otext); + return load_triple(st, sv, pred, ot, ov, event); +end; +$$ language plpgsql; + + +create or replace function load_triple( + st smallint, sv bigint, pred bigint, + ot smallint, ov bigint, event bigint, out trid bigint) as $$ +begin select tr.id into trid from triples tr where stype = st and svalue = sv and predicate = pred and otype = ot and ovalue = ov; @@ -59,11 +65,55 @@ begin values (st, sv, pred, ot, ov, event) returning triples.id into trid; end if; - return trid; end; $$ language plpgsql; -create or replace function get_event() returns bigint as $$ +create or replace function load_namespace( + nsiri text, nspfx text, out nsid int) as $$ +begin + select ns.id into nsid from namespaces ns + where iri = nsiri and prefix = nspfx; + if not found then + insert into namespaces as ns (iri, prefix) + values (nsiri, nspfx) + returning ns.id into nsid; + end if; +end; +$$ language plpgsql; + + +create or replace function load_datatype( + name text, tname text, out dtid smallint) as $$ +begin + dtid := get_datatype(name); + if dtid is null then + insert into datatypes as dt (dtname, tablename) + values (name, tname) + returning dt.id into dtid; + end if; +end; +$$ language plpgsql; + + +create or replace function load_datatype(name text, out dtid smallint) as $$ +begin + dtid := get_datatype(name); + if dtid is null then + insert into datatypes as dt (dtname) values (name) + returning dt.id into dtid; + end if; +end; +$$ language plpgsql; + + +-- simple SQL functions + +create or replace function get_datatype(name text) returns smallint as $$ + select dt.id from datatypes dt where dtname = name; +$$ language sql; + + +create or replace function new_event() returns bigint as $$ insert into events default values returning id; $$ language sql; diff --git a/pgsql/import.sql b/pgsql/import.sql deleted file mode 100644 index 4a15ca8..0000000 --- a/pgsql/import.sql +++ /dev/null @@ -1,25 +0,0 @@ - -select get_event() as event -\gset - -INSERT INTO namespaces (iri, prefix) VALUES - ('http://cyberconcepts.org/cco-common#', 'cco'), -- 1 - ('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf'), -- 2 - ('http://www.w3.org/2000/01/rdf-schema#', 'rdfs'); -- 3 - -INSERT INTO datatypes (dtname, tablename) VALUES - ('namespace', 'namespaces'), -- 1 - ('node', 'nodes'), -- 2 - ('datatype', 'datatypes'), -- 3 - ('event', 'events'), -- 4 - ('triple', 'triples'), -- 5 - ('text', 'texts'); -- 6 -INSERT INTO datatypes (dtname) VALUES - ('int'), ('decimal'), ('timestamp'); -- 7, 8, 9 - --- 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/init.sql b/pgsql/init.sql index f3e28af..35129e4 100644 --- a/pgsql/init.sql +++ b/pgsql/init.sql @@ -1,68 +1,32 @@ --- tables -CREATE TABLE namespaces ( - id serial NOT NULL primary key, - iri text NOT NULL, - prefix text -); +\i tcreate.sql +\i vtriples.sql -CREATE TABLE datatypes ( - id smallserial NOT NULL primary key, - dtname text NOT NULL, - tablename text -); +\i fload.sql +\i fshow.sql -CREATE TABLE nodes ( - id bigserial NOT NULL primary key, - namespace bigint REFERENCES namespaces, - name text -); +select new_event() as event +\gset -CREATE TABLE events ( - id bigserial NOT NULL primary key, - tstamp timestamptz default current_timestamp -); +-- load data -CREATE TABLE triples ( - id bigserial NOT NULL primary key, - stype smallint NOT NULL REFERENCES datatypes, - svalue bigint NOT NULL, - predicate bigint NOT NULL REFERENCES nodes, - otype smallint NOT NULL REFERENCES datatypes, - ovalue bigint, - creation bigint NOT NULL REFERENCES events, - deletion bigint REFERENCES events -); +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'); -CREATE TABLE texts ( - id bigserial NOT NULL primary key, - language bigint REFERENCES nodes, - text text NOT NULL -); +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'); --- set table owner +-- basic triples: type assignments for +-- type -> Property; Property, Class -> Class -ALTER TABLE nodes OWNER TO cco; -ALTER TABLE datatypes OWNER TO cco; -ALTER TABLE namespaces OWNER TO cco; -ALTER TABLE triples OWNER TO cco; -ALTER TABLE events OWNER TO cco; -ALTER TABLE texts OWNER TO cco; - --- indexes - -CREATE INDEX idx_iri ON namespaces USING btree (iri); -CREATE INDEX idx_prefix ON namespaces USING btree (prefix); - -CREATE INDEX idx_dtname ON datatypes USING btree (dtname); - -CREATE INDEX idx_node_name ON nodes USING btree (namespace, name); - -CREATE INDEX fki_creation ON triples USING btree (creation); -CREATE INDEX fki_deletion ON triples USING btree (deletion); -CREATE INDEX idx_spo ON triples USING btree - (stype, svalue, predicate, otype, ovalue); -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); +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 new file mode 100644 index 0000000..7d80590 --- /dev/null +++ b/pgsql/tcreate.sql @@ -0,0 +1,68 @@ +-- tables + +CREATE TABLE namespaces ( + id serial NOT NULL primary key, + iri text NOT NULL, + prefix text +); + +CREATE TABLE datatypes ( + id smallserial NOT NULL primary key, + dtname text NOT NULL, + tablename text +); + +CREATE TABLE nodes ( + id bigserial NOT NULL primary key, + namespace bigint REFERENCES namespaces, + name text +); + +CREATE TABLE events ( + id bigserial NOT NULL primary key, + tstamp timestamptz default current_timestamp +); + +CREATE TABLE triples ( + id bigserial NOT NULL primary key, + stype smallint NOT NULL REFERENCES datatypes, + svalue bigint NOT NULL, + predicate bigint NOT NULL REFERENCES nodes, + otype smallint NOT NULL REFERENCES datatypes, + ovalue bigint NOT NULL, + creation bigint NOT NULL REFERENCES events, + deletion bigint REFERENCES events +); + +CREATE TABLE texts ( + id bigserial NOT NULL primary key, + language bigint REFERENCES nodes, + text text NOT NULL +); + +-- set table owner + +ALTER TABLE nodes OWNER TO cco; +ALTER TABLE datatypes OWNER TO cco; +ALTER TABLE namespaces OWNER TO cco; +ALTER TABLE triples OWNER TO cco; +ALTER TABLE events OWNER TO cco; +ALTER TABLE texts OWNER TO cco; + +-- indexes + +CREATE INDEX idx_iri ON namespaces USING btree (iri); +CREATE INDEX idx_prefix ON namespaces USING btree (prefix); + +CREATE INDEX idx_dtname ON datatypes USING btree (dtname); + +CREATE INDEX idx_node_name ON nodes USING btree (namespace, name); + +CREATE INDEX fki_creation ON triples USING btree (creation); +CREATE INDEX fki_deletion ON triples USING btree (deletion); +CREATE INDEX idx_spo ON triples USING btree + (stype, svalue, predicate, otype, ovalue); +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);