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
23 * * dm_permission_flag. Представляет собой битовую маску - rw.
24 * r - чтение, w - изменение параметра.
25 * 0 - дествие запрещено, 1 - действие разрешено
27 * * dm_traff_type. Число определяющее тип подсчета трафика:
28 * 0 - up - считается по upload
29 * 1 - down - считается по download
30 * 2 - max - считается по максимальному среди upload/download
31 * 3 - up+down - считается по сумме upload и download
33 * * dm_session_event_type. Указывает тип записи в логе о сессии.
34 * 'c' - connect, 'd' - disconnect.
36 * * При занесении IP адресса в БД выполнять приведение к
39 *****************************************************************************
44 * $Date: 2009/08/20 14:58:43 $
49 *****************************************************************************
50 * -= Создание типов и доменов =-
51 *****************************************************************************
54 CREATE DOMAIN dm_name AS VARCHAR(32) NOT NULL;
55 CREATE DOMAIN dm_password AS VARCHAR(64) NOT NULL;
56 CREATE DOMAIN dm_permission_flag AS SMALLINT NOT NULL
57 CHECK ( value BETWEEN 0 AND 3 );
58 CREATE DOMAIN dm_money AS NUMERIC(12, 4) NOT NULL DEFAULT 0;
59 CREATE DOMAIN dm_traff_type AS SMALLINT NOT NULL
60 CHECK ( value BETWEEN 0 AND 3 );
61 CREATE DOMAIN dm_day AS SMALLINT NOT NULL
62 CHECK ( value BETWEEN 0 AND 31 )
64 CREATE DOMAIN dm_session_event_type AS CHAR(1) NOT NULL
65 CHECK ( value = 'c' OR value = 'd' );
68 *****************************************************************************
69 * -= Создание таблиц =-
70 *****************************************************************************
75 version INTEGER NOT NULL
78 CREATE TABLE tb_admins
80 pk_admin SERIAL PRIMARY KEY,
82 passwd dm_password NOT NULL,
83 chg_conf dm_permission_flag,
84 chg_password dm_permission_flag,
85 chg_stat dm_permission_flag,
86 chg_cash dm_permission_flag,
87 usr_add_del dm_permission_flag,
88 chg_tariff dm_permission_flag,
89 chg_admin dm_permission_flag,
90 chg_service dm_permission_flag,
91 chg_corporation dm_permission_flag
94 CREATE TABLE tb_tariffs
96 pk_tariff SERIAL PRIMARY KEY,
100 passive_cost dm_money,
101 traff_type dm_traff_type
104 CREATE TABLE tb_tariffs_params
106 pk_tariff_param SERIAL PRIMARY KEY,
107 fk_tariff INTEGER NOT NULL,
108 dir_num SMALLINT NOT NULL,
109 price_day_a dm_money,
110 price_day_b dm_money,
111 price_night_a dm_money,
112 price_night_b dm_money,
113 threshold INTEGER NOT NULL,
114 time_day_begins TIME NOT NULL,
115 time_day_ends TIME NOT NULL,
117 FOREIGN KEY (fk_tariff)
118 REFERENCES tb_tariffs (pk_tariff)
122 CREATE TABLE tb_corporations
124 pk_corporation SERIAL PRIMARY KEY,
129 CREATE TABLE tb_users
131 pk_user SERIAL PRIMARY KEY,
133 fk_tariff_change INTEGER,
134 fk_corporation INTEGER,
135 address VARCHAR(256) NOT NULL,
136 always_online BOOLEAN NOT NULL,
138 credit_expire TIMESTAMP NOT NULL,
139 disabled BOOLEAN NOT NULL,
140 disabled_detail_stat BOOLEAN NOT NULL,
141 email VARCHAR(256) NOT NULL,
144 passive BOOLEAN NOT NULL,
146 phone VARCHAR(256) NOT NULL,
148 real_name VARCHAR(256) NOT NULL,
151 last_activity_time TIMESTAMP NOT NULL,
152 last_cash_add dm_money,
153 last_cash_add_time TIMESTAMP NOT NULL,
154 passive_time INTEGER NOT NULL,
156 FOREIGN KEY (fk_tariff)
157 REFERENCES tb_tariffs (pk_tariff)
159 FOREIGN KEY (fk_tariff_change)
160 REFERENCES tb_tariffs (pk_tariff)
162 FOREIGN KEY (fk_corporation)
163 REFERENCES tb_corporations (pk_corporation)
167 CREATE TABLE tb_detail_stats
169 pk_detail_stat BIGSERIAL PRIMARY KEY,
170 fk_user INTEGER NOT NULL,
171 dir_num SMALLINT NOT NULL,
173 download BIGINT NOT NULL,
174 upload BIGINT NOT NULL,
176 from_time TIMESTAMP NOT NULL,
177 till_time TIMESTAMP NOT NULL,
179 FOREIGN KEY (fk_user)
180 REFERENCES tb_users (pk_user)
184 CREATE TABLE tb_services
186 pk_service SERIAL PRIMARY KEY,
188 comment TEXT NOT NULL,
193 CREATE TABLE tb_users_services
195 pk_user_service SERIAL PRIMARY KEY,
196 fk_user INTEGER NOT NULL,
197 fk_service INTEGER NOT NULL,
199 FOREIGN KEY (fk_user)
200 REFERENCES tb_users (pk_user)
202 FOREIGN KEY (fk_service)
203 REFERENCES tb_services (pk_service)
206 CREATE TABLE tb_messages
208 pk_message SERIAL PRIMARY KEY,
209 fk_user INTEGER NOT NULL,
210 ver SMALLINT NOT NULL,
211 msg_type SMALLINT NOT NULL,
212 last_send_time TIMESTAMP NOT NULL,
213 creation_time TIMESTAMP NOT NULL,
214 show_time INTEGER NOT NULL,
215 repeat SMALLINT NOT NULL,
216 repeat_period INTEGER NOT NULL,
217 msg_text TEXT NOT NULL,
219 FOREIGN KEY (fk_user)
220 REFERENCES tb_users (pk_user)
224 CREATE TABLE tb_stats_traffic
226 pk_stat_traffic BIGSERIAL PRIMARY KEY,
227 fk_user INTEGER NOT NULL,
228 stats_date DATE NOT NULL,
229 dir_num SMALLINT NOT NULL,
230 download BIGINT NOT NULL,
231 upload BIGINT NOT NULL,
233 FOREIGN KEY (fk_user)
234 REFERENCES tb_users (pk_user)
236 UNIQUE (fk_user, stats_date, dir_num)
239 CREATE TABLE tb_users_data
241 pk_user_data SERIAL PRIMARY KEY,
242 fk_user INTEGER NOT NULL,
243 num SMALLINT NOT NULL,
244 data VARCHAR(256) NOT NULL,
246 FOREIGN KEY (fk_user)
247 REFERENCES tb_users (pk_user)
251 CREATE TABLE tb_allowed_ip
253 pk_allowed_ip SERIAL PRIMARY KEY,
254 fk_user INTEGER NOT NULL,
257 FOREIGN KEY (fk_user)
258 REFERENCES tb_users (pk_user)
262 CREATE TABLE tb_sessions_log
264 pk_session_log SERIAL PRIMARY KEY,
265 fk_user INTEGER NOT NULL,
266 event_time TIMESTAMP NOT NULL,
267 event_type dm_session_event_type,
271 FOREIGN KEY (fk_user)
272 REFERENCES tb_users (pk_user)
276 CREATE TABLE tb_sessions_data
278 pk_session_data SERIAL PRIMARY KEY,
279 fk_session_log INTEGER NOT NULL,
280 dir_num SMALLINT NOT NULL,
281 session_upload BIGINT NOT NULL,
282 session_download BIGINT NOT NULL,
283 month_upload BIGINT NOT NULL,
284 month_download BIGINT NOT NULL,
286 FOREIGN KEY (fk_session_log)
287 REFERENCES tb_sessions_log (pk_session_log)
291 CREATE TABLE tb_parameters
293 pk_parameter SERIAL PRIMARY KEY,
297 CREATE TABLE tb_params_log
299 pk_param_log SERIAL PRIMARY KEY,
300 fk_user INTEGER NOT NULL,
301 fk_parameter INTEGER NOT NULL,
302 fk_admin INTEGER NOT NULL,
304 event_time TIMESTAMP NOT NULL,
305 from_val VARCHAR(256),
309 FOREIGN KEY (fk_user)
310 REFERENCES tb_users (pk_user)
312 FOREIGN KEY (fk_parameter)
313 REFERENCES tb_parameters (pk_parameter),
314 FOREIGN KEY (fk_admin)
315 REFERENCES tb_admins (pk_admin)
320 *****************************************************************************
321 * -= Создание хранимых процедур =-
322 *****************************************************************************
325 CREATE FUNCTION sp_add_message(_login dm_name,
328 _last_send_time TIMESTAMP,
329 _creation_time TIMESTAMP,
332 _repeat_period INTEGER,
339 SELECT pk_user INTO _pk_user
342 IF _pk_user IS NULL THEN
343 RAISE EXCEPTION 'User % not found', _login;
346 INSERT INTO tb_messages
366 RETURN CURRVAL('tb_messages_pk_message_seq');
370 CREATE FUNCTION sp_add_tariff(_name dm_name, _dirs INTEGER)
376 INSERT INTO tb_tariffs
385 SELECT CURRVAL('tb_tariffs_pk_tariff_seq') INTO pk_tariff;
386 FOR i IN 1.._dirs LOOP
387 INSERT INTO tb_tariffs_params
401 CAST('1970-01-01 00:00:00+00' AS TIMESTAMP),
402 CAST('1970-01-01 00:00:00+00' AS TIMESTAMP));
408 CREATE FUNCTION sp_add_user(_name dm_name)
423 disabled_detail_stat,
439 (NULL, NULL, NULL, '', FALSE, 0, CAST('now' AS TIMESTAMP),
440 FALSE, FALSE, '', '', '', FALSE, '', '', _name, '', 0, 0,
441 CAST('now' AS TIMESTAMP), 0, CAST('now' AS TIMESTAMP), 0);
442 SELECT CURRVAL('tb_users_pk_user_seq') INTO pk_user;
447 CREATE FUNCTION sp_add_stats_traffic (_login dm_name,
457 SELECT pk_user INTO _pk_user
461 IF _pk_user IS NULL THEN
462 RAISE EXCEPTION 'User % not found', _login;
466 UPDATE tb_stats_traffic SET
469 WHERE fk_user = _pk_user AND
470 dir_num = _dir_num AND
471 stats_date = _stats_date;
474 INSERT INTO tb_stats_traffic
492 CREATE FUNCTION sp_set_user_data (_pk_user INTEGER,
498 UPDATE tb_users_data SET
500 WHERE fk_user = _pk_user AND num = _num;
503 INSERT INTO tb_users_data
517 CREATE FUNCTION sp_add_param_log_entry(_login dm_name,
518 _admin_login dm_name,
521 _event_time TIMESTAMP,
532 SELECT pk_user INTO _pk_user
535 IF _pk_user IS NULL THEN
536 RAISE EXCEPTION 'User % not found', _login;
540 SELECT pk_admin INTO _pk_admin
542 WHERE login = _admin_login;
543 IF _pk_admin IS NULL THEN
544 RAISE EXCEPTION 'Admin % not found', _admin_login;
548 SELECT pk_parameter INTO _pk_param
550 WHERE name = _param_name;
553 INSERT INTO tb_parameters (name) VALUES (_param_name);
554 SELECT CURRVAL('tb_parameters_pk_parameter_seq') INTO _pk_param;
557 INSERT INTO tb_params_log
580 CREATE FUNCTION sp_add_session_log_entry(_login dm_name,
581 _event_time TIMESTAMP,
582 _event_type dm_session_event_type,
589 _pk_session_log INTEGER;
591 SELECT pk_user INTO _pk_user
594 IF _pk_user IS NULL THEN
595 RAISE EXCEPTION 'User % not found', _login;
599 INSERT INTO tb_sessions_log
612 SELECT CURRVAL('tb_sessions_log_pk_session_log_seq') INTO _pk_session_log;
614 RETURN _pk_session_log;
619 *****************************************************************************
620 * -= Создание администратора =-
622 * Двоичные права доступа пока не поддерживаются, по этому используются флаги
623 *****************************************************************************
625 INSERT INTO tb_admins
627 chg_conf, chg_password, chg_stat,
628 chg_cash, usr_add_del, chg_tariff,
629 chg_admin, chg_service, chg_corporation)
632 'geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmk',
633 1, 1, 1, 1, 1, 1, 1, 1, 1);
634 INSERT INTO tb_admins
636 chg_conf, chg_password, chg_stat,
637 chg_cash, usr_add_del, chg_tariff,
638 chg_admin, chg_service, chg_corporation)
642 0, 0, 0, 0, 0, 0, 0, 0, 0);