]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store.h
Implemented daily fee charge with backward compatibility.
[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 /*
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 #include <map>
37
38 #include "stg/store.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 extern "C" STORE * GetStore();
45
46 class STG_LOGGER;
47
48 class POSTGRESQL_STORE : public STORE {
49 public:
50     POSTGRESQL_STORE();
51     virtual ~POSTGRESQL_STORE();
52
53     // Users
54     int GetUsersList(std::vector<std::string> * usersList) const;
55     int AddUser(const std::string & login) const;
56     int DelUser(const std::string & login) const;
57     int SaveUserStat(const USER_STAT & stat, const std::string & login) const;
58     int SaveUserConf(const USER_CONF & conf, const std::string & login) const;
59     int RestoreUserStat(USER_STAT * stat, const std::string & login) const;
60     int RestoreUserConf(USER_CONF * conf, const std::string & login) const;
61     int WriteUserChgLog(const std::string & login,
62                         const std::string & admLogin,
63                         uint32_t admIP,
64                         const std::string & paramName,
65                         const std::string & oldValue,
66                         const std::string & newValue,
67                         const std::string & message) const;
68     int WriteUserConnect(const std::string & login, uint32_t ip) const;
69     int WriteUserDisconnect(const std::string & login,
70                             const DIR_TRAFF & up,
71                             const DIR_TRAFF & down,
72                             const DIR_TRAFF & sessionUp,
73                             const DIR_TRAFF & sessionDown,
74                             double cash,
75                             double freeMb,
76                             const std::string & reason) const;
77     int WriteDetailedStat(const TRAFF_STAT & statTree,
78                           time_t lastStat,
79                           const std::string & login) const;
80
81     // Messages
82     int AddMessage(STG_MSG * msg, const std::string & login) const;
83     int EditMessage(const STG_MSG & msg, const std::string & login) const;
84     int GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const;
85     int DelMessage(uint64_t id, const std::string & login) const;
86     int GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const;
87
88     // Stats
89     int SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string  & login) const;
90
91     // Admins
92     int GetAdminsList(std::vector<std::string> * adminsList) const;
93     int SaveAdmin(const ADMIN_CONF & ac) const;
94     int RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const;
95     int AddAdmin(const std::string & login) const;
96     int DelAdmin(const std::string & login) const;
97
98     // Tariffs
99     int GetTariffsList(std::vector<std::string> * tariffsList) const;
100     int AddTariff(const std::string & name) const;
101     int DelTariff(const string & name) const;
102     int SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const;
103     int RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const;
104
105     // Corporations
106     int GetCorpsList(std::vector<std::string> * corpsList) const;
107     int SaveCorp(const CORP_CONF & cc) const;
108     int RestoreCorp(CORP_CONF * cc, const std::string & name) const;
109     int AddCorp(const std::string & name) const;
110     int DelCorp(const std::string & name) const;
111
112     // Services
113     int GetServicesList(std::vector<std::string> * servicesList) const;
114     int SaveService(const SERVICE_CONF & sc) const;
115     int RestoreService(SERVICE_CONF * sc, const std::string & name) const;
116     int AddService(const std::string & name) const;
117     int DelService(const std::string & name) const;
118
119     // Settings
120     inline void SetSettings(const MODULE_SETTINGS & s) { settings = s; };
121     int ParseSettings();
122
123     inline const string & GetStrError() const { return strError; };
124     inline const string & GetVersion() const { return versionString; };
125 private:
126     POSTGRESQL_STORE(const POSTGRESQL_STORE & rvalue);
127     POSTGRESQL_STORE & operator=(const POSTGRESQL_STORE & rvalue);
128
129     int StartTransaction() const;
130     int CommitTransaction() const;
131     int RollbackTransaction() const;
132
133     int EscapeString(std::string & value) const;
134
135     std::string Int2TS(uint32_t value) const;
136     uint32_t TS2Int(const std::string & value) const;
137
138     int SaveStat(const USER_STAT & stat, const std::string & login, int year = 0, int month = 0) const;
139
140     int SaveUserServices(uint32_t uid, const std::vector<std::string> & services) const;
141     int SaveUserData(uint32_t uid, const std::vector<std::string> & data) const;
142     int SaveUserIPs(uint32_t uid, const USER_IPS & ips) const;
143
144     void MakeDate(std::string & date, int year = 0, int month = 0) const;
145
146     int Connect();
147     int Reset() const;
148     int CheckVersion() const;
149
150     std::string versionString;
151     mutable std::string strError;
152     std::string server;
153     std::string database;
154     std::string user;
155     std::string password;
156     std::string clientEncoding;
157     MODULE_SETTINGS settings;
158     mutable pthread_mutex_t mutex;
159     mutable int version;
160     int retries;
161
162     PGconn * connection;
163
164     STG_LOGGER & WriteServLog;
165 };
166
167 extern const volatile time_t stgTime;
168
169 #endif //POSTGRESQL_STORE_H