SET SQL DIALECT 3;

SET NAMES ISO8859_1;

CREATE DATABASE 'REPLICATE.FDB'
PAGE_SIZE 4096
DEFAULT CHARACTER SET ISO8859_1;

/******************************************************************************/
/****                               Domains                                ****/
/******************************************************************************/

CREATE DOMAIN GENSMALLINT AS
SMALLINT
DEFAULT 0
NOT NULL;

CREATE DOMAIN OP AS
CHAR(1);

CREATE DOMAIN PATH AS
VARCHAR(255);

CREATE DOMAIN PKID AS
INTEGER
NOT NULL;

CREATE DOMAIN PKIDBIG AS
BIGINT
NOT NULL;

CREATE DOMAIN TABLENAME AS
VARCHAR(32);

CREATE DOMAIN USERNAME AS
VARCHAR(50);



/******************************************************************************/
/****                              Generators                              ****/
/******************************************************************************/

CREATE GENERATOR GEN_LOC_ID;

CREATE GENERATOR GEN_REPLDEFS_ID;

CREATE GENERATOR GEN_SOURCE_LOCATION_ID;



/******************************************************************************/
/****                                Tables                                ****/
/******************************************************************************/



CREATE TABLE LOCATIONS (
    LOC_ID           PKID NOT NULL,
    LOC_PATH         PATH,
    RAS_SERVICENAME  USERNAME,
    RAS_USER         USERNAME,
    RAS_PASSWORD     USERNAME,
    USERNAME         USERNAME,
    PASSWD           USERNAME
);


CREATE TABLE REPLDEFS (
    ID         PKID NOT NULL,
    FK_SOURCE  PKID NOT NULL,
    TABLENAME  TABLENAME,
    OPTYPE     OP,
    SQLSTMT    PATH,
    MORE       GENSMALLINT
);


CREATE TABLE SOURCE_LOCATION (
    ID             PKID NOT NULL,
    SOURCE_SERVER  PATH,
    SOURCE_PATH    PATH,
    USERNAME       USERNAME,
    PASSWD         USERNAME,
    REPLUSER       USERNAME,
    REPLPASSWD     USERNAME
);




/******************************************************************************/
/****                             Primary Keys                             ****/
/******************************************************************************/

ALTER TABLE LOCATIONS ADD PRIMARY KEY (LOC_ID);
ALTER TABLE REPLDEFS ADD CONSTRAINT PK_REPLDEFS PRIMARY KEY (ID);
ALTER TABLE SOURCE_LOCATION ADD PRIMARY KEY (ID);


/******************************************************************************/
/****                             Foreign Keys                             ****/
/******************************************************************************/

ALTER TABLE REPLDEFS ADD CONSTRAINT FK_REPLDEFS FOREIGN KEY (FK_SOURCE) REFERENCES SOURCE_LOCATION (ID) ON DELETE CASCADE;


/******************************************************************************/
/****                               Indices                                ****/
/******************************************************************************/

CREATE INDEX REPLDEFS_IDXTABLENAME ON REPLDEFS (TABLENAME);


/******************************************************************************/
/****                               Triggers                               ****/
/******************************************************************************/


SET TERM ^ ;


/******************************************************************************/
/****                         Triggers for tables                          ****/
/******************************************************************************/



/* Trigger: INSERT_LOCATIONS */
CREATE TRIGGER INSERT_LOCATIONS FOR LOCATIONS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  NEW.LOC_ID = GEN_ID( GEN_LOC_ID, 1 );
END
^


/* Trigger: REPLDEFS_BI */
CREATE TRIGGER REPLDEFS_BI FOR REPLDEFS
ACTIVE BEFORE INSERT POSITION 0
AS BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GEN_REPLDEFS_ID,1);
END
^


/* Trigger: SOURCE_LOCATION_BI */
CREATE TRIGGER SOURCE_LOCATION_BI FOR SOURCE_LOCATION
ACTIVE BEFORE INSERT POSITION 0
AS BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GEN_SOURCE_LOCATION_ID,1);
END
^


SET TERM ; ^
