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"),
163 //-----------------------------------------------------------------------------
164 int MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
168 if( (ret = mysql_query(sock,sQuery)) )
170 for(i=0; i<RepitTimes; i++)
172 if( (ret = mysql_query(sock,sQuery)) )
173 ;//need to send error result
181 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
184 int MYSQL_STORE::ParseSettings()
186 int ret = storeSettings.ParseSettings(settings);
191 errorStr = storeSettings.GetStrError();
194 if(storeSettings.GetDBPassword().length() == 0)
196 errorStr = "Database password must be not empty. Please read Manual.";
200 if (!(sock = mysql_real_connect(&mysql,storeSettings.GetDBHost().c_str(),
201 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
204 errorStr = "Couldn't connect to mysql engine! With error:\n";
205 errorStr += mysql_error(&mysql);
211 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
213 string res = "CREATE DATABASE " + storeSettings.GetDBName();
215 if(MysqlQuery(res.c_str(),sock))
217 errorStr = "Couldn't create database! With error:\n";
218 errorStr += mysql_error(sock);
224 if(mysql_select_db(sock, storeSettings.GetDBName().c_str()))
226 errorStr = "Couldn't select database! With error:\n";
227 errorStr += mysql_error(sock);
231 ret = CheckAllTables(sock);
235 ret = CheckAllTables(sock);
242 //-----------------------------------------------------------------------------
243 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
247 if (!(result=mysql_list_tables(sock,str.c_str() )))
249 errorStr = "Couldn't get tables list With error:\n";
250 errorStr += mysql_error(sock);
255 unsigned int num_rows = mysql_num_rows(result);
258 mysql_free_result(result);
260 return (num_rows == 1);
262 //-----------------------------------------------------------------------------
263 int MYSQL_STORE::CheckAllTables(MYSQL * sock)
265 //admins-----------------------------------------------------------------------
266 if(!IsTablePresent("admins",sock))
268 sprintf(qbuf,"CREATE TABLE admins (login VARCHAR(40) DEFAULT '' PRIMARY KEY,"\
269 "password VARCHAR(150) DEFAULT '*',ChgConf TINYINT DEFAULT 0,"\
270 "ChgPassword TINYINT DEFAULT 0,ChgStat TINYINT DEFAULT 0,"\
271 "ChgCash TINYINT DEFAULT 0,UsrAddDel TINYINT DEFAULT 0,"\
272 "ChgTariff TINYINT DEFAULT 0,ChgAdmin TINYINT DEFAULT 0)");
274 if(MysqlQuery(qbuf,sock))
276 errorStr = "Couldn't create admin table list With error:\n";
277 errorStr += mysql_error(sock);
282 sprintf(qbuf,"INSERT INTO admins SET login='admin',"\
283 "password='geahonjehjfofnhammefahbbbfbmpkmkmmefahbbbfbmpkmkmmefahbbbfbmpkmkaa',"\
284 "ChgConf=1,ChgPassword=1,ChgStat=1,ChgCash=1,UsrAddDel=1,ChgTariff=1,ChgAdmin=1");
286 if(MysqlQuery(qbuf,sock))
288 errorStr = "Couldn't create default admin. With error:\n";
289 errorStr += mysql_error(sock);
295 //tariffs-----------------------------------------------------------------------
297 if(!IsTablePresent("tariffs",sock))
299 res = "CREATE TABLE tariffs (name VARCHAR(40) DEFAULT '' PRIMARY KEY,";
301 for (int i = 0; i < DIR_NUM; i++)
303 strprintf(¶m, " PriceDayA%d DOUBLE DEFAULT 0.0,", i);
306 strprintf(¶m, " PriceDayB%d DOUBLE DEFAULT 0.0,", i);
309 strprintf(¶m, " PriceNightA%d DOUBLE DEFAULT 0.0,", i);
312 strprintf(¶m, " PriceNightB%d DOUBLE DEFAULT 0.0,", i);
315 strprintf(¶m, " Threshold%d INT DEFAULT 0,", i);
318 strprintf(¶m, " Time%d VARCHAR(15) DEFAULT '0:0-0:0',", i);
321 strprintf(¶m, " NoDiscount%d INT DEFAULT 0,", i);
324 strprintf(¶m, " SinglePrice%d INT DEFAULT 0,", i);
328 res += "PassiveCost DOUBLE DEFAULT 0.0, Fee DOUBLE DEFAULT 0.0,"\
329 "Free DOUBLE DEFAULT 0.0, TraffType VARCHAR(10) DEFAULT '')";
331 if(MysqlQuery(res.c_str(),sock))
333 errorStr = "Couldn't create tariffs table list With error:\n";
334 errorStr += mysql_error(sock);
339 res = "INSERT INTO tariffs SET name='tariff',";
341 for (int i = 0; i < DIR_NUM; i++)
343 strprintf(¶m, " NoDiscount%d=1,", i);
346 strprintf(¶m, " Threshold%d=0,", i);
349 strprintf(¶m, " Time%d='0:0-0:0',", i);
354 strprintf(¶m, " SinglePrice%d=0,", i);
360 strprintf(¶m, " PriceDayA%d=0.0,", i);
365 strprintf(¶m, " PriceDayB%d=0.0,", i);
371 strprintf(¶m, " PriceNightA%d=0.0,", i);
376 strprintf(¶m, " PriceNightB%d=0.0,", i);
381 res += "PassiveCost=0.0, Fee=10.0, Free=0,"\
382 "SinglePrice0=1, SinglePrice1=1,PriceDayA1=0.75,PriceDayB1=0.75,"\
383 "PriceNightA0=1.0,PriceNightB0=1.0,TraffType='up+down'";
385 if(MysqlQuery(res.c_str(),sock))
387 errorStr = "Couldn't create default tariff. With error:\n";
388 errorStr += mysql_error(sock);
394 //users-----------------------------------------------------------------------
395 if(!IsTablePresent("users",sock))
397 res = "CREATE TABLE users (login VARCHAR(50) NOT NULL DEFAULT '' PRIMARY KEY, Password VARCHAR(150) NOT NULL DEFAULT '*',"\
398 "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 '',"\
399 "Address VARCHAR(254) NOT NULL DEFAULT '',Phone VARCHAR(128) NOT NULL DEFAULT '',Email VARCHAR(50) NOT NULL DEFAULT '',"\
400 "Note TEXT NOT NULL,RealName VARCHAR(254) NOT NULL DEFAULT '',StgGroup VARCHAR(40) NOT NULL DEFAULT '',"\
401 "Credit DOUBLE DEFAULT 0, TariffChange VARCHAR(40) NOT NULL DEFAULT '',";
403 for (int i = 0; i < USERDATA_NUM; i++)
405 strprintf(¶m, " Userdata%d VARCHAR(254) NOT NULL,", i);
409 param = " CreditExpire INT(11) DEFAULT 0,";
412 strprintf(¶m, " IP VARCHAR(254) DEFAULT '*',");
415 for (int i = 0; i < DIR_NUM; i++)
417 strprintf(¶m, " D%d BIGINT(30) DEFAULT 0,", i);
420 strprintf(¶m, " U%d BIGINT(30) DEFAULT 0,", i);
424 strprintf(¶m, "Cash DOUBLE DEFAULT 0,FreeMb DOUBLE DEFAULT 0,LastCashAdd DOUBLE DEFAULT 0,"\
425 "LastCashAddTime INT(11) DEFAULT 0,PassiveTime INT(11) DEFAULT 0,LastActivityTime INT(11) DEFAULT 0,"\
426 "NAS VARCHAR(17) NOT NULL, INDEX (AlwaysOnline), INDEX (IP), INDEX (Address),"\
427 " INDEX (Tariff),INDEX (Phone),INDEX (Email),INDEX (RealName))");
430 if(MysqlQuery(res.c_str(),sock))
432 errorStr = "Couldn't create users table list With error:\n";
433 errorStr += mysql_error(sock);
434 errorStr += "\n\n" + res;
439 res = "INSERT INTO users SET login='test',Address='',AlwaysOnline=0,"\
440 "Credit=0.0,CreditExpire=0,Down=0,Email='',DisabledDetailStat=0,"\
441 "StgGroup='',IP='192.168.1.1',Note='',Passive=0,Password='123456',"\
442 "Phone='', RealName='',Tariff='tariff',TariffChange='',Userdata0='',"\
445 for (int i = 0; i < DIR_NUM; i++)
447 strprintf(¶m, " D%d=0,", i);
450 strprintf(¶m, " U%d=0,", i);
454 res += "Cash=10.0,FreeMb=0.0,LastActivityTime=0,LastCashAdd=0,"\
455 "LastCashAddTime=0, PassiveTime=0";
457 if(MysqlQuery(res.c_str(),sock))
459 errorStr = "Couldn't create default user. With error:\n";
460 errorStr += mysql_error(sock);
466 //logs-----------------------------------------------------------------------
467 if(!IsTablePresent("logs"))
469 sprintf(qbuf,"CREATE TABLE logs (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)");
473 errorStr = "Couldn't create admin table list With error:\n";
474 errorStr += mysql_error(sock);
479 //messages---------------------------------------------------------------------
480 if(!IsTablePresent("messages",sock))
482 sprintf(qbuf,"CREATE TABLE messages (login VARCHAR(40) DEFAULT '', id BIGINT, "\
483 "type INT, lastSendTime INT, creationTime INT, showTime INT,"\
484 "stgRepeat INT, repeatPeriod INT, text TEXT)");
486 if(MysqlQuery(qbuf,sock))
488 errorStr = "Couldn't create messages table. With error:\n";
489 errorStr += mysql_error(sock);
495 //month_stat-------------------------------------------------------------------
496 if(!IsTablePresent("stat",sock))
498 res = "CREATE TABLE stat (login VARCHAR(50), month TINYINT, year SMALLINT,";
500 for (int i = 0; i < DIR_NUM; i++)
502 strprintf(¶m, " U%d BIGINT,", i);
505 strprintf(¶m, " D%d BIGINT,", i);
509 res += " cash DOUBLE, INDEX (login))";
511 if(MysqlQuery(res.c_str(),sock))
513 errorStr = "Couldn't create stat table. With error:\n";
514 errorStr += mysql_error(sock);
522 //-----------------------------------------------------------------------------
523 //-----------------------------------------------------------------------------
525 int MYSQL_STORE::GetAllParams(vector<string> * ParamList,
526 const string & table, const string & name) const
535 sprintf(qbuf,"SELECT %s FROM %s", name.c_str(), table.c_str());
537 if(MysqlGetQuery(qbuf,sock))
539 errorStr = "Couldn't GetAllParams Query for: ";
540 errorStr += name + " - " + table + "\n";
541 errorStr += mysql_error(sock);
546 if (!(res=mysql_store_result(sock)))
548 errorStr = "Couldn't GetAllParams Results for: ";
549 errorStr += name + " - " + table + "\n";
550 errorStr += mysql_error(sock);
554 num = mysql_num_rows(res);
558 row = mysql_fetch_row(res);
559 ParamList->push_back(row[0]);
562 mysql_free_result(res);
568 //-----------------------------------------------------------------------------
569 int MYSQL_STORE::GetUsersList(vector<string> * usersList) const
571 if(GetAllParams(usersList, "users", "login"))
576 //-----------------------------------------------------------------------------
577 int MYSQL_STORE::GetAdminsList(vector<string> * adminsList) const
579 if(GetAllParams(adminsList, "admins", "login"))
584 //-----------------------------------------------------------------------------
585 int MYSQL_STORE::GetTariffsList(vector<string> * tariffsList) const
587 if(GetAllParams(tariffsList, "tariffs", "name"))
592 //-----------------------------------------------------------------------------
593 int MYSQL_STORE::AddUser(const string & login) const
595 sprintf(qbuf,"INSERT INTO users SET login='%s'", login.c_str());
597 if(MysqlSetQuery(qbuf))
599 errorStr = "Couldn't add user:\n";
600 //errorStr += mysql_error(sock);
606 //-----------------------------------------------------------------------------
607 int MYSQL_STORE::DelUser(const string & login) const
609 sprintf(qbuf,"DELETE FROM users WHERE login='%s' LIMIT 1", login.c_str());
611 if(MysqlSetQuery(qbuf))
613 errorStr = "Couldn't delete user:\n";
614 //errorStr += mysql_error(sock);
620 //-----------------------------------------------------------------------------
621 int MYSQL_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
628 query = "SELECT login, Password, Passive, Down, DisabledDetailStat, \
629 AlwaysOnline, Tariff, Address, Phone, Email, Note, \
630 RealName, StgGroup, Credit, TariffChange, ";
632 for (int i = 0; i < USERDATA_NUM; i++)
634 sprintf(qbuf, "Userdata%d, ", i);
638 query += "CreditExpire, IP FROM users WHERE login='";
639 query += login + "' LIMIT 1";
641 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
643 if(MysqlGetQuery(query.c_str(),sock))
645 errorStr = "Couldn't restore Tariff(on query):\n";
646 errorStr += mysql_error(sock);
651 if (!(res=mysql_store_result(sock)))
653 errorStr = "Couldn't restore Tariff(on getting result):\n";
654 errorStr += mysql_error(sock);
659 if (mysql_num_rows(res) != 1)
661 errorStr = "User not found";
666 row = mysql_fetch_row(res);
670 conf->password = row[1];
672 if (conf->password.empty())
674 mysql_free_result(res);
675 errorStr = "User \'" + login + "\' password is blank.";
680 if (GetInt(row[2],&conf->passive, 0) != 0)
682 mysql_free_result(res);
683 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
688 if (GetInt(row[3], &conf->disabled, 0) != 0)
690 mysql_free_result(res);
691 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
696 if (GetInt(row[4], &conf->disabledDetailStat, 0) != 0)
698 mysql_free_result(res);
699 errorStr = "User \'" + login + "\' data not read. Parameter DisabledDetailStat.";
704 if (GetInt(row[5], &conf->alwaysOnline, 0) != 0)
706 mysql_free_result(res);
707 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
712 conf->tariffName = row[6];
714 if (conf->tariffName.empty())
716 mysql_free_result(res);
717 errorStr = "User \'" + login + "\' tariff is blank.";
722 conf->address = row[7];
723 conf->phone = row[8];
724 conf->email = row[9];
725 conf->note = row[10];
726 conf->realName = row[11];
727 conf->group = row[12];
729 if (GetDouble(row[13], &conf->credit, 0) != 0)
731 mysql_free_result(res);
732 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
737 conf->nextTariff = row[14];
739 for (int i = 0; i < USERDATA_NUM; i++)
741 conf->userdata[i] = row[15+i];
744 GetTime(row[15+USERDATA_NUM], &conf->creditExpire, 0);
746 string ipStr = row[16+USERDATA_NUM];
752 catch (const string & s)
754 mysql_free_result(res);
755 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
761 mysql_free_result(res);
766 //-----------------------------------------------------------------------------
767 int MYSQL_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
777 for (int i = 0; i < DIR_NUM; i++)
779 sprintf(qbuf, "D%d, U%d, ", i, i);
783 query += "Cash, FreeMb, LastCashAdd, LastCashAddTime, PassiveTime, LastActivityTime \
784 FROM users WHERE login = '";
785 query += login + "'";
787 //sprintf(qbuf,"SELECT * FROM users WHERE login='%s' LIMIT 1", login.c_str());
789 if(MysqlGetQuery(query.c_str() ,sock))
791 errorStr = "Couldn't restore UserStat(on query):\n";
792 errorStr += mysql_error(sock);
797 if (!(res=mysql_store_result(sock)))
799 errorStr = "Couldn't restore UserStat(on getting result):\n";
800 errorStr += mysql_error(sock);
805 row = mysql_fetch_row(res);
807 unsigned int startPos=0;
811 for (int i = 0; i < DIR_NUM; i++)
814 sprintf(s, "D%d", i);
815 if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
817 mysql_free_result(res);
818 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
822 stat->down[i] = traff;
824 sprintf(s, "U%d", i);
825 if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
827 mysql_free_result(res);
828 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
835 startPos += (2*DIR_NUM);
837 if (GetDouble(row[startPos], &stat->cash, 0) != 0)
839 mysql_free_result(res);
840 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
845 if (GetDouble(row[startPos+1],&stat->freeMb, 0) != 0)
847 mysql_free_result(res);
848 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
853 if (GetDouble(row[startPos+2], &stat->lastCashAdd, 0) != 0)
855 mysql_free_result(res);
856 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
861 if (GetTime(row[startPos+3], &stat->lastCashAddTime, 0) != 0)
863 mysql_free_result(res);
864 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
869 if (GetTime(row[startPos+4], &stat->passiveTime, 0) != 0)
871 mysql_free_result(res);
872 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
877 if (GetTime(row[startPos+5], &stat->lastActivityTime, 0) != 0)
879 mysql_free_result(res);
880 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
885 mysql_free_result(res);
889 //-----------------------------------------------------------------------------
890 int MYSQL_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
895 strprintf(&res,"UPDATE users SET Password='%s', Passive=%d, Down=%d, DisabledDetailStat = %d, "\
896 "AlwaysOnline=%d, Tariff='%s', Address='%s', Phone='%s', Email='%s', "\
897 "Note='%s', RealName='%s', StgGroup='%s', Credit=%f, TariffChange='%s', ",
898 conf.password.c_str(),
901 conf.disabledDetailStat,
903 conf.tariffName.c_str(),
904 (ReplaceStr(conf.address,badSyms,repSym)).c_str(),
905 (ReplaceStr(conf.phone,badSyms,repSym)).c_str(),
906 (ReplaceStr(conf.email,badSyms,repSym)).c_str(),
907 (ReplaceStr(conf.note,badSyms,repSym)).c_str(),
908 (ReplaceStr(conf.realName,badSyms,repSym)).c_str(),
909 (ReplaceStr(conf.group,badSyms,repSym)).c_str(),
911 conf.nextTariff.c_str()
914 for (int i = 0; i < USERDATA_NUM; i++)
916 strprintf(¶m, " Userdata%d='%s',", i,
917 (ReplaceStr(conf.userdata[i],badSyms,repSym)).c_str());
921 strprintf(¶m, " CreditExpire=%d,", conf.creditExpire);
927 strprintf(¶m, " IP='%s'", ipStr.str().c_str());
930 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
933 if(MysqlSetQuery(res.c_str()))
935 errorStr = "Couldn't save user conf:\n";
936 //errorStr += mysql_error(sock);
942 //-----------------------------------------------------------------------------
943 int MYSQL_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
948 res = "UPDATE users SET";
950 for (int i = 0; i < DIR_NUM; i++)
952 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
955 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
959 strprintf(¶m, " Cash=%f, FreeMb=%f, LastCashAdd=%f, LastCashAddTime=%d,"\
960 " PassiveTime=%d, LastActivityTime=%d",
964 stat.lastCashAddTime,
966 stat.lastActivityTime
970 strprintf(¶m, " WHERE login='%s' LIMIT 1", login.c_str());
973 if(MysqlSetQuery(res.c_str()))
975 errorStr = "Couldn't save user stat:\n";
976 // errorStr += mysql_error(sock);
982 //-----------------------------------------------------------------------------
983 int MYSQL_STORE::WriteLogString(const string & str, const string & login) const
994 strprintf(&tempStr, "logs_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
995 if (!(sock=MysqlConnect())){
996 errorStr = "Couldn't connect to Server";
999 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1001 errorStr = "Couldn't get table " + tempStr + ":\n";
1002 errorStr += mysql_error(sock);
1007 unsigned int num_rows = mysql_num_rows(result);
1009 mysql_free_result(result);
1013 sprintf(qbuf,"CREATE TABLE logs_%02d_%4d (unid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(40),text TEXT)",
1014 lt->tm_mon+1, lt->tm_year+1900);
1016 if(MysqlQuery(qbuf,sock))
1018 errorStr = "Couldn't create WriteDetailedStat table:\n";
1019 errorStr += mysql_error(sock);
1025 strprintf(&res, "%s -- %s",LogDate(t), str.c_str());
1029 strprintf(&send,"INSERT INTO logs_%02d_%4d SET login='%s', text='%s'",
1030 lt->tm_mon+1, lt->tm_year+1900,
1031 login.c_str(), (ReplaceStr(res,badSyms,repSym)).c_str());
1033 if(MysqlQuery(send.c_str(),sock))
1035 errorStr = "Couldn't write log string:\n";
1036 errorStr += mysql_error(sock);
1044 //-----------------------------------------------------------------------------
1045 int MYSQL_STORE::WriteUserChgLog(const string & login,
1046 const string & admLogin,
1048 const string & paramName,
1049 const string & oldValue,
1050 const string & newValue,
1051 const string & message) const
1053 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1054 + paramName + "\' parameter changed from \'" + oldValue +
1055 "\' to \'" + newValue + "\'. " + message;
1057 return WriteLogString(userLogMsg, login);
1059 //-----------------------------------------------------------------------------
1060 int MYSQL_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1062 string logStr = "Connect, " + inet_ntostring(ip);
1063 return WriteLogString(logStr, login);
1065 //-----------------------------------------------------------------------------
1066 int MYSQL_STORE::WriteUserDisconnect(const string & login,
1067 const DIR_TRAFF & up,
1068 const DIR_TRAFF & down,
1069 const DIR_TRAFF & sessionUp,
1070 const DIR_TRAFF & sessionDown,
1073 const std::string & reason) const
1075 string logStr = "Disconnect, ";
1080 stringstream sscash;
1086 sssd << sessionDown;
1090 logStr += " session upload: \'";
1091 logStr += sssu.str();
1092 logStr += "\' session download: \'";
1093 logStr += sssd.str();
1094 logStr += "\' month upload: \'";
1095 logStr += ssmu.str();
1096 logStr += "\' month download: \'";
1097 logStr += ssmd.str();
1098 logStr += "\' cash: \'";
1099 logStr += sscash.str();
1102 return WriteLogString(logStr, login);
1104 //-----------------------------------------------------------------------------
1105 int MYSQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year,
1106 const string & login) const
1110 strprintf(&res, "INSERT INTO stat SET login='%s', month=%d, year=%d,",
1111 login.c_str(), month+1, year+1900);
1113 for (int i = 0; i < DIR_NUM; i++)
1115 strprintf(¶m, " U%d=%lld,", i, stat.up[i]);
1118 strprintf(¶m, " D%d=%lld,", i, stat.down[i]);
1122 strprintf(¶m, " cash=%f", stat.cash);
1125 if(MysqlSetQuery(res.c_str()))
1127 errorStr = "Couldn't SaveMonthStat:\n";
1128 //errorStr += mysql_error(sock);
1134 //-----------------------------------------------------------------------------*/
1135 int MYSQL_STORE::AddAdmin(const string & login) const
1137 sprintf(qbuf,"INSERT INTO admins SET login='%s'", login.c_str());
1139 if(MysqlSetQuery(qbuf))
1141 errorStr = "Couldn't add admin:\n";
1142 //errorStr += mysql_error(sock);
1148 //-----------------------------------------------------------------------------*/
1149 int MYSQL_STORE::DelAdmin(const string & login) const
1151 sprintf(qbuf,"DELETE FROM admins where login='%s' LIMIT 1", login.c_str());
1153 if(MysqlSetQuery(qbuf))
1155 errorStr = "Couldn't delete admin:\n";
1156 //errorStr += mysql_error(sock);
1162 //-----------------------------------------------------------------------------*/
1163 int MYSQL_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1165 char passwordE[2 * ADM_PASSWD_LEN + 2];
1166 char pass[ADM_PASSWD_LEN + 1];
1167 char adminPass[ADM_PASSWD_LEN + 1];
1169 memset(pass, 0, sizeof(pass));
1170 memset(adminPass, 0, sizeof(adminPass));
1173 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1175 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1176 adminPass[ADM_PASSWD_LEN - 1] = 0;
1178 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1180 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1183 pass[ADM_PASSWD_LEN - 1] = 0;
1184 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1186 sprintf(qbuf,"UPDATE admins SET password='%s', ChgConf=%d, ChgPassword=%d, "\
1187 "ChgStat=%d, ChgCash=%d, UsrAddDel=%d, ChgTariff=%d, ChgAdmin=%d "\
1188 "WHERE login='%s' LIMIT 1",
1200 if(MysqlSetQuery(qbuf))
1202 errorStr = "Couldn't save admin:\n";
1203 //errorStr += mysql_error(sock);
1209 //-----------------------------------------------------------------------------
1210 int MYSQL_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1212 char pass[ADM_PASSWD_LEN + 1];
1213 char password[ADM_PASSWD_LEN + 1];
1214 char passwordE[2*ADM_PASSWD_LEN + 2];
1217 memset(pass, 0, sizeof(pass));
1218 memset(password, 0, sizeof(password));
1219 memset(passwordE, 0, sizeof(passwordE));
1225 sprintf(qbuf,"SELECT * FROM admins WHERE login='%s' LIMIT 1", login.c_str());
1227 if(MysqlGetQuery(qbuf,sock))
1229 errorStr = "Couldn't restore admin:\n";
1230 errorStr += mysql_error(sock);
1235 if (!(res=mysql_store_result(sock)))
1237 errorStr = "Couldn't restore admin:\n";
1238 errorStr += mysql_error(sock);
1243 if ( mysql_num_rows(res) == 0)
1245 mysql_free_result(res);
1246 errorStr = "Couldn't restore admin as couldn't found him in table.\n";
1251 row = mysql_fetch_row(res);
1258 mysql_free_result(res);
1259 errorStr = "Error in parameter password";
1264 memset(passwordE, 0, sizeof(passwordE));
1265 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1267 memset(pass, 0, sizeof(pass));
1269 if (passwordE[0] != 0)
1271 Decode21(pass, passwordE);
1272 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1274 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1276 DecodeString(password + 8*i, pass + 8*i, &ctx);
1284 ac->password = password;
1286 if (GetInt(row[2], &a, 0) == 0)
1287 ac->priv.userConf = a;
1290 mysql_free_result(res);
1291 errorStr = "Error in parameter ChgConf";
1296 if (GetInt(row[3], &a, 0) == 0)
1297 ac->priv.userPasswd = a;
1300 mysql_free_result(res);
1301 errorStr = "Error in parameter ChgPassword";
1306 if (GetInt(row[4], &a, 0) == 0)
1307 ac->priv.userStat = a;
1310 mysql_free_result(res);
1311 errorStr = "Error in parameter ChgStat";
1316 if (GetInt(row[5], &a, 0) == 0)
1317 ac->priv.userCash = a;
1320 mysql_free_result(res);
1321 errorStr = "Error in parameter ChgCash";
1326 if (GetInt(row[6], &a, 0) == 0)
1327 ac->priv.userAddDel = a;
1330 mysql_free_result(res);
1331 errorStr = "Error in parameter UsrAddDel";
1336 if (GetInt(row[7], &a, 0) == 0)
1337 ac->priv.tariffChg = a;
1340 mysql_free_result(res);
1341 errorStr = "Error in parameter ChgTariff";
1346 if (GetInt(row[8], &a, 0) == 0)
1347 ac->priv.adminChg = a;
1350 mysql_free_result(res);
1351 errorStr = "Error in parameter ChgAdmin";
1356 mysql_free_result(res);
1360 //-----------------------------------------------------------------------------
1361 int MYSQL_STORE::AddTariff(const string & name) const
1363 sprintf(qbuf,"INSERT INTO tariffs SET name='%s'", name.c_str());
1365 if(MysqlSetQuery(qbuf))
1367 errorStr = "Couldn't add tariff:\n";
1368 // errorStr += mysql_error(sock);
1374 //-----------------------------------------------------------------------------
1375 int MYSQL_STORE::DelTariff(const string & name) const
1377 sprintf(qbuf,"DELETE FROM tariffs WHERE name='%s' LIMIT 1", name.c_str());
1379 if(MysqlSetQuery(qbuf))
1381 errorStr = "Couldn't delete tariff: ";
1382 // errorStr += mysql_error(sock);
1388 //-----------------------------------------------------------------------------
1389 int MYSQL_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1394 sprintf(qbuf,"SELECT * FROM tariffs WHERE name='%s' LIMIT 1", tariffName.c_str());
1396 if(MysqlGetQuery(qbuf,sock))
1398 errorStr = "Couldn't restore Tariff:\n";
1399 errorStr += mysql_error(sock);
1404 if (!(res=mysql_store_result(sock)))
1406 errorStr = "Couldn't restore Tariff:\n";
1407 errorStr += mysql_error(sock);
1413 td->tariffConf.name = tariffName;
1415 row = mysql_fetch_row(res);
1418 for (int i = 0; i<DIR_NUM; i++)
1420 strprintf(¶m, "Time%d", i);
1422 if (str.length() == 0)
1424 mysql_free_result(res);
1425 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1430 ParseTariffTimeStr(str.c_str(),
1431 td->dirPrice[i].hDay,
1432 td->dirPrice[i].mDay,
1433 td->dirPrice[i].hNight,
1434 td->dirPrice[i].mNight);
1436 strprintf(¶m, "PriceDayA%d", i);
1437 if (GetDouble(row[1+i*8], &td->dirPrice[i].priceDayA, 0.0) < 0)
1439 mysql_free_result(res);
1440 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1444 td->dirPrice[i].priceDayA /= (1024*1024);
1446 strprintf(¶m, "PriceDayB%d", i);
1447 if (GetDouble(row[2+i*8], &td->dirPrice[i].priceDayB, 0.0) < 0)
1449 mysql_free_result(res);
1450 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1454 td->dirPrice[i].priceDayB /= (1024*1024);
1456 strprintf(¶m, "PriceNightA%d", i);
1457 if (GetDouble(row[3+i*8], &td->dirPrice[i].priceNightA, 0.0) < 0)
1459 mysql_free_result(res);
1460 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1464 td->dirPrice[i].priceNightA /= (1024*1024);
1466 strprintf(¶m, "PriceNightB%d", i);
1467 if (GetDouble(row[4+i*8], &td->dirPrice[i].priceNightB, 0.0) < 0)
1469 mysql_free_result(res);
1470 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1474 td->dirPrice[i].priceNightB /= (1024*1024);
1476 strprintf(¶m, "Threshold%d", i);
1477 if (GetInt(row[5+i*8], &td->dirPrice[i].threshold, 0) < 0)
1479 mysql_free_result(res);
1480 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1485 strprintf(¶m, "SinglePrice%d", i);
1486 if (GetInt(row[8+i*8], &td->dirPrice[i].singlePrice, 0) < 0)
1488 mysql_free_result(res);
1489 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1494 strprintf(¶m, "NoDiscount%d", i);
1495 if (GetInt(row[7+i*8], &td->dirPrice[i].noDiscount, 0) < 0)
1497 mysql_free_result(res);
1498 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1504 if (GetDouble(row[2+8*DIR_NUM], &td->tariffConf.fee, 0.0) < 0)
1506 mysql_free_result(res);
1507 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1512 if (GetDouble(row[3+8*DIR_NUM], &td->tariffConf.free, 0.0) < 0)
1514 mysql_free_result(res);
1515 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1520 if (GetDouble(row[1+8*DIR_NUM], &td->tariffConf.passiveCost, 0.0) < 0)
1522 mysql_free_result(res);
1523 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1528 str = row[4+8*DIR_NUM];
1529 param = "TraffType";
1531 if (str.length() == 0)
1533 mysql_free_result(res);
1534 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1539 if (!strcasecmp(str.c_str(), "up"))
1540 td->tariffConf.traffType = TRAFF_UP;
1542 if (!strcasecmp(str.c_str(), "down"))
1543 td->tariffConf.traffType = TRAFF_DOWN;
1545 if (!strcasecmp(str.c_str(), "up+down"))
1546 td->tariffConf.traffType = TRAFF_UP_DOWN;
1548 if (!strcasecmp(str.c_str(), "max"))
1549 td->tariffConf.traffType = TRAFF_MAX;
1552 mysql_free_result(res);
1553 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1558 mysql_free_result(res);
1562 //-----------------------------------------------------------------------------
1563 int MYSQL_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1567 string res="UPDATE tariffs SET";
1569 for (int i = 0; i < DIR_NUM; i++)
1571 strprintf(¶m, " PriceDayA%d=%f,", i,
1572 td.dirPrice[i].priceDayA * pt_mega);
1575 strprintf(¶m, " PriceDayB%d=%f,", i,
1576 td.dirPrice[i].priceDayB * pt_mega);
1579 strprintf(¶m, " PriceNightA%d=%f,", i,
1580 td.dirPrice[i].priceNightA * pt_mega);
1583 strprintf(¶m, " PriceNightB%d=%f,", i,
1584 td.dirPrice[i].priceNightB * pt_mega);
1587 strprintf(¶m, " Threshold%d=%d,", i,
1588 td.dirPrice[i].threshold);
1592 strprintf(¶m, " Time%d", i);
1594 strprintf(&s, "%0d:%0d-%0d:%0d",
1595 td.dirPrice[i].hDay,
1596 td.dirPrice[i].mDay,
1597 td.dirPrice[i].hNight,
1598 td.dirPrice[i].mNight);
1600 res += (param + "='" + s + "',");
1602 strprintf(¶m, " NoDiscount%d=%d,", i,
1603 td.dirPrice[i].noDiscount);
1606 strprintf(¶m, " SinglePrice%d=%d,", i,
1607 td.dirPrice[i].singlePrice);
1611 strprintf(¶m, " PassiveCost=%f,", td.tariffConf.passiveCost);
1614 strprintf(¶m, " Fee=%f,", td.tariffConf.fee);
1617 strprintf(¶m, " Free=%f,", td.tariffConf.free);
1620 switch (td.tariffConf.traffType)
1623 res += " TraffType='up'";
1626 res += " TraffType='down'";
1629 res += " TraffType='up+down'";
1632 res += " TraffType='max'";
1635 strprintf(¶m, " WHERE name='%s' LIMIT 1", tariffName.c_str());
1638 if(MysqlSetQuery(res.c_str()))
1640 errorStr = "Couldn't save admin:\n";
1641 //errorStr += mysql_error(sock);
1647 //-----------------------------------------------------------------------------
1648 int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1650 const string & login) const
1652 string res, stTime, endTime, tempStr;
1659 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1667 strprintf(&tempStr, "detailstat_%02d_%4d", lt->tm_mon+1, lt->tm_year+1900);
1669 if (!(sock=MysqlConnect())){
1674 if (!(result=mysql_list_tables(sock,tempStr.c_str() )))
1676 errorStr = "Couldn't get table " + tempStr + ":\n";
1677 errorStr += mysql_error(sock);
1682 unsigned int num_rows = mysql_num_rows(result);
1684 mysql_free_result(result);
1688 sprintf(qbuf,"CREATE TABLE detailstat_%02d_%4d (login VARCHAR(40) DEFAULT '',"\
1689 "day TINYINT DEFAULT 0,startTime TIME,endTime TIME,"\
1690 "IP VARCHAR(17) DEFAULT '',dir INT DEFAULT 0,"\
1691 "down BIGINT DEFAULT 0,up BIGINT DEFAULT 0, cash DOUBLE DEFAULT 0.0, INDEX (login), INDEX(dir), INDEX(day), INDEX(IP))",
1692 lt->tm_mon+1, lt->tm_year+1900);
1694 if(MysqlQuery(qbuf,sock))
1696 errorStr = "Couldn't create WriteDetailedStat table:\n";
1697 errorStr += mysql_error(sock);
1706 lt1 = localtime(&lastStat);
1715 lt2 = localtime(&t);
1721 strprintf(&stTime, "%02d:%02d:%02d", h1, m1, s1);
1722 strprintf(&endTime, "%02d:%02d:%02d", h2, m2, s2);
1724 strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
1725 "day=%d,startTime='%s',endTime='%s',",
1726 lt->tm_mon+1, lt->tm_year+1900,
1733 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1734 stIter = statTree.begin();
1736 while (stIter != statTree.end())
1738 strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f",
1739 inet_ntostring(stIter->first.ip).c_str(),
1741 stIter->second.down,
1746 if( MysqlQuery((res+tempStr).c_str(),sock) )
1748 errorStr = "Couldn't insert data in WriteDetailedStat:\n";
1749 errorStr += mysql_error(sock);
1754 result=mysql_store_result(sock);
1756 mysql_free_result(result);
1763 //-----------------------------------------------------------------------------
1764 int MYSQL_STORE::AddMessage(STG_MSG * msg, const string & login) const
1768 gettimeofday(&tv, NULL);
1770 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1772 sprintf(qbuf,"INSERT INTO messages SET login='%s', id=%lld",
1774 (long long)msg->header.id
1777 if(MysqlSetQuery(qbuf))
1779 errorStr = "Couldn't add message:\n";
1780 //errorStr += mysql_error(sock);
1784 return EditMessage(*msg, login);
1786 //-----------------------------------------------------------------------------
1787 int MYSQL_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1791 strprintf(&res,"UPDATE messages SET type=%d, lastSendTime=%u, creationTime=%u, "\
1792 "showTime=%u, stgRepeat=%d, repeatPeriod=%u, text='%s' "\
1793 "WHERE login='%s' AND id=%lld LIMIT 1",
1795 msg.header.lastSendTime,
1796 msg.header.creationTime,
1797 msg.header.showTime,
1799 msg.header.repeatPeriod,
1800 (ReplaceStr(msg.text,badSyms,repSym)).c_str(),
1802 (long long)msg.header.id
1805 if(MysqlSetQuery(res.c_str()))
1807 errorStr = "Couldn't edit message:\n";
1808 //errorStr += mysql_error(sock);
1814 //-----------------------------------------------------------------------------
1815 int MYSQL_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
1821 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
1822 login.c_str(), static_cast<unsigned long long>(id));
1824 if(MysqlGetQuery(qbuf,sock))
1826 errorStr = "Couldn't GetMessage:\n";
1827 errorStr += mysql_error(sock);
1832 if (!(res=mysql_store_result(sock)))
1834 errorStr = "Couldn't GetMessage:\n";
1835 errorStr += mysql_error(sock);
1840 row = mysql_fetch_row(res);
1842 if(row[2]&&str2x(row[2], msg->header.type))
1844 mysql_free_result(res);
1845 errorStr = "Invalid value in message header for user: " + login;
1850 if(row[3] && str2x(row[3], msg->header.lastSendTime))
1852 mysql_free_result(res);
1853 errorStr = "Invalid value in message header for user: " + login;
1858 if(row[4] && str2x(row[4], msg->header.creationTime))
1860 mysql_free_result(res);
1861 errorStr = "Invalid value in message header for user: " + login;
1866 if(row[5] && str2x(row[5], msg->header.showTime))
1868 mysql_free_result(res);
1869 errorStr = "Invalid value in message header for user: " + login;
1874 if(row[6] && str2x(row[6], msg->header.repeat))
1876 mysql_free_result(res);
1877 errorStr = "Invalid value in message header for user: " + login;
1882 if(row[7] && str2x(row[7], msg->header.repeatPeriod))
1884 mysql_free_result(res);
1885 errorStr = "Invalid value in message header for user: " + login;
1890 msg->header.id = id;
1893 mysql_free_result(res);
1897 //-----------------------------------------------------------------------------
1898 int MYSQL_STORE::DelMessage(uint64_t id, const string & login) const
1900 sprintf(qbuf,"DELETE FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
1901 login.c_str(),(long long)id);
1903 if(MysqlSetQuery(qbuf))
1905 errorStr = "Couldn't delete Message:\n";
1906 //errorStr += mysql_error(sock);
1912 //-----------------------------------------------------------------------------
1913 int MYSQL_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
1918 sprintf(qbuf,"SELECT * FROM messages WHERE login='%s'", login.c_str());
1920 if(MysqlGetQuery(qbuf,sock))
1922 errorStr = "Couldn't GetMessageHdrs:\n";
1923 errorStr += mysql_error(sock);
1928 if (!(res=mysql_store_result(sock)))
1930 errorStr = "Couldn't GetMessageHdrs:\n";
1931 errorStr += mysql_error(sock);
1936 unsigned int i, num_rows = mysql_num_rows(res);
1937 long long int unsigned id = 0;
1939 for (i=0; i<num_rows; i++)
1941 row = mysql_fetch_row(res);
1942 if (str2x(row[1], id))
1947 if(str2x(row[2], hdr.type))
1951 if(str2x(row[3], hdr.lastSendTime))
1955 if(str2x(row[4], hdr.creationTime))
1959 if(str2x(row[5], hdr.showTime))
1963 if(str2x(row[6], hdr.repeat))
1967 if(str2x(row[7], hdr.repeatPeriod))
1971 hdrsList->push_back(hdr);
1974 mysql_free_result(res);
1978 //-----------------------------------------------------------------------------
1980 int MYSQL_STORE::MysqlSetQuery(const char * Query) const {
1983 int ret=MysqlGetQuery(Query,sock);
1987 //-----------------------------------------------------------------------------
1988 int MYSQL_STORE::MysqlGetQuery(const char * Query,MYSQL * & sock) const {
1989 if (!(sock=MysqlConnect())) {
1992 return MysqlQuery(Query,sock);
1994 //-----------------------------------------------------------------------------
1995 MYSQL * MYSQL_STORE::MysqlConnect() const {
1997 if ( !(sock=mysql_init(NULL)) ){
1998 errorStr= "mysql init susck\n";
2001 if (!(sock = mysql_real_connect(sock,storeSettings.GetDBHost().c_str(),
2002 storeSettings.GetDBUser().c_str(),storeSettings.GetDBPassword().c_str(),
2005 errorStr = "Couldn't connect to mysql engine! With error:\n";
2006 errorStr += mysql_error(sock);
2010 if(mysql_select_db(sock, storeSettings.GetDBName().c_str())){
2011 errorStr = "Database lost !\n";
2017 //-----------------------------------------------------------------------------