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