X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/1bf5ae378079e212f6918d09d87dec5420a0d6ec..f37a10e385478953dcc19c2ca3b75bf5465a79c1:/projects/stargazer/plugins/store/postgresql/postgresql_store_users.cpp diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store_users.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store_users.cpp index 2c66cc7f..db2717ac 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store_users.cpp +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store_users.cpp @@ -33,15 +33,15 @@ #include -#include "stg_const.h" -#include "postgresql_store.h" -#include "stg_locker.h" +#include "stg/const.h" +#include "stg/locker.h" #include "../../../stg_timer.h" +#include "postgresql_store.h" //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::GetUsersList(vector * usersList) const +int POSTGRESQL_STORE::GetUsersList(std::vector * usersList) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -70,9 +70,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK) PQclear(result); printfd(__FILE__, "POSTGRESQL_STORE::GetUsersList(): '%s'\n", strError.c_str()); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::GetUsersList(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::GetUsersList(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -95,9 +95,9 @@ return 0; } //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::AddUser(const string & name) const +int POSTGRESQL_STORE::AddUser(const std::string & name) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -124,13 +124,13 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Failed to rollback transaction'\n"); + } return -1; } -std::stringstream query; +std::ostringstream query; query << "SELECT sp_add_user('" << elogin << "')"; result = PQexec(connection, query.str().c_str()); @@ -141,9 +141,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK) PQclear(result); printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): '%s'\n", strError.c_str()); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::AddUser(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -159,9 +159,9 @@ return 0; } //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::DelUser(const string & login) const +int POSTGRESQL_STORE::DelUser(const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -188,13 +188,13 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Failed to rollback transaction'\n"); + } return -1; } -std::stringstream query; +std::ostringstream query; query << "DELETE FROM tb_users WHERE name = '" << elogin << "'"; result = PQexec(connection, query.str().c_str()); @@ -205,9 +205,9 @@ if (PQresultStatus(result) != PGRES_COMMAND_OK) PQclear(result); printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): '%s'\n", strError.c_str()); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::DelUser(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -223,15 +223,15 @@ return 0; } //----------------------------------------------------------------------------- int POSTGRESQL_STORE::SaveUserStat(const USER_STAT & stat, - const string & login) const + const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); return SaveStat(stat, login); } //----------------------------------------------------------------------------- int POSTGRESQL_STORE::SaveStat(const USER_STAT & stat, - const string & login, + const std::string & login, int year, int month) const { @@ -260,13 +260,13 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to rollback transaction'\n"); + } return -1; } -std::stringstream query; +std::ostringstream query; query << "UPDATE tb_users SET " "cash = " << stat.cash << ", " "free_mb = " << stat.freeMb << ", " @@ -284,9 +284,9 @@ if (PQresultStatus(result) != PGRES_COMMAND_OK) PQclear(result); printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): '%s'\n", strError.c_str()); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveStat(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -303,8 +303,8 @@ for (int dir = 0; dir < DIR_NUM; ++dir) "'" << elogin << "', " "CAST('" << date << "' AS DATE), " "CAST(" << dir << " AS SMALLINT), " - "CAST(" << stat.up[dir] << " AS BIGINT), " - "CAST(" << stat.down[dir] << " AS BIGINT))"; + "CAST(" << stat.monthUp[dir] << " AS BIGINT), " + "CAST(" << stat.monthDown[dir] << " AS BIGINT))"; result = PQexec(connection, query.str().c_str()); @@ -334,9 +334,9 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::SaveUserConf(const USER_CONF & conf, - const string & login) const + const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -363,13 +363,13 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } -std::stringstream query; +std::ostringstream query; query << "SELECT pk_user FROM tb_users WHERE name = '" << elogin << "'"; result = PQexec(connection, query.str().c_str()); @@ -380,9 +380,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK) PQclear(result); printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): '%s'\n", strError.c_str()); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -394,20 +394,22 @@ if (tuples != 1) printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } -std::stringstream tuple; -tuple << PQgetvalue(result, 0, 0); +uint32_t uid; -PQclear(result); + { + std::stringstream tuple; + tuple << PQgetvalue(result, 0, 0); -uint32_t uid; + PQclear(result); -tuple >> uid; + tuple >> uid; + } std::string eaddress = conf.address; std::string eemail = conf.email; @@ -424,9 +426,9 @@ if (EscapeString(eaddress)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape address'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -434,9 +436,9 @@ if (EscapeString(eemail)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape email'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -444,9 +446,9 @@ if (EscapeString(egroup)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape group'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -454,9 +456,9 @@ if (EscapeString(enote)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape note'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -464,9 +466,9 @@ if (EscapeString(epassword)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape password'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -474,9 +476,9 @@ if (EscapeString(ephone)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape phone'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -484,9 +486,9 @@ if (EscapeString(erealname)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape real name'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -494,9 +496,9 @@ if (EscapeString(etariffname)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape tariff name'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -504,9 +506,9 @@ if (EscapeString(enexttariff)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape next tariff name'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -514,9 +516,9 @@ if (EscapeString(ecorporation)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to escape corporation name'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -554,15 +556,15 @@ if (PQresultStatus(result) != PGRES_COMMAND_OK) PQclear(result); printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): '%s'\n", strError.c_str()); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } PQclear(result); -if (SaveUserServices(uid, conf.service)) +if (SaveUserServices(uid, conf.services)) { printfd(__FILE__, "POSTGRESQL_STORE::SaveUserConf(): 'Failed to save user's services'\n"); if (RollbackTransaction()) @@ -603,9 +605,9 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::RestoreUserStat(USER_STAT * stat, - const string & login) const + const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -632,20 +634,22 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n"); + } return -1; } -std::stringstream query; -query << "SELECT cash, free_mb, " - "last_activity_time, last_cash_add, " - "last_cash_add_time, passive_time " - "FROM tb_users " - "WHERE name = '" << elogin << "'"; + { + std::ostringstream query; + query << "SELECT cash, free_mb, " + "last_activity_time, last_cash_add, " + "last_cash_add_time, passive_time " + "FROM tb_users " + "WHERE name = '" << elogin << "'"; -result = PQexec(connection, query.str().c_str()); + result = PQexec(connection, query.str().c_str()); + } if (PQresultStatus(result) != PGRES_TUPLES_OK) { @@ -653,9 +657,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK) printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): '%s'\n", strError.c_str()); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -667,35 +671,38 @@ if (tuples != 1) printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n"); + } return -1; } -std::stringstream tuple; -tuple << PQgetvalue(result, 0, 0) << " "; -tuple << PQgetvalue(result, 0, 1) << " "; -stat->lastActivityTime = TS2Int(PQgetvalue(result, 0, 2)); -tuple << PQgetvalue(result, 0, 3) << " "; -stat->lastCashAddTime = TS2Int(PQgetvalue(result, 0, 4)); -tuple << PQgetvalue(result, 0, 5) << " "; - -PQclear(result); + { + std::stringstream tuple; + tuple << PQgetvalue(result, 0, 0) << " "; + tuple << PQgetvalue(result, 0, 1) << " "; + stat->lastActivityTime = TS2Int(PQgetvalue(result, 0, 2)); + tuple << PQgetvalue(result, 0, 3) << " "; + stat->lastCashAddTime = TS2Int(PQgetvalue(result, 0, 4)); + tuple << PQgetvalue(result, 0, 5) << " "; -tuple >> stat->cash - >> stat->freeMb - >> stat->lastCashAdd - >> stat->passiveTime; + PQclear(result); -query.str(""); + tuple >> stat->cash + >> stat->freeMb + >> stat->lastCashAdd + >> stat->passiveTime; + } -query << "SELECT dir_num, upload, download " - "FROM tb_stats_traffic " - "WHERE fk_user IN (SELECT pk_user FROM tb_users WHERE name = '" << elogin << "') AND " - "DATE_TRUNC('month', stats_date) = DATE_TRUNC('month', CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP))"; + { + std::ostringstream query; + query << "SELECT dir_num, upload, download " + "FROM tb_stats_traffic " + "WHERE fk_user IN (SELECT pk_user FROM tb_users WHERE name = '" << elogin << "') AND " + "DATE_TRUNC('month', stats_date) = DATE_TRUNC('month', CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP))"; -result = PQexec(connection, query.str().c_str()); + result = PQexec(connection, query.str().c_str()); + } if (PQresultStatus(result) != PGRES_TUPLES_OK) { @@ -703,9 +710,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK) printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): '%s'\n", strError.c_str()); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -721,8 +728,8 @@ for (int i = 0; i < tuples; ++i) int dir; tuple >> dir; - tuple >> stat->up[dir]; - tuple >> stat->down[dir]; + tuple >> stat->monthUp[dir]; + tuple >> stat->monthDown[dir]; } PQclear(result); @@ -738,9 +745,9 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::RestoreUserConf(USER_CONF * conf, - const string & login) const + const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -767,27 +774,29 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n"); - } - return -1; - } - -std::stringstream query; -query << "SELECT tb_users.pk_user, tb_users.address, tb_users.always_online, " - "tb_users.credit, tb_users.credit_expire, tb_users.disabled, " - "tb_users.disabled_detail_stat, tb_users.email, tb_users.grp, " - "tb_users.note, tb_users.passive, tb_users.passwd, tb_users.phone, " - "tb_users.real_name, tf1.name, tf2.name, tb_corporations.name " - "FROM tb_users LEFT JOIN tb_tariffs AS tf1 " - "ON tf1.pk_tariff = tb_users.fk_tariff " - "LEFT JOIN tb_tariffs AS tf2 " - "ON tf2.pk_tariff = tb_users.fk_tariff_change " - "LEFT JOIN tb_corporations " - "ON tb_corporations.pk_corporation = tb_users.fk_corporation " - "WHERE tb_users.name = '" << elogin << "'"; + { + printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserStat(): 'Failed to rollback transaction'\n"); + } + return -1; + } -result = PQexec(connection, query.str().c_str()); + { + std::ostringstream query; + query << "SELECT tb_users.pk_user, tb_users.address, tb_users.always_online, " + "tb_users.credit, tb_users.credit_expire, tb_users.disabled, " + "tb_users.disabled_detail_stat, tb_users.email, tb_users.grp, " + "tb_users.note, tb_users.passive, tb_users.passwd, tb_users.phone, " + "tb_users.real_name, tf1.name, tf2.name, tb_corporations.name " + "FROM tb_users LEFT JOIN tb_tariffs AS tf1 " + "ON tf1.pk_tariff = tb_users.fk_tariff " + "LEFT JOIN tb_tariffs AS tf2 " + "ON tf2.pk_tariff = tb_users.fk_tariff_change " + "LEFT JOIN tb_corporations " + "ON tb_corporations.pk_corporation = tb_users.fk_corporation " + "WHERE tb_users.name = '" << elogin << "'"; + + result = PQexec(connection, query.str().c_str()); + } if (PQresultStatus(result) != PGRES_TUPLES_OK) { @@ -795,9 +804,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK) printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): '%s'\n", strError.c_str()); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -809,51 +818,54 @@ if (tuples != 1) printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } uint32_t uid; -std::stringstream tuple; - -tuple << PQgetvalue(result, 0, 0) << " "; // uid -conf->address = PQgetvalue(result, 0, 1); // address -conf->alwaysOnline = !strncmp(PQgetvalue(result, 0, 2), "t", 1); -tuple << PQgetvalue(result, 0, 3) << " "; // credit -conf->creditExpire = TS2Int(PQgetvalue(result, 0, 4)); // creditExpire -conf->disabled = !strncmp(PQgetvalue(result, 0, 5), "t", 1); -conf->disabledDetailStat = !strncmp(PQgetvalue(result, 0, 6), "t", 1); -conf->email = PQgetvalue(result, 0, 7); // email -conf->group = PQgetvalue(result, 0, 8); // group -conf->note = PQgetvalue(result, 0, 9); // note -conf->passive = !strncmp(PQgetvalue(result, 0, 10), "t", 1); -conf->password = PQgetvalue(result, 0, 11); // password -conf->phone = PQgetvalue(result, 0, 12); // phone -conf->realName = PQgetvalue(result, 0, 13); // realName -conf->tariffName = PQgetvalue(result, 0, 14); // tariffName -conf->nextTariff = PQgetvalue(result, 0, 15); // nextTariff -conf->corp = PQgetvalue(result, 0, 16); // corp + { + std::stringstream tuple; + tuple << PQgetvalue(result, 0, 0) << " "; // uid + conf->address = PQgetvalue(result, 0, 1); // address + conf->alwaysOnline = !strncmp(PQgetvalue(result, 0, 2), "t", 1); + tuple << PQgetvalue(result, 0, 3) << " "; // credit + conf->creditExpire = TS2Int(PQgetvalue(result, 0, 4)); // creditExpire + conf->disabled = !strncmp(PQgetvalue(result, 0, 5), "t", 1); + conf->disabledDetailStat = !strncmp(PQgetvalue(result, 0, 6), "t", 1); + conf->email = PQgetvalue(result, 0, 7); // email + conf->group = PQgetvalue(result, 0, 8); // group + conf->note = PQgetvalue(result, 0, 9); // note + conf->passive = !strncmp(PQgetvalue(result, 0, 10), "t", 1); + conf->password = PQgetvalue(result, 0, 11); // password + conf->phone = PQgetvalue(result, 0, 12); // phone + conf->realName = PQgetvalue(result, 0, 13); // realName + conf->tariffName = PQgetvalue(result, 0, 14); // tariffName + conf->nextTariff = PQgetvalue(result, 0, 15); // nextTariff + conf->corp = PQgetvalue(result, 0, 16); // corp -PQclear(result); + PQclear(result); -if (conf->tariffName == "") - conf->tariffName = NO_TARIFF_NAME; -if (conf->corp == "") - conf->corp = NO_CORP_NAME; + if (conf->tariffName == "") + conf->tariffName = NO_TARIFF_NAME; + if (conf->corp == "") + conf->corp = NO_CORP_NAME; -tuple >> uid - >> conf->credit; + tuple >> uid + >> conf->credit; + } -query.str(""); -query << "SELECT name FROM tb_services " - "WHERE pk_service IN (SELECT fk_service " - "FROM tb_users_services " - "WHERE fk_user = " << uid << ")"; + { + std::ostringstream query; + query << "SELECT name FROM tb_services " + "WHERE pk_service IN (SELECT fk_service " + "FROM tb_users_services " + "WHERE fk_user = " << uid << ")"; -result = PQexec(connection, query.str().c_str()); + result = PQexec(connection, query.str().c_str()); + } if (PQresultStatus(result) != PGRES_TUPLES_OK) { @@ -861,9 +873,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK) printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): '%s'\n", strError.c_str()); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -871,17 +883,19 @@ tuples = PQntuples(result); for (int i = 0; i < tuples; ++i) { - conf->service.push_back(PQgetvalue(result, i, 0)); + conf->services.push_back(PQgetvalue(result, i, 0)); } PQclear(result); -query.str(""); -query << "SELECT num, data " - "FROM tb_users_data " - "WHERE fk_user = " << uid; + { + std::ostringstream query; + query << "SELECT num, data " + "FROM tb_users_data " + "WHERE fk_user = " << uid; -result = PQexec(connection, query.str().c_str()); + result = PQexec(connection, query.str().c_str()); + } if (PQresultStatus(result) != PGRES_TUPLES_OK) { @@ -889,9 +903,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK) printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): '%s'\n", strError.c_str()); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -906,18 +920,24 @@ for (int i = 0; i < tuples; ++i) } else { - conf->userdata[i] = PQgetvalue(result, i, 1); + if (num < USERDATA_NUM && + num >= 0) + { + conf->userdata[num] = PQgetvalue(result, i, 1); + } } } PQclear(result); -query.str(""); -query << "SELECT host(ip), masklen(ip) " - "FROM tb_allowed_ip " - "WHERE fk_user = " << uid; + { + std::ostringstream query; + query << "SELECT host(ip), masklen(ip) " + "FROM tb_allowed_ip " + "WHERE fk_user = " << uid; -result = PQexec(connection, query.str().c_str()); + result = PQexec(connection, query.str().c_str()); + } if (PQresultStatus(result) != PGRES_TUPLES_OK) { @@ -925,9 +945,9 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK) printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): '%s'\n", strError.c_str()); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -966,15 +986,15 @@ return 0; } //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::WriteUserChgLog(const string & login, - const string & admLogin, +int POSTGRESQL_STORE::WriteUserChgLog(const std::string & login, + const std::string & admLogin, uint32_t admIP, - const string & paramName, - const string & oldValue, - const string & newValue, - const string & message = "") const + const std::string & paramName, + const std::string & oldValue, + const std::string & newValue, + const std::string & message = "") const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -1006,9 +1026,9 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -1016,9 +1036,9 @@ if (EscapeString(eadminLogin)) { printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to escape admin's login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -1026,9 +1046,9 @@ if (EscapeString(eparam)) { printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to escape param's name'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -1036,9 +1056,9 @@ if (EscapeString(eold)) { printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to escape old value'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -1046,13 +1066,13 @@ if (EscapeString(enew)) { printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to escape new value'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteUserChgLog(): 'Failed to rollback transaction'\n"); + } return -1; } -std::stringstream query; +std::ostringstream query; query << "SELECT sp_add_param_log_entry(" "'" << elogin << "', " "'" << eadminLogin << "', CAST('" @@ -1089,9 +1109,9 @@ return 0; } //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const +int POSTGRESQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -1118,13 +1138,13 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteUserConnect(): 'Failed to rollback transaction'\n"); + } return -1; } -std::stringstream query; +std::ostringstream query; if (version < 6) { query << "SELECT sp_add_session_log_entry(" @@ -1168,16 +1188,16 @@ return 0; } //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::WriteUserDisconnect(const string & login, - const DIR_TRAFF & up, - const DIR_TRAFF & down, +int POSTGRESQL_STORE::WriteUserDisconnect(const std::string & login, + const DIR_TRAFF & monthUp, + const DIR_TRAFF & monthDown, const DIR_TRAFF & sessionUp, const DIR_TRAFF & sessionDown, double cash, double freeMb, const std::string & reason) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -1204,9 +1224,9 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -1216,32 +1236,34 @@ if (EscapeString(ereason)) { printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to escape reason'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n"); + } return -1; } -std::stringstream query; -if (version < 6) - { - // Old database version - no freeMb logging support - query << "SELECT sp_add_session_log_entry(" - "'" << elogin << "', " - "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), " - "'d', CAST('0.0.0.0/0' AS INET), " - << cash << ")"; - } -else { - query << "SELECT sp_add_session_log_entry(" - "'" << elogin << "', " - "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), " - "'d', CAST('0.0.0.0/0' AS INET), " - << cash << ", " << freeMb << ", '" << ereason << "')"; - } + std::ostringstream query; + if (version < 6) + { + // Old database version - no freeMb logging support + query << "SELECT sp_add_session_log_entry(" + "'" << elogin << "', " + "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), " + "'d', CAST('0.0.0.0/0' AS INET), " + << cash << ")"; + } + else + { + query << "SELECT sp_add_session_log_entry(" + "'" << elogin << "', " + "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), " + "'d', CAST('0.0.0.0/0' AS INET), " + << cash << ", " << freeMb << ", '" << ereason << "')"; + } -result = PQexec(connection, query.str().c_str()); + result = PQexec(connection, query.str().c_str()); + } if (PQresultStatus(result) != PGRES_TUPLES_OK) { @@ -1263,9 +1285,9 @@ if (tuples != 1) printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -1277,9 +1299,9 @@ if (str2x(PQgetvalue(result, 0, 0), lid)) printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): '%s'\n", strError.c_str()); PQclear(result); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteUserDisconnect(): 'Failed to rollback transaction'\n"); + } return -1; } @@ -1287,7 +1309,7 @@ PQclear(result); for (int i = 0; i < DIR_NUM; ++i) { - std::stringstream query; + std::ostringstream query; query << "INSERT INTO tb_sessions_data " "(fk_session_log, " "dir_num, " @@ -1300,8 +1322,8 @@ for (int i = 0; i < DIR_NUM; ++i) << i << ", " << sessionUp[i] << ", " << sessionDown[i] << ", " - << up[i] << ", " - << down[i] << ")"; + << monthUp[i] << ", " + << monthDown[i] << ")"; result = PQexec(connection, query.str().c_str()); @@ -1330,11 +1352,11 @@ return 0; } //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::WriteDetailedStat(const map & statTree, +int POSTGRESQL_STORE::WriteDetailedStat(const std::map & statTree, time_t lastStat, - const string & login) const + const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -1347,8 +1369,6 @@ if (PQstatus(connection) != CONNECTION_OK) } } -PGresult * result; - if (StartTransaction()) { printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Failed to start transaction'\n"); @@ -1361,18 +1381,18 @@ if (EscapeString(elogin)) { printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Failed to escape login'\n"); if (RollbackTransaction()) - { - printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Failed to rollback transaction'\n"); - } + { + printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Failed to rollback transaction'\n"); + } return -1; } -map::const_iterator it; +std::map::const_iterator it; time_t currTime = time(NULL); for (it = statTree.begin(); it != statTree.end(); ++it) { - std::stringstream query; + std::ostringstream query; query << "INSERT INTO tb_detail_stats " "(till_time, from_time, fk_user, " "dir_num, ip, download, upload, cost) " @@ -1386,7 +1406,7 @@ for (it = statTree.begin(); it != statTree.end(); ++it) << it->second.up << ", " << it->second.cash << ")"; - result = PQexec(connection, query.str().c_str()); + PGresult * result = PQexec(connection, query.str().c_str()); if (PQresultStatus(result) != PGRES_COMMAND_OK) { @@ -1413,9 +1433,9 @@ return 0; } //----------------------------------------------------------------------------- -int POSTGRESQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const +int POSTGRESQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); return SaveStat(stat, login, year, month); } @@ -1426,21 +1446,23 @@ int POSTGRESQL_STORE::SaveUserServices(uint32_t uid, { PGresult * result; -std::stringstream query; -query << "DELETE FROM tb_users_services WHERE fk_user = " << uid; + { + std::ostringstream query; + query << "DELETE FROM tb_users_services WHERE fk_user = " << uid; -result = PQexec(connection, query.str().c_str()); + result = PQexec(connection, query.str().c_str()); + + if (PQresultStatus(result) != PGRES_COMMAND_OK) + { + strError = PQresultErrorMessage(result); + PQclear(result); + printfd(__FILE__, "POSTGRESQL_STORE::SaveUserServices(): '%s'\n", strError.c_str()); + return -1; + } -if (PQresultStatus(result) != PGRES_COMMAND_OK) - { - strError = PQresultErrorMessage(result); PQclear(result); - printfd(__FILE__, "POSTGRESQL_STORE::SaveUserServices(): '%s'\n", strError.c_str()); - return -1; } -PQclear(result); - std::vector::const_iterator it; for (it = services.begin(); it != services.end(); ++it) @@ -1453,7 +1475,7 @@ for (it = services.begin(); it != services.end(); ++it) return -1; } - std::stringstream query; + std::ostringstream query; query << "INSERT INTO tb_users_services " "(fk_user, fk_service) " "VALUES " @@ -1484,10 +1506,12 @@ int POSTGRESQL_STORE::SaveUserIPs(uint32_t uid, { PGresult * result; -std::stringstream query; -query << "DELETE FROM tb_allowed_ip WHERE fk_user = " << uid; + { + std::ostringstream query; + query << "DELETE FROM tb_allowed_ip WHERE fk_user = " << uid; -result = PQexec(connection, query.str().c_str()); + result = PQexec(connection, query.str().c_str()); + } if (PQresultStatus(result) != PGRES_COMMAND_OK) { @@ -1499,9 +1523,9 @@ if (PQresultStatus(result) != PGRES_COMMAND_OK) PQclear(result); -for (int i = 0; i < ips.Count(); ++i) +for (size_t i = 0; i < ips.Count(); ++i) { - std::stringstream query; + std::ostringstream query; query << "INSERT INTO tb_allowed_ip " "(fk_user, ip) " "VALUES " @@ -1541,7 +1565,7 @@ for (unsigned i = 0; i < data.size(); ++i) PGresult * result; - std::stringstream query; + std::ostringstream query; query << "SELECT sp_set_user_data(" << uid << ", " << "CAST(" << i << " AS SMALLINT), "