mysql_lease_mgr.h 28 KB

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