mysql_lease_mgr.h 28 KB

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