use new load_triple() for initial creation of nodes and triples

This commit is contained in:
Helmut Merz 2020-02-18 19:55:43 +01:00
parent ddad67a8f7
commit b60b4765f2
4 changed files with 45 additions and 72 deletions

View file

@ -1,4 +1,6 @@
-- drop tables in the correct order, avoiding constraint violations
-- drop views and tables in the correct order, avoiding constraint violations
drop view vtriples;
drop table texts;
drop table triples;

View file

@ -25,7 +25,7 @@ begin
where ns.prefix = pfx
and nodes.namespace = ns.id
and nodes.name = vname;
if value is null then
if not found then
insert into nodes (namespace, name)
(select ns.id, vname from namespaces ns where ns.prefix = pfx)
returning nodes.id into value;
@ -46,15 +46,15 @@ declare
ov bigint;
trid bigint;
begin
select dt.id into st from datatypes dt where dt.dtname = stname;
select dt.id into ot from datatypes dt where dt.dtname = otname;
select dt.id into st from datatypes dt where dtname = stname;
select dt.id into ot from datatypes dt where dtname = otname;
sv := get_value(stname, stext);
pred := get_value(stname, ptext);
pred := get_value('node', ptext);
ov := get_value(otname, otext);
select tr.id into trid from triples tr
where stype = st and svalue = sv and predicate = pred
and otype = ot and ovalue = ov;
if trid is null then
if not found then
insert into triples (stype, svalue, predicate, otype, ovalue, creation)
values (st, sv, pred, ot, ov, event)
returning triples.id into trid;
@ -64,8 +64,6 @@ end;
$$ language plpgsql;
create or replace function get_event(out evt bigint) as $$
begin
insert into events default values returning id into evt;
end;
$$ language plpgsql;
create or replace function get_event() returns bigint as $$
insert into events default values returning id;
$$ language sql;

25
pgsql/import.sql Normal file
View file

@ -0,0 +1,25 @@
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);

View file

@ -14,7 +14,7 @@ CREATE TABLE datatypes (
CREATE TABLE nodes (
id bigserial NOT NULL primary key,
namespace bigint,
namespace bigint REFERENCES namespaces,
name text
);
@ -25,21 +25,23 @@ CREATE TABLE events (
CREATE TABLE triples (
id bigserial NOT NULL primary key,
stype smallint NOT NULL,
stype smallint NOT NULL REFERENCES datatypes,
svalue bigint NOT NULL,
predicate bigint NOT NULL,
otype smallint NOT NULL,
predicate bigint NOT NULL REFERENCES nodes,
otype smallint NOT NULL REFERENCES datatypes,
ovalue bigint,
creation bigint NOT NULL,
deletion bigint
creation bigint NOT NULL REFERENCES events,
deletion bigint REFERENCES events
);
CREATE TABLE texts (
id bigserial NOT NULL primary key,
language bigint,
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;
@ -47,38 +49,6 @@ ALTER TABLE triples OWNER TO cco;
ALTER TABLE events OWNER TO cco;
ALTER TABLE texts OWNER TO cco;
-- data
INSERT INTO events default values;
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
INSERT INTO nodes (namespace, name) VALUES
(2, 'type'), -- 1
(2, 'Property'), -- 2
(3, 'Class'); -- 3
-- basic triples: type assignments for
-- type -> Property; Property, Class -> Class
INSERT INTO triples (stype, svalue, predicate, otype, ovalue, creation) VALUES
(2, 1, 1, 2, 2, 1),
(2, 2, 1, 2, 3, 1),
(2, 3, 1, 2, 3, 1);
-- indexes
CREATE INDEX idx_iri ON namespaces USING btree (iri);
@ -88,8 +58,6 @@ CREATE INDEX idx_dtname ON datatypes USING btree (dtname);
CREATE INDEX idx_node_name ON nodes USING btree (namespace, name);
CREATE INDEX fki_stype ON triples USING btree (stype);
CREATE INDEX fki_otype ON triples USING btree (otype);
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
@ -98,23 +66,3 @@ 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);
-- foreign key constraints
ALTER TABLE ONLY nodes
ADD CONSTRAINT namespace FOREIGN KEY (namespace) REFERENCES namespaces(id);
ALTER TABLE ONLY triples
ADD CONSTRAINT stype FOREIGN KEY (stype) REFERENCES datatypes(id);
ALTER TABLE ONLY triples
ADD CONSTRAINT predicate FOREIGN KEY (predicate) REFERENCES nodes(id);
ALTER TABLE ONLY triples
ADD CONSTRAINT otype FOREIGN KEY (otype) REFERENCES datatypes(id);
ALTER TABLE ONLY triples
ADD CONSTRAINT creation FOREIGN KEY (creation) REFERENCES events(id);
ALTER TABLE ONLY triples
ADD CONSTRAINT deletion FOREIGN KEY (deletion) REFERENCES events(id);
ALTER TABLE ONLY texts
ADD CONSTRAINT language FOREIGN KEY (language) REFERENCES nodes(id);