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
37 #include "stg/store.h"
38 #include "stg/logger.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 class POSTGRESQL_STORE : public STORE {
47 virtual ~POSTGRESQL_STORE();
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,
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,
67 const DIR_TRAFF & down,
68 const DIR_TRAFF & sessionUp,
69 const DIR_TRAFF & sessionDown,
72 const std::string & reason) const;
73 int WriteDetailedStat(const TRAFF_STAT & statTree,
75 const std::string & login) const;
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;
85 int SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const;
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;
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;
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;
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;
116 inline void SetSettings(const MODULE_SETTINGS & s) { settings = s; }
119 inline const std::string & GetStrError() const { return strError; }
120 inline const std::string & GetVersion() const { return versionString; }
122 POSTGRESQL_STORE(const POSTGRESQL_STORE & rvalue);
123 POSTGRESQL_STORE & operator=(const POSTGRESQL_STORE & rvalue);
125 int StartTransaction() const;
126 int CommitTransaction() const;
127 int RollbackTransaction() const;
129 int EscapeString(std::string & value) const;
131 std::string Int2TS(time_t value) const;
132 time_t TS2Int(const std::string & value) const;
134 int SaveStat(const USER_STAT & stat, const std::string & login, int year = 0, int month = 0) const;
136 int SaveUserServices(uint32_t uid, const std::vector<std::string> & services) const;
137 int SaveUserData(uint32_t uid, const std::vector<std::string> & data) const;
138 int SaveUserIPs(uint32_t uid, const USER_IPS & ips) const;
140 void MakeDate(std::string & date, int year = 0, int month = 0) const;
144 int CheckVersion() const;
146 std::string versionString;
147 mutable std::string strError;
149 std::string database;
151 std::string password;
152 std::string clientEncoding;
153 MODULE_SETTINGS settings;
154 mutable pthread_mutex_t mutex;
160 PLUGIN_LOGGER logger;
163 #endif //POSTGRESQL_STORE_H