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
00029
00030
00031
00032
00033
00034
00035 #ifndef MYSQLPP_CONNECTION_H
00036 #define MYSQLPP_CONNECTION_H
00037
00038 #include "common.h"
00039
00040 #include "lockable.h"
00041 #include "noexceptions.h"
00042
00043 #include <deque>
00044 #include <string>
00045
00046 namespace mysqlpp {
00047
00048 #if !defined(DOXYGEN_IGNORE)
00049
00050 class MYSQLPP_EXPORT Query;
00051 #endif
00052
00054
00055 class MYSQLPP_EXPORT Connection : public OptionalExceptions, public Lockable
00056 {
00057 public:
00059 enum OptionArgType {
00060 opt_type_none,
00061 opt_type_string,
00062 opt_type_integer,
00063 opt_type_boolean
00064 };
00065
00071 enum Option
00072 {
00073
00074
00075 opt_FIRST = -1,
00076
00077 opt_connect_timeout = 0,
00078 opt_compress,
00079 opt_named_pipe,
00080 opt_init_command,
00081 opt_read_default_file,
00082 opt_read_default_group,
00083 opt_set_charset_dir,
00084 opt_set_charset_name,
00085 opt_local_infile,
00086 opt_protocol,
00087 opt_shared_memory_base_name,
00088 opt_read_timeout,
00089 opt_write_timeout,
00090 opt_use_result,
00091 opt_use_remote_connection,
00092 opt_use_embedded_connection,
00093 opt_guess_connection,
00094 opt_set_client_ip,
00095 opt_secure_auth,
00096
00097
00098 opt_multi_statements,
00099
00100
00101 opt_report_data_truncation,
00102
00103
00104
00105 opt_reconnect,
00106
00107
00108
00109 opt_COUNT
00110 };
00111
00115 Connection(bool te = true);
00116
00143 Connection(const char* db, const char* host = "",
00144 const char* user = "", const char* passwd = "",
00145 uint port = 0, my_bool compress = 0,
00146 unsigned int connect_timeout = 60, cchar* socket_name = 0,
00147 unsigned int client_flag = 0);
00148
00153 Connection(const Connection& other);
00154
00159 bool connect(const MYSQL& mysql);
00160
00162 ~Connection();
00163
00172 bool connect(cchar* db = "", cchar* host = "",
00173 cchar* user = "", cchar* passwd = "", uint port = 0,
00174 my_bool compress = 0, unsigned int connect_timeout = 60,
00175 cchar* socket_name = 0, unsigned int client_flag = 0);
00176
00180 void close()
00181 {
00182 mysql_close(&mysql_);
00183 is_connected_ = false;
00184 }
00185
00188 std::string info();
00189
00193 bool connected() const
00194 {
00195 return is_connected_;
00196 }
00197
00199 bool success() const
00200 {
00201 return success_;
00202 }
00203
00205 void purge() { close(); }
00206
00214 Query query();
00215
00231 operator bool() { return success(); }
00232
00235 Connection& operator=(const Connection& rhs);
00236
00241 const char* error()
00242 {
00243 return mysql_error(&mysql_);
00244 }
00245
00250 int errnum() { return mysql_errno(&mysql_); }
00251
00260 int refresh(unsigned int refresh_options)
00261 {
00262 return mysql_refresh(&mysql_, refresh_options);
00263 }
00264
00276 int ping();
00277
00283 int kill(unsigned long pid)
00284 {
00285 return mysql_kill(&mysql_, pid);
00286 }
00287
00291 std::string client_info()
00292 {
00293 return std::string(mysql_get_client_info());
00294 }
00295
00302 std::string host_info()
00303 {
00304 return std::string(mysql_get_host_info(&mysql_));
00305 }
00306
00311 int proto_info()
00312 {
00313 return mysql_get_proto_info(&mysql_);
00314 }
00315
00319 std::string server_info()
00320 {
00321 return std::string(mysql_get_server_info(&mysql_));
00322 }
00323
00330 std::string stat()
00331 {
00332 return std::string(mysql_stat(&mysql_));
00333 }
00334
00340 bool create_db(const std::string& db);
00341
00347 bool drop_db(const std::string& db);
00348
00350 bool select_db(const std::string& db)
00351 {
00352 return select_db(db.c_str());
00353 }
00354
00356 bool select_db(const char* db);
00357
00365 bool reload();
00366
00372 bool shutdown();
00373
00375 st_mysql_options get_options() const
00376 {
00377 return mysql_.options;
00378 }
00379
00413 bool set_option(Option option);
00414
00416 bool set_option(Option option, const char* arg);
00417
00419 bool set_option(Option option, unsigned int arg);
00420
00422 bool set_option(Option option, bool arg);
00423
00426 bool set_option_default(Option option);
00427
00430 template <typename T>
00431 bool set_option_default(Option option, T arg);
00432
00434 bool option_set(Option option);
00435
00448 void enable_ssl(const char* key = 0,
00449 const char* cert = 0, const char* ca = 0,
00450 const char* capath = 0, const char* cipher = 0);
00451
00455 my_ulonglong affected_rows()
00456 {
00457 return mysql_affected_rows(&mysql_);
00458 }
00459
00466 my_ulonglong insert_id()
00467 {
00468 return mysql_insert_id(&mysql_);
00469 }
00470
00475 std::ostream& api_version(std::ostream& os);
00476
00477 protected:
00479 enum OptionError {
00480 opt_err_type,
00481 opt_err_value,
00482 opt_err_conn
00483 };
00484
00490 void disconnect();
00491
00493 bool bad_option(Option option, OptionError error);
00494
00496 OptionArgType option_arg_type(Option option);
00497
00503 bool set_option_impl(mysql_option moption, const void* arg = 0);
00504
00505 #if MYSQL_VERSION_ID >= 40101
00511 bool set_option_impl(enum_mysql_set_option msoption);
00512 #endif
00513
00517 void copy(const Connection& other);
00518
00519 private:
00520 friend class ResNSel;
00521 friend class ResUse;
00522 friend class Query;
00523
00524 struct OptionInfo {
00525 Option option;
00526 OptionArgType arg_type;
00527 std::string str_arg;
00528 unsigned int int_arg;
00529 bool bool_arg;
00530
00531 OptionInfo(Option o) :
00532 option(o),
00533 arg_type(opt_type_none),
00534 int_arg(0),
00535 bool_arg(false)
00536 {
00537 }
00538
00539 OptionInfo(Option o, const char* a) :
00540 option(o),
00541 arg_type(opt_type_string),
00542 str_arg(a),
00543 int_arg(0),
00544 bool_arg(false)
00545 {
00546 }
00547
00548 OptionInfo(Option o, unsigned int a) :
00549 option(o),
00550 arg_type(opt_type_integer),
00551 int_arg(a),
00552 bool_arg(false)
00553 {
00554 }
00555
00556 OptionInfo(Option o, bool a) :
00557 option(o),
00558 arg_type(opt_type_boolean),
00559 int_arg(0),
00560 bool_arg(a)
00561 {
00562 }
00563 };
00564 typedef std::deque<OptionInfo> OptionList;
00565 typedef OptionList::const_iterator OptionListIt;
00566
00567 MYSQL mysql_;
00568 bool is_connected_;
00569 bool connecting_;
00570 bool success_;
00571 OptionList applied_options_;
00572 static OptionArgType legal_opt_arg_types_[];
00573 };
00574
00575
00576 }
00577
00578 #endif
00579