MySQL++  3.2.5
dbdriver.h
Go to the documentation of this file.
1 
4 /***********************************************************************
5  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
6  (c) 2004-2009 by Educational Technology Resources, Inc. Others may
7  also hold copyrights on code in this file. See the CREDITS.txt file
8  in the top directory of the distribution for details.
9 
10  This file is part of MySQL++.
11 
12  MySQL++ is free software; you can redistribute it and/or modify it
13  under the terms of the GNU Lesser General Public License as published
14  by the Free Software Foundation; either version 2.1 of the License, or
15  (at your option) any later version.
16 
17  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
18  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20  License for more details.
21 
22  You should have received a copy of the GNU Lesser General Public
23  License along with MySQL++; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25  USA
26 ***********************************************************************/
27 
28 #if !defined(MYSQLPP_DBDRIVER_H)
29 #define MYSQLPP_DBDRIVER_H
30 
31 #include "common.h"
32 
33 #include "options.h"
34 
35 #include <typeinfo>
36 
37 #include <limits.h>
38 
39 namespace mysqlpp {
40 
56 
57 class MYSQLPP_EXPORT DBDriver
58 {
59 public:
61  enum nr_code {
62  nr_more_results,
63  nr_last_result,
64  nr_error,
65  nr_not_supported
66  };
67 
69  DBDriver();
70 
77  DBDriver(const DBDriver& other);
78 
80  virtual ~DBDriver();
81 
85  ulonglong affected_rows()
86  {
87  error_message_.clear();
88  return mysql_affected_rows(&mysql_);
89  }
90 
94  std::string client_version() const
95  {
96  error_message_.clear();
97  return mysql_get_client_info();
98  }
99 
104  bool connect(const MYSQL& mysql);
105 
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);
114 
122  bool connected() const { return is_connected_; }
123 
127  void copy(const DBDriver& other);
128 
134  bool create_db(const char* db) const;
135 
139  void data_seek(MYSQL_RES* res, ulonglong offset) const
140  {
141  error_message_.clear();
142  mysql_data_seek(res, offset);
143  }
144 
150  void disconnect();
151 
157  bool drop_db(const std::string& db) const;
158 
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);
177 
183  const char* error()
184  {
185  return error_message_.length() ? error_message_.c_str() : mysql_error(&mysql_);
186  }
187 
192  int errnum() { return mysql_errno(&mysql_); }
193 
211  size_t escape_string(char* to, const char* from, size_t length)
212  {
213  error_message_.clear();
214  return mysql_real_escape_string(&mysql_, to, from,
215  static_cast<unsigned long>(length));
216  }
217 
257  size_t escape_string(std::string* ps, const char* original,
258  size_t length);
259 
266  static size_t escape_string_no_conn(char* to, const char* from,
267  size_t length)
268  {
269  return mysql_escape_string(to, from,
270  static_cast<unsigned long>(length));
271  }
272 
278  static size_t escape_string_no_conn(std::string* ps,
279  const char* original = 0, size_t length = 0);
280 
284  bool execute(const char* qstr, size_t length)
285  {
286  error_message_.clear();
287  return !mysql_real_query(&mysql_, qstr,
288  static_cast<unsigned long>(length));
289  }
290 
298  MYSQL_ROW fetch_row(MYSQL_RES* res) const
299  {
300  error_message_.clear();
301  return mysql_fetch_row(res);
302  }
303 
308  const unsigned long* fetch_lengths(MYSQL_RES* res) const
309  {
310  error_message_.clear();
311  return mysql_fetch_lengths(res);
312  }
313 
327  MYSQL_FIELD* fetch_field(MYSQL_RES* res, size_t i = UINT_MAX) const
328  {
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));
333  }
334 
338  void field_seek(MYSQL_RES* res, size_t field) const
339  {
340  error_message_.clear();
341  mysql_field_seek(res, MYSQL_FIELD_OFFSET(field));
342  }
343 
347  void free_result(MYSQL_RES* res) const
348  {
349  error_message_.clear();
350  mysql_free_result(res);
351  }
352 
354  st_mysql_options get_options() const { return mysql_.options; }
355 
363  std::string ipc_info()
364  {
365  error_message_.clear();
366  return mysql_get_host_info(&mysql_);
367  }
368 
379  ulonglong insert_id()
380  {
381  error_message_.clear();
382  return mysql_insert_id(&mysql_);
383  }
384 
392  bool kill(unsigned long tid)
393  {
394  error_message_.clear();
395  return !mysql_kill(&mysql_, tid);
396  }
397 
402  bool more_results()
403  {
404  error_message_.clear();
405  #if MYSQL_VERSION_ID > 41000 // only in MySQL v4.1 +
406  return mysql_more_results(&mysql_);
407  #else
408  return false;
409  #endif
410  }
411 
421  nr_code next_result()
422  {
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;
429  }
430  #else
431  return nr_not_supported;
432  #endif
433  }
434 
438  int num_fields(MYSQL_RES* res) const
439  {
440  error_message_.clear();
441  return mysql_num_fields(res);
442  }
443 
447  ulonglong num_rows(MYSQL_RES* res) const
448  {
449  error_message_.clear();
450  return mysql_num_rows(res);
451  }
452 
463  bool ping()
464  {
465  error_message_.clear();
466  return !mysql_ping(&mysql_);
467  }
468 
473  int protocol_version()
474  {
475  error_message_.clear();
476  return mysql_get_proto_info(&mysql_);
477  }
478 
482  std::string query_info();
483 
492  bool refresh(unsigned options)
493  {
494  error_message_.clear();
495  return !mysql_refresh(&mysql_, options);
496  }
497 
502  bool result_empty()
503  {
504  error_message_.clear();
505  return mysql_field_count(&mysql_) == 0;
506  }
507 
509  bool select_db(const char* db)
510  {
511  error_message_.clear();
512  return !mysql_select_db(&mysql_, db);
513  }
514 
518  std::string server_version()
519  {
520  error_message_.clear();
521  return mysql_get_server_info(&mysql_);
522  }
523 
532  bool set_option(Option* o);
533 
537  bool set_option(mysql_option moption, const void* arg = 0)
538  {
539  error_message_.clear();
540  return !mysql_options(&mysql_, moption,
541  static_cast<const char*>(arg));
542  }
543 
544  #if MYSQL_VERSION_ID >= 40101
545  bool set_option(enum_mysql_set_option msoption)
549  {
550  error_message_.clear();
551  return !mysql_set_server_option(&mysql_, msoption);
552  }
553  #endif
554 
560  bool set_option(unsigned int option, bool arg);
561 
564  bool set_option_default(Option* o)
565  {
566  const std::type_info& ti = typeid(o);
567  for (OptionList::const_iterator it = applied_options_.begin();
568  it != applied_options_.end();
569  ++it) {
570  if (typeid(*it) == ti) {
571  delete o;
572  return ""; // option of this type already set
573  }
574  }
575 
576  return set_option(o);
577  }
578 
584  bool shutdown();
585 
594  std::string server_status()
595  {
596  error_message_.clear();
597  return mysql_stat(&mysql_);
598  }
599 
607  MYSQL_RES* store_result()
608  {
609  error_message_.clear();
610  return mysql_store_result(&mysql_);
611  }
612 
623  static bool thread_aware();
624 
630  static void thread_end()
631  {
632  #if MYSQL_VERSION_ID > 40000 // only in MySQL v4.0 +
633  mysql_thread_end();
634  #endif
635  }
636 
641  unsigned long thread_id()
642  {
643  error_message_.clear();
644  return mysql_thread_id(&mysql_);
645  }
646 
665  static bool thread_start()
666  {
667  #if MYSQL_VERSION_ID > 40000 // only in MySQL v4.0 +
668  return !mysql_thread_init();
669  #else
670  return false;
671  #endif
672  }
673 
681  MYSQL_RES* use_result()
682  {
683  error_message_.clear();
684  return mysql_use_result(&mysql_);
685  }
686 
687 protected:
690  bool connect_prepare();
691 
694  bool set_option_impl(Option* o);
695 
696 private:
698  typedef std::deque<Option*> OptionList;
699 
701  typedef OptionList::iterator OptionListIt;
702 
705  DBDriver& operator=(const DBDriver&);
706 
707  MYSQL mysql_;
708  bool is_connected_;
709  OptionList applied_options_;
710  OptionList pending_options_;
711  mutable std::string error_message_;
712 };
713 
714 
715 } // end namespace mysqlpp
716 
717 #endif // !defined(MYSQLPP_DBDRIVER_H)
718 
mysqlpp::Option
Define abstract interface for all *Option subclasses.
Definition: options.h:78
exceptions.h
Declares the MySQL++-specific exception classes.
mysqlpp::Option::err_api_reject
@ err_api_reject
underlying C API returned error when setting option
Definition: options.h:127
mysqlpp::Option::err_disconnected
@ err_disconnected
can only set the given option while connected
Definition: options.h:129
mysqlpp::Option::set
virtual Error set(DBDriver *dbd)=0
Apply option.
mysqlpp::DBDriver::client_version
std::string client_version() const
Get database client library version.
Definition: dbdriver.h:138
mysqlpp::Option::err_api_limit
@ err_api_limit
option not supported by underlying C API
Definition: options.h:126
mysqlpp::DBDriver::set_option
bool set_option(Option *o)
Sets a connection option.
Definition: dbdriver.cpp:283
mysqlpp::DBDriver::DBDriver
DBDriver()
Create object.
Definition: dbdriver.cpp:39
mysqlpp::BadOption
Exception thrown when you pass an unrecognized option to Connection::set_option().
Definition: exceptions.h:227
mysqlpp::DBDriver::~DBDriver
virtual ~DBDriver()
Destroy object.
Definition: dbdriver.cpp:56
mysqlpp::DBDriver::set_option_impl
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
options.h
Declares the Option class hierarchy, used to implement connection options in Connection and DBDriver ...
mysqlpp::OptionList
std::deque< Option * > OptionList
The data type of the list of connection options.
Definition: options.h:511
mysqlpp::DBDriver
Provides a thin abstraction layer over the underlying database client library.
Definition: dbdriver.h:79
mysqlpp::DBDriver::copy
void copy(const DBDriver &other)
Establish a new connection as a copy of an existing one.
Definition: dbdriver.cpp:121
mysqlpp::DBDriver::connect_prepare
bool connect_prepare()
Does things common to both connect() overloads, before each go and establish the connection in their ...
Definition: dbdriver.cpp:93
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:310
mysqlpp::DBDriver::connect
bool connect(const MYSQL &mysql)
Establish a new connection using the same parameters as an existing connection.
Definition: dbdriver.cpp:82
dbdriver.h
Declares the DBDriver class.
mysqlpp::DBDriver::connected
bool connected() const
Return true if we have an active connection to the database server.
Definition: dbdriver.h:166
mysqlpp::Option::err_NONE
@ err_NONE
option was set successfully
Definition: options.h:125
mysqlpp::Option::err_connected
@ err_connected
can't set the given option while connected
Definition: options.h:128
mysqlpp::DBDriver::query_info
std::string query_info()
Returns information about the last executed query.
Definition: dbdriver.cpp:234
mysqlpp::DBDriver::shutdown
bool shutdown()
Ask database server to shut down.
Definition: dbdriver.cpp:332
common.h
This file includes top-level definitions for use both internal to the library, and outside it....
mysqlpp::DBDriver::enable_ssl
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
mysqlpp::OptionListIt
OptionList::const_iterator OptionListIt
Primary iterator type into List.
Definition: options.h:514
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:255
mysqlpp::DBDriver::disconnect
void disconnect()
Drop the connection to the database server.
Definition: dbdriver.cpp:134
mysqlpp::DBDriver::thread_aware
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
mysqlpp::DBDriver::execute
bool execute(const char *qstr, size_t length)
Executes the given query string.
Definition: dbdriver.h:328