CREATE SCHEMA IF NOT EXISTS BASIC;

CREATE TABLE BASIC.ATTRIBUTE_GROUP (
	ID SERIAL PRIMARY KEY,
	GROUP_TYPE VARCHAR(64) NOT NULL DEFAULT '',
	GROUP_NAME VARCHAR(64) NOT NULL DEFAULT '',
	IS_REQUIRED SMALLINT NOT NULL DEFAULT -1,

    UNIQUE(GROUP_TYPE)
);

CREATE TABLE BASIC.DATA_TYPE (
	ID SERIAL PRIMARY KEY,
	DATA_TYPE VARCHAR(64) NOT NULL DEFAULT '',
	DATABASE_TYPE VARCHAR(32) NOT NULL DEFAULT '',

    UNIQUE(DATA_TYPE)
);

CREATE TABLE BASIC.ATTRIBUTE (
	ID SERIAL PRIMARY KEY,
	ATTRIBUTE VARCHAR(128) NOT NULL DEFAULT '',
	DATA_TYPE_ID INTEGER NOT NULL DEFAULT -1,
	LENGTH_PRECISION INTEGER NOT NULL DEFAULT -1,
	SCALE INTEGER NOT NULL DEFAULT -1,
	DEFAULT_VALUE VARCHAR(64) NOT NULL DEFAULT '',
	VALUE_RANGE VARCHAR(128) NOT NULL DEFAULT '[,]',
	ATTRIBUTE_GROUP_ID INTEGER NOT NULL DEFAULT -1,
	IS_NOT_NULL INTEGER NOT NULL DEFAULT -1,
	IS_INDEX INTEGER NOT NULL DEFAULT -1,

    UNIQUE(ATTRIBUTE),

	FOREIGN KEY (DATA_TYPE_ID) REFERENCES BASIC.DATA_TYPE (ID),
	FOREIGN KEY (ATTRIBUTE_GROUP_ID) REFERENCES BASIC.ATTRIBUTE_GROUP (ID)
);

CREATE TABLE BASIC.ATTRIBUTE_TEMPLATE (
	ID SERIAL PRIMARY KEY,
	TEMPLATE_NAME VARCHAR(64) NOT NULL DEFAULT '',
	DATA_TYPE_ID INTEGER NOT NULL DEFAULT -1,
	LENGTH_PRECISION VARCHAR(16) NOT NULL DEFAULT '',
	SCALE VARCHAR(16) NOT NULL DEFAULT '',
	DEFAULT_VALUE VARCHAR(64) NOT NULL DEFAULT '',
	VALUE_RANGE VARCHAR(128) NOT NULL DEFAULT '',
	ATTRIBUTE_GROUP_ID INTEGER NOT NULL DEFAULT -1,
	IS_NOT_NULL INTEGER NOT NULL DEFAULT -1,
	IS_INDEX INTEGER NOT NULL DEFAULT -1,

	FOREIGN KEY (DATA_TYPE_ID) REFERENCES BASIC.DATA_TYPE (ID),
	FOREIGN KEY (ATTRIBUTE_GROUP_ID) REFERENCES BASIC.ATTRIBUTE_GROUP (ID)
);

CREATE TABLE BASIC.MODEL_ATTRIBUTE (
	ID SERIAL PRIMARY KEY,
	MODEL_TYPE VARCHAR(64) NOT NULL DEFAULT '',
	ATTRIBUTE_ID INTEGER NOT NULL DEFAULT -1,

	FOREIGN KEY (ATTRIBUTE_ID) REFERENCES BASIC.ATTRIBUTE (ID)
);

CREATE TABLE BASIC.MODEL_CONNECTIVITY (
	ID SERIAL PRIMARY KEY,
	FROM_MODEL VARCHAR (64) NOT NULL DEFAULT '',
	TO_MODEL VARCHAR(64) NOT NULL DEFAULT '',
	CONNECTIVITY INTEGER NOT NULL DEFAULT -1
);
