]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store.h
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_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/module_settings.h"
25 #include "stg/logger.h"
26
27 #include <string>
28 #include <vector>
29 #include <mutex>
30
31 #include <libpq-fe.h>
32
33 // Minimal DB version is 5
34 // Recommended DB version is 6 (support FreeMb logging on disconnects)
35 #define DB_MIN_VERSION 5
36
37 namespace STG
38 {
39
40 class UserIPs;
41
42 }
43
44 class POSTGRESQL_STORE : public STG::Store
45 {
46     public:
47         POSTGRESQL_STORE();
48         ~POSTGRESQL_STORE() override;
49
50         // Users
51         int GetUsersList(std::vector<std::string> * usersList) const override;
52         int AddUser(const std::string & login) const override;
53         int DelUser(const std::string & login) const override;
54         int SaveUserStat(const STG::UserStat & stat, const std::string & login) const override;
55         int SaveUserConf(const STG::UserConf & conf, const std::string & login) const override;
56         int RestoreUserStat(STG::UserStat * stat, const std::string & login) const override;
57         int RestoreUserConf(STG::UserConf * conf, const std::string & login) const override;
58         int WriteUserChgLog(const std::string & login,
59                             const std::string & admLogin,
60                             uint32_t admIP,
61                             const std::string & paramName,
62                             const std::string & oldValue,
63                             const std::string & newValue,
64                             const std::string & message) const override;
65         int WriteUserConnect(const std::string & login, uint32_t ip) const override;
66         int WriteUserDisconnect(const std::string & login,
67                                 const STG::DirTraff & up,
68                                 const STG::DirTraff & down,
69                                 const STG::DirTraff & sessionUp,
70                                 const STG::DirTraff & sessionDown,
71                                 double cash,
72                                 double freeMb,
73                                 const std::string & reason) const override;
74         int WriteDetailedStat(const STG::TraffStat & statTree,
75                               time_t lastStat,
76                               const std::string & login) const override;
77
78         // Messages
79         int AddMessage(STG::Message * msg, const std::string & login) const override;
80         int EditMessage(const STG::Message & msg, const std::string & login) const override;
81         int GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const override;
82         int DelMessage(uint64_t id, const std::string & login) const override;
83         int GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList, const std::string & login) const override;
84
85         // Stats
86         int SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string  & login) const override;
87
88         // Admins
89         int GetAdminsList(std::vector<std::string> * adminsList) const override;
90         int SaveAdmin(const STG::AdminConf & ac) const override;
91         int RestoreAdmin(STG::AdminConf * ac, const std::string & login) const override;
92         int AddAdmin(const std::string & login) const override;
93         int DelAdmin(const std::string & login) const override;
94
95         // Tariffs
96         int GetTariffsList(std::vector<std::string> * tariffsList) const override;
97         int AddTariff(const std::string & name) const override;
98         int DelTariff(const std::string & name) const override;
99         int SaveTariff(const STG::TariffData & td, const std::string & tariffName) const override;
100         int RestoreTariff(STG::TariffData * td, const std::string & tariffName) const override;
101
102         // Corporations
103         int GetCorpsList(std::vector<std::string> * corpsList) const override;
104         int SaveCorp(const STG::CorpConf & cc) const override;
105         int RestoreCorp(STG::CorpConf * cc, const std::string & name) const override;
106         int AddCorp(const std::string & name) const override;
107         int DelCorp(const std::string & name) const override;
108
109         // Services
110         int GetServicesList(std::vector<std::string> * servicesList) const override;
111         int SaveService(const STG::ServiceConf & sc) const override;
112         int RestoreService(STG::ServiceConf * sc, const std::string & name) const override;
113         int AddService(const std::string & name) const override;
114         int DelService(const std::string & name) const override;
115
116         // Settings
117         void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
118         int ParseSettings() override;
119
120         const std::string & GetStrError() const override { return strError; }
121         const std::string & GetVersion() const override { return versionString; }
122     private:
123         POSTGRESQL_STORE(const POSTGRESQL_STORE & rvalue);
124         POSTGRESQL_STORE & operator=(const POSTGRESQL_STORE & rvalue);
125
126         int StartTransaction() const;
127         int CommitTransaction() const;
128         int RollbackTransaction() const;
129
130         int EscapeString(std::string & value) const;
131
132         int SaveStat(const STG::UserStat & stat, const std::string & login, int year = 0, int month = 0) const;
133
134         int SaveUserServices(uint32_t uid, const std::vector<std::string> & services) const;
135         int SaveUserData(uint32_t uid, const std::vector<std::string> & data) const;
136         int SaveUserIPs(uint32_t uid, const STG::UserIPs & ips) const;
137
138         void MakeDate(std::string & date, int year = 0, int month = 0) const;
139
140         int Connect();
141         int Reset() const;
142         int CheckVersion() const;
143
144         std::string versionString;
145         mutable std::string strError;
146         std::string server;
147         std::string database;
148         std::string user;
149         std::string password;
150         std::string clientEncoding;
151         STG::ModuleSettings settings;
152         mutable std::mutex m_mutex;
153         mutable int version;
154         int retries;
155
156         PGconn * connection;
157
158         STG::PluginLogger logger;
159 };