mysql_lease_mgr.h 27.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15 16
#ifndef MYSQL_LEASE_MGR_H
#define MYSQL_LEASE_MGR_H
17

18 19
#include <dhcpsrv/lease_mgr.h>

20
#include <boost/scoped_ptr.hpp>
21
#include <mysql/mysql.h>
22 23

#include <time.h>
24 25 26 27

namespace isc {
namespace dhcp {

28 29
// Define the current database schema values

30 31
const uint32_t CURRENT_VERSION_VERSION = 1;
const uint32_t CURRENT_VERSION_MINOR = 0;
32

33

34
// Forward declaration of the Lease exchange objects.  These classes are defined
35
// in the .cc file.
36
class MySqlLease4Exchange;
37 38 39
class MySqlLease6Exchange;


40
/// @brief MySQL Lease Manager
41
///
42 43 44 45
/// This class provides the \ref isc::dhcp::LeaseMgr interface to the MySQL
/// database.  Use of this backend presupposes that a MySQL database is
/// available and that the Kea schema has been created within it.

46 47 48 49 50 51
class MySqlLeaseMgr : public LeaseMgr {
public:
    /// @brief Constructor
    ///
    /// Uses the following keywords in the parameters passed to it to
    /// connect to the database:
52 53 54 55
    /// - name - Name of the database to which to connect (mandatory)
    /// - host - Host to which to connect (optional, defaults to "localhost")
    /// - user - Username under which to connect (optional)
    /// - password - Password for "user" on the database (optional)
56 57 58 59 60 61 62 63 64
    ///
    /// If the database is successfully opened, the version number in the
    /// schema_version table will be checked against hard-coded value in
    /// the implementation file.
    ///
    /// Finally, all the SQL commands are pre-compiled.
    ///
    /// @param parameters A data structure relating keywords and values
    ///        concerned with the database.
65
    ///
66 67 68 69
    /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given
    /// @throw isc::dhcp::DbOpenError Error opening the database
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
70 71 72 73 74
    MySqlLeaseMgr(const ParameterMap& parameters);

    /// @brief Destructor (closes database)
    virtual ~MySqlLeaseMgr();

75
    /// @brief Adds an IPv4 lease
76 77
    ///
    /// @param lease lease to be added
78 79 80 81
    ///
    /// @result true if the lease was added, false if not (because a lease
    ///         with the same address was already there).
    ///
82 83
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
84
    virtual bool addLease(const Lease4Ptr& lease);
85

86
    /// @brief Adds an IPv6 lease
87 88
    ///
    /// @param lease lease to be added
89 90 91 92
    ///
    /// @result true if the lease was added, false if not (because a lease
    ///         with the same address was already there).
    ///
93 94
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
95
    virtual bool addLease(const Lease6Ptr& lease);
96

97
    /// @brief Return IPv4 lease for specified IPv4 address and subnet_id
98 99 100 101 102
    ///
    /// This method is used to get a lease for specific subnet_id. There can be
    /// at most one lease for any given subnet, so this method returns a single
    /// pointer.
    ///
103
    /// @param addr address of the sought lease
104 105 106
    /// @param subnet_id ID of the subnet the lease must belong to
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
107
    virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr,
108
                                SubnetID subnet_id) const;
109 110 111 112

    /// @brief Returns an IPv4 lease for specified IPv4 address
    ///
    /// This method return a lease that is associated with a given address.
113
    /// For other query types (by hardware addr, by Client ID) there can be
114 115 116 117 118 119 120 121
    /// several leases in different subnets (e.g. for mobile clients that
    /// got address in different subnets). However, for a single address
    /// there can be only one lease, so this method returns a pointer to
    /// a single lease, not a container of leases.
    ///
    /// @param addr address of the searched lease
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
122 123 124 125 126 127
    ///
    /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
    ///        fit into the space allocated for the result.  This indicates a
    ///        programming error.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
128
    virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
129

130

131 132 133 134 135 136 137 138 139 140
    /// @brief Returns existing IPv4 leases for specified hardware address.
    ///
    /// Although in the usual case there will be only one lease, for mobile
    /// clients or clients with multiple static/fixed/reserved leases there
    /// can be more than one. Thus return type is a container, not a single
    /// pointer.
    ///
    /// @param hwaddr hardware address of the client
    ///
    /// @return lease collection
141 142 143 144 145 146
    ///
    /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
    ///        fit into the space allocated for the result.  This indicates a
    ///        programming error.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
147
    virtual Lease4Collection getLease4(const HWAddr& hwaddr) const;
148 149 150 151 152 153 154 155 156 157 158

    /// @brief Returns existing IPv4 leases for specified hardware address
    ///        and a subnet
    ///
    /// There can be at most one lease for a given HW address in a single
    /// pool, so this method with either return a single lease or NULL.
    ///
    /// @param hwaddr hardware address of the client
    /// @param subnet_id identifier of the subnet that lease must belong to
    ///
    /// @return a pointer to the lease (or NULL if a lease is not found)
159 160 161 162 163 164
    ///
    /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
    ///        fit into the space allocated for the result.  This indicates a
    ///        programming error.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
165
    virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
166
                                SubnetID subnet_id) const;
167 168 169 170 171 172 173 174 175 176 177

    /// @brief Returns existing IPv4 lease for specified client-id
    ///
    /// Although in the usual case there will be only one lease, for mobile
    /// clients or clients with multiple static/fixed/reserved leases there
    /// can be more than one. Thus return type is a container, not a single
    /// pointer.
    ///
    /// @param clientid client identifier
    ///
    /// @return lease collection
178 179 180 181 182 183
    ///
    /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
    ///        fit into the space allocated for the result.  This indicates a
    ///        programming error.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
184
    virtual Lease4Collection getLease4(const ClientId& clientid) const;
185 186 187 188 189 190 191 192 193 194

    /// @brief Returns existing IPv4 lease for specified client-id
    ///
    /// There can be at most one lease for a given HW address in a single
    /// pool, so this method with either return a single lease or NULL.
    ///
    /// @param clientid client identifier
    /// @param subnet_id identifier of the subnet that lease must belong to
    ///
    /// @return a pointer to the lease (or NULL if a lease is not found)
195 196 197 198 199 200
    ///
    /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
    ///        fit into the space allocated for the result.  This indicates a
    ///        programming error.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
201
    virtual Lease4Ptr getLease4(const ClientId& clientid,
202
                                SubnetID subnet_id) const;
203 204 205 206 207 208 209 210 211 212

    /// @brief Returns existing IPv6 lease for a given IPv6 address.
    ///
    /// For a given address, we assume that there will be only one lease.
    /// The assumtion here is that there will not be site or link-local
    /// addresses used, so there is no way of having address duplication.
    ///
    /// @param addr address of the searched lease
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
213
    ///
214 215
    /// @throw isc::BadValue record retrieved from database had an invalid
    ///        lease type field.
216 217 218
    /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
    ///        fit into the space allocated for the result.  This indicates a
    ///        programming error.
219 220
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
221
    virtual Lease6Ptr getLease6(const isc::asiolink::IOAddress& addr) const;
222 223 224 225 226 227 228 229 230 231 232 233

    /// @brief Returns existing IPv6 leases for a given DUID+IA combination
    ///
    /// Although in the usual case there will be only one lease, for mobile
    /// clients or clients with multiple static/fixed/reserved leases there
    /// can be more than one. Thus return type is a container, not a single
    /// pointer.
    ///
    /// @param duid client DUID
    /// @param iaid IA identifier
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
234 235 236 237 238 239 240 241
    ///
    /// @throw isc::BadValue record retrieved from database had an invalid
    ///        lease type field.
    /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
    ///        fit into the space allocated for the result.  This indicates a
    ///        programming error.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
242
    virtual Lease6Collection getLease6(const DUID& duid,
243
                                       uint32_t iaid) const;
244 245 246 247 248 249 250 251

    /// @brief Returns existing IPv6 lease for a given DUID+IA combination
    ///
    /// @param duid client DUID
    /// @param iaid IA identifier
    /// @param subnet_id subnet id of the subnet the lease belongs to
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
252 253 254 255 256 257 258 259
    ///
    /// @throw isc::BadValue record retrieved from database had an invalid
    ///        lease type field.
    /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
    ///        fit into the space allocated for the result.  This indicates a
    ///        programming error.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
260
    virtual Lease6Ptr getLease6(const DUID& duid, uint32_t iaid,
261
                                SubnetID subnet_id) const;
262 263 264

    /// @brief Updates IPv4 lease.
    ///
265 266 267
    /// Updates the record of the lease in the database (as identified by the
    /// address) with the data in the passed lease object.
    ///
268 269
    /// @param lease4 The lease to be updated.
    ///
270 271 272 273
    /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
    ///        exist.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
274
    virtual void updateLease4(const Lease4Ptr& lease4);
275

276
    /// @brief Updates IPv6 lease.
277
    ///
278 279 280
    /// Updates the record of the lease in the database (as identified by the
    /// address) with the data in the passed lease object.
    ///
281
    /// @param lease6 The lease to be updated.
282
    ///
283 284 285 286
    /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
    ///        exist.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
287
    virtual void updateLease6(const Lease6Ptr& lease6);
288

289
    /// @brief Deletes an IPv4 lease.
290
    ///
291 292 293 294
    /// @todo Merge with deleteLease6: it is possible to determine whether
    ///       an address is V4 or V6 from the IOAddress argument, so there
    ///       is no need for separate V4 or V6 methods.
    ///
295 296 297
    /// @param addr IPv4 address of the lease to be deleted.
    ///
    /// @return true if deletion was successful, false if no such lease exists
298 299 300
    ///
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
301
    virtual bool deleteLease4(const isc::asiolink::IOAddress& addr);
302

303
    /// @brief Deletes an IPv6 lease.
304
    ///
305 306 307 308
    /// @todo Merge with deleteLease4: it is possible to determine whether
    ///       an address is V4 or V6 from the IOAddress argument, so there
    ///       is no need for separate V4 or V6 methods.
    ///
309
    /// @param addr IPv6 address of the lease to be deleted.
310 311
    ///
    /// @return true if deletion was successful, false if no such lease exists
312
    ///
313 314
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
315
    virtual bool deleteLease6(const isc::asiolink::IOAddress& addr);
316

317 318 319 320 321 322 323 324 325
    /// @brief Return backend type
    ///
    /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
    ///
    /// @return Type of the backend.
    virtual std::string getType() const {
        return (std::string("mysql"));
    }

326 327 328
    /// @brief Returns backend name.
    ///
    /// Each backend have specific name, e.g. "mysql" or "sqlite".
329 330
    ///
    /// @return Name of the backend.
331
    virtual std::string getName() const;
332 333 334 335

    /// @brief Returns description of the backend.
    ///
    /// This description may be multiline text that describes the backend.
336 337
    ///
    /// @return Description of the backend.
338
    virtual std::string getDescription() const;
339 340 341 342 343 344

    /// @brief Returns backend version.
    ///
    /// @return Version number as a pair of unsigned integers.  "first" is the
    ///         major version number, "second" the minor number.
    ///
345 346
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
347 348
    virtual std::pair<uint32_t, uint32_t> getVersion() const;

349 350 351 352 353
    /// @brief Commit Transactions
    ///
    /// Commits all pending database operations.  On databases that don't
    /// support transactions, this is a no-op.
    ///
354
    /// @throw DbOperationError Iif the commit failed.
355 356 357 358 359 360 361
    virtual void commit();

    /// @brief Rollback Transactions
    ///
    /// Rolls back all pending database operations.  On databases that don't
    /// support transactions, this is a no-op.
    ///
362
    /// @throw DbOperationError If the rollback failed.
363 364 365 366
    virtual void rollback();

    ///@{
    /// The following methods are used to convert between times and time
367 368 369 370 371
    /// intervals stored in the Lease object, and the times stored in the
    /// database.  The reason for the difference is because in the DHCP server,
    /// the cltt (Client Time Since Last Transmission) is the natural data; in
    /// the lease file - which may be read by the user - it is the expiry time
    /// of the lease.
372 373 374

    /// @brief Convert Lease Time to Database Times
    ///
375 376 377 378
    /// Within the DHCP servers, times are stored as client last transmit time
    /// and valid lifetime.  In the database, the information is stored as
    /// valid lifetime and "expire" (time of expiry of the lease).  They are
    /// related by the equation:
379
    ///
380
    /// - expire = client last transmit time + valid lifetime
381 382 383 384 385
    ///
    /// This method converts from the times in the lease object into times
    /// able to be added to the database.
    ///
    /// @param cltt Client last transmit time
386
    /// @param valid_lifetime Valid lifetime
387 388 389
    /// @param expire Reference to MYSQL_TIME object where the expiry time of
    ///        the lease will be put.
    static
390 391
    void convertToDatabaseTime(time_t cltt, uint32_t valid_lifetime,
                               MYSQL_TIME& expire);
392 393 394

    /// @brief Convert Database Time to Lease Times
    ///
395 396 397 398
    /// Within the database, time is stored as "expire" (time of expiry of the
    /// lease) and valid lifetime.  In the DHCP server, the information is
    /// stored client last transmit time and valid lifetime.  These are related
    /// by the equation:
399
    ///
400
    /// - client last transmit time = expire - valid_lifetime
401 402 403 404 405 406
    ///
    /// This method converts from the times in the database into times
    /// able to be inserted into the lease object.
    ///
    /// @param expire Reference to MYSQL_TIME object from where the expiry
    ///        time of the lease is taken.
407
    /// @param valid_lifetime lifetime of the lease.
408 409 410
    /// @param cltt Reference to location where client last transmit time
    ///        is put.
    static
411 412
    void convertFromDatabaseTime(const MYSQL_TIME& expire, 
                                 uint32_t valid_lifetime, time_t& cltt);
413 414
    ///@}

415
    /// @brief Statement Tags
416
    ///
417
    /// The contents of the enum are indexes into the list of SQL statements
418
    enum StatementIndex {
419
        DELETE_LEASE4,              // Delete from lease4 by address
420
        DELETE_LEASE6,              // Delete from lease6 by address
421
        GET_LEASE4_ADDR,            // Get lease4 by address
422 423
        GET_LEASE4_CLIENTID,        // Get lease4 by client ID
        GET_LEASE4_CLIENTID_SUBID,  // Get lease4 by client ID & subnet ID
424 425
        GET_LEASE4_HWADDR,          // Get lease4 by HW address
        GET_LEASE4_HWADDR_SUBID,    // Get lease4 by HW address & subnet ID
426 427
        GET_LEASE6_ADDR,            // Get lease6 by address
        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_VERSION,                // Obtain version number
430
        INSERT_LEASE4,              // Add entry to lease4 table
431
        INSERT_LEASE6,              // Add entry to lease6 table
432
        UPDATE_LEASE4,              // Update a Lease4 entry
433 434
        UPDATE_LEASE6,              // Update a Lease6 entry
        NUM_STATEMENTS              // Number of statements
435 436
    };

437
private:
438 439 440 441 442 443 444 445 446 447
    /// @brief Prepare Single Statement
    ///
    /// Creates a prepared statement from the text given and adds it to the
    /// statements_ vector at the given index.
    ///
    /// @param index Index into the statements_ vector into which the text
    ///        should be placed.  The vector must be big enough for the index
    ///        to be valid, else an exception will be thrown.
    /// @param text Text of the SQL statement to be prepared.
    ///
448 449 450
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
    /// @throw isc::InvalidParameter 'index' is not valid for the vector.
451 452 453 454 455 456
    void prepareStatement(StatementIndex index, const char* text);

    /// @brief Prepare statements
    ///
    /// Creates the prepared statements for all of the SQL statements used
    /// by the MySQL backend.
457
    ///
458 459 460 461
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
    /// @throw isc::InvalidParameter 'index' is not valid for the vector.  This
    ///        represents an internal error within the code.
462 463
    void prepareStatements();

464 465 466 467 468
    /// @brief Open Database
    ///
    /// Opens the database using the information supplied in the parameters
    /// passed to the constructor.
    ///
469 470
    /// @throw NoDatabaseName Mandatory database name not given
    /// @throw DbOpenError Error opening the database
471 472
    void openDatabase();

473 474
    /// @brief Add Lease Common Code
    ///
475 476 477
    /// This method performs the common actions for both flavours (V4 and V6)
    /// of the addLease method.  It binds the contents of the lease object to
    /// the prepared statement and adds it to the database.
478 479 480 481 482 483 484 485 486 487
    ///
    /// @param stindex Index of statemnent being executed
    /// @param bind MYSQL_BIND array that has been created for the type
    ///        of lease in question.
    ///
    /// @return true if the lease was added, false if it was not added because
    ///         a lease with that address already exists in the database.
    ///
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
488
    bool addLeaseCommon(StatementIndex stindex, std::vector<MYSQL_BIND>& bind);
489

490 491 492 493 494 495
    /// @brief Get Lease Collection Common Code
    ///
    /// This method performs the common actions for obtaining multiple leases
    /// from the database.
    ///
    /// @param stindex Index of statement being executed
496
    /// @param bind MYSQL_BIND array for input parameters
497
    /// @param exchange Exchange object to use
498 499 500
    /// @param lease LeaseCollection object returned.  Note that any leases in
    ///        the collection when this method is called are not erased: the
    ///        new data is appended to the end.
501 502 503 504 505 506 507 508 509
    /// @param single If true, only a single data item is to be retrieved.
    ///        If more than one is present, a MultipleRecords exception will
    ///        be thrown.
    ///
    /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
    /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
    ///        from the database where only one was expected.
510
    template <typename Exchange, typename LeaseCollection>
511
    void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
512 513
                            Exchange& exchange, LeaseCollection& result,
                            bool single = false) const;
514

515
    /// @brief Get Lease Collection
516
    ///
517 518
    /// Gets a collection of Lease4 objects.  This is just an interface to
    /// the get lease collection common code.
519
    ///
520
    /// @param stindex Index of statement being executed
521
    /// @param bind MYSQL_BIND array for input parameters
522 523 524
    /// @param lease LeaseCollection object returned.  Note that any leases in
    ///        the collection when this method is called are not erased: the
    ///        new data is appended to the end.
525 526 527 528 529 530
    ///
    /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
    /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
    ///        from the database where only one was expected.
531
    void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
532
                            Lease4Collection& result) const {
533
        getLeaseCollection(stindex, bind, exchange4_, result);
534 535 536 537 538 539 540 541
    }

    /// @brief Get Lease Collection
    ///
    /// Gets a collection of Lease6 objects.  This is just an interface to
    /// the get lease collection common code.
    ///
    /// @param stindex Index of statement being executed
542
    /// @param bind MYSQL_BIND array for input parameters
543 544
    /// @param lease LeaseCollection object returned.  Note that any existing
    ///        data in the collection is erased first.
545 546 547 548 549 550
    ///
    /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
    /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
    ///        from the database where only one was expected.
551
    void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
552
                            Lease6Collection& result) const {
553
        getLeaseCollection(stindex, bind, exchange6_, result);
554 555
    }

556
    /// @brief Get Lease4 Common Code
557 558 559 560 561 562
    ///
    /// This method performs the common actions for the various getLease4()
    /// methods.  It acts as an interface to the getLeaseCollection() method,
    /// but retrieveing only a single lease.
    ///
    /// @param stindex Index of statement being executed
563
    /// @param bind MYSQL_BIND array for input parameters
564
    /// @param lease Lease4 object returned
565
    void getLease(StatementIndex stindex, MYSQL_BIND* bind,
566 567
                  Lease4Ptr& result) const;

568
    /// @brief Get Lease6 Common Code
569
    ///
570
    /// This method performs the common actions for the various getLease46)
571 572 573 574
    /// methods.  It acts as an interface to the getLeaseCollection() method,
    /// but retrieveing only a single lease.
    ///
    /// @param stindex Index of statement being executed
575
    /// @param bind MYSQL_BIND array for input parameters
576
    /// @param lease Lease6 object returned
577
    void getLease(StatementIndex stindex, MYSQL_BIND* bind,
578 579 580 581 582 583 584
                   Lease6Ptr& result) const;

    /// @brief Update lease common code
    ///
    /// Holds the common code for updating a lease.  It binds the parameters
    /// to the prepared statement, executes it, then checks how many rows
    /// were affected.
585 586
    ///
    /// @param stindex Index of prepared statement to be executed
587
    /// @param bind Array of MYSQL_BIND objects representing the parameters.
588 589
    ///        (Note that the number is determined by the number of parameters
    ///        in the statement.)
590
    /// @param lease Pointer to the lease object whose record is being updated.
591
    ///
592 593
    /// @throw NoSuchLease Could not update a lease because no lease matches
    ///        the address given.
594 595
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
596
    template <typename LeasePtr>
597 598
    void updateLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind,
                           const LeasePtr& lease);
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

    /// @brief Delete lease common code
    ///
    /// Holds the common code for deleting a lease.  It binds the parameters
    /// to the prepared statement, executes the statement and checks to
    /// see how many rows were deleted.
    ///
    /// @param stindex Index of prepared statement to be executed
    /// @param bind Array of MYSQL_BIND objects representing the parameters.
    ///        (Note that the number is determined by the number of parameters
    ///        in the statement.)
    ///
    /// @return true if one or more rows were deleted, false if none were
    ///         deleted.
    ///
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
    bool deleteLease(StatementIndex stindex, MYSQL_BIND* bind);
617

618 619 620 621 622 623 624 625 626 627
    /// @brief Check Error and Throw Exception
    ///
    /// Virtually all MySQL functions return a status which, if non-zero,
    /// indicates an error.  This inline function conceals a lot of error
    /// checking/exception-throwing code.
    ///
    /// @param status Status code: non-zero implies an error
    /// @param index Index of statement that caused the error
    /// @param what High-level description of the error
    ///
628 629
    /// @throw isc::dhcp::DbOperationError An operation on the open database has
    ///        failed.
630
    inline void checkError(int status, StatementIndex index,
631 632 633
                           const char* what) const {
        if (status != 0) {
            isc_throw(DbOperationError, what << " for <" <<
634
                      text_statements_[index] << ">, reason: " <<
635 636
                      mysql_error(mysql_) << " (error code " <<
                      mysql_errno(mysql_) << ")");
637 638 639
        }
    }

640
    // Members
641

642 643 644 645
    /// The exchange objects are used for transfer of data to/from the database.
    /// They are pointed-to objects as the contents may change in "const" calls,
    /// while the rest of this object does not.  (At alternative would be to
    /// declare them as "mutable".)
646 647
    boost::scoped_ptr<MySqlLease4Exchange> exchange4_; ///< Exchange object
    boost::scoped_ptr<MySqlLease6Exchange> exchange6_; ///< Exchange object
648 649 650
    MYSQL*              mysql_;                 ///< MySQL context object
    std::vector<MYSQL_STMT*> statements_;       ///< Prepared statements
    std::vector<std::string> text_statements_;  ///< Raw text of statements
651 652 653 654 655
};

}; // end of isc::dhcp namespace
}; // end of isc namespace

656
#endif // MYSQL_LEASE_MGR_H