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
38 #include <mysql_version.h>
51 Error() : error(NULL) {}
53 Error &clear() { error = NULL; return *this; }
54 Error &set(const char *err) { error = err; return *this; }
56 const char *string() const { return error; }
58 operator bool() const { return error != NULL; }
59 bool operator!() const { return error == NULL; }
62 std::ostream &operator<<(std::ostream &stream, const Error &error);
78 mysql_free_result(result);
87 : result(0), refcount(NULL)
90 Result(MYSQL_RES *res)
94 refcount = new int(1);
99 Result(const Result &result)
100 : result(result.result), refcount(result.refcount)
112 operator=(MYSQL_RES *res)
117 refcount = new int(1);
123 operator=(const Result &res)
127 refcount = res.refcount;
134 operator bool() const { return result != NULL; }
135 bool operator!() const { return result == NULL; }
141 return mysql_num_fields(result);
147 return mysql_fetch_row(result);
153 return mysql_fetch_lengths(result);
157 typedef MYSQL_ROW Row;
169 std::string _database;
173 virtual ~Connection();
175 bool connected() const { return valid; }
176 bool connect(const std::string &host, const std::string &user,
177 const std::string &passwd, const std::string &database);
182 operator MYSQL *() { return &mysql; }
185 bool query(const std::string &sql);
188 query(const std::stringstream &sql)
190 return query(sql.str());
194 autocommit(bool mode)
196 return mysql_autocommit(&mysql, mode);
202 return mysql_commit(&mysql);
208 return mysql_rollback(&mysql);
214 return mysql_field_count(&mysql);
220 return mysql_affected_rows(&mysql);
226 return mysql_insert_id(&mysql);
234 Result result = mysql_store_result(&mysql);
236 error.set(mysql_error(&mysql));
246 BindProxy(MYSQL_BIND *b) : bind(b) {}
248 void operator=(bool &buffer)
250 bind->buffer_type = MYSQL_TYPE_TINY;
251 bind->buffer = (char *)&buffer;
254 void operator=(int8_t &buffer)
256 bind->buffer_type = MYSQL_TYPE_TINY;
257 bind->buffer = (char *)&buffer;
260 void operator=(int16_t &buffer)
262 bind->buffer_type = MYSQL_TYPE_SHORT;
263 bind->buffer = (char *)&buffer;
266 void operator=(int32_t &buffer)
268 bind->buffer_type = MYSQL_TYPE_LONG;
269 bind->buffer = (char *)&buffer;
272 void operator=(int64_t &buffer)
274 bind->buffer_type = MYSQL_TYPE_LONGLONG;
275 bind->buffer = (char *)&buffer;
278 void operator=(uint8_t &buffer)
280 bind->buffer_type = MYSQL_TYPE_TINY;
281 bind->buffer = (char *)&buffer;
284 void operator=(uint16_t &buffer)
286 bind->buffer_type = MYSQL_TYPE_SHORT;
287 bind->buffer = (char *)&buffer;
290 void operator=(uint32_t &buffer)
292 bind->buffer_type = MYSQL_TYPE_LONG;
293 bind->buffer = (char *)&buffer;
296 void operator=(uint64_t &buffer)
298 bind->buffer_type = MYSQL_TYPE_LONGLONG;
299 bind->buffer = (char *)&buffer;
302 void operator=(float &buffer)
304 bind->buffer_type = MYSQL_TYPE_FLOAT;
305 bind->buffer = (char *)&buffer;
308 void operator=(double &buffer)
310 bind->buffer_type = MYSQL_TYPE_DOUBLE;
311 bind->buffer = (char *)&buffer;
314 void operator=(Time &buffer)
316 bind->buffer_type = MYSQL_TYPE_DATE;
317 bind->buffer = (char *)&buffer;
320 void operator=(const char *buffer)
322 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
323 bind->buffer = buffer;
326 void operator=(const std::string &buffer)
328 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
329 bind->buffer = (char *)&buffer;
330 bind->length = buffer.length;
336 bind->is_null = null;
349 Statement(Connection &mysql)
350 : error(mysql.error), bind(NULL), size(0)
352 stmt = mysql_stmt_init(mysql);
353 assert(valid() && "mysql_stmt_init(), out of memory\n");
360 if (mysql_stmt_close(stmt))
361 error.set(mysql_stmt_error(stmt));
372 void prepare(const std::string &query)
376 if (mysql_stmt_prepare(mysql, query, strlen(query)))
377 mysql.error.set(mysql_stmt_error(stmt));
380 bind = new MYSQL_BIND[size];
386 return mysql_stmt_param_count(stmt);
392 return mysql_stmt_affected_rows(stmt);
395 void bind(MYSQL_BIND *bind)
398 if (mysql_stmt_bind_param(stmt, bind))
399 mysql.error.set(mysql_stmt_error(stmt));
402 BindProxy operator[](int index)
404 assert(index > 0 && index < N);
408 operator MYSQL_BIND *()
417 if (mysql_stmt_execute(stmt))
418 error.set(mysql_stmt_error(stmt));
423 /* namespace MySQL */ }
425 #endif // __BASE_MYSQL_HH__