source: tags/0.56/mysql_pool.cpp @ 9

Last change on this file since 9 was 9, checked in by willem, 11 years ago

willem

File size: 7.2 KB
Line 
1#include <iostream>
2#include "wutils.h"
3#include "stopos.h"
4
5#include "mysql_connection.h"
6#include <cppconn/driver.h>
7#include <cppconn/exception.h>
8#include <cppconn/resultset.h>
9#include <cppconn/statement.h>
10#include <cppconn/prepared_statement.h>
11
12#include "mysql_pool.h"
13// construcor
14mysql_pool::mysql_pool()
15{
16  std::string schema = "stopos";
17  sql::Driver *driver;
18  driver = get_driver_instance();
19  //  connection, mysqlusername, mysqlpassword
20  this->connection = driver->connect("tcp://127.0.0.1:3307",schema,"x");
21  this->connection->setSchema(schema);
22  this->stmt = this->connection->createStatement();
23}
24
25mysql_pool::~mysql_pool(void)
26{
27  delete this->stmt;
28  delete this->connection;
29}
30
31int mysql_pool::create_db(void)
32{
33  int rc;
34  rc = this->purge_db();
35  if (rc != 0)
36    return rc;
37
38  try
39  {
40    int rc;
41
42    rc = this->open_db();
43    if (rc != 0)
44      return rc;
45
46    this->status.init();
47
48    rc = put_status();
49
50    if (rc != 0)
51    {
52      this->close_db();
53      return this->STOREERROR;
54    }
55
56    rc = this->close_db();
57    if (rc != 0)
58      return rc;
59  }
60  catch (sql::SQLException &e)
61  {
62    std::cerr << "# ERR: SQLException in " << __FILE__;
63    std::cerr << "(" << __FUNCTION__ << ") on line "
64       << __LINE__ << std::endl;
65    std::cerr << "# ERR: " << e.what();
66    std::cerr << " (MySQL error code: " << e.getErrorCode();
67    std::cerr << ", SQLState: " << e.getSQLState() <<
68   " )" << std::endl;
69    return OPENERROR;
70  }
71  return 0;
72}
73
74int mysql_pool::purge_db(void)
75{
76  if (this->db_name == 0)
77    return NOFILENAME;
78  try
79  {
80    std::string cmd = "DROP TABLE IF EXISTS " + this->sdb_name;
81    this->stmt->execute(cmd);
82
83    cmd = "CREATE TABLE " + this->sdb_name +
84          "(pkey CHAR(20) NOT NULL,"
85          " PRIMARY KEY (pkey),"
86          " INDEX(pkey),"
87          " data VARCHAR(" +
88            NumberToString(this->reclen) +
89          +") NOT NULL)";
90    this->stmt->execute(cmd);
91  }
92  catch (sql::SQLException &e)
93  {
94    std::cerr << "# ERR: SQLException in " << __FILE__;
95    std::cerr << "(" << __FUNCTION__ << ") on line "
96       << __LINE__ << std::endl;
97    std::cerr << "# ERR: " << e.what();
98    std::cerr << " (MySQL error code: " << e.getErrorCode();
99    std::cerr << ", SQLState: " << e.getSQLState() <<
100   " )" << std::endl;
101    return PURGEERROR;
102  }
103    return 0;
104}
105
106int mysql_pool::open_db(void)
107{
108  if (! this->db_name)
109    return OPENERROR;
110
111  if (this->db_open)
112    return 0;
113
114  try
115  {
116    sql::ResultSet *res;
117    std::string cmd;
118    cmd="select count(*) as count "
119        "from information_schema.tables "
120        "where table_schema='stopos' "
121        "and table_name = '"+this->sdb_name+"'";
122    res = this->stmt->executeQuery(cmd);
123    res->beforeFirst();
124    if (res->next())
125      if (res->getInt("count") == 0)
126        return OPENERROR;
127    delete res;
128
129    cmd = "lock tables " + this->sdb_name + " write";
130    this->stmt->execute(cmd);
131  }
132  catch (sql::SQLException &e)
133  {
134    std::cerr << "# ERR: SQLException in " << __FILE__;
135    std::cerr << "(" << __FUNCTION__ << ") on line "
136       << __LINE__ << std::endl;
137    std::cerr << "# ERR: " << e.what();
138    std::cerr << " (MySQL error code: " << e.getErrorCode();
139    std::cerr << ", SQLState: " << e.getSQLState() <<
140   " )" << std::endl;
141    return OPENERROR;
142  }
143
144  this->db_open = 1;
145
146  return 0;
147}
148
149int mysql_pool::close_db(void)
150{
151  if (!this->db_open)
152    return 0;
153
154  std::string cmd;
155  try
156  {
157    cmd = "unlock tables";
158    this->stmt->execute(cmd);
159  }
160  catch (sql::SQLException &e)
161  {
162    std::cerr << "# ERR: SQLException in " << __FILE__;
163    std::cerr << "(" << __FUNCTION__ << ") on line "
164       << __LINE__ << std::endl;
165    std::cerr << "# ERR: " << e.what();
166    std::cerr << " (MySQL error code: " << e.getErrorCode();
167    std::cerr << ", SQLState: " << e.getSQLState() <<
168   " )" << std::endl;
169    return OPENERROR;
170  }
171
172  this->db_open = 0;
173
174  return 0;
175}
176
177void mysql_pool::dump_db(void)
178{
179
180}
181
182int mysql_pool::get_record(std::string &r, const std::string &k)
183{
184  try
185  {
186    std::string cmd;
187    sql::ResultSet *res;
188    cmd = "select data from " + sdb_name +
189        " where pkey = '" + k + "'";
190    res = this->stmt->executeQuery(cmd);
191    res->beforeFirst();
192    if (res->next() == 0)
193      return NOTFOUND;
194    std::string rec = res->getString("data"); // wwvv todo no need for rec
195    r = rec;
196    delete res;
197  }
198  catch (sql::SQLException &e)
199  {
200    std::cerr << "# ERR: SQLException in " << __FILE__;
201    std::cerr << "(" << __FUNCTION__ << ") on line "
202       << __LINE__ << std::endl;
203    std::cerr << "# ERR: " << e.what();
204    std::cerr << " (MySQL error code: " << e.getErrorCode();
205    std::cerr << ", SQLState: " << e.getSQLState() <<
206   " )" << std::endl;
207    return NOTFOUND;
208  }
209
210  return 0;
211}
212
213int mysql_pool::put_record(const std::string &r, const std::string &k)
214{
215  try
216  {
217    std::string cmd;
218
219    cmd = "replace " + sdb_name +
220      " values('" + k + "','" +
221      r + "')";
222
223    this->stmt->execute(cmd);
224  }
225  catch (sql::SQLException &e)
226  {
227    std::cerr << "# ERR: SQLException in " << __FILE__;
228    std::cerr << "(" << __FUNCTION__ << ") on line "
229       << __LINE__ << std::endl;
230    std::cerr << "# ERR: " << e.what();
231    std::cerr << " (MySQL error code: " << e.getErrorCode();
232    std::cerr << ", SQLState: " << e.getSQLState() <<
233   " )" << std::endl;
234    return STOREERROR;
235  }
236
237  return 0;
238}
239
240int mysql_pool::remove_record(const stopos_key &k)
241{
242  try
243  {
244    std::string cmd;
245
246    cmd = "delete from " + sdb_name +
247          " where pkey ='" + k.str() + "'";
248
249    this->stmt->execute(cmd);
250  }
251  catch (sql::SQLException &e)
252  {
253    std::cerr << "# ERR: SQLException in " << __FILE__;
254    std::cerr << "(" << __FUNCTION__ << ") on line "
255       << __LINE__ << std::endl;
256    std::cerr << "# ERR: " << e.what();
257    std::cerr << " (MySQL error code: " << e.getErrorCode();
258    std::cerr << ", SQLState: " << e.getSQLState() <<
259   " )" << std::endl;
260    return STOREERROR;
261  }
262
263  return 0;
264}
265
266int mysql_pool::get_status(void)
267{
268  try
269  {
270    std::string cmd;
271    sql::ResultSet *res;
272
273    cmd = "select data from " + sdb_name +
274        " where pkey = '" + this->statuskey.str() + "'";
275
276    res = this->stmt->executeQuery(cmd);
277    res->beforeFirst();
278    if (res->next() == 0)
279    {
280      return NOTFOUND;
281    }
282
283    std::string rec = res->getString("data");
284    this->status = statusrecord(rec,dbsep);
285    delete res;
286  }
287  catch (sql::SQLException &e)
288  {
289    std::cerr << "# ERR: SQLException in " << __FILE__;
290    std::cerr << "(" << __FUNCTION__ << ") on line "
291       << __LINE__ << std::endl;
292    std::cerr << "# ERR: " << e.what();
293    std::cerr << " (MySQL error code: " << e.getErrorCode();
294    std::cerr << ", SQLState: " << e.getSQLState() <<
295   " )" << std::endl;
296    return FETCHERROR;
297  }
298
299  return 0;
300}
301
302int mysql_pool::put_status(void)
303{
304  try
305  {
306    std::string cmd;
307
308    cmd = "replace " + sdb_name +
309      " values('" + statuskey.str() + "','" +
310      this->status.str(dbsep) + "')";
311
312    this->stmt->execute(cmd);
313  }
314  catch (sql::SQLException &e)
315  {
316    std::cerr << "# ERR: SQLException in " << __FILE__;
317    std::cerr << "(" << __FUNCTION__ << ") on line "
318       << __LINE__ << std::endl;
319    std::cerr << "# ERR: " << e.what();
320    std::cerr << " (MySQL error code: " << e.getErrorCode();
321    std::cerr << ", SQLState: " << e.getSQLState() <<
322   " )" << std::endl;
323    return STOREERROR;
324  }
325  return 0;
326}
327
328
Note: See TracBrowser for help on using the repository browser.