MySQL++  3.2.5
connection.h
Go to the documentation of this file.
1 
11 /***********************************************************************
12  Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
13  (c) 2004-2007 by Educational Technology Resources, Inc. Others may
14  also hold copyrights on code in this file. See the CREDITS.txt file
15  in the top directory of the distribution for details.
16 
17  This file is part of MySQL++.
18 
19  MySQL++ is free software; you can redistribute it and/or modify it
20  under the terms of the GNU Lesser General Public License as published
21  by the Free Software Foundation; either version 2.1 of the License, or
22  (at your option) any later version.
23 
24  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
25  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
27  License for more details.
28 
29  You should have received a copy of the GNU Lesser General Public
30  License along with MySQL++; if not, write to the Free Software
31  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
32  USA
33 ***********************************************************************/
34 
35 #if !defined(MYSQLPP_CONNECTION_H)
36 #define MYSQLPP_CONNECTION_H
37 
38 #include "common.h"
39 
40 #include "noexceptions.h"
41 #include "options.h"
42 
43 #include <string>
44 
45 namespace mysqlpp {
46 
47 #if !defined(DOXYGEN_IGNORE)
48 // Make Doxygen ignore this
49 class MYSQLPP_EXPORT Query;
50 class DBDriver;
51 #endif
52 
58 
59 class MYSQLPP_EXPORT Connection : public OptionalExceptions
60 {
61 private:
66  typedef bool Connection::*private_bool_type;
67 
68 public:
72  Connection(bool te = true);
73 
107  Connection(const char* db, const char* server = 0, const char* user = 0,
108  const char* password = 0, unsigned int port = 0);
109 
114  Connection(const Connection& other);
115 
117  virtual ~Connection();
118 
121  std::string client_version() const;
122 
131  virtual bool connect(const char* db = 0, const char* server = 0,
132  const char* user = 0, const char* password = 0,
133  unsigned int port = 0);
134 
138  bool connected() const;
139 
146  ulonglong count_rows(const std::string& table);
147 
153  bool create_db(const std::string& db);
154 
156  void disconnect();
157 
167  DBDriver* driver() { return driver_; }
168 
174  bool drop_db(const std::string& db);
175 
178  int errnum();
179 
185  const char* error() const;
186 
192  std::string ipc_info() const;
193 
199  bool kill(unsigned long tid) const;
200 
220  operator private_bool_type() const
221  {
222  return copacetic_ ? &Connection::copacetic_ : 0;
223  }
224 
227  Connection& operator=(const Connection& rhs);
228 
235  bool ping();
236 
239  int protocol_version() const;
240 
251  Query query(const char* qstr = 0);
252 
256  Query query(const std::string& qstr);
257 
264  bool select_db(const std::string& db);
265 
267  std::string server_version() const;
268 
292  bool set_option(Option* o);
293 
295  bool shutdown();
296 
298  std::string server_status() const;
299 
302  static bool thread_aware();
303 
306  static void thread_end();
307 
312  unsigned long thread_id();
313 
333  static bool thread_start();
334 
335 protected:
339  void build_error_message(const char* core);
340 
344  void copy(const Connection& other);
345 
348  bool parse_ipc_method(const char* server, std::string& host,
349  unsigned int& port, std::string& socket_name);
350 
352  mutable std::string error_message_;
353 
354 private:
355  DBDriver* driver_;
356  bool copacetic_;
357 };
358 
359 
360 } // end namespace mysqlpp
361 
362 // Not strictly required, but bring in our specialization subclasses
363 #include "tcp_connection.h"
364 #include "uds_connection.h"
365 #include "wnp_connection.h"
366 
367 #endif // !defined(MYSQLPP_CONNECTION_H)
368 
mysqlpp::Connection::count_rows
ulonglong count_rows(const std::string &table)
Returns the number of rows in a table.
Definition: connection.cpp:135
mysqlpp::DBDriver::kill
bool kill(unsigned long tid)
Kill a MySQL server thread.
Definition: dbdriver.h:436
mysqlpp::Connection::operator=
Connection & operator=(const Connection &rhs)
Copy an existing Connection object's state into this object.
Definition: connection.cpp:207
mysqlpp::DBDriver::errnum
int errnum()
Return last MySQL error number associated with this connection.
Definition: dbdriver.h:236
mysqlpp::DBDriver::ping
bool ping()
"Pings" the MySQL database
Definition: dbdriver.h:507
mysqlpp::Option
Define abstract interface for all *Option subclasses.
Definition: options.h:78
mysqlpp::Connection::disconnect
void disconnect()
Drop the connection to the database server.
Definition: connection.cpp:160
mysqlpp::UnixDomainSocketConnection::is_socket
static bool is_socket(const char *path, std::string *error=0)
Check that the given path names a Unix domain socket and that we have read-write permission for it.
Definition: uds_connection.cpp:67
mysqlpp::Connection::build_error_message
void build_error_message(const char *core)
Build an error message in the standard form used whenever one of the methods can't succeed because we...
Definition: connection.cpp:80
mysqlpp::WindowsNamedPipeConnection::is_wnp
static bool is_wnp(const char *server)
Check that given string denotes a Windows named pipe connection to MySQL.
Definition: wnp_connection.cpp:62
mysqlpp::DBDriver::ipc_info
std::string ipc_info()
Get information about the IPC connection to the database server.
Definition: dbdriver.h:407
mysqlpp::DBDriver::client_version
std::string client_version() const
Get database client library version.
Definition: dbdriver.h:138
mysqlpp::Connection
Manages the connection to the database server.
Definition: connection.h:81
mysqlpp::DBDriver::set_option
bool set_option(Option *o)
Sets a connection option.
Definition: dbdriver.cpp:283
mysqlpp::OptionalExceptions::set_exceptions
void set_exceptions(bool e) const
Sets the exception state to a particular value.
Definition: noexceptions.h:141
mysqlpp::DBDriver::error
const char * error()
Return error message for last MySQL error associated with this connection.
Definition: dbdriver.h:227
wnp_connection.h
Declares the WindowsNamedPipeConnection class.
mysqlpp::Connection::parse_ipc_method
bool parse_ipc_method(const char *server, std::string &host, unsigned int &port, std::string &socket_name)
Extract elements from the server parameter in formats suitable for passing to DBDriver::connect().
Definition: connection.cpp:215
mysqlpp::BadOption
Exception thrown when you pass an unrecognized option to Connection::set_option().
Definition: exceptions.h:227
options.h
Declares the Option class hierarchy, used to implement connection options in Connection and DBDriver ...
mysqlpp::Connection::server_version
std::string server_version() const
Get the database server's version string.
Definition: connection.cpp:316
mysqlpp::StoreQueryResult
StoreQueryResult set type for "store" queries.
Definition: result.h:231
mysqlpp::Query
A class for building and executing SQL queries.
Definition: query.h:143
query.h
Defines a class for building and executing SQL queries.
mysqlpp::Connection::thread_start
static bool thread_start()
Tells the underlying database driver that the current thread is now using its services.
Definition: connection.cpp:387
mysqlpp::Connection::thread_id
unsigned long thread_id()
Returns the database server's thread ID for this connection.
Definition: connection.cpp:380
mysqlpp::DBDriver
Provides a thin abstraction layer over the underlying database client library.
Definition: dbdriver.h:79
mysqlpp::DBDriver::thread_id
unsigned long thread_id()
Returns the MySQL server thread ID for this connection.
Definition: dbdriver.h:685
mysqlpp::Connection::error_message_
std::string error_message_
MySQL++ specific error, if any.
Definition: connection.h:396
mysqlpp::Connection::error
const char * error() const
Return error message for last error associated with this connection.
Definition: connection.cpp:185
mysqlpp::Connection::shutdown
bool shutdown()
Ask database server to shut down.
Definition: connection.cpp:341
mysqlpp::TCPConnection::parse_address
static bool parse_address(std::string &addr, unsigned int &port, std::string &error)
Break the given TCP/IP address up into a separate address and port form.
Definition: tcp_connection.cpp:76
mysqlpp::Connection::Connection
Connection(bool te=true)
Create object without connecting to the database server.
Definition: connection.cpp:38
mysqlpp::Connection::query
Query query(const char *qstr=0)
Return a new query object.
Definition: connection.cpp:270
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::server_version
std::string server_version()
Get the database server's version number.
Definition: dbdriver.h:562
mysqlpp::DBDriver::connect
bool connect(const MYSQL &mysql)
Establish a new connection using the same parameters as an existing connection.
Definition: dbdriver.cpp:82
mysqlpp::Connection::thread_aware
static bool thread_aware()
Returns true if both MySQL++ and database driver we're using were compiled with thread awareness.
Definition: connection.cpp:366
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::Connection::ping
bool ping()
"Pings" the database server
Definition: connection.cpp:247
mysqlpp::Connection::set_option
bool set_option(Option *o)
Sets a connection option.
Definition: connection.cpp:323
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:674
mysqlpp::DBDriver::server_status
std::string server_status()
Returns the database server's status.
Definition: dbdriver.h:638
mysqlpp::Connection::client_version
std::string client_version() const
Get version of library underpinning the current database driver.
Definition: connection.cpp:89
mysqlpp::Connection::~Connection
virtual ~Connection()
Destroy object.
Definition: connection.cpp:72
noexceptions.h
Declares interface that allows exceptions to be optional.
mysqlpp::ConnectionFailed
Exception thrown when there is a problem related to the database server connection.
Definition: exceptions.h:350
mysqlpp::Connection::select_db
bool select_db(const std::string &db)
Change to a different database managed by the database server we are connected to.
Definition: connection.cpp:284
mysqlpp::DBDriver::shutdown
bool shutdown()
Ask database server to shut down.
Definition: dbdriver.cpp:332
result.h
Declares classes for holding information about SQL query results.
mysqlpp::Connection::drop_db
bool drop_db(const std::string &db)
Asks the database server to drop (destroy) a database.
Definition: connection.cpp:168
connection.h
Declares the Connection class.
common.h
This file includes top-level definitions for use both internal to the library, and outside it....
uds_connection.h
Declares the UnixDomainSocketConnection class.
mysqlpp::Connection::connected
bool connected() const
Returns true if connection was established successfully.
Definition: connection.cpp:119
mysqlpp::Query::store
StoreQueryResult store()
Execute a query that can return a result set.
Definition: query.cpp:511
mysqlpp::Connection::thread_end
static void thread_end()
Tells the underlying database driver that this thread is done using the library.
Definition: connection.cpp:373
tcp_connection.h
Declares the TCPConnection class.
mysqlpp::Connection::connect
virtual bool connect(const char *db=0, const char *server=0, const char *user=0, const char *password=0, unsigned int port=0)
Connect to database after object is created.
Definition: connection.cpp:96
mysqlpp::DBSelectionFailed
Exception thrown when the program tries to select a new database and the database server refuses for ...
Definition: exceptions.h:381
mysqlpp::OptionalExceptions
Interface allowing a class to have optional exceptions.
Definition: noexceptions.h:92
mysqlpp::Connection::create_db
bool create_db(const std::string &db)
Ask the database server to create a database.
Definition: connection.cpp:150
mysqlpp::DBDriver::disconnect
void disconnect()
Drop the connection to the database server.
Definition: dbdriver.cpp:134
mysqlpp::DBDriver::protocol_version
int protocol_version()
Returns version number of MySQL protocol this connection is using.
Definition: dbdriver.h:517
mysqlpp::Connection::kill
bool kill(unsigned long tid) const
Kill a database server thread.
Definition: connection.cpp:199
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::select_db
bool select_db(const char *db)
Asks the database server to switch to a different database.
Definition: dbdriver.h:553
mysqlpp::Connection::errnum
int errnum()
Return last error number associated with this connection.
Definition: connection.cpp:178
mysqlpp::DBDriver::thread_start
static bool thread_start()
Tells the underlying C API library that the current thread will be using the library's services.
Definition: dbdriver.h:709
mysqlpp::OptionalExceptions::throw_exceptions
bool throw_exceptions() const
Returns true if exceptions are enabled.
Definition: noexceptions.h:134
mysqlpp::Connection::copy
void copy(const Connection &other)
Establish a new connection as a copy of an existing one.
Definition: connection.cpp:126
mysqlpp::Connection::server_status
std::string server_status() const
Returns information about database server's status.
Definition: connection.cpp:309
mysqlpp::Query::exec
bool exec()
Execute a built-up query.
Definition: query.h:317
mysqlpp::Connection::ipc_info
std::string ipc_info() const
Get information about the IPC connection to the database server.
Definition: connection.cpp:192
mysqlpp::Connection::protocol_version
int protocol_version() const
Returns version number of the protocol the database driver uses to communicate with the server.
Definition: connection.cpp:263