dbdriver.h
Go to the documentation of this file.
/***********************************************************************
Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
(c) 2004-2009 by Educational Technology Resources, Inc. Others may
also hold copyrights on code in this file. See the CREDITS.txt file
in the top directory of the distribution for details.
This file is part of MySQL++.
MySQL++ is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
MySQL++ is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with MySQL++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
***********************************************************************/
#if !defined(MYSQLPP_DBDRIVER_H)
#define MYSQLPP_DBDRIVER_H
#include "common.h"
#include "options.h"
#include <typeinfo>
#include <limits.h>
namespace mysqlpp {
class MYSQLPP_EXPORT DBDriver
{
public:
enum nr_code {
nr_more_results,
nr_last_result,
nr_error,
nr_not_supported
};
DBDriver();
DBDriver(const DBDriver& other);
virtual ~DBDriver();
ulonglong affected_rows()
{
error_message_.clear();
return mysql_affected_rows(&mysql_);
}
std::string client_version() const
{
error_message_.clear();
return mysql_get_client_info();
}
bool connect(const MYSQL& mysql);
virtual bool connect(const char* host, const char* socket_name,
unsigned int port, const char* db, const char* user,
const char* password);
bool connected() const { return is_connected_; }
void copy(const DBDriver& other);
bool create_db(const char* db) const;
void data_seek(MYSQL_RES* res, ulonglong offset) const
{
error_message_.clear();
mysql_data_seek(res, offset);
}
void disconnect();
bool drop_db(const std::string& db) const;
bool enable_ssl(const char* key = 0, const char* cert = 0,
const char* ca = 0, const char* capath = 0,
const char* cipher = 0);
const char* error()
{
return error_message_.length() ? error_message_.c_str() : mysql_error(&mysql_);
}
int errnum() { return mysql_errno(&mysql_); }
size_t escape_string(char* to, const char* from, size_t length)
{
error_message_.clear();
return mysql_real_escape_string(&mysql_, to, from,
static_cast<unsigned long>(length));
}
size_t escape_string(std::string* ps, const char* original,
size_t length);
static size_t escape_string_no_conn(char* to, const char* from,
size_t length)
{
return mysql_escape_string(to, from,
static_cast<unsigned long>(length));
}
static size_t escape_string_no_conn(std::string* ps,
const char* original = 0, size_t length = 0);
bool execute(const char* qstr, size_t length)
{
error_message_.clear();
return !mysql_real_query(&mysql_, qstr,
static_cast<unsigned long>(length));
}
MYSQL_ROW fetch_row(MYSQL_RES* res) const
{
error_message_.clear();
return mysql_fetch_row(res);
}
const unsigned long* fetch_lengths(MYSQL_RES* res) const
{
error_message_.clear();
return mysql_fetch_lengths(res);
}
MYSQL_FIELD* fetch_field(MYSQL_RES* res, size_t i = UINT_MAX) const
{
error_message_.clear();
return i == UINT_MAX ? mysql_fetch_field(res) :
mysql_fetch_field_direct(res,
static_cast<unsigned int>(i));
}
void field_seek(MYSQL_RES* res, size_t field) const
{
error_message_.clear();
mysql_field_seek(res, MYSQL_FIELD_OFFSET(field));
}
void free_result(MYSQL_RES* res) const
{
error_message_.clear();
mysql_free_result(res);
}
st_mysql_options get_options() const { return mysql_.options; }
std::string ipc_info()
{
error_message_.clear();
return mysql_get_host_info(&mysql_);
}
ulonglong insert_id()
{
error_message_.clear();
return mysql_insert_id(&mysql_);
}
bool kill(unsigned long tid)
{
error_message_.clear();
return !mysql_kill(&mysql_, tid);
}
bool more_results()
{
error_message_.clear();
#if MYSQL_VERSION_ID > 41000 // only in MySQL v4.1 +
return mysql_more_results(&mysql_);
#else
return false;
#endif
}
nr_code next_result()
{
error_message_.clear();
#if MYSQL_VERSION_ID > 41000 // only in MySQL v4.1 +
switch (mysql_next_result(&mysql_)) {
case 0: return nr_more_results;
case -1: return nr_last_result;
default: return nr_error;
}
#else
return nr_not_supported;
#endif
}
int num_fields(MYSQL_RES* res) const
{
error_message_.clear();
return mysql_num_fields(res);
}
ulonglong num_rows(MYSQL_RES* res) const
{
error_message_.clear();
return mysql_num_rows(res);
}
bool ping()
{
error_message_.clear();
return !mysql_ping(&mysql_);
}
int protocol_version()
{
error_message_.clear();
return mysql_get_proto_info(&mysql_);
}
std::string query_info();
bool refresh(unsigned options)
{
error_message_.clear();
return !mysql_refresh(&mysql_, options);
}
bool result_empty()
{
error_message_.clear();
return mysql_field_count(&mysql_) == 0;
}
bool select_db(const char* db)
{
error_message_.clear();
return !mysql_select_db(&mysql_, db);
}
std::string server_version()
{
error_message_.clear();
return mysql_get_server_info(&mysql_);
}
bool set_option(Option* o);
bool set_option(mysql_option moption, const void* arg = 0)
{
error_message_.clear();
return !mysql_options(&mysql_, moption,
static_cast<const char*>(arg));
}
#if MYSQL_VERSION_ID >= 40101
bool set_option(enum_mysql_set_option msoption)
{
error_message_.clear();
return !mysql_set_server_option(&mysql_, msoption);
}
#endif
bool set_option(unsigned int option, bool arg);
bool set_option_default(Option* o)
{
const std::type_info& ti = typeid(o);
for (OptionList::const_iterator it = applied_options_.begin();
it != applied_options_.end();
++it) {
if (typeid(*it) == ti) {
delete o;
return ""; // option of this type already set
}
}
return set_option(o);
}
bool shutdown();
std::string server_status()
{
error_message_.clear();
return mysql_stat(&mysql_);
}
MYSQL_RES* store_result()
{
error_message_.clear();
return mysql_store_result(&mysql_);
}
static bool thread_aware();
static void thread_end()
{
#if MYSQL_VERSION_ID > 40000 // only in MySQL v4.0 +
mysql_thread_end();
#endif
}
unsigned long thread_id()
{
error_message_.clear();
return mysql_thread_id(&mysql_);
}
static bool thread_start()
{
#if MYSQL_VERSION_ID > 40000 // only in MySQL v4.0 +
return !mysql_thread_init();
#else
return false;
#endif
}
MYSQL_RES* use_result()
{
error_message_.clear();
return mysql_use_result(&mysql_);
}
protected:
bool connect_prepare();
bool set_option_impl(Option* o);
private:
typedef std::deque<Option*> OptionList;
typedef OptionList::iterator OptionListIt;
DBDriver& operator=(const DBDriver&);
MYSQL mysql_;
bool is_connected_;
OptionList applied_options_;
OptionList pending_options_;
mutable std::string error_message_;
};
} // end namespace mysqlpp
#endif // !defined(MYSQLPP_DBDRIVER_H)
mysqlpp::DBDriver::get_options
st_mysql_options get_options() const
Return the connection options object.
Definition: dbdriver.h:354
mysqlpp::DBDriver::insert_id
ulonglong insert_id()
Get ID generated for an AUTO_INCREMENT column in the previous INSERT query.
Definition: dbdriver.h:379
mysqlpp::DBDriver::ping
bool ping()
&quot;Pings&quot; the MySQL database
Definition: dbdriver.h:463
mysqlpp::DBDriver::connected
bool connected() const
Return true if we have an active connection to the database server.
Definition: dbdriver.h:122
mysqlpp::DBDriver::set_option
bool set_option(mysql_option moption, const void *arg=0)
Set MySQL C API connection option.
Definition: dbdriver.h:537
mysqlpp::DBDriver::thread_start
static bool thread_start()
Tells the underlying C API library that the current thread will be using the library&#39;s services...
Definition: dbdriver.h:665
mysqlpp::DBDriver::nr_more_results
success, with more results to come
Definition: dbdriver.h:62
mysqlpp::DBDriver::data_seek
void data_seek(MYSQL_RES *res, ulonglong offset) const
Seeks to a particualr row within the result set.
Definition: dbdriver.h:139
mysqlpp::DBDriver::thread_end
static void thread_end()
Tells the underlying MySQL C API library that this thread is done using the library.
Definition: dbdriver.h:630
mysqlpp::DBDriver::affected_rows
ulonglong affected_rows()
Return the number of rows affected by the last query.
Definition: dbdriver.h:85
mysqlpp::DBDriver::set_option_default
bool set_option_default(Option *o)
Same as set_option(), except that it won&#39;t override a previously-set option.
Definition: dbdriver.h:564
mysqlpp::DBDriver::num_rows
ulonglong num_rows(MYSQL_RES *res) const
Returns the number of rows in the given result set.
Definition: dbdriver.h:447
mysqlpp::DBDriver::protocol_version
int protocol_version()
Returns version number of MySQL protocol this connection is using.
Definition: dbdriver.h:473
mysqlpp::DBDriver::escape_string_no_conn
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:266
mysqlpp::DBDriver::error
const char * error()
Return error message for last MySQL error associated with this connection.
Definition: dbdriver.h:183
mysqlpp::DBDriver::nr_code
nr_code
Result code returned by next_result()
Definition: dbdriver.h:61
mysqlpp::DBDriver::next_result
nr_code next_result()
Moves to the next result set from a multi-query.
Definition: dbdriver.h:421
mysqlpp::DBDriver::field_seek
void field_seek(MYSQL_RES *res, size_t field) const
Jumps to the given field within the result set.
Definition: dbdriver.h:338
mysqlpp::Option
Define abstract interface for all *Option subclasses.
Definition: options.h:57
mysqlpp::DBDriver::free_result
void free_result(MYSQL_RES *res) const
Releases memory used by a result set.
Definition: dbdriver.h:347
mysqlpp::DBDriver::fetch_lengths
const unsigned long * fetch_lengths(MYSQL_RES *res) const
Returns the lengths of the fields in the current row from a &quot;use&quot; query.
Definition: dbdriver.h:308
mysqlpp::DBDriver::server_status
std::string server_status()
Returns the database server&#39;s status.
Definition: dbdriver.h:594
mysqlpp::DBDriver::server_version
std::string server_version()
Get the database server&#39;s version number.
Definition: dbdriver.h:518
mysqlpp::DBDriver::client_version
std::string client_version() const
Get database client library version.
Definition: dbdriver.h:94
mysqlpp::DBDriver::store_result
MYSQL_RES * store_result()
Saves the results of the query just execute()d in memory and returns a pointer to the MySQL C API dat...
Definition: dbdriver.h:607
mysqlpp::DBDriver::result_empty
bool result_empty()
Returns true if the most recent result set was empty.
Definition: dbdriver.h:502
mysqlpp::DBDriver::more_results
bool more_results()
Returns true if there are unconsumed results from the most recent query.
Definition: dbdriver.h:402
mysqlpp::DBDriver::nr_error
problem retrieving next result
Definition: dbdriver.h:64
mysqlpp::DBDriver::fetch_row
MYSQL_ROW fetch_row(MYSQL_RES *res) const
Returns the next raw C API row structure from the given result set.
Definition: dbdriver.h:298
mysqlpp::DBDriver::nr_last_result
success, last result received
Definition: dbdriver.h:63
mysqlpp::DBDriver::kill
bool kill(unsigned long tid)
Kill a MySQL server thread.
Definition: dbdriver.h:392
mysqlpp::DBDriver::select_db
bool select_db(const char *db)
Asks the database server to switch to a different database.
Definition: dbdriver.h:509
mysqlpp::DBDriver::ipc_info
std::string ipc_info()
Get information about the IPC connection to the database server.
Definition: dbdriver.h:363
mysqlpp::DBDriver::thread_id
unsigned long thread_id()
Returns the MySQL server thread ID for this connection.
Definition: dbdriver.h:641
mysqlpp::DBDriver::refresh
bool refresh(unsigned options)
Asks the database server to refresh certain internal data structures.
Definition: dbdriver.h:492
common.h
This file includes top-level definitions for use both internal to the library, and outside it...
mysqlpp::DBDriver::num_fields
int num_fields(MYSQL_RES *res) const
Returns the number of fields in the given result set.
Definition: dbdriver.h:438
mysqlpp::DBDriver::errnum
int errnum()
Return last MySQL error number associated with this connection.
Definition: dbdriver.h:192
options.h
Declares the Option class hierarchy, used to implement connection options in Connection and DBDriver ...
mysqlpp::DBDriver::use_result
MYSQL_RES * use_result()
Returns a result set from the last-executed query which we can walk through in linear fashion...
Definition: dbdriver.h:681
mysqlpp::DBDriver::fetch_field
MYSQL_FIELD * fetch_field(MYSQL_RES *res, size_t i=UINT_MAX) const
Returns information about a particular field in a result set.
Definition: dbdriver.h:327
mysqlpp::DBDriver::execute
bool execute(const char *qstr, size_t length)
Executes the given query string.
Definition: dbdriver.h:284
mysqlpp::DBDriver::escape_string
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:211
mysqlpp::DBDriver
Provides a thin abstraction layer over the underlying database client library.
Definition: dbdriver.h:57