]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/store/mysql/mysql_store.cpp
Code cleanup.
[stg.git] / projects / stargazer / plugins / store / mysql / mysql_store.cpp
index 102be151dd27e736bb2dca8ceb0d690e81cb6abe..79ec39e35f8edafb791f709d79ff88886e94592c 100644 (file)
@@ -8,11 +8,12 @@
 #include <mysql.h>
 #include <errmsg.h>
 
-#include "user_ips.h"
-#include "user_conf.h"
-#include "user_stat.h"
+#include "stg/user_ips.h"
+#include "stg/user_conf.h"
+#include "stg/user_stat.h"
+#include "stg/blowfish.h"
+#include "stg/plugin_creator.h"
 #include "mysql_store.h"
-#include "blowfish.h"
 
 #define adm_enc_passwd "cjeifY8m3"
 char qbuf[4096];
@@ -95,42 +96,23 @@ int GetULongLongInt(const string & str, uint64_t * val, uint64_t defaultVal)
     return 0;
 } 
 
-class MYSQL_STORE_CREATOR
-{
-private:
-    MYSQL_STORE * ms;
-
-public:
-    MYSQL_STORE_CREATOR()
-        : ms(new MYSQL_STORE())
-        {
-        };
-    ~MYSQL_STORE_CREATOR()
-        {
-        delete ms;
-        };
-
-    MYSQL_STORE * GetStore()
-        {
-        return ms;
-        };
-} msc;
+PLUGIN_CREATOR<MYSQL_STORE> msc;
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-BASE_STORE * GetStore()
+STORE * GetStore()
 {
-return msc.GetStore();
+return msc.GetPlugin();
 }
 //-----------------------------------------------------------------------------
 MYSQL_STORE_SETTINGS::MYSQL_STORE_SETTINGS()
-    : settings(NULL)
+    : settings(NULL),
+      errorStr(),
+      dbUser(),
+      dbPass(),
+      dbName(),
+      dbHost()
 {
-}
-//-----------------------------------------------------------------------------
-MYSQL_STORE_SETTINGS::~MYSQL_STORE_SETTINGS()
-{
-
 }
 //-----------------------------------------------------------------------------
 int MYSQL_STORE_SETTINGS::ParseParam(const vector<PARAM_VALUE> & moduleParams, 
@@ -153,66 +135,40 @@ return 0;
 //-----------------------------------------------------------------------------
 int MYSQL_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
 {
-if (ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
+if (ParseParam(s.moduleParams, "user", dbUser) < 0 &&
+    ParseParam(s.moduleParams, "dbuser", dbUser) < 0)
     return -1;
-if (ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
+if (ParseParam(s.moduleParams, "password", dbPass) < 0 &&
+    ParseParam(s.moduleParams, "rootdbpass", dbPass) < 0)
     return -1;
-if (ParseParam(s.moduleParams, "dbname", dbName) < 0)
+if (ParseParam(s.moduleParams, "database", dbName) < 0 &&
+    ParseParam(s.moduleParams, "dbname", dbName) < 0)
     return -1;
-if (ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
+if (ParseParam(s.moduleParams, "server", dbHost) < 0 &&
+    ParseParam(s.moduleParams, "dbhost", dbHost) < 0)
     return -1;
 
 return 0;
 }
 //-----------------------------------------------------------------------------
-const string & MYSQL_STORE_SETTINGS::GetStrError() const
-{
-return errorStr;
-}
-//-----------------------------------------------------------------------------
-string MYSQL_STORE_SETTINGS::GetDBUser() const
-{
-return dbUser;
-}
-//-----------------------------------------------------------------------------
-string MYSQL_STORE_SETTINGS::GetDBPassword() const
-{
-return dbPass;
-}
-//-----------------------------------------------------------------------------
-string MYSQL_STORE_SETTINGS::GetDBHost() const
-{
-return dbHost;
-}
-//-----------------------------------------------------------------------------
-string MYSQL_STORE_SETTINGS::GetDBName() const
-{
-return dbName;
-}
-//-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 MYSQL_STORE::MYSQL_STORE()
+    : errorStr(),
+      version("mysql_store v.0.67"),
+      storeSettings(),
+      settings(),
+      logger(GetPluginLogger(GetStgLogger(), "store_mysql"))
 {
-version = "mysql_store v.0.67";
 };
 //-----------------------------------------------------------------------------
-MYSQL_STORE::~MYSQL_STORE()
-{    
-};
-//-----------------------------------------------------------------------------
-void MYSQL_STORE::SetSettings(const MODULE_SETTINGS & s)
-{
-settings = s;
-}
-//-----------------------------------------------------------------------------
 int    MYSQL_STORE::MysqlQuery(const char* sQuery,MYSQL * sock) const
 {
-    int ret,i;
+    int ret;
 
     if( (ret = mysql_query(sock,sQuery)) )
     {
-        for(i=0; i<RepitTimes; i++)
+        for(int i=0; i<RepitTimes; i++)
         {
             if( (ret = mysql_query(sock,sQuery)) )
                 ;//need to send error result
@@ -285,16 +241,6 @@ else
 return ret;
 }
 //-----------------------------------------------------------------------------
-const string & MYSQL_STORE::GetStrError() const
-{
-return errorStr;
-}
-//-----------------------------------------------------------------------------
-const string & MYSQL_STORE::GetVersion() const
-{
-return version;
-}
-//-----------------------------------------------------------------------------
 bool MYSQL_STORE::IsTablePresent(const string & str,MYSQL * sock)
 {
 MYSQL_RES* result;
@@ -711,6 +657,13 @@ if (!(res=mysql_store_result(sock)))
     return -1;
 }
 
+if (mysql_num_rows(res) != 1)
+{
+    errorStr = "User not found";
+    mysql_close(sock);
+    return -1;
+}
+
 row = mysql_fetch_row(res);
 
 string param;
@@ -797,7 +750,7 @@ try
     {
     i = StrToIPS(ipStr);
     }
-catch (string s)
+catch (const string & s)
     {
     mysql_free_result(res);
     errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
@@ -855,30 +808,29 @@ row = mysql_fetch_row(res);
 unsigned int startPos=0;
 
 char s[22];
-uint64_t traffU[DIR_NUM];
-uint64_t traffD[DIR_NUM];
 
 for (int i = 0; i < DIR_NUM; i++)
     {
+    uint64_t traff;
     sprintf(s, "D%d", i);
-    if (GetULongLongInt(row[startPos+i*2],&traffD[i], 0) != 0)
+    if (GetULongLongInt(row[startPos+i*2], &traff, 0) != 0)
         {
         mysql_free_result(res);
         errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
         mysql_close(sock);
         return -1;
         }
-    stat->down = traffD;
+    stat->down[i] = traff;
 
     sprintf(s, "U%d", i);
-    if (GetULongLongInt(row[startPos+i*2+1], &traffU[i], 0) != 0)
+    if (GetULongLongInt(row[startPos+i*2+1], &traff, 0) != 0)
         {
         mysql_free_result(res);
         errorStr =   "User \'" + login + "\' stat not read. Parameter " + string(s);
         mysql_close(sock);
         return -1;
         }
-    stat->up = traffU;
+    stat->up[i] = traff;
     }//for
 
 startPos += (2*DIR_NUM);
@@ -1118,8 +1070,8 @@ int MYSQL_STORE::WriteUserDisconnect(const string & login,
                                      const DIR_TRAFF & sessionUp,
                                      const DIR_TRAFF & sessionDown,
                                      double cash,
-                                     double freeMb,
-                                     const std::string & reason) const
+                                     double /*freeMb*/,
+                                     const std::string & /*reason*/) const
 {
 string logStr = "Disconnect, ";
 stringstream sssu;
@@ -1263,6 +1215,10 @@ char password[ADM_PASSWD_LEN + 1];
 char passwordE[2*ADM_PASSWD_LEN + 2];
 BLOWFISH_CTX ctx;
 
+memset(pass, 0, sizeof(pass));
+memset(password, 0, sizeof(password));
+memset(passwordE, 0, sizeof(passwordE));
+
 string p;
 MYSQL_RES *res;
 MYSQL_ROW row;
@@ -1690,7 +1646,7 @@ if(MysqlSetQuery(res.c_str()))
 return 0;
 }
 //-----------------------------------------------------------------------------
-int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> * statTree, 
+int MYSQL_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree, 
                                    time_t lastStat, 
                                    const string & login) const
 {
@@ -1775,11 +1731,10 @@ strprintf(&res,"INSERT INTO detailstat_%02d_%4d SET login='%s',"\
     endTime.c_str()
     );
 
-int retRes;
 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
-stIter = statTree->begin();
+stIter = statTree.begin();
 
-while (stIter != statTree->end())
+while (stIter != statTree.end())
     {
         strprintf(&tempStr,"IP='%s', dir=%d, down=%lld, up=%lld, cash=%f", 
                 inet_ntostring(stIter->first.ip).c_str(),
@@ -1789,7 +1744,7 @@ while (stIter != statTree->end())
                 stIter->second.cash
             );
     
-        if( (retRes = MysqlQuery((res+tempStr).c_str(),sock)) )
+        if( MysqlQuery((res+tempStr).c_str(),sock) )
         {
             errorStr = "Couldn't insert data in WriteDetailedStat:\n";
             errorStr += mysql_error(sock);
@@ -1864,8 +1819,8 @@ MYSQL_RES *res;
 MYSQL_ROW row;
 MYSQL * sock;
 
-sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%lld LIMIT 1",
-    login.c_str(), id);
+sprintf(qbuf,"SELECT * FROM messages WHERE login='%s' AND id=%llu LIMIT 1",
+        login.c_str(), static_cast<unsigned long long>(id));
     
 if(MysqlGetQuery(qbuf,sock))
 {