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