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()
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 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
156 MYSQL_STORE::MYSQL_STORE()
158 version("mysql_store v.0.67"),
161 logger(GetPluginLogger(GetStgLogger(), "store_mysql"))
164 //-----------------------------------------------------------------------------
165 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
169 if( (ret = mysql_query(sock,sQuery)) )
171 for(int i=0; i<RepitTimes; i++)
173 if( (ret = mysql_query(sock,sQuery)) )
174 ;//need to send error result
182 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
185 int MYSQL_STORE::ParseSettings()
187 int ret = storeSettings.ParseSettings(settings);
192 errorStr = storeSettings.GetStrError();
195 if(storeSettings.GetDBPassword().length() == 0)
197 errorStr = "Database password must be not empty. Please read Manual.";
201 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
202 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
205 errorStr = "Couldn't connect to mysql engine! With error:\n";
206 errorStr += mysql_error(&mysql);
212 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
214 string res = "CREATE DATABASE " + storeSettings.GetDBName();
216 if(MysqlQuery(res.c_str(),sock))
218 errorStr = "Couldn't create database! With error:\n";
219 errorStr += mysql_error(sock);
225 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
227 errorStr = "Couldn't select database! With error:\n";
228 errorStr += mysql_error(sock);
232 ret = CheckAllTables(sock);
236 ret = CheckAllTables(sock);
243 //-----------------------------------------------------------------------------
244 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
248 if (!(result=mysql_list_tables(sock,str.c_str() )))
250 errorStr = "Couldn't get tables list With error:\n";
251 errorStr += mysql_error(sock);
256 unsigned int num_rows = mysql_num_rows(result);
259 mysql_free_result(result);
261 return (num_rows == 1);
263 //-----------------------------------------------------------------------------
264 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
266 //admins-----------------------------------------------------------------------
267 if(!IsTablePresent("admins",sock))
269 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
270 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
271 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
272 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
273 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
275 if(MysqlQuery(qbuf,sock))
277 errorStr = "Couldn't create admin table list With error:\n";
278 errorStr += mysql_error(sock);
283 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
284 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
285 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
287 if(MysqlQuery(qbuf,sock))
289 errorStr = "Couldn't create default admin. With error:\n";
290 errorStr += mysql_error(sock);
296 //tariffs-----------------------------------------------------------------------
298 if(!IsTablePresent("tariffs",sock))
300 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
302 for (int i = 0; i < DIR_NUM; i++)
304 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
307 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
310 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
313 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
316 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
319 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
322 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
325 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
329 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
330 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
332 if(MysqlQuery(res.c_str(),sock))
334 errorStr = "Couldn't create tariffs table list With error:\n";
335 errorStr += mysql_error(sock);
340 res = "INSERT INTO tariffs SET name='tariff',";
342 for (int i = 0; i < DIR_NUM; i++)
344 strprintf(¶m, " NoDiscount%d=1,", i);
347 strprintf(¶m, " Threshold%d=0,", i);
350 strprintf(¶m, " Time%d='0:0-0:0',", i);
355 strprintf(¶m, " SinglePrice%d=0,", i);
361 strprintf(¶m, " PriceDayA%d=0.0,", i);
366 strprintf(¶m, " PriceDayB%d=0.0,", i);
372 strprintf(¶m, " PriceNightA%d=0.0,", i);
377 strprintf(¶m, " PriceNightB%d=0.0,", i);
382 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
383 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
384 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
386 if(MysqlQuery(res.c_str(),sock))
388 errorStr = "Couldn't create default tariff. With error:\n";
389 errorStr += mysql_error(sock);
395 //users-----------------------------------------------------------------------
396 if(!IsTablePresent("users",sock))
398 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
399 "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 '',"\
400 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
401 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
402 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
404 for (int i = 0; i < USERDATA_NUM; i++)
406 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
410 param = " CreditExpire INT(11) DEFAULT 0,";
413 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
416 for (int i = 0; i < DIR_NUM; i++)
418 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
421 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
425 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
426 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
427 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
428 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
431 if(MysqlQuery(res.c_str(),sock))
433 errorStr = "Couldn't create users table list With error:\n";
434 errorStr += mysql_error(sock);
435 errorStr += "\n\n" + res;
440 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
441 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
442 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
443 "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
446 for (int i = 0; i < DIR_NUM; i++)
448 strprintf(¶m, " D%d=0,", i);
451 strprintf(¶m, " U%d=0,", i);
455 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
456 "LastCashAddTime=0, PassiveTime=0";
458 if(MysqlQuery(res.c_str(),sock))
460 errorStr = "Couldn't create default user. With error:\n";
461 errorStr += mysql_error(sock);
467 //logs-----------------------------------------------------------------------
468 if(!IsTablePresent("logs"))
470 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
474 errorStr = "Couldn't create admin table list With error:\n";
475 errorStr += mysql_error(sock);
480 //messages---------------------------------------------------------------------
481 if(!IsTablePresent("messages",sock))
483 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
484 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
485 "stgRepeat INT, repeatPeriod INT, text TEXT)");
487 if(MysqlQuery(qbuf,sock))
489 errorStr = "Couldn't create messages table. With error:\n";
490 errorStr += mysql_error(sock);
496 //month_stat-------------------------------------------------------------------
497 if(!IsTablePresent("stat",sock))
499 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
501 for (int i = 0; i < DIR_NUM; i++)
503 strprintf(¶m, " U%d BIGINT,", i);
506 strprintf(¶m, " D%d BIGINT,", i);
510 res += " cash DOUBLE, INDEX (login))";
512 if(MysqlQuery(res.c_str(),sock))
514 errorStr = "Couldn't create stat table. With error:\n";
515 errorStr += mysql_error(sock);
523 //-----------------------------------------------------------------------------
524 //-----------------------------------------------------------------------------
526 int MYSQL_STORE::GetAllParams(vector<string> * ParamList,
527 const string & table, const string & name) const
536 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
538 if(MysqlGetQuery(qbuf,sock))
540 errorStr = "Couldn't GetAllParams Query for: ";
541 errorStr += name + " - " + table + "\n";
542 errorStr += mysql_error(sock);
547 if (!(res=mysql_store_result(sock)))
549 errorStr = "Couldn't GetAllParams Results for: ";
550 errorStr += name + " - " + table + "\n";
551 errorStr += mysql_error(sock);
555 num = mysql_num_rows(res);
559 row = mysql_fetch_row(res);
560 ParamList->push_back(row[0]);
563 mysql_free_result(res);
569 //-----------------------------------------------------------------------------
570 int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
572 if(GetAllParams(usersList, "users", "login"))
577 //-----------------------------------------------------------------------------
578 int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
580 if(GetAllParams(adminsList, "admins", "login"))
585 //-----------------------------------------------------------------------------
586 int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
588 if(GetAllParams(tariffsList, "tariffs", "name"))
593 //-----------------------------------------------------------------------------
594 int MYSQL_STORE::AddUser(const string & login) const
596 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
598 if(MysqlSetQuery(qbuf))
600 errorStr = "Couldn't add user:\n";
601 //errorStr += mysql_error(sock);
607 //-----------------------------------------------------------------------------
608 int MYSQL_STORE::DelUser(const string & login) const
610 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
612 if(MysqlSetQuery(qbuf))
614 errorStr = "Couldn't delete user:\n";
615 //errorStr += mysql_error(sock);
621 //-----------------------------------------------------------------------------
622 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
629 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
630 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
631 RealName, StgGroup, Credit, TariffChange, ";
633 for (int i = 0; i < USERDATA_NUM; i++)
635 sprintf(qbuf, "Userdata%d, ", i);
639 query += "CreditExpire, IP FROM users WHERE login='";
640 query += login + "' LIMIT 1";
642 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
644 if(MysqlGetQuery(query.c_str(),sock))
646 errorStr = "Couldn't restore Tariff(on query):\n";
647 errorStr += mysql_error(sock);
652 if (!(res=mysql_store_result(sock)))
654 errorStr = "Couldn't restore Tariff(on getting result):\n";
655 errorStr += mysql_error(sock);
660 if (mysql_num_rows(res) != 1)
662 errorStr = "User not found";
667 row = mysql_fetch_row(res);
671 conf->password = row[1];
673 if (conf->password.empty())
675 mysql_free_result(res);
676 errorStr = "User \'" + login + "\' password is blank.";
681 if (GetInt(row[2],&conf->passive, 0) != 0)
683 mysql_free_result(res);
684 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
689 if (GetInt(row[3], &conf->disabled, 0) != 0)
691 mysql_free_result(res);
692 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
697 if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
699 mysql_free_result(res);
700 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
705 if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
707 mysql_free_result(res);
708 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
713 conf->tariffName = row[6];
715 if (conf->tariffName.empty())
717 mysql_free_result(res);
718 errorStr = "User \'" + login + "\' tariff is blank.";
723 conf->address = row[7];
724 conf->phone = row[8];
725 conf->email = row[9];
726 conf->note = row[10];
727 conf->realName = row[11];
728 conf->group = row[12];
730 if (GetDouble(row[13], &conf->credit, 0) != 0)
732 mysql_free_result(res);
733 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
738 conf->nextTariff = row[14];
740 for (int i = 0; i < USERDATA_NUM; i++)
742 conf->userdata[i] = row[15+i];
745 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
747 string ipStr = row[16+USERDATA_NUM];
753 catch (const string & s)
755 mysql_free_result(res);
756 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
762 mysql_free_result(res);
767 //-----------------------------------------------------------------------------
768 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
778 for (int i = 0; i < DIR_NUM; i++)
780 sprintf(qbuf, "D%d, U%d, ", i, i);
784 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
785 FROM users WHERE login = '";
786 query += login + "'";
788 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
790 if(MysqlGetQuery(query.c_str() ,sock))
792 errorStr = "Couldn't restore UserStat(on query):\n";
793 errorStr += mysql_error(sock);
798 if (!(res=mysql_store_result(sock)))
800 errorStr = "Couldn't restore UserStat(on getting result):\n";
801 errorStr += mysql_error(sock);
806 row = mysql_fetch_row(res);
808 unsigned int startPos=0;
812 for (int i = 0; i < DIR_NUM; i++)
815 sprintf(s, "D%d", i);
816 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
818 mysql_free_result(res);
819 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
823 stat->down[i] = traff;
825 sprintf(s, "U%d", i);
826 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
828 mysql_free_result(res);
829 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
836 startPos += (2*DIR_NUM);
838 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
840 mysql_free_result(res);
841 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
846 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
848 mysql_free_result(res);
849 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
854 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
856 mysql_free_result(res);
857 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
862 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
864 mysql_free_result(res);
865 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
870 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
872 mysql_free_result(res);
873 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
878 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
880 mysql_free_result(res);
881 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
886 mysql_free_result(res);
890 //-----------------------------------------------------------------------------
891 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
896 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
897 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
898 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
899 conf.password.c_str(),
902 conf.disabledDetailStat,
904 conf.tariffName.c_str(),
905 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
906 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
907 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
908 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
909 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
910 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
912 conf.nextTariff.c_str()
915 for (int i = 0; i < USERDATA_NUM; i++)
917 strprintf(¶m, " Userdata%d='%s',", i,
918 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
922 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
928 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
931 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
934 if(MysqlSetQuery(res.c_str()))
936 errorStr = "Couldn't save user conf:\n";
937 //errorStr += mysql_error(sock);
943 //-----------------------------------------------------------------------------
944 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
949 res = "UPDATE users SET";
951 for (int i = 0; i < DIR_NUM; i++)
953 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
956 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
960 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
961 " PassiveTime=%d, LastActivityTime=%d",
965 stat.lastCashAddTime,
967 stat.lastActivityTime
971 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
974 if(MysqlSetQuery(res.c_str()))
976 errorStr = "Couldn't save user stat:\n";
977 // errorStr += mysql_error(sock);
983 //-----------------------------------------------------------------------------
984 int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
995 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
996 if (!(sock=MysqlConnect())){
997 errorStr = "Couldn't connect to Server";
1000 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1002 errorStr = "Couldn't get table " + tempStr + ":\n";
1003 errorStr += mysql_error(sock);
1008 unsigned int num_rows = mysql_num_rows(result);
1010 mysql_free_result(result);
1014 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1015 lt->tm_mon+1, lt->tm_year+1900);
1017 if(MysqlQuery(qbuf,sock))
1019 errorStr = "Couldn't create WriteDetailedStat table:\n";
1020 errorStr += mysql_error(sock);
1026 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1030 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1031 lt->tm_mon+1, lt->tm_year+1900,
1032 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1034 if(MysqlQuery(send.c_str(),sock))
1036 errorStr = "Couldn't write log string:\n";
1037 errorStr += mysql_error(sock);
1045 //-----------------------------------------------------------------------------
1046 int MYSQL_STORE::WriteUserChgLog(const string & login,
1047 const string & admLogin,
1049 const string & paramName,
1050 const string & oldValue,
1051 const string & newValue,
1052 const string & message) const
1054 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1055 + paramName + "\' parameter changed from \'" + oldValue +
1056 "\' to \'" + newValue + "\'. " + message;
1058 return WriteLogString(userLogMsg, login);
1060 //-----------------------------------------------------------------------------
1061 int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1063 string logStr = "Connect, " + inet_ntostring(ip);
1064 return WriteLogString(logStr, login);
1066 //-----------------------------------------------------------------------------
1067 int MYSQL_STORE::WriteUserDisconnect(const string & login,
1068 const DIR_TRAFF & up,
1069 const DIR_TRAFF & down,
1070 const DIR_TRAFF & sessionUp,
1071 const DIR_TRAFF & sessionDown,
1074 const std::string & reason) const
1076 string logStr = "Disconnect, ";
1081 stringstream sscash;
1087 sssd << sessionDown;
1091 logStr += " session upload: \'";
1092 logStr += sssu.str();
1093 logStr += "\' session download: \'";
1094 logStr += sssd.str();
1095 logStr += "\' month upload: \'";
1096 logStr += ssmu.str();
1097 logStr += "\' month download: \'";
1098 logStr += ssmd.str();
1099 logStr += "\' cash: \'";
1100 logStr += sscash.str();
1103 return WriteLogString(logStr, login);
1105 //-----------------------------------------------------------------------------
1106 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1107 const string & login) const
1111 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1112 login.c_str(), month+1, year+1900);
1114 for (int i = 0; i < DIR_NUM; i++)
1116 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1119 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1123 strprintf(¶m, " cash=%f", stat.cash);
1126 if(MysqlSetQuery(res.c_str()))
1128 errorStr = "Couldn't SaveMonthStat:\n";
1129 //errorStr += mysql_error(sock);
1135 //-----------------------------------------------------------------------------*/
1136 int MYSQL_STORE::AddAdmin(const string & login) const
1138 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1140 if(MysqlSetQuery(qbuf))
1142 errorStr = "Couldn't add admin:\n";
1143 //errorStr += mysql_error(sock);
1149 //-----------------------------------------------------------------------------*/
1150 int MYSQL_STORE::DelAdmin(const string & login) const
1152 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1154 if(MysqlSetQuery(qbuf))
1156 errorStr = "Couldn't delete admin:\n";
1157 //errorStr += mysql_error(sock);
1163 //-----------------------------------------------------------------------------*/
1164 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1166 char passwordE[2 * ADM_PASSWD_LEN + 2];
1167 char pass[ADM_PASSWD_LEN + 1];
1168 char adminPass[ADM_PASSWD_LEN + 1];
1170 memset(pass, 0, sizeof(pass));
1171 memset(adminPass, 0, sizeof(adminPass));
1174 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1176 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1177 adminPass[ADM_PASSWD_LEN - 1] = 0;
1179 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1181 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1184 pass[ADM_PASSWD_LEN - 1] = 0;
1185 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1187 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1188 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1189 "WHERE login='%s' LIMIT 1",
1201 if(MysqlSetQuery(qbuf))
1203 errorStr = "Couldn't save admin:\n";
1204 //errorStr += mysql_error(sock);
1210 //-----------------------------------------------------------------------------
1211 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1213 char pass[ADM_PASSWD_LEN + 1];
1214 char password[ADM_PASSWD_LEN + 1];
1215 char passwordE[2*ADM_PASSWD_LEN + 2];
1218 memset(pass, 0, sizeof(pass));
1219 memset(password, 0, sizeof(password));
1220 memset(passwordE, 0, sizeof(passwordE));
1226 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1228 if(MysqlGetQuery(qbuf,sock))
1230 errorStr = "Couldn't restore admin:\n";
1231 errorStr += mysql_error(sock);
1236 if (!(res=mysql_store_result(sock)))
1238 errorStr = "Couldn't restore admin:\n";
1239 errorStr += mysql_error(sock);
1244 if ( mysql_num_rows(res) == 0)
1246 mysql_free_result(res);
1247 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1252 row = mysql_fetch_row(res);
1259 mysql_free_result(res);
1260 errorStr = "Error in parameter password";
1265 memset(passwordE, 0, sizeof(passwordE));
1266 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1268 memset(pass, 0, sizeof(pass));
1270 if (passwordE[0] != 0)
1272 Decode21(pass, passwordE);
1273 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1275 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1277 DecodeString(password + 8*i, pass + 8*i, &ctx);
1285 ac->password = password;
1287 if (GetInt(row[2], &a, 0) == 0)
1288 ac->priv.userConf = a;
1291 mysql_free_result(res);
1292 errorStr = "Error in parameter ChgConf";
1297 if (GetInt(row[3], &a, 0) == 0)
1298 ac->priv.userPasswd = a;
1301 mysql_free_result(res);
1302 errorStr = "Error in parameter ChgPassword";
1307 if (GetInt(row[4], &a, 0) == 0)
1308 ac->priv.userStat = a;
1311 mysql_free_result(res);
1312 errorStr = "Error in parameter ChgStat";
1317 if (GetInt(row[5], &a, 0) == 0)
1318 ac->priv.userCash = a;
1321 mysql_free_result(res);
1322 errorStr = "Error in parameter ChgCash";
1327 if (GetInt(row[6], &a, 0) == 0)
1328 ac->priv.userAddDel = a;
1331 mysql_free_result(res);
1332 errorStr = "Error in parameter UsrAddDel";
1337 if (GetInt(row[7], &a, 0) == 0)
1338 ac->priv.tariffChg = a;
1341 mysql_free_result(res);
1342 errorStr = "Error in parameter ChgTariff";
1347 if (GetInt(row[8], &a, 0) == 0)
1348 ac->priv.adminChg = a;
1351 mysql_free_result(res);
1352 errorStr = "Error in parameter ChgAdmin";
1357 mysql_free_result(res);
1361 //-----------------------------------------------------------------------------
1362 int MYSQL_STORE::AddTariff(const string & name) const
1364 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1366 if(MysqlSetQuery(qbuf))
1368 errorStr = "Couldn't add tariff:\n";
1369 // errorStr += mysql_error(sock);
1375 //-----------------------------------------------------------------------------
1376 int MYSQL_STORE::DelTariff(const string & name) const
1378 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1380 if(MysqlSetQuery(qbuf))
1382 errorStr = "Couldn't delete tariff: ";
1383 // errorStr += mysql_error(sock);
1389 //-----------------------------------------------------------------------------
1390 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1395 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1397 if(MysqlGetQuery(qbuf,sock))
1399 errorStr = "Couldn't restore Tariff:\n";
1400 errorStr += mysql_error(sock);
1405 if (!(res=mysql_store_result(sock)))
1407 errorStr = "Couldn't restore Tariff:\n";
1408 errorStr += mysql_error(sock);
1414 td->tariffConf.name = tariffName;
1416 row = mysql_fetch_row(res);
1419 for (int i = 0; i<DIR_NUM; i++)
1421 strprintf(¶m, "Time%d", i);
1423 if (str.length() == 0)
1425 mysql_free_result(res);
1426 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1431 ParseTariffTimeStr(str.c_str(),
1432 td->dirPrice[i].hDay,
1433 td->dirPrice[i].mDay,
1434 td->dirPrice[i].hNight,
1435 td->dirPrice[i].mNight);
1437 strprintf(¶m, "PriceDayA%d", i);
1438 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1440 mysql_free_result(res);
1441 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1445 td->dirPrice[i].priceDayA /= (1024*1024);
1447 strprintf(¶m, "PriceDayB%d", i);
1448 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1450 mysql_free_result(res);
1451 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1455 td->dirPrice[i].priceDayB /= (1024*1024);
1457 strprintf(¶m, "PriceNightA%d", i);
1458 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1460 mysql_free_result(res);
1461 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1465 td->dirPrice[i].priceNightA /= (1024*1024);
1467 strprintf(¶m, "PriceNightB%d", i);
1468 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1470 mysql_free_result(res);
1471 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1475 td->dirPrice[i].priceNightB /= (1024*1024);
1477 strprintf(¶m, "Threshold%d", i);
1478 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
1480 mysql_free_result(res);
1481 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1486 strprintf(¶m, "SinglePrice%d", i);
1487 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
1489 mysql_free_result(res);
1490 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1495 strprintf(¶m, "NoDiscount%d", i);
1496 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
1498 mysql_free_result(res);
1499 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1505 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1507 mysql_free_result(res);
1508 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1513 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1515 mysql_free_result(res);
1516 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1521 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1523 mysql_free_result(res);
1524 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1529 str = row[4+8*DIR_NUM];
1530 param = "TraffType";
1532 if (str.length() == 0)
1534 mysql_free_result(res);
1535 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1540 if (!strcasecmp(str.c_str(), "up"))
1541 td->tariffConf.traffType = TRAFF_UP;
1543 if (!strcasecmp(str.c_str(), "down"))
1544 td->tariffConf.traffType = TRAFF_DOWN;
1546 if (!strcasecmp(str.c_str(), "up+down"))
1547 td->tariffConf.traffType = TRAFF_UP_DOWN;
1549 if (!strcasecmp(str.c_str(), "max"))
1550 td->tariffConf.traffType = TRAFF_MAX;
1553 mysql_free_result(res);
1554 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1559 mysql_free_result(res);
1563 //-----------------------------------------------------------------------------
1564 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1568 string res="UPDATE tariffs SET";
1570 for (int i = 0; i < DIR_NUM; i++)
1572 strprintf(¶m, " PriceDayA%d=%f,", i,
1573 td.dirPrice[i].priceDayA * pt_mega);
1576 strprintf(¶m, " PriceDayB%d=%f,", i,
1577 td.dirPrice[i].priceDayB * pt_mega);
1580 strprintf(¶m, " PriceNightA%d=%f,", i,
1581 td.dirPrice[i].priceNightA * pt_mega);
1584 strprintf(¶m, " PriceNightB%d=%f,", i,
1585 td.dirPrice[i].priceNightB * pt_mega);
1588 strprintf(¶m, " Threshold%d=%d,", i,
1589 td.dirPrice[i].threshold);
1593 strprintf(¶m, " Time%d", i);
1595 strprintf(&s, "%0d:%0d-%0d:%0d",
1596 td.dirPrice[i].hDay,
1597 td.dirPrice[i].mDay,
1598 td.dirPrice[i].hNight,
1599 td.dirPrice[i].mNight);
1601 res += (param + "='" + s + "',");
1603 strprintf(¶m, " NoDiscount%d=%d,", i,
1604 td.dirPrice[i].noDiscount);
1607 strprintf(¶m, " SinglePrice%d=%d,", i,
1608 td.dirPrice[i].singlePrice);
1612 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1615 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1618 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1621 switch (td.tariffConf.traffType)
1624 res += " TraffType='up'";
1627 res += " TraffType='down'";
1630 res += " TraffType='up+down'";
1633 res += " TraffType='max'";
1636 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1639 if(MysqlSetQuery(res.c_str()))
1641 errorStr = "Couldn't save admin:\n";
1642 //errorStr += mysql_error(sock);
1648 //-----------------------------------------------------------------------------
1649 int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1651 const string & login) const
1653 string res, stTime, endTime, tempStr;
1660 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1668 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1670 if (!(sock=MysqlConnect())){
1675 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1677 errorStr = "Couldn't get table " + tempStr + ":\n";
1678 errorStr += mysql_error(sock);
1683 unsigned int num_rows = mysql_num_rows(result);
1685 mysql_free_result(result);
1689 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1690 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1691 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1692 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1693 lt->tm_mon+1, lt->tm_year+1900);
1695 if(MysqlQuery(qbuf,sock))
1697 errorStr = "Couldn't create WriteDetailedStat table:\n";
1698 errorStr += mysql_error(sock);
1707 lt1 = localtime(&lastStat);
1716 lt2 = localtime(&t);
1722 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1723 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1725 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1726 "day=%d,startTime='%s',endTime='%s',",
1727 lt->tm_mon+1, lt->tm_year+1900,
1734 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1735 stIter = statTree.begin();
1737 while (stIter != statTree.end())
1739 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1740 inet_ntostring(stIter->first.ip).c_str(),
1742 stIter->second.down,
1747 if( MysqlQuery((res+tempStr).c_str(),sock) )
1749 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1750 errorStr += mysql_error(sock);
1755 result=mysql_store_result(sock);
1757 mysql_free_result(result);
1764 //-----------------------------------------------------------------------------
1765 int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
1769 gettimeofday(&tv, NULL);
1771 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1773 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1775 (long long)msg->header.id
1778 if(MysqlSetQuery(qbuf))
1780 errorStr = "Couldn't add message:\n";
1781 //errorStr += mysql_error(sock);
1785 return EditMessage(*msg, login);
1787 //-----------------------------------------------------------------------------
1788 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1792 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1793 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1794 "WHERE login='%s' AND id=%lld LIMIT 1",
1796 msg.header.lastSendTime,
1797 msg.header.creationTime,
1798 msg.header.showTime,
1800 msg.header.repeatPeriod,
1801 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1803 (long long)msg.header.id
1806 if(MysqlSetQuery(res.c_str()))
1808 errorStr = "Couldn't edit message:\n";
1809 //errorStr += mysql_error(sock);
1815 //-----------------------------------------------------------------------------
1816 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1822 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1823 login.c_str(), static_cast<unsigned long long>(id));
1825 if(MysqlGetQuery(qbuf,sock))
1827 errorStr = "Couldn't GetMessage:\n";
1828 errorStr += mysql_error(sock);
1833 if (!(res=mysql_store_result(sock)))
1835 errorStr = "Couldn't GetMessage:\n";
1836 errorStr += mysql_error(sock);
1841 row = mysql_fetch_row(res);
1843 if(row[2]&&str2x(row[2], msg->header.type))
1845 mysql_free_result(res);
1846 errorStr = "Invalid value in message header for user: " + login;
1851 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1853 mysql_free_result(res);
1854 errorStr = "Invalid value in message header for user: " + login;
1859 if(row[4] && str2x(row[4], msg->header.creationTime))
1861 mysql_free_result(res);
1862 errorStr = "Invalid value in message header for user: " + login;
1867 if(row[5] && str2x(row[5], msg->header.showTime))
1869 mysql_free_result(res);
1870 errorStr = "Invalid value in message header for user: " + login;
1875 if(row[6] && str2x(row[6], msg->header.repeat))
1877 mysql_free_result(res);
1878 errorStr = "Invalid value in message header for user: " + login;
1883 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1885 mysql_free_result(res);
1886 errorStr = "Invalid value in message header for user: " + login;
1891 msg->header.id = id;
1894 mysql_free_result(res);
1898 //-----------------------------------------------------------------------------
1899 int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
1901 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1902 login.c_str(),(long long)id);
1904 if(MysqlSetQuery(qbuf))
1906 errorStr = "Couldn't delete Message:\n";
1907 //errorStr += mysql_error(sock);
1913 //-----------------------------------------------------------------------------
1914 int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1919 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
1921 if(MysqlGetQuery(qbuf,sock))
1923 errorStr = "Couldn't GetMessageHdrs:\n";
1924 errorStr += mysql_error(sock);
1929 if (!(res=mysql_store_result(sock)))
1931 errorStr = "Couldn't GetMessageHdrs:\n";
1932 errorStr += mysql_error(sock);
1937 unsigned int i, num_rows = mysql_num_rows(res);
1938 long long int unsigned id = 0;
1940 for (i=0; i<num_rows; i++)
1942 row = mysql_fetch_row(res);
1943 if (str2x(row[1], id))
1948 if(str2x(row[2], hdr.type))
1952 if(str2x(row[3], hdr.lastSendTime))
1956 if(str2x(row[4], hdr.creationTime))
1960 if(str2x(row[5], hdr.showTime))
1964 if(str2x(row[6], hdr.repeat))
1968 if(str2x(row[7], hdr.repeatPeriod))
1972 hdrsList->push_back(hdr);
1975 mysql_free_result(res);
1979 //-----------------------------------------------------------------------------
1981 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
1984 int ret=MysqlGetQuery(Query,sock);
1988 //-----------------------------------------------------------------------------
1989 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
1990 if (!(sock=MysqlConnect())) {
1993 return MysqlQuery(Query,sock);
1995 //-----------------------------------------------------------------------------
1996 MYSQL * MYSQL_STORE::MysqlConnect() const {
1998 if ( !(sock=mysql_init(NULL)) ){
1999 errorStr= "mysql init susck\n";
2002 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2003 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2006 errorStr = "Couldn't connect to mysql engine! With error:\n";
2007 errorStr += mysql_error(sock);
2011 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2012 errorStr = "Database lost !\n";
2018 //-----------------------------------------------------------------------------