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_GUIID AS CHAR(16) CHARACTER SET OCTETS;

/* Create Table... */

CREATE TABLE DLV_SMS(SMS_ID D_INT64 NOT NULL,
SMS_CREATE D_DATETIME,
SMS_SENDED D_DATETIME,
SMS_DELIVERED D_DATETIME,
FOR_STATUS D_SMALLINT,
ORDER_ID D_INT64,
ORDER_GUID D_STRING_50,
CLIENT_ID D_INT64,
PHONENUMBER D_STRING_25,
SMS_TEXT D_STRING_255);



ALTER TABLE DLV_CCEVENTS ADD ANY_ID D_INT64;

ALTER TABLE DLV_CCEVENTS ADD ANY_NAME D_STRING_100;

ALTER TABLE DLV_CCEVENTS ADD EXECUTE_TIME D_DATETIME;

ALTER TABLE DLV_EXPEDITORS_WORK ADD ROUTE D_GUIID;

ALTER TABLE DLV_ORDERS ADD RK7GUID D_STRING_50;

ALTER TABLE DLV_ORDERS ADD SMSPHONEID D_INT64;

ALTER TABLE DLV_PERSONALS ADD PERSONAL_GUID D_GUIID;

ALTER TABLE DLV_PERSONALS ADD REST_AVAILABLE D_STRING_255;

ALTER TABLE DLV_RESTAURANTS ADD DLVBASE D_STRING_100;

UPDATE RDB$RELATION_FIELDS SET RDB$Collation_ID =
(Select C.Rdb$Collation_ID from Rdb$Collations C
where C.Rdb$Collation_Name='WIN1251'
 and C.Rdb$Character_Set_ID = (select Rdb$Character_Set_ID from Rdb$character_Sets where Rdb$character_set_name = 'WIN1251'))
  WHERE RDB$FIELD_NAME='TYPE_NAME' AND RDB$RELATION_NAME='DLV_ADDRESSES';

UPDATE RDB$RELATION_FIELDS SET RDB$Collation_ID =
(Select C.Rdb$Collation_ID from Rdb$Collations C
where C.Rdb$Collation_Name='WIN1251'
 and C.Rdb$Character_Set_ID = (select Rdb$Character_Set_ID from Rdb$character_Sets where Rdb$character_set_name = 'WIN1251'))
  WHERE RDB$FIELD_NAME='TYPE_NAME' AND RDB$RELATION_NAME='DLV_CLIENT_EMAILS';

UPDATE RDB$RELATION_FIELDS SET RDB$Collation_ID =
(Select C.Rdb$Collation_ID from Rdb$Collations C
where C.Rdb$Collation_Name='WIN1251'
 and C.Rdb$Character_Set_ID = (select Rdb$Character_Set_ID from Rdb$character_Sets where Rdb$character_set_name = 'WIN1251'))
  WHERE RDB$FIELD_NAME='TYPE_NAME' AND RDB$RELATION_NAME='DLV_CLIENT_PHONES';

/* Create index... */
CREATE INDEX DLV_CCEVENTS_DONE_TIME ON DLV_CCEVENTS(DONETIME);

CREATE INDEX DLV_CCEVENTS_ORDER ON DLV_CCEVENTS(ORDER_ID);

CREATE INDEX DLV_SMS_IDX_CREATE ON DLV_SMS(SMS_CREATE);

CREATE INDEX DLV_SMS_IDX_DONE ON DLV_SMS(SMS_DELIVERED);

CREATE INDEX DLV_SMS_IDX_ORDER ON DLV_SMS(ORDER_ID);

CREATE INDEX DLV_SMS_IDX_SENDED ON DLV_SMS(SMS_SENDED);


/* Create generator... */


CREATE GENERATOR GEN_DLV_SMS_ID;


/* Create Primary Key... */

ALTER TABLE DLV_SMS ADD CONSTRAINT PK_DLV_SMS PRIMARY KEY (SMS_ID);

/* Create(Add) Crant */

GRANT ALL ON DLV_SMS TO SYSDBA WITH GRANT OPTION;

commit work;
