Move the query function to the cc file and make trace stuff work
[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 query(const std::string &sql);
181
182 bool
183 query(const std::stringstream &sql)
184 {
185 return query(sql.str());
186 }
187
188 unsigned
189 field_count()
190 {
191 return mysql_field_count(&mysql);
192 }
193
194 unsigned
195 affected_rows()
196 {
197 return mysql_affected_rows(&mysql);
198 }
199
200 unsigned
201 insert_id()
202 {
203 return mysql_insert_id(&mysql);
204 }
205
206
207 Result
208 store_result()
209 {
210 error.clear();
211 Result result = mysql_store_result(&mysql);
212 if (!result)
213 error.set(mysql_error(&mysql));
214
215 return result;
216 }
217 };
218
219 #if 0
220 class BindProxy
221 {
222 MYSQL_BIND *bind;
223 BindProxy(MYSQL_BIND *b) : bind(b) {}
224
225 void operator=(bool &buffer)
226 {
227 bind->buffer_type = MYSQL_TYPE_TINY;
228 bind->buffer = (char *)&buffer;
229 }
230
231 void operator=(int8_t &buffer)
232 {
233 bind->buffer_type = MYSQL_TYPE_TINY;
234 bind->buffer = (char *)&buffer;
235 }
236
237 void operator=(int16_t &buffer)
238 {
239 bind->buffer_type = MYSQL_TYPE_SHORT;
240 bind->buffer = (char *)&buffer;
241 }
242
243 void operator=(int32_t &buffer)
244 {
245 bind->buffer_type = MYSQL_TYPE_LONG;
246 bind->buffer = (char *)&buffer;
247 }
248
249 void operator=(int64_t &buffer)
250 {
251 bind->buffer_type = MYSQL_TYPE_LONGLONG;
252 bind->buffer = (char *)&buffer;
253 }
254
255 void operator=(uint8_t &buffer)
256 {
257 bind->buffer_type = MYSQL_TYPE_TINY;
258 bind->buffer = (char *)&buffer;
259 }
260
261 void operator=(uint16_t &buffer)
262 {
263 bind->buffer_type = MYSQL_TYPE_SHORT;
264 bind->buffer = (char *)&buffer;
265 }
266
267 void operator=(uint32_t &buffer)
268 {
269 bind->buffer_type = MYSQL_TYPE_LONG;
270 bind->buffer = (char *)&buffer;
271 }
272
273 void operator=(uint64_t &buffer)
274 {
275 bind->buffer_type = MYSQL_TYPE_LONGLONG;
276 bind->buffer = (char *)&buffer;
277 }
278
279 void operator=(float &buffer)
280 {
281 bind->buffer_type = MYSQL_TYPE_FLOAT;
282 bind->buffer = (char *)&buffer;
283 }
284
285 void operator=(double &buffer)
286 {
287 bind->buffer_type = MYSQL_TYPE_DOUBLE;
288 bind->buffer = (char *)&buffer;
289 }
290
291 void operator=(Time &buffer)
292 {
293 bind->buffer_type = MYSQL_TYPE_DATE;
294 bind->buffer = (char *)&buffer;
295 }
296
297 void operator=(const char *buffer)
298 {
299 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
300 bind->buffer = buffer;
301 }
302
303 void operator=(const std::string &buffer)
304 {
305 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
306 bind->buffer = (char *)&buffer;
307 bind->length = buffer.length;
308 }
309
310 bool
311 set_null(bool null)
312 {
313 bind->is_null = null;
314 }
315 };
316
317 class Statement
318 {
319 protected:
320 Error &error;
321 MYSQL_STMT *stmt;
322 MYSQL_BIND *bind;
323 int size;
324
325 public:
326 Statement(Connection &mysql)
327 : error(mysql.error), bind(NULL), size(0)
328 {
329 stmt = mysql_stmt_init(mysql);
330 assert(valid() && "mysql_stmt_init(), out of memory\n");
331 }
332
333 ~Statement()
334 {
335 assert(valid());
336 error.clear();
337 if (mysql_stmt_close(stmt))
338 error.set(mysql_stmt_error(stmt));
339
340 if (bind)
341 delete [] bind;
342 }
343
344 bool valid()
345 {
346 return stmt != NULL;
347 }
348
349 void prepare(const std::string &query)
350 {
351 assert(valid());
352 mysql.error.clear();
353 if (mysql_stmt_prepare(mysql, query, strlen(query)))
354 mysql.error.set(mysql_stmt_error(stmt));
355
356 int size = count();
357 bind = new MYSQL_BIND[size];
358 }
359
360 unsigned count()
361 {
362 assert(valid());
363 return mysql_stmt_param_count(stmt);
364 }
365
366 unsigned affected()
367 {
368 assert(valid());
369 return mysql_stmt_affected_rows(stmt);
370 }
371
372 void bind(MYSQL_BIND *bind)
373 {
374 mysql.error.clear();
375 if (mysql_stmt_bind_param(stmt, bind))
376 mysql.error.set(mysql_stmt_error(stmt));
377 }
378
379 BindProxy operator[](int index)
380 {
381 assert(index > 0 && index < N);
382 return &bind[N];
383 }
384
385 operator MYSQL_BIND *()
386 {
387 return bind;
388 }
389
390 void operator()()
391 {
392 assert(valid());
393 error.clear();
394 if (mysql_stmt_execute(stmt))
395 error.set(mysql_stmt_error(stmt));
396 }
397 }
398 #endif
399
400 /* namespace MySQL */ }
401
402 #endif // __BASE_MYQSL_HH__