]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store.cpp
Добавление исходников
[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 dosconnects 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 "postgresql_store.h"
48 #include "postgresql_store_utils.h"
49 #include "base_settings.h"
50
51 class POSTGRESQL_STORE_CREATOR
52 {
53 public:
54     POSTGRESQL_STORE_CREATOR()
55         : pqStore(new POSTGRESQL_STORE())
56         {
57         };
58     ~POSTGRESQL_STORE_CREATOR()
59         {
60         delete pqStore;
61         };
62     POSTGRESQL_STORE * GetStore() { return pqStore; };
63 private:
64     POSTGRESQL_STORE * pqStore;
65 } pqStoreeCreator;
66
67 //-----------------------------------------------------------------------------
68 BASE_STORE * GetStore()
69 {
70 return pqStoreeCreator.GetStore();
71 }
72
73 //-----------------------------------------------------------------------------
74 POSTGRESQL_STORE::POSTGRESQL_STORE()
75     : connection(NULL)
76 {
77 server = "localhost";
78 database = "stargazer";
79 user = "stg";
80 password = "123456";
81 versionString = "postgresql_store v.1.3";
82 pthread_mutex_init(&mutex, NULL);
83 }
84 //-----------------------------------------------------------------------------
85 POSTGRESQL_STORE::~POSTGRESQL_STORE()
86 {
87 if (connection)
88     {
89     PQfinish(connection);
90     }
91 pthread_mutex_destroy(&mutex);
92 }
93 //-----------------------------------------------------------------------------
94 int POSTGRESQL_STORE::ParseSettings()
95 {
96 std::vector<PARAM_VALUE>::iterator i;
97 string s;
98
99 for(i = settings.moduleParams.begin(); i != settings.moduleParams.end(); ++i)
100     {
101     s = i->param;
102     std::transform(s.begin(), s.end(), s.begin(), ToLower());
103     if (s == "server")
104         {
105         server = *(i->value.begin());
106         }
107     if (s == "database")
108         {
109         database = *(i->value.begin());
110         }
111     if (s == "user")
112         {
113         user = *(i->value.begin());
114         }
115     if (s == "password")
116         {
117         password = *(i->value.begin());
118         }
119     }
120
121 clientEncoding = "KOI8";
122
123 return Connect();
124 }
125 //-----------------------------------------------------------------------------
126 int POSTGRESQL_STORE::Connect()
127 {
128 std::string params;
129 params = "host=" + server + " "
130        + "dbname=" + database + " "
131        + "user=" + user + " "
132        + "password=" + password;
133
134 connection = PQconnectdb(params.c_str());
135
136 if (PQstatus(connection) != CONNECTION_OK)
137     {
138     strError = PQerrorMessage(connection);
139     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
140     return 1;
141     }
142
143 if (PQsetClientEncoding(connection, clientEncoding.c_str()))
144     {
145     strError = PQerrorMessage(connection);
146     printfd(__FILE__, "POSTGRESQL_STORE::Connect(): '%s'\n", strError.c_str());
147     return 1;
148     }
149
150 return CheckVersion();
151 }
152 //-----------------------------------------------------------------------------
153 int POSTGRESQL_STORE::Reset() const
154 {
155 PQreset(connection);
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 return 0;
227 }
228 //-----------------------------------------------------------------------------