]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
Use std::lock_guard instead of STG_LOCKER.
[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 "postgresql_store.h"
42
43 #include "stg/common.h" // str2x, printfd
44
45 #include <string>
46 #include <vector>
47
48 #include <libpq-fe.h>
49
50 extern "C" STG::Store* GetStore()
51 {
52     static POSTGRESQL_STORE plugin;
53     return &plugin;
54 }
55
56 //-----------------------------------------------------------------------------
57 POSTGRESQL_STORE::POSTGRESQL_STORE()
58     : versionString("postgresql_store v.1.3"),
59       server("localhost"),
60       database("stargazer"),
61       user("stg"),
62       password("123456"),
63       clientEncoding("KOI8"),
64       version(0),
65       retries(3),
66       connection(NULL),
67       logger(STG::PluginLogger::get("store_postgresql"))
68 {
69 }
70 //-----------------------------------------------------------------------------
71 POSTGRESQL_STORE::~POSTGRESQL_STORE()
72 {
73 if (connection)
74     {
75     PQfinish(connection);
76     }
77 }
78 //-----------------------------------------------------------------------------
79 int POSTGRESQL_STORE::ParseSettings()
80 {
81 std::vector<STG::ParamValue>::iterator i;
82
83 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
84     {
85     if (i->value.empty())
86         continue;
87     std::string s = ToLower(i->param);
88     if (s == "server")
89         {
90         server = i->value.front();
91         }
92     if (s == "database")
93         {
94         database = i->value.front();
95         }
96     if (s == "user")
97         {
98         user = i->value.front();
99         }
100     if (s == "password")
101         {
102         password = i->value.front();
103         }
104     if (s == "retries")
105         {
106         if (str2x(i->value.front(), retries))
107             {
108             strError = "Invalid 'retries' value";
109             printfd(__FILE__, "POSTGRESQL_STORE::ParseSettings(): '%s'\n", strError.c_str());
110             return -1;
111             }
112         }
113     }
114
115 clientEncoding = "KOI8";
116
117 return Connect();
118 }
119 //-----------------------------------------------------------------------------
120 int POSTGRESQL_STORE::Connect()
121 {
122 std::string params;
123 params = "host=" + server + " "
124        + "dbname=" + database + " "
125        + "user=" + user + " "
126        + "password=" + password;
127
128 connection = PQconnectdb(params.c_str());
129
130 if (PQstatus(connection) != CONNECTION_OK)
131     {
132     strError = PQerrorMessage(connection);
133     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
134     // Will try to connect later
135     return 0;
136     }
137
138 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
139     {
140     strError = PQerrorMessage(connection);
141     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
142     return 1;
143     }
144
145 return CheckVersion();
146 }
147 //-----------------------------------------------------------------------------
148 int POSTGRESQL_STORE::Reset() const
149 {
150 for (int i = 0; i < retries && PQstatus(connection) != CONNECTION_OK; ++i)
151     {
152     struct timespec ts = {1, 0};
153     nanosleep(&ts, NULL);
154     PQreset(connection);
155     }
156
157 if (PQstatus(connection) != CONNECTION_OK)
158     {
159     strError = PQerrorMessage(connection);
160     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
161     return 1;
162     }
163
164 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
165     {
166     strError = PQerrorMessage(connection);
167     printfd(__FILE__, "POSTGRESQL_STORE::Reset(): '%s'\n", strError.c_str());
168     return -1;
169     }
170
171 return CheckVersion();
172 }
173 //-----------------------------------------------------------------------------
174 int POSTGRESQL_STORE::CheckVersion() const
175 {
176
177 if (StartTransaction())
178     {
179     strError = "Failed to start transaction";
180     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
181     return -1;
182     }
183
184 PGresult * result = PQexec(connection, "SELECT MAX(version) FROM tb_info");
185
186 if (PQresultStatus(result) != PGRES_TUPLES_OK)
187     {
188     strError = PQresultErrorMessage(result);
189     PQclear(result);
190     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n");
191     RollbackTransaction();
192     return -1;
193     }
194
195 if (str2x(PQgetvalue(result, 0, 0), version))
196     {
197     strError = "Invalid DB version";
198     PQclear(result);
199     RollbackTransaction();
200     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
201     return -1;
202     }
203
204 PQclear(result);
205
206 if (version < DB_MIN_VERSION)
207     {
208     strError = "DB version too old";
209     RollbackTransaction();
210     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
211     return -1;
212     }
213
214 if (version < 6)
215     {
216     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);
217     }
218
219 if (CommitTransaction())
220     {
221     strError = "Failed to commit transaction";
222     printfd(__FILE__, "POSTGRESQL_STORE::CheckVersion(): '%s'\n", strError.c_str());
223     return -1;
224     }
225
226 logger("POSTGRESQL_STORE: Current DB schema version: %d", version);
227
228 return 0;
229 }
230 //-----------------------------------------------------------------------------