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