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 "stg/plugin_creator.h"
16 #include "mysql_store.h"
18 #define adm_enc_passwd "cjeifY8m3"
23 const int pt_mega = 1024 * 1024;
24 const string badSyms = "'`";
25 const char repSym = '\"';
26 const int RepitTimes = 3;
28 int GetInt(const string & str, int * val, int defaultVal)
32 *val = strtol(str.c_str(), &res, 10);
36 *val = defaultVal; //Error!
43 int GetDouble(const string & str, double * val, double defaultVal)
47 *val = strtod(str.c_str(), &res);
51 *val = defaultVal; //Error!
58 int GetTime(const string & str, time_t * val, time_t defaultVal)
62 *val = strtol(str.c_str(), &res, 10);
66 *val = defaultVal; //Error!
73 //-----------------------------------------------------------------------------
74 string ReplaceStr(string source, const string symlist, const char chgsym)
76 string::size_type pos=0;
78 while( (pos = source.find_first_of(symlist,pos)) != string::npos)
79 source.replace(pos, 1,1, chgsym);
84 int GetULongLongInt(const string & str, uint64_t * val, uint64_t defaultVal)
88 *val = strtoull(str.c_str(), &res, 10);
92 *val = defaultVal; //Error!
99 PLUGIN_CREATOR<MYSQL_STORE> msc;
100 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
102 //-----------------------------------------------------------------------------
105 return msc.GetPlugin();
107 //-----------------------------------------------------------------------------
108 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
112 //-----------------------------------------------------------------------------
113 MYSQL_STORE_SETTINGS::~MYSQL_STORE_SETTINGS()
117 //-----------------------------------------------------------------------------
118 int MYSQL_STORE_SETTINGS::ParseParam(const vector<PARAM_VALUE> & moduleParams,
119 const string & name, string & result)
123 vector<PARAM_VALUE>::const_iterator pvi;
124 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
125 if (pvi == moduleParams.end())
127 errorStr = "Parameter \'" + name + "\' not found.";
131 result = pvi->value[0];
135 //-----------------------------------------------------------------------------
136 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
138 if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
139 ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
141 if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
142 ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
144 if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
145 ParseParam(s.moduleParams, "dbname", dbName) < 0)
147 if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
148 ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
153 //-----------------------------------------------------------------------------
154 const string & MYSQL_STORE_SETTINGS::GetStrError() const
158 //-----------------------------------------------------------------------------
159 string MYSQL_STORE_SETTINGS::GetDBUser() const
163 //-----------------------------------------------------------------------------
164 string MYSQL_STORE_SETTINGS::GetDBPassword() const
168 //-----------------------------------------------------------------------------
169 string MYSQL_STORE_SETTINGS::GetDBHost() const
173 //-----------------------------------------------------------------------------
174 string MYSQL_STORE_SETTINGS::GetDBName() const
178 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
181 MYSQL_STORE::MYSQL_STORE()
183 version = "mysql_store v.0.67";
185 //-----------------------------------------------------------------------------
186 MYSQL_STORE::~MYSQL_STORE()
189 //-----------------------------------------------------------------------------
190 void MYSQL_STORE::SetSettings(const MODULE_SETTINGS & s)
194 //-----------------------------------------------------------------------------
195 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
199 if( (ret = mysql_query(sock,sQuery)) )
201 for(i=0; i<RepitTimes; i++)
203 if( (ret = mysql_query(sock,sQuery)) )
204 ;//need to send error result
212 //-----------------------------------------------------------------------------
214 //-----------------------------------------------------------------------------
215 int MYSQL_STORE::ParseSettings()
217 int ret = storeSettings.ParseSettings(settings);
222 errorStr = storeSettings.GetStrError();
225 if(storeSettings.GetDBPassword().length() == 0)
227 errorStr = "Database password must be not empty. Please read Manual.";
231 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
232 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
235 errorStr = "Couldn't connect to mysql engine! With error:\n";
236 errorStr += mysql_error(&mysql);
242 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
244 string res = "CREATE DATABASE " + storeSettings.GetDBName();
246 if(MysqlQuery(res.c_str(),sock))
248 errorStr = "Couldn't create database! With error:\n";
249 errorStr += mysql_error(sock);
255 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
257 errorStr = "Couldn't select database! With error:\n";
258 errorStr += mysql_error(sock);
262 ret = CheckAllTables(sock);
266 ret = CheckAllTables(sock);
273 //-----------------------------------------------------------------------------
274 const string & MYSQL_STORE::GetStrError() const
278 //-----------------------------------------------------------------------------
279 const string & MYSQL_STORE::GetVersion() const
283 //-----------------------------------------------------------------------------
284 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
288 if (!(result=mysql_list_tables(sock,str.c_str() )))
290 errorStr = "Couldn't get tables list With error:\n";
291 errorStr += mysql_error(sock);
296 unsigned int num_rows = mysql_num_rows(result);
299 mysql_free_result(result);
301 return (num_rows == 1);
303 //-----------------------------------------------------------------------------
304 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
306 //admins-----------------------------------------------------------------------
307 if(!IsTablePresent("admins",sock))
309 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
310 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
311 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
312 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
313 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
315 if(MysqlQuery(qbuf,sock))
317 errorStr = "Couldn't create admin table list With error:\n";
318 errorStr += mysql_error(sock);
323 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
324 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
325 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
327 if(MysqlQuery(qbuf,sock))
329 errorStr = "Couldn't create default admin. With error:\n";
330 errorStr += mysql_error(sock);
336 //tariffs-----------------------------------------------------------------------
338 if(!IsTablePresent("tariffs",sock))
340 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
342 for (int i = 0; i < DIR_NUM; i++)
344 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
347 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
350 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
353 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
356 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
359 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
362 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
365 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
369 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
370 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
372 if(MysqlQuery(res.c_str(),sock))
374 errorStr = "Couldn't create tariffs table list With error:\n";
375 errorStr += mysql_error(sock);
380 res = "INSERT INTO tariffs SET name='tariff',";
382 for (int i = 0; i < DIR_NUM; i++)
384 strprintf(¶m, " NoDiscount%d=1,", i);
387 strprintf(¶m, " Threshold%d=0,", i);
390 strprintf(¶m, " Time%d='0:0-0:0',", i);
395 strprintf(¶m, " SinglePrice%d=0,", i);
401 strprintf(¶m, " PriceDayA%d=0.0,", i);
406 strprintf(¶m, " PriceDayB%d=0.0,", i);
412 strprintf(¶m, " PriceNightA%d=0.0,", i);
417 strprintf(¶m, " PriceNightB%d=0.0,", i);
422 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
423 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
424 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
426 if(MysqlQuery(res.c_str(),sock))
428 errorStr = "Couldn't create default tariff. With error:\n";
429 errorStr += mysql_error(sock);
435 //users-----------------------------------------------------------------------
436 if(!IsTablePresent("users",sock))
438 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
439 "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 '',"\
440 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
441 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
442 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
444 for (int i = 0; i < USERDATA_NUM; i++)
446 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
450 param = " CreditExpire INT(11) DEFAULT 0,";
453 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
456 for (int i = 0; i < DIR_NUM; i++)
458 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
461 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
465 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
466 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
467 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
468 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
471 if(MysqlQuery(res.c_str(),sock))
473 errorStr = "Couldn't create users table list With error:\n";
474 errorStr += mysql_error(sock);
475 errorStr += "\n\n" + res;
480 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
481 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
482 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
483 "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
486 for (int i = 0; i < DIR_NUM; i++)
488 strprintf(¶m, " D%d=0,", i);
491 strprintf(¶m, " U%d=0,", i);
495 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
496 "LastCashAddTime=0, PassiveTime=0";
498 if(MysqlQuery(res.c_str(),sock))
500 errorStr = "Couldn't create default user. With error:\n";
501 errorStr += mysql_error(sock);
507 //logs-----------------------------------------------------------------------
508 if(!IsTablePresent("logs"))
510 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
514 errorStr = "Couldn't create admin table list With error:\n";
515 errorStr += mysql_error(sock);
520 //messages---------------------------------------------------------------------
521 if(!IsTablePresent("messages",sock))
523 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
524 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
525 "stgRepeat INT, repeatPeriod INT, text TEXT)");
527 if(MysqlQuery(qbuf,sock))
529 errorStr = "Couldn't create messages table. With error:\n";
530 errorStr += mysql_error(sock);
536 //month_stat-------------------------------------------------------------------
537 if(!IsTablePresent("stat",sock))
539 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
541 for (int i = 0; i < DIR_NUM; i++)
543 strprintf(¶m, " U%d BIGINT,", i);
546 strprintf(¶m, " D%d BIGINT,", i);
550 res += " cash DOUBLE, INDEX (login))";
552 if(MysqlQuery(res.c_str(),sock))
554 errorStr = "Couldn't create stat table. With error:\n";
555 errorStr += mysql_error(sock);
563 //-----------------------------------------------------------------------------
564 //-----------------------------------------------------------------------------
566 int MYSQL_STORE::GetAllParams(vector<string> * ParamList,
567 const string & table, const string & name) const
576 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
578 if(MysqlGetQuery(qbuf,sock))
580 errorStr = "Couldn't GetAllParams Query for: ";
581 errorStr += name + " - " + table + "\n";
582 errorStr += mysql_error(sock);
587 if (!(res=mysql_store_result(sock)))
589 errorStr = "Couldn't GetAllParams Results for: ";
590 errorStr += name + " - " + table + "\n";
591 errorStr += mysql_error(sock);
595 num = mysql_num_rows(res);
599 row = mysql_fetch_row(res);
600 ParamList->push_back(row[0]);
603 mysql_free_result(res);
609 //-----------------------------------------------------------------------------
610 int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
612 if(GetAllParams(usersList, "users", "login"))
617 //-----------------------------------------------------------------------------
618 int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
620 if(GetAllParams(adminsList, "admins", "login"))
625 //-----------------------------------------------------------------------------
626 int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
628 if(GetAllParams(tariffsList, "tariffs", "name"))
633 //-----------------------------------------------------------------------------
634 int MYSQL_STORE::AddUser(const string & login) const
636 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
638 if(MysqlSetQuery(qbuf))
640 errorStr = "Couldn't add user:\n";
641 //errorStr += mysql_error(sock);
647 //-----------------------------------------------------------------------------
648 int MYSQL_STORE::DelUser(const string & login) const
650 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
652 if(MysqlSetQuery(qbuf))
654 errorStr = "Couldn't delete user:\n";
655 //errorStr += mysql_error(sock);
661 //-----------------------------------------------------------------------------
662 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
669 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
670 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
671 RealName, StgGroup, Credit, TariffChange, ";
673 for (int i = 0; i < USERDATA_NUM; i++)
675 sprintf(qbuf, "Userdata%d, ", i);
679 query += "CreditExpire, IP FROM users WHERE login='";
680 query += login + "' LIMIT 1";
682 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
684 if(MysqlGetQuery(query.c_str(),sock))
686 errorStr = "Couldn't restore Tariff(on query):\n";
687 errorStr += mysql_error(sock);
692 if (!(res=mysql_store_result(sock)))
694 errorStr = "Couldn't restore Tariff(on getting result):\n";
695 errorStr += mysql_error(sock);
700 if (mysql_num_rows(res) != 1)
702 errorStr = "User not found";
707 row = mysql_fetch_row(res);
711 conf->password = row[1];
713 if (conf->password.empty())
715 mysql_free_result(res);
716 errorStr = "User \'" + login + "\' password is blank.";
721 if (GetInt(row[2],&conf->passive, 0) != 0)
723 mysql_free_result(res);
724 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
729 if (GetInt(row[3], &conf->disabled, 0) != 0)
731 mysql_free_result(res);
732 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
737 if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
739 mysql_free_result(res);
740 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
745 if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
747 mysql_free_result(res);
748 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
753 conf->tariffName = row[6];
755 if (conf->tariffName.empty())
757 mysql_free_result(res);
758 errorStr = "User \'" + login + "\' tariff is blank.";
763 conf->address = row[7];
764 conf->phone = row[8];
765 conf->email = row[9];
766 conf->note = row[10];
767 conf->realName = row[11];
768 conf->group = row[12];
770 if (GetDouble(row[13], &conf->credit, 0) != 0)
772 mysql_free_result(res);
773 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
778 conf->nextTariff = row[14];
780 for (int i = 0; i < USERDATA_NUM; i++)
782 conf->userdata[i] = row[15+i];
785 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
787 string ipStr = row[16+USERDATA_NUM];
793 catch (const string & s)
795 mysql_free_result(res);
796 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
802 mysql_free_result(res);
807 //-----------------------------------------------------------------------------
808 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
818 for (int i = 0; i < DIR_NUM; i++)
820 sprintf(qbuf, "D%d, U%d, ", i, i);
824 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
825 FROM users WHERE login = '";
826 query += login + "'";
828 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
830 if(MysqlGetQuery(query.c_str() ,sock))
832 errorStr = "Couldn't restore UserStat(on query):\n";
833 errorStr += mysql_error(sock);
838 if (!(res=mysql_store_result(sock)))
840 errorStr = "Couldn't restore UserStat(on getting result):\n";
841 errorStr += mysql_error(sock);
846 row = mysql_fetch_row(res);
848 unsigned int startPos=0;
852 for (int i = 0; i < DIR_NUM; i++)
855 sprintf(s, "D%d", i);
856 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
858 mysql_free_result(res);
859 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
863 stat->down[i] = traff;
865 sprintf(s, "U%d", i);
866 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
868 mysql_free_result(res);
869 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
876 startPos += (2*DIR_NUM);
878 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
880 mysql_free_result(res);
881 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
886 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
888 mysql_free_result(res);
889 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
894 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
896 mysql_free_result(res);
897 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
902 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
904 mysql_free_result(res);
905 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
910 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
912 mysql_free_result(res);
913 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
918 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
920 mysql_free_result(res);
921 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
926 mysql_free_result(res);
930 //-----------------------------------------------------------------------------
931 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
936 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
937 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
938 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
939 conf.password.c_str(),
942 conf.disabledDetailStat,
944 conf.tariffName.c_str(),
945 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
946 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
947 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
948 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
949 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
950 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
952 conf.nextTariff.c_str()
955 for (int i = 0; i < USERDATA_NUM; i++)
957 strprintf(¶m, " Userdata%d='%s',", i,
958 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
962 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
968 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
971 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
974 if(MysqlSetQuery(res.c_str()))
976 errorStr = "Couldn't save user conf:\n";
977 //errorStr += mysql_error(sock);
983 //-----------------------------------------------------------------------------
984 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
989 res = "UPDATE users SET";
991 for (int i = 0; i < DIR_NUM; i++)
993 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
996 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1000 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1001 " PassiveTime=%d, LastActivityTime=%d",
1005 stat.lastCashAddTime,
1007 stat.lastActivityTime
1011 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1014 if(MysqlSetQuery(res.c_str()))
1016 errorStr = "Couldn't save user stat:\n";
1017 // errorStr += mysql_error(sock);
1023 //-----------------------------------------------------------------------------
1024 int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
1026 string res, tempStr;
1035 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1036 if (!(sock=MysqlConnect())){
1037 errorStr = "Couldn't connect to Server";
1040 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1042 errorStr = "Couldn't get table " + tempStr + ":\n";
1043 errorStr += mysql_error(sock);
1048 unsigned int num_rows = mysql_num_rows(result);
1050 mysql_free_result(result);
1054 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1055 lt->tm_mon+1, lt->tm_year+1900);
1057 if(MysqlQuery(qbuf,sock))
1059 errorStr = "Couldn't create WriteDetailedStat table:\n";
1060 errorStr += mysql_error(sock);
1066 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1070 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1071 lt->tm_mon+1, lt->tm_year+1900,
1072 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1074 if(MysqlQuery(send.c_str(),sock))
1076 errorStr = "Couldn't write log string:\n";
1077 errorStr += mysql_error(sock);
1085 //-----------------------------------------------------------------------------
1086 int MYSQL_STORE::WriteUserChgLog(const string & login,
1087 const string & admLogin,
1089 const string & paramName,
1090 const string & oldValue,
1091 const string & newValue,
1092 const string & message) const
1094 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1095 + paramName + "\' parameter changed from \'" + oldValue +
1096 "\' to \'" + newValue + "\'. " + message;
1098 return WriteLogString(userLogMsg, login);
1100 //-----------------------------------------------------------------------------
1101 int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1103 string logStr = "Connect, " + inet_ntostring(ip);
1104 return WriteLogString(logStr, login);
1106 //-----------------------------------------------------------------------------
1107 int MYSQL_STORE::WriteUserDisconnect(const string & login,
1108 const DIR_TRAFF & up,
1109 const DIR_TRAFF & down,
1110 const DIR_TRAFF & sessionUp,
1111 const DIR_TRAFF & sessionDown,
1114 const std::string & reason) const
1116 string logStr = "Disconnect, ";
1121 stringstream sscash;
1127 sssd << sessionDown;
1131 logStr += " session upload: \'";
1132 logStr += sssu.str();
1133 logStr += "\' session download: \'";
1134 logStr += sssd.str();
1135 logStr += "\' month upload: \'";
1136 logStr += ssmu.str();
1137 logStr += "\' month download: \'";
1138 logStr += ssmd.str();
1139 logStr += "\' cash: \'";
1140 logStr += sscash.str();
1143 return WriteLogString(logStr, login);
1145 //-----------------------------------------------------------------------------
1146 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1147 const string & login) const
1151 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1152 login.c_str(), month+1, year+1900);
1154 for (int i = 0; i < DIR_NUM; i++)
1156 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1159 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1163 strprintf(¶m, " cash=%f", stat.cash);
1166 if(MysqlSetQuery(res.c_str()))
1168 errorStr = "Couldn't SaveMonthStat:\n";
1169 //errorStr += mysql_error(sock);
1175 //-----------------------------------------------------------------------------*/
1176 int MYSQL_STORE::AddAdmin(const string & login) const
1178 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1180 if(MysqlSetQuery(qbuf))
1182 errorStr = "Couldn't add admin:\n";
1183 //errorStr += mysql_error(sock);
1189 //-----------------------------------------------------------------------------*/
1190 int MYSQL_STORE::DelAdmin(const string & login) const
1192 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1194 if(MysqlSetQuery(qbuf))
1196 errorStr = "Couldn't delete admin:\n";
1197 //errorStr += mysql_error(sock);
1203 //-----------------------------------------------------------------------------*/
1204 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1206 char passwordE[2 * ADM_PASSWD_LEN + 2];
1207 char pass[ADM_PASSWD_LEN + 1];
1208 char adminPass[ADM_PASSWD_LEN + 1];
1210 memset(pass, 0, sizeof(pass));
1211 memset(adminPass, 0, sizeof(adminPass));
1214 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1216 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1217 adminPass[ADM_PASSWD_LEN - 1] = 0;
1219 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1221 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1224 pass[ADM_PASSWD_LEN - 1] = 0;
1225 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1227 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1228 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1229 "WHERE login='%s' LIMIT 1",
1241 if(MysqlSetQuery(qbuf))
1243 errorStr = "Couldn't save admin:\n";
1244 //errorStr += mysql_error(sock);
1250 //-----------------------------------------------------------------------------
1251 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1253 char pass[ADM_PASSWD_LEN + 1];
1254 char password[ADM_PASSWD_LEN + 1];
1255 char passwordE[2*ADM_PASSWD_LEN + 2];
1258 memset(pass, 0, sizeof(pass));
1259 memset(password, 0, sizeof(password));
1260 memset(passwordE, 0, sizeof(passwordE));
1266 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1268 if(MysqlGetQuery(qbuf,sock))
1270 errorStr = "Couldn't restore admin:\n";
1271 errorStr += mysql_error(sock);
1276 if (!(res=mysql_store_result(sock)))
1278 errorStr = "Couldn't restore admin:\n";
1279 errorStr += mysql_error(sock);
1284 if ( mysql_num_rows(res) == 0)
1286 mysql_free_result(res);
1287 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1292 row = mysql_fetch_row(res);
1299 mysql_free_result(res);
1300 errorStr = "Error in parameter password";
1305 memset(passwordE, 0, sizeof(passwordE));
1306 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1308 memset(pass, 0, sizeof(pass));
1310 if (passwordE[0] != 0)
1312 Decode21(pass, passwordE);
1313 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1315 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1317 DecodeString(password + 8*i, pass + 8*i, &ctx);
1325 ac->password = password;
1327 if (GetInt(row[2], &a, 0) == 0)
1328 ac->priv.userConf = a;
1331 mysql_free_result(res);
1332 errorStr = "Error in parameter ChgConf";
1337 if (GetInt(row[3], &a, 0) == 0)
1338 ac->priv.userPasswd = a;
1341 mysql_free_result(res);
1342 errorStr = "Error in parameter ChgPassword";
1347 if (GetInt(row[4], &a, 0) == 0)
1348 ac->priv.userStat = a;
1351 mysql_free_result(res);
1352 errorStr = "Error in parameter ChgStat";
1357 if (GetInt(row[5], &a, 0) == 0)
1358 ac->priv.userCash = a;
1361 mysql_free_result(res);
1362 errorStr = "Error in parameter ChgCash";
1367 if (GetInt(row[6], &a, 0) == 0)
1368 ac->priv.userAddDel = a;
1371 mysql_free_result(res);
1372 errorStr = "Error in parameter UsrAddDel";
1377 if (GetInt(row[7], &a, 0) == 0)
1378 ac->priv.tariffChg = a;
1381 mysql_free_result(res);
1382 errorStr = "Error in parameter ChgTariff";
1387 if (GetInt(row[8], &a, 0) == 0)
1388 ac->priv.adminChg = a;
1391 mysql_free_result(res);
1392 errorStr = "Error in parameter ChgAdmin";
1397 mysql_free_result(res);
1401 //-----------------------------------------------------------------------------
1402 int MYSQL_STORE::AddTariff(const string & name) const
1404 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1406 if(MysqlSetQuery(qbuf))
1408 errorStr = "Couldn't add tariff:\n";
1409 // errorStr += mysql_error(sock);
1415 //-----------------------------------------------------------------------------
1416 int MYSQL_STORE::DelTariff(const string & name) const
1418 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1420 if(MysqlSetQuery(qbuf))
1422 errorStr = "Couldn't delete tariff: ";
1423 // errorStr += mysql_error(sock);
1429 //-----------------------------------------------------------------------------
1430 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1435 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1437 if(MysqlGetQuery(qbuf,sock))
1439 errorStr = "Couldn't restore Tariff:\n";
1440 errorStr += mysql_error(sock);
1445 if (!(res=mysql_store_result(sock)))
1447 errorStr = "Couldn't restore Tariff:\n";
1448 errorStr += mysql_error(sock);
1454 td->tariffConf.name = tariffName;
1456 row = mysql_fetch_row(res);
1459 for (int i = 0; i<DIR_NUM; i++)
1461 strprintf(¶m, "Time%d", i);
1463 if (str.length() == 0)
1465 mysql_free_result(res);
1466 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1471 ParseTariffTimeStr(str.c_str(),
1472 td->dirPrice[i].hDay,
1473 td->dirPrice[i].mDay,
1474 td->dirPrice[i].hNight,
1475 td->dirPrice[i].mNight);
1477 strprintf(¶m, "PriceDayA%d", i);
1478 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1480 mysql_free_result(res);
1481 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1485 td->dirPrice[i].priceDayA /= (1024*1024);
1487 strprintf(¶m, "PriceDayB%d", i);
1488 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1490 mysql_free_result(res);
1491 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1495 td->dirPrice[i].priceDayB /= (1024*1024);
1497 strprintf(¶m, "PriceNightA%d", i);
1498 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1500 mysql_free_result(res);
1501 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1505 td->dirPrice[i].priceNightA /= (1024*1024);
1507 strprintf(¶m, "PriceNightB%d", i);
1508 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1510 mysql_free_result(res);
1511 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1515 td->dirPrice[i].priceNightB /= (1024*1024);
1517 strprintf(¶m, "Threshold%d", i);
1518 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
1520 mysql_free_result(res);
1521 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1526 strprintf(¶m, "SinglePrice%d", i);
1527 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
1529 mysql_free_result(res);
1530 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1535 strprintf(¶m, "NoDiscount%d", i);
1536 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
1538 mysql_free_result(res);
1539 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1545 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1547 mysql_free_result(res);
1548 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1553 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1555 mysql_free_result(res);
1556 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1561 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1563 mysql_free_result(res);
1564 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1569 str = row[4+8*DIR_NUM];
1570 param = "TraffType";
1572 if (str.length() == 0)
1574 mysql_free_result(res);
1575 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1580 if (!strcasecmp(str.c_str(), "up"))
1581 td->tariffConf.traffType = TRAFF_UP;
1583 if (!strcasecmp(str.c_str(), "down"))
1584 td->tariffConf.traffType = TRAFF_DOWN;
1586 if (!strcasecmp(str.c_str(), "up+down"))
1587 td->tariffConf.traffType = TRAFF_UP_DOWN;
1589 if (!strcasecmp(str.c_str(), "max"))
1590 td->tariffConf.traffType = TRAFF_MAX;
1593 mysql_free_result(res);
1594 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1599 mysql_free_result(res);
1603 //-----------------------------------------------------------------------------
1604 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1608 string res="UPDATE tariffs SET";
1610 for (int i = 0; i < DIR_NUM; i++)
1612 strprintf(¶m, " PriceDayA%d=%f,", i,
1613 td.dirPrice[i].priceDayA * pt_mega);
1616 strprintf(¶m, " PriceDayB%d=%f,", i,
1617 td.dirPrice[i].priceDayB * pt_mega);
1620 strprintf(¶m, " PriceNightA%d=%f,", i,
1621 td.dirPrice[i].priceNightA * pt_mega);
1624 strprintf(¶m, " PriceNightB%d=%f,", i,
1625 td.dirPrice[i].priceNightB * pt_mega);
1628 strprintf(¶m, " Threshold%d=%d,", i,
1629 td.dirPrice[i].threshold);
1633 strprintf(¶m, " Time%d", i);
1635 strprintf(&s, "%0d:%0d-%0d:%0d",
1636 td.dirPrice[i].hDay,
1637 td.dirPrice[i].mDay,
1638 td.dirPrice[i].hNight,
1639 td.dirPrice[i].mNight);
1641 res += (param + "='" + s + "',");
1643 strprintf(¶m, " NoDiscount%d=%d,", i,
1644 td.dirPrice[i].noDiscount);
1647 strprintf(¶m, " SinglePrice%d=%d,", i,
1648 td.dirPrice[i].singlePrice);
1652 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1655 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1658 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1661 switch (td.tariffConf.traffType)
1664 res += " TraffType='up'";
1667 res += " TraffType='down'";
1670 res += " TraffType='up+down'";
1673 res += " TraffType='max'";
1676 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1679 if(MysqlSetQuery(res.c_str()))
1681 errorStr = "Couldn't save admin:\n";
1682 //errorStr += mysql_error(sock);
1688 //-----------------------------------------------------------------------------
1689 int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1691 const string & login) const
1693 string res, stTime, endTime, tempStr;
1700 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1708 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1710 if (!(sock=MysqlConnect())){
1715 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1717 errorStr = "Couldn't get table " + tempStr + ":\n";
1718 errorStr += mysql_error(sock);
1723 unsigned int num_rows = mysql_num_rows(result);
1725 mysql_free_result(result);
1729 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1730 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1731 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1732 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1733 lt->tm_mon+1, lt->tm_year+1900);
1735 if(MysqlQuery(qbuf,sock))
1737 errorStr = "Couldn't create WriteDetailedStat table:\n";
1738 errorStr += mysql_error(sock);
1747 lt1 = localtime(&lastStat);
1756 lt2 = localtime(&t);
1762 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1763 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1765 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1766 "day=%d,startTime='%s',endTime='%s',",
1767 lt->tm_mon+1, lt->tm_year+1900,
1774 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1775 stIter = statTree.begin();
1777 while (stIter != statTree.end())
1779 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1780 inet_ntostring(stIter->first.ip).c_str(),
1782 stIter->second.down,
1787 if( MysqlQuery((res+tempStr).c_str(),sock) )
1789 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1790 errorStr += mysql_error(sock);
1795 result=mysql_store_result(sock);
1797 mysql_free_result(result);
1804 //-----------------------------------------------------------------------------
1805 int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
1809 gettimeofday(&tv, NULL);
1811 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1813 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1815 (long long)msg->header.id
1818 if(MysqlSetQuery(qbuf))
1820 errorStr = "Couldn't add message:\n";
1821 //errorStr += mysql_error(sock);
1825 return EditMessage(*msg, login);
1827 //-----------------------------------------------------------------------------
1828 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1832 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1833 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1834 "WHERE login='%s' AND id=%lld LIMIT 1",
1836 msg.header.lastSendTime,
1837 msg.header.creationTime,
1838 msg.header.showTime,
1840 msg.header.repeatPeriod,
1841 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1843 (long long)msg.header.id
1846 if(MysqlSetQuery(res.c_str()))
1848 errorStr = "Couldn't edit message:\n";
1849 //errorStr += mysql_error(sock);
1855 //-----------------------------------------------------------------------------
1856 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1862 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1863 login.c_str(), static_cast<unsigned long long>(id));
1865 if(MysqlGetQuery(qbuf,sock))
1867 errorStr = "Couldn't GetMessage:\n";
1868 errorStr += mysql_error(sock);
1873 if (!(res=mysql_store_result(sock)))
1875 errorStr = "Couldn't GetMessage:\n";
1876 errorStr += mysql_error(sock);
1881 row = mysql_fetch_row(res);
1883 if(row[2]&&str2x(row[2], msg->header.type))
1885 mysql_free_result(res);
1886 errorStr = "Invalid value in message header for user: " + login;
1891 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1893 mysql_free_result(res);
1894 errorStr = "Invalid value in message header for user: " + login;
1899 if(row[4] && str2x(row[4], msg->header.creationTime))
1901 mysql_free_result(res);
1902 errorStr = "Invalid value in message header for user: " + login;
1907 if(row[5] && str2x(row[5], msg->header.showTime))
1909 mysql_free_result(res);
1910 errorStr = "Invalid value in message header for user: " + login;
1915 if(row[6] && str2x(row[6], msg->header.repeat))
1917 mysql_free_result(res);
1918 errorStr = "Invalid value in message header for user: " + login;
1923 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1925 mysql_free_result(res);
1926 errorStr = "Invalid value in message header for user: " + login;
1931 msg->header.id = id;
1934 mysql_free_result(res);
1938 //-----------------------------------------------------------------------------
1939 int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
1941 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1942 login.c_str(),(long long)id);
1944 if(MysqlSetQuery(qbuf))
1946 errorStr = "Couldn't delete Message:\n";
1947 //errorStr += mysql_error(sock);
1953 //-----------------------------------------------------------------------------
1954 int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1959 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
1961 if(MysqlGetQuery(qbuf,sock))
1963 errorStr = "Couldn't GetMessageHdrs:\n";
1964 errorStr += mysql_error(sock);
1969 if (!(res=mysql_store_result(sock)))
1971 errorStr = "Couldn't GetMessageHdrs:\n";
1972 errorStr += mysql_error(sock);
1977 unsigned int i, num_rows = mysql_num_rows(res);
1978 long long int unsigned id = 0;
1980 for (i=0; i<num_rows; i++)
1982 row = mysql_fetch_row(res);
1983 if (str2x(row[1], id))
1988 if(str2x(row[2], hdr.type))
1992 if(str2x(row[3], hdr.lastSendTime))
1996 if(str2x(row[4], hdr.creationTime))
2000 if(str2x(row[5], hdr.showTime))
2004 if(str2x(row[6], hdr.repeat))
2008 if(str2x(row[7], hdr.repeatPeriod))
2012 hdrsList->push_back(hdr);
2015 mysql_free_result(res);
2019 //-----------------------------------------------------------------------------
2021 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2024 int ret=MysqlGetQuery(Query,sock);
2028 //-----------------------------------------------------------------------------
2029 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2030 if (!(sock=MysqlConnect())) {
2033 return MysqlQuery(Query,sock);
2035 //-----------------------------------------------------------------------------
2036 MYSQL * MYSQL_STORE::MysqlConnect() const {
2038 if ( !(sock=mysql_init(NULL)) ){
2039 errorStr= "mysql init susck\n";
2042 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2043 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2046 errorStr = "Couldn't connect to mysql engine! With error:\n";
2047 errorStr += mysql_error(sock);
2051 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2052 errorStr = "Database lost !\n";
2058 //-----------------------------------------------------------------------------