Author: mysqlpp
Date: Fri Aug 14 06:06:56 2009
New Revision: 2566
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2566&view=rev
Log:
Library wasn't coping well with a connect(), disconnect(), connect()
call pattern on Windows when the DB server was on the same machine and
the user asked for a pipe connection to it. The second connect() call
would crash because we hadn't called mysql_init() again. Other OS and
IPC methods seem not to care about this.
Modified:
trunk/Wishlist
trunk/lib/dbdriver.cpp
trunk/lib/dbdriver.h
Modified: trunk/Wishlist
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=2566&r1=2565&r2=2566&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Fri Aug 14 06:06:56 2009
@@ -454,3 +454,12 @@
could be an adapter between one end of the queue and a Connection
object, which creates Queries to handle standardized messages,
delivering the results back to the queue.
+
+ o Get rid of two-step create in DBDriver, requiring a connection to
+ be established in ctor for object to be valid? RAII. The
+ DB-specific functions that don't require a connection can be
+ static methods. Tricky bit: a failed Connection::connect() call
+ will likely be followed by an indirect call to DBDriver::err*().
+ Does Connection cache the error value and message? If we can pull
+ this off, we can drop the DBDriver::is_connected_ flag and change
+ Connection::connected() to "return driver_ != 0".
Modified: trunk/lib/dbdriver.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/dbdriver.cpp?rev=2566&r1=2565&r2=2566&view=diff
==============================================================================
--- trunk/lib/dbdriver.cpp (original)
+++ trunk/lib/dbdriver.cpp Fri Aug 14 06:06:56 2009
@@ -46,7 +46,10 @@
DBDriver::DBDriver() :
is_connected_(false)
{
- mysql_init(&mysql_);
+ // We won't allow calls to mysql_*() functions that take a MYSQL
+ // object until we get a connection up. Such calls are nonsense.
+ // MySQL++ coped with them before, but this masks bugs.
+ memset(&mysql_, 0, sizeof(mysql_));
}
@@ -75,16 +78,7 @@
unsigned int port, const char* db, const char* user,
const char* password)
{
- // Drop previous connection, if any
- if (connected()) {
- disconnect();
- }
-
- // Set defaults for connection options. User can override these
- // by calling set_option() before connect().
- set_option_default(new ReadDefaultFileOption("my"));
-
- // Establish the connection
+ connect_prepare();
return is_connected_ =
mysql_real_connect(&mysql_, host, user, password, db,
port, socket_name, mysql_.client_flag);
@@ -94,16 +88,7 @@
bool
DBDriver::connect(const MYSQL& other)
{
- // Drop previous connection, if any
- if (connected()) {
- disconnect();
- }
-
- // Set defaults for connection options. User can override these
- // by calling set_option() before connect().
- set_option_default(new ReadDefaultFileOption("my"));
-
- // Establish the connection
+ connect_prepare();
return is_connected_ =
mysql_real_connect(&mysql_, other.host, other.user,
other.passwd, other.db, other.port, other.unix_socket,
@@ -112,6 +97,22 @@
void
+DBDriver::connect_prepare()
+{
+ // Drop previous connection, if any, then prepare underlying C API
+ // library to establish a new connection.
+ if (connected()) {
+ disconnect();
+ }
+ mysql_init(&mysql_);
+
+ // Set defaults for connection options. User can override these
+ // by calling set_option() before connect().
+ set_option_default(new ReadDefaultFileOption("my"));
+}
+
+
+void
DBDriver::copy(const DBDriver& other)
{
if (other.connected()) {
@@ -128,6 +129,7 @@
{
if (is_connected_) {
mysql_close(&mysql_);
+ memset(&mysql_, 0, sizeof(mysql_));
is_connected_ = false;
}
}
Modified: trunk/lib/dbdriver.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/dbdriver.h?rev=2566&r1=2565&r2=2566&view=diff
==============================================================================
--- trunk/lib/dbdriver.h (original)
+++ trunk/lib/dbdriver.h Fri Aug 14 06:06:56 2009
@@ -133,9 +133,9 @@
/// \brief Drop the connection to the database server
///
- /// This method is protected because it should only be used within
- /// the library. Unless you use the default constructor, this
- /// object should always be connected.
+ /// This method should only be used by MySQL++ library internals.
+ /// Unless you use the default constructor, this object should
+ /// always be connected.
void disconnect();
/// \brief Drop a database
@@ -593,6 +593,11 @@
/// Wraps \c mysql_use_result() in the MySQL C API.
MYSQL_RES* use_result() { return mysql_use_result(&mysql_); }
+protected:
+ /// \brief Does things common to both connect() overloads, before
+ /// each go and establish the connection in their different ways.
+ void connect_prepare();
+
private:
/// \brief Data type of the list of applied connection options
typedef std::deque<Option*> OptionList;
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits