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.
29 #ifndef __BASE_MYSQL_HH__
30 #define __BASE_MYSQL_HH__
32 #define TO_BE_INCLUDED_LATER 0
36 #include <mysql_version.h>
49 Error() : error(NULL) {}
51 Error &clear() { error = NULL; return *this; }
52 Error &set(const char *err) { error = err; return *this; }
54 const char *string() const { return error; }
56 operator bool() const { return error != NULL; }
57 bool operator!() const { return error == NULL; }
60 std::ostream &operator<<(std::ostream &stream, const Error &error);
76 mysql_free_result(result);
85 : result(0), refcount(NULL)
88 Result(MYSQL_RES *res)
92 refcount = new int(1);
97 Result(const Result &result)
98 : result(result.result), refcount(result.refcount)
110 operator=(MYSQL_RES *res)
115 refcount = new int(1);
121 operator=(const Result &res)
125 refcount = res.refcount;
132 operator bool() const { return result != NULL; }
133 bool operator!() const { return result == NULL; }
139 return mysql_num_fields(result);
145 return mysql_fetch_row(result);
151 return mysql_fetch_lengths(result);
155 typedef MYSQL_ROW Row;
167 std::string _database;
171 virtual ~Connection();
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);
180 operator MYSQL *() { return &mysql; }
183 bool query(const std::string &sql);
186 query(const std::stringstream &sql)
188 return query(sql.str());
192 autocommit(bool mode)
194 return mysql_autocommit(&mysql, mode);
200 return mysql_commit(&mysql);
206 return mysql_rollback(&mysql);
212 return mysql_field_count(&mysql);
218 return mysql_affected_rows(&mysql);
224 return mysql_insert_id(&mysql);
232 Result result = mysql_store_result(&mysql);
234 error.set(mysql_error(&mysql));
244 BindProxy(MYSQL_BIND *b) : bind(b) {}
246 void operator=(bool &buffer)
248 bind->buffer_type = MYSQL_TYPE_TINY;
249 bind->buffer = (char *)&buffer;
252 void operator=(int8_t &buffer)
254 bind->buffer_type = MYSQL_TYPE_TINY;
255 bind->buffer = (char *)&buffer;
258 void operator=(int16_t &buffer)
260 bind->buffer_type = MYSQL_TYPE_SHORT;
261 bind->buffer = (char *)&buffer;
264 void operator=(int32_t &buffer)
266 bind->buffer_type = MYSQL_TYPE_LONG;
267 bind->buffer = (char *)&buffer;
270 void operator=(int64_t &buffer)
272 bind->buffer_type = MYSQL_TYPE_LONGLONG;
273 bind->buffer = (char *)&buffer;
276 void operator=(uint8_t &buffer)
278 bind->buffer_type = MYSQL_TYPE_TINY;
279 bind->buffer = (char *)&buffer;
282 void operator=(uint16_t &buffer)
284 bind->buffer_type = MYSQL_TYPE_SHORT;
285 bind->buffer = (char *)&buffer;
288 void operator=(uint32_t &buffer)
290 bind->buffer_type = MYSQL_TYPE_LONG;
291 bind->buffer = (char *)&buffer;
294 void operator=(uint64_t &buffer)
296 bind->buffer_type = MYSQL_TYPE_LONGLONG;
297 bind->buffer = (char *)&buffer;
300 void operator=(float &buffer)
302 bind->buffer_type = MYSQL_TYPE_FLOAT;
303 bind->buffer = (char *)&buffer;
306 void operator=(double &buffer)
308 bind->buffer_type = MYSQL_TYPE_DOUBLE;
309 bind->buffer = (char *)&buffer;
312 void operator=(Time &buffer)
314 bind->buffer_type = MYSQL_TYPE_DATE;
315 bind->buffer = (char *)&buffer;
318 void operator=(const char *buffer)
320 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
321 bind->buffer = buffer;
324 void operator=(const std::string &buffer)
326 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
327 bind->buffer = (char *)&buffer;
328 bind->length = buffer.length;
334 bind->is_null = null;
347 Statement(Connection &mysql)
348 : error(mysql.error), bind(NULL), size(0)
350 stmt = mysql_stmt_init(mysql);
351 assert(valid() && "mysql_stmt_init(), out of memory\n");
358 if (mysql_stmt_close(stmt))
359 error.set(mysql_stmt_error(stmt));
370 void prepare(const std::string &query)
374 if (mysql_stmt_prepare(mysql, query, strlen(query)))
375 mysql.error.set(mysql_stmt_error(stmt));
378 bind = new MYSQL_BIND[size];
384 return mysql_stmt_param_count(stmt);
390 return mysql_stmt_affected_rows(stmt);
393 void bind(MYSQL_BIND *bind)
396 if (mysql_stmt_bind_param(stmt, bind))
397 mysql.error.set(mysql_stmt_error(stmt));
400 BindProxy operator[](int index)
402 assert(index > 0 && index < N);
406 operator MYSQL_BIND *()
415 if (mysql_stmt_execute(stmt))
416 error.set(mysql_stmt_error(stmt));
421 /* namespace MySQL */ }
423 #endif // __BASE_MYSQL_HH__