X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/1bf5ae378079e212f6918d09d87dec5420a0d6ec..f907011b4827e85763372cdea76944b3b1cc3fd2:/projects/stargazer/plugins/store/mysql/mysql_store.cpp diff --git a/projects/stargazer/plugins/store/mysql/mysql_store.cpp b/projects/stargazer/plugins/store/mysql/mysql_store.cpp index f78586fb..95ac298d 100644 --- a/projects/stargazer/plugins/store/mysql/mysql_store.cpp +++ b/projects/stargazer/plugins/store/mysql/mysql_store.cpp @@ -8,11 +8,13 @@ #include #include -#include "user_ips.h" -#include "user_conf.h" -#include "user_stat.h" +#include "stg/user_ips.h" +#include "stg/user_conf.h" +#include "stg/user_stat.h" +#include "stg/blowfish.h" +#include "stg/plugin_creator.h" +#include "stg/logger.h" #include "mysql_store.h" -#include "blowfish.h" #define adm_enc_passwd "cjeifY8m3" char qbuf[4096]; @@ -95,42 +97,23 @@ int GetULongLongInt(const string & str, uint64_t * val, uint64_t defaultVal) return 0; } -class MYSQL_STORE_CREATOR -{ -private: - MYSQL_STORE * ms; - -public: - MYSQL_STORE_CREATOR() - : ms(new MYSQL_STORE()) - { - }; - ~MYSQL_STORE_CREATOR() - { - delete ms; - }; - - MYSQL_STORE * GetStore() - { - return ms; - }; -} msc; +PLUGIN_CREATOR msc; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -BASE_STORE * GetStore() +STORE * GetStore() { -return msc.GetStore(); +return msc.GetPlugin(); } //----------------------------------------------------------------------------- MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS() - : settings(NULL) + : settings(NULL), + errorStr(), + dbUser(), + dbPass(), + dbName(), + dbHost() { -} -//----------------------------------------------------------------------------- -MYSQL_STORE_SETTINGS::~MYSQL_STORE_SETTINGS() -{ - } //----------------------------------------------------------------------------- int MYSQL_STORE_SETTINGS::ParseParam(const vector & moduleParams, @@ -153,66 +136,41 @@ return 0; //----------------------------------------------------------------------------- int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) { -if (ParseParam(s.moduleParams, "dbuser", dbUser) < 0) +if (ParseParam(s.moduleParams, "user", dbUser) < 0 && + ParseParam(s.moduleParams, "dbuser", dbUser) < 0) return -1; -if (ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0) +if (ParseParam(s.moduleParams, "password", dbPass) < 0 && + ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0) return -1; -if (ParseParam(s.moduleParams, "dbname", dbName) < 0) +if (ParseParam(s.moduleParams, "database", dbName) < 0 && + ParseParam(s.moduleParams, "dbname", dbName) < 0) return -1; -if (ParseParam(s.moduleParams, "dbhost", dbHost) < 0) +if (ParseParam(s.moduleParams, "server", dbHost) < 0 && + ParseParam(s.moduleParams, "dbhost", dbHost) < 0) return -1; return 0; } //----------------------------------------------------------------------------- -const string & MYSQL_STORE_SETTINGS::GetStrError() const -{ -return errorStr; -} -//----------------------------------------------------------------------------- -string MYSQL_STORE_SETTINGS::GetDBUser() const -{ -return dbUser; -} -//----------------------------------------------------------------------------- -string MYSQL_STORE_SETTINGS::GetDBPassword() const -{ -return dbPass; -} -//----------------------------------------------------------------------------- -string MYSQL_STORE_SETTINGS::GetDBHost() const -{ -return dbHost; -} -//----------------------------------------------------------------------------- -string MYSQL_STORE_SETTINGS::GetDBName() const -{ -return dbName; -} -//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- MYSQL_STORE::MYSQL_STORE() + : errorStr(), + version("mysql_store v.0.67"), + storeSettings(), + settings(), + schemaVersion(0), + WriteServLog(GetStgLogger()) { -version = "mysql_store v.0.67"; -}; -//----------------------------------------------------------------------------- -MYSQL_STORE::~MYSQL_STORE() -{ }; //----------------------------------------------------------------------------- -void MYSQL_STORE::SetSettings(const MODULE_SETTINGS & s) -{ -settings = s; -} -//----------------------------------------------------------------------------- int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const { - int ret,i; + int ret; if( (ret = mysql_query(sock,sQuery)) ) { - for(i=0; i info; + if (GetAllParams(&info, "info", "version")) + schemaVersion = 0; + else + { + if (info.empty()) + schemaVersion = 0; + else + GetInt(info.front(), &schemaVersion, 0); + } +} //admins----------------------------------------------------------------------- if(!IsTablePresent("admins",sock)) { @@ -380,8 +371,9 @@ if(!IsTablePresent("tariffs",sock)) res += param; } - res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\ - "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')"; + res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0," + "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT ''," + "period VARCHAR(32) NOT NULL DEFAULT 'month')"; if(MysqlQuery(res.c_str(),sock)) { @@ -435,7 +427,7 @@ if(!IsTablePresent("tariffs",sock)) res += "PassiveCost=0.0, Fee=10.0, Free=0,"\ "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\ - "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'"; + "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down',period='month'"; if(MysqlQuery(res.c_str(),sock)) { @@ -444,6 +436,17 @@ if(!IsTablePresent("tariffs",sock)) mysql_close(sock); return -1; } + + sprintf(qbuf,"UPDATE info SET version=1"); + + if(MysqlQuery(qbuf,sock)) + { + errorStr = "Couldn't write default version. With error:\n"; + errorStr += mysql_error(sock); + mysql_close(sock); + return -1; + } + schemaVersion = 1; } //users----------------------------------------------------------------------- @@ -575,6 +578,29 @@ if(!IsTablePresent("stat",sock)) return 0; } //----------------------------------------------------------------------------- +int MYSQL_STORE::MakeUpdates(MYSQL * sock) +{ +if (schemaVersion < 1) + { + if (MysqlQuery("ALTER TABLE tariffs ADD period VARCHAR(32) NOT NULL DEFAULT 'month'", sock)) + { + errorStr = "Couldn't update tariffs table to version 1. With error:\n"; + errorStr += mysql_error(sock); + mysql_close(sock); + return -1; + } + if (MysqlQuery("UPDATE info SET version = 1", sock)) + { + errorStr = "Couldn't update DB schema version to 1. With error:\n"; + errorStr += mysql_error(sock); + mysql_close(sock); + return -1; + } + schemaVersion = 1; + WriteServLog("MYSQL_STORE: Updated DB schema to version %d", schemaVersion); + } +return 0; +} //----------------------------------------------------------------------------- int MYSQL_STORE::GetAllParams(vector * ParamList, @@ -711,6 +737,13 @@ if (!(res=mysql_store_result(sock))) return -1; } +if (mysql_num_rows(res) != 1) +{ + errorStr = "User not found"; + mysql_close(sock); + return -1; +} + row = mysql_fetch_row(res); string param; @@ -797,7 +830,7 @@ try { i = StrToIPS(ipStr); } -catch (string s) +catch (const string & s) { mysql_free_result(res); errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s; @@ -1262,6 +1295,10 @@ char password[ADM_PASSWD_LEN + 1]; char passwordE[2*ADM_PASSWD_LEN + 2]; BLOWFISH_CTX ctx; +memset(pass, 0, sizeof(pass)); +memset(password, 0, sizeof(password)); +memset(passwordE, 0, sizeof(passwordE)); + string p; MYSQL_RES *res; MYSQL_ROW row; @@ -1599,6 +1636,26 @@ else return -1; } +if (schemaVersion > 0) +{ + str = row[5+8*DIR_NUM]; + param = "Period"; + + if (str.length() == 0) + { + mysql_free_result(res); + errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; + mysql_close(sock); + return -1; + } + + td->tariffConf.period = TARIFF::StringToPeriod(str); + } +else + { + td->tariffConf.period = TARIFF::MONTH; + } + mysql_free_result(res); mysql_close(sock); return 0; @@ -1676,12 +1733,16 @@ switch (td.tariffConf.traffType) res += " TraffType='max'"; break; } + +if (schemaVersion > 0) + res += ", Period='" + TARIFF::PeriodToString(td.tariffConf.period) + "'"; + strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str()); res += param; if(MysqlSetQuery(res.c_str())) { - errorStr = "Couldn't save admin:\n"; + errorStr = "Couldn't save tariff:\n"; //errorStr += mysql_error(sock); return -1; } @@ -1774,7 +1835,6 @@ strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\ endTime.c_str() ); -int retRes; map::const_iterator stIter; stIter = statTree.begin(); @@ -1788,7 +1848,7 @@ while (stIter != statTree.end()) stIter->second.cash ); - if( (retRes = MysqlQuery((res+tempStr).c_str(),sock)) ) + if( MysqlQuery((res+tempStr).c_str(),sock) ) { errorStr = "Couldn't insert data in WriteDetailedStat:\n"; errorStr += mysql_error(sock); @@ -1863,8 +1923,8 @@ MYSQL_RES *res; MYSQL_ROW row; MYSQL * sock; -sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%lld LIMIT 1", - login.c_str(), id); +sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1", + login.c_str(), static_cast(id)); if(MysqlGetQuery(qbuf,sock)) {