source: tags/0.58/mysql_pool.cpp @ 9

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

willem

File size: 5.8 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 "stopos_pool.h"
13#include "mysql_pool.h"
14// construcor
15mysql_pool::mysql_pool()
16{
17  this->whoami = "mysql";
18  std::string schema = "stopos";
19  sql::Driver *driver;
20  driver = get_driver_instance();
21  //  connection, mysqlusername, mysqlpassword
22  this->connection = driver->connect("tcp://127.0.0.1:3307",schema,"x");
23  this->connection->setSchema(schema);
24  this->stmt = this->connection->createStatement();
25}
26
27mysql_pool::~mysql_pool(void)
28{
29  delete this->stmt;
30  delete this->connection;
31}
32
33int mysql_pool::create_db(const std::string &dbname)
34{
35  int rc;
36  rc = this->purge_db();
37  if (rc != 0)
38    return rc;
39
40  try
41  {
42    int rc;
43
44    rc = this->open_db();
45    if (rc != 0)
46      return rc;
47    this->close_db();
48
49    return 0;
50  }
51  catch (sql::SQLException &e)
52  {
53    std::cerr << "# ERR: SQLException in " << __FILE__;
54    std::cerr << "(" << __FUNCTION__ << ") on line "
55       << __LINE__ << std::endl;
56    std::cerr << "# ERR: " << e.what();
57    std::cerr << " (MySQL error code: " << e.getErrorCode();
58    std::cerr << ", SQLState: " << e.getSQLState() <<
59   " )" << std::endl;
60    return OPENERROR;
61  }
62}
63
64int mysql_pool::purge_db(void)
65{
66  if (this->db_name == 0)
67    return NOFILENAME;
68  try
69  {
70    std::string cmd = "DROP TABLE IF EXISTS " + this->sdb_name;
71    this->stmt->execute(cmd);
72
73    cmd = "CREATE TABLE " + this->sdb_name +
74          "(pkey CHAR(20) NOT NULL,"
75          " PRIMARY KEY (pkey),"
76          " INDEX(pkey),"
77          " data VARCHAR(" +
78            NumberToString(this->reclen) +
79          +") NOT NULL)";
80    this->stmt->execute(cmd);
81  }
82  catch (sql::SQLException &e)
83  {
84    std::cerr << "# ERR: SQLException in " << __FILE__;
85    std::cerr << "(" << __FUNCTION__ << ") on line "
86       << __LINE__ << std::endl;
87    std::cerr << "# ERR: " << e.what();
88    std::cerr << " (MySQL error code: " << e.getErrorCode();
89    std::cerr << ", SQLState: " << e.getSQLState() <<
90   " )" << std::endl;
91    return PURGEERROR;
92  }
93    return 0;
94}
95
96int mysql_pool::open_db(void)
97{
98  if (! this->db_name)
99    return OPENERROR;
100
101  if (this->db_open)
102    return 0;
103
104  try
105  {
106    sql::ResultSet *res;
107    std::string cmd;
108    cmd="select count(*) as count "
109        "from information_schema.tables "
110        "where table_schema='stopos' "
111        "and table_name = '"+this->sdb_name+"'";
112    res = this->stmt->executeQuery(cmd);
113    res->beforeFirst();
114    if (res->next())
115      if (res->getInt("count") == 0)
116        return OPENERROR;
117    delete res;
118
119    cmd = "lock tables " + this->sdb_name + " write";
120    this->stmt->execute(cmd);
121  }
122  catch (sql::SQLException &e)
123  {
124    std::cerr << "# ERR: SQLException in " << __FILE__;
125    std::cerr << "(" << __FUNCTION__ << ") on line "
126       << __LINE__ << std::endl;
127    std::cerr << "# ERR: " << e.what();
128    std::cerr << " (MySQL error code: " << e.getErrorCode();
129    std::cerr << ", SQLState: " << e.getSQLState() <<
130   " )" << std::endl;
131    return OPENERROR;
132  }
133
134  this->db_open = 1;
135
136  return 0;
137}
138
139int mysql_pool::close_db(void)
140{
141  if (!this->db_open)
142    return 0;
143
144  std::string cmd;
145  try
146  {
147    cmd = "unlock tables";
148    this->stmt->execute(cmd);
149  }
150  catch (sql::SQLException &e)
151  {
152    std::cerr << "# ERR: SQLException in " << __FILE__;
153    std::cerr << "(" << __FUNCTION__ << ") on line "
154       << __LINE__ << std::endl;
155    std::cerr << "# ERR: " << e.what();
156    std::cerr << " (MySQL error code: " << e.getErrorCode();
157    std::cerr << ", SQLState: " << e.getSQLState() <<
158   " )" << std::endl;
159    return OPENERROR;
160  }
161
162  this->db_open = 0;
163
164  return 0;
165}
166
167void mysql_pool::dump_db(void)
168{
169
170}
171
172int mysql_pool::get_record(std::string &r, const std::string &k, const std::string &l)
173{
174  try
175  {
176    std::string cmd;
177    sql::ResultSet *res;
178    cmd = "select data from " + sdb_name +
179        " where pkey = '" + k + "'";
180    res = this->stmt->executeQuery(cmd);
181    res->beforeFirst();
182    if (res->next() == 0)
183      return NOTFOUND;
184    r = res->getString("data"); 
185    delete res;
186  }
187  catch (sql::SQLException &e)
188  {
189    std::cerr << "# ERR: SQLException in " << __FILE__;
190    std::cerr << "(" << __FUNCTION__ << ") on line "
191       << __LINE__ << std::endl;
192    std::cerr << "# ERR: " << e.what();
193    std::cerr << " (MySQL error code: " << e.getErrorCode();
194    std::cerr << ", SQLState: " << e.getSQLState() <<
195   " )" << std::endl;
196    return NOTFOUND;
197  }
198
199  return 0;
200}
201
202int mysql_pool::put_record(const std::string &r, 
203                           const std::string &k,
204                           const std::string &l)
205{
206  try
207  {
208    std::string cmd;
209
210    if (r.size() >= reclen)
211      return RECORDTOOLONG;
212
213    cmd = "replace " + sdb_name +
214      " values('" + k + "','" +
215      r + "')";
216
217    this->stmt->execute(cmd);
218  }
219  catch (sql::SQLException &e)
220  {
221    std::cerr << "# ERR: SQLException in " << __FILE__;
222    std::cerr << "(" << __FUNCTION__ << ") on line "
223       << __LINE__ << std::endl;
224    std::cerr << "# ERR: " << e.what();
225    std::cerr << " (MySQL error code: " << e.getErrorCode();
226    std::cerr << ", SQLState: " << e.getSQLState() <<
227   " )" << std::endl;
228    return STOREERROR;
229  }
230
231  return 0;
232}
233
234int mysql_pool::remove_record(const std::string &k, const std::string &l)
235{
236  try
237  {
238    std::string cmd;
239
240    cmd = "delete from " + sdb_name +
241          " where pkey ='" + k + "'";
242
243    this->stmt->execute(cmd);
244  }
245  catch (sql::SQLException &e)
246  {
247    std::cerr << "# ERR: SQLException in " << __FILE__;
248    std::cerr << "(" << __FUNCTION__ << ") on line "
249       << __LINE__ << std::endl;
250    std::cerr << "# ERR: " << e.what();
251    std::cerr << " (MySQL error code: " << e.getErrorCode();
252    std::cerr << ", SQLState: " << e.getSQLState() <<
253   " )" << std::endl;
254    return STOREERROR;
255  }
256
257  return 0;
258}
259
260
261
Note: See TracBrowser for help on using the repository browser.