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_PUBLIC SMALLINT NOT NULL DEFAULT -1,
	REMARK VARCHAR(128) NOT NULL DEFAULT '',
	
	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.MODEL_TYPE (
	ID SERIAL PRIMARY KEY,
	MODEL_TYPE VARCHAR(64) NOT NULL DEFAULT '',
	MODEL_NAME VARCHAR(64) NOT NULL DEFAULT '',
	REMARK VARCHAR(128) NOT NULL DEFAULT '',
	
	UNIQUE (MODEL_TYPE)
);

CREATE TABLE BASIC.MAPPING_MODEL_GROUP (
	ID SERIAL PRIMARY KEY,
	MODEL_TYPE_ID INTEGER NOT NULL DEFAULT -1,
	ATTRIBUTE_GROUP_ID INTEGER NOT NULL DEFAULT -1,
	
	UNIQUE (MODEL_TYPE_ID, ATTRIBUTE_GROUP_ID),

	FOREIGN KEY (MODEL_TYPE_ID) REFERENCES BASIC.MODEL_TYPE (ID),
	FOREIGN KEY (ATTRIBUTE_GROUP_ID) REFERENCES BASIC.ATTRIBUTE_GROUP (ID)
);

CREATE TABLE BASIC.ATTRIBUTE (
	ID SERIAL PRIMARY KEY,
	ATTRIBUTE_GROUP_ID INTEGER NOT NULL DEFAULT -1,
	ATTRIBUTE VARCHAR(128) NOT NULL DEFAULT '',
	ATTRIBUTE_NAME VARCHAR(64) NOT NULL DEFAULT '',
	DATA_TYPE_ID INTEGER NOT NULL DEFAULT -1,
	LENGTH_PRECISION INTEGER NOT NULL DEFAULT 0,
	SCALE INTEGER NOT NULL DEFAULT 0,
	IS_NOT_NULL SMALLINT NOT NULL DEFAULT -1,
	DEFAULT_VALUE VARCHAR(64) NOT NULL DEFAULT '',
	VALUE_RANGE VARCHAR(128) NOT NULL DEFAULT ',',
	
	UNIQUE (ATTRIBUTE_GROUP_ID, ATTRIBUTE),
    UNIQUE (ATTRIBUTE, ATTRIBUTE_NAME),
	
	FOREIGN KEY (ATTRIBUTE_GROUP_ID) REFERENCES BASIC.ATTRIBUTE_GROUP (ID),
	FOREIGN KEY (DATA_TYPE_ID) REFERENCES BASIC.DATA_TYPE (ID)
);

CREATE TABLE BASIC.ATTRIBUTE_TEMPLATE (
	ID SERIAL PRIMARY KEY,
	TEMPLATE_NAME VARCHAR(64) NOT NULL DEFAULT '',
	ATTRIBUTE_GROUP_ID INTEGER NOT NULL DEFAULT -1,
	DATA_TYPE_ID INTEGER NOT NULL DEFAULT -1,
	LENGTH_PRECISION INTEGER NOT NULL DEFAULT 0,
	SCALE INTEGER NOT NULL DEFAULT 0,
	IS_NOT_NULL SMALLINT NOT NULL DEFAULT -1,
	DEFAULT_VALUE VARCHAR(64) NOT NULL DEFAULT '',
	VALUE_RANGE VARCHAR(128) NOT NULL DEFAULT '',
	
	FOREIGN KEY (ATTRIBUTE_GROUP_ID) REFERENCES BASIC.ATTRIBUTE_GROUP (ID),
	FOREIGN KEY (DATA_TYPE_ID) REFERENCES BASIC.DATA_TYPE (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 SMALLINT NOT NULL DEFAULT -1
);