2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *****************************************************************************
20 * Скрипт генерации структуры базы для хранения данных Stargazer-a
22 * $Id: 00-base-00.sql,v 1.7 2010/01/06 14:41:13 faust Exp $
25 * * dm_permission_flag. Представляет собой битовую маску - rw.
26 * r - чтение, w - изменение параметра.
27 * 0 - дествие запрещено, 1 - действие разрешено
29 * * dm_traff_type. Число определяющее тип подсчета трафика:
30 * 0 - up - считается по upload
31 * 1 - down - считается по download
32 * 2 - max - считается по максимальному среди upload/download
33 * 3 - up+down - считается по сумме upload и download
34 * Как альтернативу этому полю можно сделать еще одну таблицу - типов
35 * подсчета трафика. И в этом поле хранить ссылку на эту таблицу.
36 * Вопрос только "А надо ли это?"
38 * * dm_ip. IP адресс в виде четырех байтового целого числа со знаком.
39 * Выполнять приведение к знаковуму целому при занесении IP в БД!!!
41 * * dm_period. Задает периодичность показа сообщения пользователю.
42 * Период задается целым числом (int16). Если значение равно 0 то
43 * сообщение показывается только при подключении пользователя.
44 * Также этот домен определяет промежуток времени в течении которого
45 * сообщение показывается пользователю.
47 * * dm_session_event_type. Указывает тип записи в логе о сессии.
48 * 'c' - connect, 'd' - disconnect.
50 *****************************************************************************
54 * CONNECT 'localhost:/var/stg/stargazer.fdb' USER 'stg' PASSWORD '123456';
57 * CREATE DATABASE 'localhost:/var/stg/stargazer.fdb' USER 'stg' PASSWORD '123456' DEFAULT CHARACTER SET UTF8;
63 *****************************************************************************
64 * -= Создание ДОМЕНОВ =-
65 *****************************************************************************
68 CREATE DOMAIN dm_id AS INTEGER NOT NULL;
69 CREATE DOMAIN dm_null_id AS INTEGER;
70 CREATE DOMAIN dm_login AS VARCHAR(32) NOT NULL;
71 CREATE DOMAIN dm_tariff_name AS VARCHAR(32) NOT NULL;
72 CREATE DOMAIN dm_group_name AS VARCHAR(32);
73 CREATE DOMAIN dm_corporation_name AS VARCHAR(32);
74 CREATE DOMAIN dm_parameter_name AS VARCHAR(32);
76 CREATE DOMAIN dm_password AS VARCHAR(64) NOT NULL;
77 /* bitmask - rw => Read, Write */
78 CREATE DOMAIN dm_permission_flag AS SMALLINT NOT NULL
79 CHECK ( VALUE BETWEEN 0 AND 3 );
80 CREATE DOMAIN dm_money AS NUMERIC(10,6) NOT NULL;
81 /* (0, 1, 2, 3) => (up, down, max, up+down) */
82 CREATE DOMAIN dm_traff_type AS SMALLINT NOT NULL
83 CHECK ( VALUE BETWEEN 0 AND 3 );
84 CREATE DOMAIN dm_dir_num AS SMALLINT NOT NULL;
85 CREATE DOMAIN dm_num AS SMALLINT NOT NULL;
86 CREATE DOMAIN dm_traffic_mb AS INTEGER NOT NULL;
87 CREATE DOMAIN dm_traffic_byte AS BIGINT NOT NULL;
88 CREATE DOMAIN dm_time AS TIME NOT NULL;
89 CREATE DOMAIN dm_moment AS TIMESTAMP NOT NULL;
90 CREATE DOMAIN dm_credit_moment AS TIMESTAMP;
91 CREATE DOMAIN dm_ip AS INTEGER NOT NULL;
92 CREATE DOMAIN dm_mask AS INTEGER NOT NULL;
93 CREATE DOMAIN dm_user_address AS VARCHAR(256) DEFAULT '';
94 CREATE DOMAIN dm_bool AS CHAR(1) NOT NULL
95 CHECK ( VALUE IN ('0', '1', 't', 'f', 'T', 'F') );
96 CREATE DOMAIN dm_email AS VARCHAR(256) DEFAULT '';
97 CREATE DOMAIN dm_note AS VARCHAR(256) DEFAULT '';
98 CREATE DOMAIN dm_phone AS VARCHAR(256) DEFAULT '';
99 CREATE DOMAIN dm_user_name AS VARCHAR(256) DEFAULT '';
100 CREATE DOMAIN dm_service_comment AS VARCHAR(256) DEFAULT '';
101 CREATE DOMAIN dm_service_name AS VARCHAR(32) DEFAULT '';
102 /* TODO: why 0-31? Which is default? */
103 CREATE DOMAIN dm_pay_day AS SMALLINT NOT NULL
104 CHECK ( VALUE BETWEEN 0 AND 31 );
105 CREATE DOMAIN dm_period AS INTEGER NOT NULL;
106 CREATE DOMAIN dm_counter AS SMALLINT NOT NULL;
108 CREATE DOMAIN dm_message_ver AS INTEGER NOT NULL;
109 CREATE DOMAIN dm_message_type AS INTEGER NOT NULL;
111 CREATE DOMAIN dm_message AS VARCHAR(256) NOT NULL;
112 CREATE DOMAIN dm_user_data AS VARCHAR(256) NOT NULL;
113 CREATE DOMAIN dm_session_event_type AS CHAR(1) NOT NULL
114 CHECK ( VALUE IN ('c', 'd') );
115 CREATE DOMAIN dm_char_value AS VARCHAR(64) NOT NULL;
116 CREATE DOMAIN dm_date AS DATE NOT NULL;
121 *****************************************************************************
122 * -= Создание ТАБЛИЦ =-
123 *****************************************************************************
126 CREATE TABLE tb_admins
128 pk_admin dm_id PRIMARY KEY,
129 login dm_login UNIQUE,
131 chg_conf dm_permission_flag,
132 chg_password dm_permission_flag,
133 chg_stat dm_permission_flag,
134 chg_cash dm_permission_flag,
135 usr_add_del dm_permission_flag,
136 chg_tariff dm_permission_flag,
137 chg_admin dm_permission_flag,
138 chg_service dm_permission_flag,
139 chg_corporation dm_permission_flag
142 CREATE TABLE tb_tariffs
144 pk_tariff dm_id PRIMARY KEY,
145 name dm_tariff_name UNIQUE,
148 passive_cost dm_money,
149 traff_type dm_traff_type
152 CREATE TABLE tb_tariffs_params
154 pk_tariff_param dm_id PRIMARY KEY,
157 price_day_a dm_money,
158 price_day_b dm_money,
159 price_night_a dm_money,
160 price_night_b dm_money,
161 threshold dm_traffic_mb,
162 time_day_begins dm_time,
163 time_day_ends dm_time,
165 FOREIGN KEY (fk_tariff) REFERENCES tb_tariffs (pk_tariff)
168 CREATE TABLE tb_corporations
170 pk_corporation dm_id PRIMARY KEY,
171 name dm_corporation_name UNIQUE,
175 CREATE TABLE tb_users
177 pk_user dm_id PRIMARY KEY,
178 fk_tariff dm_null_id,
179 fk_tariff_change dm_null_id,
180 fk_corporation dm_null_id,
181 address dm_user_address,
182 always_online dm_bool,
184 credit_expire dm_credit_moment,
186 disabled_detail_stat dm_bool,
193 name dm_login UNIQUE,
194 real_name dm_user_name,
196 FOREIGN KEY (fk_tariff) REFERENCES tb_tariffs (pk_tariff),
197 FOREIGN KEY (fk_tariff_change) REFERENCES tb_tariffs (pk_tariff),
198 FOREIGN KEY (fk_corporation) REFERENCES tb_corporations (pk_corporation)
201 CREATE TABLE tb_detail_stats
203 pk_detail_stat dm_id PRIMARY KEY,
207 download dm_traffic_byte,
208 upload dm_traffic_byte,
213 FOREIGN KEY (fk_user) REFERENCES tb_users (pk_user)
216 CREATE TABLE tb_services
218 pk_service dm_id PRIMARY KEY,
219 name dm_service_name UNIQUE,
220 comment dm_service_comment,
225 CREATE TABLE tb_users_services
227 pk_user_service dm_id PRIMARY KEY,
231 FOREIGN KEY (fk_user) REFERENCES tb_users (pk_user),
232 FOREIGN KEY (fk_service) REFERENCES tb_services (pk_service)
235 CREATE TABLE tb_messages
237 pk_message dm_id PRIMARY KEY,
240 msg_type dm_message_type,
241 last_send_time dm_period,
242 creation_time dm_period,
245 repeat_period dm_period,
248 FOREIGN KEY (fk_user) REFERENCES tb_users (pk_user)
251 CREATE TABLE tb_stats
253 pk_stat dm_id PRIMARY KEY,
257 last_activity_time dm_moment,
258 last_cash_add dm_money,
259 last_cash_add_time dm_moment,
260 passive_time dm_period,
263 FOREIGN KEY (fk_user) REFERENCES tb_users (pk_user)
266 CREATE TABLE tb_stats_traffic
268 pk_stat_traffic dm_id PRIMARY KEY,
271 download dm_traffic_byte,
272 upload dm_traffic_byte,
274 FOREIGN KEY (fk_stat) REFERENCES tb_stats (pk_stat)
277 CREATE TABLE tb_users_data
279 pk_user_data dm_id PRIMARY KEY,
281 num dm_num, /* data_id dm_id renamed */
284 FOREIGN KEY (fk_user) REFERENCES tb_users (pk_user)
287 CREATE TABLE tb_allowed_ip
289 pk_allowed_ip dm_id PRIMARY KEY,
294 FOREIGN KEY (fk_user) REFERENCES tb_users (pk_user)
297 CREATE TABLE tb_sessions_log
299 pk_session_log dm_id PRIMARY KEY,
301 event_time dm_moment,
302 event_type dm_session_event_type,
305 FOREIGN KEY (fk_user) REFERENCES tb_users (pk_user)
308 CREATE TABLE tb_sessions_data
310 pk_session_data dm_id PRIMARY KEY,
311 fk_session_log dm_id,
313 session_upload dm_traffic_byte,
314 session_download dm_traffic_byte,
315 month_upload dm_traffic_byte,
316 month_download dm_traffic_byte,
318 FOREIGN KEY (fk_session_log) REFERENCES tb_sessions_log (pk_session_log)
321 CREATE TABLE tb_parameters
323 pk_parameter dm_id PRIMARY KEY,
324 name dm_parameter_name UNIQUE
327 CREATE TABLE tb_params_log
329 pk_param_log dm_id PRIMARY KEY,
332 event_time dm_moment,
333 from_val dm_char_value,
334 to_val dm_char_value,
335 comment dm_service_comment,
337 FOREIGN KEY (fk_user) REFERENCES tb_users (pk_user),
338 FOREIGN KEY (fk_parameter) REFERENCES tb_parameters (pk_parameter)
343 *****************************************************************************
344 * -= Создание ИНДЕКСОВ =-
345 *****************************************************************************
351 *****************************************************************************
352 * -= Создание ГЕНЕРАТОРОВ =-
353 *****************************************************************************
356 CREATE GENERATOR gn_pk_admin;
357 SET GENERATOR gn_pk_admin TO 0;
358 CREATE GENERATOR gn_pk_tariff;
359 SET GENERATOR gn_pk_tariff TO 0;
360 CREATE GENERATOR gn_pk_tariff_param;
361 SET GENERATOR gn_pk_tariff_param TO 0;
362 CREATE GENERATOR gn_pk_corporation;
363 SET GENERATOR gn_pk_corporation TO 0;
364 CREATE GENERATOR gn_pk_user;
365 SET GENERATOR gn_pk_user TO 0;
366 CREATE GENERATOR gn_pk_detail_stat;
367 SET GENERATOR gn_pk_detail_stat TO 0;
368 CREATE GENERATOR gn_pk_service;
369 SET GENERATOR gn_pk_service TO 0;
370 CREATE GENERATOR gn_pk_user_service;
371 SET GENERATOR gn_pk_user_service TO 0;
372 CREATE GENERATOR gn_pk_message;
373 SET GENERATOR gn_pk_message TO 0;
374 CREATE GENERATOR gn_pk_stat;
375 SET GENERATOR gn_pk_stat TO 0;
376 CREATE GENERATOR gn_pk_stat_traffic;
377 SET GENERATOR gn_pk_stat_traffic TO 0;
378 CREATE GENERATOR gn_pk_user_data;
379 SET GENERATOR gn_pk_user_data TO 0;
380 CREATE GENERATOR gn_pk_allowed_ip;
381 SET GENERATOR gn_pk_allowed_ip TO 0;
382 CREATE GENERATOR gn_pk_session;
383 SET GENERATOR gn_pk_session TO 0;
384 CREATE GENERATOR gn_pk_session_log;
385 SET GENERATOR gn_pk_session_log TO 0;
386 CREATE GENERATOR gn_pk_session_data;
387 SET GENERATOR gn_pk_session_data TO 0;
388 CREATE GENERATOR gn_pk_parameter;
389 SET GENERATOR gn_pk_parameter TO 0;
390 CREATE GENERATOR gn_pk_param_log;
391 SET GENERATOR gn_pk_param_log TO 0;
395 *****************************************************************************
396 * -= Создание ТРИГГЕРОВ =-
397 *****************************************************************************
401 CREATE TRIGGER tr_admin_bi FOR tb_admins
402 ACTIVE BEFORE INSERT POSITION 0
405 IF (new.pk_admin IS NULL)
406 THEN new.pk_admin = GEN_ID(gn_pk_admin, 1);
411 create trigger tr_tariff_bi for tb_tariffs active
412 before insert position 0
415 if (new.pk_tariff is null)
416 then new.pk_tariff = gen_id(gn_pk_tariff, 1);
421 create trigger tr_tariff_param_bi for tb_tariffs_params active
422 before insert position 0
425 if (new.pk_tariff_param is null)
426 then new.pk_tariff_param = gen_id(gn_pk_tariff_param, 1);
431 create trigger tr_corporation_bi for tb_corporations active
432 before insert position 0
435 if (new.pk_corporation is null)
436 then new.pk_corporation = gen_id(gn_pk_corporation, 1);
441 create trigger tr_user_bi for tb_users active
442 before insert position 0
445 if (new.pk_user is null)
446 then new.pk_user = gen_id(gn_pk_user, 1);
451 create trigger tr_detail_stat_bi for tb_detail_stats active
452 before insert position 0
455 if (new.pk_detail_stat is null)
456 then new.pk_detail_stat = gen_id(gn_pk_detail_stat, 1);
461 create trigger tr_service_bi for tb_services active
462 before insert position 0
465 if (new.pk_service is null)
466 then new.pk_service = gen_id(gn_pk_service, 1);
471 create trigger tr_user_service_bi for tb_users_services active
472 before insert position 0
475 if (new.pk_user_service is null)
476 then new.pk_user_service = gen_id(gn_pk_user_service, 1);
481 create trigger tr_message_bi for tb_messages active
482 before insert position 0
485 if (new.pk_message is null)
486 then new.pk_message = gen_id(gn_pk_message, 1);
491 create trigger tr_stat_bi for tb_stats active
492 before insert position 0
495 if (new.pk_stat is null)
496 then new.pk_stat = gen_id(gn_pk_stat, 1);
501 create trigger tr_stat_traffic_bi for tb_stats_traffic active
502 before insert position 0
505 if (new.pk_stat_traffic is null)
506 then new.pk_stat_traffic = gen_id(gn_pk_stat_traffic, 1);
511 create trigger tr_user_data_bi for tb_users_data active
512 before insert position 0
515 if (new.pk_user_data is null)
516 then new.pk_user_data = gen_id(gn_pk_user_data, 1);
521 create trigger tr_allowed_ip_bi for tb_allowed_ip active
522 before insert position 0
525 if (new.pk_allowed_ip is null)
526 then new.pk_allowed_ip = gen_id(gn_pk_allowed_ip, 1);
531 create trigger tr_session_log_bi for tb_sessions_log active
532 before insert position 0
535 if (new.pk_session_log is null)
536 then new.pk_session_log = gen_id(gn_pk_session_log, 1);
541 create trigger tr_session_data_bi for tb_sessions_data active
542 before insert position 0
545 if (new.pk_session_data is null)
546 then new.pk_session_data = gen_id(gn_pk_session_data, 1);
551 create trigger tr_parameter_bi for tb_parameters active
552 before insert position 0
555 if (new.pk_parameter is null)
556 then new.pk_parameter = gen_id(gn_pk_parameter, 1);
561 create trigger tr_param_log_bi for tb_params_log active
562 before insert position 0
565 if (new.pk_param_log is null)
566 then new.pk_param_log = gen_id(gn_pk_param_log, 1);
571 *****************************************************************************
572 * -= Создание stored procedure =-
573 *****************************************************************************
577 * Add a message returning it's ID
580 create procedure sp_add_message(pk_message integer, login varchar(32), ver integer, msg_type integer, last_send_time integer, creation_time integer, show_time integer, repeat integer, repeat_period integer, msg_text varchar(256))
584 if (:pk_message is null) then
586 pk_message = gen_id(gn_pk_message, 1);
587 insert into tb_messages values (:pk_message,
588 (select pk_user from tb_users where name = :login),
600 update tb_messages set fk_user = (select pk_user from tb_users where name = :login),
602 msg_type = :msg_type,
603 last_send_time = :last_send_time,
604 creation_time = :creation_time,
605 show_time = :show_time,
606 repeat = :repeat_period,
607 repeat_period = :repeat_period,
609 where pk_message = :pk_message;
616 create procedure sp_delete_service(name varchar(32))
618 declare variable pk_service integer;
620 select pk_service from tb_services where name = :name into pk_service;
621 if (pk_service is not null) then
623 delete from tb_users_services where fk_service = :pk_service;
624 delete from tb_services where pk_service = :pk_service;
630 create procedure sp_add_tariff(name varchar(32), dirs integer)
632 declare variable pk_tariff integer;
634 pk_tariff = gen_id(gn_pk_tariff, 1);
635 insert into tb_tariffs (pk_tariff, name, fee, free, passive_cost, traff_type) values (:pk_tariff, :name, 0, 0, 0, 0);
638 insert into tb_tariffs_params (fk_tariff, dir_num, price_day_a,
639 price_day_b, price_night_a, price_night_b,
640 threshold, time_day_begins, time_day_ends)
641 values (:pk_tariff, :dirs - 1, 0, 0, 0, 0, 0, '0:0', '0:0');
648 create procedure sp_delete_tariff(name varchar(32))
650 declare variable pk_tariff integer;
652 select pk_tariff from tb_tariffs where name = :name into pk_tariff;
653 if (pk_tariff is not null) then
655 delete from tb_tariffs_params where fk_tariff = :pk_tariff;
656 delete from tb_tariffs where pk_tariff = :pk_tariff;
662 create procedure sp_add_user(name varchar(32), dirs integer)
664 declare variable pk_user integer;
665 declare variable pk_stat integer;
667 pk_user = gen_id(gn_pk_user, 1);
668 insert into tb_users(pk_user, fk_tariff, fk_tariff_change, fk_corporation, address, always_online, credit, credit_expire, disabled, disabled_detail_stat, email, grp, note, passive, passwd, phone, name, real_name) values (:pk_user, NULL, NULL, NULL, '', 0, 0, 'now', 0, 0, '', '_', '', 0, '', '', :name, '');
669 pk_stat = gen_id(gn_pk_stat, 1);
670 insert into tb_stats values (:pk_stat, :pk_user, 0, 0, 'now', 0, 'now', 0, 'now');
673 insert into tb_stats_traffic (fk_stat, dir_num, upload, download) values (:pk_stat, :dirs - 1, 0, 0);
680 create procedure sp_delete_user(name varchar(32))
682 declare variable pk_user integer;
684 select pk_user from tb_users where name = :name into pk_user;
685 if (pk_user is not null) then
687 delete from tb_users_services where fk_user = :pk_user;
688 delete from tb_params_log where fk_user = :pk_user;
689 delete from tb_detail_stats where fk_user = :pk_user;
690 delete from tb_stats_traffic where fk_stat in (select pk_stat from tb_stats where fk_user = :pk_user);
691 delete from tb_stats where fk_user = :pk_user;
692 delete from tb_sessions_data where fk_session_log in (select pk_session_log from tb_sessions_log where fk_user = :pk_user);
693 delete from tb_sessions_log where fk_user = :pk_user;
694 delete from tb_allowed_ip where fk_user = :pk_user;
695 delete from tb_users_data where fk_user = :pk_user;
696 delete from tb_messages where fk_user = :pk_user;
697 delete from tb_users where pk_user = :pk_user;
703 create procedure sp_append_session_log(name varchar(32), event_time timestamp, event_type char(1), ip integer)
704 returns(pk_session_log integer)
707 pk_session_log = gen_id(gn_pk_session_log, 1);
708 insert into tb_sessions_log (pk_session_log, fk_user, event_time, event_type, ip) values (:pk_session_log, (select pk_user from tb_users where name = :name), :event_time, :event_type, :ip);
713 create procedure sp_add_stat(name varchar(32), cash numeric(10,6), free_mb numeric(10,6), last_activity_time timestamp, last_cash_add numeric(10,6), last_cash_add_time timestamp, passive_time integer, stats_date date)
714 returns(pk_stat integer)
717 pk_stat = gen_id(gn_pk_stat, 1);
718 insert into tb_stats (pk_stat, fk_user, cash, free_mb, last_activity_time, last_cash_add, last_cash_add_time, passive_time, stats_date) values (:pk_stat, (select pk_user from tb_users where name = :name), :cash, :free_mb, :last_activity_time, :last_cash_add, :last_cash_add_time, :passive_time, :stats_date);
723 *****************************************************************************
724 * -= Создание администратора =-
725 *****************************************************************************
728 insert into tb_admins values(0, 'admin', 'geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmk', 1, 1, 1, 1, 1, 1, 1, 1, 1);