Author: wyoung
Date: Mon Jun 26 21:03:19 2006
New Revision: 1298

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1298&view=rev
Log:
Added copy semantics to Connection.  Previously, if you assigned one
connection object to another, you'd just get a bitwise copy, which would
fail to work, because the underlying MYSQL data structure isn't meant to
be copied.  What you actually want is for it to establish a new
connection using the same parameters, which is what it does do now.

Modified:
    trunk/lib/connection.cpp
    trunk/lib/connection.h

Modified: trunk/lib/connection.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.cpp?rev=1298&r1=1297&r2=1298&view=diff
==============================================================================
--- trunk/lib/connection.cpp (original)
+++ trunk/lib/connection.cpp Mon Jun 26 21:03:19 2006
@@ -134,9 +134,25 @@
 }
 
 
+Connection::Connection(const Connection& other) :
+OptionalExceptions(),
+Lockable(false)
+{
+       copy(other);
+}
+
+
 Connection::~Connection()
 {
        disconnect();
+}
+
+
+Connection&
+Connection::operator=(const Connection& rhs)
+{
+       copy(rhs);
+       return *this;
 }
 
 
@@ -187,6 +203,38 @@
        }
 
        return success_;
+}
+
+
+bool
+Connection::connect(const MYSQL& mysql)
+{
+       return connect(mysql.db, mysql.host, mysql.user, mysql.passwd,
+                       mysql.port, mysql.options.compress,
+                       mysql.options.connect_timeout, mysql.unix_socket,
+                       mysql.client_flag);
+}
+
+
+void
+Connection::copy(const Connection& other)
+{
+       if (connected()) {
+               disconnect();
+       }
+
+       mysql_init(&mysql_);
+       set_exceptions(other.throw_exceptions());
+
+       if (other.connected()) {
+               // Try to reconnect to server using same parameters
+               connect(other.mysql_);
+       }
+       else {
+               is_connected_ = false;
+               connecting_ = false;
+               success_ = false;
+       }
 }
 
 

Modified: trunk/lib/connection.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/connection.h?rev=1298&r1=1297&r2=1298&view=diff
==============================================================================
--- trunk/lib/connection.h (original)
+++ trunk/lib/connection.h Mon Jun 26 21:03:19 2006
@@ -147,6 +147,18 @@
                        unsigned int connect_timeout = 60, cchar* socket_name = 
0,
                        unsigned int client_flag = 0);
 
+       /// \brief Establish a new connection using the same parameters as
+       /// an existing C API connection.
+       ///
+       /// \param other existing Connection object
+       MYSQLPP_EXPORT Connection(const Connection& other);
+
+       /// \brief Establish a new connection using the same parameters as
+       /// an existing C API connection.
+       ///
+       /// \param mysql existing MySQL C API connection object
+       MYSQLPP_EXPORT bool connect(const MYSQL& mysql);
+
        /// \brief Destroy connection object
        MYSQLPP_EXPORT ~Connection();
 
@@ -219,6 +231,10 @@
        /// \endcode
        operator bool() { return success(); }
 
+       /// \brief Copy an existing Connection object's state into this
+       /// object.
+       Connection& operator=(const Connection& rhs);
+
        /// \brief Return error message for last MySQL error associated with
        /// this connection.
        ///
@@ -496,6 +512,11 @@
        bool set_option_impl(enum_mysql_set_option msoption);
 #endif
 
+       /// \brief Establish a new connection as a copy of an existing one
+       ///
+       /// \param other the connection to copy
+       void copy(const Connection& other);
+
 private:
        friend class ResNSel;
        friend class ResUse;


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

Reply via email to