1 | |
---|
2 | /* |
---|
3 | Copyright 2013 Willem Vermin, SURFsara |
---|
4 | |
---|
5 | Licensed under the Apache License, Version 2.0 (the "License"); |
---|
6 | you may not use this file except in compliance with the License. |
---|
7 | You may obtain a copy of the License at |
---|
8 | |
---|
9 | http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | |
---|
11 | Unless required by applicable law or agreed to in writing, software |
---|
12 | distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | See the License for the specific language governing permissions and |
---|
15 | limitations under the License. |
---|
16 | */ |
---|
17 | #include <iostream> |
---|
18 | #include "wutils.h" |
---|
19 | #include "stopos.h" |
---|
20 | |
---|
21 | #include "mysql_connection.h" |
---|
22 | #include <cppconn/driver.h> |
---|
23 | #include <cppconn/exception.h> |
---|
24 | #include <cppconn/resultset.h> |
---|
25 | #include <cppconn/statement.h> |
---|
26 | #include <cppconn/prepared_statement.h> |
---|
27 | |
---|
28 | #include "stopos_pool.h" |
---|
29 | #include "mysql_pool.h" |
---|
30 | // construcor |
---|
31 | mysql_pool::mysql_pool() |
---|
32 | { |
---|
33 | this->whoami = "mysql"; |
---|
34 | std::string schema = "stopos"; |
---|
35 | sql::Driver *driver; |
---|
36 | driver = get_driver_instance(); |
---|
37 | // connection, mysqlusername, mysqlpassword |
---|
38 | this->connection = driver->connect("tcp://127.0.0.1:3307",schema,"x"); |
---|
39 | this->connection->setSchema(schema); |
---|
40 | this->stmt = this->connection->createStatement(); |
---|
41 | } |
---|
42 | |
---|
43 | mysql_pool::~mysql_pool(void) |
---|
44 | { |
---|
45 | delete this->stmt; |
---|
46 | delete this->connection; |
---|
47 | } |
---|
48 | |
---|
49 | int mysql_pool::create_db(const std::string &dbname) |
---|
50 | { |
---|
51 | int rc; |
---|
52 | rc = this->purge_db(); |
---|
53 | if (rc != 0) |
---|
54 | return rc; |
---|
55 | |
---|
56 | try |
---|
57 | { |
---|
58 | int rc; |
---|
59 | |
---|
60 | rc = this->open_db(); |
---|
61 | if (rc != 0) |
---|
62 | return rc; |
---|
63 | this->close_db(); |
---|
64 | |
---|
65 | return 0; |
---|
66 | } |
---|
67 | catch (sql::SQLException &e) |
---|
68 | { |
---|
69 | std::cerr << "# ERR: SQLException in " << __FILE__; |
---|
70 | std::cerr << "(" << __FUNCTION__ << ") on line " |
---|
71 | << __LINE__ << std::endl; |
---|
72 | std::cerr << "# ERR: " << e.what(); |
---|
73 | std::cerr << " (MySQL error code: " << e.getErrorCode(); |
---|
74 | std::cerr << ", SQLState: " << e.getSQLState() << |
---|
75 | " )" << std::endl; |
---|
76 | return OPENERROR; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | int mysql_pool::purge_db(void) |
---|
81 | { |
---|
82 | if (this->db_name == 0) |
---|
83 | return NOFILENAME; |
---|
84 | try |
---|
85 | { |
---|
86 | std::string cmd = "DROP TABLE IF EXISTS " + this->sdb_name; |
---|
87 | this->stmt->execute(cmd); |
---|
88 | |
---|
89 | cmd = "CREATE TABLE " + this->sdb_name + |
---|
90 | "(pkey CHAR(20) NOT NULL," |
---|
91 | " PRIMARY KEY (pkey)," |
---|
92 | " INDEX(pkey)," |
---|
93 | " data VARCHAR(" + |
---|
94 | NumberToString(this->reclen) + |
---|
95 | +") NOT NULL)"; |
---|
96 | this->stmt->execute(cmd); |
---|
97 | } |
---|
98 | catch (sql::SQLException &e) |
---|
99 | { |
---|
100 | std::cerr << "# ERR: SQLException in " << __FILE__; |
---|
101 | std::cerr << "(" << __FUNCTION__ << ") on line " |
---|
102 | << __LINE__ << std::endl; |
---|
103 | std::cerr << "# ERR: " << e.what(); |
---|
104 | std::cerr << " (MySQL error code: " << e.getErrorCode(); |
---|
105 | std::cerr << ", SQLState: " << e.getSQLState() << |
---|
106 | " )" << std::endl; |
---|
107 | return PURGEERROR; |
---|
108 | } |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | int mysql_pool::open_db(void) |
---|
113 | { |
---|
114 | if (! this->db_name) |
---|
115 | return OPENERROR; |
---|
116 | |
---|
117 | if (this->db_open) |
---|
118 | return 0; |
---|
119 | |
---|
120 | try |
---|
121 | { |
---|
122 | sql::ResultSet *res; |
---|
123 | std::string cmd; |
---|
124 | cmd="select count(*) as count " |
---|
125 | "from information_schema.tables " |
---|
126 | "where table_schema='stopos' " |
---|
127 | "and table_name = '"+this->sdb_name+"'"; |
---|
128 | res = this->stmt->executeQuery(cmd); |
---|
129 | res->beforeFirst(); |
---|
130 | if (res->next()) |
---|
131 | if (res->getInt("count") == 0) |
---|
132 | return OPENERROR; |
---|
133 | delete res; |
---|
134 | |
---|
135 | cmd = "lock tables " + this->sdb_name + " write"; |
---|
136 | this->stmt->execute(cmd); |
---|
137 | } |
---|
138 | catch (sql::SQLException &e) |
---|
139 | { |
---|
140 | std::cerr << "# ERR: SQLException in " << __FILE__; |
---|
141 | std::cerr << "(" << __FUNCTION__ << ") on line " |
---|
142 | << __LINE__ << std::endl; |
---|
143 | std::cerr << "# ERR: " << e.what(); |
---|
144 | std::cerr << " (MySQL error code: " << e.getErrorCode(); |
---|
145 | std::cerr << ", SQLState: " << e.getSQLState() << |
---|
146 | " )" << std::endl; |
---|
147 | return OPENERROR; |
---|
148 | } |
---|
149 | |
---|
150 | this->db_open = 1; |
---|
151 | |
---|
152 | return 0; |
---|
153 | } |
---|
154 | |
---|
155 | int mysql_pool::close_db(void) |
---|
156 | { |
---|
157 | if (!this->db_open) |
---|
158 | return 0; |
---|
159 | |
---|
160 | std::string cmd; |
---|
161 | try |
---|
162 | { |
---|
163 | cmd = "unlock tables"; |
---|
164 | this->stmt->execute(cmd); |
---|
165 | } |
---|
166 | catch (sql::SQLException &e) |
---|
167 | { |
---|
168 | std::cerr << "# ERR: SQLException in " << __FILE__; |
---|
169 | std::cerr << "(" << __FUNCTION__ << ") on line " |
---|
170 | << __LINE__ << std::endl; |
---|
171 | std::cerr << "# ERR: " << e.what(); |
---|
172 | std::cerr << " (MySQL error code: " << e.getErrorCode(); |
---|
173 | std::cerr << ", SQLState: " << e.getSQLState() << |
---|
174 | " )" << std::endl; |
---|
175 | return OPENERROR; |
---|
176 | } |
---|
177 | |
---|
178 | this->db_open = 0; |
---|
179 | |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|
183 | int mysql_pool::get_record(std::string &r, const std::string &k, const std::string &l) |
---|
184 | { |
---|
185 | try |
---|
186 | { |
---|
187 | std::string cmd; |
---|
188 | sql::ResultSet *res; |
---|
189 | cmd = "select data from " + sdb_name + |
---|
190 | " where pkey = '" + k + "'"; |
---|
191 | res = this->stmt->executeQuery(cmd); |
---|
192 | res->beforeFirst(); |
---|
193 | if (res->next() == 0) |
---|
194 | return NOTFOUND; |
---|
195 | r = res->getString("data"); |
---|
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 | |
---|
213 | int mysql_pool::put_record(const std::string &r, |
---|
214 | const std::string &k, |
---|
215 | const std::string &l) |
---|
216 | { |
---|
217 | bool rectoolong=0; |
---|
218 | try |
---|
219 | { |
---|
220 | std::string cmd; |
---|
221 | std::string rr; |
---|
222 | rr = r; |
---|
223 | |
---|
224 | if (rr.size() >= reclen) |
---|
225 | { |
---|
226 | rectoolong = 1; |
---|
227 | rr.resize(reclen-1); |
---|
228 | } |
---|
229 | |
---|
230 | cmd = "replace " + sdb_name + |
---|
231 | " values('" + k + "','" + |
---|
232 | rr + "')"; |
---|
233 | |
---|
234 | this->stmt->execute(cmd); |
---|
235 | } |
---|
236 | catch (sql::SQLException &e) |
---|
237 | { |
---|
238 | std::cerr << "# ERR: SQLException in " << __FILE__; |
---|
239 | std::cerr << "(" << __FUNCTION__ << ") on line " |
---|
240 | << __LINE__ << std::endl; |
---|
241 | std::cerr << "# ERR: " << e.what(); |
---|
242 | std::cerr << " (MySQL error code: " << e.getErrorCode(); |
---|
243 | std::cerr << ", SQLState: " << e.getSQLState() << |
---|
244 | " )" << std::endl; |
---|
245 | return STOREERROR; |
---|
246 | } |
---|
247 | |
---|
248 | if (rectoolong) |
---|
249 | return RECORDTOOLONG; |
---|
250 | |
---|
251 | return 0; |
---|
252 | } |
---|
253 | |
---|
254 | int mysql_pool::remove_record(const std::string &k, const std::string &l) |
---|
255 | { |
---|
256 | try |
---|
257 | { |
---|
258 | std::string cmd; |
---|
259 | |
---|
260 | cmd = "delete from " + sdb_name + |
---|
261 | " where pkey ='" + k + "'"; |
---|
262 | |
---|
263 | this->stmt->execute(cmd); |
---|
264 | } |
---|
265 | catch (sql::SQLException &e) |
---|
266 | { |
---|
267 | std::cerr << "# ERR: SQLException in " << __FILE__; |
---|
268 | std::cerr << "(" << __FUNCTION__ << ") on line " |
---|
269 | << __LINE__ << std::endl; |
---|
270 | std::cerr << "# ERR: " << e.what(); |
---|
271 | std::cerr << " (MySQL error code: " << e.getErrorCode(); |
---|
272 | std::cerr << ", SQLState: " << e.getSQLState() << |
---|
273 | " )" << std::endl; |
---|
274 | return STOREERROR; |
---|
275 | } |
---|
276 | |
---|
277 | return 0; |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | |
---|