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_reconnect,
00107
00108
00109
00110 opt_COUNT
00111 };
00112
00116 MYSQLPP_EXPORT Connection(bool te = true);
00117
00144 MYSQLPP_EXPORT Connection(const char* db, const char* host = "",
00145 const char* user = "", const char* passwd = "",
00146 uint port = 0, my_bool compress = 0,
00147 unsigned int connect_timeout = 60, cchar* socket_name = 0,
00148 unsigned int client_flag = 0);
00149
00151 MYSQLPP_EXPORT ~Connection();
00152
00161 MYSQLPP_EXPORT bool connect(cchar* db = "", cchar* host = "",
00162 cchar* user = "", cchar* passwd = "", uint port = 0,
00163 my_bool compress = 0, unsigned int connect_timeout = 60,
00164 cchar* socket_name = 0, unsigned int client_flag = 0);
00165
00169 void close()
00170 {
00171 mysql_close(&mysql_);
00172 is_connected_ = false;
00173 }
00174
00177 MYSQLPP_EXPORT std::string info();
00178
00182 bool connected() const
00183 {
00184 return is_connected_;
00185 }
00186
00188 bool success() const
00189 {
00190 return success_;
00191 }
00192
00194 void purge() { close(); }
00195
00203 MYSQLPP_EXPORT Query query();
00204
00220 operator bool() { return success(); }
00221
00226 const char* error()
00227 {
00228 return mysql_error(&mysql_);
00229 }
00230
00235 int errnum() { return mysql_errno(&mysql_); }
00236
00245 int refresh(unsigned int refresh_options)
00246 {
00247 return mysql_refresh(&mysql_, refresh_options);
00248 }
00249
00261 int ping();
00262
00268 int kill(unsigned long pid)
00269 {
00270 return mysql_kill(&mysql_, pid);
00271 }
00272
00276 std::string client_info()
00277 {
00278 return std::string(mysql_get_client_info());
00279 }
00280
00287 std::string host_info()
00288 {
00289 return std::string(mysql_get_host_info(&mysql_));
00290 }
00291
00296 int proto_info()
00297 {
00298 return mysql_get_proto_info(&mysql_);
00299 }
00300
00304 std::string server_info()
00305 {
00306 return std::string(mysql_get_server_info(&mysql_));
00307 }
00308
00315 std::string stat()
00316 {
00317 return std::string(mysql_stat(&mysql_));
00318 }
00319
00325 MYSQLPP_EXPORT bool create_db(const std::string& db);
00326
00332 MYSQLPP_EXPORT bool drop_db(const std::string& db);
00333
00335 bool select_db(const std::string& db)
00336 {
00337 return select_db(db.c_str());
00338 }
00339
00341 MYSQLPP_EXPORT bool select_db(const char* db);
00342
00350 MYSQLPP_EXPORT bool reload();
00351
00357 MYSQLPP_EXPORT bool shutdown();
00358
00360 st_mysql_options get_options() const
00361 {
00362 return mysql_.options;
00363 }
00364
00397 MYSQLPP_EXPORT bool set_option(Option option);
00398
00400 MYSQLPP_EXPORT bool set_option(Option option, const char* arg);
00401
00403 MYSQLPP_EXPORT bool set_option(Option option, unsigned int arg);
00404
00406 MYSQLPP_EXPORT bool set_option(Option option, bool arg);
00407
00420 MYSQLPP_EXPORT void enable_ssl(const char* key = 0,
00421 const char* cert = 0, const char* ca = 0,
00422 const char* capath = 0, const char* cipher = 0);
00423
00427 my_ulonglong affected_rows()
00428 {
00429 return mysql_affected_rows(&mysql_);
00430 }
00431
00438 my_ulonglong insert_id()
00439 {
00440 return mysql_insert_id(&mysql_);
00441 }
00442
00447 std::ostream& api_version(std::ostream& os);
00448
00449 protected:
00455 MYSQLPP_EXPORT void disconnect();
00456
00462 bool option_pending(Option option, bool arg) const;
00463
00469 void apply_pending_options();
00470
00472 bool bad_option(Option option, OptionArgType type);
00473
00475 bool bad_option_type(Option option);
00476
00478 bool bad_option_value(Option option);
00479
00481 OptionArgType option_arg_type(Option option);
00482
00488 bool set_option_impl(mysql_option moption, const void* arg = 0);
00489
00490 #if MYSQL_VERSION_ID >= 40101
00496 bool set_option_impl(enum_mysql_set_option msoption);
00497 #endif
00498
00499 private:
00500 friend class ResNSel;
00501 friend class ResUse;
00502 friend class Query;
00503
00504 struct OptionInfo {
00505 Option option;
00506 OptionArgType arg_type;
00507 std::string str_arg;
00508 unsigned int int_arg;
00509 bool bool_arg;
00510
00511 OptionInfo(Option o) :
00512 option(o),
00513 arg_type(opt_type_none),
00514 int_arg(0),
00515 bool_arg(false)
00516 {
00517 }
00518
00519 OptionInfo(Option o, const char* a) :
00520 option(o),
00521 arg_type(opt_type_string),
00522 str_arg(a),
00523 int_arg(0),
00524 bool_arg(false)
00525 {
00526 }
00527
00528 OptionInfo(Option o, unsigned int a) :
00529 option(o),
00530 arg_type(opt_type_integer),
00531 int_arg(a),
00532 bool_arg(false)
00533 {
00534 }
00535
00536 OptionInfo(Option o, bool a) :
00537 option(o),
00538 arg_type(opt_type_boolean),
00539 int_arg(0),
00540 bool_arg(a)
00541 {
00542 }
00543 };
00544
00545 MYSQL mysql_;
00546 bool is_connected_;
00547 bool connecting_;
00548 bool success_;
00549 std::deque<OptionInfo> pending_options_;
00550 static OptionArgType legal_opt_arg_types_[];
00551 };
00552
00553
00554 }
00555
00556 #endif
00557