Author: wyoung
Date: Fri Mar 10 21:04:16 2006
New Revision: 1239

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1239&view=rev
Log:
Several functions in Connection now check whether we're connected before
calling MySQL C API functions that require that, so we can return a
MySQL++ level error if we're not.  Before, we'd get a less useful C API
level error in this instance.

Modified:
    branches/v2.1-bakefile/Wishlist
    branches/v2.1-bakefile/lib/connection.cpp
    branches/v2.1-bakefile/lib/connection.h

Modified: branches/v2.1-bakefile/Wishlist
URL: 
http://svn.gna.org/viewcvs/mysqlpp/branches/v2.1-bakefile/Wishlist?rev=1239&r1=1238&r2=1239&view=diff
==============================================================================
--- branches/v2.1-bakefile/Wishlist (original)
+++ branches/v2.1-bakefile/Wishlist Fri Mar 10 21:04:16 2006
@@ -100,17 +100,6 @@
          that can be extended, instead of reinventing the wheel.
          Boost, perhaps?
 
-       o When you create a Connection object with its default ctor and
-         don't .connect() it, several of its functions can fail to
-         work correctly.  ping(), for one, because the MYSQL handle
-         isn't initialized until the connection is established.
-         Decide how to cope: a) init the handle in all ctors; b)
-         throw ObjectNotInitialized when the library knows the
-         call will fail; or c) just return a failure code and hope
-         the user is checking it.  Could be a different answer for
-         each function.  Keep in mind the consequences for database-
-         independence here.
-
        o Build a forward iterator mechanism for ResUse.  Make it
          general enough that you can use it with STL algorithms
          like find_if().  Then make an example to demonstrate this
@@ -162,6 +151,12 @@
        schedule, the best way to ensure it is to start coding and
        provide a patch!
 
+
+       o Several MySQL++ functions wrap the MySQL C API too literally:
+         they indicate success by returning 0 instead of true,
+         as most other wrapper functions do.  Fixing this won't
+         necessarily break the ABI, but it should wait for v3.x
+         because it could silently break programs.
 
        o Create adaptors for std::bitset, for storing binary data in a
          MySQL table.  Make two options available, one for storing

Modified: branches/v2.1-bakefile/lib/connection.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/branches/v2.1-bakefile/lib/connection.cpp?rev=1239&r1=1238&r2=1239&view=diff
==============================================================================
--- branches/v2.1-bakefile/lib/connection.cpp (original)
+++ branches/v2.1-bakefile/lib/connection.cpp Fri Mar 10 21:04:16 2006
@@ -2,7 +2,7 @@
  connection.cpp - Implements the Connection class.
 
  Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
- MySQL AB, and (c) 2004, 2005 by Educational Technology Resources, Inc.
+ MySQL AB, and (c) 2004-2006 by Educational Technology Resources, Inc.
  Others may also hold copyrights on code in this file.  See the CREDITS
  file in the top directory of the distribution for details.
 
@@ -216,12 +216,22 @@
 bool
 Connection::select_db(const char *db)
 {
-       bool suc = !(mysql_select_db(&mysql_, db));
-       if (throw_exceptions() && !suc) {
-               throw DBSelectionFailed(error());
-       }
-       else {
-               return suc;
+       if (connected()) {
+               bool suc = !(mysql_select_db(&mysql_, db));
+               if (throw_exceptions() && !suc) {
+                       throw DBSelectionFailed(error());
+               }
+               else {
+                       return suc;
+               }
+       }
+       else {
+               if (throw_exceptions()) {
+                       throw DBSelectionFailed("MySQL++ connection not 
established");
+               }
+               else {
+                       return false;
+               }
        }
 }
 
@@ -229,16 +239,26 @@
 bool
 Connection::reload()
 {
-       bool suc = !mysql_reload(&mysql_);
-       if (throw_exceptions() && !suc) {
-               // Reloading grant tables through this API isn't precisely a
-               // query, but it's acceptable to signal errors with BadQuery
-               // because the new mechanism is the FLUSH PRIVILEGES statement.
-               // A program won't have to change when moving to the new way.
-               throw BadQuery(error());
-       }
-       else {
-               return suc;
+       if (connected()) {
+               bool suc = !mysql_reload(&mysql_);
+               if (throw_exceptions() && !suc) {
+                       // Reloading grant tables through this API isn't 
precisely a
+                       // query, but it's acceptable to signal errors with 
BadQuery
+                       // because the new mechanism is the FLUSH PRIVILEGES 
query.
+                       // A program won't have to change when doing it the new 
way.
+                       throw BadQuery(error());
+               }
+               else {
+                       return suc;
+               }
+       }
+       else {
+               if (throw_exceptions()) {
+                       throw BadQuery("MySQL++ connection not established");
+               }
+               else {
+                       return false;
+               }
        }
 }
 
@@ -246,12 +266,22 @@
 bool
 Connection::shutdown()
 {
-       bool suc = !(mysql_shutdown(&mysql_ SHUTDOWN_ARG));
-       if (throw_exceptions() && !suc) {
-               throw ConnectionFailed(error());
-       }
-       else {
-               return suc;
+       if (connected()) {
+               bool suc = !(mysql_shutdown(&mysql_ SHUTDOWN_ARG));
+               if (throw_exceptions() && !suc) {
+                       throw ConnectionFailed(error());
+               }
+               else {
+                       return suc;
+               }
+       }
+       else {
+               if (throw_exceptions()) {
+                       throw ConnectionFailed("MySQL++ connection not 
established");
+               }
+               else {
+                       return false;
+               }
        }
 }
 
@@ -605,5 +635,19 @@
 }
 
 
+int
+Connection::ping()
+{
+       if (connected()) {
+               return mysql_ping(&mysql_);
+       }
+       else {
+               // Not connected, and we've forgotten everything we need in
+               // order to re-connect, if we once were connected.
+               return 1;
+       }
+}
+
+
 } // end namespace mysqlpp
 

Modified: branches/v2.1-bakefile/lib/connection.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/branches/v2.1-bakefile/lib/connection.h?rev=1239&r1=1238&r2=1239&view=diff
==============================================================================
--- branches/v2.1-bakefile/lib/connection.h (original)
+++ branches/v2.1-bakefile/lib/connection.h Fri Mar 10 21:04:16 2006
@@ -245,15 +245,16 @@
 
        /// \brief "Pings" the MySQL database
        ///
-       /// If server doesn't respond, this function tries to reconnect.
+       /// Wraps \c mysql_ping() in the C API.  As a result, this function
+       /// will try to reconnect to the server if the connection has been
+       /// dropped.
        /// 
        /// \retval 0 if server is responding, regardless of whether we had
        /// to reconnect or not
-       /// \retval nonzero if server did not respond to ping and we could
-       /// not re-establish the connection
-       ///
-       /// Simply wraps \c mysql_ping() in the C API.
-       int ping() { return mysql_ping(&mysql_); }
+       /// \retval nonzero if either we already know the connection is down
+       /// and cannot re-establish it, or if the server did not respond to
+       /// the ping and we could not re-establish the connection.
+       int ping();
 
        /// \brief Kill a MySQL server thread
        ///


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to