Parcourir la source

[2821] Move MySqlHolder to header, and use as member

Half-related change, if available, call mysql_library_end() at the end of the unit test run
Jelte Jansen il y a 12 ans
Parent
commit
e58c2fd32c

+ 1 - 42
src/lib/dhcpsrv/mysql_lease_mgr.cc

@@ -193,32 +193,6 @@ TaggedStatement tagged_statements[] = {
     {MySqlLeaseMgr::NUM_STATEMENTS, NULL}
     {MySqlLeaseMgr::NUM_STATEMENTS, NULL}
 };
 };
 
 
-// Small RAII object for safer initialization, will close the database
-// connection upon destruction, unless release() has been called.
-class MySQLHolder {
-public:
-    MySQLHolder(MYSQL* mysql) : mysql_(mysql) {}
-
-    ~MySQLHolder() {
-        if (mysql_) {
-            mysql_close(mysql_);
-        }
-    }
-
-    MYSQL* get_ptr() {
-        return (mysql_);
-    }
-
-    MYSQL* release() {
-        MYSQL* ptr = mysql_;
-        mysql_ = NULL;
-        return (ptr);
-    }
-
-private:
-    MYSQL* mysql_;
-};
-
 };  // Anonymous namespace
 };  // Anonymous namespace
 
 
 
 
@@ -899,15 +873,7 @@ private:
 // MySqlLeaseMgr Constructor and Destructor
 // MySqlLeaseMgr Constructor and Destructor
 
 
 MySqlLeaseMgr::MySqlLeaseMgr(const LeaseMgr::ParameterMap& parameters)
 MySqlLeaseMgr::MySqlLeaseMgr(const LeaseMgr::ParameterMap& parameters)
-    : LeaseMgr(parameters), mysql_(NULL) {
-
-    // Allocate context for MySQL
-    MySQLHolder mysql_holder(mysql_init(NULL));
-
-    mysql_ = mysql_holder.get_ptr();
-    if (mysql_ == NULL) {
-        isc_throw(DbOpenError, "unable to initialize MySQL");
-    }
+    : LeaseMgr(parameters) {
 
 
     // Open the database.
     // Open the database.
     openDatabase();
     openDatabase();
@@ -929,9 +895,6 @@ MySqlLeaseMgr::MySqlLeaseMgr(const LeaseMgr::ParameterMap& parameters)
     // program and the database.
     // program and the database.
     exchange4_.reset(new MySqlLease4Exchange());
     exchange4_.reset(new MySqlLease4Exchange());
     exchange6_.reset(new MySqlLease6Exchange());
     exchange6_.reset(new MySqlLease6Exchange());
-
-    // Let the real destructor take care of cleaning up now
-    mysql_holder.release();
 }
 }
 
 
 
 
@@ -945,10 +908,6 @@ MySqlLeaseMgr::~MySqlLeaseMgr() {
             statements_[i] = NULL;
             statements_[i] = NULL;
         }
         }
     }
     }
-
-    // Close the database
-    mysql_close(mysql_);
-    mysql_ = NULL;
 }
 }
 
 
 
 

+ 26 - 2
src/lib/dhcpsrv/mysql_lease_mgr.h

@@ -26,6 +26,30 @@
 namespace isc {
 namespace isc {
 namespace dhcp {
 namespace dhcp {
 
 
+/// Small RAII object for safer initialization, will close the database
+/// connection upon destruction
+class MySqlHolder {
+public:
+    MySqlHolder() : mysql_(mysql_init(NULL)) {
+        if (mysql_ == NULL) {
+            isc_throw(DbOpenError, "unable to initialize MySQL");
+        }
+    }
+
+    ~MySqlHolder() {
+        if (mysql_) {
+            mysql_close(mysql_);
+        }
+    }
+
+    operator MYSQL*() const {
+        return (mysql_);
+    }
+
+private:
+    MYSQL* mysql_;
+};
+
 // Define the current database schema values
 // Define the current database schema values
 
 
 const uint32_t CURRENT_VERSION_VERSION = 1;
 const uint32_t CURRENT_VERSION_VERSION = 1;
@@ -379,7 +403,7 @@ public:
     /// @param cltt Reference to location where client last transmit time
     /// @param cltt Reference to location where client last transmit time
     ///        is put.
     ///        is put.
     static
     static
-    void convertFromDatabaseTime(const MYSQL_TIME& expire, 
+    void convertFromDatabaseTime(const MYSQL_TIME& expire,
                                  uint32_t valid_lifetime, time_t& cltt);
                                  uint32_t valid_lifetime, time_t& cltt);
     ///@}
     ///@}
 
 
@@ -616,7 +640,7 @@ private:
     /// declare them as "mutable".)
     /// declare them as "mutable".)
     boost::scoped_ptr<MySqlLease4Exchange> exchange4_; ///< Exchange object
     boost::scoped_ptr<MySqlLease4Exchange> exchange4_; ///< Exchange object
     boost::scoped_ptr<MySqlLease6Exchange> exchange6_; ///< Exchange object
     boost::scoped_ptr<MySqlLease6Exchange> exchange6_; ///< Exchange object
-    MYSQL*              mysql_;                 ///< MySQL context object
+    MySqlHolder mysql_;
     std::vector<MYSQL_STMT*> statements_;       ///< Prepared statements
     std::vector<MYSQL_STMT*> statements_;       ///< Prepared statements
     std::vector<std::string> text_statements_;  ///< Raw text of statements
     std::vector<std::string> text_statements_;  ///< Raw text of statements
 };
 };

+ 9 - 0
src/lib/dhcpsrv/tests/run_unittests.cc

@@ -12,10 +12,15 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 // PERFORMANCE OF THIS SOFTWARE.
 
 
+#include <config.h>
 #include <log/logger_support.h>
 #include <log/logger_support.h>
 
 
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
 
 
+#ifdef HAVE_MYSQL
+#include <mysql/mysql.h>
+#endif
+
 int
 int
 main(int argc, char* argv[]) {
 main(int argc, char* argv[]) {
     ::testing::InitGoogleTest(&argc, argv);
     ::testing::InitGoogleTest(&argc, argv);
@@ -23,5 +28,9 @@ main(int argc, char* argv[]) {
 
 
     int result = RUN_ALL_TESTS();
     int result = RUN_ALL_TESTS();
 
 
+#ifdef HAVE_MYSQL
+    mysql_library_end();
+#endif
+
     return (result);
     return (result);
 }
 }