function improvements; init.sql as central initilization script
This commit is contained in:
parent
b60b4765f2
commit
8956bdb35e
4 changed files with 150 additions and 93 deletions
|
|
@ -13,11 +13,10 @@ end;
|
||||||
$$ language plpgsql;
|
$$ 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
|
declare
|
||||||
pfx text;
|
pfx text;
|
||||||
vname text;
|
vname text;
|
||||||
value bigint;
|
|
||||||
begin
|
begin
|
||||||
pfx := split_part(vtext, ':', 1);
|
pfx := split_part(vtext, ':', 1);
|
||||||
vname := split_part(vtext, ':', 2);
|
vname := split_part(vtext, ':', 2);
|
||||||
|
|
@ -30,7 +29,6 @@ begin
|
||||||
(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;
|
||||||
end if;
|
end if;
|
||||||
return value;
|
|
||||||
end;
|
end;
|
||||||
$$ language plpgsql;
|
$$ language plpgsql;
|
||||||
|
|
||||||
|
|
@ -44,13 +42,21 @@ declare
|
||||||
pred bigint;
|
pred bigint;
|
||||||
ot smallint;
|
ot smallint;
|
||||||
ov bigint;
|
ov bigint;
|
||||||
trid bigint;
|
|
||||||
begin
|
begin
|
||||||
select dt.id into st from datatypes dt where dtname = stname;
|
st := get_datatype(stname);
|
||||||
select dt.id into ot from datatypes dt where dtname = otname;
|
ot := get_datatype(otname);
|
||||||
sv := get_value(stname, stext);
|
sv := get_value(stname, stext);
|
||||||
pred := get_value('node', ptext);
|
pred := get_value('node', ptext);
|
||||||
ov := get_value(otname, otext);
|
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
|
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;
|
||||||
|
|
@ -59,11 +65,55 @@ begin
|
||||||
values (st, sv, pred, ot, ov, event)
|
values (st, sv, pred, ot, ov, event)
|
||||||
returning triples.id into trid;
|
returning triples.id into trid;
|
||||||
end if;
|
end if;
|
||||||
return trid;
|
|
||||||
end;
|
end;
|
||||||
$$ language plpgsql;
|
$$ 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;
|
insert into events default values returning id;
|
||||||
$$ language sql;
|
$$ language sql;
|
||||||
|
|
|
||||||
|
|
@ -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);
|
|
||||||
|
|
@ -1,68 +1,32 @@
|
||||||
-- tables
|
|
||||||
|
|
||||||
CREATE TABLE namespaces (
|
\i tcreate.sql
|
||||||
id serial NOT NULL primary key,
|
\i vtriples.sql
|
||||||
iri text NOT NULL,
|
|
||||||
prefix text
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE datatypes (
|
\i fload.sql
|
||||||
id smallserial NOT NULL primary key,
|
\i fshow.sql
|
||||||
dtname text NOT NULL,
|
|
||||||
tablename text
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE nodes (
|
select new_event() as event
|
||||||
id bigserial NOT NULL primary key,
|
\gset
|
||||||
namespace bigint REFERENCES namespaces,
|
|
||||||
name text
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE events (
|
-- load data
|
||||||
id bigserial NOT NULL primary key,
|
|
||||||
tstamp timestamptz default current_timestamp
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE triples (
|
select load_namespace('http://cyberconcepts.org/cco-common#', 'cco');
|
||||||
id bigserial NOT NULL primary key,
|
select load_namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf');
|
||||||
stype smallint NOT NULL REFERENCES datatypes,
|
select load_namespace('http://www.w3.org/2000/01/rdf-schema#', 'rdfs');
|
||||||
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
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE texts (
|
select load_datatype('namespace', 'namespaces');
|
||||||
id bigserial NOT NULL primary key,
|
select load_datatype('node', 'nodes');
|
||||||
language bigint REFERENCES nodes,
|
select load_datatype('datatype', 'datatypes');
|
||||||
text text NOT NULL
|
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;
|
select load_triple('node', 'rdf:type', 'rdf:type', 'node', 'rdf:Property', :event);
|
||||||
ALTER TABLE datatypes OWNER TO cco;
|
select load_triple('node', 'rdf:Property', 'rdf:type', 'node', 'rdfs:Class', :event);
|
||||||
ALTER TABLE namespaces OWNER TO cco;
|
select load_triple('node', 'rdfs:Class', 'rdf:type', 'node', 'rdfs:Class', :event);
|
||||||
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);
|
|
||||||
|
|
|
||||||
68
pgsql/tcreate.sql
Normal file
68
pgsql/tcreate.sql
Normal file
|
|
@ -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);
|
||||||
Loading…
Add table
Reference in a new issue