Go to the documentation of this file.
28 #if !defined(MYSQLPP_DBDRIVER_H)
29 #define MYSQLPP_DBDRIVER_H
57 class MYSQLPP_EXPORT DBDriver
77 DBDriver(
const DBDriver& other);
85 ulonglong affected_rows()
87 error_message_.clear();
88 return mysql_affected_rows(&mysql_);
94 std::string client_version()
const
96 error_message_.clear();
97 return mysql_get_client_info();
104 bool connect(
const MYSQL& mysql);
111 virtual bool connect(
const char* host,
const char* socket_name,
112 unsigned int port,
const char* db,
const char* user,
113 const char* password);
122 bool connected()
const {
return is_connected_; }
134 bool create_db(
const char* db)
const;
139 void data_seek(MYSQL_RES* res, ulonglong offset)
const
141 error_message_.clear();
142 mysql_data_seek(res, offset);
157 bool drop_db(
const std::string& db)
const;
174 bool enable_ssl(
const char* key = 0,
const char* cert = 0,
175 const char* ca = 0,
const char* capath = 0,
176 const char* cipher = 0);
185 return error_message_.length() ? error_message_.c_str() : mysql_error(&mysql_);
192 int errnum() {
return mysql_errno(&mysql_); }
211 size_t escape_string(
char* to,
const char* from,
size_t length)
213 error_message_.clear();
214 return mysql_real_escape_string(&mysql_, to, from,
215 static_cast<unsigned long>(length));
257 size_t escape_string(std::string* ps,
const char* original,
266 static size_t escape_string_no_conn(
char* to,
const char* from,
269 return mysql_escape_string(to, from,
270 static_cast<unsigned long>(length));
278 static size_t escape_string_no_conn(std::string* ps,
279 const char* original = 0,
size_t length = 0);
284 bool execute(
const char* qstr,
size_t length)
286 error_message_.clear();
287 return !mysql_real_query(&mysql_, qstr,
288 static_cast<unsigned long>(length));
298 MYSQL_ROW fetch_row(MYSQL_RES* res)
const
300 error_message_.clear();
301 return mysql_fetch_row(res);
308 const unsigned long* fetch_lengths(MYSQL_RES* res)
const
310 error_message_.clear();
311 return mysql_fetch_lengths(res);
327 MYSQL_FIELD* fetch_field(MYSQL_RES* res,
size_t i = UINT_MAX)
const
329 error_message_.clear();
330 return i == UINT_MAX ? mysql_fetch_field(res) :
331 mysql_fetch_field_direct(res,
332 static_cast<unsigned int>(i));
338 void field_seek(MYSQL_RES* res,
size_t field)
const
340 error_message_.clear();
341 mysql_field_seek(res, MYSQL_FIELD_OFFSET(field));
347 void free_result(MYSQL_RES* res)
const
349 error_message_.clear();
350 mysql_free_result(res);
354 st_mysql_options get_options()
const {
return mysql_.options; }
363 std::string ipc_info()
365 error_message_.clear();
366 return mysql_get_host_info(&mysql_);
379 ulonglong insert_id()
381 error_message_.clear();
382 return mysql_insert_id(&mysql_);
392 bool kill(
unsigned long tid)
394 error_message_.clear();
395 return !mysql_kill(&mysql_, tid);
404 error_message_.clear();
405 #if MYSQL_VERSION_ID > 41000 // only in MySQL v4.1 +
406 return mysql_more_results(&mysql_);
421 nr_code next_result()
423 error_message_.clear();
424 #if MYSQL_VERSION_ID > 41000 // only in MySQL v4.1 +
425 switch (mysql_next_result(&mysql_)) {
426 case 0:
return nr_more_results;
427 case -1:
return nr_last_result;
428 default:
return nr_error;
431 return nr_not_supported;
438 int num_fields(MYSQL_RES* res)
const
440 error_message_.clear();
441 return mysql_num_fields(res);
447 ulonglong num_rows(MYSQL_RES* res)
const
449 error_message_.clear();
450 return mysql_num_rows(res);
465 error_message_.clear();
466 return !mysql_ping(&mysql_);
473 int protocol_version()
475 error_message_.clear();
476 return mysql_get_proto_info(&mysql_);
482 std::string query_info();
492 bool refresh(
unsigned options)
494 error_message_.clear();
495 return !mysql_refresh(&mysql_, options);
504 error_message_.clear();
505 return mysql_field_count(&mysql_) == 0;
509 bool select_db(
const char* db)
511 error_message_.clear();
512 return !mysql_select_db(&mysql_, db);
518 std::string server_version()
520 error_message_.clear();
521 return mysql_get_server_info(&mysql_);
532 bool set_option(Option* o);
537 bool set_option(mysql_option moption,
const void* arg = 0)
539 error_message_.clear();
540 return !mysql_options(&mysql_, moption,
541 static_cast<const char*
>(arg));
544 #if MYSQL_VERSION_ID >= 40101
545 bool set_option(enum_mysql_set_option msoption)
550 error_message_.clear();
551 return !mysql_set_server_option(&mysql_, msoption);
560 bool set_option(
unsigned int option,
bool arg);
564 bool set_option_default(
Option* o)
566 const std::type_info& ti =
typeid(o);
567 for (OptionList::const_iterator it = applied_options_.begin();
568 it != applied_options_.end();
570 if (
typeid(*it) == ti) {
576 return set_option(o);
594 std::string server_status()
596 error_message_.clear();
597 return mysql_stat(&mysql_);
607 MYSQL_RES* store_result()
609 error_message_.clear();
610 return mysql_store_result(&mysql_);
623 static bool thread_aware();
630 static void thread_end()
632 #if MYSQL_VERSION_ID > 40000 // only in MySQL v4.0 +
641 unsigned long thread_id()
643 error_message_.clear();
644 return mysql_thread_id(&mysql_);
665 static bool thread_start()
667 #if MYSQL_VERSION_ID > 40000 // only in MySQL v4.0 +
668 return !mysql_thread_init();
681 MYSQL_RES* use_result()
683 error_message_.clear();
684 return mysql_use_result(&mysql_);
690 bool connect_prepare();
694 bool set_option_impl(
Option* o);
709 OptionList applied_options_;
710 OptionList pending_options_;
711 mutable std::string error_message_;
717 #endif // !defined(MYSQLPP_DBDRIVER_H)
Define abstract interface for all *Option subclasses.
Definition: options.h:78
Declares the MySQL++-specific exception classes.
@ err_api_reject
underlying C API returned error when setting option
Definition: options.h:127
@ err_disconnected
can only set the given option while connected
Definition: options.h:129
virtual Error set(DBDriver *dbd)=0
Apply option.
std::string client_version() const
Get database client library version.
Definition: dbdriver.h:138
@ err_api_limit
option not supported by underlying C API
Definition: options.h:126
bool set_option(Option *o)
Sets a connection option.
Definition: dbdriver.cpp:283
DBDriver()
Create object.
Definition: dbdriver.cpp:39
Exception thrown when you pass an unrecognized option to Connection::set_option().
Definition: exceptions.h:227
virtual ~DBDriver()
Destroy object.
Definition: dbdriver.cpp:56
bool set_option_impl(Option *o)
Common implementation of set_option(Option*) and the delayed option setting code in connect_prepare()
Definition: dbdriver.cpp:297
Declares the Option class hierarchy, used to implement connection options in Connection and DBDriver ...
std::deque< Option * > OptionList
The data type of the list of connection options.
Definition: options.h:511
Provides a thin abstraction layer over the underlying database client library.
Definition: dbdriver.h:79
void copy(const DBDriver &other)
Establish a new connection as a copy of an existing one.
Definition: dbdriver.cpp:121
bool connect_prepare()
Does things common to both connect() overloads, before each go and establish the connection in their ...
Definition: dbdriver.cpp:93
static size_t escape_string_no_conn(char *to, const char *from, size_t length)
SQL-escapes the given string without reference to the character set of a database server.
Definition: dbdriver.h:310
bool connect(const MYSQL &mysql)
Establish a new connection using the same parameters as an existing connection.
Definition: dbdriver.cpp:82
Declares the DBDriver class.
bool connected() const
Return true if we have an active connection to the database server.
Definition: dbdriver.h:166
@ err_NONE
option was set successfully
Definition: options.h:125
@ err_connected
can't set the given option while connected
Definition: options.h:128
std::string query_info()
Returns information about the last executed query.
Definition: dbdriver.cpp:234
bool shutdown()
Ask database server to shut down.
Definition: dbdriver.cpp:332
This file includes top-level definitions for use both internal to the library, and outside it....
bool enable_ssl(const char *key=0, const char *cert=0, const char *ca=0, const char *capath=0, const char *cipher=0)
Enable SSL-encrypted connection.
Definition: dbdriver.cpp:146
OptionList::const_iterator OptionListIt
Primary iterator type into List.
Definition: options.h:514
size_t escape_string(char *to, const char *from, size_t length)
Return a SQL-escaped version of the given character buffer.
Definition: dbdriver.h:255
void disconnect()
Drop the connection to the database server.
Definition: dbdriver.cpp:134
static bool thread_aware()
Returns true if MySQL++ and the underlying MySQL C API library were both compiled with thread awarene...
Definition: dbdriver.cpp:352
bool execute(const char *qstr, size_t length)
Executes the given query string.
Definition: dbdriver.h:328