pgsql_lease_mgr.h 30 KB

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