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
47 ~POSTGRESQL_STORE() override;
50 int GetUsersList(std::vector<std::string> * usersList) const override;
51 int AddUser(const std::string & login) const override;
52 int DelUser(const std::string & login) const override;
53 int SaveUserStat(const STG::UserStat & stat, const std::string & login) const override;
54 int SaveUserConf(const STG::UserConf & conf, const std::string & login) const override;
55 int RestoreUserStat(STG::UserStat * stat, const std::string & login) const override;
56 int RestoreUserConf(STG::UserConf * conf, const std::string & login) const override;
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 override;
64 int WriteUserConnect(const std::string & login, uint32_t ip) const override;
65 int WriteUserDisconnect(const std::string & login,
66 const STG::DirTraff & up,
67 const STG::DirTraff & down,
68 const STG::DirTraff & sessionUp,
69 const STG::DirTraff & sessionDown,
72 const std::string & reason) const override;
73 int WriteDetailedStat(const STG::TraffStat & statTree,
75 const std::string & login) const override;
78 int AddMessage(STG::Message * msg, const std::string & login) const override;
79 int EditMessage(const STG::Message & msg, const std::string & login) const override;
80 int GetMessage(uint64_t id, STG::Message * msg, const std::string & login) const override;
81 int DelMessage(uint64_t id, const std::string & login) const override;
82 int GetMessageHdrs(std::vector<STG::Message::Header> * hdrsList, const std::string & login) const override;
85 int SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const override;
88 int GetAdminsList(std::vector<std::string> * adminsList) const override;
89 int SaveAdmin(const STG::AdminConf & ac) const override;
90 int RestoreAdmin(STG::AdminConf * ac, const std::string & login) const override;
91 int AddAdmin(const std::string & login) const override;
92 int DelAdmin(const std::string & login) const override;
95 int GetTariffsList(std::vector<std::string> * tariffsList) const override;
96 int AddTariff(const std::string & name) const override;
97 int DelTariff(const std::string & name) const override;
98 int SaveTariff(const STG::TariffData & td, const std::string & tariffName) const override;
99 int RestoreTariff(STG::TariffData * td, const std::string & tariffName) const override;
102 int GetCorpsList(std::vector<std::string> * corpsList) const override;
103 int SaveCorp(const STG::CorpConf & cc) const override;
104 int RestoreCorp(STG::CorpConf * cc, const std::string & name) const override;
105 int AddCorp(const std::string & name) const override;
106 int DelCorp(const std::string & name) const override;
109 int GetServicesList(std::vector<std::string> * servicesList) const override;
110 int SaveService(const STG::ServiceConf & sc) const override;
111 int RestoreService(STG::ServiceConf * sc, const std::string & name) const override;
112 int AddService(const std::string & name) const override;
113 int DelService(const std::string & name) const override;
116 void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
117 int ParseSettings() override;
119 const std::string & GetStrError() const override { return strError; }
120 const std::string & GetVersion() const override { 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 int SaveStat(const STG::UserStat & stat, const std::string & login, int year = 0, int month = 0) const;
133 int SaveUserServices(uint32_t uid, const std::vector<std::string> & services) const;
134 int SaveUserData(uint32_t uid, const std::vector<std::string> & data) const;
135 int SaveUserIPs(uint32_t uid, const STG::UserIPs & ips) const;
137 void MakeDate(std::string & date, int year = 0, int month = 0) const;
141 int CheckVersion() const;
143 std::string versionString;
144 mutable std::string strError;
146 std::string database;
148 std::string password;
149 std::string clientEncoding;
150 STG::ModuleSettings settings;
151 mutable pthread_mutex_t mutex;
157 STG::PluginLogger logger;