includes: sort all includes
[gem5.git] / src / base / mysql.hh
1 /*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31 #ifndef __BASE_MYSQL_HH__
32 #define __BASE_MYSQL_HH__
33
34 #define TO_BE_INCLUDED_LATER 0
35
36 #include <mysql.h>
37 #include <mysql_version.h>
38
39 #include <cassert>
40 #include <iosfwd>
41 #include <sstream>
42 #include <string>
43
44 namespace MySQL {
45
46 class Error
47 {
48 protected:
49 const char *error;
50
51 public:
52 Error() : error(NULL) {}
53
54 Error &clear() { error = NULL; return *this; }
55 Error &set(const char *err) { error = err; return *this; }
56
57 const char *string() const { return error; }
58
59 operator bool() const { return error != NULL; }
60 bool operator!() const { return error == NULL; }
61 };
62
63 std::ostream &operator<<(std::ostream &stream, const Error &error);
64
65 class Result
66 {
67 private:
68 MYSQL_RES *result;
69 int *refcount;
70
71 void
72 decref()
73 {
74 if (!refcount)
75 return;
76
77 *refcount -= 1;
78 if (*refcount == 0) {
79 mysql_free_result(result);
80 delete refcount;
81 }
82
83 refcount = NULL;
84 }
85
86 public:
87 Result()
88 : result(0), refcount(NULL)
89 { }
90
91 Result(MYSQL_RES *res)
92 : result(res)
93 {
94 if (result)
95 refcount = new int(1);
96 else
97 refcount = NULL;
98 }
99
100 Result(const Result &result)
101 : result(result.result), refcount(result.refcount)
102 {
103 if (result)
104 *refcount += 1;
105 }
106
107 ~Result()
108 {
109 decref();
110 }
111
112 const Result &
113 operator=(MYSQL_RES *res)
114 {
115 decref();
116 result = res;
117 if (result)
118 refcount = new int(1);
119
120 return *this;
121 }
122
123 const Result &
124 operator=(const Result &res)
125 {
126 decref();
127 result = res.result;
128 refcount = res.refcount;
129 if (result)
130 *refcount += 1;
131
132 return *this;
133 }
134
135 operator bool() const { return result != NULL; }
136 bool operator!() const { return result == NULL; }
137
138 unsigned
139 num_fields()
140 {
141 assert(result);
142 return mysql_num_fields(result);
143 }
144
145 MYSQL_ROW
146 fetch_row()
147 {
148 return mysql_fetch_row(result);
149 }
150
151 unsigned long *
152 fetch_lengths()
153 {
154 return mysql_fetch_lengths(result);
155 }
156 };
157
158 typedef MYSQL_ROW Row;
159
160 class Connection
161 {
162 protected:
163 MYSQL mysql;
164 bool valid;
165
166 protected:
167 std::string _host;
168 std::string _user;
169 std::string _passwd;
170 std::string _database;
171
172 public:
173 Connection();
174 virtual ~Connection();
175
176 bool connected() const { return valid; }
177 bool connect(const std::string &host, const std::string &user,
178 const std::string &passwd, const std::string &database);
179 void close();
180
181 public:
182 Error error;
183 operator MYSQL *() { return &mysql; }
184
185 public:
186 bool query(const std::string &sql);
187
188 bool
189 query(const std::stringstream &sql)
190 {
191 return query(sql.str());
192 }
193
194 bool
195 autocommit(bool mode)
196 {
197 return mysql_autocommit(&mysql, mode);
198 }
199
200 bool
201 commit()
202 {
203 return mysql_commit(&mysql);
204 }
205
206 bool
207 rollback()
208 {
209 return mysql_rollback(&mysql);
210 }
211
212 unsigned
213 field_count()
214 {
215 return mysql_field_count(&mysql);
216 }
217
218 unsigned
219 affected_rows()
220 {
221 return mysql_affected_rows(&mysql);
222 }
223
224 unsigned
225 insert_id()
226 {
227 return mysql_insert_id(&mysql);
228 }
229
230
231 Result
232 store_result()
233 {
234 error.clear();
235 Result result = mysql_store_result(&mysql);
236 if (!result)
237 error.set(mysql_error(&mysql));
238
239 return result;
240 }
241 };
242
243 #if 0
244 class BindProxy
245 {
246 MYSQL_BIND *bind;
247 BindProxy(MYSQL_BIND *b) : bind(b) {}
248
249 void operator=(bool &buffer)
250 {
251 bind->buffer_type = MYSQL_TYPE_TINY;
252 bind->buffer = (char *)&buffer;
253 }
254
255 void operator=(int8_t &buffer)
256 {
257 bind->buffer_type = MYSQL_TYPE_TINY;
258 bind->buffer = (char *)&buffer;
259 }
260
261 void operator=(int16_t &buffer)
262 {
263 bind->buffer_type = MYSQL_TYPE_SHORT;
264 bind->buffer = (char *)&buffer;
265 }
266
267 void operator=(int32_t &buffer)
268 {
269 bind->buffer_type = MYSQL_TYPE_LONG;
270 bind->buffer = (char *)&buffer;
271 }
272
273 void operator=(int64_t &buffer)
274 {
275 bind->buffer_type = MYSQL_TYPE_LONGLONG;
276 bind->buffer = (char *)&buffer;
277 }
278
279 void operator=(uint8_t &buffer)
280 {
281 bind->buffer_type = MYSQL_TYPE_TINY;
282 bind->buffer = (char *)&buffer;
283 }
284
285 void operator=(uint16_t &buffer)
286 {
287 bind->buffer_type = MYSQL_TYPE_SHORT;
288 bind->buffer = (char *)&buffer;
289 }
290
291 void operator=(uint32_t &buffer)
292 {
293 bind->buffer_type = MYSQL_TYPE_LONG;
294 bind->buffer = (char *)&buffer;
295 }
296
297 void operator=(uint64_t &buffer)
298 {
299 bind->buffer_type = MYSQL_TYPE_LONGLONG;
300 bind->buffer = (char *)&buffer;
301 }
302
303 void operator=(float &buffer)
304 {
305 bind->buffer_type = MYSQL_TYPE_FLOAT;
306 bind->buffer = (char *)&buffer;
307 }
308
309 void operator=(double &buffer)
310 {
311 bind->buffer_type = MYSQL_TYPE_DOUBLE;
312 bind->buffer = (char *)&buffer;
313 }
314
315 void operator=(Time &buffer)
316 {
317 bind->buffer_type = MYSQL_TYPE_DATE;
318 bind->buffer = (char *)&buffer;
319 }
320
321 void operator=(const char *buffer)
322 {
323 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
324 bind->buffer = buffer;
325 }
326
327 void operator=(const std::string &buffer)
328 {
329 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
330 bind->buffer = (char *)&buffer;
331 bind->length = buffer.length;
332 }
333
334 bool
335 set_null(bool null)
336 {
337 bind->is_null = null;
338 }
339 };
340
341 class Statement
342 {
343 protected:
344 Error &error;
345 MYSQL_STMT *stmt;
346 MYSQL_BIND *bind;
347 int size;
348
349 public:
350 Statement(Connection &mysql)
351 : error(mysql.error), bind(NULL), size(0)
352 {
353 stmt = mysql_stmt_init(mysql);
354 assert(valid() && "mysql_stmt_init(), out of memory\n");
355 }
356
357 ~Statement()
358 {
359 assert(valid());
360 error.clear();
361 if (mysql_stmt_close(stmt))
362 error.set(mysql_stmt_error(stmt));
363
364 if (bind)
365 delete [] bind;
366 }
367
368 bool valid()
369 {
370 return stmt != NULL;
371 }
372
373 void prepare(const std::string &query)
374 {
375 assert(valid());
376 mysql.error.clear();
377 if (mysql_stmt_prepare(mysql, query, strlen(query)))
378 mysql.error.set(mysql_stmt_error(stmt));
379
380 int size = count();
381 bind = new MYSQL_BIND[size];
382 }
383
384 unsigned count()
385 {
386 assert(valid());
387 return mysql_stmt_param_count(stmt);
388 }
389
390 unsigned affected()
391 {
392 assert(valid());
393 return mysql_stmt_affected_rows(stmt);
394 }
395
396 void bind(MYSQL_BIND *bind)
397 {
398 mysql.error.clear();
399 if (mysql_stmt_bind_param(stmt, bind))
400 mysql.error.set(mysql_stmt_error(stmt));
401 }
402
403 BindProxy operator[](int index)
404 {
405 assert(index > 0 && index < N);
406 return &bind[N];
407 }
408
409 operator MYSQL_BIND *()
410 {
411 return bind;
412 }
413
414 void operator()()
415 {
416 assert(valid());
417 error.clear();
418 if (mysql_stmt_execute(stmt))
419 error.set(mysql_stmt_error(stmt));
420 }
421 }
422 #endif
423
424 } // namespace MySQL
425
426 #endif // __BASE_MYSQL_HH__