-- 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,
    name text
);

CREATE TABLE events (
    id bigserial NOT NULL primary key,
    tstamp timestamp NOT NULL   // ??? better timestamptz?
);

CREATE TABLE triples (
    id bigserial NOT NULL primary key,
    stype smallint NOT NULL,
    svalue bigint NOT NULL,
    predicate bigint NOT NULL,
    otype smallint NOT NULL,
    ovalue bigint,
    creation bigint NOT NULL,
    deletion bigint
);

CREATE TABLE texts (
    id bigserial NOT NULL primary key,
    language bigint,
    text text NOT NULL
);

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;

-- data

INSERT INTO events (tstamp) values (transaction_timestamp());

INSERT INTO namespaces (iri, prefix) 
    VALUES ('http://cyberconcepts.org/cco-common#', 'cco'); -- 1
INSERT INTO namespaces (iri, prefix)
    VALUES ('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf'); -- 2
INSERT INTO namespaces (iri, prefix) 
    VALUES ('http://www.w3.org/2000/01/rdf-schema#', 'rdfs'); -- 3

INSERT INTO datatypes (dtname, tablename) VALUES ('namespace', 'namespaces'); -- 1
INSERT INTO datatypes (dtname, tablename) VALUES ('node', 'nodes'); -- 2
INSERT INTO datatypes (dtname, tablename) VALUES ('datatype', 'datatypes'); -- 3
INSERT INTO datatypes (dtname, tablename) VALUES ('event', 'events'); -- 4
INSERT INTO datatypes (dtname, tablename) VALUES ('triple', 'triples'); -- 5
INSERT INTO datatypes (dtname, tablename) VALUES ('text', 'texts'); -- 6
INSERT INTO datatypes (dtname) VALUES ('int');             -- 7
INSERT INTO datatypes (dtname) VALUES ('float');           -- 8
INSERT INTO datatypes (dtname) VALUES ('decimal');         -- 9
INSERT INTO datatypes (dtname) VALUES ('timestamp');       -- 10

INSERT INTO nodes (namespace, name) VALUES (2, 'type');    -- 1
INSERT INTO nodes (namespace, name) VALUES (2, 'Property');-- 2
INSERT INTO nodes (namespace, name) VALUES (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, 2, 2, 2, 1);
INSERT INTO triples (stype, svalue, predicate, otype, ovalue, creation)
    VALUES (2, 2, 2, 2, 3, 1);
INSERT INTO triples (stype, svalue, predicate, otype, ovalue, creation) 
    VALUES (2, 3, 2, 2, 3, 1);

-- indexes

CREATE INDEX idx_iri ON namespaces USING btree (iri);
CREATE INDEX idx_prefix ON namespaces USING btree (prefix);

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 
    (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);

-- 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);

