Merge ktlim@zizzer:/bk/m5
[gem5.git] / 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
29 #ifndef __BASE_MYSQL_HH__
30 #define __BASE_MYSQL_HH__
31
32 #define TO_BE_INCLUDED_LATER 0
33
34 #include <cassert>
35 #include <iosfwd>
36 #include <mysql_version.h>
37 #include <mysql.h>
38 #include <string>
39 #include <sstream>
40
41 namespace MySQL {
42
43 class Error
44 {
45 protected:
46 const char *error;
47
48 public:
49 Error() : error(NULL) {}
50
51 Error &clear() { error = NULL; return *this; }
52 Error &set(const char *err) { error = err; return *this; }
53
54 const char *string() const { return error; }
55
56 operator bool() const { return error != NULL; }
57 bool operator!() const { return error == NULL; }
58 };
59
60 std::ostream &operator<<(std::ostream &stream, const Error &error);
61
62 class Result
63 {
64 private:
65 MYSQL_RES *result;
66 int *refcount;
67
68 void
69 decref()
70 {
71 if (!refcount)
72 return;
73
74 *refcount -= 1;
75 if (*refcount == 0) {
76 mysql_free_result(result);
77 delete refcount;
78 }
79
80 refcount = NULL;
81 }
82
83 public:
84 Result()
85 : result(0), refcount(NULL)
86 { }
87
88 Result(MYSQL_RES *res)
89 : result(res)
90 {
91 if (result)
92 refcount = new int(1);
93 else
94 refcount = NULL;
95 }
96
97 Result(const Result &result)
98 : result(result.result), refcount(result.refcount)
99 {
100 if (result)
101 *refcount += 1;
102 }
103
104 ~Result()
105 {
106 decref();
107 }
108
109 const Result &
110 operator=(MYSQL_RES *res)
111 {
112 decref();
113 result = res;
114 if (result)
115 refcount = new int(1);
116
117 return *this;
118 }
119
120 const Result &
121 operator=(const Result &res)
122 {
123 decref();
124 result = res.result;
125 refcount = res.refcount;
126 if (result)
127 *refcount += 1;
128
129 return *this;
130 }
131
132 operator bool() const { return result != NULL; }
133 bool operator!() const { return result == NULL; }
134
135 unsigned
136 num_fields()
137 {
138 assert(result);
139 return mysql_num_fields(result);
140 }
141
142 MYSQL_ROW
143 fetch_row()
144 {
145 return mysql_fetch_row(result);
146 }
147
148 unsigned long *
149 fetch_lengths()
150 {
151 return mysql_fetch_lengths(result);
152 }
153 };
154
155 typedef MYSQL_ROW Row;
156
157 class Connection
158 {
159 protected:
160 MYSQL mysql;
161 bool valid;
162
163 protected:
164 std::string _host;
165 std::string _user;
166 std::string _passwd;
167 std::string _database;
168
169 public:
170 Connection();
171 virtual ~Connection();
172
173 bool connected() const { return valid; }
174 bool connect(const std::string &host, const std::string &user,
175 const std::string &passwd, const std::string &database);
176 void close();
177
178 public:
179 Error error;
180 operator MYSQL *() { return &mysql; }
181
182 public:
183 bool query(const std::string &sql);
184
185 bool
186 query(const std::stringstream &sql)
187 {
188 return query(sql.str());
189 }
190
191 bool
192 autocommit(bool mode)
193 {
194 return mysql_autocommit(&mysql, mode);
195 }
196
197 bool
198 commit()
199 {
200 return mysql_commit(&mysql);
201 }
202
203 bool
204 rollback()
205 {
206 return mysql_rollback(&mysql);
207 }
208
209 unsigned
210 field_count()
211 {
212 return mysql_field_count(&mysql);
213 }
214
215 unsigned
216 affected_rows()
217 {
218 return mysql_affected_rows(&mysql);
219 }
220
221 unsigned
222 insert_id()
223 {
224 return mysql_insert_id(&mysql);
225 }
226
227
228 Result
229 store_result()
230 {
231 error.clear();
232 Result result = mysql_store_result(&mysql);
233 if (!result)
234 error.set(mysql_error(&mysql));
235
236 return result;
237 }
238 };
239
240 #if 0
241 class BindProxy
242 {
243 MYSQL_BIND *bind;
244 BindProxy(MYSQL_BIND *b) : bind(b) {}
245
246 void operator=(bool &buffer)
247 {
248 bind->buffer_type = MYSQL_TYPE_TINY;
249 bind->buffer = (char *)&buffer;
250 }
251
252 void operator=(int8_t &buffer)
253 {
254 bind->buffer_type = MYSQL_TYPE_TINY;
255 bind->buffer = (char *)&buffer;
256 }
257
258 void operator=(int16_t &buffer)
259 {
260 bind->buffer_type = MYSQL_TYPE_SHORT;
261 bind->buffer = (char *)&buffer;
262 }
263
264 void operator=(int32_t &buffer)
265 {
266 bind->buffer_type = MYSQL_TYPE_LONG;
267 bind->buffer = (char *)&buffer;
268 }
269
270 void operator=(int64_t &buffer)
271 {
272 bind->buffer_type = MYSQL_TYPE_LONGLONG;
273 bind->buffer = (char *)&buffer;
274 }
275
276 void operator=(uint8_t &buffer)
277 {
278 bind->buffer_type = MYSQL_TYPE_TINY;
279 bind->buffer = (char *)&buffer;
280 }
281
282 void operator=(uint16_t &buffer)
283 {
284 bind->buffer_type = MYSQL_TYPE_SHORT;
285 bind->buffer = (char *)&buffer;
286 }
287
288 void operator=(uint32_t &buffer)
289 {
290 bind->buffer_type = MYSQL_TYPE_LONG;
291 bind->buffer = (char *)&buffer;
292 }
293
294 void operator=(uint64_t &buffer)
295 {
296 bind->buffer_type = MYSQL_TYPE_LONGLONG;
297 bind->buffer = (char *)&buffer;
298 }
299
300 void operator=(float &buffer)
301 {
302 bind->buffer_type = MYSQL_TYPE_FLOAT;
303 bind->buffer = (char *)&buffer;
304 }
305
306 void operator=(double &buffer)
307 {
308 bind->buffer_type = MYSQL_TYPE_DOUBLE;
309 bind->buffer = (char *)&buffer;
310 }
311
312 void operator=(Time &buffer)
313 {
314 bind->buffer_type = MYSQL_TYPE_DATE;
315 bind->buffer = (char *)&buffer;
316 }
317
318 void operator=(const char *buffer)
319 {
320 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
321 bind->buffer = buffer;
322 }
323
324 void operator=(const std::string &buffer)
325 {
326 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
327 bind->buffer = (char *)&buffer;
328 bind->length = buffer.length;
329 }
330
331 bool
332 set_null(bool null)
333 {
334 bind->is_null = null;
335 }
336 };
337
338 class Statement
339 {
340 protected:
341 Error &error;
342 MYSQL_STMT *stmt;
343 MYSQL_BIND *bind;
344 int size;
345
346 public:
347 Statement(Connection &mysql)
348 : error(mysql.error), bind(NULL), size(0)
349 {
350 stmt = mysql_stmt_init(mysql);
351 assert(valid() && "mysql_stmt_init(), out of memory\n");
352 }
353
354 ~Statement()
355 {
356 assert(valid());
357 error.clear();
358 if (mysql_stmt_close(stmt))
359 error.set(mysql_stmt_error(stmt));
360
361 if (bind)
362 delete [] bind;
363 }
364
365 bool valid()
366 {
367 return stmt != NULL;
368 }
369
370 void prepare(const std::string &query)
371 {
372 assert(valid());
373 mysql.error.clear();
374 if (mysql_stmt_prepare(mysql, query, strlen(query)))
375 mysql.error.set(mysql_stmt_error(stmt));
376
377 int size = count();
378 bind = new MYSQL_BIND[size];
379 }
380
381 unsigned count()
382 {
383 assert(valid());
384 return mysql_stmt_param_count(stmt);
385 }
386
387 unsigned affected()
388 {
389 assert(valid());
390 return mysql_stmt_affected_rows(stmt);
391 }
392
393 void bind(MYSQL_BIND *bind)
394 {
395 mysql.error.clear();
396 if (mysql_stmt_bind_param(stmt, bind))
397 mysql.error.set(mysql_stmt_error(stmt));
398 }
399
400 BindProxy operator[](int index)
401 {
402 assert(index > 0 && index < N);
403 return &bind[N];
404 }
405
406 operator MYSQL_BIND *()
407 {
408 return bind;
409 }
410
411 void operator()()
412 {
413 assert(valid());
414 error.clear();
415 if (mysql_stmt_execute(stmt))
416 error.set(mysql_stmt_error(stmt));
417 }
418 }
419 #endif
420
421 /* namespace MySQL */ }
422
423 #endif // __BASE_MYSQL_HH__