mysql_lease_mgr.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. // Copyright (C) 2012-2014 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 = 2;
  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 IPv4 lease for the specified client identifier, HW
  198. /// address and subnet identifier.
  199. ///
  200. /// @param client_id A client identifier.
  201. /// @param hwaddr Hardware address.
  202. /// @param subnet_id A subnet identifier.
  203. ///
  204. /// @return A pointer to the lease or NULL if the lease is not found.
  205. /// @throw isc::NotImplemented On every call as this function is currently
  206. /// not implemented for the MySQL backend.
  207. virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr,
  208. SubnetID subnet_id) const;
  209. /// @brief Returns existing IPv4 lease for specified client-id
  210. ///
  211. /// There can be at most one lease for a given HW address in a single
  212. /// pool, so this method with either return a single lease or NULL.
  213. ///
  214. /// @param clientid client identifier
  215. /// @param subnet_id identifier of the subnet that lease must belong to
  216. ///
  217. /// @return a pointer to the lease (or NULL if a lease is not found)
  218. ///
  219. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  220. /// fit into the space allocated for the result. This indicates a
  221. /// programming error.
  222. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  223. /// failed.
  224. virtual Lease4Ptr getLease4(const ClientId& clientid,
  225. SubnetID subnet_id) const;
  226. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  227. ///
  228. /// For a given address, we assume that there will be only one lease.
  229. /// The assumption here is that there will not be site or link-local
  230. /// addresses used, so there is no way of having address duplication.
  231. ///
  232. /// @param type specifies lease type: (NA, TA or PD)
  233. /// @param addr address of the searched lease
  234. ///
  235. /// @return smart pointer to the lease (or NULL if a lease is not found)
  236. ///
  237. /// @throw isc::BadValue record retrieved from database had an invalid
  238. /// lease type field.
  239. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  240. /// fit into the space allocated for the result. This indicates a
  241. /// programming error.
  242. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  243. /// failed.
  244. virtual Lease6Ptr getLease6(Lease::Type type,
  245. const isc::asiolink::IOAddress& addr) const;
  246. /// @brief Returns existing IPv6 leases for a given DUID+IA combination
  247. ///
  248. /// Although in the usual case there will be only one lease, for mobile
  249. /// clients or clients with multiple static/fixed/reserved leases there
  250. /// can be more than one. Thus return type is a container, not a single
  251. /// pointer.
  252. ///
  253. /// @param type specifies lease type: (NA, TA or PD)
  254. /// @param duid client DUID
  255. /// @param iaid IA identifier
  256. ///
  257. /// @return smart pointer to the lease (or NULL if a lease is not found)
  258. ///
  259. /// @throw isc::BadValue record retrieved from database had an invalid
  260. /// lease type field.
  261. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  262. /// fit into the space allocated for the result. This indicates a
  263. /// programming error.
  264. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  265. /// failed.
  266. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  267. uint32_t iaid) const;
  268. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  269. ///
  270. /// @param type specifies lease type: (NA, TA or PD)
  271. /// @param duid client DUID
  272. /// @param iaid IA identifier
  273. /// @param subnet_id subnet id of the subnet the lease belongs to
  274. ///
  275. /// @return lease collection (may be empty if no lease is found)
  276. ///
  277. /// @throw isc::BadValue record retrieved from database had an invalid
  278. /// lease type field.
  279. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  280. /// fit into the space allocated for the result. This indicates a
  281. /// programming error.
  282. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  283. /// failed.
  284. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  285. uint32_t iaid, SubnetID subnet_id) const;
  286. /// @brief Updates IPv4 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 lease4 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 updateLease4(const Lease4Ptr& lease4);
  298. /// @brief Updates IPv6 lease.
  299. ///
  300. /// Updates the record of the lease in the database (as identified by the
  301. /// address) with the data in the passed lease object.
  302. ///
  303. /// @param lease6 The lease to be updated.
  304. ///
  305. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  306. /// exist.
  307. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  308. /// failed.
  309. virtual void updateLease6(const Lease6Ptr& lease6);
  310. /// @brief Deletes a lease.
  311. ///
  312. /// @param addr Address of the lease to be deleted. This can be an IPv4
  313. /// address or an IPv6 address.
  314. ///
  315. /// @return true if deletion was successful, false if no such lease exists
  316. ///
  317. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  318. /// failed.
  319. virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
  320. /// @brief Return backend type
  321. ///
  322. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  323. ///
  324. /// @return Type of the backend.
  325. virtual std::string getType() const {
  326. return (std::string("mysql"));
  327. }
  328. /// @brief Returns backend name.
  329. ///
  330. /// Each backend have specific name, e.g. "mysql" or "sqlite".
  331. ///
  332. /// @return Name of the backend.
  333. virtual std::string getName() const;
  334. /// @brief Returns description of the backend.
  335. ///
  336. /// This description may be multiline text that describes the backend.
  337. ///
  338. /// @return Description of the backend.
  339. virtual std::string getDescription() const;
  340. /// @brief Returns backend version.
  341. ///
  342. /// @return Version number as a pair of unsigned integers. "first" is the
  343. /// major version number, "second" the minor number.
  344. ///
  345. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  346. /// failed.
  347. virtual std::pair<uint32_t, uint32_t> getVersion() const;
  348. /// @brief Commit Transactions
  349. ///
  350. /// Commits all pending database operations. On databases that don't
  351. /// support transactions, this is a no-op.
  352. ///
  353. /// @throw DbOperationError Iif the commit failed.
  354. virtual void commit();
  355. /// @brief Rollback Transactions
  356. ///
  357. /// Rolls back all pending database operations. On databases that don't
  358. /// support transactions, this is a no-op.
  359. ///
  360. /// @throw DbOperationError If the rollback failed.
  361. virtual void rollback();
  362. ///@{
  363. /// The following methods are used to convert between times and time
  364. /// intervals stored in the Lease object, and the times stored in the
  365. /// database. The reason for the difference is because in the DHCP server,
  366. /// the cltt (Client Time Since Last Transmission) is the natural data; in
  367. /// the lease file - which may be read by the user - it is the expiry time
  368. /// of the lease.
  369. /// @brief Convert Lease Time to Database Times
  370. ///
  371. /// Within the DHCP servers, times are stored as client last transmit time
  372. /// and valid lifetime. In the database, the information is stored as
  373. /// valid lifetime and "expire" (time of expiry of the lease). They are
  374. /// related by the equation:
  375. ///
  376. /// - expire = client last transmit time + valid lifetime
  377. ///
  378. /// This method converts from the times in the lease object into times
  379. /// able to be added to the database.
  380. ///
  381. /// @param cltt Client last transmit time
  382. /// @param valid_lifetime Valid lifetime
  383. /// @param expire Reference to MYSQL_TIME object where the expiry time of
  384. /// the lease will be put.
  385. static
  386. void convertToDatabaseTime(time_t cltt, uint32_t valid_lifetime,
  387. MYSQL_TIME& expire);
  388. /// @brief Convert Database Time to Lease Times
  389. ///
  390. /// Within the database, time is stored as "expire" (time of expiry of the
  391. /// lease) and valid lifetime. In the DHCP server, the information is
  392. /// stored client last transmit time and valid lifetime. These are related
  393. /// by the equation:
  394. ///
  395. /// - client last transmit time = expire - valid_lifetime
  396. ///
  397. /// This method converts from the times in the database into times
  398. /// able to be inserted into the lease object.
  399. ///
  400. /// @param expire Reference to MYSQL_TIME object from where the expiry
  401. /// time of the lease is taken.
  402. /// @param valid_lifetime lifetime of the lease.
  403. /// @param cltt Reference to location where client last transmit time
  404. /// is put.
  405. static
  406. void convertFromDatabaseTime(const MYSQL_TIME& expire,
  407. uint32_t valid_lifetime, time_t& cltt);
  408. ///@}
  409. /// @brief Statement Tags
  410. ///
  411. /// The contents of the enum are indexes into the list of SQL statements
  412. enum StatementIndex {
  413. DELETE_LEASE4, // Delete from lease4 by address
  414. DELETE_LEASE6, // Delete from lease6 by address
  415. GET_LEASE4_ADDR, // Get lease4 by address
  416. GET_LEASE4_CLIENTID, // Get lease4 by client ID
  417. GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
  418. GET_LEASE4_HWADDR, // Get lease4 by HW address
  419. GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
  420. GET_LEASE6_ADDR, // Get lease6 by address
  421. GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
  422. GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
  423. GET_VERSION, // Obtain version number
  424. INSERT_LEASE4, // Add entry to lease4 table
  425. INSERT_LEASE6, // Add entry to lease6 table
  426. UPDATE_LEASE4, // Update a Lease4 entry
  427. UPDATE_LEASE6, // Update a Lease6 entry
  428. NUM_STATEMENTS // Number of statements
  429. };
  430. private:
  431. /// @brief Prepare Single Statement
  432. ///
  433. /// Creates a prepared statement from the text given and adds it to the
  434. /// statements_ vector at the given index.
  435. ///
  436. /// @param index Index into the statements_ vector into which the text
  437. /// should be placed. The vector must be big enough for the index
  438. /// to be valid, else an exception will be thrown.
  439. /// @param text Text of the SQL statement to be prepared.
  440. ///
  441. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  442. /// failed.
  443. /// @throw isc::InvalidParameter 'index' is not valid for the vector.
  444. void prepareStatement(StatementIndex index, const char* text);
  445. /// @brief Prepare statements
  446. ///
  447. /// Creates the prepared statements for all of the SQL statements used
  448. /// by the MySQL backend.
  449. ///
  450. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  451. /// failed.
  452. /// @throw isc::InvalidParameter 'index' is not valid for the vector. This
  453. /// represents an internal error within the code.
  454. void prepareStatements();
  455. /// @brief Open Database
  456. ///
  457. /// Opens the database using the information supplied in the parameters
  458. /// passed to the constructor.
  459. ///
  460. /// @throw NoDatabaseName Mandatory database name not given
  461. /// @throw DbOpenError Error opening the database
  462. void openDatabase();
  463. /// @brief Add Lease Common Code
  464. ///
  465. /// This method performs the common actions for both flavours (V4 and V6)
  466. /// of the addLease method. It binds the contents of the lease object to
  467. /// the prepared statement and adds it to the database.
  468. ///
  469. /// @param stindex Index of statemnent being executed
  470. /// @param bind MYSQL_BIND array that has been created for the type
  471. /// of lease in question.
  472. ///
  473. /// @return true if the lease was added, false if it was not added because
  474. /// a lease with that address already exists in the database.
  475. ///
  476. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  477. /// failed.
  478. bool addLeaseCommon(StatementIndex stindex, std::vector<MYSQL_BIND>& bind);
  479. /// @brief Get Lease Collection Common Code
  480. ///
  481. /// This method performs the common actions for obtaining multiple leases
  482. /// from the database.
  483. ///
  484. /// @param stindex Index of statement being executed
  485. /// @param bind MYSQL_BIND array for input parameters
  486. /// @param exchange Exchange object to use
  487. /// @param lease LeaseCollection object returned. Note that any leases in
  488. /// the collection when this method is called are not erased: the
  489. /// new data is appended to the end.
  490. /// @param single If true, only a single data item is to be retrieved.
  491. /// If more than one is present, a MultipleRecords exception will
  492. /// be thrown.
  493. ///
  494. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  495. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  496. /// failed.
  497. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  498. /// from the database where only one was expected.
  499. template <typename Exchange, typename LeaseCollection>
  500. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  501. Exchange& exchange, LeaseCollection& result,
  502. bool single = false) const;
  503. /// @brief Get Lease Collection
  504. ///
  505. /// Gets a collection of Lease4 objects. This is just an interface to
  506. /// the get lease collection common code.
  507. ///
  508. /// @param stindex Index of statement being executed
  509. /// @param bind MYSQL_BIND array for input parameters
  510. /// @param lease LeaseCollection object returned. Note that any leases in
  511. /// the collection when this method is called are not erased: the
  512. /// new data is appended to the end.
  513. ///
  514. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  515. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  516. /// failed.
  517. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  518. /// from the database where only one was expected.
  519. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  520. Lease4Collection& result) const {
  521. getLeaseCollection(stindex, bind, exchange4_, result);
  522. }
  523. /// @brief Get Lease Collection
  524. ///
  525. /// Gets a collection of Lease6 objects. This is just an interface to
  526. /// the get lease collection common code.
  527. ///
  528. /// @param stindex Index of statement being executed
  529. /// @param bind MYSQL_BIND array for input parameters
  530. /// @param lease LeaseCollection object returned. Note that any existing
  531. /// data in the collection is erased first.
  532. ///
  533. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  534. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  535. /// failed.
  536. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  537. /// from the database where only one was expected.
  538. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  539. Lease6Collection& result) const {
  540. getLeaseCollection(stindex, bind, exchange6_, result);
  541. }
  542. /// @brief Get Lease4 Common Code
  543. ///
  544. /// This method performs the common actions for the various getLease4()
  545. /// methods. It acts as an interface to the getLeaseCollection() method,
  546. /// but retrieveing only a single lease.
  547. ///
  548. /// @param stindex Index of statement being executed
  549. /// @param bind MYSQL_BIND array for input parameters
  550. /// @param lease Lease4 object returned
  551. void getLease(StatementIndex stindex, MYSQL_BIND* bind,
  552. Lease4Ptr& result) const;
  553. /// @brief Get Lease6 Common Code
  554. ///
  555. /// This method performs the common actions for the various getLease46)
  556. /// methods. It acts as an interface to the getLeaseCollection() method,
  557. /// but retrieveing only a single lease.
  558. ///
  559. /// @param stindex Index of statement being executed
  560. /// @param bind MYSQL_BIND array for input parameters
  561. /// @param lease Lease6 object returned
  562. void getLease(StatementIndex stindex, MYSQL_BIND* bind,
  563. Lease6Ptr& result) const;
  564. /// @brief Update lease common code
  565. ///
  566. /// Holds the common code for updating a lease. It binds the parameters
  567. /// to the prepared statement, executes it, then checks how many rows
  568. /// were affected.
  569. ///
  570. /// @param stindex Index of prepared statement to be executed
  571. /// @param bind Array of MYSQL_BIND objects representing the parameters.
  572. /// (Note that the number is determined by the number of parameters
  573. /// in the statement.)
  574. /// @param lease Pointer to the lease object whose record is being updated.
  575. ///
  576. /// @throw NoSuchLease Could not update a lease because no lease matches
  577. /// the address given.
  578. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  579. /// failed.
  580. template <typename LeasePtr>
  581. void updateLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind,
  582. const LeasePtr& lease);
  583. /// @brief Delete lease common code
  584. ///
  585. /// Holds the common code for deleting a lease. It binds the parameters
  586. /// to the prepared statement, executes the statement and checks to
  587. /// see how many rows were deleted.
  588. ///
  589. /// @param stindex Index of prepared statement to be executed
  590. /// @param bind Array of MYSQL_BIND objects representing the parameters.
  591. /// (Note that the number is determined by the number of parameters
  592. /// in the statement.)
  593. ///
  594. /// @return true if one or more rows were deleted, false if none were
  595. /// deleted.
  596. ///
  597. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  598. /// failed.
  599. bool deleteLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind);
  600. /// @brief Check Error and Throw Exception
  601. ///
  602. /// Virtually all MySQL functions return a status which, if non-zero,
  603. /// indicates an error. This inline function conceals a lot of error
  604. /// checking/exception-throwing code.
  605. ///
  606. /// @param status Status code: non-zero implies an error
  607. /// @param index Index of statement that caused the error
  608. /// @param what High-level description of the error
  609. ///
  610. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  611. /// failed.
  612. inline void checkError(int status, StatementIndex index,
  613. const char* what) const {
  614. if (status != 0) {
  615. isc_throw(DbOperationError, what << " for <" <<
  616. text_statements_[index] << ">, reason: " <<
  617. mysql_error(mysql_) << " (error code " <<
  618. mysql_errno(mysql_) << ")");
  619. }
  620. }
  621. // Members
  622. /// The exchange objects are used for transfer of data to/from the database.
  623. /// They are pointed-to objects as the contents may change in "const" calls,
  624. /// while the rest of this object does not. (At alternative would be to
  625. /// declare them as "mutable".)
  626. boost::scoped_ptr<MySqlLease4Exchange> exchange4_; ///< Exchange object
  627. boost::scoped_ptr<MySqlLease6Exchange> exchange6_; ///< Exchange object
  628. MySqlHolder mysql_;
  629. std::vector<MYSQL_STMT*> statements_; ///< Prepared statements
  630. std::vector<std::string> text_statements_; ///< Raw text of statements
  631. };
  632. }; // end of isc::dhcp namespace
  633. }; // end of isc namespace
  634. #endif // MYSQL_LEASE_MGR_H