]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store.h
More std::jthread
[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 class FIREBIRD_STORE : public STG::Store {
34 public:
35     FIREBIRD_STORE();
36     ~FIREBIRD_STORE() override;
37
38     int GetUsersList(std::vector<std::string> * usersList) const override;
39     int AddUser(const std::string & login) const override;
40     int DelUser(const std::string & login) const override;
41     int SaveUserStat(const STG::UserStat & stat, const std::string & login) const override;
42     int SaveUserConf(const STG::UserConf & conf, const std::string & login) const override;
43     int RestoreUserStat(STG::UserStat * stat, const std::string & login) const override;
44     int RestoreUserConf(STG::UserConf * conf, const std::string & login) const override;
45     int WriteUserChgLog(const std::string & login,
46                         const std::string & admLogin,
47                         uint32_t admIP,
48                         const std::string & paramName,
49                         const std::string & oldValue,
50                         const std::string & newValue,
51                         const std::string & message) const override;
52     int WriteUserConnect(const std::string & login, uint32_t ip) const override;
53     int WriteUserDisconnect(const std::string & login,
54                             const STG::DirTraff & up,
55                             const STG::DirTraff & down,
56                             const STG::DirTraff & sessionUp,
57                             const STG::DirTraff & sessionDown,
58                             double cash,
59                             double freeMb,
60                             const std::string & reason) const override;
61     int WriteDetailedStat(const STG::TraffStat & statTree,
62                           time_t lastStat,
63                           const std::string & login) const override;
64
65     int AddMessage(STG::Message * msg, const std::string & login) const override;
66     int EditMessage(const STG::Message & msg, const std::string & login) const override;
67     int GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const override;
68     int DelMessage(uint64_t id, const std::string & login) const override;
69     int GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList, const std::string & login) const override;
70
71     int SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string  & login) const override;
72
73     int GetAdminsList(std::vector<std::string> * adminsList) const override;
74     int SaveAdmin(const STG::AdminConf & ac) const override;
75     int RestoreAdmin(STG::AdminConf * ac, const std::string & login) const override;
76     int AddAdmin(const std::string & login) const override;
77     int DelAdmin(const std::string & login) const override;
78
79     int GetTariffsList(std::vector<std::string> * tariffsList) const override;
80     int AddTariff(const std::string & name) const override;
81     int DelTariff(const std::string & name) const override;
82     int SaveTariff(const STG::TariffData & td, const std::string & tariffName) const override;
83     int RestoreTariff(STG::TariffData * td, const std::string & tariffName) const override;
84
85     int GetCorpsList(std::vector<std::string> * corpsList) const override;
86     int SaveCorp(const STG::CorpConf & cc) const override;
87     int RestoreCorp(STG::CorpConf * cc, const std::string & name) const override;
88     int AddCorp(const std::string & name) const override;
89     int DelCorp(const std::string & name) const override;
90
91     inline void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
92     int ParseSettings() override;
93
94     inline const std::string & GetStrError() const override { return strError; }
95
96     inline const std::string & GetVersion() const override { return version; }
97
98     int GetServicesList(std::vector<std::string> * servicesList) const override;
99     int SaveService(const STG::ServiceConf & sc) const override;
100     int RestoreService(STG::ServiceConf * sc, const std::string & name) const override;
101     int AddService(const std::string & name) const override;
102     int DelService(const std::string & name) const override;
103
104 private:
105     FIREBIRD_STORE(const FIREBIRD_STORE & rvalue);
106     FIREBIRD_STORE & operator=(const FIREBIRD_STORE & rvalue);
107
108     std::string version;
109     mutable std::string strError;
110     std::string db_server, db_database, db_user, db_password;
111     STG::ModuleSettings settings;
112     mutable IBPP::Database db;
113     mutable pthread_mutex_t mutex;
114     IBPP::TIL til;
115     IBPP::TLR tlr;
116     int schemaVersion;
117     STG::PluginLogger logger;
118
119     int SaveStat(const STG::UserStat & stat, const std::string & login, int year = 0, int month = 0) const;
120     int CheckVersion();
121 };
122
123 time_t ts2time_t(const IBPP::Timestamp & ts);
124 void time_t2ts(time_t t, IBPP::Timestamp * ts);
125 void ym2date(int year, int month, IBPP::Date * date);
126
127 template <typename T>
128 inline
129 T Get(IBPP::Statement st, size_t pos)
130 {
131     T value;
132     st->Get(pos, value);
133     return value;
134 }