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 *****************************************************************************
41 * $Date: 2009/08/20 14:58:43 $
46 *****************************************************************************
47 * -= Создание типов и доменов =-
48 *****************************************************************************
51 CREATE DOMAIN dm_name AS VARCHAR(32) NOT NULL;
52 CREATE DOMAIN dm_password AS VARCHAR(64) NOT NULL;
53 CREATE DOMAIN dm_permission_flag AS SMALLINT NOT NULL
54 CHECK ( value BETWEEN 0 AND 3 );
55 CREATE DOMAIN dm_money AS NUMERIC(12, 4) NOT NULL DEFAULT 0;
56 CREATE DOMAIN dm_traff_type AS SMALLINT NOT NULL
57 CHECK ( value BETWEEN 0 AND 3 );
58 CREATE DOMAIN dm_day AS SMALLINT NOT NULL
59 CHECK ( value BETWEEN 0 AND 31 )
61 CREATE DOMAIN dm_session_event_type AS CHAR(1) NOT NULL
62 CHECK ( value = 'c' OR value = 'd' );
65 *****************************************************************************
66 * -= Создание таблиц =-
67 *****************************************************************************
72 version INTEGER NOT NULL
75 CREATE TABLE tb_admins
77 pk_admin SERIAL PRIMARY KEY,
79 passwd dm_password NOT NULL,
80 chg_conf dm_permission_flag,
81 chg_password dm_permission_flag,
82 chg_stat dm_permission_flag,
83 chg_cash dm_permission_flag,
84 usr_add_del dm_permission_flag,
85 chg_tariff dm_permission_flag,
86 chg_admin dm_permission_flag,
87 chg_service dm_permission_flag,
88 chg_corporation dm_permission_flag
91 CREATE TABLE tb_tariffs
93 pk_tariff SERIAL PRIMARY KEY,
97 passive_cost dm_money,
98 traff_type dm_traff_type
101 CREATE TABLE tb_tariffs_params
103 pk_tariff_param SERIAL PRIMARY KEY,
104 fk_tariff INTEGER NOT NULL,
105 dir_num SMALLINT NOT NULL,
106 price_day_a dm_money,
107 price_day_b dm_money,
108 price_night_a dm_money,
109 price_night_b dm_money,
110 threshold INTEGER NOT NULL,
111 time_day_begins TIME NOT NULL,
112 time_day_ends TIME NOT NULL,
114 FOREIGN KEY (fk_tariff)
115 REFERENCES tb_tariffs (pk_tariff)
119 CREATE TABLE tb_corporations
121 pk_corporation SERIAL PRIMARY KEY,
126 CREATE TABLE tb_users
128 pk_user SERIAL PRIMARY KEY,
130 fk_tariff_change INTEGER,
131 fk_corporation INTEGER,
132 address VARCHAR(256) NOT NULL,
133 always_online BOOLEAN NOT NULL,
135 credit_expire TIMESTAMP NOT NULL,
136 disabled BOOLEAN NOT NULL,
137 disabled_detail_stat BOOLEAN NOT NULL,
138 email VARCHAR(256) NOT NULL,
141 passive BOOLEAN NOT NULL,
143 phone VARCHAR(256) NOT NULL,
145 real_name VARCHAR(256) NOT NULL,
148 last_activity_time TIMESTAMP NOT NULL,
149 last_cash_add dm_money,
150 last_cash_add_time TIMESTAMP NOT NULL,
151 passive_time INTEGER NOT NULL,
153 FOREIGN KEY (fk_tariff)
154 REFERENCES tb_tariffs (pk_tariff)
156 FOREIGN KEY (fk_tariff_change)
157 REFERENCES tb_tariffs (pk_tariff)
159 FOREIGN KEY (fk_corporation)
160 REFERENCES tb_corporations (pk_corporation)
164 CREATE TABLE tb_detail_stats
166 pk_detail_stat BIGSERIAL PRIMARY KEY,
167 fk_user INTEGER NOT NULL,
168 dir_num SMALLINT NOT NULL,
170 download BIGINT NOT NULL,
171 upload BIGINT NOT NULL,
173 from_time TIMESTAMP NOT NULL,
174 till_time TIMESTAMP NOT NULL,
176 FOREIGN KEY (fk_user)
177 REFERENCES tb_users (pk_user)
181 CREATE TABLE tb_services
183 pk_service SERIAL PRIMARY KEY,
185 comment TEXT NOT NULL,
190 CREATE TABLE tb_users_services
192 pk_user_service SERIAL PRIMARY KEY,
193 fk_user INTEGER NOT NULL,
194 fk_service INTEGER NOT NULL,
196 FOREIGN KEY (fk_user)
197 REFERENCES tb_users (pk_user)
199 FOREIGN KEY (fk_service)
200 REFERENCES tb_services (pk_service)
203 CREATE TABLE tb_messages
205 pk_message SERIAL PRIMARY KEY,
206 fk_user INTEGER NOT NULL,
207 ver SMALLINT NOT NULL,
208 msg_type SMALLINT NOT NULL,
209 last_send_time TIMESTAMP NOT NULL,
210 creation_time TIMESTAMP NOT NULL,
211 show_time INTEGER NOT NULL,
212 repeat SMALLINT NOT NULL,
213 repeat_period INTEGER NOT NULL,
214 msg_text TEXT NOT NULL,
216 FOREIGN KEY (fk_user)
217 REFERENCES tb_users (pk_user)
221 CREATE TABLE tb_stats_traffic
223 pk_stat_traffic BIGSERIAL PRIMARY KEY,
224 fk_user INTEGER NOT NULL,
225 stats_date DATE NOT NULL,
226 dir_num SMALLINT NOT NULL,
227 download BIGINT NOT NULL,
228 upload BIGINT NOT NULL,
230 FOREIGN KEY (fk_user)
231 REFERENCES tb_users (pk_user)
233 UNIQUE (fk_user, stats_date, dir_num)
236 CREATE TABLE tb_users_data
238 pk_user_data SERIAL PRIMARY KEY,
239 fk_user INTEGER NOT NULL,
240 num SMALLINT NOT NULL,
241 data VARCHAR(256) NOT NULL,
243 FOREIGN KEY (fk_user)
244 REFERENCES tb_users (pk_user)
248 CREATE TABLE tb_allowed_ip
250 pk_allowed_ip SERIAL PRIMARY KEY,
251 fk_user INTEGER NOT NULL,
254 FOREIGN KEY (fk_user)
255 REFERENCES tb_users (pk_user)
259 CREATE TABLE tb_sessions_log
261 pk_session_log SERIAL PRIMARY KEY,
262 fk_user INTEGER NOT NULL,
263 event_time TIMESTAMP NOT NULL,
264 event_type dm_session_event_type,
268 FOREIGN KEY (fk_user)
269 REFERENCES tb_users (pk_user)
273 CREATE TABLE tb_sessions_data
275 pk_session_data SERIAL PRIMARY KEY,
276 fk_session_log INTEGER NOT NULL,
277 dir_num SMALLINT NOT NULL,
278 session_upload BIGINT NOT NULL,
279 session_download BIGINT NOT NULL,
280 month_upload BIGINT NOT NULL,
281 month_download BIGINT NOT NULL,
283 FOREIGN KEY (fk_session_log)
284 REFERENCES tb_sessions_log (pk_session_log)
288 CREATE TABLE tb_parameters
290 pk_parameter SERIAL PRIMARY KEY,
294 CREATE TABLE tb_params_log
296 pk_param_log SERIAL PRIMARY KEY,
297 fk_user INTEGER NOT NULL,
298 fk_parameter INTEGER NOT NULL,
299 fk_admin INTEGER NOT NULL,
301 event_time TIMESTAMP NOT NULL,
302 from_val VARCHAR(256),
306 FOREIGN KEY (fk_user)
307 REFERENCES tb_users (pk_user)
309 FOREIGN KEY (fk_parameter)
310 REFERENCES tb_parameters (pk_parameter),
311 FOREIGN KEY (fk_admin)
312 REFERENCES tb_admins (pk_admin)
317 *****************************************************************************
318 * -= Создание хранимых процедур =-
319 *****************************************************************************
322 CREATE FUNCTION sp_add_message(_login dm_name,
325 _last_send_time TIMESTAMP,
326 _creation_time TIMESTAMP,
329 _repeat_period INTEGER,
336 SELECT pk_user INTO _pk_user
339 IF _pk_user IS NULL THEN
340 RAISE EXCEPTION 'User % not found', _login;
343 INSERT INTO tb_messages
363 RETURN CURRVAL('tb_messages_pk_message_seq');
367 CREATE FUNCTION sp_add_tariff(_name dm_name, _dirs INTEGER)
373 INSERT INTO tb_tariffs
382 SELECT CURRVAL('tb_tariffs_pk_tariff_seq') INTO pk_tariff;
383 FOR i IN 1.._dirs LOOP
384 INSERT INTO tb_tariffs_params
398 CAST('1970-01-01 00:00:00+00' AS TIMESTAMP),
399 CAST('1970-01-01 00:00:00+00' AS TIMESTAMP));
405 CREATE FUNCTION sp_add_user(_name dm_name)
420 disabled_detail_stat,
436 (NULL, NULL, NULL, '', FALSE, 0, CAST('now' AS TIMESTAMP),
437 FALSE, FALSE, '', '', '', FALSE, '', '', _name, '', 0, 0,
438 CAST('now' AS TIMESTAMP), 0, CAST('now' AS TIMESTAMP), 0);
439 SELECT CURRVAL('tb_users_pk_user_seq') INTO pk_user;
444 CREATE FUNCTION sp_add_stats_traffic (_login dm_name,
454 SELECT pk_user INTO _pk_user
458 IF _pk_user IS NULL THEN
459 RAISE EXCEPTION 'User % not found', _login;
463 UPDATE tb_stats_traffic SET
466 WHERE fk_user = _pk_user AND
467 dir_num = _dir_num AND
468 stats_date = _stats_date;
471 INSERT INTO tb_stats_traffic
489 CREATE FUNCTION sp_set_user_data (_pk_user INTEGER,
495 UPDATE tb_users_data SET
497 WHERE fk_user = _pk_user AND num = _num;
500 INSERT INTO tb_users_data
514 CREATE FUNCTION sp_add_param_log_entry(_login dm_name,
515 _admin_login dm_name,
518 _event_time TIMESTAMP,
529 SELECT pk_user INTO _pk_user
532 IF _pk_user IS NULL THEN
533 RAISE EXCEPTION 'User % not found', _login;
537 SELECT pk_admin INTO _pk_admin
539 WHERE login = _admin_login;
540 IF _pk_admin IS NULL THEN
541 RAISE EXCEPTION 'Admin % not found', _admin_login;
545 SELECT pk_parameter INTO _pk_param
547 WHERE name = _param_name;
550 INSERT INTO tb_parameters (name) VALUES (_param_name);
551 SELECT CURRVAL('tb_parameters_pk_parameter_seq') INTO _pk_param;
554 INSERT INTO tb_params_log
577 CREATE FUNCTION sp_add_session_log_entry(_login dm_name,
578 _event_time TIMESTAMP,
579 _event_type dm_session_event_type,
586 _pk_session_log INTEGER;
588 SELECT pk_user INTO _pk_user
591 IF _pk_user IS NULL THEN
592 RAISE EXCEPTION 'User % not found', _login;
596 INSERT INTO tb_sessions_log
609 SELECT CURRVAL('tb_sessions_log_pk_session_log_seq') INTO _pk_session_log;
611 RETURN _pk_session_log;
616 *****************************************************************************
617 * -= Создание администратора =-
619 * Двоичные права доступа пока не поддерживаются, по этому используются флаги
620 *****************************************************************************
622 INSERT INTO tb_admins
624 chg_conf, chg_password, chg_stat,
625 chg_cash, usr_add_del, chg_tariff,
626 chg_admin, chg_service, chg_corporation)
629 'geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmk',
630 1, 1, 1, 1, 1, 1, 1, 1, 1);
631 INSERT INTO tb_admins
633 chg_conf, chg_password, chg_stat,
634 chg_cash, usr_add_del, chg_tariff,
635 chg_admin, chg_service, chg_corporation)
639 0, 0, 0, 0, 0, 0, 0, 0, 0);