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"
33 // Minimal DB version is 5
34 // Recommended DB version is 6 (support FreeMb logging on disconnects)
35 #define DB_MIN_VERSION 5
44 class POSTGRESQL_STORE : public STG::Store
48 ~POSTGRESQL_STORE() override;
51 int GetUsersList(std::vector<std::string> * usersList) const override;
52 int AddUser(const std::string & login) const override;
53 int DelUser(const std::string & login) const override;
54 int SaveUserStat(const STG::UserStat & stat, const std::string & login) const override;
55 int SaveUserConf(const STG::UserConf & conf, const std::string & login) const override;
56 int RestoreUserStat(STG::UserStat * stat, const std::string & login) const override;
57 int RestoreUserConf(STG::UserConf * conf, const std::string & login) const override;
58 int WriteUserChgLog(const std::string & login,
59 const std::string & admLogin,
61 const std::string & paramName,
62 const std::string & oldValue,
63 const std::string & newValue,
64 const std::string & message) const override;
65 int WriteUserConnect(const std::string & login, uint32_t ip) const override;
66 int WriteUserDisconnect(const std::string & login,
67 const STG::DirTraff & up,
68 const STG::DirTraff & down,
69 const STG::DirTraff & sessionUp,
70 const STG::DirTraff & sessionDown,
73 const std::string & reason) const override;
74 int WriteDetailedStat(const STG::TraffStat & statTree,
76 const std::string & login) const override;
79 int AddMessage(STG::Message * msg, const std::string & login) const override;
80 int EditMessage(const STG::Message & msg, const std::string & login) const override;
81 int GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const override;
82 int DelMessage(uint64_t id, const std::string & login) const override;
83 int GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList, const std::string & login) const override;
86 int SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const override;
89 int GetAdminsList(std::vector<std::string> * adminsList) const override;
90 int SaveAdmin(const STG::AdminConf & ac) const override;
91 int RestoreAdmin(STG::AdminConf * ac, const std::string & login) const override;
92 int AddAdmin(const std::string & login) const override;
93 int DelAdmin(const std::string & login) const override;
96 int GetTariffsList(std::vector<std::string> * tariffsList) const override;
97 int AddTariff(const std::string & name) const override;
98 int DelTariff(const std::string & name) const override;
99 int SaveTariff(const STG::TariffData & td, const std::string & tariffName) const override;
100 int RestoreTariff(STG::TariffData * td, const std::string & tariffName) const override;
103 int GetCorpsList(std::vector<std::string> * corpsList) const override;
104 int SaveCorp(const STG::CorpConf & cc) const override;
105 int RestoreCorp(STG::CorpConf * cc, const std::string & name) const override;
106 int AddCorp(const std::string & name) const override;
107 int DelCorp(const std::string & name) const override;
110 int GetServicesList(std::vector<std::string> * servicesList) const override;
111 int SaveService(const STG::ServiceConf & sc) const override;
112 int RestoreService(STG::ServiceConf * sc, const std::string & name) const override;
113 int AddService(const std::string & name) const override;
114 int DelService(const std::string & name) const override;
117 void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
118 int ParseSettings() override;
120 const std::string & GetStrError() const override { return strError; }
121 const std::string & GetVersion() const override { return versionString; }
123 POSTGRESQL_STORE(const POSTGRESQL_STORE & rvalue);
124 POSTGRESQL_STORE & operator=(const POSTGRESQL_STORE & rvalue);
126 int StartTransaction() const;
127 int CommitTransaction() const;
128 int RollbackTransaction() const;
130 int EscapeString(std::string & value) const;
132 int SaveStat(const STG::UserStat & stat, const std::string & login, int year = 0, int month = 0) const;
134 int SaveUserServices(uint32_t uid, const std::vector<std::string> & services) const;
135 int SaveUserData(uint32_t uid, const std::vector<std::string> & data) const;
136 int SaveUserIPs(uint32_t uid, const STG::UserIPs & ips) const;
138 void MakeDate(std::string & date, int year = 0, int month = 0) const;
142 int CheckVersion() const;
144 std::string versionString;
145 mutable std::string strError;
147 std::string database;
149 std::string password;
150 std::string clientEncoding;
151 STG::ModuleSettings settings;
152 mutable std::mutex m_mutex;
158 STG::PluginLogger logger;