source: tags/0.5/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    int rc;
81    std::string cmd = "DROP TABLE IF EXISTS " + this->sdb_name;
82    this->stmt->execute(cmd);
83
84    cmd = "CREATE TABLE " + this->sdb_name +
85          "(pkey CHAR(20) NOT NULL,"
86          " PRIMARY KEY (pkey),"
87          " INDEX(pkey),"
88          " data VARCHAR(" +
89            NumberToString(this->reclen) +
90          +") NOT NULL)";
91    this->stmt->execute(cmd);
92  }
93  catch (sql::SQLException &e)
94  {
95    std::cerr << "# ERR: SQLException in " << __FILE__;
96    std::cerr << "(" << __FUNCTION__ << ") on line "
97       << __LINE__ << std::endl;
98    std::cerr << "# ERR: " << e.what();
99    std::cerr << " (MySQL error code: " << e.getErrorCode();
100    std::cerr << ", SQLState: " << e.getSQLState() <<
101   " )" << std::endl;
102    return PURGEERROR;
103  }
104    return 0;
105}
106
107int mysql_pool::open_db(void)
108{
109  if (! this->db_name)
110    return OPENERROR;
111
112  if (this->db_open)
113    return 0;
114
115  try
116  {
117    sql::ResultSet *res;
118    std::string cmd;
119    cmd="select count(*) as count "
120        "from information_schema.tables "
121        "where table_schema='stopos' "
122        "and table_name = '"+this->sdb_name+"'";
123    res = this->stmt->executeQuery(cmd);
124    res->beforeFirst();
125    if (res->next())
126      if (res->getInt("count") == 0)
127        return OPENERROR;
128    delete res;
129
130    cmd = "lock tables " + this->sdb_name + " write";
131    this->stmt->execute(cmd);
132  }
133  catch (sql::SQLException &e)
134  {
135    std::cerr << "# ERR: SQLException in " << __FILE__;
136    std::cerr << "(" << __FUNCTION__ << ") on line "
137       << __LINE__ << std::endl;
138    std::cerr << "# ERR: " << e.what();
139    std::cerr << " (MySQL error code: " << e.getErrorCode();
140    std::cerr << ", SQLState: " << e.getSQLState() <<
141   " )" << std::endl;
142    return OPENERROR;
143  }
144
145  this->db_open = 1;
146
147  return 0;
148}
149
150int mysql_pool::close_db(void)
151{
152  if (!this->db_open)
153    return 0;
154
155  std::string cmd;
156  try
157  {
158    cmd = "unlock tables";
159    this->stmt->execute(cmd);
160  }
161  catch (sql::SQLException &e)
162  {
163    std::cerr << "# ERR: SQLException in " << __FILE__;
164    std::cerr << "(" << __FUNCTION__ << ") on line "
165       << __LINE__ << std::endl;
166    std::cerr << "# ERR: " << e.what();
167    std::cerr << " (MySQL error code: " << e.getErrorCode();
168    std::cerr << ", SQLState: " << e.getSQLState() <<
169   " )" << std::endl;
170    return OPENERROR;
171  }
172
173  this->db_open = 0;
174
175  return 0;
176}
177
178void mysql_pool::dump_db(void)
179{
180
181}
182
183int mysql_pool::get_record(datarecord &r, const stopos_key &k)
184{
185  try
186  {
187    std::string cmd;
188    sql::ResultSet *res;
189    cmd = "select data from " + sdb_name +
190        " where pkey = '" + k.str() + "'";
191    res = this->stmt->executeQuery(cmd);
192    res->beforeFirst();
193    if (res->next() == 0)
194      return NOTFOUND;
195    std::string rec = res->getString("data");
196    r = datarecord(rec,dbsep);
197    delete res;
198  }
199  catch (sql::SQLException &e)
200  {
201    std::cerr << "# ERR: SQLException in " << __FILE__;
202    std::cerr << "(" << __FUNCTION__ << ") on line "
203       << __LINE__ << std::endl;
204    std::cerr << "# ERR: " << e.what();
205    std::cerr << " (MySQL error code: " << e.getErrorCode();
206    std::cerr << ", SQLState: " << e.getSQLState() <<
207   " )" << std::endl;
208    return NOTFOUND;
209  }
210
211  return 0;
212}
213
214int mysql_pool::put_record(datarecord &r, const stopos_key &k)
215{
216  try
217  {
218    std::string cmd;
219
220    cmd = "replace " + sdb_name +
221      " values('" + k.str() + "','" +
222      r.str(dbsep) + "')";
223
224    this->stmt->execute(cmd);
225  }
226  catch (sql::SQLException &e)
227  {
228    std::cerr << "# ERR: SQLException in " << __FILE__;
229    std::cerr << "(" << __FUNCTION__ << ") on line "
230       << __LINE__ << std::endl;
231    std::cerr << "# ERR: " << e.what();
232    std::cerr << " (MySQL error code: " << e.getErrorCode();
233    std::cerr << ", SQLState: " << e.getSQLState() <<
234   " )" << std::endl;
235    return STOREERROR;
236  }
237
238  return 0;
239}
240
241int mysql_pool::remove_record(const stopos_key &k)
242{
243  try
244  {
245    std::string cmd;
246
247    cmd = "delete from " + sdb_name +
248          " where pkey ='" + k.str() + "'";
249
250    this->stmt->execute(cmd);
251  }
252  catch (sql::SQLException &e)
253  {
254    std::cerr << "# ERR: SQLException in " << __FILE__;
255    std::cerr << "(" << __FUNCTION__ << ") on line "
256       << __LINE__ << std::endl;
257    std::cerr << "# ERR: " << e.what();
258    std::cerr << " (MySQL error code: " << e.getErrorCode();
259    std::cerr << ", SQLState: " << e.getSQLState() <<
260   " )" << std::endl;
261    return STOREERROR;
262  }
263
264  return 0;
265}
266
267int mysql_pool::get_status(void)
268{
269  try
270  {
271    std::string cmd;
272    sql::ResultSet *res;
273
274    cmd = "select data from " + sdb_name +
275        " where pkey = '" + this->statuskey.str() + "'";
276
277    res = this->stmt->executeQuery(cmd);
278    res->beforeFirst();
279    if (res->next() == 0)
280    {
281      return NOTFOUND;
282    }
283
284    std::string rec = res->getString("data");
285    this->status = statusrecord(rec,dbsep);
286    delete res;
287  }
288  catch (sql::SQLException &e)
289  {
290    std::cerr << "# ERR: SQLException in " << __FILE__;
291    std::cerr << "(" << __FUNCTION__ << ") on line "
292       << __LINE__ << std::endl;
293    std::cerr << "# ERR: " << e.what();
294    std::cerr << " (MySQL error code: " << e.getErrorCode();
295    std::cerr << ", SQLState: " << e.getSQLState() <<
296   " )" << std::endl;
297    return FETCHERROR;
298  }
299
300  return 0;
301}
302
303int mysql_pool::put_status(void)
304{
305  try
306  {
307    std::string cmd;
308
309    cmd = "replace " + sdb_name +
310      " values('" + statuskey.str() + "','" +
311      this->status.str(dbsep) + "')";
312
313    this->stmt->execute(cmd);
314  }
315  catch (sql::SQLException &e)
316  {
317    std::cerr << "# ERR: SQLException in " << __FILE__;
318    std::cerr << "(" << __FUNCTION__ << ") on line "
319       << __LINE__ << std::endl;
320    std::cerr << "# ERR: " << e.what();
321    std::cerr << " (MySQL error code: " << e.getErrorCode();
322    std::cerr << ", SQLState: " << e.getSQLState() <<
323   " )" << std::endl;
324    return STOREERROR;
325  }
326  return 0;
327}
328
329
Note: See TracBrowser for help on using the repository browser.