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 <mysql.h>
00044
00045 #include <deque>
00046 #include <string>
00047
00048 namespace mysqlpp {
00049
00050 #if !defined(DOXYGEN_IGNORE)
00051
00052 class MYSQLPP_EXPORT Query;
00053 #endif
00054
00056
00057 class MYSQLPP_EXPORT Connection : public OptionalExceptions, public Lockable
00058 {
00059 public:
00061 enum OptionArgType {
00062 opt_type_none,
00063 opt_type_string,
00064 opt_type_integer,
00065 opt_type_boolean
00066 };
00067
00073 enum Option
00074 {
00075
00076
00077 opt_FIRST = -1,
00078
00079 opt_connect_timeout = 0,
00080 opt_compress,
00081 opt_named_pipe,
00082 opt_init_command,
00083 opt_read_default_file,
00084 opt_read_default_group,
00085 opt_set_charset_dir,
00086 opt_set_charset_name,
00087 opt_local_infile,
00088 opt_protocol,
00089 opt_shared_memory_base_name,
00090 opt_read_timeout,
00091 opt_write_timeout,
00092 opt_use_result,
00093 opt_use_remote_connection,
00094 opt_use_embedded_connection,
00095 opt_guess_connection,
00096 opt_set_client_ip,
00097 opt_secure_auth,
00098
00099
00100 opt_multi_statements,
00101
00102
00103 opt_report_data_truncation,
00104
00105
00106
00107 opt_reconnect,
00108
00109
00110
00111 opt_COUNT
00112 };
00113
00117 Connection(bool te = true);
00118
00145 Connection(const char* db, const char* host = "",
00146 const char* user = "", const char* passwd = "",
00147 uint port = 0, my_bool compress = 0,
00148 unsigned int connect_timeout = 60, cchar* socket_name = 0,
00149 unsigned int client_flag = 0);
00150
00155 Connection(const Connection& other);
00156
00161 bool connect(const MYSQL& mysql);
00162
00164 ~Connection();
00165
00174 bool connect(cchar* db = "", cchar* host = "",
00175 cchar* user = "", cchar* passwd = "", uint port = 0,
00176 my_bool compress = 0, unsigned int connect_timeout = 60,
00177 cchar* socket_name = 0, unsigned int client_flag = 0);
00178
00182 void close()
00183 {
00184 mysql_close(&mysql_);
00185 is_connected_ = false;
00186 }
00187
00190 std::string info();
00191
00195 bool connected() const
00196 {
00197 return is_connected_;
00198 }
00199
00201 bool success() const
00202 {
00203 return success_;
00204 }
00205
00207 void purge() { close(); }
00208
00216 Query query();
00217
00233 operator bool() { return success(); }
00234
00237 Connection& operator=(const Connection& rhs);
00238
00243 const char* error()
00244 {
00245 return mysql_error(&mysql_);
00246 }
00247
00252 int errnum() { return mysql_errno(&mysql_); }
00253
00262 int refresh(unsigned int refresh_options)
00263 {
00264 return mysql_refresh(&mysql_, refresh_options);
00265 }
00266
00278 int ping();
00279
00285 int kill(unsigned long pid)
00286 {
00287 return mysql_kill(&mysql_, pid);
00288 }
00289
00293 std::string client_info()
00294 {
00295 return std::string(mysql_get_client_info());
00296 }
00297
00304 std::string host_info()
00305 {
00306 return std::string(mysql_get_host_info(&mysql_));
00307 }
00308
00313 int proto_info()
00314 {
00315 return mysql_get_proto_info(&mysql_);
00316 }
00317
00321 std::string server_info()
00322 {
00323 return std::string(mysql_get_server_info(&mysql_));
00324 }
00325
00332 std::string stat()
00333 {
00334 return std::string(mysql_stat(&mysql_));
00335 }
00336
00342 bool create_db(const std::string& db);
00343
00349 bool drop_db(const std::string& db);
00350
00352 bool select_db(const std::string& db)
00353 {
00354 return select_db(db.c_str());
00355 }
00356
00358 bool select_db(const char* db);
00359
00367 bool reload();
00368
00374 bool shutdown();
00375
00377 st_mysql_options get_options() const
00378 {
00379 return mysql_.options;
00380 }
00381
00414 bool set_option(Option option);
00415
00417 bool set_option(Option option, const char* arg);
00418
00420 bool set_option(Option option, unsigned int arg);
00421
00423 bool set_option(Option option, bool arg);
00424
00437 void enable_ssl(const char* key = 0,
00438 const char* cert = 0, const char* ca = 0,
00439 const char* capath = 0, const char* cipher = 0);
00440
00444 my_ulonglong affected_rows()
00445 {
00446 return mysql_affected_rows(&mysql_);
00447 }
00448
00455 my_ulonglong insert_id()
00456 {
00457 return mysql_insert_id(&mysql_);
00458 }
00459
00464 std::ostream& api_version(std::ostream& os);
00465
00466 protected:
00472 void disconnect();
00473
00479 bool option_pending(Option option, bool arg) const;
00480
00486 void apply_pending_options();
00487
00489 bool bad_option(Option option, OptionArgType type);
00490
00492 bool bad_option_type(Option option);
00493
00495 bool bad_option_value(Option option);
00496
00498 OptionArgType option_arg_type(Option option);
00499
00505 bool set_option_impl(mysql_option moption, const void* arg = 0);
00506
00507 #if MYSQL_VERSION_ID >= 40101
00513 bool set_option_impl(enum_mysql_set_option msoption);
00514 #endif
00515
00519 void copy(const Connection& other);
00520
00521 private:
00522 friend class ResNSel;
00523 friend class ResUse;
00524 friend class Query;
00525
00526 struct OptionInfo {
00527 Option option;
00528 OptionArgType arg_type;
00529 std::string str_arg;
00530 unsigned int int_arg;
00531 bool bool_arg;
00532
00533 OptionInfo(Option o) :
00534 option(o),
00535 arg_type(opt_type_none),
00536 int_arg(0),
00537 bool_arg(false)
00538 {
00539 }
00540
00541 OptionInfo(Option o, const char* a) :
00542 option(o),
00543 arg_type(opt_type_string),
00544 str_arg(a),
00545 int_arg(0),
00546 bool_arg(false)
00547 {
00548 }
00549
00550 OptionInfo(Option o, unsigned int a) :
00551 option(o),
00552 arg_type(opt_type_integer),
00553 int_arg(a),
00554 bool_arg(false)
00555 {
00556 }
00557
00558 OptionInfo(Option o, bool a) :
00559 option(o),
00560 arg_type(opt_type_boolean),
00561 int_arg(0),
00562 bool_arg(a)
00563 {
00564 }
00565 };
00566
00567 MYSQL mysql_;
00568 bool is_connected_;
00569 bool connecting_;
00570 bool success_;
00571 std::deque<OptionInfo> pending_options_;
00572 static OptionArgType legal_opt_arg_types_[];
00573 };
00574
00575
00576 }
00577
00578 #endif
00579