mirror of
https://gitlab.isc.org/isc-projects/kea
synced 2025-08-31 14:05:33 +00:00
[master] Merge branch 'trac2821'
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
#include <dhcpsrv/lease_mgr.h>
|
||||
|
||||
#include <boost/scoped_ptr.hpp>
|
||||
#include <boost/utility.hpp>
|
||||
#include <mysql/mysql.h>
|
||||
|
||||
#include <time.h>
|
||||
@@ -26,6 +27,54 @@
|
||||
namespace isc {
|
||||
namespace dhcp {
|
||||
|
||||
/// @brief MySQL Handle Holder
|
||||
///
|
||||
/// Small RAII object for safer initialization, will close the database
|
||||
/// connection upon destruction. This means that if an exception is thrown
|
||||
/// during database initialization, resources allocated to the database are
|
||||
/// guaranteed to be freed.
|
||||
///
|
||||
/// It makes no sense to copy an object of this class. After the copy, both
|
||||
/// objects would contain pointers to the same MySql context object. The
|
||||
/// destruction of one would invalid the context in the remaining object.
|
||||
/// For this reason, the class is declared noncopyable.
|
||||
class MySqlHolder : public boost::noncopyable {
|
||||
public:
|
||||
|
||||
/// @brief Constructor
|
||||
///
|
||||
/// Initialize MySql and store the associated context object.
|
||||
///
|
||||
/// @throw DbOpenError Unable to initialize MySql handle.
|
||||
MySqlHolder() : mysql_(mysql_init(NULL)) {
|
||||
if (mysql_ == NULL) {
|
||||
isc_throw(DbOpenError, "unable to initialize MySQL");
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Destructor
|
||||
///
|
||||
/// Frees up resources allocated by the initialization of MySql.
|
||||
~MySqlHolder() {
|
||||
if (mysql_ != NULL) {
|
||||
mysql_close(mysql_);
|
||||
}
|
||||
// The library itself shouldn't be needed anymore
|
||||
mysql_library_end();
|
||||
}
|
||||
|
||||
/// @brief Conversion Operator
|
||||
///
|
||||
/// Allows the MySqlHolder object to be passed as the context argument to
|
||||
/// mysql_xxx functions.
|
||||
operator MYSQL*() const {
|
||||
return (mysql_);
|
||||
}
|
||||
|
||||
private:
|
||||
MYSQL* mysql_; ///< Initialization context
|
||||
};
|
||||
|
||||
// Define the current database schema values
|
||||
|
||||
const uint32_t CURRENT_VERSION_VERSION = 1;
|
||||
@@ -379,7 +428,7 @@ public:
|
||||
/// @param cltt Reference to location where client last transmit time
|
||||
/// is put.
|
||||
static
|
||||
void convertFromDatabaseTime(const MYSQL_TIME& expire,
|
||||
void convertFromDatabaseTime(const MYSQL_TIME& expire,
|
||||
uint32_t valid_lifetime, time_t& cltt);
|
||||
///@}
|
||||
|
||||
@@ -616,7 +665,7 @@ private:
|
||||
/// declare them as "mutable".)
|
||||
boost::scoped_ptr<MySqlLease4Exchange> exchange4_; ///< 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<std::string> text_statements_; ///< Raw text of statements
|
||||
};
|
||||
|
Reference in New Issue
Block a user