mysql_lease_mgr.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef MYSQL_LEASE_MGR_H
  7. #define MYSQL_LEASE_MGR_H
  8. #include <dhcp/hwaddr.h>
  9. #include <dhcpsrv/lease_mgr.h>
  10. #include <dhcpsrv/mysql_connection.h>
  11. #include <boost/scoped_ptr.hpp>
  12. #include <boost/utility.hpp>
  13. #include <mysql.h>
  14. #include <time.h>
  15. namespace isc {
  16. namespace dhcp {
  17. // Forward declaration of the Lease exchange objects. These classes are defined
  18. // in the .cc file.
  19. class MySqlLease4Exchange;
  20. class MySqlLease6Exchange;
  21. /// @brief MySQL Lease Manager
  22. ///
  23. /// This class provides the \ref isc::dhcp::LeaseMgr interface to the MySQL
  24. /// database. Use of this backend presupposes that a MySQL database is
  25. /// available and that the Kea schema has been created within it.
  26. class MySqlLeaseMgr : public LeaseMgr {
  27. public:
  28. /// @brief Constructor
  29. ///
  30. /// Uses the following keywords in the parameters passed to it to
  31. /// connect to the database:
  32. /// - name - Name of the database to which to connect (mandatory)
  33. /// - host - Host to which to connect (optional, defaults to "localhost")
  34. /// - user - Username under which to connect (optional)
  35. /// - password - Password for "user" on the database (optional)
  36. ///
  37. /// If the database is successfully opened, the version number in the
  38. /// schema_version table will be checked against hard-coded value in
  39. /// the implementation file.
  40. ///
  41. /// Finally, all the SQL commands are pre-compiled.
  42. ///
  43. /// @param parameters A data structure relating keywords and values
  44. /// concerned with the database.
  45. ///
  46. /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given
  47. /// @throw isc::dhcp::DbOpenError Error opening the database
  48. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  49. /// failed.
  50. MySqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters);
  51. /// @brief Destructor (closes database)
  52. virtual ~MySqlLeaseMgr();
  53. /// @brief Local version of getDBVersion() class method
  54. static std::string getDBVersion();
  55. /// @brief Adds an IPv4 lease
  56. ///
  57. /// @param lease lease to be added
  58. ///
  59. /// @result true if the lease was added, false if not (because a lease
  60. /// with the same address was already there).
  61. ///
  62. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  63. /// failed.
  64. virtual bool addLease(const Lease4Ptr& lease);
  65. /// @brief Adds an IPv6 lease
  66. ///
  67. /// @param lease lease to be added
  68. ///
  69. /// @result true if the lease was added, false if not (because a lease
  70. /// with the same address was already there).
  71. ///
  72. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  73. /// failed.
  74. virtual bool addLease(const Lease6Ptr& lease);
  75. /// @brief Returns an IPv4 lease for specified IPv4 address
  76. ///
  77. /// This method return a lease that is associated with a given address.
  78. /// For other query types (by hardware addr, by Client ID) there can be
  79. /// several leases in different subnets (e.g. for mobile clients that
  80. /// got address in different subnets). However, for a single address
  81. /// there can be only one lease, so this method returns a pointer to
  82. /// a single lease, not a container of leases.
  83. ///
  84. /// @param addr address of the searched lease
  85. ///
  86. /// @return smart pointer to the lease (or NULL if a lease is not found)
  87. ///
  88. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  89. /// fit into the space allocated for the result. This indicates a
  90. /// programming error.
  91. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  92. /// failed.
  93. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
  94. /// @brief Returns existing IPv4 leases for specified hardware address.
  95. ///
  96. /// Although in the usual case there will be only one lease, for mobile
  97. /// clients or clients with multiple static/fixed/reserved leases there
  98. /// can be more than one. Thus return type is a container, not a single
  99. /// pointer.
  100. ///
  101. /// @param hwaddr hardware address of the client
  102. ///
  103. /// @return lease collection
  104. ///
  105. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  106. /// fit into the space allocated for the result. This indicates a
  107. /// programming error.
  108. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  109. /// failed.
  110. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
  111. /// @brief Returns existing IPv4 leases for specified hardware address
  112. /// and a subnet
  113. ///
  114. /// There can be at most one lease for a given HW address in a single
  115. /// pool, so this method with either return a single lease or NULL.
  116. ///
  117. /// @param hwaddr hardware address of the client
  118. /// @param subnet_id identifier of the subnet that lease must belong to
  119. ///
  120. /// @return a pointer to the lease (or NULL if a lease is not found)
  121. ///
  122. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  123. /// fit into the space allocated for the result. This indicates a
  124. /// programming error.
  125. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  126. /// failed.
  127. virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
  128. SubnetID subnet_id) const;
  129. /// @brief Returns existing IPv4 lease for specified client-id
  130. ///
  131. /// Although in the usual case there will be only one lease, for mobile
  132. /// clients or clients with multiple static/fixed/reserved leases there
  133. /// can be more than one. Thus return type is a container, not a single
  134. /// pointer.
  135. ///
  136. /// @param clientid client identifier
  137. ///
  138. /// @return lease collection
  139. ///
  140. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  141. /// fit into the space allocated for the result. This indicates a
  142. /// programming error.
  143. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  144. /// failed.
  145. virtual Lease4Collection getLease4(const ClientId& clientid) const;
  146. /// @brief Returns IPv4 lease for the specified client identifier, HW
  147. /// address and subnet identifier.
  148. ///
  149. /// @param client_id A client identifier.
  150. /// @param hwaddr Hardware address.
  151. /// @param subnet_id A subnet identifier.
  152. ///
  153. /// @return A pointer to the lease or NULL if the lease is not found.
  154. /// @throw isc::NotImplemented On every call as this function is currently
  155. /// not implemented for the MySQL backend.
  156. virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr,
  157. SubnetID subnet_id) const;
  158. /// @brief Returns existing IPv4 lease for specified client-id
  159. ///
  160. /// There can be at most one lease for a given HW address in a single
  161. /// pool, so this method with either return a single lease or NULL.
  162. ///
  163. /// @param clientid client identifier
  164. /// @param subnet_id identifier of the subnet that lease must belong to
  165. ///
  166. /// @return a pointer to the lease (or NULL if a lease is not found)
  167. ///
  168. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  169. /// fit into the space allocated for the result. This indicates a
  170. /// programming error.
  171. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  172. /// failed.
  173. virtual Lease4Ptr getLease4(const ClientId& clientid,
  174. SubnetID subnet_id) const;
  175. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  176. ///
  177. /// For a given address, we assume that there will be only one lease.
  178. /// The assumption here is that there will not be site or link-local
  179. /// addresses used, so there is no way of having address duplication.
  180. ///
  181. /// @param type specifies lease type: (NA, TA or PD)
  182. /// @param addr address of the searched lease
  183. ///
  184. /// @return smart pointer to the lease (or NULL if a lease is not found)
  185. ///
  186. /// @throw isc::BadValue record retrieved from database had an invalid
  187. /// lease type field.
  188. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  189. /// fit into the space allocated for the result. This indicates a
  190. /// programming error.
  191. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  192. /// failed.
  193. virtual Lease6Ptr getLease6(Lease::Type type,
  194. const isc::asiolink::IOAddress& addr) const;
  195. /// @brief Returns existing IPv6 leases for a given DUID+IA combination
  196. ///
  197. /// Although in the usual case there will be only one lease, for mobile
  198. /// clients or clients with multiple static/fixed/reserved leases there
  199. /// can be more than one. Thus return type is a container, not a single
  200. /// pointer.
  201. ///
  202. /// @param type specifies lease type: (NA, TA or PD)
  203. /// @param duid client DUID
  204. /// @param iaid IA identifier
  205. ///
  206. /// @return smart pointer to the lease (or NULL if a lease is not found)
  207. ///
  208. /// @throw isc::BadValue record retrieved from database had an invalid
  209. /// lease type field.
  210. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  211. /// fit into the space allocated for the result. This indicates a
  212. /// programming error.
  213. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  214. /// failed.
  215. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  216. uint32_t iaid) const;
  217. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  218. ///
  219. /// @param type specifies lease type: (NA, TA or PD)
  220. /// @param duid client DUID
  221. /// @param iaid IA identifier
  222. /// @param subnet_id subnet id of the subnet the lease belongs to
  223. ///
  224. /// @return lease collection (may be empty if no lease is found)
  225. ///
  226. /// @throw isc::BadValue record retrieved from database had an invalid
  227. /// lease type field.
  228. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  229. /// fit into the space allocated for the result. This indicates a
  230. /// programming error.
  231. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  232. /// failed.
  233. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  234. uint32_t iaid, SubnetID subnet_id) const;
  235. /// @brief Returns a collection of expired DHCPv6 leases.
  236. ///
  237. /// This method returns at most @c max_leases expired leases. The leases
  238. /// returned haven't been reclaimed, i.e. the database query must exclude
  239. /// reclaimed leases from the results returned.
  240. ///
  241. /// @param [out] expired_leases A container to which expired leases returned
  242. /// by the database backend are added.
  243. /// @param max_leases A maximum number of leases to be returned. If this
  244. /// value is set to 0, all expired (but not reclaimed) leases are returned.
  245. virtual void getExpiredLeases6(Lease6Collection& expired_leases,
  246. const size_t max_leases) const;
  247. /// @brief Returns a collection of expired DHCPv4 leases.
  248. ///
  249. /// This method returns at most @c max_leases expired leases. The leases
  250. /// returned haven't been reclaimed, i.e. the database query must exclude
  251. /// reclaimed leases from the results returned.
  252. ///
  253. /// @param [out] expired_leases A container to which expired leases returned
  254. /// by the database backend are added.
  255. /// @param max_leases A maximum number of leases to be returned. If this
  256. /// value is set to 0, all expired (but not reclaimed) leases are returned.
  257. virtual void getExpiredLeases4(Lease4Collection& expired_leases,
  258. const size_t max_leases) const;
  259. /// @brief Updates IPv4 lease.
  260. ///
  261. /// Updates the record of the lease in the database (as identified by the
  262. /// address) with the data in the passed lease object.
  263. ///
  264. /// @param lease4 The lease to be updated.
  265. ///
  266. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  267. /// exist.
  268. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  269. /// failed.
  270. virtual void updateLease4(const Lease4Ptr& lease4);
  271. /// @brief Updates IPv6 lease.
  272. ///
  273. /// Updates the record of the lease in the database (as identified by the
  274. /// address) with the data in the passed lease object.
  275. ///
  276. /// @param lease6 The lease to be updated.
  277. ///
  278. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  279. /// exist.
  280. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  281. /// failed.
  282. virtual void updateLease6(const Lease6Ptr& lease6);
  283. /// @brief Deletes a lease.
  284. ///
  285. /// @param addr Address of the lease to be deleted. This can be an IPv4
  286. /// address or an IPv6 address.
  287. ///
  288. /// @return true if deletion was successful, false if no such lease exists
  289. ///
  290. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  291. /// failed.
  292. virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
  293. /// @brief Deletes all expired-reclaimed DHCPv4 leases.
  294. ///
  295. /// @param secs Number of seconds since expiration of leases before
  296. /// they can be removed. Leases which have expired later than this
  297. /// time will not be deleted.
  298. ///
  299. /// @return Number of leases deleted.
  300. virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs);
  301. /// @brief Deletes all expired-reclaimed DHCPv6 leases.
  302. ///
  303. /// @param secs Number of seconds since expiration of leases before
  304. /// they can be removed. Leases which have expired later than this
  305. /// time will not be deleted.
  306. ///
  307. /// @return Number of leases deleted.
  308. virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs);
  309. /// @brief Return backend type
  310. ///
  311. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  312. ///
  313. /// @return Type of the backend.
  314. virtual std::string getType() const {
  315. return (std::string("mysql"));
  316. }
  317. /// @brief Returns backend name.
  318. ///
  319. /// Each backend have specific name, e.g. "mysql" or "sqlite".
  320. ///
  321. /// @return Name of the backend.
  322. virtual std::string getName() const;
  323. /// @brief Returns description of the backend.
  324. ///
  325. /// This description may be multiline text that describes the backend.
  326. ///
  327. /// @return Description of the backend.
  328. virtual std::string getDescription() const;
  329. /// @brief Returns backend version.
  330. ///
  331. /// @return Version number as a pair of unsigned integers. "first" is the
  332. /// major version number, "second" the minor number.
  333. ///
  334. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  335. /// failed.
  336. virtual std::pair<uint32_t, uint32_t> getVersion() const;
  337. /// @brief Commit Transactions
  338. ///
  339. /// Commits all pending database operations. On databases that don't
  340. /// support transactions, this is a no-op.
  341. ///
  342. /// @throw DbOperationError If the commit failed.
  343. virtual void commit();
  344. /// @brief Rollback Transactions
  345. ///
  346. /// Rolls back all pending database operations. On databases that don't
  347. /// support transactions, this is a no-op.
  348. ///
  349. /// @throw DbOperationError If the rollback failed.
  350. virtual void rollback();
  351. /// @brief Statement Tags
  352. ///
  353. /// The contents of the enum are indexes into the list of SQL statements
  354. enum StatementIndex {
  355. DELETE_LEASE4, // Delete from lease4 by address
  356. DELETE_LEASE4_STATE_EXPIRED, // Delete expired lease4 in a given state
  357. DELETE_LEASE6, // Delete from lease6 by address
  358. DELETE_LEASE6_STATE_EXPIRED, // Delete expired lease6 in a given state
  359. GET_LEASE4_ADDR, // Get lease4 by address
  360. GET_LEASE4_CLIENTID, // Get lease4 by client ID
  361. GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
  362. GET_LEASE4_HWADDR, // Get lease4 by HW address
  363. GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
  364. GET_LEASE4_EXPIRE, // Get lease4 by expiration.
  365. GET_LEASE6_ADDR, // Get lease6 by address
  366. GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
  367. GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
  368. GET_LEASE6_EXPIRE, // Get lease6 by expiration.
  369. GET_VERSION, // Obtain version number
  370. INSERT_LEASE4, // Add entry to lease4 table
  371. INSERT_LEASE6, // Add entry to lease6 table
  372. UPDATE_LEASE4, // Update a Lease4 entry
  373. UPDATE_LEASE6, // Update a Lease6 entry
  374. RECOUNT_LEASE4_STATS, // Fetches address statisics
  375. NUM_STATEMENTS // Number of statements
  376. };
  377. private:
  378. /// @brief Add Lease Common Code
  379. ///
  380. /// This method performs the common actions for both flavours (V4 and V6)
  381. /// of the addLease method. It binds the contents of the lease object to
  382. /// the prepared statement and adds it to the database.
  383. ///
  384. /// @param stindex Index of statemnent being executed
  385. /// @param bind MYSQL_BIND array that has been created for the type
  386. /// of lease in question.
  387. ///
  388. /// @return true if the lease was added, false if it was not added because
  389. /// a lease with that address already exists in the database.
  390. ///
  391. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  392. /// failed.
  393. bool addLeaseCommon(StatementIndex stindex, std::vector<MYSQL_BIND>& bind);
  394. /// @brief Get Lease Collection Common Code
  395. ///
  396. /// This method performs the common actions for obtaining multiple leases
  397. /// from the database.
  398. ///
  399. /// @param stindex Index of statement being executed
  400. /// @param bind MYSQL_BIND array for input parameters
  401. /// @param exchange Exchange object to use
  402. /// @param lease LeaseCollection object returned. Note that any leases in
  403. /// the collection when this method is called are not erased: the
  404. /// new data is appended to the end.
  405. /// @param single If true, only a single data item is to be retrieved.
  406. /// If more than one is present, a MultipleRecords exception will
  407. /// be thrown.
  408. ///
  409. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  410. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  411. /// failed.
  412. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  413. /// from the database where only one was expected.
  414. template <typename Exchange, typename LeaseCollection>
  415. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  416. Exchange& exchange, LeaseCollection& result,
  417. bool single = false) const;
  418. /// @brief Get Lease Collection
  419. ///
  420. /// Gets a collection of Lease4 objects. This is just an interface to
  421. /// the get lease collection common code.
  422. ///
  423. /// @param stindex Index of statement being executed
  424. /// @param bind MYSQL_BIND array for input parameters
  425. /// @param lease LeaseCollection object returned. Note that any leases in
  426. /// the collection when this method is called are not erased: the
  427. /// new data is appended to the end.
  428. ///
  429. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  430. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  431. /// failed.
  432. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  433. /// from the database where only one was expected.
  434. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  435. Lease4Collection& result) const {
  436. getLeaseCollection(stindex, bind, exchange4_, result);
  437. }
  438. /// @brief Get Lease Collection
  439. ///
  440. /// Gets a collection of Lease6 objects. This is just an interface to
  441. /// the get lease collection common code.
  442. ///
  443. /// @param stindex Index of statement being executed
  444. /// @param bind MYSQL_BIND array for input parameters
  445. /// @param lease LeaseCollection object returned. Note that any existing
  446. /// data in the collection is erased first.
  447. ///
  448. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  449. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  450. /// failed.
  451. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  452. /// from the database where only one was expected.
  453. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  454. Lease6Collection& result) const {
  455. getLeaseCollection(stindex, bind, exchange6_, result);
  456. }
  457. /// @brief Get Lease4 Common Code
  458. ///
  459. /// This method performs the common actions for the various getLease4()
  460. /// methods. It acts as an interface to the getLeaseCollection() method,
  461. /// but retrieveing only a single lease.
  462. ///
  463. /// @param stindex Index of statement being executed
  464. /// @param bind MYSQL_BIND array for input parameters
  465. /// @param lease Lease4 object returned
  466. void getLease(StatementIndex stindex, MYSQL_BIND* bind,
  467. Lease4Ptr& result) const;
  468. /// @brief Get Lease6 Common Code
  469. ///
  470. /// This method performs the common actions for the various getLease46)
  471. /// methods. It acts as an interface to the getLeaseCollection() method,
  472. /// but retrieveing only a single lease.
  473. ///
  474. /// @param stindex Index of statement being executed
  475. /// @param bind MYSQL_BIND array for input parameters
  476. /// @param lease Lease6 object returned
  477. void getLease(StatementIndex stindex, MYSQL_BIND* bind,
  478. Lease6Ptr& result) const;
  479. /// @brief Get expired leases common code.
  480. ///
  481. /// This method retrieves expired and not reclaimed leases from the
  482. /// lease database. The returned leases are ordered by the expiration
  483. /// time. The maximum number of leases to be returned is specified
  484. /// as an argument.
  485. ///
  486. /// @param [out] expired_leases Reference to the container where the
  487. /// retrieved leases are put.
  488. /// @param max_leases Maximum number of leases to be returned.
  489. /// @param statement_index One of the @c GET_LEASE4_EXPIRE or
  490. /// @c GET_LEASE6_EXPIRE.
  491. ///
  492. /// @tparam One of the @c Lease4Collection or @c Lease6Collection.
  493. template<typename LeaseCollection>
  494. void getExpiredLeasesCommon(LeaseCollection& expired_leases,
  495. const size_t max_leases,
  496. StatementIndex statement_index) const;
  497. /// @brief Update lease common code
  498. ///
  499. /// Holds the common code for updating a lease. It binds the parameters
  500. /// to the prepared statement, executes it, then checks how many rows
  501. /// were affected.
  502. ///
  503. /// @param stindex Index of prepared statement to be executed
  504. /// @param bind Array of MYSQL_BIND objects representing the parameters.
  505. /// (Note that the number is determined by the number of parameters
  506. /// in the statement.)
  507. /// @param lease Pointer to the lease object whose record is being updated.
  508. ///
  509. /// @throw NoSuchLease Could not update a lease because no lease matches
  510. /// the address given.
  511. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  512. /// failed.
  513. template <typename LeasePtr>
  514. void updateLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind,
  515. const LeasePtr& lease);
  516. /// @brief Delete lease common code
  517. ///
  518. /// Holds the common code for deleting a lease. It binds the parameters
  519. /// to the prepared statement, executes the statement and checks to
  520. /// see how many rows were deleted.
  521. ///
  522. /// @param stindex Index of prepared statement to be executed
  523. /// @param bind Array of MYSQL_BIND objects representing the parameters.
  524. /// (Note that the number is determined by the number of parameters
  525. /// in the statement.)
  526. ///
  527. /// @return Number of deleted leases.
  528. ///
  529. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  530. /// failed.
  531. uint64_t deleteLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind);
  532. /// @brief Delete expired-reclaimed leases.
  533. ///
  534. /// @param secs Number of seconds since expiration of leases before
  535. /// they can be removed. Leases which have expired later than this
  536. /// time will not be deleted.
  537. /// @param statement_index One of the @c DELETE_LEASE4_STATE_EXPIRED or
  538. /// @c DELETE_LEASE6_STATE_EXPIRED.
  539. ///
  540. /// @return Number of leases deleted.
  541. uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
  542. StatementIndex statement_index);
  543. /// @brief Creates and runs the IPv4 lease stats query
  544. ///
  545. /// It creates an instance of a MySqlAddressStatsQuery4 and then
  546. /// invokes its start method, which fetches its statistical data
  547. /// result set by executing the RECOUNT_LEASE_STATS4 query.
  548. /// The query object is then returned.
  549. ///
  550. /// @return The populated query as a pointer to an AddressStatsQuery4
  551. virtual AddressStatsQuery4Ptr startAddressStatsQuery4();
  552. /// @brief Check Error and Throw Exception
  553. ///
  554. /// This method invokes @ref MySqlConnection::checkError.
  555. ///
  556. /// @param status Status code: non-zero implies an error
  557. /// @param index Index of statement that caused the error
  558. /// @param what High-level description of the error
  559. ///
  560. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  561. /// failed.
  562. void checkError(int status, StatementIndex index,
  563. const char* what) const;
  564. // Members
  565. /// The exchange objects are used for transfer of data to/from the database.
  566. /// They are pointed-to objects as the contents may change in "const" calls,
  567. /// while the rest of this object does not. (At alternative would be to
  568. /// declare them as "mutable".)
  569. boost::scoped_ptr<MySqlLease4Exchange> exchange4_; ///< Exchange object
  570. boost::scoped_ptr<MySqlLease6Exchange> exchange6_; ///< Exchange object
  571. /// @brief MySQL connection
  572. MySqlConnection conn_;
  573. };
  574. }; // end of isc::dhcp namespace
  575. }; // end of isc namespace
  576. #endif // MYSQL_LEASE_MGR_H