dbdriver.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 /***********************************************************************
00005  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
00006  (c) 2004-2008 by Educational Technology Resources, Inc.  Others may
00007  also hold copyrights on code in this file.  See the CREDITS file in
00008  the top directory of the distribution for details.
00009 
00010  This file is part of MySQL++.
00011 
00012  MySQL++ is free software; you can redistribute it and/or modify it
00013  under the terms of the GNU Lesser General Public License as published
00014  by the Free Software Foundation; either version 2.1 of the License, or
00015  (at your option) any later version.
00016 
00017  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
00018  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00019  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00020  License for more details.
00021 
00022  You should have received a copy of the GNU Lesser General Public
00023  License along with MySQL++; if not, write to the Free Software
00024  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
00025  USA
00026 ***********************************************************************/
00027 
00028 #if !defined(MYSQLPP_DBDRIVER_H)
00029 #define MYSQLPP_DBDRIVER_H
00030 
00031 #include "common.h"
00032 
00033 #include "options.h"
00034 
00035 #include <typeinfo>
00036 
00037 #include <limits.h>
00038 
00039 namespace mysqlpp {
00040 
00056 
00057 class DBDriver
00058 {
00059 public:
00061         enum nr_code {
00062                 nr_more_results,        
00063                 nr_last_result,         
00064                 nr_error,                       
00065                 nr_not_supported        
00066         };
00067 
00069         DBDriver();
00070 
00077         DBDriver(const DBDriver& other);
00078 
00080         virtual ~DBDriver();
00081 
00085         ulonglong affected_rows() { return mysql_affected_rows(&mysql_); }
00086 
00090         std::string client_version() const { return mysql_get_client_info(); }
00091 
00096         bool connect(const MYSQL& mysql);
00097 
00103         virtual bool connect(const char* host, const char* socket_name,
00104                         unsigned int port, const char* db, const char* user,
00105                         const char* password);
00106 
00114         bool connected() const { return is_connected_; }
00115 
00119         void copy(const DBDriver& other);
00120 
00126         bool create_db(const char* db) const;
00127 
00131         void data_seek(MYSQL_RES* res, ulonglong offset) const
00132                         { mysql_data_seek(res, offset); }
00133 
00139         void disconnect();
00140 
00146         bool drop_db(const std::string& db) const;
00147 
00163         bool enable_ssl(const char* key = 0, const char* cert = 0,
00164                         const char* ca = 0, const char* capath = 0,
00165                         const char* cipher = 0);
00166 
00172         const char* error() { return mysql_error(&mysql_); }
00173 
00178         int errnum() { return mysql_errno(&mysql_); }
00179 
00184         size_t escape_string(char* to, const char* from, size_t length)
00185                         { return mysql_real_escape_string(&mysql_, to, from, length); }
00186 
00191         static size_t escape_string_no_conn(char* to, const char* from,
00192             size_t length)
00193                         { return mysql_escape_string(to, from, length); }
00194 
00198         bool execute(const char* qstr, size_t length)
00199                         { return !mysql_real_query(&mysql_, qstr, length); }
00200 
00208         MYSQL_ROW fetch_row(MYSQL_RES* res) const
00209                         { return mysql_fetch_row(res); }
00210 
00215         const unsigned long* fetch_lengths(MYSQL_RES* res) const
00216                         { return mysql_fetch_lengths(res); }
00217 
00231         MYSQL_FIELD* fetch_field(MYSQL_RES* res, size_t i = UINT_MAX) const
00232         {
00233                 return i == UINT_MAX ? mysql_fetch_field(res) :
00234                                 mysql_fetch_field_direct(res, i);
00235         }
00236 
00240         void field_seek(MYSQL_RES* res, size_t field) const
00241                         { mysql_field_seek(res, field); }
00242 
00246         void free_result(MYSQL_RES* res) const
00247                         { mysql_free_result(res); }
00248 
00250         st_mysql_options get_options() const { return mysql_.options; }
00251 
00259         std::string ipc_info() { return mysql_get_host_info(&mysql_); }
00260 
00267         ulonglong insert_id() { return mysql_insert_id(&mysql_); }
00268 
00276         bool kill(unsigned long tid) { return !mysql_kill(&mysql_, tid); }
00277 
00282         bool more_results()
00283         {
00284                 #if MYSQL_VERSION_ID > 41000            // only in MySQL v4.1 +
00285                         return mysql_more_results(&mysql_);
00286                 #else
00287                         return false;
00288                 #endif
00289         }
00290 
00300         nr_code next_result()
00301         {
00302                 #if MYSQL_VERSION_ID > 41000            // only in MySQL v4.1 +
00303                         switch (mysql_next_result(&mysql_)) {
00304                                 case 0:  return nr_more_results;
00305                                 case -1: return nr_last_result;
00306                                 default: return nr_error;
00307                         }
00308                 #else
00309                         return nr_not_supported;
00310                 #endif
00311         }
00312 
00316         int num_fields(MYSQL_RES* res) const
00317                         { return mysql_num_fields(res); }
00318         
00322         ulonglong num_rows(MYSQL_RES* res) const
00323                         { return mysql_num_rows(res); }
00324 
00335         bool ping() { return !mysql_ping(&mysql_); }
00336 
00341         int protocol_version() { return mysql_get_proto_info(&mysql_); }
00342 
00346         std::string query_info();
00347 
00356         bool refresh(unsigned options) { return !mysql_refresh(&mysql_, options); }
00357 
00362         bool result_empty() { return mysql_field_count(&mysql_) == 0; }
00363 
00365         bool select_db(const char* db) { return !mysql_select_db(&mysql_, db); }
00366 
00370         std::string server_version() { return mysql_get_server_info(&mysql_); }
00371 
00380         std::string set_option(Option* o);
00381 
00385         bool set_option(mysql_option moption, const void* arg = 0)
00386         {
00387                 return !mysql_options(&mysql_, moption,
00388                                 static_cast<const char*>(arg));
00389         }
00390 
00391         #if MYSQL_VERSION_ID >= 40101
00395         bool set_option(enum_mysql_set_option msoption)
00396         {
00397                 return !mysql_set_server_option(&mysql_, msoption);
00398         }
00399         #endif
00400 
00406         bool set_option(unsigned int option, bool arg);
00407 
00410         std::string set_option_default(Option* o)
00411         {
00412                 const std::type_info& ti = typeid(o);
00413                 for (OptionList::const_iterator it = applied_options_.begin();
00414                                 it != applied_options_.end(); 
00415                                 ++it) {
00416                         if (typeid(*it) == ti) {
00417                                 delete o;
00418                                 return "";              // option of this type already set
00419                         }
00420                 }
00421 
00422                 return set_option(o);
00423         }
00424 
00430         bool shutdown();
00431 
00440         std::string server_status() { return mysql_stat(&mysql_); }
00441 
00449         MYSQL_RES* store_result() { return mysql_store_result(&mysql_); }
00450 
00461         static bool thread_aware();
00462 
00468         static void thread_end()
00469         {
00470                 #if MYSQL_VERSION_ID > 40000            // only in MySQL v4.0 +
00471                         mysql_thread_end();
00472                 #endif
00473         }
00474 
00479         unsigned long thread_id() { return mysql_thread_id(&mysql_); }
00480 
00499         static bool thread_start()
00500         {
00501                 #if MYSQL_VERSION_ID > 40000            // only in MySQL v4.0 +
00502                         return !mysql_thread_init();
00503                 #else
00504                         return false;
00505                 #endif
00506         }
00507 
00515         MYSQL_RES* use_result() { return mysql_use_result(&mysql_); }
00516 
00517 private:
00519         typedef std::deque<Option*> OptionList;
00520 
00523         DBDriver& operator=(const DBDriver&);
00524 
00525         MYSQL mysql_;
00526         bool is_connected_;
00527         OptionList applied_options_;
00528 };
00529 
00530 
00531 } // end namespace mysqlpp
00532 
00533 #endif // !defined(MYSQLPP_DBDRIVER_H)
00534 

Generated on Sun Apr 13 12:58:17 2008 for MySQL++ by  doxygen 1.4.7