11 #include "stg/user_ips.h"
12 #include "stg/user_conf.h"
13 #include "stg/user_stat.h"
14 #include "stg/blowfish.h"
15 #include "mysql_store.h"
17 #define adm_enc_passwd "cjeifY8m3"
22 const int pt_mega = 1024 * 1024;
23 const string badSyms = "'`";
24 const char repSym = '\"';
25 const int RepitTimes = 3;
27 int GetInt(const string & str, int * val, int defaultVal)
31 *val = strtol(str.c_str(), &res, 10);
35 *val = defaultVal; //Error!
42 int GetDouble(const string & str, double * val, double defaultVal)
46 *val = strtod(str.c_str(), &res);
50 *val = defaultVal; //Error!
57 int GetTime(const string & str, time_t * val, time_t defaultVal)
61 *val = strtol(str.c_str(), &res, 10);
65 *val = defaultVal; //Error!
72 //-----------------------------------------------------------------------------
73 string ReplaceStr(string source, const string symlist, const char chgsym)
75 string::size_type pos=0;
77 while( (pos = source.find_first_of(symlist,pos)) != string::npos)
78 source.replace(pos, 1,1, chgsym);
83 int GetULongLongInt(const string & str, uint64_t * val, uint64_t defaultVal)
87 *val = strtoull(str.c_str(), &res, 10);
91 *val = defaultVal; //Error!
98 class MYSQL_STORE_CREATOR
104 MYSQL_STORE_CREATOR()
105 : ms(new MYSQL_STORE())
108 ~MYSQL_STORE_CREATOR()
113 MYSQL_STORE * GetStore()
118 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
123 return msc.GetStore();
125 //-----------------------------------------------------------------------------
126 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
130 //-----------------------------------------------------------------------------
131 MYSQL_STORE_SETTINGS::~MYSQL_STORE_SETTINGS()
135 //-----------------------------------------------------------------------------
136 int MYSQL_STORE_SETTINGS::ParseParam(const vector<PARAM_VALUE> & moduleParams,
137 const string & name, string & result)
141 vector<PARAM_VALUE>::const_iterator pvi;
142 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
143 if (pvi == moduleParams.end())
145 errorStr = "Parameter \'" + name + "\' not found.";
149 result = pvi->value[0];
153 //-----------------------------------------------------------------------------
154 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
156 if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
157 ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
159 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
160 ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
162 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
163 ParseParam(s.moduleParams, "dbname", dbName) < 0)
165 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
166 ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
171 //-----------------------------------------------------------------------------
172 const string & MYSQL_STORE_SETTINGS::GetStrError() const
176 //-----------------------------------------------------------------------------
177 string MYSQL_STORE_SETTINGS::GetDBUser() const
181 //-----------------------------------------------------------------------------
182 string MYSQL_STORE_SETTINGS::GetDBPassword() const
186 //-----------------------------------------------------------------------------
187 string MYSQL_STORE_SETTINGS::GetDBHost() const
191 //-----------------------------------------------------------------------------
192 string MYSQL_STORE_SETTINGS::GetDBName() const
196 //-----------------------------------------------------------------------------
197 //-----------------------------------------------------------------------------
198 //-----------------------------------------------------------------------------
199 MYSQL_STORE::MYSQL_STORE()
201 version = "mysql_store v.0.67";
203 //-----------------------------------------------------------------------------
204 MYSQL_STORE::~MYSQL_STORE()
207 //-----------------------------------------------------------------------------
208 void MYSQL_STORE::SetSettings(const MODULE_SETTINGS & s)
212 //-----------------------------------------------------------------------------
213 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
217 if( (ret = mysql_query(sock,sQuery)) )
219 for(i=0; i<RepitTimes; i++)
221 if( (ret = mysql_query(sock,sQuery)) )
222 ;//need to send error result
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
233 int MYSQL_STORE::ParseSettings()
235 int ret = storeSettings.ParseSettings(settings);
240 errorStr = storeSettings.GetStrError();
243 if(storeSettings.GetDBPassword().length() == 0)
245 errorStr = "Database password must be not empty. Please read Manual.";
249 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
250 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
253 errorStr = "Couldn't connect to mysql engine! With error:\n";
254 errorStr += mysql_error(&mysql);
260 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
262 string res = "CREATE DATABASE " + storeSettings.GetDBName();
264 if(MysqlQuery(res.c_str(),sock))
266 errorStr = "Couldn't create database! With error:\n";
267 errorStr += mysql_error(sock);
273 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
275 errorStr = "Couldn't select database! With error:\n";
276 errorStr += mysql_error(sock);
280 ret = CheckAllTables(sock);
284 ret = CheckAllTables(sock);
291 //-----------------------------------------------------------------------------
292 const string & MYSQL_STORE::GetStrError() const
296 //-----------------------------------------------------------------------------
297 const string & MYSQL_STORE::GetVersion() const
301 //-----------------------------------------------------------------------------
302 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
306 if (!(result=mysql_list_tables(sock,str.c_str() )))
308 errorStr = "Couldn't get tables list With error:\n";
309 errorStr += mysql_error(sock);
314 unsigned int num_rows = mysql_num_rows(result);
317 mysql_free_result(result);
319 return (num_rows == 1);
321 //-----------------------------------------------------------------------------
322 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
324 //admins-----------------------------------------------------------------------
325 if(!IsTablePresent("admins",sock))
327 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
328 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
329 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
330 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
331 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
333 if(MysqlQuery(qbuf,sock))
335 errorStr = "Couldn't create admin table list With error:\n";
336 errorStr += mysql_error(sock);
341 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
342 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
343 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
345 if(MysqlQuery(qbuf,sock))
347 errorStr = "Couldn't create default admin. With error:\n";
348 errorStr += mysql_error(sock);
354 //tariffs-----------------------------------------------------------------------
356 if(!IsTablePresent("tariffs",sock))
358 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
360 for (int i = 0; i < DIR_NUM; i++)
362 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
365 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
368 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
371 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
374 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
377 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
380 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
383 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
387 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
388 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
390 if(MysqlQuery(res.c_str(),sock))
392 errorStr = "Couldn't create tariffs table list With error:\n";
393 errorStr += mysql_error(sock);
398 res = "INSERT INTO tariffs SET name='tariff',";
400 for (int i = 0; i < DIR_NUM; i++)
402 strprintf(¶m, " NoDiscount%d=1,", i);
405 strprintf(¶m, " Threshold%d=0,", i);
408 strprintf(¶m, " Time%d='0:0-0:0',", i);
413 strprintf(¶m, " SinglePrice%d=0,", i);
419 strprintf(¶m, " PriceDayA%d=0.0,", i);
424 strprintf(¶m, " PriceDayB%d=0.0,", i);
430 strprintf(¶m, " PriceNightA%d=0.0,", i);
435 strprintf(¶m, " PriceNightB%d=0.0,", i);
440 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
441 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
442 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
444 if(MysqlQuery(res.c_str(),sock))
446 errorStr = "Couldn't create default tariff. With error:\n";
447 errorStr += mysql_error(sock);
453 //users-----------------------------------------------------------------------
454 if(!IsTablePresent("users",sock))
456 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
457 "Passive INT(3) DEFAULT 0,Down INT(3) DEFAULT 0,DisabledDetailStat INT(3) DEFAULT 0,AlwaysOnline INT(3) DEFAULT 0,Tariff VARCHAR(40) NOT NULL DEFAULT '',"\
458 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
459 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
460 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
462 for (int i = 0; i < USERDATA_NUM; i++)
464 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
468 param = " CreditExpire INT(11) DEFAULT 0,";
471 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
474 for (int i = 0; i < DIR_NUM; i++)
476 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
479 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
483 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
484 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
485 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
486 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
489 if(MysqlQuery(res.c_str(),sock))
491 errorStr = "Couldn't create users table list With error:\n";
492 errorStr += mysql_error(sock);
493 errorStr += "\n\n" + res;
498 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
499 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
500 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
501 "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
504 for (int i = 0; i < DIR_NUM; i++)
506 strprintf(¶m, " D%d=0,", i);
509 strprintf(¶m, " U%d=0,", i);
513 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
514 "LastCashAddTime=0, PassiveTime=0";
516 if(MysqlQuery(res.c_str(),sock))
518 errorStr = "Couldn't create default user. With error:\n";
519 errorStr += mysql_error(sock);
525 //logs-----------------------------------------------------------------------
526 if(!IsTablePresent("logs"))
528 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
532 errorStr = "Couldn't create admin table list With error:\n";
533 errorStr += mysql_error(sock);
538 //messages---------------------------------------------------------------------
539 if(!IsTablePresent("messages",sock))
541 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
542 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
543 "stgRepeat INT, repeatPeriod INT, text TEXT)");
545 if(MysqlQuery(qbuf,sock))
547 errorStr = "Couldn't create messages table. With error:\n";
548 errorStr += mysql_error(sock);
554 //month_stat-------------------------------------------------------------------
555 if(!IsTablePresent("stat",sock))
557 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
559 for (int i = 0; i < DIR_NUM; i++)
561 strprintf(¶m, " U%d BIGINT,", i);
564 strprintf(¶m, " D%d BIGINT,", i);
568 res += " cash DOUBLE, INDEX (login))";
570 if(MysqlQuery(res.c_str(),sock))
572 errorStr = "Couldn't create stat table. With error:\n";
573 errorStr += mysql_error(sock);
581 //-----------------------------------------------------------------------------
582 //-----------------------------------------------------------------------------
584 int MYSQL_STORE::GetAllParams(vector<string> * ParamList,
585 const string & table, const string & name) const
594 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
596 if(MysqlGetQuery(qbuf,sock))
598 errorStr = "Couldn't GetAllParams Query for: ";
599 errorStr += name + " - " + table + "\n";
600 errorStr += mysql_error(sock);
605 if (!(res=mysql_store_result(sock)))
607 errorStr = "Couldn't GetAllParams Results for: ";
608 errorStr += name + " - " + table + "\n";
609 errorStr += mysql_error(sock);
613 num = mysql_num_rows(res);
617 row = mysql_fetch_row(res);
618 ParamList->push_back(row[0]);
621 mysql_free_result(res);
627 //-----------------------------------------------------------------------------
628 int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
630 if(GetAllParams(usersList, "users", "login"))
635 //-----------------------------------------------------------------------------
636 int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
638 if(GetAllParams(adminsList, "admins", "login"))
643 //-----------------------------------------------------------------------------
644 int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
646 if(GetAllParams(tariffsList, "tariffs", "name"))
651 //-----------------------------------------------------------------------------
652 int MYSQL_STORE::AddUser(const string & login) const
654 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
656 if(MysqlSetQuery(qbuf))
658 errorStr = "Couldn't add user:\n";
659 //errorStr += mysql_error(sock);
665 //-----------------------------------------------------------------------------
666 int MYSQL_STORE::DelUser(const string & login) const
668 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
670 if(MysqlSetQuery(qbuf))
672 errorStr = "Couldn't delete user:\n";
673 //errorStr += mysql_error(sock);
679 //-----------------------------------------------------------------------------
680 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
687 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
688 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
689 RealName, StgGroup, Credit, TariffChange, ";
691 for (int i = 0; i < USERDATA_NUM; i++)
693 sprintf(qbuf, "Userdata%d, ", i);
697 query += "CreditExpire, IP FROM users WHERE login='";
698 query += login + "' LIMIT 1";
700 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
702 if(MysqlGetQuery(query.c_str(),sock))
704 errorStr = "Couldn't restore Tariff(on query):\n";
705 errorStr += mysql_error(sock);
710 if (!(res=mysql_store_result(sock)))
712 errorStr = "Couldn't restore Tariff(on getting result):\n";
713 errorStr += mysql_error(sock);
718 if (mysql_num_rows(res) != 1)
720 errorStr = "User not found";
725 row = mysql_fetch_row(res);
729 conf->password = row[1];
731 if (conf->password.empty())
733 mysql_free_result(res);
734 errorStr = "User \'" + login + "\' password is blank.";
739 if (GetInt(row[2],&conf->passive, 0) != 0)
741 mysql_free_result(res);
742 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
747 if (GetInt(row[3], &conf->disabled, 0) != 0)
749 mysql_free_result(res);
750 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
755 if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
757 mysql_free_result(res);
758 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
763 if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
765 mysql_free_result(res);
766 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
771 conf->tariffName = row[6];
773 if (conf->tariffName.empty())
775 mysql_free_result(res);
776 errorStr = "User \'" + login + "\' tariff is blank.";
781 conf->address = row[7];
782 conf->phone = row[8];
783 conf->email = row[9];
784 conf->note = row[10];
785 conf->realName = row[11];
786 conf->group = row[12];
788 if (GetDouble(row[13], &conf->credit, 0) != 0)
790 mysql_free_result(res);
791 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
796 conf->nextTariff = row[14];
798 for (int i = 0; i < USERDATA_NUM; i++)
800 conf->userdata[i] = row[15+i];
803 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
805 string ipStr = row[16+USERDATA_NUM];
811 catch (const string & s)
813 mysql_free_result(res);
814 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
820 mysql_free_result(res);
825 //-----------------------------------------------------------------------------
826 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
836 for (int i = 0; i < DIR_NUM; i++)
838 sprintf(qbuf, "D%d, U%d, ", i, i);
842 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
843 FROM users WHERE login = '";
844 query += login + "'";
846 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
848 if(MysqlGetQuery(query.c_str() ,sock))
850 errorStr = "Couldn't restore UserStat(on query):\n";
851 errorStr += mysql_error(sock);
856 if (!(res=mysql_store_result(sock)))
858 errorStr = "Couldn't restore UserStat(on getting result):\n";
859 errorStr += mysql_error(sock);
864 row = mysql_fetch_row(res);
866 unsigned int startPos=0;
870 for (int i = 0; i < DIR_NUM; i++)
873 sprintf(s, "D%d", i);
874 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
876 mysql_free_result(res);
877 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
881 stat->down[i] = traff;
883 sprintf(s, "U%d", i);
884 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
886 mysql_free_result(res);
887 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
894 startPos += (2*DIR_NUM);
896 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
898 mysql_free_result(res);
899 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
904 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
906 mysql_free_result(res);
907 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
912 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
914 mysql_free_result(res);
915 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
920 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
922 mysql_free_result(res);
923 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
928 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
930 mysql_free_result(res);
931 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
936 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
938 mysql_free_result(res);
939 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
944 mysql_free_result(res);
948 //-----------------------------------------------------------------------------
949 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
954 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
955 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
956 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
957 conf.password.c_str(),
960 conf.disabledDetailStat,
962 conf.tariffName.c_str(),
963 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
964 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
965 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
966 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
967 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
968 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
970 conf.nextTariff.c_str()
973 for (int i = 0; i < USERDATA_NUM; i++)
975 strprintf(¶m, " Userdata%d='%s',", i,
976 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
980 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
986 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
989 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
992 if(MysqlSetQuery(res.c_str()))
994 errorStr = "Couldn't save user conf:\n";
995 //errorStr += mysql_error(sock);
1001 //-----------------------------------------------------------------------------
1002 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
1007 res = "UPDATE users SET";
1009 for (int i = 0; i < DIR_NUM; i++)
1011 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1014 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1018 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1019 " PassiveTime=%d, LastActivityTime=%d",
1023 stat.lastCashAddTime,
1025 stat.lastActivityTime
1029 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1032 if(MysqlSetQuery(res.c_str()))
1034 errorStr = "Couldn't save user stat:\n";
1035 // errorStr += mysql_error(sock);
1041 //-----------------------------------------------------------------------------
1042 int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
1044 string res, tempStr;
1053 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1054 if (!(sock=MysqlConnect())){
1055 errorStr = "Couldn't connect to Server";
1058 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1060 errorStr = "Couldn't get table " + tempStr + ":\n";
1061 errorStr += mysql_error(sock);
1066 unsigned int num_rows = mysql_num_rows(result);
1068 mysql_free_result(result);
1072 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1073 lt->tm_mon+1, lt->tm_year+1900);
1075 if(MysqlQuery(qbuf,sock))
1077 errorStr = "Couldn't create WriteDetailedStat table:\n";
1078 errorStr += mysql_error(sock);
1084 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1088 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1089 lt->tm_mon+1, lt->tm_year+1900,
1090 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1092 if(MysqlQuery(send.c_str(),sock))
1094 errorStr = "Couldn't write log string:\n";
1095 errorStr += mysql_error(sock);
1103 //-----------------------------------------------------------------------------
1104 int MYSQL_STORE::WriteUserChgLog(const string & login,
1105 const string & admLogin,
1107 const string & paramName,
1108 const string & oldValue,
1109 const string & newValue,
1110 const string & message) const
1112 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1113 + paramName + "\' parameter changed from \'" + oldValue +
1114 "\' to \'" + newValue + "\'. " + message;
1116 return WriteLogString(userLogMsg, login);
1118 //-----------------------------------------------------------------------------
1119 int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1121 string logStr = "Connect, " + inet_ntostring(ip);
1122 return WriteLogString(logStr, login);
1124 //-----------------------------------------------------------------------------
1125 int MYSQL_STORE::WriteUserDisconnect(const string & login,
1126 const DIR_TRAFF & up,
1127 const DIR_TRAFF & down,
1128 const DIR_TRAFF & sessionUp,
1129 const DIR_TRAFF & sessionDown,
1132 const std::string & reason) const
1134 string logStr = "Disconnect, ";
1139 stringstream sscash;
1145 sssd << sessionDown;
1149 logStr += " session upload: \'";
1150 logStr += sssu.str();
1151 logStr += "\' session download: \'";
1152 logStr += sssd.str();
1153 logStr += "\' month upload: \'";
1154 logStr += ssmu.str();
1155 logStr += "\' month download: \'";
1156 logStr += ssmd.str();
1157 logStr += "\' cash: \'";
1158 logStr += sscash.str();
1161 return WriteLogString(logStr, login);
1163 //-----------------------------------------------------------------------------
1164 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1165 const string & login) const
1169 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1170 login.c_str(), month+1, year+1900);
1172 for (int i = 0; i < DIR_NUM; i++)
1174 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1177 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1181 strprintf(¶m, " cash=%f", stat.cash);
1184 if(MysqlSetQuery(res.c_str()))
1186 errorStr = "Couldn't SaveMonthStat:\n";
1187 //errorStr += mysql_error(sock);
1193 //-----------------------------------------------------------------------------*/
1194 int MYSQL_STORE::AddAdmin(const string & login) const
1196 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1198 if(MysqlSetQuery(qbuf))
1200 errorStr = "Couldn't add admin:\n";
1201 //errorStr += mysql_error(sock);
1207 //-----------------------------------------------------------------------------*/
1208 int MYSQL_STORE::DelAdmin(const string & login) const
1210 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1212 if(MysqlSetQuery(qbuf))
1214 errorStr = "Couldn't delete admin:\n";
1215 //errorStr += mysql_error(sock);
1221 //-----------------------------------------------------------------------------*/
1222 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1224 char passwordE[2 * ADM_PASSWD_LEN + 2];
1225 char pass[ADM_PASSWD_LEN + 1];
1226 char adminPass[ADM_PASSWD_LEN + 1];
1228 memset(pass, 0, sizeof(pass));
1229 memset(adminPass, 0, sizeof(adminPass));
1232 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1234 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1235 adminPass[ADM_PASSWD_LEN - 1] = 0;
1237 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1239 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1242 pass[ADM_PASSWD_LEN - 1] = 0;
1243 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1245 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1246 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1247 "WHERE login='%s' LIMIT 1",
1259 if(MysqlSetQuery(qbuf))
1261 errorStr = "Couldn't save admin:\n";
1262 //errorStr += mysql_error(sock);
1268 //-----------------------------------------------------------------------------
1269 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1271 char pass[ADM_PASSWD_LEN + 1];
1272 char password[ADM_PASSWD_LEN + 1];
1273 char passwordE[2*ADM_PASSWD_LEN + 2];
1280 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1282 if(MysqlGetQuery(qbuf,sock))
1284 errorStr = "Couldn't restore admin:\n";
1285 errorStr += mysql_error(sock);
1290 if (!(res=mysql_store_result(sock)))
1292 errorStr = "Couldn't restore admin:\n";
1293 errorStr += mysql_error(sock);
1298 if ( mysql_num_rows(res) == 0)
1300 mysql_free_result(res);
1301 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1306 row = mysql_fetch_row(res);
1313 mysql_free_result(res);
1314 errorStr = "Error in parameter password";
1319 memset(passwordE, 0, sizeof(passwordE));
1320 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1322 memset(pass, 0, sizeof(pass));
1324 if (passwordE[0] != 0)
1326 Decode21(pass, passwordE);
1327 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1329 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1331 DecodeString(password + 8*i, pass + 8*i, &ctx);
1339 ac->password = password;
1341 if (GetInt(row[2], &a, 0) == 0)
1342 ac->priv.userConf = a;
1345 mysql_free_result(res);
1346 errorStr = "Error in parameter ChgConf";
1351 if (GetInt(row[3], &a, 0) == 0)
1352 ac->priv.userPasswd = a;
1355 mysql_free_result(res);
1356 errorStr = "Error in parameter ChgPassword";
1361 if (GetInt(row[4], &a, 0) == 0)
1362 ac->priv.userStat = a;
1365 mysql_free_result(res);
1366 errorStr = "Error in parameter ChgStat";
1371 if (GetInt(row[5], &a, 0) == 0)
1372 ac->priv.userCash = a;
1375 mysql_free_result(res);
1376 errorStr = "Error in parameter ChgCash";
1381 if (GetInt(row[6], &a, 0) == 0)
1382 ac->priv.userAddDel = a;
1385 mysql_free_result(res);
1386 errorStr = "Error in parameter UsrAddDel";
1391 if (GetInt(row[7], &a, 0) == 0)
1392 ac->priv.tariffChg = a;
1395 mysql_free_result(res);
1396 errorStr = "Error in parameter ChgTariff";
1401 if (GetInt(row[8], &a, 0) == 0)
1402 ac->priv.adminChg = a;
1405 mysql_free_result(res);
1406 errorStr = "Error in parameter ChgAdmin";
1411 mysql_free_result(res);
1415 //-----------------------------------------------------------------------------
1416 int MYSQL_STORE::AddTariff(const string & name) const
1418 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1420 if(MysqlSetQuery(qbuf))
1422 errorStr = "Couldn't add tariff:\n";
1423 // errorStr += mysql_error(sock);
1429 //-----------------------------------------------------------------------------
1430 int MYSQL_STORE::DelTariff(const string & name) const
1432 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1434 if(MysqlSetQuery(qbuf))
1436 errorStr = "Couldn't delete tariff: ";
1437 // errorStr += mysql_error(sock);
1443 //-----------------------------------------------------------------------------
1444 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1449 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1451 if(MysqlGetQuery(qbuf,sock))
1453 errorStr = "Couldn't restore Tariff:\n";
1454 errorStr += mysql_error(sock);
1459 if (!(res=mysql_store_result(sock)))
1461 errorStr = "Couldn't restore Tariff:\n";
1462 errorStr += mysql_error(sock);
1468 td->tariffConf.name = tariffName;
1470 row = mysql_fetch_row(res);
1473 for (int i = 0; i<DIR_NUM; i++)
1475 strprintf(¶m, "Time%d", i);
1477 if (str.length() == 0)
1479 mysql_free_result(res);
1480 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1485 ParseTariffTimeStr(str.c_str(),
1486 td->dirPrice[i].hDay,
1487 td->dirPrice[i].mDay,
1488 td->dirPrice[i].hNight,
1489 td->dirPrice[i].mNight);
1491 strprintf(¶m, "PriceDayA%d", i);
1492 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1494 mysql_free_result(res);
1495 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1499 td->dirPrice[i].priceDayA /= (1024*1024);
1501 strprintf(¶m, "PriceDayB%d", i);
1502 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1504 mysql_free_result(res);
1505 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1509 td->dirPrice[i].priceDayB /= (1024*1024);
1511 strprintf(¶m, "PriceNightA%d", i);
1512 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1514 mysql_free_result(res);
1515 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1519 td->dirPrice[i].priceNightA /= (1024*1024);
1521 strprintf(¶m, "PriceNightB%d", i);
1522 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1524 mysql_free_result(res);
1525 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1529 td->dirPrice[i].priceNightB /= (1024*1024);
1531 strprintf(¶m, "Threshold%d", i);
1532 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
1534 mysql_free_result(res);
1535 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1540 strprintf(¶m, "SinglePrice%d", i);
1541 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
1543 mysql_free_result(res);
1544 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1549 strprintf(¶m, "NoDiscount%d", i);
1550 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
1552 mysql_free_result(res);
1553 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1559 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1561 mysql_free_result(res);
1562 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1567 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1569 mysql_free_result(res);
1570 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1575 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1577 mysql_free_result(res);
1578 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1583 str = row[4+8*DIR_NUM];
1584 param = "TraffType";
1586 if (str.length() == 0)
1588 mysql_free_result(res);
1589 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1594 if (!strcasecmp(str.c_str(), "up"))
1595 td->tariffConf.traffType = TRAFF_UP;
1597 if (!strcasecmp(str.c_str(), "down"))
1598 td->tariffConf.traffType = TRAFF_DOWN;
1600 if (!strcasecmp(str.c_str(), "up+down"))
1601 td->tariffConf.traffType = TRAFF_UP_DOWN;
1603 if (!strcasecmp(str.c_str(), "max"))
1604 td->tariffConf.traffType = TRAFF_MAX;
1607 mysql_free_result(res);
1608 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1613 mysql_free_result(res);
1617 //-----------------------------------------------------------------------------
1618 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1622 string res="UPDATE tariffs SET";
1624 for (int i = 0; i < DIR_NUM; i++)
1626 strprintf(¶m, " PriceDayA%d=%f,", i,
1627 td.dirPrice[i].priceDayA * pt_mega);
1630 strprintf(¶m, " PriceDayB%d=%f,", i,
1631 td.dirPrice[i].priceDayB * pt_mega);
1634 strprintf(¶m, " PriceNightA%d=%f,", i,
1635 td.dirPrice[i].priceNightA * pt_mega);
1638 strprintf(¶m, " PriceNightB%d=%f,", i,
1639 td.dirPrice[i].priceNightB * pt_mega);
1642 strprintf(¶m, " Threshold%d=%d,", i,
1643 td.dirPrice[i].threshold);
1647 strprintf(¶m, " Time%d", i);
1649 strprintf(&s, "%0d:%0d-%0d:%0d",
1650 td.dirPrice[i].hDay,
1651 td.dirPrice[i].mDay,
1652 td.dirPrice[i].hNight,
1653 td.dirPrice[i].mNight);
1655 res += (param + "='" + s + "',");
1657 strprintf(¶m, " NoDiscount%d=%d,", i,
1658 td.dirPrice[i].noDiscount);
1661 strprintf(¶m, " SinglePrice%d=%d,", i,
1662 td.dirPrice[i].singlePrice);
1666 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1669 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1672 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1675 switch (td.tariffConf.traffType)
1678 res += " TraffType='up'";
1681 res += " TraffType='down'";
1684 res += " TraffType='up+down'";
1687 res += " TraffType='max'";
1690 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1693 if(MysqlSetQuery(res.c_str()))
1695 errorStr = "Couldn't save admin:\n";
1696 //errorStr += mysql_error(sock);
1702 //-----------------------------------------------------------------------------
1703 int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1705 const string & login) const
1707 string res, stTime, endTime, tempStr;
1714 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1722 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1724 if (!(sock=MysqlConnect())){
1729 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1731 errorStr = "Couldn't get table " + tempStr + ":\n";
1732 errorStr += mysql_error(sock);
1737 unsigned int num_rows = mysql_num_rows(result);
1739 mysql_free_result(result);
1743 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1744 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1745 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1746 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1747 lt->tm_mon+1, lt->tm_year+1900);
1749 if(MysqlQuery(qbuf,sock))
1751 errorStr = "Couldn't create WriteDetailedStat table:\n";
1752 errorStr += mysql_error(sock);
1761 lt1 = localtime(&lastStat);
1770 lt2 = localtime(&t);
1776 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1777 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1779 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1780 "day=%d,startTime='%s',endTime='%s',",
1781 lt->tm_mon+1, lt->tm_year+1900,
1788 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1789 stIter = statTree.begin();
1791 while (stIter != statTree.end())
1793 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1794 inet_ntostring(stIter->first.ip).c_str(),
1796 stIter->second.down,
1801 if( MysqlQuery((res+tempStr).c_str(),sock) )
1803 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1804 errorStr += mysql_error(sock);
1809 result=mysql_store_result(sock);
1811 mysql_free_result(result);
1818 //-----------------------------------------------------------------------------
1819 int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
1823 gettimeofday(&tv, NULL);
1825 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1827 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1829 (long long)msg->header.id
1832 if(MysqlSetQuery(qbuf))
1834 errorStr = "Couldn't add message:\n";
1835 //errorStr += mysql_error(sock);
1839 return EditMessage(*msg, login);
1841 //-----------------------------------------------------------------------------
1842 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1846 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1847 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1848 "WHERE login='%s' AND id=%lld LIMIT 1",
1850 msg.header.lastSendTime,
1851 msg.header.creationTime,
1852 msg.header.showTime,
1854 msg.header.repeatPeriod,
1855 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1857 (long long)msg.header.id
1860 if(MysqlSetQuery(res.c_str()))
1862 errorStr = "Couldn't edit message:\n";
1863 //errorStr += mysql_error(sock);
1869 //-----------------------------------------------------------------------------
1870 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1876 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1879 if(MysqlGetQuery(qbuf,sock))
1881 errorStr = "Couldn't GetMessage:\n";
1882 errorStr += mysql_error(sock);
1887 if (!(res=mysql_store_result(sock)))
1889 errorStr = "Couldn't GetMessage:\n";
1890 errorStr += mysql_error(sock);
1895 row = mysql_fetch_row(res);
1897 if(row[2]&&str2x(row[2], msg->header.type))
1899 mysql_free_result(res);
1900 errorStr = "Invalid value in message header for user: " + login;
1905 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1907 mysql_free_result(res);
1908 errorStr = "Invalid value in message header for user: " + login;
1913 if(row[4] && str2x(row[4], msg->header.creationTime))
1915 mysql_free_result(res);
1916 errorStr = "Invalid value in message header for user: " + login;
1921 if(row[5] && str2x(row[5], msg->header.showTime))
1923 mysql_free_result(res);
1924 errorStr = "Invalid value in message header for user: " + login;
1929 if(row[6] && str2x(row[6], msg->header.repeat))
1931 mysql_free_result(res);
1932 errorStr = "Invalid value in message header for user: " + login;
1937 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1939 mysql_free_result(res);
1940 errorStr = "Invalid value in message header for user: " + login;
1945 msg->header.id = id;
1948 mysql_free_result(res);
1952 //-----------------------------------------------------------------------------
1953 int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
1955 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1956 login.c_str(),(long long)id);
1958 if(MysqlSetQuery(qbuf))
1960 errorStr = "Couldn't delete Message:\n";
1961 //errorStr += mysql_error(sock);
1967 //-----------------------------------------------------------------------------
1968 int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1973 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
1975 if(MysqlGetQuery(qbuf,sock))
1977 errorStr = "Couldn't GetMessageHdrs:\n";
1978 errorStr += mysql_error(sock);
1983 if (!(res=mysql_store_result(sock)))
1985 errorStr = "Couldn't GetMessageHdrs:\n";
1986 errorStr += mysql_error(sock);
1991 unsigned int i, num_rows = mysql_num_rows(res);
1992 long long int unsigned id = 0;
1994 for (i=0; i<num_rows; i++)
1996 row = mysql_fetch_row(res);
1997 if (str2x(row[1], id))
2002 if(str2x(row[2], hdr.type))
2006 if(str2x(row[3], hdr.lastSendTime))
2010 if(str2x(row[4], hdr.creationTime))
2014 if(str2x(row[5], hdr.showTime))
2018 if(str2x(row[6], hdr.repeat))
2022 if(str2x(row[7], hdr.repeatPeriod))
2026 hdrsList->push_back(hdr);
2029 mysql_free_result(res);
2033 //-----------------------------------------------------------------------------
2035 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2038 int ret=MysqlGetQuery(Query,sock);
2042 //-----------------------------------------------------------------------------
2043 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2044 if (!(sock=MysqlConnect())) {
2047 return MysqlQuery(Query,sock);
2049 //-----------------------------------------------------------------------------
2050 MYSQL * MYSQL_STORE::MysqlConnect() const {
2052 if ( !(sock=mysql_init(NULL)) ){
2053 errorStr= "mysql init susck\n";
2056 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2057 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2060 errorStr = "Couldn't connect to mysql engine! With error:\n";
2061 errorStr += mysql_error(sock);
2065 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2066 errorStr = "Database lost !\n";
2072 //-----------------------------------------------------------------------------