12 #include "user_conf.h"
13 #include "user_stat.h"
14 #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, "dbuser", dbUser) < 0)
158 if (ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
160 if (ParseParam(s.moduleParams, "dbname", dbName) < 0)
162 if (ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
167 //-----------------------------------------------------------------------------
168 const string & MYSQL_STORE_SETTINGS::GetStrError() const
172 //-----------------------------------------------------------------------------
173 string MYSQL_STORE_SETTINGS::GetDBUser() const
177 //-----------------------------------------------------------------------------
178 string MYSQL_STORE_SETTINGS::GetDBPassword() const
182 //-----------------------------------------------------------------------------
183 string MYSQL_STORE_SETTINGS::GetDBHost() const
187 //-----------------------------------------------------------------------------
188 string MYSQL_STORE_SETTINGS::GetDBName() const
192 //-----------------------------------------------------------------------------
193 //-----------------------------------------------------------------------------
194 //-----------------------------------------------------------------------------
195 MYSQL_STORE::MYSQL_STORE()
197 version = "mysql_store v.0.67";
199 //-----------------------------------------------------------------------------
200 MYSQL_STORE::~MYSQL_STORE()
203 //-----------------------------------------------------------------------------
204 void MYSQL_STORE::SetSettings(const MODULE_SETTINGS & s)
208 //-----------------------------------------------------------------------------
209 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
213 if( (ret = mysql_query(sock,sQuery)) )
215 for(i=0; i<RepitTimes; i++)
217 if( (ret = mysql_query(sock,sQuery)) )
218 ;//need to send error result
226 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
229 int MYSQL_STORE::ParseSettings()
231 int ret = storeSettings.ParseSettings(settings);
236 errorStr = storeSettings.GetStrError();
239 if(storeSettings.GetDBPassword().length() == 0)
241 errorStr = "Database password must be not empty. Please read Manual.";
245 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
246 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
249 errorStr = "Couldn't connect to mysql engine! With error:\n";
250 errorStr += mysql_error(&mysql);
256 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
258 string res = "CREATE DATABASE " + storeSettings.GetDBName();
260 if(MysqlQuery(res.c_str(),sock))
262 errorStr = "Couldn't create database! With error:\n";
263 errorStr += mysql_error(sock);
269 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
271 errorStr = "Couldn't select database! With error:\n";
272 errorStr += mysql_error(sock);
276 ret = CheckAllTables(sock);
280 ret = CheckAllTables(sock);
287 //-----------------------------------------------------------------------------
288 const string & MYSQL_STORE::GetStrError() const
292 //-----------------------------------------------------------------------------
293 const string & MYSQL_STORE::GetVersion() const
297 //-----------------------------------------------------------------------------
298 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
302 if (!(result=mysql_list_tables(sock,str.c_str() )))
304 errorStr = "Couldn't get tables list With error:\n";
305 errorStr += mysql_error(sock);
310 unsigned int num_rows = mysql_num_rows(result);
313 mysql_free_result(result);
315 return (num_rows == 1);
317 //-----------------------------------------------------------------------------
318 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
320 //admins-----------------------------------------------------------------------
321 if(!IsTablePresent("admins",sock))
323 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
324 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
325 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
326 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
327 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
329 if(MysqlQuery(qbuf,sock))
331 errorStr = "Couldn't create admin table list With error:\n";
332 errorStr += mysql_error(sock);
337 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
338 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
339 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
341 if(MysqlQuery(qbuf,sock))
343 errorStr = "Couldn't create default admin. With error:\n";
344 errorStr += mysql_error(sock);
350 //tariffs-----------------------------------------------------------------------
352 if(!IsTablePresent("tariffs",sock))
354 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
356 for (int i = 0; i < DIR_NUM; i++)
358 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
361 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
364 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
367 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
370 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
373 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
376 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
379 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
383 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
384 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
386 if(MysqlQuery(res.c_str(),sock))
388 errorStr = "Couldn't create tariffs table list With error:\n";
389 errorStr += mysql_error(sock);
394 res = "INSERT INTO tariffs SET name='tariff',";
396 for (int i = 0; i < DIR_NUM; i++)
398 strprintf(¶m, " NoDiscount%d=1,", i);
401 strprintf(¶m, " Threshold%d=0,", i);
404 strprintf(¶m, " Time%d='0:0-0:0',", i);
409 strprintf(¶m, " SinglePrice%d=0,", i);
415 strprintf(¶m, " PriceDayA%d=0.0,", i);
420 strprintf(¶m, " PriceDayB%d=0.0,", i);
426 strprintf(¶m, " PriceNightA%d=0.0,", i);
431 strprintf(¶m, " PriceNightB%d=0.0,", i);
436 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
437 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
438 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
440 if(MysqlQuery(res.c_str(),sock))
442 errorStr = "Couldn't create default tariff. With error:\n";
443 errorStr += mysql_error(sock);
449 //users-----------------------------------------------------------------------
450 if(!IsTablePresent("users",sock))
452 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
453 "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 '',"\
454 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
455 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
456 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
458 for (int i = 0; i < USERDATA_NUM; i++)
460 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
464 param = " CreditExpire INT(11) DEFAULT 0,";
467 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
470 for (int i = 0; i < DIR_NUM; i++)
472 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
475 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
479 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
480 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
481 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
482 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
485 if(MysqlQuery(res.c_str(),sock))
487 errorStr = "Couldn't create users table list With error:\n";
488 errorStr += mysql_error(sock);
489 errorStr += "\n\n" + res;
494 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
495 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
496 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
497 "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
500 for (int i = 0; i < DIR_NUM; i++)
502 strprintf(¶m, " D%d=0,", i);
505 strprintf(¶m, " U%d=0,", i);
509 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
510 "LastCashAddTime=0, PassiveTime=0";
512 if(MysqlQuery(res.c_str(),sock))
514 errorStr = "Couldn't create default user. With error:\n";
515 errorStr += mysql_error(sock);
521 //logs-----------------------------------------------------------------------
522 if(!IsTablePresent("logs"))
524 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
528 errorStr = "Couldn't create admin table list With error:\n";
529 errorStr += mysql_error(sock);
534 //messages---------------------------------------------------------------------
535 if(!IsTablePresent("messages",sock))
537 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
538 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
539 "stgRepeat INT, repeatPeriod INT, text TEXT)");
541 if(MysqlQuery(qbuf,sock))
543 errorStr = "Couldn't create messages table. With error:\n";
544 errorStr += mysql_error(sock);
550 //month_stat-------------------------------------------------------------------
551 if(!IsTablePresent("stat",sock))
553 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
555 for (int i = 0; i < DIR_NUM; i++)
557 strprintf(¶m, " U%d BIGINT,", i);
560 strprintf(¶m, " D%d BIGINT,", i);
564 res += " cash DOUBLE, INDEX (login))";
566 if(MysqlQuery(res.c_str(),sock))
568 errorStr = "Couldn't create stat table. With error:\n";
569 errorStr += mysql_error(sock);
577 //-----------------------------------------------------------------------------
578 //-----------------------------------------------------------------------------
580 int MYSQL_STORE::GetAllParams(vector<string> * ParamList,
581 const string & table, const string & name) const
590 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
592 if(MysqlGetQuery(qbuf,sock))
594 errorStr = "Couldn't GetAllParams Query for: ";
595 errorStr += name + " - " + table + "\n";
596 errorStr += mysql_error(sock);
601 if (!(res=mysql_store_result(sock)))
603 errorStr = "Couldn't GetAllParams Results for: ";
604 errorStr += name + " - " + table + "\n";
605 errorStr += mysql_error(sock);
609 num = mysql_num_rows(res);
613 row = mysql_fetch_row(res);
614 ParamList->push_back(row[0]);
617 mysql_free_result(res);
623 //-----------------------------------------------------------------------------
624 int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
626 if(GetAllParams(usersList, "users", "login"))
631 //-----------------------------------------------------------------------------
632 int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
634 if(GetAllParams(adminsList, "admins", "login"))
639 //-----------------------------------------------------------------------------
640 int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
642 if(GetAllParams(tariffsList, "tariffs", "name"))
647 //-----------------------------------------------------------------------------
648 int MYSQL_STORE::AddUser(const string & login) const
650 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
652 if(MysqlSetQuery(qbuf))
654 errorStr = "Couldn't add user:\n";
655 //errorStr += mysql_error(sock);
661 //-----------------------------------------------------------------------------
662 int MYSQL_STORE::DelUser(const string & login) const
664 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
666 if(MysqlSetQuery(qbuf))
668 errorStr = "Couldn't delete user:\n";
669 //errorStr += mysql_error(sock);
675 //-----------------------------------------------------------------------------
676 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
683 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
684 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
685 RealName, StgGroup, Credit, TariffChange, ";
687 for (int i = 0; i < USERDATA_NUM; i++)
689 sprintf(qbuf, "Userdata%d, ", i);
693 query += "CreditExpire, IP FROM users WHERE login='";
694 query += login + "' LIMIT 1";
696 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
698 if(MysqlGetQuery(query.c_str(),sock))
700 errorStr = "Couldn't restore Tariff(on query):\n";
701 errorStr += mysql_error(sock);
706 if (!(res=mysql_store_result(sock)))
708 errorStr = "Couldn't restore Tariff(on getting result):\n";
709 errorStr += mysql_error(sock);
714 if (mysql_num_rows(res) != 1)
716 errorStr = "User not found";
721 row = mysql_fetch_row(res);
725 conf->password = row[1];
727 if (conf->password.empty())
729 mysql_free_result(res);
730 errorStr = "User \'" + login + "\' password is blank.";
735 if (GetInt(row[2],&conf->passive, 0) != 0)
737 mysql_free_result(res);
738 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
743 if (GetInt(row[3], &conf->disabled, 0) != 0)
745 mysql_free_result(res);
746 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
751 if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
753 mysql_free_result(res);
754 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
759 if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
761 mysql_free_result(res);
762 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
767 conf->tariffName = row[6];
769 if (conf->tariffName.empty())
771 mysql_free_result(res);
772 errorStr = "User \'" + login + "\' tariff is blank.";
777 conf->address = row[7];
778 conf->phone = row[8];
779 conf->email = row[9];
780 conf->note = row[10];
781 conf->realName = row[11];
782 conf->group = row[12];
784 if (GetDouble(row[13], &conf->credit, 0) != 0)
786 mysql_free_result(res);
787 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
792 conf->nextTariff = row[14];
794 for (int i = 0; i < USERDATA_NUM; i++)
796 conf->userdata[i] = row[15+i];
799 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
801 string ipStr = row[16+USERDATA_NUM];
809 mysql_free_result(res);
810 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
816 mysql_free_result(res);
821 //-----------------------------------------------------------------------------
822 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
832 for (int i = 0; i < DIR_NUM; i++)
834 sprintf(qbuf, "D%d, U%d, ", i, i);
838 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
839 FROM users WHERE login = '";
840 query += login + "'";
842 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
844 if(MysqlGetQuery(query.c_str() ,sock))
846 errorStr = "Couldn't restore UserStat(on query):\n";
847 errorStr += mysql_error(sock);
852 if (!(res=mysql_store_result(sock)))
854 errorStr = "Couldn't restore UserStat(on getting result):\n";
855 errorStr += mysql_error(sock);
860 row = mysql_fetch_row(res);
862 unsigned int startPos=0;
866 for (int i = 0; i < DIR_NUM; i++)
869 sprintf(s, "D%d", i);
870 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
872 mysql_free_result(res);
873 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
877 stat->down[i] = traff;
879 sprintf(s, "U%d", i);
880 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
882 mysql_free_result(res);
883 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
890 startPos += (2*DIR_NUM);
892 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
894 mysql_free_result(res);
895 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
900 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
902 mysql_free_result(res);
903 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
908 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
910 mysql_free_result(res);
911 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
916 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
918 mysql_free_result(res);
919 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
924 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
926 mysql_free_result(res);
927 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
932 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
934 mysql_free_result(res);
935 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
940 mysql_free_result(res);
944 //-----------------------------------------------------------------------------
945 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
950 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
951 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
952 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
953 conf.password.c_str(),
956 conf.disabledDetailStat,
958 conf.tariffName.c_str(),
959 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
960 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
961 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
962 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
963 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
964 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
966 conf.nextTariff.c_str()
969 for (int i = 0; i < USERDATA_NUM; i++)
971 strprintf(¶m, " Userdata%d='%s',", i,
972 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
976 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
982 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
985 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
988 if(MysqlSetQuery(res.c_str()))
990 errorStr = "Couldn't save user conf:\n";
991 //errorStr += mysql_error(sock);
997 //-----------------------------------------------------------------------------
998 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
1003 res = "UPDATE users SET";
1005 for (int i = 0; i < DIR_NUM; i++)
1007 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1010 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1014 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1015 " PassiveTime=%d, LastActivityTime=%d",
1019 stat.lastCashAddTime,
1021 stat.lastActivityTime
1025 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1028 if(MysqlSetQuery(res.c_str()))
1030 errorStr = "Couldn't save user stat:\n";
1031 // errorStr += mysql_error(sock);
1037 //-----------------------------------------------------------------------------
1038 int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
1040 string res, tempStr;
1049 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1050 if (!(sock=MysqlConnect())){
1051 errorStr = "Couldn't connect to Server";
1054 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1056 errorStr = "Couldn't get table " + tempStr + ":\n";
1057 errorStr += mysql_error(sock);
1062 unsigned int num_rows = mysql_num_rows(result);
1064 mysql_free_result(result);
1068 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1069 lt->tm_mon+1, lt->tm_year+1900);
1071 if(MysqlQuery(qbuf,sock))
1073 errorStr = "Couldn't create WriteDetailedStat table:\n";
1074 errorStr += mysql_error(sock);
1080 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1084 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1085 lt->tm_mon+1, lt->tm_year+1900,
1086 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1088 if(MysqlQuery(send.c_str(),sock))
1090 errorStr = "Couldn't write log string:\n";
1091 errorStr += mysql_error(sock);
1099 //-----------------------------------------------------------------------------
1100 int MYSQL_STORE::WriteUserChgLog(const string & login,
1101 const string & admLogin,
1103 const string & paramName,
1104 const string & oldValue,
1105 const string & newValue,
1106 const string & message) const
1108 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1109 + paramName + "\' parameter changed from \'" + oldValue +
1110 "\' to \'" + newValue + "\'. " + message;
1112 return WriteLogString(userLogMsg, login);
1114 //-----------------------------------------------------------------------------
1115 int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1117 string logStr = "Connect, " + inet_ntostring(ip);
1118 return WriteLogString(logStr, login);
1120 //-----------------------------------------------------------------------------
1121 int MYSQL_STORE::WriteUserDisconnect(const string & login,
1122 const DIR_TRAFF & up,
1123 const DIR_TRAFF & down,
1124 const DIR_TRAFF & sessionUp,
1125 const DIR_TRAFF & sessionDown,
1128 const std::string & reason) const
1130 string logStr = "Disconnect, ";
1135 stringstream sscash;
1141 sssd << sessionDown;
1145 logStr += " session upload: \'";
1146 logStr += sssu.str();
1147 logStr += "\' session download: \'";
1148 logStr += sssd.str();
1149 logStr += "\' month upload: \'";
1150 logStr += ssmu.str();
1151 logStr += "\' month download: \'";
1152 logStr += ssmd.str();
1153 logStr += "\' cash: \'";
1154 logStr += sscash.str();
1157 return WriteLogString(logStr, login);
1159 //-----------------------------------------------------------------------------
1160 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1161 const string & login) const
1165 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1166 login.c_str(), month+1, year+1900);
1168 for (int i = 0; i < DIR_NUM; i++)
1170 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1173 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1177 strprintf(¶m, " cash=%f", stat.cash);
1180 if(MysqlSetQuery(res.c_str()))
1182 errorStr = "Couldn't SaveMonthStat:\n";
1183 //errorStr += mysql_error(sock);
1189 //-----------------------------------------------------------------------------*/
1190 int MYSQL_STORE::AddAdmin(const string & login) const
1192 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1194 if(MysqlSetQuery(qbuf))
1196 errorStr = "Couldn't add admin:\n";
1197 //errorStr += mysql_error(sock);
1203 //-----------------------------------------------------------------------------*/
1204 int MYSQL_STORE::DelAdmin(const string & login) const
1206 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1208 if(MysqlSetQuery(qbuf))
1210 errorStr = "Couldn't delete admin:\n";
1211 //errorStr += mysql_error(sock);
1217 //-----------------------------------------------------------------------------*/
1218 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1220 char passwordE[2 * ADM_PASSWD_LEN + 2];
1221 char pass[ADM_PASSWD_LEN + 1];
1222 char adminPass[ADM_PASSWD_LEN + 1];
1224 memset(pass, 0, sizeof(pass));
1225 memset(adminPass, 0, sizeof(adminPass));
1228 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1230 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1231 adminPass[ADM_PASSWD_LEN - 1] = 0;
1233 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1235 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1238 pass[ADM_PASSWD_LEN - 1] = 0;
1239 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1241 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1242 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1243 "WHERE login='%s' LIMIT 1",
1255 if(MysqlSetQuery(qbuf))
1257 errorStr = "Couldn't save admin:\n";
1258 //errorStr += mysql_error(sock);
1264 //-----------------------------------------------------------------------------
1265 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1267 char pass[ADM_PASSWD_LEN + 1];
1268 char password[ADM_PASSWD_LEN + 1];
1269 char passwordE[2*ADM_PASSWD_LEN + 2];
1276 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1278 if(MysqlGetQuery(qbuf,sock))
1280 errorStr = "Couldn't restore admin:\n";
1281 errorStr += mysql_error(sock);
1286 if (!(res=mysql_store_result(sock)))
1288 errorStr = "Couldn't restore admin:\n";
1289 errorStr += mysql_error(sock);
1294 if ( mysql_num_rows(res) == 0)
1296 mysql_free_result(res);
1297 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1302 row = mysql_fetch_row(res);
1309 mysql_free_result(res);
1310 errorStr = "Error in parameter password";
1315 memset(passwordE, 0, sizeof(passwordE));
1316 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1318 memset(pass, 0, sizeof(pass));
1320 if (passwordE[0] != 0)
1322 Decode21(pass, passwordE);
1323 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1325 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1327 DecodeString(password + 8*i, pass + 8*i, &ctx);
1335 ac->password = password;
1337 if (GetInt(row[2], &a, 0) == 0)
1338 ac->priv.userConf = a;
1341 mysql_free_result(res);
1342 errorStr = "Error in parameter ChgConf";
1347 if (GetInt(row[3], &a, 0) == 0)
1348 ac->priv.userPasswd = a;
1351 mysql_free_result(res);
1352 errorStr = "Error in parameter ChgPassword";
1357 if (GetInt(row[4], &a, 0) == 0)
1358 ac->priv.userStat = a;
1361 mysql_free_result(res);
1362 errorStr = "Error in parameter ChgStat";
1367 if (GetInt(row[5], &a, 0) == 0)
1368 ac->priv.userCash = a;
1371 mysql_free_result(res);
1372 errorStr = "Error in parameter ChgCash";
1377 if (GetInt(row[6], &a, 0) == 0)
1378 ac->priv.userAddDel = a;
1381 mysql_free_result(res);
1382 errorStr = "Error in parameter UsrAddDel";
1387 if (GetInt(row[7], &a, 0) == 0)
1388 ac->priv.tariffChg = a;
1391 mysql_free_result(res);
1392 errorStr = "Error in parameter ChgTariff";
1397 if (GetInt(row[8], &a, 0) == 0)
1398 ac->priv.adminChg = a;
1401 mysql_free_result(res);
1402 errorStr = "Error in parameter ChgAdmin";
1407 mysql_free_result(res);
1411 //-----------------------------------------------------------------------------
1412 int MYSQL_STORE::AddTariff(const string & name) const
1414 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1416 if(MysqlSetQuery(qbuf))
1418 errorStr = "Couldn't add tariff:\n";
1419 // errorStr += mysql_error(sock);
1425 //-----------------------------------------------------------------------------
1426 int MYSQL_STORE::DelTariff(const string & name) const
1428 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1430 if(MysqlSetQuery(qbuf))
1432 errorStr = "Couldn't delete tariff: ";
1433 // errorStr += mysql_error(sock);
1439 //-----------------------------------------------------------------------------
1440 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1445 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1447 if(MysqlGetQuery(qbuf,sock))
1449 errorStr = "Couldn't restore Tariff:\n";
1450 errorStr += mysql_error(sock);
1455 if (!(res=mysql_store_result(sock)))
1457 errorStr = "Couldn't restore Tariff:\n";
1458 errorStr += mysql_error(sock);
1464 td->tariffConf.name = tariffName;
1466 row = mysql_fetch_row(res);
1469 for (int i = 0; i<DIR_NUM; i++)
1471 strprintf(¶m, "Time%d", i);
1473 if (str.length() == 0)
1475 mysql_free_result(res);
1476 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1481 ParseTariffTimeStr(str.c_str(),
1482 td->dirPrice[i].hDay,
1483 td->dirPrice[i].mDay,
1484 td->dirPrice[i].hNight,
1485 td->dirPrice[i].mNight);
1487 strprintf(¶m, "PriceDayA%d", i);
1488 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1490 mysql_free_result(res);
1491 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1495 td->dirPrice[i].priceDayA /= (1024*1024);
1497 strprintf(¶m, "PriceDayB%d", i);
1498 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1500 mysql_free_result(res);
1501 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1505 td->dirPrice[i].priceDayB /= (1024*1024);
1507 strprintf(¶m, "PriceNightA%d", i);
1508 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1510 mysql_free_result(res);
1511 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1515 td->dirPrice[i].priceNightA /= (1024*1024);
1517 strprintf(¶m, "PriceNightB%d", i);
1518 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1520 mysql_free_result(res);
1521 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1525 td->dirPrice[i].priceNightB /= (1024*1024);
1527 strprintf(¶m, "Threshold%d", i);
1528 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
1530 mysql_free_result(res);
1531 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1536 strprintf(¶m, "SinglePrice%d", i);
1537 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
1539 mysql_free_result(res);
1540 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1545 strprintf(¶m, "NoDiscount%d", i);
1546 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
1548 mysql_free_result(res);
1549 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1555 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1557 mysql_free_result(res);
1558 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1563 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1565 mysql_free_result(res);
1566 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1571 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1573 mysql_free_result(res);
1574 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1579 str = row[4+8*DIR_NUM];
1580 param = "TraffType";
1582 if (str.length() == 0)
1584 mysql_free_result(res);
1585 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1590 if (!strcasecmp(str.c_str(), "up"))
1591 td->tariffConf.traffType = TRAFF_UP;
1593 if (!strcasecmp(str.c_str(), "down"))
1594 td->tariffConf.traffType = TRAFF_DOWN;
1596 if (!strcasecmp(str.c_str(), "up+down"))
1597 td->tariffConf.traffType = TRAFF_UP_DOWN;
1599 if (!strcasecmp(str.c_str(), "max"))
1600 td->tariffConf.traffType = TRAFF_MAX;
1603 mysql_free_result(res);
1604 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1609 mysql_free_result(res);
1613 //-----------------------------------------------------------------------------
1614 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1618 string res="UPDATE tariffs SET";
1620 for (int i = 0; i < DIR_NUM; i++)
1622 strprintf(¶m, " PriceDayA%d=%f,", i,
1623 td.dirPrice[i].priceDayA * pt_mega);
1626 strprintf(¶m, " PriceDayB%d=%f,", i,
1627 td.dirPrice[i].priceDayB * pt_mega);
1630 strprintf(¶m, " PriceNightA%d=%f,", i,
1631 td.dirPrice[i].priceNightA * pt_mega);
1634 strprintf(¶m, " PriceNightB%d=%f,", i,
1635 td.dirPrice[i].priceNightB * pt_mega);
1638 strprintf(¶m, " Threshold%d=%d,", i,
1639 td.dirPrice[i].threshold);
1643 strprintf(¶m, " Time%d", i);
1645 strprintf(&s, "%0d:%0d-%0d:%0d",
1646 td.dirPrice[i].hDay,
1647 td.dirPrice[i].mDay,
1648 td.dirPrice[i].hNight,
1649 td.dirPrice[i].mNight);
1651 res += (param + "='" + s + "',");
1653 strprintf(¶m, " NoDiscount%d=%d,", i,
1654 td.dirPrice[i].noDiscount);
1657 strprintf(¶m, " SinglePrice%d=%d,", i,
1658 td.dirPrice[i].singlePrice);
1662 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1665 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1668 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1671 switch (td.tariffConf.traffType)
1674 res += " TraffType='up'";
1677 res += " TraffType='down'";
1680 res += " TraffType='up+down'";
1683 res += " TraffType='max'";
1686 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1689 if(MysqlSetQuery(res.c_str()))
1691 errorStr = "Couldn't save admin:\n";
1692 //errorStr += mysql_error(sock);
1698 //-----------------------------------------------------------------------------
1699 int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1701 const string & login) const
1703 string res, stTime, endTime, tempStr;
1710 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1718 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1720 if (!(sock=MysqlConnect())){
1725 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1727 errorStr = "Couldn't get table " + tempStr + ":\n";
1728 errorStr += mysql_error(sock);
1733 unsigned int num_rows = mysql_num_rows(result);
1735 mysql_free_result(result);
1739 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1740 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1741 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1742 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1743 lt->tm_mon+1, lt->tm_year+1900);
1745 if(MysqlQuery(qbuf,sock))
1747 errorStr = "Couldn't create WriteDetailedStat table:\n";
1748 errorStr += mysql_error(sock);
1757 lt1 = localtime(&lastStat);
1766 lt2 = localtime(&t);
1772 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1773 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1775 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1776 "day=%d,startTime='%s',endTime='%s',",
1777 lt->tm_mon+1, lt->tm_year+1900,
1784 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1785 stIter = statTree.begin();
1787 while (stIter != statTree.end())
1789 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1790 inet_ntostring(stIter->first.ip).c_str(),
1792 stIter->second.down,
1797 if( MysqlQuery((res+tempStr).c_str(),sock) )
1799 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1800 errorStr += mysql_error(sock);
1805 result=mysql_store_result(sock);
1807 mysql_free_result(result);
1814 //-----------------------------------------------------------------------------
1815 int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
1819 gettimeofday(&tv, NULL);
1821 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1823 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1825 (long long)msg->header.id
1828 if(MysqlSetQuery(qbuf))
1830 errorStr = "Couldn't add message:\n";
1831 //errorStr += mysql_error(sock);
1835 return EditMessage(*msg, login);
1837 //-----------------------------------------------------------------------------
1838 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1842 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1843 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1844 "WHERE login='%s' AND id=%lld LIMIT 1",
1846 msg.header.lastSendTime,
1847 msg.header.creationTime,
1848 msg.header.showTime,
1850 msg.header.repeatPeriod,
1851 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1853 (long long)msg.header.id
1856 if(MysqlSetQuery(res.c_str()))
1858 errorStr = "Couldn't edit message:\n";
1859 //errorStr += mysql_error(sock);
1865 //-----------------------------------------------------------------------------
1866 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1872 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1875 if(MysqlGetQuery(qbuf,sock))
1877 errorStr = "Couldn't GetMessage:\n";
1878 errorStr += mysql_error(sock);
1883 if (!(res=mysql_store_result(sock)))
1885 errorStr = "Couldn't GetMessage:\n";
1886 errorStr += mysql_error(sock);
1891 row = mysql_fetch_row(res);
1893 if(row[2]&&str2x(row[2], msg->header.type))
1895 mysql_free_result(res);
1896 errorStr = "Invalid value in message header for user: " + login;
1901 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1903 mysql_free_result(res);
1904 errorStr = "Invalid value in message header for user: " + login;
1909 if(row[4] && str2x(row[4], msg->header.creationTime))
1911 mysql_free_result(res);
1912 errorStr = "Invalid value in message header for user: " + login;
1917 if(row[5] && str2x(row[5], msg->header.showTime))
1919 mysql_free_result(res);
1920 errorStr = "Invalid value in message header for user: " + login;
1925 if(row[6] && str2x(row[6], msg->header.repeat))
1927 mysql_free_result(res);
1928 errorStr = "Invalid value in message header for user: " + login;
1933 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1935 mysql_free_result(res);
1936 errorStr = "Invalid value in message header for user: " + login;
1941 msg->header.id = id;
1944 mysql_free_result(res);
1948 //-----------------------------------------------------------------------------
1949 int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
1951 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1952 login.c_str(),(long long)id);
1954 if(MysqlSetQuery(qbuf))
1956 errorStr = "Couldn't delete Message:\n";
1957 //errorStr += mysql_error(sock);
1963 //-----------------------------------------------------------------------------
1964 int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1969 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
1971 if(MysqlGetQuery(qbuf,sock))
1973 errorStr = "Couldn't GetMessageHdrs:\n";
1974 errorStr += mysql_error(sock);
1979 if (!(res=mysql_store_result(sock)))
1981 errorStr = "Couldn't GetMessageHdrs:\n";
1982 errorStr += mysql_error(sock);
1987 unsigned int i, num_rows = mysql_num_rows(res);
1988 long long int unsigned id = 0;
1990 for (i=0; i<num_rows; i++)
1992 row = mysql_fetch_row(res);
1993 if (str2x(row[1], id))
1998 if(str2x(row[2], hdr.type))
2002 if(str2x(row[3], hdr.lastSendTime))
2006 if(str2x(row[4], hdr.creationTime))
2010 if(str2x(row[5], hdr.showTime))
2014 if(str2x(row[6], hdr.repeat))
2018 if(str2x(row[7], hdr.repeatPeriod))
2022 hdrsList->push_back(hdr);
2025 mysql_free_result(res);
2029 //-----------------------------------------------------------------------------
2031 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2034 int ret=MysqlGetQuery(Query,sock);
2038 //-----------------------------------------------------------------------------
2039 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2040 if (!(sock=MysqlConnect())) {
2043 return MysqlQuery(Query,sock);
2045 //-----------------------------------------------------------------------------
2046 MYSQL * MYSQL_STORE::MysqlConnect() const {
2048 if ( !(sock=mysql_init(NULL)) ){
2049 errorStr= "mysql init susck\n";
2052 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2053 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2056 errorStr = "Couldn't connect to mysql engine! With error:\n";
2057 errorStr += mysql_error(sock);
2061 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2062 errorStr = "Database lost !\n";
2068 //-----------------------------------------------------------------------------