5 #include <mysql/mysql.h>
6 #include <mysql/errmsg.h>
10 #include "user_stat.h"
11 #include "mysql_store.h"
14 #define adm_enc_passwd "cjeifY8m3"
19 const int pt_mega = 1024 * 1024;
20 const string badSyms = "'`";
21 const char repSym = '\"';
22 const int RepitTimes = 3;
24 int GetInt(const string & str, int * val, int defaultVal)
28 *val = strtol(str.c_str(), &res, 10);
32 *val = defaultVal; //Error!
39 int GetDouble(const string & str, double * val, double defaultVal)
43 *val = strtod(str.c_str(), &res);
47 *val = defaultVal; //Error!
54 int GetTime(const string & str, time_t * val, time_t defaultVal)
58 *val = strtol(str.c_str(), &res, 10);
62 *val = defaultVal; //Error!
69 //-----------------------------------------------------------------------------
70 string ReplaceStr(string source, const string symlist, const char chgsym)
72 string::size_type pos=0;
74 while( (pos = source.find_first_of(symlist,pos)) != string::npos)
75 source.replace(pos, 1,1, chgsym);
80 int GetULongLongInt(const string & str, uint64_t * val, uint64_t defaultVal)
84 *val = strtoull(str.c_str(), &res, 10);
88 *val = defaultVal; //Error!
103 bs = new MYSQL_STORE();
111 BASE_STORE * GetStore()
116 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
123 BASE_STORE * GetStore()
125 //return sc.GetStore();
126 return new MYSQL_STORE();
128 //-----------------------------------------------------------------------------
129 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
132 //-----------------------------------------------------------------------------
133 MYSQL_STORE_SETTINGS::~MYSQL_STORE_SETTINGS()
137 //-----------------------------------------------------------------------------
138 int MYSQL_STORE_SETTINGS::ParseParam(const vector<PARAM_VALUE> & moduleParams,
139 const string & name, string & result)
143 vector<PARAM_VALUE>::const_iterator pvi;
144 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
145 if (pvi == moduleParams.end())
147 errorStr = "Parameter \'" + name + "\' not found.";
151 result = pvi->value[0];
155 //-----------------------------------------------------------------------------
156 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
158 if (ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
160 if (ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
162 if (ParseParam(s.moduleParams, "dbname", dbName) < 0)
164 if (ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
169 //-----------------------------------------------------------------------------
170 const string & MYSQL_STORE_SETTINGS::GetStrError() const
174 //-----------------------------------------------------------------------------
175 string MYSQL_STORE_SETTINGS::GetDBUser() const
179 //-----------------------------------------------------------------------------
180 string MYSQL_STORE_SETTINGS::GetDBPassword() const
184 //-----------------------------------------------------------------------------
185 string MYSQL_STORE_SETTINGS::GetDBHost() const
189 //-----------------------------------------------------------------------------
190 string MYSQL_STORE_SETTINGS::GetDBName() const
194 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
196 //-----------------------------------------------------------------------------
197 MYSQL_STORE::MYSQL_STORE()
199 version = "mysql_store v.0.67";
201 //-----------------------------------------------------------------------------
202 MYSQL_STORE::~MYSQL_STORE()
205 //-----------------------------------------------------------------------------
206 void MYSQL_STORE::SetSettings(const MODULE_SETTINGS & s)
210 //-----------------------------------------------------------------------------
211 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
215 if( (ret = mysql_query(sock,sQuery)) )
217 for(i=0; i<RepitTimes; i++)
219 if( (ret = mysql_query(sock,sQuery)) )
220 ;//need to send error result
228 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
231 int MYSQL_STORE::ParseSettings()
233 int ret = storeSettings.ParseSettings(settings);
238 errorStr = storeSettings.GetStrError();
241 if(storeSettings.GetDBPassword().length() == 0)
243 errorStr = "Database password must be not empty. Please read Manual.";
247 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
248 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
251 errorStr = "Couldn't connect to mysql engine! With error:\n";
252 errorStr += mysql_error(&mysql);
258 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
260 string res = "CREATE DATABASE " + storeSettings.GetDBName();
262 if(MysqlQuery(res.c_str(),sock))
264 errorStr = "Couldn't create database! With error:\n";
265 errorStr += mysql_error(sock);
271 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
273 errorStr = "Couldn't select database! With error:\n";
274 errorStr += mysql_error(sock);
278 ret = CheckAllTables(sock);
282 ret = CheckAllTables(sock);
289 //-----------------------------------------------------------------------------
290 const string & MYSQL_STORE::GetStrError() const
294 //-----------------------------------------------------------------------------
295 const string & MYSQL_STORE::GetVersion() const
299 //-----------------------------------------------------------------------------
300 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
304 if (!(result=mysql_list_tables(sock,str.c_str() )))
306 errorStr = "Couldn't get tables list With error:\n";
307 errorStr += mysql_error(sock);
312 unsigned int num_rows = mysql_num_rows(result);
315 mysql_free_result(result);
317 return (num_rows == 1);
319 //-----------------------------------------------------------------------------
320 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
322 //admins-----------------------------------------------------------------------
323 if(!IsTablePresent("admins",sock))
325 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
326 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
327 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
328 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
329 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
331 if(MysqlQuery(qbuf,sock))
333 errorStr = "Couldn't create admin table list With error:\n";
334 errorStr += mysql_error(sock);
339 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
340 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
341 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
343 if(MysqlQuery(qbuf,sock))
345 errorStr = "Couldn't create default admin. With error:\n";
346 errorStr += mysql_error(sock);
352 //tariffs-----------------------------------------------------------------------
354 if(!IsTablePresent("tariffs",sock))
356 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
358 for (int i = 0; i < DIR_NUM; i++)
360 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
363 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
366 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
369 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
372 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
376 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
379 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
382 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
386 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
387 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
389 if(MysqlQuery(res.c_str(),sock))
391 errorStr = "Couldn't create tariffs table list With error:\n";
392 errorStr += mysql_error(sock);
397 res = "INSERT INTO tariffs SET name='tariff',";
399 for (int i = 0; i < DIR_NUM; i++)
401 strprintf(¶m, " NoDiscount%d=1,", i);
404 strprintf(¶m, " Threshold%d=0,", i);
407 strprintf(¶m, " Time%d='0:0-0:0',", i);
412 strprintf(¶m, " SinglePrice%d=0,", i);
418 strprintf(¶m, " PriceDayA%d=0.0,", i);
423 strprintf(¶m, " PriceDayB%d=0.0,", i);
429 strprintf(¶m, " PriceNightA%d=0.0,", i);
434 strprintf(¶m, " PriceNightB%d=0.0,", i);
439 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
440 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
441 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
443 if(MysqlQuery(res.c_str(),sock))
445 errorStr = "Couldn't create default tariff. With error:\n";
446 errorStr += mysql_error(sock);
452 //users-----------------------------------------------------------------------
453 if(!IsTablePresent("users",sock))
455 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
456 "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 '',"\
457 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
458 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
459 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
461 for (int i = 0; i < USERDATA_NUM; i++)
463 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
467 param = " CreditExpire INT(11) DEFAULT 0,";
470 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
473 for (int i = 0; i < DIR_NUM; i++)
475 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
478 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
482 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
483 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
484 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
485 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
488 if(MysqlQuery(res.c_str(),sock))
490 errorStr = "Couldn't create users table list With error:\n";
491 errorStr += mysql_error(sock);
492 errorStr += "\n\n" + res;
497 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
498 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
499 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
500 "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
503 for (int i = 0; i < DIR_NUM; i++)
505 strprintf(¶m, " D%d=0,", i);
508 strprintf(¶m, " U%d=0,", i);
512 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
513 "LastCashAddTime=0, PassiveTime=0";
515 if(MysqlQuery(res.c_str(),sock))
517 errorStr = "Couldn't create default user. With error:\n";
518 errorStr += mysql_error(sock);
524 //logs-----------------------------------------------------------------------
525 if(!IsTablePresent("logs"))
527 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
531 errorStr = "Couldn't create admin table list With error:\n";
532 errorStr += mysql_error(sock);
537 //messages---------------------------------------------------------------------
538 if(!IsTablePresent("messages",sock))
540 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
541 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
542 "stgRepeat INT, repeatPeriod INT, text TEXT)");
544 if(MysqlQuery(qbuf,sock))
546 errorStr = "Couldn't create messages table. With error:\n";
547 errorStr += mysql_error(sock);
553 //month_stat-------------------------------------------------------------------
554 if(!IsTablePresent("stat",sock))
556 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
558 for (int i = 0; i < DIR_NUM; i++)
560 strprintf(¶m, " U%d BIGINT,", i);
563 strprintf(¶m, " D%d BIGINT,", i);
567 res += " cash DOUBLE, INDEX (login))";
569 if(MysqlQuery(res.c_str(),sock))
571 errorStr = "Couldn't create stat table. With error:\n";
572 errorStr += mysql_error(sock);
580 //-----------------------------------------------------------------------------
581 //-----------------------------------------------------------------------------
583 int MYSQL_STORE::GetAllParams(vector<string> * ParamList,
584 const string & table, const string & name) const
593 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
595 if(MysqlGetQuery(qbuf,sock))
597 errorStr = "Couldn't GetAllParams Query for: ";
598 errorStr += name + " - " + table + "\n";
599 errorStr += mysql_error(sock);
604 if (!(res=mysql_store_result(sock)))
606 errorStr = "Couldn't GetAllParams Results for: ";
607 errorStr += name + " - " + table + "\n";
608 errorStr += mysql_error(sock);
612 num = mysql_num_rows(res);
616 row = mysql_fetch_row(res);
617 ParamList->push_back(row[0]);
620 mysql_free_result(res);
626 //-----------------------------------------------------------------------------
627 int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
629 if(GetAllParams(usersList, "users", "login"))
634 //-----------------------------------------------------------------------------
635 int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
637 if(GetAllParams(adminsList, "admins", "login"))
642 //-----------------------------------------------------------------------------
643 int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
645 if(GetAllParams(tariffsList, "tariffs", "name"))
650 //-----------------------------------------------------------------------------
651 int MYSQL_STORE::AddUser(const string & login) const
653 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
655 if(MysqlSetQuery(qbuf))
657 errorStr = "Couldn't add user:\n";
658 //errorStr += mysql_error(sock);
664 //-----------------------------------------------------------------------------
665 int MYSQL_STORE::DelUser(const string & login) const
667 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
669 if(MysqlSetQuery(qbuf))
671 errorStr = "Couldn't delete user:\n";
672 //errorStr += mysql_error(sock);
678 //-----------------------------------------------------------------------------
679 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
686 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
687 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
688 RealName, StgGroup, Credit, TariffChange, ";
690 for (int i = 0; i < USERDATA_NUM; i++)
692 sprintf(qbuf, "Userdata%d, ", i);
696 query += "CreditExpire, IP FROM users WHERE login='";
697 query += login + "' LIMIT 1";
699 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
701 if(MysqlGetQuery(query.c_str(),sock))
703 errorStr = "Couldn't restore Tariff(on query):\n";
704 errorStr += mysql_error(sock);
709 if (!(res=mysql_store_result(sock)))
711 errorStr = "Couldn't restore Tariff(on getting result):\n";
712 errorStr += mysql_error(sock);
717 row = mysql_fetch_row(res);
721 conf->password = row[1];
723 if (conf->password.empty())
725 mysql_free_result(res);
726 errorStr = "User \'" + login + "\' password is blank.";
731 if (GetInt(row[2],&conf->passive, 0) != 0)
733 mysql_free_result(res);
734 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
739 if (GetInt(row[3], &conf->disabled, 0) != 0)
741 mysql_free_result(res);
742 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
747 if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
749 mysql_free_result(res);
750 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
755 if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
757 mysql_free_result(res);
758 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
763 conf->tariffName = row[6];
765 if (conf->tariffName.empty())
767 mysql_free_result(res);
768 errorStr = "User \'" + login + "\' tariff is blank.";
773 conf->address = row[7];
774 conf->phone = row[8];
775 conf->email = row[9];
776 conf->note = row[10];
777 conf->realName = row[11];
778 conf->group = row[12];
780 if (GetDouble(row[13], &conf->credit, 0) != 0)
782 mysql_free_result(res);
783 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
788 conf->nextTariff = row[14];
790 for (int i = 0; i < USERDATA_NUM; i++)
792 conf->userdata[i] = row[15+i];
795 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
797 string ipStr = row[16+USERDATA_NUM];
805 mysql_free_result(res);
806 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
812 mysql_free_result(res);
817 //-----------------------------------------------------------------------------
818 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
828 for (int i = 0; i < DIR_NUM; i++)
830 sprintf(qbuf, "D%d, U%d, ", i, i);
834 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
835 FROM users WHERE login = '";
836 query += login + "'";
838 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
840 if(MysqlGetQuery(query.c_str() ,sock))
842 errorStr = "Couldn't restore UserStat(on query):\n";
843 errorStr += mysql_error(sock);
848 if (!(res=mysql_store_result(sock)))
850 errorStr = "Couldn't restore UserStat(on getting result):\n";
851 errorStr += mysql_error(sock);
856 row = mysql_fetch_row(res);
858 unsigned int startPos=0;
861 uint64_t traffU[DIR_NUM];
862 uint64_t traffD[DIR_NUM];
864 for (int i = 0; i < DIR_NUM; i++)
866 sprintf(s, "D%d", i);
867 if (GetULongLongInt(row[startPos+i*2],&traffD[i], 0) != 0)
869 mysql_free_result(res);
870 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
876 sprintf(s, "U%d", i);
877 if (GetULongLongInt(row[startPos+i*2+1], &traffU[i], 0) != 0)
879 mysql_free_result(res);
880 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
887 startPos += (2*DIR_NUM);
889 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
891 mysql_free_result(res);
892 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
897 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
899 mysql_free_result(res);
900 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
905 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
907 mysql_free_result(res);
908 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
913 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
915 mysql_free_result(res);
916 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
921 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
923 mysql_free_result(res);
924 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
929 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
931 mysql_free_result(res);
932 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
937 mysql_free_result(res);
941 //-----------------------------------------------------------------------------
942 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
947 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
948 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
949 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
950 conf.password.c_str(),
953 conf.disabledDetailStat,
955 conf.tariffName.c_str(),
956 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
957 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
958 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
959 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
960 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
961 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
963 conf.nextTariff.c_str()
966 for (int i = 0; i < USERDATA_NUM; i++)
968 strprintf(¶m, " Userdata%d='%s',", i,
969 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
973 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
979 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
982 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
985 if(MysqlSetQuery(res.c_str()))
987 errorStr = "Couldn't save user conf:\n";
988 //errorStr += mysql_error(sock);
994 //-----------------------------------------------------------------------------
995 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
1000 res = "UPDATE users SET";
1002 for (int i = 0; i < DIR_NUM; i++)
1004 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1007 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1011 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
1012 " PassiveTime=%d, LastActivityTime=%d",
1016 stat.lastCashAddTime,
1018 stat.lastActivityTime
1022 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
1025 if(MysqlSetQuery(res.c_str()))
1027 errorStr = "Couldn't save user stat:\n";
1028 // errorStr += mysql_error(sock);
1034 //-----------------------------------------------------------------------------
1035 int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
1037 string res, tempStr;
1046 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1047 if (!(sock=MysqlConnect())){
1048 errorStr = "Couldn't connect to Server";
1051 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1053 errorStr = "Couldn't get table " + tempStr + ":\n";
1054 errorStr += mysql_error(sock);
1059 unsigned int num_rows = mysql_num_rows(result);
1061 mysql_free_result(result);
1065 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1066 lt->tm_mon+1, lt->tm_year+1900);
1068 if(MysqlQuery(qbuf,sock))
1070 errorStr = "Couldn't create WriteDetailedStat table:\n";
1071 errorStr += mysql_error(sock);
1077 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1081 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1082 lt->tm_mon+1, lt->tm_year+1900,
1083 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1085 if(MysqlQuery(send.c_str(),sock))
1087 errorStr = "Couldn't write log string:\n";
1088 errorStr += mysql_error(sock);
1096 //-----------------------------------------------------------------------------
1097 int MYSQL_STORE::WriteUserChgLog(const string & login,
1098 const string & admLogin,
1100 const string & paramName,
1101 const string & oldValue,
1102 const string & newValue,
1103 const string & message) const
1105 string userLogMsg = "Admin \'" + admLogin + "\', " + string(inet_ntostr(admIP)) + ": \'"
1106 + paramName + "\' parameter changed from \'" + oldValue +
1107 "\' to \'" + newValue + "\'. " + message;
1109 return WriteLogString(userLogMsg, login);
1111 //-----------------------------------------------------------------------------
1112 int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1114 string logStr = "Connect, " + string(inet_ntostr(ip));
1115 return WriteLogString(logStr, login);
1117 //-----------------------------------------------------------------------------
1118 int MYSQL_STORE::WriteUserDisconnect(const string & login,
1119 const DIR_TRAFF & up,
1120 const DIR_TRAFF & down,
1121 const DIR_TRAFF & sessionUp,
1122 const DIR_TRAFF & sessionDown,
1124 double freeMb) const
1126 string logStr = "Disconnect, ";
1131 stringstream sscash;
1137 sssd << sessionDown;
1141 logStr += " session upload: \'";
1142 logStr += sssu.str();
1143 logStr += "\' session download: \'";
1144 logStr += sssd.str();
1145 logStr += "\' month upload: \'";
1146 logStr += ssmu.str();
1147 logStr += "\' month download: \'";
1148 logStr += ssmd.str();
1149 logStr += "\' cash: \'";
1150 logStr += sscash.str();
1153 return WriteLogString(logStr, login);
1155 //-----------------------------------------------------------------------------
1156 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1157 const string & login) const
1161 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1162 login.c_str(), month+1, year+1900);
1164 for (int i = 0; i < DIR_NUM; i++)
1166 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1169 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1173 strprintf(¶m, " cash=%f", stat.cash);
1176 if(MysqlSetQuery(res.c_str()))
1178 errorStr = "Couldn't SaveMonthStat:\n";
1179 //errorStr += mysql_error(sock);
1185 //-----------------------------------------------------------------------------*/
1186 int MYSQL_STORE::AddAdmin(const string & login) const
1188 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1190 if(MysqlSetQuery(qbuf))
1192 errorStr = "Couldn't add admin:\n";
1193 //errorStr += mysql_error(sock);
1199 //-----------------------------------------------------------------------------*/
1200 int MYSQL_STORE::DelAdmin(const string & login) const
1202 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1204 if(MysqlSetQuery(qbuf))
1206 errorStr = "Couldn't delete admin:\n";
1207 //errorStr += mysql_error(sock);
1213 //-----------------------------------------------------------------------------*/
1214 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1216 char passwordE[2 * ADM_PASSWD_LEN + 2];
1217 char pass[ADM_PASSWD_LEN + 1];
1218 char adminPass[ADM_PASSWD_LEN + 1];
1220 memset(pass, 0, sizeof(pass));
1221 memset(adminPass, 0, sizeof(adminPass));
1224 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1226 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1227 adminPass[ADM_PASSWD_LEN - 1] = 0;
1229 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1231 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1234 pass[ADM_PASSWD_LEN - 1] = 0;
1235 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1237 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1238 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1239 "WHERE login='%s' LIMIT 1",
1251 if(MysqlSetQuery(qbuf))
1253 errorStr = "Couldn't save admin:\n";
1254 //errorStr += mysql_error(sock);
1260 //-----------------------------------------------------------------------------
1261 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1263 char pass[ADM_PASSWD_LEN + 1];
1264 char password[ADM_PASSWD_LEN + 1];
1265 char passwordE[2*ADM_PASSWD_LEN + 2];
1272 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1274 if(MysqlGetQuery(qbuf,sock))
1276 errorStr = "Couldn't restore admin:\n";
1277 errorStr += mysql_error(sock);
1282 if (!(res=mysql_store_result(sock)))
1284 errorStr = "Couldn't restore admin:\n";
1285 errorStr += mysql_error(sock);
1290 if ( mysql_num_rows(res) == 0)
1292 mysql_free_result(res);
1293 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1298 row = mysql_fetch_row(res);
1305 mysql_free_result(res);
1306 errorStr = "Error in parameter password";
1311 memset(passwordE, 0, sizeof(passwordE));
1312 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1314 memset(pass, 0, sizeof(pass));
1316 if (passwordE[0] != 0)
1318 Decode21(pass, passwordE);
1319 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1321 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1323 DecodeString(password + 8*i, pass + 8*i, &ctx);
1331 ac->password = password;
1333 if (GetInt(row[2], &a, 0) == 0)
1334 ac->priv.userConf = a;
1337 mysql_free_result(res);
1338 errorStr = "Error in parameter ChgConf";
1343 if (GetInt(row[3], &a, 0) == 0)
1344 ac->priv.userPasswd = a;
1347 mysql_free_result(res);
1348 errorStr = "Error in parameter ChgPassword";
1353 if (GetInt(row[4], &a, 0) == 0)
1354 ac->priv.userStat = a;
1357 mysql_free_result(res);
1358 errorStr = "Error in parameter ChgStat";
1363 if (GetInt(row[5], &a, 0) == 0)
1364 ac->priv.userCash = a;
1367 mysql_free_result(res);
1368 errorStr = "Error in parameter ChgCash";
1373 if (GetInt(row[6], &a, 0) == 0)
1374 ac->priv.userAddDel = a;
1377 mysql_free_result(res);
1378 errorStr = "Error in parameter UsrAddDel";
1383 if (GetInt(row[7], &a, 0) == 0)
1384 ac->priv.tariffChg = a;
1387 mysql_free_result(res);
1388 errorStr = "Error in parameter ChgTariff";
1393 if (GetInt(row[8], &a, 0) == 0)
1394 ac->priv.adminChg = a;
1397 mysql_free_result(res);
1398 errorStr = "Error in parameter ChgAdmin";
1403 mysql_free_result(res);
1407 //-----------------------------------------------------------------------------
1408 int MYSQL_STORE::AddTariff(const string & name) const
1410 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1412 if(MysqlSetQuery(qbuf))
1414 errorStr = "Couldn't add tariff:\n";
1415 // errorStr += mysql_error(sock);
1421 //-----------------------------------------------------------------------------
1422 int MYSQL_STORE::DelTariff(const string & name) const
1424 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1426 if(MysqlSetQuery(qbuf))
1428 errorStr = "Couldn't delete tariff: ";
1429 // errorStr += mysql_error(sock);
1435 //-----------------------------------------------------------------------------
1436 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1441 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1443 if(MysqlGetQuery(qbuf,sock))
1445 errorStr = "Couldn't restore Tariff:\n";
1446 errorStr += mysql_error(sock);
1451 if (!(res=mysql_store_result(sock)))
1453 errorStr = "Couldn't restore Tariff:\n";
1454 errorStr += mysql_error(sock);
1460 td->tariffConf.name = tariffName;
1462 row = mysql_fetch_row(res);
1465 for (int i = 0; i<DIR_NUM; i++)
1467 strprintf(¶m, "Time%d", i);
1469 if (str.length() == 0)
1471 mysql_free_result(res);
1472 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1477 ParseTariffTimeStr(str.c_str(),
1478 td->dirPrice[i].hDay,
1479 td->dirPrice[i].mDay,
1480 td->dirPrice[i].hNight,
1481 td->dirPrice[i].mNight);
1483 strprintf(¶m, "PriceDayA%d", i);
1484 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1486 mysql_free_result(res);
1487 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1491 td->dirPrice[i].priceDayA /= (1024*1024);
1493 strprintf(¶m, "PriceDayB%d", i);
1494 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1496 mysql_free_result(res);
1497 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1501 td->dirPrice[i].priceDayB /= (1024*1024);
1503 strprintf(¶m, "PriceNightA%d", i);
1504 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1506 mysql_free_result(res);
1507 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1511 td->dirPrice[i].priceNightA /= (1024*1024);
1513 strprintf(¶m, "PriceNightB%d", i);
1514 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1516 mysql_free_result(res);
1517 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1521 td->dirPrice[i].priceNightB /= (1024*1024);
1523 strprintf(¶m, "Threshold%d", i);
1524 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
1526 mysql_free_result(res);
1527 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1532 strprintf(¶m, "SinglePrice%d", i);
1533 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
1535 mysql_free_result(res);
1536 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1541 strprintf(¶m, "NoDiscount%d", i);
1542 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
1544 mysql_free_result(res);
1545 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1551 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1553 mysql_free_result(res);
1554 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1559 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1561 mysql_free_result(res);
1562 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1567 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1569 mysql_free_result(res);
1570 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1575 str = row[4+8*DIR_NUM];
1576 param = "TraffType";
1578 if (str.length() == 0)
1580 mysql_free_result(res);
1581 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1586 if (!strcasecmp(str.c_str(), "up"))
1587 td->tariffConf.traffType = TRAFF_UP;
1589 if (!strcasecmp(str.c_str(), "down"))
1590 td->tariffConf.traffType = TRAFF_DOWN;
1592 if (!strcasecmp(str.c_str(), "up+down"))
1593 td->tariffConf.traffType = TRAFF_UP_DOWN;
1595 if (!strcasecmp(str.c_str(), "max"))
1596 td->tariffConf.traffType = TRAFF_MAX;
1599 mysql_free_result(res);
1600 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1605 mysql_free_result(res);
1609 //-----------------------------------------------------------------------------
1610 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1614 string res="UPDATE tariffs SET";
1616 for (int i = 0; i < DIR_NUM; i++)
1618 strprintf(¶m, " PriceDayA%d=%f,", i,
1619 td.dirPrice[i].priceDayA * pt_mega);
1622 strprintf(¶m, " PriceDayB%d=%f,", i,
1623 td.dirPrice[i].priceDayB * pt_mega);
1626 strprintf(¶m, " PriceNightA%d=%f,", i,
1627 td.dirPrice[i].priceNightA * pt_mega);
1630 strprintf(¶m, " PriceNightB%d=%f,", i,
1631 td.dirPrice[i].priceNightB * pt_mega);
1634 strprintf(¶m, " Threshold%d=%d,", i,
1635 td.dirPrice[i].threshold);
1639 strprintf(¶m, " Time%d", i);
1641 strprintf(&s, "%0d:%0d-%0d:%0d",
1642 td.dirPrice[i].hDay,
1643 td.dirPrice[i].mDay,
1644 td.dirPrice[i].hNight,
1645 td.dirPrice[i].mNight);
1647 res += (param + "='" + s + "',");
1649 strprintf(¶m, " NoDiscount%d=%d,", i,
1650 td.dirPrice[i].noDiscount);
1653 strprintf(¶m, " SinglePrice%d=%d,", i,
1654 td.dirPrice[i].singlePrice);
1658 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1661 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1664 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1667 switch (td.tariffConf.traffType)
1670 res += " TraffType='up'";
1673 res += " TraffType='down'";
1676 res += " TraffType='up+down'";
1679 res += " TraffType='max'";
1682 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1685 if(MysqlSetQuery(res.c_str()))
1687 errorStr = "Couldn't save admin:\n";
1688 //errorStr += mysql_error(sock);
1694 //-----------------------------------------------------------------------------
1695 int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> * statTree,
1697 const string & login) const
1699 string res, stTime, endTime, tempStr;
1706 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1714 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1716 if (!(sock=MysqlConnect())){
1721 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1723 errorStr = "Couldn't get table " + tempStr + ":\n";
1724 errorStr += mysql_error(sock);
1729 unsigned int num_rows = mysql_num_rows(result);
1731 mysql_free_result(result);
1735 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1736 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1737 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1738 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1739 lt->tm_mon+1, lt->tm_year+1900);
1741 if(MysqlQuery(qbuf,sock))
1743 errorStr = "Couldn't create WriteDetailedStat table:\n";
1744 errorStr += mysql_error(sock);
1753 lt1 = localtime(&lastStat);
1762 lt2 = localtime(&t);
1768 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1769 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1771 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1772 "day=%d,startTime='%s',endTime='%s',",
1773 lt->tm_mon+1, lt->tm_year+1900,
1781 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1782 stIter = statTree->begin();
1784 while (stIter != statTree->end())
1786 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1787 inet_ntostr(stIter->first.ip),
1789 stIter->second.down,
1794 if( (retRes = MysqlQuery((res+tempStr).c_str(),sock)) )
1796 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1797 errorStr += mysql_error(sock);
1802 result=mysql_store_result(sock);
1804 mysql_free_result(result);
1811 //-----------------------------------------------------------------------------
1812 int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
1816 gettimeofday(&tv, NULL);
1818 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1820 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1822 (long long)msg->header.id
1825 if(MysqlSetQuery(qbuf))
1827 errorStr = "Couldn't add message:\n";
1828 //errorStr += mysql_error(sock);
1832 return EditMessage(*msg, login);
1834 //-----------------------------------------------------------------------------
1835 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1839 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1840 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1841 "WHERE login='%s' AND id=%lld LIMIT 1",
1843 msg.header.lastSendTime,
1844 msg.header.creationTime,
1845 msg.header.showTime,
1847 msg.header.repeatPeriod,
1848 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1850 (long long)msg.header.id
1853 if(MysqlSetQuery(res.c_str()))
1855 errorStr = "Couldn't edit message:\n";
1856 //errorStr += mysql_error(sock);
1862 //-----------------------------------------------------------------------------
1863 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1869 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1872 if(MysqlGetQuery(qbuf,sock))
1874 errorStr = "Couldn't GetMessage:\n";
1875 errorStr += mysql_error(sock);
1880 if (!(res=mysql_store_result(sock)))
1882 errorStr = "Couldn't GetMessage:\n";
1883 errorStr += mysql_error(sock);
1888 row = mysql_fetch_row(res);
1890 if(row[2]&&str2x(row[2], msg->header.type))
1892 mysql_free_result(res);
1893 errorStr = "Invalid value in message header for user: " + login;
1898 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1900 mysql_free_result(res);
1901 errorStr = "Invalid value in message header for user: " + login;
1906 if(row[4] && str2x(row[4], msg->header.creationTime))
1908 mysql_free_result(res);
1909 errorStr = "Invalid value in message header for user: " + login;
1914 if(row[5] && str2x(row[5], msg->header.showTime))
1916 mysql_free_result(res);
1917 errorStr = "Invalid value in message header for user: " + login;
1922 if(row[6] && str2x(row[6], msg->header.repeat))
1924 mysql_free_result(res);
1925 errorStr = "Invalid value in message header for user: " + login;
1930 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1932 mysql_free_result(res);
1933 errorStr = "Invalid value in message header for user: " + login;
1938 msg->header.id = id;
1941 mysql_free_result(res);
1945 //-----------------------------------------------------------------------------
1946 int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
1948 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1949 login.c_str(),(long long)id);
1951 if(MysqlSetQuery(qbuf))
1953 errorStr = "Couldn't delete Message:\n";
1954 //errorStr += mysql_error(sock);
1960 //-----------------------------------------------------------------------------
1961 int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1966 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
1968 if(MysqlGetQuery(qbuf,sock))
1970 errorStr = "Couldn't GetMessageHdrs:\n";
1971 errorStr += mysql_error(sock);
1976 if (!(res=mysql_store_result(sock)))
1978 errorStr = "Couldn't GetMessageHdrs:\n";
1979 errorStr += mysql_error(sock);
1984 unsigned int i, num_rows = mysql_num_rows(res);
1985 long long int unsigned id;
1987 for (i=0; i<num_rows; i++)
1989 row = mysql_fetch_row(res);
1990 if (str2x(row[1], id))
1995 if(str2x(row[2], hdr.type))
1999 if(str2x(row[3], hdr.lastSendTime))
2003 if(str2x(row[4], hdr.creationTime))
2007 if(str2x(row[5], hdr.showTime))
2011 if(str2x(row[6], hdr.repeat))
2015 if(str2x(row[7], hdr.repeatPeriod))
2019 hdrsList->push_back(hdr);
2022 mysql_free_result(res);
2026 //-----------------------------------------------------------------------------
2028 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
2031 int ret=MysqlGetQuery(Query,sock);
2035 //-----------------------------------------------------------------------------
2036 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
2037 if (!(sock=MysqlConnect())) {
2040 return MysqlQuery(Query,sock);
2042 //-----------------------------------------------------------------------------
2043 MYSQL * MYSQL_STORE::MysqlConnect() const {
2045 if ( !(sock=mysql_init(NULL)) ){
2046 errorStr= "mysql init susck\n";
2049 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2050 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2053 errorStr = "Couldn't connect to mysql engine! With error:\n";
2054 errorStr += mysql_error(sock);
2058 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2059 errorStr = "Database lost !\n";
2065 //-----------------------------------------------------------------------------