]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store.h
14f0b26997fffe1d105199d1871c68fe9d6ce25f
[stg.git] / projects / stargazer / plugins / store / firebird / firebird_store.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #pragma once
22
23 #include "stg/store.h"
24 #include "stg/locker.h"
25 #include "stg/ibpp.h"
26 #include "stg/logger.h"
27 #include "stg/module_settings.h"
28
29 #include <ctime>
30 #include <string>
31 #include <vector>
32
33 struct ToLower {
34     char operator() (char c) const  { return static_cast<char>(std::tolower(c)); }
35 };
36
37 class FIREBIRD_STORE : public STG::Store {
38 public:
39     FIREBIRD_STORE();
40     ~FIREBIRD_STORE() override;
41
42     int GetUsersList(std::vector<std::string> * usersList) const override;
43     int AddUser(const std::string & login) const override;
44     int DelUser(const std::string & login) const override;
45     int SaveUserStat(const STG::UserStat & stat, const std::string & login) const override;
46     int SaveUserConf(const STG::UserConf & conf, const std::string & login) const override;
47     int RestoreUserStat(STG::UserStat * stat, const std::string & login) const override;
48     int RestoreUserConf(STG::UserConf * conf, const std::string & login) const override;
49     int WriteUserChgLog(const std::string & login,
50                         const std::string & admLogin,
51                         uint32_t admIP,
52                         const std::string & paramName,
53                         const std::string & oldValue,
54                         const std::string & newValue,
55                         const std::string & message) const override;
56     int WriteUserConnect(const std::string & login, uint32_t ip) const override;
57     int WriteUserDisconnect(const std::string & login,
58                             const STG::DirTraff & up,
59                             const STG::DirTraff & down,
60                             const STG::DirTraff & sessionUp,
61                             const STG::DirTraff & sessionDown,
62                             double cash,
63                             double freeMb,
64                             const std::string & reason) const override;
65     int WriteDetailedStat(const STG::TraffStat & statTree,
66                           time_t lastStat,
67                           const std::string & login) const override;
68
69     int AddMessage(STG::Message * msg, const std::string & login) const override;
70     int EditMessage(const STG::Message & msg, const std::string & login) const override;
71     int GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const override;
72     int DelMessage(uint64_t id, const std::string & login) const override;
73     int GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList, const std::string & login) const override;
74
75     int SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string  & login) const override;
76
77     int GetAdminsList(std::vector<std::string> * adminsList) const override;
78     int SaveAdmin(const STG::AdminConf & ac) const override;
79     int RestoreAdmin(STG::AdminConf * ac, const std::string & login) const override;
80     int AddAdmin(const std::string & login) const override;
81     int DelAdmin(const std::string & login) const override;
82
83     int GetTariffsList(std::vector<std::string> * tariffsList) const override;
84     int AddTariff(const std::string & name) const override;
85     int DelTariff(const std::string & name) const override;
86     int SaveTariff(const STG::TariffData & td, const std::string & tariffName) const override;
87     int RestoreTariff(STG::TariffData * td, const std::string & tariffName) const override;
88
89     int GetCorpsList(std::vector<std::string> * corpsList) const override;
90     int SaveCorp(const STG::CorpConf & cc) const override;
91     int RestoreCorp(STG::CorpConf * cc, const std::string & name) const override;
92     int AddCorp(const std::string & name) const override;
93     int DelCorp(const std::string & name) const override;
94
95     inline void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
96     int ParseSettings() override;
97
98     inline const std::string & GetStrError() const override { return strError; }
99
100     inline const std::string & GetVersion() const override { return version; }
101
102     int GetServicesList(std::vector<std::string> * servicesList) const override;
103     int SaveService(const STG::ServiceConf & sc) const override;
104     int RestoreService(STG::ServiceConf * sc, const std::string & name) const override;
105     int AddService(const std::string & name) const override;
106     int DelService(const std::string & name) const override;
107
108 private:
109     FIREBIRD_STORE(const FIREBIRD_STORE & rvalue);
110     FIREBIRD_STORE & operator=(const FIREBIRD_STORE & rvalue);
111
112     std::string version;
113     mutable std::string strError;
114     std::string db_server, db_database, db_user, db_password;
115     STG::ModuleSettings settings;
116     mutable IBPP::Database db;
117     mutable pthread_mutex_t mutex;
118     IBPP::TIL til;
119     IBPP::TLR tlr;
120     int schemaVersion;
121     STG::PluginLogger logger;
122
123     int SaveStat(const STG::UserStat & stat, const std::string & login, int year = 0, int month = 0) const;
124     int CheckVersion();
125 };
126
127 time_t ts2time_t(const IBPP::Timestamp & ts);
128 void time_t2ts(time_t t, IBPP::Timestamp * ts);
129 void ym2date(int year, int month, IBPP::Date * date);
130
131 template <typename T>
132 inline
133 T Get(IBPP::Statement st, size_t pos)
134 {
135     T value;
136     st->Get(pos, value);
137     return value;
138 }