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