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>
23 #include "stg/store.h"
24 #include "stg/module_settings.h"
25 #include "stg/logger.h"
32 // Minimal DB version is 5
33 // Recommended DB version is 6 (support FreeMb logging on disconnects)
34 #define DB_MIN_VERSION 5
43 class POSTGRESQL_STORE : public STG::Store {
46 ~POSTGRESQL_STORE() override;
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,
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,
71 const std::string & reason) const override;
72 int WriteDetailedStat(const STG::TraffStat & statTree,
74 const std::string & login) const override;
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;
84 int SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const override;
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;
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;
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;
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;
115 void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
116 int ParseSettings() override;
118 const std::string & GetStrError() const override { return strError; }
119 const std::string & GetVersion() const override { return versionString; }
121 POSTGRESQL_STORE(const POSTGRESQL_STORE & rvalue);
122 POSTGRESQL_STORE & operator=(const POSTGRESQL_STORE & rvalue);
124 int StartTransaction() const;
125 int CommitTransaction() const;
126 int RollbackTransaction() const;
128 int EscapeString(std::string & value) const;
130 int SaveStat(const STG::UserStat & stat, const std::string & login, int year = 0, int month = 0) const;
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;
136 void MakeDate(std::string & date, int year = 0, int month = 0) const;
140 int CheckVersion() const;
142 std::string versionString;
143 mutable std::string strError;
145 std::string database;
147 std::string password;
148 std::string clientEncoding;
149 STG::ModuleSettings settings;
150 mutable pthread_mutex_t mutex;
156 STG::PluginLogger logger;