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 "platform.h"
00039
00040 #include "defs.h"
00041
00042 #include "lockable.h"
00043 #include "noexceptions.h"
00044
00045 #include <mysql.h>
00046
00047 #include <deque>
00048 #include <string>
00049
00050 namespace mysqlpp {
00051
00052 class Query;
00053
00055
00056 class Connection : public OptionalExceptions, public Lockable
00057 {
00058 public:
00060 enum OptionArgType {
00061 opt_type_none,
00062 opt_type_string,
00063 opt_type_integer,
00064 opt_type_boolean
00065 };
00066
00072 enum Option
00073 {
00074
00075
00076 opt_FIRST = -1,
00077
00078 opt_connect_timeout = 0,
00079 opt_compress,
00080 opt_named_pipe,
00081 opt_init_command,
00082 opt_read_default_file,
00083 opt_read_default_group,
00084 opt_set_charset_dir,
00085 opt_set_charset_name,
00086 opt_local_infile,
00087 opt_protocol,
00088 opt_shared_memory_base_name,
00089 opt_read_timeout,
00090 opt_write_timeout,
00091 opt_use_result,
00092 opt_use_remote_connection,
00093 opt_use_embedded_connection,
00094 opt_guess_connection,
00095 opt_set_client_ip,
00096 opt_secure_auth,
00097
00098
00099 opt_multi_statements,
00100
00101
00102 opt_report_data_truncation,
00103
00104
00105
00106 opt_COUNT
00107 };
00108
00112 MYSQLPP_EXPORT Connection(bool te = true);
00113
00140 MYSQLPP_EXPORT Connection(const char* db, const char* host = "",
00141 const char* user = "", const char* passwd = "",
00142 uint port = 0, my_bool compress = 0,
00143 unsigned int connect_timeout = 60, cchar* socket_name = 0,
00144 unsigned int client_flag = 0);
00145
00147 MYSQLPP_EXPORT ~Connection();
00148
00157 MYSQLPP_EXPORT bool connect(cchar* db = "", cchar* host = "",
00158 cchar* user = "", cchar* passwd = "", uint port = 0,
00159 my_bool compress = 0, unsigned int connect_timeout = 60,
00160 cchar* socket_name = 0, unsigned int client_flag = 0);
00161
00165 void close()
00166 {
00167 mysql_close(&mysql_);
00168 is_connected_ = false;
00169 }
00170
00173 MYSQLPP_EXPORT std::string info();
00174
00178 bool connected() const
00179 {
00180 return is_connected_;
00181 }
00182
00184 bool success() const
00185 {
00186 return success_;
00187 }
00188
00190 void purge() { close(); }
00191
00199 MYSQLPP_EXPORT Query query();
00200
00216 operator bool() { return success(); }
00217
00222 const char* error()
00223 {
00224 return mysql_error(&mysql_);
00225 }
00226
00231 int errnum() { return mysql_errno(&mysql_); }
00232
00241 int refresh(unsigned int refresh_options)
00242 {
00243 return mysql_refresh(&mysql_, refresh_options);
00244 }
00245
00256 int ping() { return mysql_ping(&mysql_); }
00257
00263 int kill(unsigned long pid)
00264 {
00265 return mysql_kill(&mysql_, pid);
00266 }
00267
00271 std::string client_info()
00272 {
00273 return std::string(mysql_get_client_info());
00274 }
00275
00282 std::string host_info()
00283 {
00284 return std::string(mysql_get_host_info(&mysql_));
00285 }
00286
00291 int proto_info()
00292 {
00293 return mysql_get_proto_info(&mysql_);
00294 }
00295
00299 std::string server_info()
00300 {
00301 return std::string(mysql_get_server_info(&mysql_));
00302 }
00303
00310 std::string stat()
00311 {
00312 return std::string(mysql_stat(&mysql_));
00313 }
00314
00320 MYSQLPP_EXPORT bool create_db(const std::string& db);
00321
00327 MYSQLPP_EXPORT bool drop_db(const std::string& db);
00328
00330 bool select_db(const std::string& db)
00331 {
00332 return select_db(db.c_str());
00333 }
00334
00336 MYSQLPP_EXPORT bool select_db(const char* db);
00337
00345 MYSQLPP_EXPORT bool reload();
00346
00352 MYSQLPP_EXPORT bool shutdown();
00353
00355 st_mysql_options get_options() const
00356 {
00357 return mysql_.options;
00358 }
00359
00392 MYSQLPP_EXPORT bool set_option(Option option);
00393
00395 MYSQLPP_EXPORT bool set_option(Option option, const char* arg);
00396
00398 MYSQLPP_EXPORT bool set_option(Option option, unsigned int arg);
00399
00401 MYSQLPP_EXPORT bool set_option(Option option, bool arg);
00402
00415 MYSQLPP_EXPORT void enable_ssl(const char* key = 0,
00416 const char* cert = 0, const char* ca = 0,
00417 const char* capath = 0, const char* cipher = 0);
00418
00422 my_ulonglong affected_rows()
00423 {
00424 return mysql_affected_rows(&mysql_);
00425 }
00426
00433 my_ulonglong insert_id()
00434 {
00435 return mysql_insert_id(&mysql_);
00436 }
00437
00442 std::ostream& api_version(std::ostream& os);
00443
00444 protected:
00450 MYSQLPP_EXPORT void disconnect();
00451
00457 bool option_pending(Option option, bool arg) const;
00458
00464 void apply_pending_options();
00465
00467 bool bad_option(Option option, OptionArgType type);
00468
00470 bool bad_option_type(Option option);
00471
00473 bool bad_option_value(Option option);
00474
00476 OptionArgType option_arg_type(Option option);
00477
00483 bool set_option_impl(mysql_option moption, const void* arg = 0);
00484
00485 #if MYSQL_VERSION_ID >= 40101
00491 bool set_option_impl(enum_mysql_set_option msoption);
00492 #endif
00493
00494 private:
00495 friend class ResNSel;
00496 friend class ResUse;
00497 friend class Query;
00498
00499 struct OptionInfo {
00500 Option option;
00501 OptionArgType arg_type;
00502 std::string str_arg;
00503 unsigned int int_arg;
00504 bool bool_arg;
00505
00506 OptionInfo(Option o) :
00507 option(o),
00508 arg_type(opt_type_none),
00509 int_arg(0),
00510 bool_arg(false)
00511 {
00512 }
00513
00514 OptionInfo(Option o, const char* a) :
00515 option(o),
00516 arg_type(opt_type_string),
00517 str_arg(a),
00518 int_arg(0),
00519 bool_arg(false)
00520 {
00521 }
00522
00523 OptionInfo(Option o, unsigned int a) :
00524 option(o),
00525 arg_type(opt_type_integer),
00526 int_arg(a),
00527 bool_arg(false)
00528 {
00529 }
00530
00531 OptionInfo(Option o, bool a) :
00532 option(o),
00533 arg_type(opt_type_boolean),
00534 int_arg(0),
00535 bool_arg(a)
00536 {
00537 }
00538 };
00539
00540 MYSQL mysql_;
00541 bool is_connected_;
00542 bool connecting_;
00543 bool success_;
00544 std::deque<OptionInfo> pending_options_;
00545 static OptionArgType legal_opt_arg_types_[];
00546 };
00547
00548
00549 }
00550
00551 #endif
00552