mysql_lease_mgr.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // Copyright (C) 2012 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 <boost/scoped_ptr.hpp>
  19. #include <boost/utility.hpp>
  20. #include <mysql.h>
  21. #include <time.h>
  22. namespace isc {
  23. namespace dhcp {
  24. /// @brief MySQL Handle Holder
  25. ///
  26. /// Small RAII object for safer initialization, will close the database
  27. /// connection upon destruction. This means that if an exception is thrown
  28. /// during database initialization, resources allocated to the database are
  29. /// guaranteed to be freed.
  30. ///
  31. /// It makes no sense to copy an object of this class. After the copy, both
  32. /// objects would contain pointers to the same MySql context object. The
  33. /// destruction of one would invalid the context in the remaining object.
  34. /// For this reason, the class is declared noncopyable.
  35. class MySqlHolder : public boost::noncopyable {
  36. public:
  37. /// @brief Constructor
  38. ///
  39. /// Initialize MySql and store the associated context object.
  40. ///
  41. /// @throw DbOpenError Unable to initialize MySql handle.
  42. MySqlHolder() : mysql_(mysql_init(NULL)) {
  43. if (mysql_ == NULL) {
  44. isc_throw(DbOpenError, "unable to initialize MySQL");
  45. }
  46. }
  47. /// @brief Destructor
  48. ///
  49. /// Frees up resources allocated by the initialization of MySql.
  50. ~MySqlHolder() {
  51. if (mysql_ != NULL) {
  52. mysql_close(mysql_);
  53. }
  54. // The library itself shouldn't be needed anymore
  55. mysql_library_end();
  56. }
  57. /// @brief Conversion Operator
  58. ///
  59. /// Allows the MySqlHolder object to be passed as the context argument to
  60. /// mysql_xxx functions.
  61. operator MYSQL*() const {
  62. return (mysql_);
  63. }
  64. private:
  65. MYSQL* mysql_; ///< Initialization context
  66. };
  67. // Define the current database schema values
  68. const uint32_t CURRENT_VERSION_VERSION = 1;
  69. const uint32_t CURRENT_VERSION_MINOR = 0;
  70. // Forward declaration of the Lease exchange objects. These classes are defined
  71. // in the .cc file.
  72. class MySqlLease4Exchange;
  73. class MySqlLease6Exchange;
  74. /// @brief MySQL Lease Manager
  75. ///
  76. /// This class provides the \ref isc::dhcp::LeaseMgr interface to the MySQL
  77. /// database. Use of this backend presupposes that a MySQL database is
  78. /// available and that the Kea schema has been created within it.
  79. class MySqlLeaseMgr : public LeaseMgr {
  80. public:
  81. /// @brief Constructor
  82. ///
  83. /// Uses the following keywords in the parameters passed to it to
  84. /// connect to the database:
  85. /// - name - Name of the database to which to connect (mandatory)
  86. /// - host - Host to which to connect (optional, defaults to "localhost")
  87. /// - user - Username under which to connect (optional)
  88. /// - password - Password for "user" on the database (optional)
  89. ///
  90. /// If the database is successfully opened, the version number in the
  91. /// schema_version table will be checked against hard-coded value in
  92. /// the implementation file.
  93. ///
  94. /// Finally, all the SQL commands are pre-compiled.
  95. ///
  96. /// @param parameters A data structure relating keywords and values
  97. /// concerned with the database.
  98. ///
  99. /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given
  100. /// @throw isc::dhcp::DbOpenError Error opening the database
  101. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  102. /// failed.
  103. MySqlLeaseMgr(const ParameterMap& parameters);
  104. /// @brief Destructor (closes database)
  105. virtual ~MySqlLeaseMgr();
  106. /// @brief Adds an IPv4 lease
  107. ///
  108. /// @param lease lease to be added
  109. ///
  110. /// @result true if the lease was added, false if not (because a lease
  111. /// with the same address was already there).
  112. ///
  113. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  114. /// failed.
  115. virtual bool addLease(const Lease4Ptr& lease);
  116. /// @brief Adds an IPv6 lease
  117. ///
  118. /// @param lease lease to be added
  119. ///
  120. /// @result true if the lease was added, false if not (because a lease
  121. /// with the same address was already there).
  122. ///
  123. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  124. /// failed.
  125. virtual bool addLease(const Lease6Ptr& lease);
  126. /// @brief Returns an IPv4 lease for specified IPv4 address
  127. ///
  128. /// This method return a lease that is associated with a given address.
  129. /// For other query types (by hardware addr, by Client ID) there can be
  130. /// several leases in different subnets (e.g. for mobile clients that
  131. /// got address in different subnets). However, for a single address
  132. /// there can be only one lease, so this method returns a pointer to
  133. /// a single lease, not a container of leases.
  134. ///
  135. /// @param addr address of the searched lease
  136. ///
  137. /// @return smart pointer to the lease (or NULL if a lease is not found)
  138. ///
  139. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  140. /// fit into the space allocated for the result. This indicates a
  141. /// programming error.
  142. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  143. /// failed.
  144. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
  145. /// @brief Returns existing IPv4 leases for specified hardware address.
  146. ///
  147. /// Although in the usual case there will be only one lease, for mobile
  148. /// clients or clients with multiple static/fixed/reserved leases there
  149. /// can be more than one. Thus return type is a container, not a single
  150. /// pointer.
  151. ///
  152. /// @param hwaddr hardware address of the client
  153. ///
  154. /// @return lease collection
  155. ///
  156. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  157. /// fit into the space allocated for the result. This indicates a
  158. /// programming error.
  159. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  160. /// failed.
  161. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
  162. /// @brief Returns existing IPv4 leases for specified hardware address
  163. /// and a subnet
  164. ///
  165. /// There can be at most one lease for a given HW address in a single
  166. /// pool, so this method with either return a single lease or NULL.
  167. ///
  168. /// @param hwaddr hardware address of the client
  169. /// @param subnet_id identifier of the subnet that lease must belong to
  170. ///
  171. /// @return a pointer to the lease (or NULL if a lease is not found)
  172. ///
  173. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  174. /// fit into the space allocated for the result. This indicates a
  175. /// programming error.
  176. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  177. /// failed.
  178. virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
  179. SubnetID subnet_id) const;
  180. /// @brief Returns existing IPv4 lease for specified client-id
  181. ///
  182. /// Although in the usual case there will be only one lease, for mobile
  183. /// clients or clients with multiple static/fixed/reserved leases there
  184. /// can be more than one. Thus return type is a container, not a single
  185. /// pointer.
  186. ///
  187. /// @param clientid client identifier
  188. ///
  189. /// @return lease collection
  190. ///
  191. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  192. /// fit into the space allocated for the result. This indicates a
  193. /// programming error.
  194. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  195. /// failed.
  196. virtual Lease4Collection getLease4(const ClientId& clientid) const;
  197. /// @brief Returns existing IPv4 lease for specified client-id
  198. ///
  199. /// There can be at most one lease for a given HW address in a single
  200. /// pool, so this method with either return a single lease or NULL.
  201. ///
  202. /// @param clientid client identifier
  203. /// @param subnet_id identifier of the subnet that lease must belong to
  204. ///
  205. /// @return a pointer to the lease (or NULL if a lease is not found)
  206. ///
  207. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  208. /// fit into the space allocated for the result. This indicates a
  209. /// programming error.
  210. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  211. /// failed.
  212. virtual Lease4Ptr getLease4(const ClientId& clientid,
  213. SubnetID subnet_id) const;
  214. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  215. ///
  216. /// For a given address, we assume that there will be only one lease.
  217. /// The assumption here is that there will not be site or link-local
  218. /// addresses used, so there is no way of having address duplication.
  219. ///
  220. /// @param type specifies lease type: (NA, TA or PD)
  221. /// @param addr address of the searched lease
  222. ///
  223. /// @return smart pointer to the lease (or NULL if a lease is not found)
  224. ///
  225. /// @throw isc::BadValue record retrieved from database had an invalid
  226. /// lease type field.
  227. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  228. /// fit into the space allocated for the result. This indicates a
  229. /// programming error.
  230. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  231. /// failed.
  232. virtual Lease6Ptr getLease6(Lease6::LeaseType type,
  233. const isc::asiolink::IOAddress& addr) const;
  234. /// @brief Returns existing IPv6 leases for a given DUID+IA combination
  235. ///
  236. /// Although in the usual case there will be only one lease, for mobile
  237. /// clients or clients with multiple static/fixed/reserved leases there
  238. /// can be more than one. Thus return type is a container, not a single
  239. /// pointer.
  240. ///
  241. /// @param type specifies lease type: (NA, TA or PD)
  242. /// @param duid client DUID
  243. /// @param iaid IA identifier
  244. ///
  245. /// @return smart pointer to the lease (or NULL if a lease is not found)
  246. ///
  247. /// @throw isc::BadValue record retrieved from database had an invalid
  248. /// lease type field.
  249. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  250. /// fit into the space allocated for the result. This indicates a
  251. /// programming error.
  252. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  253. /// failed.
  254. virtual Lease6Collection getLease6(Lease6::LeaseType type, const DUID& duid,
  255. uint32_t iaid) const;
  256. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  257. ///
  258. /// @param type specifies lease type: (NA, TA or PD)
  259. /// @param duid client DUID
  260. /// @param iaid IA identifier
  261. /// @param subnet_id subnet id of the subnet the lease belongs to
  262. ///
  263. /// @return lease collection (may be empty if no lease is found)
  264. ///
  265. /// @throw isc::BadValue record retrieved from database had an invalid
  266. /// lease type field.
  267. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  268. /// fit into the space allocated for the result. This indicates a
  269. /// programming error.
  270. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  271. /// failed.
  272. virtual Lease6Collection getLeases6(Lease6::LeaseType type, const DUID& duid,
  273. uint32_t iaid, SubnetID subnet_id) const;
  274. /// @brief Updates IPv4 lease.
  275. ///
  276. /// Updates the record of the lease in the database (as identified by the
  277. /// address) with the data in the passed lease object.
  278. ///
  279. /// @param lease4 The lease to be updated.
  280. ///
  281. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  282. /// exist.
  283. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  284. /// failed.
  285. virtual void updateLease4(const Lease4Ptr& lease4);
  286. /// @brief Updates IPv6 lease.
  287. ///
  288. /// Updates the record of the lease in the database (as identified by the
  289. /// address) with the data in the passed lease object.
  290. ///
  291. /// @param lease6 The lease to be updated.
  292. ///
  293. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  294. /// exist.
  295. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  296. /// failed.
  297. virtual void updateLease6(const Lease6Ptr& lease6);
  298. /// @brief Deletes a lease.
  299. ///
  300. /// @param addr Address of the lease to be deleted. This can be an IPv4
  301. /// address or an IPv6 address.
  302. ///
  303. /// @return true if deletion was successful, false if no such lease exists
  304. ///
  305. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  306. /// failed.
  307. virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
  308. /// @brief Return backend type
  309. ///
  310. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  311. ///
  312. /// @return Type of the backend.
  313. virtual std::string getType() const {
  314. return (std::string("mysql"));
  315. }
  316. /// @brief Returns backend name.
  317. ///
  318. /// Each backend have specific name, e.g. "mysql" or "sqlite".
  319. ///
  320. /// @return Name of the backend.
  321. virtual std::string getName() const;
  322. /// @brief Returns description of the backend.
  323. ///
  324. /// This description may be multiline text that describes the backend.
  325. ///
  326. /// @return Description of the backend.
  327. virtual std::string getDescription() const;
  328. /// @brief Returns backend version.
  329. ///
  330. /// @return Version number as a pair of unsigned integers. "first" is the
  331. /// major version number, "second" the minor number.
  332. ///
  333. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  334. /// failed.
  335. virtual std::pair<uint32_t, uint32_t> getVersion() const;
  336. /// @brief Commit Transactions
  337. ///
  338. /// Commits all pending database operations. On databases that don't
  339. /// support transactions, this is a no-op.
  340. ///
  341. /// @throw DbOperationError Iif the commit failed.
  342. virtual void commit();
  343. /// @brief Rollback Transactions
  344. ///
  345. /// Rolls back all pending database operations. On databases that don't
  346. /// support transactions, this is a no-op.
  347. ///
  348. /// @throw DbOperationError If the rollback failed.
  349. virtual void rollback();
  350. ///@{
  351. /// The following methods are used to convert between times and time
  352. /// intervals stored in the Lease object, and the times stored in the
  353. /// database. The reason for the difference is because in the DHCP server,
  354. /// the cltt (Client Time Since Last Transmission) is the natural data; in
  355. /// the lease file - which may be read by the user - it is the expiry time
  356. /// of the lease.
  357. /// @brief Convert Lease Time to Database Times
  358. ///
  359. /// Within the DHCP servers, times are stored as client last transmit time
  360. /// and valid lifetime. In the database, the information is stored as
  361. /// valid lifetime and "expire" (time of expiry of the lease). They are
  362. /// related by the equation:
  363. ///
  364. /// - expire = client last transmit time + valid lifetime
  365. ///
  366. /// This method converts from the times in the lease object into times
  367. /// able to be added to the database.
  368. ///
  369. /// @param cltt Client last transmit time
  370. /// @param valid_lifetime Valid lifetime
  371. /// @param expire Reference to MYSQL_TIME object where the expiry time of
  372. /// the lease will be put.
  373. static
  374. void convertToDatabaseTime(time_t cltt, uint32_t valid_lifetime,
  375. MYSQL_TIME& expire);
  376. /// @brief Convert Database Time to Lease Times
  377. ///
  378. /// Within the database, time is stored as "expire" (time of expiry of the
  379. /// lease) and valid lifetime. In the DHCP server, the information is
  380. /// stored client last transmit time and valid lifetime. These are related
  381. /// by the equation:
  382. ///
  383. /// - client last transmit time = expire - valid_lifetime
  384. ///
  385. /// This method converts from the times in the database into times
  386. /// able to be inserted into the lease object.
  387. ///
  388. /// @param expire Reference to MYSQL_TIME object from where the expiry
  389. /// time of the lease is taken.
  390. /// @param valid_lifetime lifetime of the lease.
  391. /// @param cltt Reference to location where client last transmit time
  392. /// is put.
  393. static
  394. void convertFromDatabaseTime(const MYSQL_TIME& expire,
  395. uint32_t valid_lifetime, time_t& cltt);
  396. ///@}
  397. /// @brief Statement Tags
  398. ///
  399. /// The contents of the enum are indexes into the list of SQL statements
  400. enum StatementIndex {
  401. DELETE_LEASE4, // Delete from lease4 by address
  402. DELETE_LEASE6, // Delete from lease6 by address
  403. GET_LEASE4_ADDR, // Get lease4 by address
  404. GET_LEASE4_CLIENTID, // Get lease4 by client ID
  405. GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
  406. GET_LEASE4_HWADDR, // Get lease4 by HW address
  407. GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
  408. GET_LEASE6_ADDR, // Get lease6 by address
  409. GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
  410. GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
  411. GET_VERSION, // Obtain version number
  412. INSERT_LEASE4, // Add entry to lease4 table
  413. INSERT_LEASE6, // Add entry to lease6 table
  414. UPDATE_LEASE4, // Update a Lease4 entry
  415. UPDATE_LEASE6, // Update a Lease6 entry
  416. NUM_STATEMENTS // Number of statements
  417. };
  418. private:
  419. /// @brief Prepare Single Statement
  420. ///
  421. /// Creates a prepared statement from the text given and adds it to the
  422. /// statements_ vector at the given index.
  423. ///
  424. /// @param index Index into the statements_ vector into which the text
  425. /// should be placed. The vector must be big enough for the index
  426. /// to be valid, else an exception will be thrown.
  427. /// @param text Text of the SQL statement to be prepared.
  428. ///
  429. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  430. /// failed.
  431. /// @throw isc::InvalidParameter 'index' is not valid for the vector.
  432. void prepareStatement(StatementIndex index, const char* text);
  433. /// @brief Prepare statements
  434. ///
  435. /// Creates the prepared statements for all of the SQL statements used
  436. /// by the MySQL backend.
  437. ///
  438. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  439. /// failed.
  440. /// @throw isc::InvalidParameter 'index' is not valid for the vector. This
  441. /// represents an internal error within the code.
  442. void prepareStatements();
  443. /// @brief Open Database
  444. ///
  445. /// Opens the database using the information supplied in the parameters
  446. /// passed to the constructor.
  447. ///
  448. /// @throw NoDatabaseName Mandatory database name not given
  449. /// @throw DbOpenError Error opening the database
  450. void openDatabase();
  451. /// @brief Add Lease Common Code
  452. ///
  453. /// This method performs the common actions for both flavours (V4 and V6)
  454. /// of the addLease method. It binds the contents of the lease object to
  455. /// the prepared statement and adds it to the database.
  456. ///
  457. /// @param stindex Index of statemnent being executed
  458. /// @param bind MYSQL_BIND array that has been created for the type
  459. /// of lease in question.
  460. ///
  461. /// @return true if the lease was added, false if it was not added because
  462. /// a lease with that address already exists in the database.
  463. ///
  464. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  465. /// failed.
  466. bool addLeaseCommon(StatementIndex stindex, std::vector<MYSQL_BIND>& bind);
  467. /// @brief Get Lease Collection Common Code
  468. ///
  469. /// This method performs the common actions for obtaining multiple leases
  470. /// from the database.
  471. ///
  472. /// @param stindex Index of statement being executed
  473. /// @param bind MYSQL_BIND array for input parameters
  474. /// @param exchange Exchange object to use
  475. /// @param lease LeaseCollection object returned. Note that any leases in
  476. /// the collection when this method is called are not erased: the
  477. /// new data is appended to the end.
  478. /// @param single If true, only a single data item is to be retrieved.
  479. /// If more than one is present, a MultipleRecords exception will
  480. /// be thrown.
  481. ///
  482. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  483. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  484. /// failed.
  485. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  486. /// from the database where only one was expected.
  487. template <typename Exchange, typename LeaseCollection>
  488. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  489. Exchange& exchange, LeaseCollection& result,
  490. bool single = false) const;
  491. /// @brief Get Lease Collection
  492. ///
  493. /// Gets a collection of Lease4 objects. This is just an interface to
  494. /// the get lease collection common code.
  495. ///
  496. /// @param stindex Index of statement being executed
  497. /// @param bind MYSQL_BIND array for input parameters
  498. /// @param lease LeaseCollection object returned. Note that any leases in
  499. /// the collection when this method is called are not erased: the
  500. /// new data is appended to the end.
  501. ///
  502. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  503. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  504. /// failed.
  505. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  506. /// from the database where only one was expected.
  507. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  508. Lease4Collection& result) const {
  509. getLeaseCollection(stindex, bind, exchange4_, result);
  510. }
  511. /// @brief Get Lease Collection
  512. ///
  513. /// Gets a collection of Lease6 objects. This is just an interface to
  514. /// the get lease collection common code.
  515. ///
  516. /// @param stindex Index of statement being executed
  517. /// @param bind MYSQL_BIND array for input parameters
  518. /// @param lease LeaseCollection object returned. Note that any existing
  519. /// data in the collection is erased first.
  520. ///
  521. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  522. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  523. /// failed.
  524. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  525. /// from the database where only one was expected.
  526. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  527. Lease6Collection& result) const {
  528. getLeaseCollection(stindex, bind, exchange6_, result);
  529. }
  530. /// @brief Get Lease4 Common Code
  531. ///
  532. /// This method performs the common actions for the various getLease4()
  533. /// methods. It acts as an interface to the getLeaseCollection() method,
  534. /// but retrieveing only a single lease.
  535. ///
  536. /// @param stindex Index of statement being executed
  537. /// @param bind MYSQL_BIND array for input parameters
  538. /// @param lease Lease4 object returned
  539. void getLease(StatementIndex stindex, MYSQL_BIND* bind,
  540. Lease4Ptr& result) const;
  541. /// @brief Get Lease6 Common Code
  542. ///
  543. /// This method performs the common actions for the various getLease46)
  544. /// methods. It acts as an interface to the getLeaseCollection() method,
  545. /// but retrieveing only a single lease.
  546. ///
  547. /// @param stindex Index of statement being executed
  548. /// @param bind MYSQL_BIND array for input parameters
  549. /// @param lease Lease6 object returned
  550. void getLease(StatementIndex stindex, MYSQL_BIND* bind,
  551. Lease6Ptr& result) const;
  552. /// @brief Update lease common code
  553. ///
  554. /// Holds the common code for updating a lease. It binds the parameters
  555. /// to the prepared statement, executes it, then checks how many rows
  556. /// were affected.
  557. ///
  558. /// @param stindex Index of prepared statement to be executed
  559. /// @param bind Array of MYSQL_BIND objects representing the parameters.
  560. /// (Note that the number is determined by the number of parameters
  561. /// in the statement.)
  562. /// @param lease Pointer to the lease object whose record is being updated.
  563. ///
  564. /// @throw NoSuchLease Could not update a lease because no lease matches
  565. /// the address given.
  566. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  567. /// failed.
  568. template <typename LeasePtr>
  569. void updateLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind,
  570. const LeasePtr& lease);
  571. /// @brief Delete lease common code
  572. ///
  573. /// Holds the common code for deleting a lease. It binds the parameters
  574. /// to the prepared statement, executes the statement and checks to
  575. /// see how many rows were deleted.
  576. ///
  577. /// @param stindex Index of prepared statement to be executed
  578. /// @param bind Array of MYSQL_BIND objects representing the parameters.
  579. /// (Note that the number is determined by the number of parameters
  580. /// in the statement.)
  581. ///
  582. /// @return true if one or more rows were deleted, false if none were
  583. /// deleted.
  584. ///
  585. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  586. /// failed.
  587. bool deleteLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind);
  588. /// @brief Check Error and Throw Exception
  589. ///
  590. /// Virtually all MySQL functions return a status which, if non-zero,
  591. /// indicates an error. This inline function conceals a lot of error
  592. /// checking/exception-throwing code.
  593. ///
  594. /// @param status Status code: non-zero implies an error
  595. /// @param index Index of statement that caused the error
  596. /// @param what High-level description of the error
  597. ///
  598. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  599. /// failed.
  600. inline void checkError(int status, StatementIndex index,
  601. const char* what) const {
  602. if (status != 0) {
  603. isc_throw(DbOperationError, what << " for <" <<
  604. text_statements_[index] << ">, reason: " <<
  605. mysql_error(mysql_) << " (error code " <<
  606. mysql_errno(mysql_) << ")");
  607. }
  608. }
  609. // Members
  610. /// The exchange objects are used for transfer of data to/from the database.
  611. /// They are pointed-to objects as the contents may change in "const" calls,
  612. /// while the rest of this object does not. (At alternative would be to
  613. /// declare them as "mutable".)
  614. boost::scoped_ptr<MySqlLease4Exchange> exchange4_; ///< Exchange object
  615. boost::scoped_ptr<MySqlLease6Exchange> exchange6_; ///< Exchange object
  616. MySqlHolder mysql_;
  617. std::vector<MYSQL_STMT*> statements_; ///< Prepared statements
  618. std::vector<std::string> text_statements_; ///< Raw text of statements
  619. };
  620. }; // end of isc::dhcp namespace
  621. }; // end of isc namespace
  622. #endif // MYSQL_LEASE_MGR_H