]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/store/firebird/firebird_store_users.cpp
Enable some more libs.
[stg.git] / projects / stargazer / plugins / store / firebird / firebird_store_users.cpp
index 035ae134e3385fa8348c7114b708b39ca69c34f3..bf6e9e9faa4dbd29b8edd51aae1461db0fab6691 100644 (file)
  *
  */
 
-#include "stg_const.h"
 #include "firebird_store.h"
-#include "ibpp.h"
+
+#include "stg/ibpp.h"
+#include "stg/user_conf.h"
+#include "stg/user_stat.h"
+#include "stg/user_traff.h"
+#include "stg/user_ips.h"
+#include "stg/const.h"
+#include "stg/common.h"
 
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::GetUsersList(vector<string> * usersList) const
+int FIREBIRD_STORE::GetUsersList(std::vector<std::string> * usersList) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
-string name;
-
 try
     {
     tr->Start();
     st->Execute("select name from tb_users");
     while (st->Fetch())
         {
+        std::string name;
         st->Get(1, name);
         usersList->push_back(name);
         }
@@ -63,9 +68,9 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::AddUser(const string & name) const
+int FIREBIRD_STORE::AddUser(const std::string & name) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
@@ -91,9 +96,9 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::DelUser(const string & login) const
+int FIREBIRD_STORE::DelUser(const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
@@ -118,29 +123,22 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::SaveUserStat(const USER_STAT & stat,
-                                 const string & login) const
+int FIREBIRD_STORE::SaveUserStat(const STG::UserStat & stat,
+                                 const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 return SaveStat(stat, login);
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::SaveStat(const USER_STAT & stat,
-                                 const string & login,
-                                 int year,
-                                 int month) const
+int FIREBIRD_STORE::SaveStat(const STG::UserStat & stat,
+                             const std::string & login,
+                             int year,
+                             int month) const
 {
-
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
-IBPP::Timestamp actTime;
-IBPP::Timestamp addTime;
-IBPP::Date dt;
-int i;
-int32_t sid, uid;
-
 try
     {
     tr->Start();
@@ -154,7 +152,7 @@ try
     tr->Rollback();
     return -1;
     }
-    st->Get(1, uid);
+    int32_t uid = Get<int32_t>(st, 1);
     st->Close();
     st->Prepare("select first 1 pk_stat from tb_stats where fk_user = ? order by stats_date desc");
     st->Set(1, uid);
@@ -166,11 +164,15 @@ try
     printfd(__FILE__, "No stat info for user '%s'\n", login.c_str());
     return -1;
     }
+    int32_t sid;
     st->Get(1, sid);
     st->Close();
 
+    IBPP::Timestamp actTime;
     time_t2ts(stat.lastActivityTime, &actTime);
+    IBPP::Timestamp addTime;
     time_t2ts(stat.lastCashAddTime, &addTime);
+    IBPP::Date dt;
     if (year != 0)
         ym2date(year, month, &dt);
     else
@@ -198,14 +200,14 @@ try
     st->Execute();
     st->Close();
 
-    for(i = 0; i < DIR_NUM; i++)
+    for(int i = 0; i < DIR_NUM; i++)
         {
         st->Prepare("update tb_stats_traffic set \
                         upload = ?, \
                         download = ? \
                      where fk_stat = ? and dir_num = ?");
-        st->Set(1, (int64_t)stat.up[i]);
-        st->Set(2, (int64_t)stat.down[i]);
+        st->Set(1, (int64_t)stat.monthUp[i]);
+        st->Set(2, (int64_t)stat.monthDown[i]);
         st->Set(3, sid);
         st->Set(4, i);
         st->Execute();
@@ -226,19 +228,14 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::SaveUserConf(const USER_CONF & conf,
-                                 const string & login) const
+int FIREBIRD_STORE::SaveUserConf(const STG::UserConf & conf,
+                                 const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
-int i;
-int32_t uid;
-IBPP::Timestamp creditExpire;
-vector<string>::const_iterator it;
-
 try
     {
     tr->Start();
@@ -252,9 +249,11 @@ try
         tr->Rollback();
         return -1;
         }
+    int32_t uid;
     st->Get(1, uid);
     st->Close();
 
+    IBPP::Timestamp creditExpire;
     time_t2ts(conf.creditExpire, &creditExpire);
 
     st->Prepare("update tb_users set \
@@ -308,7 +307,7 @@ try
     st->Prepare("insert into tb_users_services (fk_user, fk_service) \
                     values (?, (select pk_service from tb_services \
                                 where name = ?))");
-    for(it = conf.service.begin(); it != conf.service.end(); ++it)
+    for(std::vector<std::string>::const_iterator it = conf.services.begin(); it != conf.services.end(); ++it)
         {
         st->Set(1, uid);
         st->Set(2, *it);
@@ -321,9 +320,9 @@ try
     st->Execute();
     st->Close();
 
-    i = 0;
+    int i = 0;
     st->Prepare("insert into tb_users_data (fk_user, data, num) values (?, ?, ?)");
-    for (it = conf.userdata.begin(); it != conf.userdata.end(); ++it)
+    for (std::vector<std::string>::const_iterator it = conf.userdata.begin(); it != conf.userdata.end(); ++it)
         {
         st->Set(1, uid);
         st->Set(2, *it);
@@ -337,16 +336,15 @@ try
     st->Execute();
 
     st->Prepare("insert into tb_allowed_ip (fk_user, ip, mask) values (?, ?, ?)");
-    for(i = 0; i < conf.ips.Count(); i++)
+    for(size_t j = 0; i < conf.ips.count(); j++)
         {
         st->Set(1, uid);
-        st->Set(2, (int32_t)conf.ips[i].ip);
-        st->Set(3, (int32_t)conf.ips[i].mask);
+        st->Set(2, (int32_t)conf.ips[j].ip);
+        st->Set(3, (int32_t)conf.ips[j].mask);
         st->Execute();
         }
     tr->Commit();
     }
-
 catch (IBPP::Exception & ex)
     {
     tr->Rollback();
@@ -358,18 +356,14 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::RestoreUserStat(USER_STAT * stat,
-                                    const string & login) const
+int FIREBIRD_STORE::RestoreUserStat(STG::UserStat * stat,
+                                    const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
-IBPP::Timestamp actTime, addTime;
-int i, dir;
-int32_t uid, sid, passiveTime;
-
 try
     {
     tr->Start();
@@ -382,6 +376,7 @@ try
         printfd(__FILE__, "User '%s' not found in database\n", login.c_str());
         return -1;
         }
+    int32_t uid;
     st->Get(1, uid);
     st->Close();
 
@@ -398,12 +393,16 @@ try
         return -1;
         }
 
+    int32_t sid;
     st->Get(1, sid);
     st->Get(2, stat->cash);
     st->Get(3, stat->freeMb);
+    IBPP::Timestamp actTime;
     st->Get(4, actTime);
     st->Get(5, stat->lastCashAdd);
+    IBPP::Timestamp addTime;
     st->Get(6, addTime);
+    int32_t passiveTime;
     st->Get(7, passiveTime);
 
     stat->passiveTime = passiveTime;
@@ -416,13 +415,14 @@ try
     st->Prepare("select * from tb_stats_traffic where fk_stat = ?");
     st->Set(1, sid);
     st->Execute();
-    for(i = 0; i < DIR_NUM; i++)
+    for(int i = 0; i < DIR_NUM; i++)
         {
         if (st->Fetch())
             {
+            int dir;
             st->Get(3, dir);
-            st->Get(5, (int64_t &)stat->up[dir]);
-            st->Get(4, (int64_t &)stat->down[dir]);
+            st->Get(5, (int64_t &)stat->monthUp[dir]);
+            st->Get(4, (int64_t &)stat->monthDown[dir]);
             }
         else
             {
@@ -443,21 +443,14 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::RestoreUserConf(USER_CONF * conf,
-                                    const string & login) const
+int FIREBIRD_STORE::RestoreUserConf(STG::UserConf * conf,
+                                    const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
-int32_t uid;
-int i;
-IBPP::Timestamp timestamp;
-IP_MASK im;
-string name;
-bool test;
-
 try
     {
     tr->Start();
@@ -482,12 +475,15 @@ try
         tr->Rollback();
         return -1;
         }
+    int32_t uid;
     st->Get(1, uid);
     // Getting base config
     st->Get(2, conf->address);
+    bool test;
     st->Get(3, test);
     conf->alwaysOnline = test;
     st->Get(4, conf->credit);
+    IBPP::Timestamp timestamp;
     st->Get(5, timestamp);
 
     conf->creditExpire = ts2time_t(timestamp);
@@ -523,8 +519,9 @@ try
     st->Execute();
     while (st->Fetch())
         {
+        std::string name;
         st->Get(1, name);
-        conf->service.push_back(name);
+        conf->services.push_back(name);
         }
 
     // User data
@@ -534,6 +531,7 @@ try
     st->Execute();
     while (st->Fetch())
         {
+        int i;
         st->Get(2, i);
         st->Get(1, conf->userdata[i]);
         }
@@ -544,17 +542,18 @@ try
                  where fk_user = ?");
     st->Set(1, uid);
     st->Execute();
-    conf->ips.Erase();
+    STG::UserIPs ips;
     while (st->Fetch())
         {
+        STG::IPMask im;
         st->Get(1, (int32_t &)im.ip);
         st->Get(2, (int32_t &)im.mask);
-        conf->ips.Add(im);
+        ips.add(im);
         }
+    conf->ips = ips;
 
     tr->Commit();
     }
-
 catch (IBPP::Exception & ex)
     {
     tr->Rollback();
@@ -566,22 +565,22 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::WriteUserChgLog(const string & login,
-                                    const string & admLogin,
+int FIREBIRD_STORE::WriteUserChgLog(const std::string & login,
+                                    const std::string & admLogin,
                                     uint32_t admIP,
-                                    const string & paramName,
-                                    const string & oldValue,
-                                    const string & newValue,
-                                    const string & message = "") const
+                                    const std::string & paramName,
+                                    const std::string & oldValue,
+                                    const std::string & newValue,
+                                    const std::string & message = "") const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 IBPP::Timestamp now;
 now.Now();
 
-string temp = ""; // Composed message for log
+std::string temp = ""; // Composed message for log
 
 try
     {
@@ -632,9 +631,9 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::WriteUserConnect(const string & login, uint32_t ip) const
+int FIREBIRD_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
@@ -662,25 +661,22 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::WriteUserDisconnect(const string & login,
-                    const DIR_TRAFF & up,
-                    const DIR_TRAFF & down,
-                    const DIR_TRAFF & sessionUp,
-                    const DIR_TRAFF & sessionDown,
-                    double cash,
-                    double freeMb,
-                    const std::string & reason) const
+int FIREBIRD_STORE::WriteUserDisconnect(const std::string & login,
+                    const STG::DirTraff & up,
+                    const STG::DirTraff & down,
+                    const STG::DirTraff & sessionUp,
+                    const STG::DirTraff & sessionDown,
+                    double /*cash*/,
+                    double /*freeMb*/,
+                    const std::string & /*reason*/) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 IBPP::Timestamp now;
 now.Now();
 
-int32_t id;
-int i;
-
 try
     {
     tr->Start();
@@ -688,12 +684,13 @@ try
     st->Set(1, login);
     st->Set(2, now);
     st->Execute();
+    int32_t id;
     st->Get(1, id);
     st->Prepare("insert into tb_sessions_data \
                     (fk_session_log, dir_num, session_upload, \
                      session_download, month_upload, month_download) \
                  values (?, ?, ?, ?, ?, ?)");
-    for(i = 0; i < DIR_NUM; i++)
+    for(int i = 0; i < DIR_NUM; i++)
         {
         st->Set(1, id);
         st->Set(2, i);
@@ -717,11 +714,11 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> * statTree,
+int FIREBIRD_STORE::WriteDetailedStat(const STG::TraffStat & statTree,
                                       time_t lastStat,
-                                      const string & login) const
+                                      const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
@@ -734,15 +731,15 @@ time_t2ts(lastStat, &statTime);
 try
     {
     tr->Start();
-    map<IP_DIR_PAIR, STAT_NODE>::const_iterator it;
-    it = statTree->begin();
+    STG::TraffStat::const_iterator it;
+    it = statTree.begin();
     st->Prepare("insert into tb_detail_stats \
                     (till_time, from_time, fk_user, dir_num, \
                      ip, download, upload, cost) \
                  values (?, ?, (select pk_user from tb_users \
                                 where name = ?), \
                      ?, ?, ?, ?, ?)");
-    while (it != statTree->end())
+    while (it != statTree.end())
         {
         st->Set(1, now);
         st->Set(2, statTime);
@@ -769,9 +766,9 @@ catch (IBPP::Exception & ex)
 return 0;
 }
 //-----------------------------------------------------------------------------
-int FIREBIRD_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
+int FIREBIRD_STORE::SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 IBPP::Statement st = IBPP::StatementFactory(db, tr);
@@ -780,8 +777,6 @@ IBPP::Timestamp now;
 IBPP::Date nowDate;
 nowDate.Today();
 now.Now();
-int32_t id;
-int i;
 
 if (SaveStat(stat, login, year, month))
     {
@@ -799,6 +794,7 @@ try
     st->Set(4, nowDate);
 
     st->Execute();
+    int32_t id;
     st->Get(1, id);
     st->Close();
 
@@ -806,7 +802,7 @@ try
                     (fk_stat, dir_num, upload, download) \
                  values (?, ?, 0, 0)");
 
-    for(i = 0; i < DIR_NUM; i++)
+    for(int i = 0; i < DIR_NUM; i++)
         {
         st->Set(1, id);
         st->Set(2, i);
@@ -815,7 +811,6 @@ try
 
     tr->Commit();
     }
-
 catch (IBPP::Exception & ex)
     {
     tr->Rollback();