/* Server version: WI-V6.3.0.26074 Firebird 2.5 
   SQLDialect: 3. ODS: 11.2. Forced writes: On. Sweep inteval: 20000.
   Page size: 4096. Cache pages: 2048 (8192 Kb). Read-only: False. */
SET NAMES WIN1251;

SET SQL DIALECT 3;

CONNECT 'c:\DeliveryRK7\DB\DELIVERYRK7.FDB' USER 'SYSDBA' PASSWORD 'masterkey';



SET AUTODDL ON;

/* Create Domains... */
CREATE DOMAIN D_GUIDSTR AS VARCHAR(38);

CREATE DOMAIN D_STRING_36 AS VARCHAR(36);

CREATE DOMAIN D_STRING_40 AS VARCHAR(40);

/* Drop Index... */
RECONNECT;

/* Drop: DLV_ADDRESSES_ZIP (TIdxData) */
DROP INDEX DLV_ADDRESSES_ZIP;


/* Create Table... */
CREATE TABLE DLV_ORDER_EVENTS(ORDER_EVENT_ID BIGINT NOT NULL,
ORDER_ID D_INT64,
PERSONAL D_INT64,
PERSONAL_NAME D_STRING_100,
EVENT_TYPE D_INTEGER,
EVENT_NAME D_STRING_100,
ORDERGUID D_GUIDSTR,
EVENT_VALUE D_STRING_100,
EVENT_DT D_DATETIME);



ALTER TABLE DLV_EXPEDITORS_WORK ADD PHONE D_STRING_50;

ALTER TABLE DLV_ORDERS ADD START_COOK_TIME D_DATETIME;

ALTER TABLE DLV_ORDERS ADD END_COOK_TIME D_DATETIME;

ALTER TABLE DLV_ORDERS ADD PREPAY D_DOUBLE;

ALTER TABLE DLV_ORDERS ADD UNPAY D_DOUBLE;

ALTER TABLE DLV_ORDERS ADD PROMISED D_DOUBLE;

ALTER TABLE DLV_ZONES ADD ZCOLOR D_INTEGER;

ALTER TABLE DLV_ZONES ADD ZONE_GUIDSTR D_GUIDSTR;

ALTER TABLE DLV_ADDRESSES ALTER COLUMN ZIP TYPE D_STRING_40;

/* String-length truncation not supported:
ALTER TABLE DLV_ORDERS ALTER COLUMN RK7GUID TYPE D_GUIDSTR */
/* Create index... */
CREATE INDEX DLV_ORDER_EVENTS_IDX1 ON DLV_ORDER_EVENTS(ORDERGUID);

CREATE INDEX DLV_ORDER_EVENTS_IDX2 ON DLV_ORDER_EVENTS(EVENT_TYPE);

CREATE INDEX DLV_ORDER_EVENTS_IDX3 ON DLV_ORDER_EVENTS(EVENT_DT);

CREATE INDEX DLV_ORDER_EVENTS_IDX4 ON DLV_ORDER_EVENTS(ORDER_ID);

CREATE INDEX DLV_ORDERS_IDX_ECT ON DLV_ORDERS(END_COOK_TIME);

CREATE INDEX DLV_ORDERS_IDX_RDT ON DLV_ORDERS(REAL_DELIVERY_TIME);

CREATE INDEX DLV_ORDERS_IDX_SCT ON DLV_ORDERS(START_COOK_TIME);


/* Create generator... */
CREATE GENERATOR GEN_DLV_ORDER_EVENTS_ID;


/* Create Primary Key... */
ALTER TABLE DLV_ORDER_EVENTS ADD CONSTRAINT PK_DLV_ORDER_EVENTS PRIMARY KEY (ORDER_EVENT_ID);

/* Creating trigger... */
SET TERM ^ ;

CREATE TRIGGER DLV_ORDERS_BI0 FOR DLV_ORDERS
ACTIVE BEFORE INSERT POSITION 0 
AS
begin
  /* Trigger text */
  if (new.date_insert is null) then
    new.date_insert=current_timestamp;
  if (new.date_modify is null) then
    new.date_modify=current_timestamp;
end
^

CREATE TRIGGER DLV_ORDERS_BU0 FOR DLV_ORDERS
ACTIVE BEFORE UPDATE POSITION 0 
AS
begin
  if ((New.date_modify is null) or (New.date_modify=Old.date_modify)) then
     NEW.DATE_MODIFY=current_timestamp;
end
^


/* Create(Add) Crant */
SET TERM ; ^


GRANT ALL ON DLV_ORDER_EVENTS TO SYSDBA WITH GRANT OPTION;


