2 * Copyright (c) 2003-2004 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_MYQSL_HH__
30 #define __BASE_MYQSL_HH__
32 #define TO_BE_INCLUDED_LATER 0
48 Error() : error(NULL) {}
50 Error &clear() { error = NULL; return *this; }
51 Error &set(const char *err) { error = err; return *this; }
53 const char *string() const { return error; }
55 operator bool() const { return error != NULL; }
56 bool operator!() const { return error == NULL; }
59 std::ostream &operator<<(std::ostream &stream, const Error &error);
75 mysql_free_result(result);
84 : result(0), refcount(NULL)
87 Result(MYSQL_RES *res)
91 refcount = new int(1);
94 Result(const Result &result)
95 : result(result.result), refcount(result.refcount)
107 operator=(MYSQL_RES *res)
112 refcount = new int(1);
118 operator=(const Result &res)
122 refcount = res.refcount;
129 operator bool() const { return result != NULL; }
130 bool operator!() const { return result == NULL; }
136 return mysql_num_fields(result);
142 return mysql_fetch_row(result);
148 return mysql_fetch_lengths(result);
152 typedef MYSQL_ROW Row;
164 std::string _database;
168 virtual ~Connection();
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);
177 operator MYSQL *() { return &mysql; }
181 query(const std::string &sql)
184 if (mysql_real_query(&mysql, sql.c_str(), sql.size()))
185 error.set(mysql_error(&mysql));
191 query(const std::stringstream &sql)
193 return query(sql.str());
199 return mysql_field_count(&mysql);
205 return mysql_affected_rows(&mysql);
211 return mysql_insert_id(&mysql);
219 Result result = mysql_store_result(&mysql);
221 error.set(mysql_error(&mysql));
231 BindProxy(MYSQL_BIND *b) : bind(b) {}
233 void operator=(bool &buffer)
235 bind->buffer_type = MYSQL_TYPE_TINY;
236 bind->buffer = (char *)&buffer;
239 void operator=(int8_t &buffer)
241 bind->buffer_type = MYSQL_TYPE_TINY;
242 bind->buffer = (char *)&buffer;
245 void operator=(int16_t &buffer)
247 bind->buffer_type = MYSQL_TYPE_SHORT;
248 bind->buffer = (char *)&buffer;
251 void operator=(int32_t &buffer)
253 bind->buffer_type = MYSQL_TYPE_LONG;
254 bind->buffer = (char *)&buffer;
257 void operator=(int64_t &buffer)
259 bind->buffer_type = MYSQL_TYPE_LONGLONG;
260 bind->buffer = (char *)&buffer;
263 void operator=(uint8_t &buffer)
265 bind->buffer_type = MYSQL_TYPE_TINY;
266 bind->buffer = (char *)&buffer;
269 void operator=(uint16_t &buffer)
271 bind->buffer_type = MYSQL_TYPE_SHORT;
272 bind->buffer = (char *)&buffer;
275 void operator=(uint32_t &buffer)
277 bind->buffer_type = MYSQL_TYPE_LONG;
278 bind->buffer = (char *)&buffer;
281 void operator=(uint64_t &buffer)
283 bind->buffer_type = MYSQL_TYPE_LONGLONG;
284 bind->buffer = (char *)&buffer;
287 void operator=(float &buffer)
289 bind->buffer_type = MYSQL_TYPE_FLOAT;
290 bind->buffer = (char *)&buffer;
293 void operator=(double &buffer)
295 bind->buffer_type = MYSQL_TYPE_DOUBLE;
296 bind->buffer = (char *)&buffer;
299 void operator=(Time &buffer)
301 bind->buffer_type = MYSQL_TYPE_DATE;
302 bind->buffer = (char *)&buffer;
305 void operator=(const char *buffer)
307 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
308 bind->buffer = buffer;
311 void operator=(const std::string &buffer)
313 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
314 bind->buffer = (char *)&buffer;
315 bind->length = buffer.length;
321 bind->is_null = null;
334 Statement(Connection &mysql)
335 : error(mysql.error), bind(NULL), size(0)
337 stmt = mysql_stmt_init(mysql);
338 assert(valid() && "mysql_stmt_init(), out of memory\n");
345 if (mysql_stmt_close(stmt))
346 error.set(mysql_stmt_error(stmt));
357 void prepare(const std::string &query)
361 if (mysql_stmt_prepare(mysql, query, strlen(query)))
362 mysql.error.set(mysql_stmt_error(stmt));
365 bind = new MYSQL_BIND[size];
371 return mysql_stmt_param_count(stmt);
377 return mysql_stmt_affected_rows(stmt);
380 void bind(MYSQL_BIND *bind)
383 if (mysql_stmt_bind_param(stmt, bind))
384 mysql.error.set(mysql_stmt_error(stmt));
387 BindProxy operator[](int index)
389 assert(index > 0 && index < N);
393 operator MYSQL_BIND *()
402 if (mysql_stmt_execute(stmt))
403 error.set(mysql_stmt_error(stmt));
408 /* namespace MySQL */ }
410 #endif // __BASE_MYQSL_HH__