2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-09-04 07:55:18 +00:00

[3681] DataSource class added.

- code refactoring after last commit, clean-up, compilation fixes
 - Makefile updates
 - Added new unit-tests for DataSource (mostly moved from LeaseMgr
   unit-tests)
This commit is contained in:
Tomek Mrugalski
2015-04-17 21:21:26 +02:00
parent 2a3ac3ef7d
commit 373aa0a40e
19 changed files with 446 additions and 459 deletions

View File

@@ -28,57 +28,9 @@
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 invalidate 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 = 2;
const uint32_t CURRENT_VERSION_VERSION = 3;
const uint32_t CURRENT_VERSION_MINOR = 0;
@@ -356,6 +308,54 @@ public:
/// failed.
virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
/// @brief Return backend type
///
/// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
///
/// @return Type of the backend.
virtual std::string getType() const {
return (std::string("mysql"));
}
/// @brief Returns backend name.
///
/// Each backend have specific name, e.g. "mysql" or "sqlite".
///
/// @return Name of the backend.
virtual std::string getName() const;
/// @brief Returns description of the backend.
///
/// This description may be multiline text that describes the backend.
///
/// @return Description of the backend.
virtual std::string getDescription() const;
/// @brief Returns backend version.
///
/// @return Version number as a pair of unsigned integers. "first" is the
/// major version number, "second" the minor number.
///
/// @throw isc::dhcp::DbOperationError An operation on the open database has
/// failed.
virtual std::pair<uint32_t, uint32_t> getVersion() const;
/// @brief Commit Transactions
///
/// Commits all pending database operations. On databases that don't
/// support transactions, this is a no-op.
///
/// @throw DbOperationError Iif the commit failed.
virtual void commit();
/// @brief Rollback Transactions
///
/// Rolls back all pending database operations. On databases that don't
/// support transactions, this is a no-op.
///
/// @throw DbOperationError If the rollback failed.
virtual void rollback();
///@{
/// The following methods are used to convert between times and time
/// intervals stored in the Lease object, and the times stored in the
@@ -409,7 +409,27 @@ public:
uint32_t valid_lifetime, time_t& cltt);
///@}
/// @brief Statement Tags
///
/// The contents of the enum are indexes into the list of SQL statements
enum StatementIndex {
DELETE_LEASE4, // Delete from lease4 by address
DELETE_LEASE6, // Delete from lease6 by address
GET_LEASE4_ADDR, // Get lease4 by address
GET_LEASE4_CLIENTID, // Get lease4 by client ID
GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
GET_LEASE4_HWADDR, // Get lease4 by HW address
GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
GET_LEASE6_ADDR, // Get lease6 by address
GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
GET_VERSION, // Obtain version number
INSERT_LEASE4, // Add entry to lease4 table
INSERT_LEASE6, // Add entry to lease6 table
UPDATE_LEASE4, // Update a Lease4 entry
UPDATE_LEASE6, // Update a Lease6 entry
NUM_STATEMENTS // Number of statements
};
private: