pgsql_lease_mgr.h 26 KB

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