Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / docs / gsg / CXX / CoreDbCXXUsage.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4   <head>
5     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6     <title>Database Example</title>
7     <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8     <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9     <link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
10     <link rel="up" href="databases.html" title="Chapter 2. Databases" />
11     <link rel="prev" href="CoreEnvUsage.html" title="Managing Databases in Environments" />
12     <link rel="next" href="DBEntry.html" title="Chapter 3. Database Records" />
13   </head>
14   <body>
15     <div xmlns="" class="navheader">
16       <div class="libver">
17         <p>Library Version 11.2.5.3</p>
18       </div>
19       <table width="100%" summary="Navigation header">
20         <tr>
21           <th colspan="3" align="center">Database Example</th>
22         </tr>
23         <tr>
24           <td width="20%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a> </td>
25           <th width="60%" align="center">Chapter 2. Databases</th>
26           <td width="20%" align="right"> <a accesskey="n" href="DBEntry.html">Next</a></td>
27         </tr>
28       </table>
29       <hr />
30     </div>
31     <div class="sect1" lang="en" xml:lang="en">
32       <div class="titlepage">
33         <div>
34           <div>
35             <h2 class="title" style="clear: both"><a id="CoreDbCXXUsage"></a>Database Example</h2>
36           </div>
37         </div>
38       </div>
39       <p>
40         Throughout this book we will build a couple of applications that load
41         and retrieve inventory data from DB databases. While we are not yet ready to
42         begin reading from or writing to our databases, we can at least create
43         the class that we will use to manage our databases.
44     </p>
45       <p>
46         Note that subsequent examples in this book will build on this code to
47         perform the more interesting work of writing to and reading from the
48         databases. 
49     </p>
50       <p>
51         Note that you can find the complete implementation of these functions
52         in:
53     </p>
54       <pre class="programlisting"><span class="emphasis"><em>DB_INSTALL</em></span>/examples_cxx/getting_started</pre>
55       <p>
56         where <code class="literal"><span class="emphasis"><em>DB_INSTALL</em></span></code> is the location where you
57         placed your DB distribution.  
58     </p>
59       <div class="example">
60         <a id="MyDb-cxx"></a>
61         <p class="title">
62           <b>Example 2.1 MyDb Class</b>
63         </p>
64         <div class="example-contents">
65           <p>
66             To manage our database open and close activities, we encapsulate them
67             in the <code class="classname">MyDb</code> class. There are several good reasons
68             to do this, the most important being that we can ensure our databases are
69             closed by putting that activity in the <code class="classname">MyDb</code>
70             class destructor.
71         </p>
72           <p>
73             To begin, we create our class definition:
74         </p>
75           <a id="cxx_db11"></a>
76           <pre class="programlisting">// File: MyDb.hpp
77 #include &lt;db_cxx.h&gt;
78
79 class MyDb
80 {
81 public:
82     // Constructor requires a path to the database,
83     // and a database name.
84     MyDb(std::string &amp;path, std::string &amp;dbName);
85
86     // Our destructor just calls our private close method.
87     ~MyDb() { close(); }
88
89     inline Db &amp;getDb() {return db_;}
90
91 private:
92     Db db_;
93     std::string dbFileName_;
94     u_int32_t cFlags_;
95
96     // Make sure the default constructor is private
97     // We don't want it used.
98     MyDb() : db_(NULL, 0) {}
99
100     // We put our database close activity here.
101     // This is called from our destructor. In
102     // a more complicated example, we might want
103     // to make this method public, but a private
104     // method is more appropriate for this example.
105     void close();
106 }; </pre>
107           <p>
108         Next we need the implementation for the constructor:
109     </p>
110           <a id="cxx_db12"></a>
111           <pre class="programlisting">// File: MyDb.cpp
112 #include "MyDb.hpp"
113
114 // Class constructor. Requires a path to the location
115 // where the database is located, and a database name
116 MyDb::MyDb(std::string &amp;path, std::string &amp;dbName)
117     : db_(NULL, 0),               // Instantiate Db object
118       dbFileName_(path + dbName), // Database file name
119       cFlags_(DB_CREATE)          // If the database doesn't yet exist,
120                                   // allow it to be created.
121 {
122     try
123     {
124         // Redirect debugging information to std::cerr
125         db_.set_error_stream(&amp;std::cerr);
126
127         // Open the database
128         db_.open(NULL, dbFileName_.c_str(), NULL, DB_BTREE, cFlags_, 0);
129     }
130     // DbException is not a subclass of std::exception, so we
131     // need to catch them both.
132     catch(DbException &amp;e)
133     {
134         std::cerr &lt;&lt; "Error opening database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
135         std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
136     }
137     catch(std::exception &amp;e)
138     {
139         std::cerr &lt;&lt; "Error opening database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
140         std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
141     }
142 }</pre>
143           <p>
144     And then we need the implementation for the 
145    <code class="methodname">close()</code> method:
146    
147 </p>
148           <a id="cxx_db12.1"></a>
149           <pre class="programlisting">// Private member used to close a database. Called from the class
150 // destructor.
151 void
152 MyDb::close()
153 {
154     // Close the db
155     try
156     {
157         db_.close(0);
158         std::cout &lt;&lt; "Database " &lt;&lt; dbFileName_
159                   &lt;&lt; " is closed." &lt;&lt; std::endl;
160     }
161     catch(DbException &amp;e)
162     {
163         std::cerr &lt;&lt; "Error closing database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
164         std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
165     }
166     catch(std::exception &amp;e)
167     {
168         std::cerr &lt;&lt; "Error closing database: " &lt;&lt; dbFileName_ &lt;&lt; "\n";
169         std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
170     }
171 } </pre>
172         </div>
173       </div>
174       <br class="example-break" />
175     </div>
176     <div class="navfooter">
177       <hr />
178       <table width="100%" summary="Navigation footer">
179         <tr>
180           <td width="40%" align="left"><a accesskey="p" href="CoreEnvUsage.html">Prev</a> </td>
181           <td width="20%" align="center">
182             <a accesskey="u" href="databases.html">Up</a>
183           </td>
184           <td width="40%" align="right"> <a accesskey="n" href="DBEntry.html">Next</a></td>
185         </tr>
186         <tr>
187           <td width="40%" align="left" valign="top">Managing Databases in Environments </td>
188           <td width="20%" align="center">
189             <a accesskey="h" href="index.html">Home</a>
190           </td>
191           <td width="40%" align="right" valign="top"> Chapter 3. Database Records</td>
192         </tr>
193       </table>
194     </div>
195   </body>
196 </html>