use new load_triple() for initial creation of nodes and triples
This commit is contained in:
parent
ddad67a8f7
commit
b60b4765f2
4 changed files with 45 additions and 72 deletions
|
|
@ -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 texts;
|
||||||
drop table triples;
|
drop table triples;
|
||||||
|
|
@ -25,7 +25,7 @@ begin
|
||||||
where ns.prefix = pfx
|
where ns.prefix = pfx
|
||||||
and nodes.namespace = ns.id
|
and nodes.namespace = ns.id
|
||||||
and nodes.name = vname;
|
and nodes.name = vname;
|
||||||
if value is null then
|
if not found then
|
||||||
insert into nodes (namespace, name)
|
insert into nodes (namespace, name)
|
||||||
(select ns.id, vname from namespaces ns where ns.prefix = pfx)
|
(select ns.id, vname from namespaces ns where ns.prefix = pfx)
|
||||||
returning nodes.id into value;
|
returning nodes.id into value;
|
||||||
|
|
@ -46,15 +46,15 @@ declare
|
||||||
ov bigint;
|
ov bigint;
|
||||||
trid bigint;
|
trid bigint;
|
||||||
begin
|
begin
|
||||||
select dt.id into st from datatypes dt where dt.dtname = stname;
|
select dt.id into st from datatypes dt where dtname = stname;
|
||||||
select dt.id into ot from datatypes dt where dt.dtname = otname;
|
select dt.id into ot from datatypes dt where dtname = otname;
|
||||||
sv := get_value(stname, stext);
|
sv := get_value(stname, stext);
|
||||||
pred := get_value(stname, ptext);
|
pred := get_value('node', ptext);
|
||||||
ov := get_value(otname, otext);
|
ov := get_value(otname, otext);
|
||||||
select tr.id into trid from triples tr
|
select tr.id into trid from triples tr
|
||||||
where stype = st and svalue = sv and predicate = pred
|
where stype = st and svalue = sv and predicate = pred
|
||||||
and otype = ot and ovalue = ov;
|
and otype = ot and ovalue = ov;
|
||||||
if trid is null then
|
if not found then
|
||||||
insert into triples (stype, svalue, predicate, otype, ovalue, creation)
|
insert into triples (stype, svalue, predicate, otype, ovalue, creation)
|
||||||
values (st, sv, pred, ot, ov, event)
|
values (st, sv, pred, ot, ov, event)
|
||||||
returning triples.id into trid;
|
returning triples.id into trid;
|
||||||
|
|
@ -64,8 +64,6 @@ end;
|
||||||
$$ language plpgsql;
|
$$ language plpgsql;
|
||||||
|
|
||||||
|
|
||||||
create or replace function get_event(out evt bigint) as $$
|
create or replace function get_event() returns bigint as $$
|
||||||
begin
|
insert into events default values returning id;
|
||||||
insert into events default values returning id into evt;
|
$$ language sql;
|
||||||
end;
|
|
||||||
$$ language plpgsql;
|
|
||||||
|
|
|
||||||
25
pgsql/import.sql
Normal file
25
pgsql/import.sql
Normal 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);
|
||||||
|
|
@ -14,7 +14,7 @@ CREATE TABLE datatypes (
|
||||||
|
|
||||||
CREATE TABLE nodes (
|
CREATE TABLE nodes (
|
||||||
id bigserial NOT NULL primary key,
|
id bigserial NOT NULL primary key,
|
||||||
namespace bigint,
|
namespace bigint REFERENCES namespaces,
|
||||||
name text
|
name text
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -25,21 +25,23 @@ CREATE TABLE events (
|
||||||
|
|
||||||
CREATE TABLE triples (
|
CREATE TABLE triples (
|
||||||
id bigserial NOT NULL primary key,
|
id bigserial NOT NULL primary key,
|
||||||
stype smallint NOT NULL,
|
stype smallint NOT NULL REFERENCES datatypes,
|
||||||
svalue bigint NOT NULL,
|
svalue bigint NOT NULL,
|
||||||
predicate bigint NOT NULL,
|
predicate bigint NOT NULL REFERENCES nodes,
|
||||||
otype smallint NOT NULL,
|
otype smallint NOT NULL REFERENCES datatypes,
|
||||||
ovalue bigint,
|
ovalue bigint,
|
||||||
creation bigint NOT NULL,
|
creation bigint NOT NULL REFERENCES events,
|
||||||
deletion bigint
|
deletion bigint REFERENCES events
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE texts (
|
CREATE TABLE texts (
|
||||||
id bigserial NOT NULL primary key,
|
id bigserial NOT NULL primary key,
|
||||||
language bigint,
|
language bigint REFERENCES nodes,
|
||||||
text text NOT NULL
|
text text NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- set table owner
|
||||||
|
|
||||||
ALTER TABLE nodes OWNER TO cco;
|
ALTER TABLE nodes OWNER TO cco;
|
||||||
ALTER TABLE datatypes OWNER TO cco;
|
ALTER TABLE datatypes OWNER TO cco;
|
||||||
ALTER TABLE namespaces 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 events OWNER TO cco;
|
||||||
ALTER TABLE texts 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
|
-- indexes
|
||||||
|
|
||||||
CREATE INDEX idx_iri ON namespaces USING btree (iri);
|
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 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_creation ON triples USING btree (creation);
|
||||||
CREATE INDEX fki_deletion ON triples USING btree (deletion);
|
CREATE INDEX fki_deletion ON triples USING btree (deletion);
|
||||||
CREATE INDEX idx_spo ON triples USING btree
|
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 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);
|
||||||
|
|
||||||
-- 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);
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue