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 };
00066
00068 DBDriver();
00069
00076 DBDriver(const DBDriver& other);
00077
00079 virtual ~DBDriver();
00080
00084 ulonglong affected_rows() { return mysql_affected_rows(&mysql_); }
00085
00089 std::string client_version() const { return mysql_get_client_info(); }
00090
00095 bool connect(const MYSQL& mysql);
00096
00102 virtual bool connect(const char* host, const char* socket_name,
00103 unsigned int port, const char* db, const char* user,
00104 const char* password);
00105
00113 bool connected() const { return is_connected_; }
00114
00118 void copy(const DBDriver& other);
00119
00125 bool create_db(const char* db) const;
00126
00130 void data_seek(MYSQL_RES* res, ulonglong offset) const
00131 { mysql_data_seek(res, offset); }
00132
00138 void disconnect();
00139
00145 bool drop_db(const std::string& db) const;
00146
00162 bool enable_ssl(const char* key = 0, const char* cert = 0,
00163 const char* ca = 0, const char* capath = 0,
00164 const char* cipher = 0);
00165
00171 const char* error() { return mysql_error(&mysql_); }
00172
00177 int errnum() { return mysql_errno(&mysql_); }
00178
00183 size_t escape_string(char* to, const char* from, size_t length)
00184 { return mysql_real_escape_string(&mysql_, to, from, length); }
00185
00190 static size_t escape_string_no_conn(char* to, const char* from,
00191 size_t length)
00192 { return mysql_escape_string(to, from, length); }
00193
00197 bool execute(const char* qstr, size_t length)
00198 { return !mysql_real_query(&mysql_, qstr, length); }
00199
00207 MYSQL_ROW fetch_row(MYSQL_RES* res) const
00208 { return mysql_fetch_row(res); }
00209
00214 const unsigned long* fetch_lengths(MYSQL_RES* res) const
00215 { return mysql_fetch_lengths(res); }
00216
00230 MYSQL_FIELD* fetch_field(MYSQL_RES* res, size_t i = UINT_MAX) const
00231 {
00232 return i == UINT_MAX ? mysql_fetch_field(res) :
00233 mysql_fetch_field_direct(res, i);
00234 }
00235
00239 void field_seek(MYSQL_RES* res, size_t field) const
00240 { mysql_field_seek(res, field); }
00241
00245 void free_result(MYSQL_RES* res) const
00246 { mysql_free_result(res); }
00247
00249 st_mysql_options get_options() const { return mysql_.options; }
00250
00258 std::string ipc_info() { return mysql_get_host_info(&mysql_); }
00259
00266 ulonglong insert_id() { return mysql_insert_id(&mysql_); }
00267
00275 bool kill(unsigned long tid) { return !mysql_kill(&mysql_, tid); }
00276
00281 bool more_results()
00282 {
00283 #if MYSQL_VERSION_ID > 41000 // only in MySQL v4.1 +
00284 return mysql_more_results(&mysql_);
00285 #else
00286 return false;
00287 #endif
00288 }
00289
00299 nr_code next_result()
00300 {
00301 switch (mysql_next_result(&mysql_)) {
00302 case 0: return nr_more_results;
00303 case -1: return nr_last_result;
00304 default: return nr_error;
00305 }
00306 }
00307
00311 int num_fields(MYSQL_RES* res) const
00312 { return mysql_num_fields(res); }
00313
00317 ulonglong num_rows(MYSQL_RES* res) const
00318 { return mysql_num_rows(res); }
00319
00330 bool ping() { return !mysql_ping(&mysql_); }
00331
00336 int protocol_version() { return mysql_get_proto_info(&mysql_); }
00337
00341 std::string query_info();
00342
00351 bool refresh(unsigned options) { return !mysql_refresh(&mysql_, options); }
00352
00357 bool result_empty() { return mysql_field_count(&mysql_) == 0; }
00358
00360 bool select_db(const char* db) { return !mysql_select_db(&mysql_, db); }
00361
00365 std::string server_version() { return mysql_get_server_info(&mysql_); }
00366
00375 std::string set_option(Option* o);
00376
00380 bool set_option(mysql_option moption, const void* arg = 0)
00381 {
00382 return !mysql_options(&mysql_, moption,
00383 static_cast<const char*>(arg));
00384 }
00385
00386 #if MYSQL_VERSION_ID >= 40101
00390 bool set_option(enum_mysql_set_option msoption)
00391 {
00392 return !mysql_set_server_option(&mysql_, msoption);
00393 }
00394 #endif
00395
00401 bool set_option(unsigned int option, bool arg);
00402
00405 std::string set_option_default(Option* o)
00406 {
00407 const std::type_info& ti = typeid(o);
00408 for (OptionList::const_iterator it = applied_options_.begin();
00409 it != applied_options_.end();
00410 ++it) {
00411 if (typeid(*it) == ti) {
00412 delete o;
00413 return "";
00414 }
00415 }
00416
00417 return set_option(o);
00418 }
00419
00425 bool shutdown();
00426
00435 std::string server_status() { return mysql_stat(&mysql_); }
00436
00444 MYSQL_RES* store_result() { return mysql_store_result(&mysql_); }
00445
00456 bool thread_aware() const;
00457
00463 void thread_end() { mysql_thread_end(); }
00464
00469 unsigned long thread_id() { return mysql_thread_id(&mysql_); }
00470
00489 bool thread_start() { return !mysql_thread_init(); }
00490
00498 MYSQL_RES* use_result() { return mysql_use_result(&mysql_); }
00499
00500 private:
00502 typedef std::deque<Option*> OptionList;
00503
00506 DBDriver& operator=(const DBDriver&);
00507
00508 MYSQL mysql_;
00509 bool is_connected_;
00510 OptionList applied_options_;
00511 };
00512
00513
00514 }
00515
00516 #endif // !defined(MYSQLPP_DBDRIVER_H)
00517