pgsql_lease_mgr.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // Copyright (C) 2013-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 PGSQL_LEASE_MGR_H
  15. #define PGSQL_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 <libpq-fe.h>
  21. #include <vector>
  22. namespace isc {
  23. namespace dhcp {
  24. /// @brief An auxiliary structure for marshalling data for compiled statements
  25. ///
  26. /// It represents a single field used in a query (e.g. one field used in WHERE
  27. /// or UPDATE clauses).
  28. struct PgSqlParam {
  29. std::string value; ///< The actual value represented as text
  30. bool isbinary; ///< Boolean flag that indicates if data is binary
  31. int binarylen; ///< Specified binary length
  32. /// @brief Constructor for text parameters
  33. ///
  34. /// Constructs a text (i.e. non-binary) instance given a string value.
  35. /// @param val string containing the text value of the parameter. The
  36. /// default is an empty string which serves as the default or empty
  37. /// parameter constructor.
  38. PgSqlParam (const std::string& val = "")
  39. : value(val), isbinary(false), binarylen(0) {
  40. }
  41. /// @brief Constructor for binary data parameters
  42. ///
  43. /// Constructs a binary data instance given a vector of binary data.
  44. /// @param data vector of binary data from which to set the parameter's
  45. /// value.
  46. PgSqlParam (const std::vector<uint8_t>& data)
  47. : value(data.begin(), data.end()), isbinary(true),
  48. binarylen(data.size()) {
  49. }
  50. };
  51. /// @brief Defines all parameters for binding a compiled statement
  52. typedef std::vector<PgSqlParam> BindParams;
  53. /// @brief Describes a single compiled statement
  54. struct PgSqlStatementBind {
  55. const char* stmt_name; ///< Name of the compiled statement
  56. int stmt_nbparams; ///< Number of statement parameters
  57. };
  58. // Forward definitions (needed for shared_ptr definitions)
  59. // See pgsql_lease_mgr.cc file for actual class definitions
  60. class PgSqlLease4Exchange;
  61. class PgSqlLease6Exchange;
  62. /// Defines PostgreSQL backend version: 1.0
  63. const uint32_t PG_CURRENT_VERSION = 1;
  64. const uint32_t PG_CURRENT_MINOR = 0;
  65. /// @brief PostgreSQL Lease Manager
  66. ///
  67. /// This class provides the \ref isc::dhcp::LeaseMgr interface to the PostgreSQL
  68. /// database. Use of this backend presupposes that a PostgreSQL database is
  69. /// available and that the Kea schema has been created within it.
  70. class PgSqlLeaseMgr : public LeaseMgr {
  71. public:
  72. /// @brief Constructor
  73. ///
  74. /// Uses the following keywords in the parameters passed to it to
  75. /// connect to the database:
  76. /// - name - Name of the database to which to connect (mandatory)
  77. /// - host - Host to which to connect (optional, defaults to "localhost")
  78. /// - user - Username under which to connect (optional)
  79. /// - password - Password for "user" on the database (optional)
  80. ///
  81. /// If the database is successfully opened, the version number in the
  82. /// schema_version table will be checked against hard-coded value in
  83. /// the implementation file.
  84. ///
  85. /// Finally, all the SQL commands are pre-compiled.
  86. ///
  87. /// @param parameters A data structure relating keywords and values
  88. /// concerned with the database.
  89. ///
  90. /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given
  91. /// @throw isc::dhcp::DbOpenError Error opening the database
  92. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  93. /// failed.
  94. PgSqlLeaseMgr(const ParameterMap& parameters);
  95. /// @brief Destructor (closes database)
  96. virtual ~PgSqlLeaseMgr();
  97. /// @brief Adds an IPv4 lease
  98. ///
  99. /// @param lease lease to be added
  100. ///
  101. /// @result true if the lease was added, false if not (because a lease
  102. /// with the same address was already there).
  103. ///
  104. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  105. /// failed.
  106. virtual bool addLease(const Lease4Ptr& lease);
  107. /// @brief Adds an IPv6 lease
  108. ///
  109. /// @param lease lease to be added
  110. ///
  111. /// @result true if the lease was added, false if not (because a lease
  112. /// with the same address was already there).
  113. ///
  114. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  115. /// failed.
  116. virtual bool addLease(const Lease6Ptr& lease);
  117. /// @brief Returns an IPv4 lease for specified IPv4 address
  118. ///
  119. /// This method return a lease that is associated with a given address.
  120. /// For other query types (by hardware addr, by Client ID) there can be
  121. /// several leases in different subnets (e.g. for mobile clients that
  122. /// got address in different subnets). However, for a single address
  123. /// there can be only one lease, so this method returns a pointer to
  124. /// a single lease, not a container of leases.
  125. ///
  126. /// @param addr address of the searched lease
  127. ///
  128. /// @return smart pointer to the lease (or NULL if a lease is not found)
  129. ///
  130. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  131. /// fit into the space allocated for the result. This indicates a
  132. /// programming error.
  133. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  134. /// failed.
  135. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
  136. /// @brief Returns existing IPv4 leases for specified hardware address.
  137. ///
  138. /// Although in the usual case there will be only one lease, for mobile
  139. /// clients or clients with multiple static/fixed/reserved leases there
  140. /// can be more than one. Thus return type is a container, not a single
  141. /// pointer.
  142. ///
  143. /// @param hwaddr hardware address of the client
  144. ///
  145. /// @return lease collection
  146. ///
  147. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  148. /// fit into the space allocated for the result. This indicates a
  149. /// programming error.
  150. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  151. /// failed.
  152. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
  153. /// @brief Returns existing IPv4 leases for specified hardware address
  154. /// and a subnet
  155. ///
  156. /// There can be at most one lease for a given HW address in a single
  157. /// pool, so this method with either return a single lease or NULL.
  158. ///
  159. /// @param hwaddr hardware address of the client
  160. /// @param subnet_id identifier of the subnet that lease must belong to
  161. ///
  162. /// @return a pointer to the lease (or NULL if a lease is not found)
  163. ///
  164. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  165. /// fit into the space allocated for the result. This indicates a
  166. /// programming error.
  167. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  168. /// failed.
  169. virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
  170. SubnetID subnet_id) const;
  171. /// @brief Returns existing IPv4 leases for specified client-id
  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 clientid client identifier
  179. ///
  180. /// @return lease collection
  181. ///
  182. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  183. /// fit into the space allocated for the result. This indicates a
  184. /// programming error.
  185. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  186. /// failed.
  187. virtual Lease4Collection getLease4(const ClientId& clientid) const;
  188. /// @brief Returns IPv4 lease for the specified client identifier, HW
  189. /// address and subnet identifier.
  190. ///
  191. /// @param client_id A client identifier.
  192. /// @param hwaddr Hardware address.
  193. /// @param subnet_id A subnet identifier.
  194. ///
  195. /// @return A pointer to the lease or NULL if the lease is not found.
  196. /// @throw isc::NotImplemented On every call as this function is currently
  197. /// not implemented for the MySQL backend.
  198. virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr,
  199. SubnetID subnet_id) const;
  200. /// @brief Returns existing IPv4 lease for specified client-id
  201. ///
  202. /// There can be at most one lease for a given HW address in a single
  203. /// pool, so this method with either return a single lease or NULL.
  204. ///
  205. /// @param clientid client identifier
  206. /// @param subnet_id identifier of the subnet that lease must belong to
  207. ///
  208. /// @return a pointer to the lease (or NULL if a lease is not found)
  209. ///
  210. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  211. /// failed.
  212. virtual Lease4Ptr getLease4(const ClientId& clientid,
  213. SubnetID subnet_id) const;
  214. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  215. ///
  216. /// For a given address, we assume that there will be only one lease.
  217. /// The assumption here is that there will not be site or link-local
  218. /// addresses used, so there is no way of having address duplication.
  219. ///
  220. /// @param type specifies lease type: (NA, TA or PD)
  221. /// @param addr address of the searched lease
  222. ///
  223. /// @return smart pointer to the lease (or NULL if a lease is not found)
  224. ///
  225. /// @throw isc::BadValue record retrieved from database had an invalid
  226. /// lease type field.
  227. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  228. /// failed.
  229. virtual Lease6Ptr getLease6(Lease::Type type,
  230. const isc::asiolink::IOAddress& addr) const;
  231. /// @brief Returns existing IPv6 leases for a given DUID+IA combination
  232. ///
  233. /// Although in the usual case there will be only one lease, for mobile
  234. /// clients or clients with multiple static/fixed/reserved leases there
  235. /// can be more than one. Thus return type is a container, not a single
  236. /// pointer.
  237. ///
  238. /// @param type specifies lease type: (NA, TA or PD)
  239. /// @param duid client DUID
  240. /// @param iaid IA identifier
  241. ///
  242. /// @return smart pointer to the lease (or NULL if a lease is not found)
  243. ///
  244. /// @throw isc::BadValue record retrieved from database had an invalid
  245. /// lease type field.
  246. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  247. /// failed.
  248. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  249. uint32_t iaid) const;
  250. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  251. ///
  252. /// @param type specifies lease type: (NA, TA or PD)
  253. /// @param duid client DUID
  254. /// @param iaid IA identifier
  255. /// @param subnet_id subnet id of the subnet the lease belongs to
  256. ///
  257. /// @return lease collection (may be empty if no lease is found)
  258. ///
  259. /// @throw isc::BadValue record retrieved from database had an invalid
  260. /// lease type field.
  261. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  262. /// failed.
  263. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  264. uint32_t iaid, SubnetID subnet_id) const;
  265. /// @brief Updates IPv4 lease.
  266. ///
  267. /// Updates the record of the lease in the database (as identified by the
  268. /// address) with the data in the passed lease object.
  269. ///
  270. /// @param lease4 The lease to be updated.
  271. ///
  272. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  273. /// exist.
  274. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  275. /// failed.
  276. virtual void updateLease4(const Lease4Ptr& lease4);
  277. /// @brief Updates IPv6 lease.
  278. ///
  279. /// Updates the record of the lease in the database (as identified by the
  280. /// address) with the data in the passed lease object.
  281. ///
  282. /// @param lease6 The lease to be updated.
  283. ///
  284. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  285. /// exist.
  286. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  287. /// failed.
  288. virtual void updateLease6(const Lease6Ptr& lease6);
  289. /// @brief Deletes a lease.
  290. ///
  291. /// @param addr Address of the lease to be deleted. This can be an IPv4
  292. /// address or an IPv6 address.
  293. ///
  294. /// @return true if deletion was successful, false if no such lease exists
  295. ///
  296. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  297. /// failed.
  298. virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
  299. /// @brief Return backend type
  300. ///
  301. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  302. ///
  303. /// @return Type of the backend.
  304. virtual std::string getType() const {
  305. return (std::string("postgresql"));
  306. }
  307. /// @brief Returns name of the database.
  308. ///
  309. /// @return database name
  310. virtual std::string getName() const;
  311. /// @brief Returns description of the backend.
  312. ///
  313. /// This description may be multiline text that describes the backend.
  314. ///
  315. /// @return Description of the backend.
  316. virtual std::string getDescription() const;
  317. /// @brief Returns backend version.
  318. ///
  319. /// @return Version number as a pair of unsigned integers. "first" is the
  320. /// major version number, "second" the minor number.
  321. ///
  322. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  323. /// failed.
  324. virtual std::pair<uint32_t, uint32_t> getVersion() const;
  325. /// @brief Commit Transactions
  326. ///
  327. /// Commits all pending database operations. On databases that don't
  328. /// support transactions, this is a no-op.
  329. ///
  330. /// @throw DbOperationError Iif the commit failed.
  331. virtual void commit();
  332. /// @brief Rollback Transactions
  333. ///
  334. /// Rolls back all pending database operations. On databases that don't
  335. /// support transactions, this is a no-op.
  336. ///
  337. /// @throw DbOperationError If the rollback failed.
  338. virtual void rollback();
  339. /// @brief Statement Tags
  340. ///
  341. /// The contents of the enum are indexes into the list of compiled SQL statements
  342. enum StatementIndex {
  343. DELETE_LEASE4, // Delete from lease4 by address
  344. DELETE_LEASE6, // Delete from lease6 by address
  345. GET_LEASE4_ADDR, // Get lease4 by address
  346. GET_LEASE4_CLIENTID, // Get lease4 by client ID
  347. GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
  348. GET_LEASE4_HWADDR, // Get lease4 by HW address
  349. GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
  350. GET_LEASE6_ADDR, // Get lease6 by address
  351. GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
  352. GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
  353. GET_VERSION, // Obtain version number
  354. INSERT_LEASE4, // Add entry to lease4 table
  355. INSERT_LEASE6, // Add entry to lease6 table
  356. UPDATE_LEASE4, // Update a Lease4 entry
  357. UPDATE_LEASE6, // Update a Lease6 entry
  358. NUM_STATEMENTS // Number of statements
  359. };
  360. private:
  361. /// @brief Prepare statements
  362. ///
  363. /// Creates the prepared statements for all of the SQL statements used
  364. /// by the PostgreSQL backend.
  365. ///
  366. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  367. /// failed.
  368. /// @throw isc::InvalidParameter 'index' is not valid for the vector. This
  369. /// represents an internal error within the code.
  370. void prepareStatements();
  371. /// @brief Open Database
  372. ///
  373. /// Opens the database using the information supplied in the parameters
  374. /// passed to the constructor.
  375. ///
  376. /// @throw NoDatabaseName Mandatory database name not given
  377. /// @throw DbOpenError Error opening the database
  378. void openDatabase();
  379. /// @brief Add Lease Common Code
  380. ///
  381. /// This method performs the common actions for both flavours (V4 and V6)
  382. /// of the addLease method. It binds the contents of the lease object to
  383. /// the prepared statement and adds it to the database.
  384. ///
  385. /// @param stindex Index of statemnent being executed
  386. /// @param bind MYSQL_BIND array that has been created for the type
  387. /// of lease in question.
  388. ///
  389. /// @return true if the lease was added, false if it was not added because
  390. /// a lease with that address already exists in the database.
  391. ///
  392. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  393. /// failed.
  394. bool addLeaseCommon(StatementIndex stindex, BindParams& params);
  395. /// @brief Get Lease Collection Common Code
  396. ///
  397. /// This method performs the common actions for obtaining multiple leases
  398. /// from the database.
  399. ///
  400. /// @param stindex Index of statement being executed
  401. /// @param params PostgreSQL parameters for the query
  402. /// @param exchange Exchange object to use
  403. /// @param result Returned collection of Leases Note that any leases in
  404. /// the collection when this method is called are not erased: the
  405. /// new data is appended to the end.
  406. /// @param single If true, only a single data item is to be retrieved.
  407. /// If more than one is present, a MultipleRecords exception will
  408. /// be thrown.
  409. ///
  410. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  411. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  412. /// failed.
  413. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  414. /// from the database where only one was expected.
  415. template <typename Exchange, typename LeaseCollection>
  416. void getLeaseCollection(StatementIndex stindex, BindParams& params,
  417. Exchange& exchange, LeaseCollection& result,
  418. bool single = false) const;
  419. /// @brief Gets Lease4 Collection
  420. ///
  421. /// Gets a collection of Lease4 objects. This is just an interface to
  422. /// the get lease collection common code.
  423. ///
  424. /// @param stindex Index of statement being executed
  425. /// @param params PostgreSQL parameters for the query
  426. /// @param lease LeaseCollection object returned. Note that any leases in
  427. /// the collection when this method is called are not erased: the
  428. /// new data is appended to the end.
  429. ///
  430. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  431. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  432. /// failed.
  433. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  434. /// from the database where only one was expected.
  435. void getLeaseCollection(StatementIndex stindex, BindParams& params,
  436. Lease4Collection& result) const {
  437. getLeaseCollection(stindex, params, exchange4_, result);
  438. }
  439. /// @brief Get Lease6 Collection
  440. ///
  441. /// Gets a collection of Lease6 objects. This is just an interface to
  442. /// the get lease collection common code.
  443. ///
  444. /// @param stindex Index of statement being executed
  445. /// @param params PostgreSQL parameters for the query
  446. /// @param lease LeaseCollection object returned. Note that any existing
  447. /// data in the collection is erased first.
  448. ///
  449. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  450. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  451. /// failed.
  452. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  453. /// from the database where only one was expected.
  454. void getLeaseCollection(StatementIndex stindex, BindParams& params,
  455. Lease6Collection& result) const {
  456. getLeaseCollection(stindex, params, exchange6_, result);
  457. }
  458. /// @brief Checks result of the r object
  459. ///
  460. /// Checks status of the operation passed as first argument and throws
  461. /// DbOperationError with details if it is non-success.
  462. ///
  463. /// @param r result of the last PostgreSQL operation
  464. /// @param index will be used to print out compiled statement name
  465. ///
  466. /// @throw isc::dhcp::DbOperationError Detailed PostgreSQL failure
  467. inline void checkStatementError(PGresult* r, StatementIndex index) const;
  468. /// @brief Converts query parameters to format accepted by PostgreSQL
  469. ///
  470. /// Converts parameters stored in params into 3 vectors: out_params,
  471. /// out_lengths and out_formats.
  472. /// @param params input parameters
  473. /// @param out_values [out] values of specified parameters
  474. /// @param out_lengths [out] lengths of specified values
  475. /// @param out_formats [out] specifies format (text (0) or binary (1))
  476. inline void convertToQuery(const BindParams& params,
  477. std::vector<const char *>& out_values,
  478. std::vector<int>& out_lengths,
  479. std::vector<int>& out_formats) const;
  480. /// @brief Get Lease4 Common Code
  481. ///
  482. /// This method performs the common actions for the various getLease4()
  483. /// methods. It acts as an interface to the getLeaseCollection() method,
  484. /// but retrieveing only a single lease.
  485. ///
  486. /// @param stindex Index of statement being executed
  487. /// @param BindParams PostgreSQL array for input parameters
  488. /// @param lease Lease4 object returned
  489. void getLease(StatementIndex stindex, BindParams& params,
  490. Lease4Ptr& result) const;
  491. /// @brief Get Lease6 Common Code
  492. ///
  493. /// This method performs the common actions for the various getLease4()
  494. /// methods. It acts as an interface to the getLeaseCollection() method,
  495. /// but retrieveing only a single lease.
  496. ///
  497. /// @param stindex Index of statement being executed
  498. /// @param BindParams PostgreSQL array for input parameters
  499. /// @param lease Lease6 object returned
  500. void getLease(StatementIndex stindex, BindParams& params,
  501. Lease6Ptr& result) const;
  502. /// @brief Update lease common code
  503. ///
  504. /// Holds the common code for updating a lease. It binds the parameters
  505. /// to the prepared statement, executes it, then checks how many rows
  506. /// were affected.
  507. ///
  508. /// @param stindex Index of prepared statement to be executed
  509. /// @param BindParams Array of PostgreSQL objects representing the parameters.
  510. /// (Note that the number is determined by the number of parameters
  511. /// in the statement.)
  512. /// @param lease Pointer to the lease object whose record is being updated.
  513. ///
  514. /// @throw NoSuchLease Could not update a lease because no lease matches
  515. /// the address given.
  516. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  517. /// failed.
  518. template <typename LeasePtr>
  519. void updateLeaseCommon(StatementIndex stindex, BindParams& params,
  520. const LeasePtr& lease);
  521. /// @brief Delete lease common code
  522. ///
  523. /// Holds the common code for deleting a lease. It binds the parameters
  524. /// to the prepared statement, executes the statement and checks to
  525. /// see how many rows were deleted.
  526. ///
  527. /// @param stindex Index of prepared statement to be executed
  528. /// @param BindParams Array of PostgreSQL objects representing the parameters.
  529. /// (Note that the number is determined by the number of parameters
  530. /// in the statement.)
  531. ///
  532. /// @return true if one or more rows were deleted, false if none were
  533. /// deleted.
  534. ///
  535. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  536. /// failed.
  537. bool deleteLeaseCommon(StatementIndex stindex, BindParams& params);
  538. /// The exchange objects are used for transfer of data to/from the database.
  539. /// They are pointed-to objects as the contents may change in "const" calls,
  540. /// while the rest of this object does not. (At alternative would be to
  541. /// declare them as "mutable".)
  542. boost::scoped_ptr<PgSqlLease4Exchange> exchange4_; ///< Exchange object
  543. boost::scoped_ptr<PgSqlLease6Exchange> exchange6_; ///< Exchange object
  544. /// A vector of compiled SQL statements
  545. std::vector<PgSqlStatementBind> statements_;
  546. /// PostgreSQL connection handle
  547. PGconn* conn_;
  548. };
  549. }; // end of isc::dhcp namespace
  550. }; // end of isc namespace
  551. #endif // PGSQL_LEASE_MGR_H