]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
Implemented daily fee charge with backward compatibility.
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_store.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  This file contains a realization of a base postgresql-storage plugin class
23  *
24  *  v. 1.3
25  *  FreeMb logging on disconnects added
26  *
27  *  v. 1.2
28  *  Reconnection on faults added
29  *
30  *  v. 1.1
31  *  tb_stats removed
32  *
33  *  v. 1.0
34  *  Initial implementation
35  *
36  *  $Revision: 1.5 $
37  *  $Date: 2010/01/06 10:43:48 $
38  *
39  */
40
41 #include <string>
42 #include <vector>
43 #include <algorithm>
44
45 #include <libpq-fe.h>
46
47 #include "stg/module_settings.h"
48 #include "stg/plugin_creator.h"
49 #include "stg/logger.h"
50 #include "postgresql_store_utils.h"
51 #include "postgresql_store.h"
52
53 PLUGIN_CREATOR<POSTGRESQL_STORE> pqStoreeCreator;
54
55 //-----------------------------------------------------------------------------
56 STORE * GetStore()
57 {
58 return pqStoreeCreator.GetPlugin();
59 }
60
61 //-----------------------------------------------------------------------------
62 POSTGRESQL_STORE::POSTGRESQL_STORE()
63     : versionString("postgresql_store v.1.3"),
64       strError(),
65       server("localhost"),
66       database("stargazer"),
67       user("stg"),
68       password("123456"),
69       clientEncoding("KOI8"),
70       settings(),
71       mutex(),
72       version(0),
73       retries(3),
74       connection(NULL),
75       WriteServLog(GetStgLogger())
76 {
77 pthread_mutex_init(&mutex, NULL);
78 }
79 //-----------------------------------------------------------------------------
80 POSTGRESQL_STORE::~POSTGRESQL_STORE()
81 {
82 if (connection)
83     {
84     PQfinish(connection);
85     }
86 pthread_mutex_destroy(&mutex);
87 }
88 //-----------------------------------------------------------------------------
89 int POSTGRESQL_STORE::ParseSettings()
90 {
91 std::vector<PARAM_VALUE>::iterator i;
92 string s;
93
94 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
95     {
96     s = i->param;
97     std::transform(s.begin(), s.end(), s.begin(), ToLower());
98     if (s == "server")
99         {
100         server = *(i->value.begin());
101         }
102     if (s == "database")
103         {
104         database = *(i->value.begin());
105         }
106     if (s == "user")
107         {
108         user = *(i->value.begin());
109         }
110     if (s == "password")
111         {
112         password = *(i->value.begin());
113         }
114     if (s == "retries")
115         {
116         if (str2x(*(i->value.begin()), retries))
117             {
118             strError = "Invalid 'retries' value";
119             printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
120             return -1;
121             }
122         }
123     }
124
125 clientEncoding = "KOI8";
126
127 return Connect();
128 }
129 //-----------------------------------------------------------------------------
130 int POSTGRESQL_STORE::Connect()
131 {
132 std::string params;
133 params = "host=" + server + " "
134        + "dbname=" + database + " "
135        + "user=" + user + " "
136        + "password=" + password;
137
138 connection = PQconnectdb(params.c_str());
139
140 if (PQstatus(connection) != CONNECTION_OK)
141     {
142     strError = PQerrorMessage(connection);
143     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
144     // Will try to connect later
145     return 0;
146     }
147
148 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
149     {
150     strError = PQerrorMessage(connection);
151     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
152     return 1;
153     }
154
155 return CheckVersion();
156 }
157 //-----------------------------------------------------------------------------
158 int POSTGRESQL_STORE::Reset() const
159 {
160 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
161     {
162     struct timespec ts = {1, 0};
163     nanosleep(&ts, NULL);
164     PQreset(connection);
165     }
166
167 if (PQstatus(connection) != CONNECTION_OK)
168     {
169     strError = PQerrorMessage(connection);
170     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
171     return 1;
172     }
173
174 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
175     {
176     strError = PQerrorMessage(connection);
177     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
178     return -1;
179     }
180
181 return CheckVersion();
182 }
183 //-----------------------------------------------------------------------------
184 int POSTGRESQL_STORE::CheckVersion() const
185 {
186
187 if (StartTransaction())
188     {
189     strError = "Failed to start transaction";
190     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
191     return -1;
192     }
193
194 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
195
196 if (PQresultStatus(result) != PGRES_TUPLES_OK)
197     {
198     strError = PQresultErrorMessage(result);
199     PQclear(result);
200     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
201     RollbackTransaction();
202     return -1;
203     }
204
205 if (str2x(PQgetvalue(result, 0, 0), version))
206     {
207     strError = "Invalid DB version";
208     PQclear(result);
209     RollbackTransaction();
210     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
211     return -1;
212     }
213
214 PQclear(result);
215
216 if (version < DB_MIN_VERSION)
217     {
218     strError = "DB version too old";
219     RollbackTransaction();
220     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
221     return -1;
222     }
223
224 if (version < 6)
225     {
226     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): I recommend you to upgrade your DB to higher version to support FreeMb logging on disconnect. Current version is %d\n", version);
227     }
228
229 if (CommitTransaction())
230     {
231     strError = "Failed to commit transaction";
232     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
233     return -1;
234     }
235
236 WriteServLog("POSTGRESQL_STORE: Current DB schema version: %d", version);
237
238 return 0;
239 }
240 //-----------------------------------------------------------------------------