CREATE TABLE URI (
	"URI_ID" BIGINT  NOT NULL  GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1, NO CACHE ) , 
	"URI_STRING" VARCHAR (4000)  NOT NULL , PRIMARY KEY (URI_ID)  
)


CREATE TABLE OBJECT (
    URI_ID          bigint               PRIMARY KEY NOT NULL,
    CLASS_NAME      varchar(255)          NOT NULL,
    CONSTRAINT      FK_OBJECT_URI_ID
       FOREIGN KEY (URI_ID)
       REFERENCES   URI (URI_ID)
)

CREATE TABLE CHILDREN (
	"URI_ID" BIGINT  NOT NULL , 
	"CHILD_URI_ID" BIGINT  NOT NULL , 
	PRIMARY KEY (URI_ID, CHILD_URI_ID) ,   
	FOREIGN KEY (URI_ID) REFERENCES URI (URI_ID) ON DELETE NO ACTION ON UPDATE NO ACTION,   
	FOREIGN KEY (CHILD_URI_ID) REFERENCES URI (URI_ID) ON DELETE NO ACTION ON UPDATE NO ACTION 
) 

CREATE TABLE LINKS (
    URI_ID          bigint               NOT NULL,  
    LINK_TO_ID      bigint               NOT NULL,  
    PRIMARY KEY     (URI_ID, LINK_TO_ID),
    FOREIGN KEY (URI_ID) REFERENCES URI (URI_ID) ON DELETE NO ACTION ON UPDATE NO ACTION,   
    FOREIGN KEY (LINK_TO_ID) REFERENCES URI (URI_ID) ON DELETE NO ACTION ON UPDATE NO ACTION 
)

CREATE TABLE LOCKS (
    LOCK_ID         bigint               PRIMARY KEY NOT NULL,
    OBJECT_ID       bigint               NOT NULL ,
    SUBJECT_ID      bigint               NOT NULL ,
    TYPE_ID         bigint               NOT NULL,
    EXPIRATION_DATE timestamp             NOT NULL, 
    IS_INHERITABLE  char(1)                   NOT NULL, 
    IS_EXCLUSIVE    char(1)                   NOT NULL,
    
    FOREIGN KEY (OBJECT_ID) REFERENCES  URI (URI_ID),
    FOREIGN KEY (SUBJECT_ID) REFERENCES URI (URI_ID),
    FOREIGN KEY (TYPE_ID) REFERENCES   	URI (URI_ID),
    
    CONSTRAINT      FK_LOCKS_LOCK_ID
       FOREIGN KEY (LOCK_ID)
       REFERENCES   URI (URI_ID)
)

CREATE TABLE BRANCH ( 
    BRANCH_ID       bigint               GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1, NO CACHE )  PRIMARY KEY,
    BRANCH_STRING   varchar(255)          NOT NULL    
)

CREATE TABLE LABEL (
    LABEL_ID        bigint               GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1, NO CACHE )  PRIMARY KEY,
    LABEL_STRING    varchar(255)          NOT NULL
)

CREATE TABLE VERSION (
    URI_ID          bigint               PRIMARY KEY NOT NULL,
    IS_VERSIONED    char(1)                   NOT NULL,    
    CONSTRAINT      FK_VERSION_URI_ID
       FOREIGN KEY (URI_ID)
       REFERENCES   URI (URI_ID)
)

CREATE TABLE VERSION_HISTORY (
    VERSION_ID      bigint               GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1, NO CACHE ) UNIQUE,
    URI_ID          bigint               NOT NULL,
    BRANCH_ID       bigint               NOT NULL,
    REVISION_NO     VARCHAR(20)	       	  NOT NULL,
    FOREIGN KEY (URI_ID) REFERENCES   VERSION (URI_ID),
    FOREIGN KEY (BRANCH_ID) REFERENCES   BRANCH (BRANCH_ID),
    PRIMARY KEY (URI_ID, BRANCH_ID, REVISION_NO)
)

CREATE TABLE VERSION_PREDS (
    VERSION_ID         bigint            NOT NULL,
    PREDECESSOR_ID     bigint            NOT NULL,
        
      FOREIGN KEY (VERSION_ID) REFERENCES  VERSION_HISTORY (VERSION_ID),
      FOREIGN KEY (PREDECESSOR_ID) REFERENCES  VERSION_HISTORY (VERSION_ID),
    PRIMARY KEY (VERSION_ID, PREDECESSOR_ID)
)



CREATE TABLE VERSION_LABELS (
    	VERSION_ID         bigint            NOT NULL,
    	LABEL_ID           bigint            NOT NULL, 
	FOREIGN KEY (VERSION_ID) REFERENCES  VERSION_HISTORY (VERSION_ID),
      	FOREIGN KEY (LABEL_ID)   REFERENCES  LABEL (LABEL_ID),
    	PRIMARY KEY (VERSION_ID, LABEL_ID)
)

CREATE TABLE VERSION_CONTENT (
    VERSION_ID         bigint            PRIMARY KEY NOT NULL,
    CONTENT            BLOB(4M)  		NOT NULL,
    CONSTRAINT FK_VC_VERSION_ID 
        FOREIGN KEY (VERSION_ID)
        REFERENCES  VERSION_HISTORY(VERSION_ID)
)

CREATE TABLE PROPERTIES (
    VERSION_ID         bigint            NOT NULL,    
    PROPERTY_NAMESPACE varchar(50)        NOT NULL, 
    PROPERTY_NAME      varchar(50)        NOT NULL,        
    PROPERTY_VALUE     varchar(255)       NOT NULL,
    PROPERTY_TYPE      varchar(50)        NOT NULL, 
    IS_PROTECTED       char(1)                NOT NULL,
    FOREIGN KEY (VERSION_ID) REFERENCES  VERSION_HISTORY (VERSION_ID),
    PRIMARY KEY (VERSION_ID, PROPERTY_NAMESPACE, PROPERTY_NAME)
)

CREATE TABLE PERMISSIONS (
    OBJECT_ID       bigint               NOT NULL,
    SUBJECT_ID      bigint               NOT NULL,
    ACTION_ID       bigint               NOT NULL,
    VERSION_NO      VARCHAR(20),
    IS_INHERITABLE  char(1)                   NOT NULL,
    IS_NEGATIVE     char(1)                   NOT NULL,

	FOREIGN KEY (OBJECT_ID)	REFERENCES   URI (URI_ID),
      	FOREIGN KEY (SUBJECT_ID) REFERENCES   URI (URI_ID),
      	  FOREIGN KEY (ACTION_ID) REFERENCES   URI (URI_ID),
    PRIMARY KEY (OBJECT_ID, SUBJECT_ID, ACTION_ID)
)

