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 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 * User manipulation methods
25 * $Date: 2010/05/07 07:26:36 $
36 #include "stg_const.h"
37 #include "postgresql_store.h"
38 #include "stg_locker.h"
39 #include "../../../stg_timer.h"
41 //-----------------------------------------------------------------------------
42 int POSTGRESQL_STORE::GetUsersList(vector<string> * usersList) const
44 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
46 if (PQstatus(connection) != CONNECTION_OK)
48 printfd(__FILE__, "POSTGRESQL_STORE::GetUsersList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
51 strError = "Connection lost";
52 printfd(__FILE__, "POSTGRESQL_STORE::GetUsersList(): '%s'\n", strError.c_str());
59 if (StartTransaction())
61 printfd(__FILE__, "POSTGRESQL_STORE::GetUsersList(): 'Failed to start transaction'\n");
65 result = PQexec(connection, "SELECT name FROM tb_users");
67 if (PQresultStatus(result) != PGRES_TUPLES_OK)
69 strError = PQresultErrorMessage(result);
71 printfd(__FILE__, "POSTGRESQL_STORE::GetUsersList(): '%s'\n", strError.c_str());
72 if (RollbackTransaction())
74 printfd(__FILE__, "POSTGRESQL_STORE::GetUsersList(): 'Failed to rollback transaction'\n");
79 int tuples = PQntuples(result);
81 for (int i = 0; i < tuples; ++i)
83 usersList->push_back(PQgetvalue(result, i, 0));
88 if (CommitTransaction())
90 printfd(__FILE__, "POSTGRESQL_STORE::GetUsersList(): 'Failed to commit transaction'\n");
97 //-----------------------------------------------------------------------------
98 int POSTGRESQL_STORE::AddUser(const string & name) const
100 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
102 if (PQstatus(connection) != CONNECTION_OK)
104 printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
107 strError = "Connection lost";
108 printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): '%s'\n", strError.c_str());
115 if (StartTransaction())
117 printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Failed to start transaction'\n");
121 std::string elogin = name;
123 if (EscapeString(elogin))
125 printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Failed to escape login'\n");
126 if (RollbackTransaction())
128 printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Failed to rollback transaction'\n");
133 std::stringstream query;
134 query << "SELECT sp_add_user('" << elogin << "')";
136 result = PQexec(connection, query.str().c_str());
138 if (PQresultStatus(result) != PGRES_TUPLES_OK)
140 strError = PQresultErrorMessage(result);
142 printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): '%s'\n", strError.c_str());
143 if (RollbackTransaction())
145 printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Failed to rollback transaction'\n");
152 if (CommitTransaction())
154 printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Failed to commit transaction'\n");
161 //-----------------------------------------------------------------------------
162 int POSTGRESQL_STORE::DelUser(const string & login) const
164 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
166 if (PQstatus(connection) != CONNECTION_OK)
168 printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
171 strError = "Connection lost";
172 printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): '%s'\n", strError.c_str());
179 if (StartTransaction())
181 printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Failed to start transaction'\n");
185 std::string elogin = login;
187 if (EscapeString(elogin))
189 printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Failed to escape login'\n");
190 if (RollbackTransaction())
192 printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Failed to rollback transaction'\n");
197 std::stringstream query;
198 query << "DELETE FROM tb_users WHERE name = '" << elogin << "'";
200 result = PQexec(connection, query.str().c_str());
202 if (PQresultStatus(result) != PGRES_COMMAND_OK)
204 strError = PQresultErrorMessage(result);
206 printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): '%s'\n", strError.c_str());
207 if (RollbackTransaction())
209 printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Failed to rollback transaction'\n");
216 if (CommitTransaction())
218 printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Failed to commit transaction'\n");
224 //-----------------------------------------------------------------------------
225 int POSTGRESQL_STORE::SaveUserStat(const USER_STAT & stat,
226 const string & login) const
228 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
230 return SaveStat(stat, login);
232 //-----------------------------------------------------------------------------
233 int POSTGRESQL_STORE::SaveStat(const USER_STAT & stat,
234 const string & login,
238 if (PQstatus(connection) != CONNECTION_OK)
240 printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
243 strError = "Connection lost";
244 printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): '%s'\n", strError.c_str());
251 if (StartTransaction())
253 printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to start transaction'\n");
257 std::string elogin = login;
259 if (EscapeString(elogin))
261 printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to escape login'\n");
262 if (RollbackTransaction())
264 printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to rollback transaction'\n");
269 std::stringstream query;
270 query << "UPDATE tb_users SET "
271 "cash = " << stat.cash << ", "
272 "free_mb = " << stat.freeMb << ", "
273 "last_activity_time = CAST('" << Int2TS(stat.lastActivityTime) << "' AS TIMESTAMP), "
274 "last_cash_add = " << stat.lastCashAdd << ", "
275 "last_cash_add_time = CAST('" << Int2TS(stat.lastCashAddTime) << "' AS TIMESTAMP), "
276 "passive_time = " << stat.passiveTime << " "
277 "WHERE name = '" << elogin << "'";
279 result = PQexec(connection, query.str().c_str());
281 if (PQresultStatus(result) != PGRES_COMMAND_OK)
283 strError = PQresultErrorMessage(result);
285 printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): '%s'\n", strError.c_str());
286 if (RollbackTransaction())
288 printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to rollback transaction'\n");
297 MakeDate(date, year, month);
299 for (int dir = 0; dir < DIR_NUM; ++dir)
302 query << "SELECT sp_add_stats_traffic ("
303 "'" << elogin << "', "
304 "CAST('" << date << "' AS DATE), "
305 "CAST(" << dir << " AS SMALLINT), "
306 "CAST(" << stat.up[dir] << " AS BIGINT), "
307 "CAST(" << stat.down[dir] << " AS BIGINT))";
309 result = PQexec(connection, query.str().c_str());
311 if (PQresultStatus(result) != PGRES_TUPLES_OK)
313 strError = PQresultErrorMessage(result);
315 printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): '%s'\n", strError.c_str());
316 if (RollbackTransaction())
318 printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to rollback transaction'\n");
326 if (CommitTransaction())
328 printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to commit transaction'\n");
335 //-----------------------------------------------------------------------------
336 int POSTGRESQL_STORE::SaveUserConf(const USER_CONF & conf,
337 const string & login) const
339 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
341 if (PQstatus(connection) != CONNECTION_OK)
343 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
346 strError = "Connection lost";
347 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): '%s'\n", strError.c_str());
354 if (StartTransaction())
356 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to start transaction'\n");
360 std::string elogin = login;
362 if (EscapeString(elogin))
364 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape login'\n");
365 if (RollbackTransaction())
367 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
372 std::stringstream query;
373 query << "SELECT pk_user FROM tb_users WHERE name = '" << elogin << "'";
375 result = PQexec(connection, query.str().c_str());
377 if (PQresultStatus(result) != PGRES_TUPLES_OK)
379 strError = PQresultErrorMessage(result);
381 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): '%s'\n", strError.c_str());
382 if (RollbackTransaction())
384 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
389 int tuples = PQntuples(result);
393 strError = "Failed to fetch user's ID";
394 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
396 if (RollbackTransaction())
398 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
403 std::stringstream tuple;
404 tuple << PQgetvalue(result, 0, 0);
412 std::string eaddress = conf.address;
413 std::string eemail = conf.email;
414 std::string egroup = conf.group;
415 std::string enote = conf.note;
416 std::string epassword = conf.password;
417 std::string ephone = conf.phone;
418 std::string erealname = conf.realName;
419 std::string etariffname = conf.tariffName;
420 std::string enexttariff = conf.nextTariff;
421 std::string ecorporation = conf.corp;
423 if (EscapeString(eaddress))
425 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape address'\n");
426 if (RollbackTransaction())
428 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
433 if (EscapeString(eemail))
435 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape email'\n");
436 if (RollbackTransaction())
438 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
443 if (EscapeString(egroup))
445 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape group'\n");
446 if (RollbackTransaction())
448 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
453 if (EscapeString(enote))
455 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape note'\n");
456 if (RollbackTransaction())
458 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
463 if (EscapeString(epassword))
465 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape password'\n");
466 if (RollbackTransaction())
468 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
473 if (EscapeString(ephone))
475 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape phone'\n");
476 if (RollbackTransaction())
478 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
483 if (EscapeString(erealname))
485 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape real name'\n");
486 if (RollbackTransaction())
488 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
493 if (EscapeString(etariffname))
495 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape tariff name'\n");
496 if (RollbackTransaction())
498 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
503 if (EscapeString(enexttariff))
505 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape next tariff name'\n");
506 if (RollbackTransaction())
508 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
513 if (EscapeString(ecorporation))
515 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape corporation name'\n");
516 if (RollbackTransaction())
518 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
524 query << "UPDATE tb_users SET "
525 "address = '" << eaddress << "', "
526 "always_online = " << (conf.alwaysOnline ? "'t'" : "'f'") << ", "
527 "credit = " << conf.credit << ", "
528 "credit_expire = CAST('" << Int2TS(conf.creditExpire) << "' AS TIMESTAMP), "
529 "disabled = " << (conf.disabled ? "'t'" : "'f'") << ", "
530 "disabled_detail_stat = " << (conf.disabledDetailStat ? "'t'" : "'f'") << ", "
531 "email = '" << eemail << "', "
532 "grp = '" << egroup << "', "
533 "note = '" << enote << "', "
534 "passive = " << (conf.passive ? "'t'" : "'f'") << ", "
535 "passwd = '" << epassword << "', "
536 "phone = '" << ephone << "', "
537 "real_name = '" << erealname << "', "
538 "fk_tariff = (SELECT pk_tariff "
540 "WHERE name = '" << etariffname << "'), "
541 "fk_tariff_change = (SELECT pk_tariff "
543 "WHERE name = '" << enexttariff << "'), "
544 "fk_corporation = (SELECT pk_corporation "
545 "FROM tb_corporations "
546 "WHERE name = '" << ecorporation << "') "
547 "WHERE pk_user = " << uid;
549 result = PQexec(connection, query.str().c_str());
551 if (PQresultStatus(result) != PGRES_COMMAND_OK)
553 strError = PQresultErrorMessage(result);
555 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): '%s'\n", strError.c_str());
556 if (RollbackTransaction())
558 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
565 if (SaveUserServices(uid, conf.service))
567 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to save user's services'\n");
568 if (RollbackTransaction())
570 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
575 if (SaveUserData(uid, conf.userdata))
577 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to save user's data'\n");
578 if (RollbackTransaction())
580 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
585 if (SaveUserIPs(uid, conf.ips))
587 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to save user's IPs'\n");
588 if (RollbackTransaction())
590 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n");
595 if (CommitTransaction())
597 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to commit transaction'\n");
604 //-----------------------------------------------------------------------------
605 int POSTGRESQL_STORE::RestoreUserStat(USER_STAT * stat,
606 const string & login) const
608 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
610 if (PQstatus(connection) != CONNECTION_OK)
612 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
615 strError = "Connection lost";
616 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): '%s'\n", strError.c_str());
623 if (StartTransaction())
625 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to start transaction'\n");
629 std::string elogin = login;
631 if (EscapeString(elogin))
633 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to escape login'\n");
634 if (RollbackTransaction())
636 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n");
641 std::stringstream query;
642 query << "SELECT cash, free_mb, "
643 "last_activity_time, last_cash_add, "
644 "last_cash_add_time, passive_time "
646 "WHERE name = '" << elogin << "'";
648 result = PQexec(connection, query.str().c_str());
650 if (PQresultStatus(result) != PGRES_TUPLES_OK)
652 strError = PQresultErrorMessage(result);
653 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): '%s'\n", strError.c_str());
655 if (RollbackTransaction())
657 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n");
662 int tuples = PQntuples(result);
666 strError = "Failed to fetch user's stat";
667 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
669 if (RollbackTransaction())
671 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n");
676 std::stringstream tuple;
677 tuple << PQgetvalue(result, 0, 0) << " ";
678 tuple << PQgetvalue(result, 0, 1) << " ";
679 stat->lastActivityTime = TS2Int(PQgetvalue(result, 0, 2));
680 tuple << PQgetvalue(result, 0, 3) << " ";
681 stat->lastCashAddTime = TS2Int(PQgetvalue(result, 0, 4));
682 tuple << PQgetvalue(result, 0, 5) << " ";
689 >> stat->passiveTime;
693 query << "SELECT dir_num, upload, download "
694 "FROM tb_stats_traffic "
695 "WHERE fk_user IN (SELECT pk_user FROM tb_users WHERE name = '" << elogin << "') AND "
696 "DATE_TRUNC('month', stats_date) = DATE_TRUNC('month', CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP))";
698 result = PQexec(connection, query.str().c_str());
700 if (PQresultStatus(result) != PGRES_TUPLES_OK)
702 strError = PQresultErrorMessage(result);
703 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): '%s'\n", strError.c_str());
705 if (RollbackTransaction())
707 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n");
712 tuples = PQntuples(result);
714 for (int i = 0; i < tuples; ++i)
716 std::stringstream tuple;
717 tuple << PQgetvalue(result, i, 0) << " ";
718 tuple << PQgetvalue(result, i, 1) << " ";
719 tuple << PQgetvalue(result, i, 2) << " ";
724 tuple >> stat->up[dir];
725 tuple >> stat->down[dir];
730 if (CommitTransaction())
732 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to commit transaction'\n");
739 //-----------------------------------------------------------------------------
740 int POSTGRESQL_STORE::RestoreUserConf(USER_CONF * conf,
741 const string & login) const
743 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
745 if (PQstatus(connection) != CONNECTION_OK)
747 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
750 strError = "Connection lost";
751 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): '%s'\n", strError.c_str());
758 if (StartTransaction())
760 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to start transaction'\n");
764 std::string elogin = login;
766 if (EscapeString(elogin))
768 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to escape login'\n");
769 if (RollbackTransaction())
771 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n");
776 std::stringstream query;
777 query << "SELECT tb_users.pk_user, tb_users.address, tb_users.always_online, "
778 "tb_users.credit, tb_users.credit_expire, tb_users.disabled, "
779 "tb_users.disabled_detail_stat, tb_users.email, tb_users.grp, "
780 "tb_users.note, tb_users.passive, tb_users.passwd, tb_users.phone, "
781 "tb_users.real_name, tf1.name, tf2.name, tb_corporations.name "
782 "FROM tb_users LEFT JOIN tb_tariffs AS tf1 "
783 "ON tf1.pk_tariff = tb_users.fk_tariff "
784 "LEFT JOIN tb_tariffs AS tf2 "
785 "ON tf2.pk_tariff = tb_users.fk_tariff_change "
786 "LEFT JOIN tb_corporations "
787 "ON tb_corporations.pk_corporation = tb_users.fk_corporation "
788 "WHERE tb_users.name = '" << elogin << "'";
790 result = PQexec(connection, query.str().c_str());
792 if (PQresultStatus(result) != PGRES_TUPLES_OK)
794 strError = PQresultErrorMessage(result);
795 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): '%s'\n", strError.c_str());
797 if (RollbackTransaction())
799 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n");
804 int tuples = PQntuples(result);
808 strError = "Failed to fetch user's stat";
809 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
811 if (RollbackTransaction())
813 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n");
820 std::stringstream tuple;
822 tuple << PQgetvalue(result, 0, 0) << " "; // uid
823 conf->address = PQgetvalue(result, 0, 1); // address
824 conf->alwaysOnline = !strncmp(PQgetvalue(result, 0, 2), "t", 1);
825 tuple << PQgetvalue(result, 0, 3) << " "; // credit
826 conf->creditExpire = TS2Int(PQgetvalue(result, 0, 4)); // creditExpire
827 conf->disabled = !strncmp(PQgetvalue(result, 0, 5), "t", 1);
828 conf->disabledDetailStat = !strncmp(PQgetvalue(result, 0, 6), "t", 1);
829 conf->email = PQgetvalue(result, 0, 7); // email
830 conf->group = PQgetvalue(result, 0, 8); // group
831 conf->note = PQgetvalue(result, 0, 9); // note
832 conf->passive = !strncmp(PQgetvalue(result, 0, 10), "t", 1);
833 conf->password = PQgetvalue(result, 0, 11); // password
834 conf->phone = PQgetvalue(result, 0, 12); // phone
835 conf->realName = PQgetvalue(result, 0, 13); // realName
836 conf->tariffName = PQgetvalue(result, 0, 14); // tariffName
837 conf->nextTariff = PQgetvalue(result, 0, 15); // nextTariff
838 conf->corp = PQgetvalue(result, 0, 16); // corp
842 if (conf->tariffName == "")
843 conf->tariffName = NO_TARIFF_NAME;
844 if (conf->corp == "")
845 conf->corp = NO_CORP_NAME;
851 query << "SELECT name FROM tb_services "
852 "WHERE pk_service IN (SELECT fk_service "
853 "FROM tb_users_services "
854 "WHERE fk_user = " << uid << ")";
856 result = PQexec(connection, query.str().c_str());
858 if (PQresultStatus(result) != PGRES_TUPLES_OK)
860 strError = PQresultErrorMessage(result);
861 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): '%s'\n", strError.c_str());
863 if (RollbackTransaction())
865 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n");
870 tuples = PQntuples(result);
872 for (int i = 0; i < tuples; ++i)
874 conf->service.push_back(PQgetvalue(result, i, 0));
880 query << "SELECT num, data "
881 "FROM tb_users_data "
882 "WHERE fk_user = " << uid;
884 result = PQexec(connection, query.str().c_str());
886 if (PQresultStatus(result) != PGRES_TUPLES_OK)
888 strError = PQresultErrorMessage(result);
889 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): '%s'\n", strError.c_str());
891 if (RollbackTransaction())
893 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n");
898 tuples = PQntuples(result);
900 for (int i = 0; i < tuples; ++i)
903 if (str2x(PQgetvalue(result, i, 0), num))
905 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to convert string to int'\n");
909 conf->userdata[i] = PQgetvalue(result, i, 1);
916 query << "SELECT host(ip), masklen(ip) "
917 "FROM tb_allowed_ip "
918 "WHERE fk_user = " << uid;
920 result = PQexec(connection, query.str().c_str());
922 if (PQresultStatus(result) != PGRES_TUPLES_OK)
924 strError = PQresultErrorMessage(result);
925 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): '%s'\n", strError.c_str());
927 if (RollbackTransaction())
929 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n");
934 tuples = PQntuples(result);
937 for (int i = 0; i < tuples; ++i)
943 ip = inet_strington(PQgetvalue(result, i, 0));
945 if (str2x(PQgetvalue(result, i, 1), mask))
947 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to fetch mask'\n");
959 if (CommitTransaction())
961 printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to commit transaction'\n");
968 //-----------------------------------------------------------------------------
969 int POSTGRESQL_STORE::WriteUserChgLog(const string & login,
970 const string & admLogin,
972 const string & paramName,
973 const string & oldValue,
974 const string & newValue,
975 const string & message = "") const
977 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
979 if (PQstatus(connection) != CONNECTION_OK)
981 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
984 strError = "Connection lost";
985 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): '%s'\n", strError.c_str());
992 if (StartTransaction())
994 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to start transaction'\n");
998 std::string elogin(login);
999 std::string eadminLogin(admLogin);
1000 std::string eparam(paramName);
1001 std::string eold(oldValue);
1002 std::string enew(newValue);
1003 std::string emessage(message);
1005 if (EscapeString(elogin))
1007 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to escape login'\n");
1008 if (RollbackTransaction())
1010 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n");
1015 if (EscapeString(eadminLogin))
1017 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to escape admin's login'\n");
1018 if (RollbackTransaction())
1020 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n");
1025 if (EscapeString(eparam))
1027 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to escape param's name'\n");
1028 if (RollbackTransaction())
1030 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n");
1035 if (EscapeString(eold))
1037 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to escape old value'\n");
1038 if (RollbackTransaction())
1040 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n");
1045 if (EscapeString(enew))
1047 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to escape new value'\n");
1048 if (RollbackTransaction())
1050 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n");
1055 std::stringstream query;
1056 query << "SELECT sp_add_param_log_entry("
1057 "'" << elogin << "', "
1058 "'" << eadminLogin << "', CAST('"
1059 << inet_ntostring(admIP) << "/32' AS INET), "
1060 "'" << eparam << "', "
1061 "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), "
1062 "'" << eold << "', "
1063 "'" << enew << "', "
1064 "'" << emessage << "')";
1066 result = PQexec(connection, query.str().c_str());
1068 if (PQresultStatus(result) != PGRES_TUPLES_OK)
1070 strError = PQresultErrorMessage(result);
1072 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): '%s'\n", strError.c_str());
1073 if (RollbackTransaction())
1075 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n");
1082 if (CommitTransaction())
1084 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to commit transaction'\n");
1091 //-----------------------------------------------------------------------------
1092 int POSTGRESQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1094 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1096 if (PQstatus(connection) != CONNECTION_OK)
1098 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
1101 strError = "Connection lost";
1102 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): '%s'\n", strError.c_str());
1109 if (StartTransaction())
1111 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): 'Failed to start transaction'\n");
1115 std::string elogin(login);
1117 if (EscapeString(elogin))
1119 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): 'Failed to escape login'\n");
1120 if (RollbackTransaction())
1122 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): 'Failed to rollback transaction'\n");
1127 std::stringstream query;
1130 query << "SELECT sp_add_session_log_entry("
1131 "'" << elogin << "', "
1132 "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), "
1134 << inet_ntostring(ip) << "/32' AS INET), 0)";
1138 query << "SELECT sp_add_session_log_entry("
1139 "'" << elogin << "', "
1140 "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), "
1142 << inet_ntostring(ip) << "/32' AS INET), 0, 0, '')";
1145 result = PQexec(connection, query.str().c_str());
1147 if (PQresultStatus(result) != PGRES_TUPLES_OK)
1149 strError = PQresultErrorMessage(result);
1151 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): '%s'\n", strError.c_str());
1152 if (RollbackTransaction())
1154 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): 'Failed to rollback transaction'\n");
1161 if (CommitTransaction())
1163 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): 'Failed to commit transaction'\n");
1170 //-----------------------------------------------------------------------------
1171 int POSTGRESQL_STORE::WriteUserDisconnect(const string & login,
1172 const DIR_TRAFF & up,
1173 const DIR_TRAFF & down,
1174 const DIR_TRAFF & sessionUp,
1175 const DIR_TRAFF & sessionDown,
1178 const std::string & reason) const
1180 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1182 if (PQstatus(connection) != CONNECTION_OK)
1184 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
1187 strError = "Connection lost";
1188 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): '%s'\n", strError.c_str());
1195 if (StartTransaction())
1197 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to start transaction'\n");
1201 std::string elogin(login);
1203 if (EscapeString(elogin))
1205 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to escape login'\n");
1206 if (RollbackTransaction())
1208 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n");
1213 std::string ereason(reason);
1215 if (EscapeString(ereason))
1217 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to escape reason'\n");
1218 if (RollbackTransaction())
1220 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n");
1225 std::stringstream query;
1228 // Old database version - no freeMb logging support
1229 query << "SELECT sp_add_session_log_entry("
1230 "'" << elogin << "', "
1231 "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), "
1232 "'d', CAST('0.0.0.0/0' AS INET), "
1237 query << "SELECT sp_add_session_log_entry("
1238 "'" << elogin << "', "
1239 "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), "
1240 "'d', CAST('0.0.0.0/0' AS INET), "
1241 << cash << ", " << freeMb << ", '" << ereason << "')";
1244 result = PQexec(connection, query.str().c_str());
1246 if (PQresultStatus(result) != PGRES_TUPLES_OK)
1248 strError = PQresultErrorMessage(result);
1250 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): '%s'\n", strError.c_str());
1251 if (RollbackTransaction())
1253 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n");
1258 int tuples = PQntuples(result);
1262 strError = "Failed to fetch session's log ID";
1263 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
1265 if (RollbackTransaction())
1267 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n");
1274 if (str2x(PQgetvalue(result, 0, 0), lid))
1276 strError = "Failed to convert string to int";
1277 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): '%s'\n", strError.c_str());
1279 if (RollbackTransaction())
1281 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n");
1288 for (int i = 0; i < DIR_NUM; ++i)
1290 std::stringstream query;
1291 query << "INSERT INTO tb_sessions_data "
1295 "session_download, "
1301 << sessionUp[i] << ", "
1302 << sessionDown[i] << ", "
1306 result = PQexec(connection, query.str().c_str());
1308 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1310 strError = PQresultErrorMessage(result);
1312 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): '%s'\n", strError.c_str());
1313 if (RollbackTransaction())
1315 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n");
1323 if (CommitTransaction())
1325 printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to commit transaction'\n");
1332 //-----------------------------------------------------------------------------
1333 int POSTGRESQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1335 const string & login) const
1337 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1339 if (PQstatus(connection) != CONNECTION_OK)
1341 printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
1344 strError = "Connection lost";
1345 printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): '%s'\n", strError.c_str());
1352 if (StartTransaction())
1354 printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Failed to start transaction'\n");
1358 std::string elogin(login);
1360 if (EscapeString(elogin))
1362 printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Failed to escape login'\n");
1363 if (RollbackTransaction())
1365 printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Failed to rollback transaction'\n");
1370 map<IP_DIR_PAIR, STAT_NODE>::const_iterator it;
1371 time_t currTime = time(NULL);
1373 for (it = statTree.begin(); it != statTree.end(); ++it)
1375 std::stringstream query;
1376 query << "INSERT INTO tb_detail_stats "
1377 "(till_time, from_time, fk_user, "
1378 "dir_num, ip, download, upload, cost) "
1380 "CAST('" << Int2TS(currTime) << "' AS TIMESTAMP), "
1381 "CAST('" << Int2TS(lastStat) << "' AS TIMESTAMP), "
1382 "(SELECT pk_user FROM tb_users WHERE name = '" << elogin << "'), "
1383 << it->first.dir << ", "
1384 << "CAST('" << inet_ntostring(it->first.ip) << "' AS INET), "
1385 << it->second.down << ", "
1386 << it->second.up << ", "
1387 << it->second.cash << ")";
1389 result = PQexec(connection, query.str().c_str());
1391 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1393 strError = PQresultErrorMessage(result);
1395 printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): '%s'\n", strError.c_str());
1396 if (RollbackTransaction())
1398 printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Failed to rollback transaction'\n");
1406 if (CommitTransaction())
1408 printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Failed to commit transaction'\n");
1415 //-----------------------------------------------------------------------------
1416 int POSTGRESQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
1418 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1420 return SaveStat(stat, login, year, month);
1423 //-----------------------------------------------------------------------------
1424 int POSTGRESQL_STORE::SaveUserServices(uint32_t uid,
1425 const std::vector<std::string> & services) const
1429 std::stringstream query;
1430 query << "DELETE FROM tb_users_services WHERE fk_user = " << uid;
1432 result = PQexec(connection, query.str().c_str());
1434 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1436 strError = PQresultErrorMessage(result);
1438 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserServices(): '%s'\n", strError.c_str());
1444 std::vector<std::string>::const_iterator it;
1446 for (it = services.begin(); it != services.end(); ++it)
1448 std::string ename = *it;
1450 if (EscapeString(ename))
1452 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserServices(): 'Failed to escape service name'\n");
1456 std::stringstream query;
1457 query << "INSERT INTO tb_users_services "
1458 "(fk_user, fk_service) "
1461 "(SELECT pk_service "
1463 "WHERE name = '" << ename << "'))";
1465 result = PQexec(connection, query.str().c_str());
1467 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1469 strError = PQresultErrorMessage(result);
1471 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserServices(): '%s'\n", strError.c_str());
1481 //-----------------------------------------------------------------------------
1482 int POSTGRESQL_STORE::SaveUserIPs(uint32_t uid,
1483 const USER_IPS & ips) const
1487 std::stringstream query;
1488 query << "DELETE FROM tb_allowed_ip WHERE fk_user = " << uid;
1490 result = PQexec(connection, query.str().c_str());
1492 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1494 strError = PQresultErrorMessage(result);
1496 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserIPs(): '%s'\n", strError.c_str());
1502 for (int i = 0; i < ips.Count(); ++i)
1504 std::stringstream query;
1505 query << "INSERT INTO tb_allowed_ip "
1508 "(" << uid << ", CAST('"
1509 << inet_ntostring(ips[i].ip) << "/"
1510 << static_cast<int>(ips[i].mask) << "' AS INET))";
1512 result = PQexec(connection, query.str().c_str());
1514 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1516 strError = PQresultErrorMessage(result);
1518 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserIPs(): '%s'\n", strError.c_str());
1528 //-----------------------------------------------------------------------------
1529 int POSTGRESQL_STORE::SaveUserData(uint32_t uid,
1530 const std::vector<std::string> & data) const
1532 for (unsigned i = 0; i < data.size(); ++i)
1534 std::string edata = data[i];
1536 if (EscapeString(edata))
1538 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserData(): 'Failed to escape userdata field'\n");
1544 std::stringstream query;
1545 query << "SELECT sp_set_user_data("
1547 << "CAST(" << i << " AS SMALLINT), "
1548 << "'" << edata << "')";
1550 result = PQexec(connection, query.str().c_str());
1552 if (PQresultStatus(result) != PGRES_TUPLES_OK)
1554 strError = PQresultErrorMessage(result);
1556 printfd(__FILE__, "POSTGRESQL_STORE::SaveUserData(): '%s'\n", strError.c_str());