00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #if !defined(MYSQLPP_DBDRIVER_H)
00029 #define MYSQLPP_DBDRIVER_H
00030
00031 #include "common.h"
00032
00033 #include "options.h"
00034
00035 #include <typeinfo>
00036
00037 #include <limits.h>
00038
00039 namespace mysqlpp {
00040
00056
00057 class DBDriver
00058 {
00059 public:
00061 enum nr_code {
00062 nr_more_results,
00063 nr_last_result,
00064 nr_error,
00065 nr_not_supported
00066 };
00067
00069 DBDriver();
00070
00077 DBDriver(const DBDriver& other);
00078
00080 virtual ~DBDriver();
00081
00085 ulonglong affected_rows() { return mysql_affected_rows(&mysql_); }
00086
00090 std::string client_version() const { return mysql_get_client_info(); }
00091
00096 bool connect(const MYSQL& mysql);
00097
00103 virtual bool connect(const char* host, const char* socket_name,
00104 unsigned int port, const char* db, const char* user,
00105 const char* password);
00106
00114 bool connected() const { return is_connected_; }
00115
00119 void copy(const DBDriver& other);
00120
00126 bool create_db(const char* db) const;
00127
00131 void data_seek(MYSQL_RES* res, ulonglong offset) const
00132 { mysql_data_seek(res, offset); }
00133
00139 void disconnect();
00140
00146 bool drop_db(const std::string& db) const;
00147
00163 bool enable_ssl(const char* key = 0, const char* cert = 0,
00164 const char* ca = 0, const char* capath = 0,
00165 const char* cipher = 0);
00166
00172 const char* error() { return mysql_error(&mysql_); }
00173
00178 int errnum() { return mysql_errno(&mysql_); }
00179
00184 size_t escape_string(char* to, const char* from, size_t length)
00185 { return mysql_real_escape_string(&mysql_, to, from, length); }
00186
00191 static size_t escape_string_no_conn(char* to, const char* from,
00192 size_t length)
00193 { return mysql_escape_string(to, from, length); }
00194
00198 bool execute(const char* qstr, size_t length)
00199 { return !mysql_real_query(&mysql_, qstr, length); }
00200
00208 MYSQL_ROW fetch_row(MYSQL_RES* res) const
00209 { return mysql_fetch_row(res); }
00210
00215 const unsigned long* fetch_lengths(MYSQL_RES* res) const
00216 { return mysql_fetch_lengths(res); }
00217
00231 MYSQL_FIELD* fetch_field(MYSQL_RES* res, size_t i = UINT_MAX) const
00232 {
00233 return i == UINT_MAX ? mysql_fetch_field(res) :
00234 mysql_fetch_field_direct(res, i);
00235 }
00236
00240 void field_seek(MYSQL_RES* res, size_t field) const
00241 { mysql_field_seek(res, field); }
00242
00246 void free_result(MYSQL_RES* res) const
00247 { mysql_free_result(res); }
00248
00250 st_mysql_options get_options() const { return mysql_.options; }
00251
00259 std::string ipc_info() { return mysql_get_host_info(&mysql_); }
00260
00271 ulonglong insert_id() { return mysql_insert_id(&mysql_); }
00272
00280 bool kill(unsigned long tid) { return !mysql_kill(&mysql_, tid); }
00281
00286 bool more_results()
00287 {
00288 #if MYSQL_VERSION_ID > 41000 // only in MySQL v4.1 +
00289 return mysql_more_results(&mysql_);
00290 #else
00291 return false;
00292 #endif
00293 }
00294
00304 nr_code next_result()
00305 {
00306 #if MYSQL_VERSION_ID > 41000 // only in MySQL v4.1 +
00307 switch (mysql_next_result(&mysql_)) {
00308 case 0: return nr_more_results;
00309 case -1: return nr_last_result;
00310 default: return nr_error;
00311 }
00312 #else
00313 return nr_not_supported;
00314 #endif
00315 }
00316
00320 int num_fields(MYSQL_RES* res) const
00321 { return mysql_num_fields(res); }
00322
00326 ulonglong num_rows(MYSQL_RES* res) const
00327 { return mysql_num_rows(res); }
00328
00339 bool ping() { return !mysql_ping(&mysql_); }
00340
00345 int protocol_version() { return mysql_get_proto_info(&mysql_); }
00346
00350 std::string query_info();
00351
00360 bool refresh(unsigned options) { return !mysql_refresh(&mysql_, options); }
00361
00366 bool result_empty() { return mysql_field_count(&mysql_) == 0; }
00367
00369 bool select_db(const char* db) { return !mysql_select_db(&mysql_, db); }
00370
00374 std::string server_version() { return mysql_get_server_info(&mysql_); }
00375
00384 std::string set_option(Option* o);
00385
00389 bool set_option(mysql_option moption, const void* arg = 0)
00390 {
00391 return !mysql_options(&mysql_, moption,
00392 static_cast<const char*>(arg));
00393 }
00394
00395 #if MYSQL_VERSION_ID >= 40101
00399 bool set_option(enum_mysql_set_option msoption)
00400 {
00401 return !mysql_set_server_option(&mysql_, msoption);
00402 }
00403 #endif
00404
00410 bool set_option(unsigned int option, bool arg);
00411
00414 std::string set_option_default(Option* o)
00415 {
00416 const std::type_info& ti = typeid(o);
00417 for (OptionList::const_iterator it = applied_options_.begin();
00418 it != applied_options_.end();
00419 ++it) {
00420 if (typeid(*it) == ti) {
00421 delete o;
00422 return "";
00423 }
00424 }
00425
00426 return set_option(o);
00427 }
00428
00434 bool shutdown();
00435
00444 std::string server_status() { return mysql_stat(&mysql_); }
00445
00453 MYSQL_RES* store_result() { return mysql_store_result(&mysql_); }
00454
00465 static bool thread_aware();
00466
00472 static void thread_end()
00473 {
00474 #if MYSQL_VERSION_ID > 40000 // only in MySQL v4.0 +
00475 mysql_thread_end();
00476 #endif
00477 }
00478
00483 unsigned long thread_id() { return mysql_thread_id(&mysql_); }
00484
00503 static bool thread_start()
00504 {
00505 #if MYSQL_VERSION_ID > 40000 // only in MySQL v4.0 +
00506 return !mysql_thread_init();
00507 #else
00508 return false;
00509 #endif
00510 }
00511
00519 MYSQL_RES* use_result() { return mysql_use_result(&mysql_); }
00520
00521 private:
00523 typedef std::deque<Option*> OptionList;
00524
00527 DBDriver& operator=(const DBDriver&);
00528
00529 MYSQL mysql_;
00530 bool is_connected_;
00531 OptionList applied_options_;
00532 };
00533
00534
00535 }
00536
00537 #endif // !defined(MYSQLPP_DBDRIVER_H)
00538