2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
31 #ifndef __BASE_MYSQL_HH__
32 #define __BASE_MYSQL_HH__
34 #define TO_BE_INCLUDED_LATER 0
37 #include <mysql_version.h>
52 Error() : error(NULL) {}
54 Error &clear() { error = NULL; return *this; }
55 Error &set(const char *err) { error = err; return *this; }
57 const char *string() const { return error; }
59 operator bool() const { return error != NULL; }
60 bool operator!() const { return error == NULL; }
63 std::ostream &operator<<(std::ostream &stream, const Error &error);
79 mysql_free_result(result);
88 : result(0), refcount(NULL)
91 Result(MYSQL_RES *res)
95 refcount = new int(1);
100 Result(const Result &result)
101 : result(result.result), refcount(result.refcount)
113 operator=(MYSQL_RES *res)
118 refcount = new int(1);
124 operator=(const Result &res)
128 refcount = res.refcount;
135 operator bool() const { return result != NULL; }
136 bool operator!() const { return result == NULL; }
142 return mysql_num_fields(result);
148 return mysql_fetch_row(result);
154 return mysql_fetch_lengths(result);
158 typedef MYSQL_ROW Row;
170 std::string _database;
174 virtual ~Connection();
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);
183 operator MYSQL *() { return &mysql; }
186 bool query(const std::string &sql);
189 query(const std::stringstream &sql)
191 return query(sql.str());
195 autocommit(bool mode)
197 return mysql_autocommit(&mysql, mode);
203 return mysql_commit(&mysql);
209 return mysql_rollback(&mysql);
215 return mysql_field_count(&mysql);
221 return mysql_affected_rows(&mysql);
227 return mysql_insert_id(&mysql);
235 Result result = mysql_store_result(&mysql);
237 error.set(mysql_error(&mysql));
247 BindProxy(MYSQL_BIND *b) : bind(b) {}
249 void operator=(bool &buffer)
251 bind->buffer_type = MYSQL_TYPE_TINY;
252 bind->buffer = (char *)&buffer;
255 void operator=(int8_t &buffer)
257 bind->buffer_type = MYSQL_TYPE_TINY;
258 bind->buffer = (char *)&buffer;
261 void operator=(int16_t &buffer)
263 bind->buffer_type = MYSQL_TYPE_SHORT;
264 bind->buffer = (char *)&buffer;
267 void operator=(int32_t &buffer)
269 bind->buffer_type = MYSQL_TYPE_LONG;
270 bind->buffer = (char *)&buffer;
273 void operator=(int64_t &buffer)
275 bind->buffer_type = MYSQL_TYPE_LONGLONG;
276 bind->buffer = (char *)&buffer;
279 void operator=(uint8_t &buffer)
281 bind->buffer_type = MYSQL_TYPE_TINY;
282 bind->buffer = (char *)&buffer;
285 void operator=(uint16_t &buffer)
287 bind->buffer_type = MYSQL_TYPE_SHORT;
288 bind->buffer = (char *)&buffer;
291 void operator=(uint32_t &buffer)
293 bind->buffer_type = MYSQL_TYPE_LONG;
294 bind->buffer = (char *)&buffer;
297 void operator=(uint64_t &buffer)
299 bind->buffer_type = MYSQL_TYPE_LONGLONG;
300 bind->buffer = (char *)&buffer;
303 void operator=(float &buffer)
305 bind->buffer_type = MYSQL_TYPE_FLOAT;
306 bind->buffer = (char *)&buffer;
309 void operator=(double &buffer)
311 bind->buffer_type = MYSQL_TYPE_DOUBLE;
312 bind->buffer = (char *)&buffer;
315 void operator=(Time &buffer)
317 bind->buffer_type = MYSQL_TYPE_DATE;
318 bind->buffer = (char *)&buffer;
321 void operator=(const char *buffer)
323 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
324 bind->buffer = buffer;
327 void operator=(const std::string &buffer)
329 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
330 bind->buffer = (char *)&buffer;
331 bind->length = buffer.length;
337 bind->is_null = null;
350 Statement(Connection &mysql)
351 : error(mysql.error), bind(NULL), size(0)
353 stmt = mysql_stmt_init(mysql);
354 assert(valid() && "mysql_stmt_init(), out of memory\n");
361 if (mysql_stmt_close(stmt))
362 error.set(mysql_stmt_error(stmt));
373 void prepare(const std::string &query)
377 if (mysql_stmt_prepare(mysql, query, strlen(query)))
378 mysql.error.set(mysql_stmt_error(stmt));
381 bind = new MYSQL_BIND[size];
387 return mysql_stmt_param_count(stmt);
393 return mysql_stmt_affected_rows(stmt);
396 void bind(MYSQL_BIND *bind)
399 if (mysql_stmt_bind_param(stmt, bind))
400 mysql.error.set(mysql_stmt_error(stmt));
403 BindProxy operator[](int index)
405 assert(index > 0 && index < N);
409 operator MYSQL_BIND *()
418 if (mysql_stmt_execute(stmt))
419 error.set(mysql_stmt_error(stmt));
426 #endif // __BASE_MYSQL_HH__