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-2009 by Educational Technology Resources, Inc.  Others may
00007  also hold copyrights on code in this file.  See the CREDITS.txt file
00008  in 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()
00086         {
00087                 error_message_.clear();
00088                 return mysql_affected_rows(&mysql_);
00089         }
00090 
00094         std::string client_version() const
00095         {
00096                 error_message_.clear();
00097                 return mysql_get_client_info();
00098         }
00099 
00104         bool connect(const MYSQL& mysql);
00105 
00111         virtual bool connect(const char* host, const char* socket_name,
00112                         unsigned int port, const char* db, const char* user,
00113                         const char* password);
00114 
00122         bool connected() const { return is_connected_; }
00123 
00127         void copy(const DBDriver& other);
00128 
00134         bool create_db(const char* db) const;
00135 
00139         void data_seek(MYSQL_RES* res, ulonglong offset) const
00140         {
00141                 error_message_.clear();
00142                 mysql_data_seek(res, offset);
00143         }
00144 
00150         void disconnect();
00151 
00157         bool drop_db(const std::string& db) const;
00158 
00174         bool enable_ssl(const char* key = 0, const char* cert = 0,
00175                         const char* ca = 0, const char* capath = 0,
00176                         const char* cipher = 0);
00177 
00183         const char* error()
00184         {
00185                 return error_message_.length() ? error_message_.c_str() : mysql_error(&mysql_);
00186         }
00187 
00192         int errnum() { return mysql_errno(&mysql_); }
00193 
00211         size_t escape_string(char* to, const char* from, size_t length)
00212         {
00213                 error_message_.clear();
00214                 return mysql_real_escape_string(&mysql_, to, from, 
00215                                 static_cast<unsigned long>(length));
00216         }
00217 
00257         size_t escape_string(std::string* ps, const char* original,
00258                         size_t length);
00259 
00266         static size_t escape_string_no_conn(char* to, const char* from,
00267                         size_t length)
00268         {
00269                 return mysql_escape_string(to, from,
00270                                 static_cast<unsigned long>(length));
00271         }
00272 
00278         static size_t escape_string_no_conn(std::string* ps, 
00279                         const char* original = 0, size_t length = 0);
00280 
00284         bool execute(const char* qstr, size_t length)
00285         {
00286                 error_message_.clear();
00287                 return !mysql_real_query(&mysql_, qstr,
00288                                 static_cast<unsigned long>(length));
00289         }
00290 
00298         MYSQL_ROW fetch_row(MYSQL_RES* res) const
00299         {
00300                 error_message_.clear();
00301                 return mysql_fetch_row(res);
00302         }
00303 
00308         const unsigned long* fetch_lengths(MYSQL_RES* res) const
00309         {
00310                 error_message_.clear();
00311                 return mysql_fetch_lengths(res);
00312         }
00313 
00327         MYSQL_FIELD* fetch_field(MYSQL_RES* res, size_t i = UINT_MAX) const
00328         {
00329                 error_message_.clear();
00330                 return i == UINT_MAX ? mysql_fetch_field(res) :
00331                                 mysql_fetch_field_direct(res,
00332                                 static_cast<unsigned int>(i));
00333         }
00334 
00338         void field_seek(MYSQL_RES* res, size_t field) const
00339         {
00340                 error_message_.clear();
00341                 mysql_field_seek(res, MYSQL_FIELD_OFFSET(field));
00342         }
00343 
00347         void free_result(MYSQL_RES* res) const
00348         {
00349                 error_message_.clear();
00350                 mysql_free_result(res);
00351         }
00352 
00354         st_mysql_options get_options() const { return mysql_.options; }
00355 
00363         std::string ipc_info()
00364         {
00365                 error_message_.clear();
00366                 return mysql_get_host_info(&mysql_);
00367         }
00368 
00379         ulonglong insert_id()
00380         {
00381                 error_message_.clear();
00382                 return mysql_insert_id(&mysql_);
00383         }
00384 
00392         bool kill(unsigned long tid)
00393         {
00394                 error_message_.clear();
00395                 return !mysql_kill(&mysql_, tid);
00396         }
00397 
00402         bool more_results()
00403         {
00404                 error_message_.clear();
00405                 #if MYSQL_VERSION_ID > 41000            // only in MySQL v4.1 +
00406                         return mysql_more_results(&mysql_);
00407                 #else
00408                         return false;
00409                 #endif
00410         }
00411 
00421         nr_code next_result()
00422         {
00423                 error_message_.clear();
00424                 #if MYSQL_VERSION_ID > 41000            // only in MySQL v4.1 +
00425                         switch (mysql_next_result(&mysql_)) {
00426                                 case 0:  return nr_more_results;
00427                                 case -1: return nr_last_result;
00428                                 default: return nr_error;
00429                         }
00430                 #else
00431                         return nr_not_supported;
00432                 #endif
00433         }
00434 
00438         int num_fields(MYSQL_RES* res) const
00439         {
00440                 error_message_.clear();
00441                 return mysql_num_fields(res);
00442         }
00443 
00447         ulonglong num_rows(MYSQL_RES* res) const
00448         {
00449                 error_message_.clear();
00450                 return mysql_num_rows(res);
00451         }
00452 
00463         bool ping()
00464         {
00465                 error_message_.clear();
00466                 return !mysql_ping(&mysql_);
00467         }
00468 
00473         int protocol_version()
00474         {
00475                 error_message_.clear();
00476                 return mysql_get_proto_info(&mysql_);
00477         }
00478 
00482         std::string query_info();
00483 
00492         bool refresh(unsigned options)
00493         {
00494                 error_message_.clear();
00495                 return !mysql_refresh(&mysql_, options);
00496         }
00497 
00502         bool result_empty()
00503         {
00504                 error_message_.clear();
00505                 return mysql_field_count(&mysql_) == 0;
00506         }
00507 
00509         bool select_db(const char* db)
00510         {
00511                 error_message_.clear();
00512                 return !mysql_select_db(&mysql_, db);
00513         }
00514 
00518         std::string server_version()
00519         {
00520                 error_message_.clear();
00521                 return mysql_get_server_info(&mysql_);
00522         }
00523 
00532         bool set_option(Option* o);
00533 
00537         bool set_option(mysql_option moption, const void* arg = 0)
00538         {
00539                 error_message_.clear();
00540                 return !mysql_options(&mysql_, moption,
00541                                 static_cast<const char*>(arg));
00542         }
00543 
00544         #if MYSQL_VERSION_ID >= 40101
00548         bool set_option(enum_mysql_set_option msoption)
00549         {
00550                 error_message_.clear();
00551                 return !mysql_set_server_option(&mysql_, msoption);
00552         }
00553         #endif
00554 
00560         bool set_option(unsigned int option, bool arg);
00561 
00564         bool set_option_default(Option* o)
00565         {
00566                 const std::type_info& ti = typeid(o);
00567                 for (OptionList::const_iterator it = applied_options_.begin();
00568                                 it != applied_options_.end();
00569                                 ++it) {
00570                         if (typeid(*it) == ti) {
00571                                 delete o;
00572                                 return "";              // option of this type already set
00573                         }
00574                 }
00575 
00576                 return set_option(o);
00577         }
00578 
00584         bool shutdown();
00585 
00594         std::string server_status()
00595         {
00596                 error_message_.clear();
00597                 return mysql_stat(&mysql_);
00598         }
00599 
00607         MYSQL_RES* store_result()
00608         {
00609                 error_message_.clear();
00610                 return mysql_store_result(&mysql_);
00611         }
00612 
00623         static bool thread_aware();
00624 
00630         static void thread_end()
00631         {
00632                 #if MYSQL_VERSION_ID > 40000            // only in MySQL v4.0 +
00633                         mysql_thread_end();
00634                 #endif
00635         }
00636 
00641         unsigned long thread_id()
00642         {
00643                 error_message_.clear();
00644                 return mysql_thread_id(&mysql_);
00645         }
00646 
00665         static bool thread_start()
00666         {
00667                 #if MYSQL_VERSION_ID > 40000            // only in MySQL v4.0 +
00668                         return !mysql_thread_init();
00669                 #else
00670                         return false;
00671                 #endif
00672         }
00673 
00681         MYSQL_RES* use_result()
00682         {
00683                 error_message_.clear();
00684                 return mysql_use_result(&mysql_);
00685         }
00686 
00687 protected:
00690         bool connect_prepare();
00691 
00694         bool set_option_impl(Option* o);
00695 
00696 private:
00698         typedef std::deque<Option*> OptionList;
00699 
00701         typedef OptionList::iterator OptionListIt;
00702 
00705         DBDriver& operator=(const DBDriver&);
00706 
00707         MYSQL mysql_;
00708         bool is_connected_;
00709         OptionList applied_options_;
00710         OptionList pending_options_;
00711         mutable std::string error_message_;
00712 };
00713 
00714 
00715 } // end namespace mysqlpp
00716 
00717 #endif // !defined(MYSQLPP_DBDRIVER_H)
00718 

Generated on Thu Jun 3 11:59:12 2010 for MySQL++ by  doxygen 1.4.7