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; }
180 bool query(const std::string &sql);
183 query(const std::stringstream &sql)
185 return query(sql.str());
191 return mysql_field_count(&mysql);
197 return mysql_affected_rows(&mysql);
203 return mysql_insert_id(&mysql);
211 Result result = mysql_store_result(&mysql);
213 error.set(mysql_error(&mysql));
223 BindProxy(MYSQL_BIND *b) : bind(b) {}
225 void operator=(bool &buffer)
227 bind->buffer_type = MYSQL_TYPE_TINY;
228 bind->buffer = (char *)&buffer;
231 void operator=(int8_t &buffer)
233 bind->buffer_type = MYSQL_TYPE_TINY;
234 bind->buffer = (char *)&buffer;
237 void operator=(int16_t &buffer)
239 bind->buffer_type = MYSQL_TYPE_SHORT;
240 bind->buffer = (char *)&buffer;
243 void operator=(int32_t &buffer)
245 bind->buffer_type = MYSQL_TYPE_LONG;
246 bind->buffer = (char *)&buffer;
249 void operator=(int64_t &buffer)
251 bind->buffer_type = MYSQL_TYPE_LONGLONG;
252 bind->buffer = (char *)&buffer;
255 void operator=(uint8_t &buffer)
257 bind->buffer_type = MYSQL_TYPE_TINY;
258 bind->buffer = (char *)&buffer;
261 void operator=(uint16_t &buffer)
263 bind->buffer_type = MYSQL_TYPE_SHORT;
264 bind->buffer = (char *)&buffer;
267 void operator=(uint32_t &buffer)
269 bind->buffer_type = MYSQL_TYPE_LONG;
270 bind->buffer = (char *)&buffer;
273 void operator=(uint64_t &buffer)
275 bind->buffer_type = MYSQL_TYPE_LONGLONG;
276 bind->buffer = (char *)&buffer;
279 void operator=(float &buffer)
281 bind->buffer_type = MYSQL_TYPE_FLOAT;
282 bind->buffer = (char *)&buffer;
285 void operator=(double &buffer)
287 bind->buffer_type = MYSQL_TYPE_DOUBLE;
288 bind->buffer = (char *)&buffer;
291 void operator=(Time &buffer)
293 bind->buffer_type = MYSQL_TYPE_DATE;
294 bind->buffer = (char *)&buffer;
297 void operator=(const char *buffer)
299 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
300 bind->buffer = buffer;
303 void operator=(const std::string &buffer)
305 bind->buffer_type = MYSQL_TYPE_VAR_STRING;
306 bind->buffer = (char *)&buffer;
307 bind->length = buffer.length;
313 bind->is_null = null;
326 Statement(Connection &mysql)
327 : error(mysql.error), bind(NULL), size(0)
329 stmt = mysql_stmt_init(mysql);
330 assert(valid() && "mysql_stmt_init(), out of memory\n");
337 if (mysql_stmt_close(stmt))
338 error.set(mysql_stmt_error(stmt));
349 void prepare(const std::string &query)
353 if (mysql_stmt_prepare(mysql, query, strlen(query)))
354 mysql.error.set(mysql_stmt_error(stmt));
357 bind = new MYSQL_BIND[size];
363 return mysql_stmt_param_count(stmt);
369 return mysql_stmt_affected_rows(stmt);
372 void bind(MYSQL_BIND *bind)
375 if (mysql_stmt_bind_param(stmt, bind))
376 mysql.error.set(mysql_stmt_error(stmt));
379 BindProxy operator[](int index)
381 assert(index > 0 && index < N);
385 operator MYSQL_BIND *()
394 if (mysql_stmt_execute(stmt))
395 error.set(mysql_stmt_error(stmt));
400 /* namespace MySQL */ }
402 #endif // __BASE_MYQSL_HH__