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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 * PostgreSQL storage class definition
25 * $Date: 2010/01/19 11:06:53 $
29 #ifndef POSTGRESQL_STORE_H
30 #define POSTGRESQL_STORE_H
38 #include "stg/store.h"
40 // Minimal DB version is 5
41 // Recommended DB version is 6 (support FreeMb logging on disconnects)
42 #define DB_MIN_VERSION 5
44 extern "C" STORE * GetStore();
46 class POSTGRESQL_STORE : public STORE {
49 virtual ~POSTGRESQL_STORE();
52 int GetUsersList(std::vector<std::string> * usersList) const;
53 int AddUser(const std::string & login) const;
54 int DelUser(const std::string & login) const;
55 int SaveUserStat(const USER_STAT & stat, const std::string & login) const;
56 int SaveUserConf(const USER_CONF & conf, const std::string & login) const;
57 int RestoreUserStat(USER_STAT * stat, const std::string & login) const;
58 int RestoreUserConf(USER_CONF * conf, const std::string & login) const;
59 int WriteUserChgLog(const std::string & login,
60 const std::string & admLogin,
62 const std::string & paramName,
63 const std::string & oldValue,
64 const std::string & newValue,
65 const std::string & message) const;
66 int WriteUserConnect(const std::string & login, uint32_t ip) const;
67 int WriteUserDisconnect(const std::string & login,
69 const DIR_TRAFF & down,
70 const DIR_TRAFF & sessionUp,
71 const DIR_TRAFF & sessionDown,
74 const std::string & reason) const;
75 int WriteDetailedStat(const TRAFF_STAT & statTree,
77 const std::string & login) const;
80 int AddMessage(STG_MSG * msg, const std::string & login) const;
81 int EditMessage(const STG_MSG & msg, const std::string & login) const;
82 int GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const;
83 int DelMessage(uint64_t id, const std::string & login) const;
84 int GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const;
87 int SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const;
90 int GetAdminsList(std::vector<std::string> * adminsList) const;
91 int SaveAdmin(const ADMIN_CONF & ac) const;
92 int RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const;
93 int AddAdmin(const std::string & login) const;
94 int DelAdmin(const std::string & login) const;
97 int GetTariffsList(std::vector<std::string> * tariffsList) const;
98 int AddTariff(const std::string & name) const;
99 int DelTariff(const string & name) const;
100 int SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const;
101 int RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const;
104 int GetCorpsList(std::vector<std::string> * corpsList) const;
105 int SaveCorp(const CORP_CONF & cc) const;
106 int RestoreCorp(CORP_CONF * cc, const std::string & name) const;
107 int AddCorp(const std::string & name) const;
108 int DelCorp(const std::string & name) const;
111 int GetServicesList(std::vector<std::string> * servicesList) const;
112 int SaveService(const SERVICE_CONF & sc) const;
113 int RestoreService(SERVICE_CONF * sc, const std::string & name) const;
114 int AddService(const std::string & name) const;
115 int DelService(const std::string & name) const;
118 inline void SetSettings(const MODULE_SETTINGS & s) { settings = s; };
121 inline const string & GetStrError() const { return strError; };
122 inline const string & GetVersion() const { return versionString; };
124 int StartTransaction() const;
125 int CommitTransaction() const;
126 int RollbackTransaction() const;
128 int EscapeString(std::string & value) const;
130 std::string Int2TS(uint32_t value) const;
131 uint32_t TS2Int(const std::string & value) const;
133 int SaveStat(const USER_STAT & stat, const std::string & login, int year = 0, int month = 0) const;
135 int SaveUserServices(uint32_t uid, const std::vector<std::string> & services) const;
136 int SaveUserData(uint32_t uid, const std::vector<std::string> & data) const;
137 int SaveUserIPs(uint32_t uid, const USER_IPS & ips) const;
139 void MakeDate(std::string & date, int year = 0, int month = 0) const;
143 int CheckVersion() const;
145 std::string versionString;
146 mutable std::string strError;
148 std::string database;
150 std::string password;
151 std::string clientEncoding;
152 MODULE_SETTINGS settings;
153 mutable pthread_mutex_t mutex;
159 extern const volatile time_t stgTime;
161 #endif //POSTGRESQL_STORE_H