CoreDbCXXUsage.html   [plain text]


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Database Example</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
    <link rel="home" href="index.html" title="Getting Started with Berkeley DB" />
    <link rel="up" href="DB.html" title="Chapter 2. Databases" />
    <link rel="previous" href="CoreEnvUsage.html" title="Managing Databases in Environments" />
    <link rel="next" href="DBEntry.html" title="Chapter 3. Database Records" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Database Example</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a> </td>
          <th width="60%" align="center">Chapter 2. Databases</th>
          <td width="20%" align="right"> <a accesskey="n" href="DBEntry.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="sect1" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a id="CoreDbCXXUsage"></a>Database Example</h2>
          </div>
        </div>
        <div></div>
      </div>
      <p>
        Throughout this book we will build a couple of applications that load
        and retrieve inventory data from DB databases. While we are not yet ready to
        begin reading from or writing to our databases, we can at least create
        the class that we will use to manage our databases.
    </p>
      <p>
        Note that subsequent examples in this book will build on this code to
        perform the more interesting work of writing to and reading from the
        databases. 
    </p>
      <p>
        Note that you can find the complete implementation of these functions
        in:
    </p>
      <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_cxx/getting_started</pre>
      <p>
        where <tt class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></tt> is the location where you
        placed your DB distribution.  
    </p>
      <div class="example">
        <a id="MyDb-cxx"></a>
        <p class="title">
          <b>Example 2.1 MyDb Class</b>
        </p>
        <p>
            To manage our database open and close activities, we encapsulate them
            in the <tt class="classname">MyDb</tt> class. There are several good reasons
            to do this, the most important being that we can ensure our databases are
            closed by putting that activity in the <tt class="classname">MyDb</tt>
            class destructor.
        </p>
        <p>
            To begin, we create our class definition:
        </p>
        <a id="cxx_db11"></a>
        <pre class="programlisting">// File: MyDb.hpp
#include &lt;db_cxx.h&gt;
                                                                                                                                    
class MyDb
{
public:
    // Constructor requires a path to the database,
    // and a database name.
    MyDb(std::string &amp;path, std::string &amp;dbName);
                                                                                                                                    
    // Our destructor just calls our private close method.
    ~MyDb() { close(); }
                                                                                                                                    
    inline Db &amp;getDb() {return db_;}
                                                                                                                                    
private:
    Db db_;
    std::string dbFileName_;
    u_int32_t cFlags_;
                                                                                                                                    
    // Make sure the default constructor is private
    // We don't want it used.
    MyDb() : db_(NULL, 0) {}
                                                                                                                                    
    // We put our database close activity here.
    // This is called from our destructor. In
    // a more complicated example, we might want
    // to make this method public, but a private
    // method is more appropriate for this example.
    void close();
}; </pre>
        <p>
        Next we need the implementation for the constructor:
    </p>
        <a id="cxx_db12"></a>
        <pre class="programlisting">// File: MyDb.cpp
#include "MyDb.hpp"

// Class constructor. Requires a path to the location
// where the database is located, and a database name
MyDb::MyDb(std::string &amp;path, std::string &amp;dbName)
    : db_(NULL, 0),               // Instantiate Db object
      dbFileName_(path + dbName), // Database file name
      cFlags_(DB_CREATE)          // If the database doesn't yet exist,
                                  // allow it to be created.
{
    try
    {
        // Redirect debugging information to std::cerr
        db_.set_error_stream(&amp;std::cerr);
                                                                                                                                    
        // Open the database
        db_.open(NULL, dbFileName_.c_str(), NULL, DB_BTREE, cFlags_, 0);
    }
    // DbException is not a subclass of std::exception, so we
    // need to catch them both.
    catch(DbException &amp;e)
    {
        std::cerr &lt;&lt; "Error opening database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
    }
    catch(std::exception &amp;e)
    {
        std::cerr &lt;&lt; "Error opening database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
    }
}</pre>
        <p>
    And then we need the implementation for the 
   <tt class="methodname">close()</tt> method:
   
</p>
        <a id="cxx_db12.1"></a>
        <pre class="programlisting">// Private member used to close a database. Called from the class
// destructor.
void
MyDb::close()
{
    // Close the db
    try
    {
        db_.close(0);
        std::cout &lt;&lt; "Database " &lt;&lt; dbFileName_
                  &lt;&lt; " is closed." &lt;&lt; std::endl;
    }
    catch(DbException &amp;e)
    {
        std::cerr &lt;&lt; "Error closing database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
    }
    catch(std::exception &amp;e)
    {
        std::cerr &lt;&lt; "Error closing database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
    }
} </pre>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="DB.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="DBEntry.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">Managing Databases in Environments </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> Chapter 3. Database Records</td>
        </tr>
      </table>
    </div>
  </body>
</html>